Example #1
0
XML *XML_Helpers::handle_rand_tag(XML *xml)
{
	std::list<XML *> &nodes = xml->get_nodes();

	std::list<XML *>::iterator it;

	int total_percent = 0;
	std::vector< std::pair<int, XML *> > values;

	for (it = nodes.begin(); it != nodes.end(); it++) {
		int percent;
		XML *x = *it;
		XML *value = x->find("value");
		if (value == 0) {
			continue;
		}
		XML *percent_xml = x->find("percent");
		if (percent_xml) {
			percent = atoi(percent_xml->get_value().c_str());
		}
		else {
			percent = 100;
		}
		total_percent += percent;
		values.push_back(std::pair<int, XML *>(percent, value));
	}

	int r = rand() % total_percent;
	int percent = 0;

	for (size_t i = 0; i < values.size(); i++) {
		std::pair<int, XML *> &p = values[i];
		percent += p.first;
		if (r < percent) {
			return *(p.second->get_nodes().begin());
		}
	}

	return 0;
}
Example #2
0
void Sprite::load(std::string xml_filename, std::string image_directory, bool absolute_path)
{
	if (absolute_path == false) {
		xml_filename = "sprites/" + xml_filename;
		image_directory = "sprites/" + image_directory;
	}

	this->xml_filename = xml_filename;
	this->image_directory = image_directory;

	XML *xml = new XML(xml_filename);

	std::list<XML *> nodes = xml->get_nodes();
	std::list<XML *>::iterator it;

	bool first = true;

	for (it = nodes.begin(); it != nodes.end(); it++) {
		XML *anim = *it;
		int count;
		std::vector<Image *> images;
		for (count = 0; count < 1024 /* NOTE: hardcoded max frames */; count++) {
			std::string filename = image_directory + "/" + anim->get_name() + itos(count) + ".tga";
			Image *image;
			try {
				image = new Image(filename, true);
			}
			catch (Error e) {
				break;
			}
			images.push_back(image);
		}
		XML *loop_xml = anim->find("loop");
		bool looping;
		if (loop_xml && loop_xml->get_value() == "false") {
			looping = false;
		}
		else {
			looping = true;
		}
		XML *delays = anim->find("delays");
		std::vector<Uint32> delays_vector;
		if (delays == 0) {
			XML *delay = anim->find("delay");
			if  (delay == 0) {
				for (int i = 0; i < count; i++) {
					delays_vector.push_back(100);
				}				
			}
			else {
				Uint32 delay_int = atoi(delay->get_value().c_str());
				for (int i = 0; i < count; i++) {
					delays_vector.push_back(delay_int);
				}
			}
		}
		else {
			for (int i = 0; i < count; i++) {
				XML *delay = delays->find(itos(i));
				if (delay == 0) {
					delays_vector.push_back(100);
				}
				else {
					delays_vector.push_back(atoi(delay->get_value().c_str()));
				}
			}
		}
		Uint32 total_delays = 0;
		for (size_t i = 0; i < delays_vector.size(); i++) {
			total_delays += delays_vector[i];
		}
		Animation *a;
		if (animations.find(anim->get_name()) == animations.end()) {
			a = new Animation();
			a->images = images;
			a->delays = delays_vector;
			a->total_delays = total_delays;
			a->rand_start = anim->find("rand_start") != 0;
			a->looping = looping;
			animations[anim->get_name()] = a;
		}
		else {
			throw Error("Duplicate animation!");
		}
		if (first) {
			first = false;
			current_animation = anim->get_name();
		}
	}

	delete xml;
}
Map_Entity::Map_Entity(std::string name) :
	name(name),
	direction(S),
	sprite(0),
	brain(0),
	position(0, 0),
	moving(false),
	speed(0.09f),
	offset(0.0f, 0.0f),
	draw_offset(0, 0),
	solid(true),
	size(noo.tile_size, noo.tile_size),
	stop_next_tile(false),
	activate_next_tile(false),
	sitting(false),
	sleeping(false),
	input_enabled(true),
	following_path(false),
	path_callback(0),
	has_blink(false),
	type(OTHER),
	stats(0),
	low(false),
	high(false),
	z(0),
	z_add(0),
	pre_sit_sleep_direction(DIRECTION_UNKNOWN),
	sit_sleep_directions(0),
	sat(false),
	should_face(true),
	path_count(0)
{
	id = current_id++;

	XML *xml = noo.miscellaneous_xml->find("blink");
	if (xml) {
		xml = xml->find(name);
		if (xml) {
			XML *eye = xml->find("eye");
			XML *blink = xml->find("blink");
			if (eye && blink) {
				XML *eye_r = eye->find("r");
				XML *eye_g = eye->find("g");
				XML *eye_b = eye->find("b");
				XML *blink_r = blink->find("r");
				XML *blink_g = blink->find("g");
				XML *blink_b = blink->find("b");
				if (eye_r && eye_g && eye_b && blink_r && blink_g && blink_b) {
					has_blink = true;
					eye_colour[0] = atoi(eye_r->get_value().c_str()) / 255.0f;
					eye_colour[1] = atoi(eye_g->get_value().c_str()) / 255.0f;
					eye_colour[2] = atoi(eye_b->get_value().c_str()) / 255.0f;
					eye_colour[3] = 1.0f;
					blink_colour[0] = atoi(blink_r->get_value().c_str()) / 255.0f;
					blink_colour[1] = atoi(blink_g->get_value().c_str()) / 255.0f;
					blink_colour[2] = atoi(blink_b->get_value().c_str()) / 255.0f;
					blink_colour[3] = 1.0f;
				}
			}
		}
	}

	set_next_blink();
}