Esempio n. 1
0
void Map::loadLayer(FileParser &infile, maprow **current_layer) {
	if (infile.key == "type") {
		// @ATTR layer.type|string|Map layer type.
		*current_layer = new maprow[w];
		layers.push_back(*current_layer);
		layernames.push_back(infile.val);
	}
	else if (infile.key == "format") {
		// @ATTR layer.format|string|Format for map layer, must be 'dec'
		if (infile.val != "dec") {
			fprintf(stderr, "ERROR: maploading: The format of a layer must be \"dec\"!\n");
			SDL_Quit();
			exit(1);
		}
	}
	else if (infile.key == "data") {
		// @ATTR layer.data|raw|Raw map layer data
		// layer map data handled as a special case
		// The next h lines must contain layer data.  TODO: err
		for (int j=0; j<h; j++) {
			std::string val = infile.getRawLine() + ',';
			for (int i=0; i<w; i++)
				(*current_layer)[i][j] = eatFirstInt(val, ',');
		}
	}
}
Esempio n. 2
0
void Map::loadLayer(FileParser &infile) {
	if (infile.key == "type") {
		// @ATTR layer.type|string|Map layer type.
		layers.resize(layers.size()+1);
		layers.back().resize(w);
		for (size_t i=0; i<layers.back().size(); ++i) {
			layers.back()[i].resize(h);
		}
		layernames.push_back(infile.val);
		if (infile.val == "collision")
			collision_layer = static_cast<int>(layernames.size())-1;
	}
	else if (infile.key == "format") {
		// @ATTR layer.format|string|Format for map layer, must be 'dec'
		if (infile.val != "dec") {
			infile.error("Map: The format of a layer must be 'dec'!");
			logErrorDialog("Map: The format of a layer must be 'dec'!");
			mods->resetModConfig();
			Exit(1);
		}
	}
	else if (infile.key == "data") {
		// @ATTR layer.data|raw|Raw map layer data
		// layer map data handled as a special case
		// The next h lines must contain layer data.
		for (int j=0; j<h; j++) {
			std::string val = infile.getRawLine();
			infile.incrementLineNum();
			if (!val.empty() && val[val.length()-1] != ',') {
				val += ',';
			}

			// verify the width of this row
			int comma_count = 0;
			for (unsigned i=0; i<val.length(); ++i) {
				if (val[i] == ',') comma_count++;
			}
			if (comma_count != w) {
				infile.error("Map: A row of layer data has a width not equal to %d.", w);
				mods->resetModConfig();
				Exit(1);
			}

			for (int i=0; i<w; i++)
				layers.back()[i][j] = static_cast<unsigned short>(popFirstInt(val));
		}
	}
	else {
		infile.error("Map: '%s' is not a valid key.", infile.key.c_str());
	}
}
Esempio n. 3
0
/**
 * load
 */
int MapRenderer::load(string filename) {
	FileParser infile;
	string val;
	string cur_layer;
	string data_format;

	clearEvents();

	bool collider_set = false;

	if (infile.open(mods->locate("maps/" + filename))) {
		while (infile.next()) {
			if (infile.new_section) {
				data_format = "dec"; // default

				if (enemy_awaiting_queue) {
					enemies.push(new_enemy);
					enemy_awaiting_queue = false;
				}
				if (npc_awaiting_queue) {
					npcs.push(new_npc);
					npc_awaiting_queue = false;
				}
				if (group_awaiting_queue) {
					push_enemy_group(new_group);
					group_awaiting_queue = false;
				}

				// for sections that are stored in collections, add a new object here
				if (infile.section == "enemy") {
					new_enemy.clear();
					enemy_awaiting_queue = true;
				}
				else if (infile.section == "enemygroup") {
					new_group.clear();
					group_awaiting_queue = true;
				}
				else if (infile.section == "npc") {
					new_npc.clear();
					npc_awaiting_queue = true;
				}
				else if (infile.section == "event") {
					events.push_back(Map_Event());
				}

			}
			if (infile.section == "header") {
				if (infile.key == "title") {
					this->title = msg->get(infile.val);
				}
				else if (infile.key == "width") {
					this->w = atoi(infile.val.c_str());
				}
				else if (infile.key == "height") {
					this->h = atoi(infile.val.c_str());
				}
				else if (infile.key == "tileset") {
					this->tileset = infile.val;
				}
				else if (infile.key == "music") {
					if (this->music_filename == infile.val) {
						this->new_music = false;
					}
					else {
						this->music_filename = infile.val;
						this->new_music = true;
					}
				}
				else if (infile.key == "location") {
					spawn.x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					spawn.y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					spawn_dir = atoi(infile.nextValue().c_str());
				}
			}
			else if (infile.section == "layer") {
				if (infile.key == "type") {
					cur_layer = infile.val;
				}
				else if (infile.key == "format") {
					data_format = infile.val;
				}
				else if (infile.key == "data") {
					// layer map data handled as a special case

					// The next h lines must contain layer data.  TODO: err
					if (data_format == "hex") {
						for (int j=0; j<h; j++) {
							val = infile.getRawLine() + ',';
							for (int i=0; i<w; i++) {
								if (cur_layer == "background") background[i][j] = eatFirstHex(val, ',');
								else if (cur_layer == "object") object[i][j] = eatFirstHex(val, ',');
								else if (cur_layer == "collision") collision[i][j] = eatFirstHex(val, ',');
							}
						}
					}
					else if (data_format == "dec") {
						for (int j=0; j<h; j++) {
							val = infile.getRawLine() + ',';
							for (int i=0; i<w; i++) {
								if (cur_layer == "background") background[i][j] = eatFirstInt(val, ',');
								else if (cur_layer == "object") object[i][j] = eatFirstInt(val, ',');
								else if (cur_layer == "collision") collision[i][j] = eatFirstInt(val, ',');
							}
						}
					}
					if ((cur_layer == "collision") && !collider_set) {
						collider.setmap(collision);
						collider.map_size.x = w;
						collider.map_size.y = h;
					}
				}
			}
			else if (infile.section == "enemy") {
				if (infile.key == "type") {
					new_enemy.type = infile.val;
				}
				else if (infile.key == "location") {
					new_enemy.pos.x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					new_enemy.pos.y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				}
				else if (infile.key == "direction") {
					new_enemy.direction = atoi(infile.val.c_str());
				}
				else if (infile.key == "waypoints") {
					string none = "";
					string a = infile.nextValue();
					string b = infile.nextValue();

					while (a != none) {
						Point p;
						p.x = atoi(a.c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
						p.y = atoi(b.c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
						new_enemy.waypoints.push(p);
						a = infile.nextValue();
						b = infile.nextValue();
					}
				} else if (infile.key == "wander_area") {
					new_enemy.wander = true;
					new_enemy.wander_area.x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
					new_enemy.wander_area.y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
					new_enemy.wander_area.w = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
					new_enemy.wander_area.h = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
				}
			}
			else if (infile.section == "enemygroup") {
				if (infile.key == "type") {
					new_group.category = infile.val;
				}
				else if (infile.key == "level") {
					new_group.levelmin = atoi(infile.nextValue().c_str());
					new_group.levelmax = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "location") {
					new_group.pos.x = atoi(infile.nextValue().c_str());
					new_group.pos.y = atoi(infile.nextValue().c_str());
					new_group.area.x = atoi(infile.nextValue().c_str());
					new_group.area.y = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "number") {
					new_group.numbermin = atoi(infile.nextValue().c_str());
					new_group.numbermax = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "chance") {
					new_group.chance = atoi(infile.nextValue().c_str()) / 100.0f;
					if (new_group.chance > 1.0f) {
						new_group.chance = 1.0f;
					}
					if (new_group.chance < 0.0f) {
						new_group.chance = 0.0f;
					}
				}
			}
			else if (infile.section == "npc") {
				if (infile.key == "type") {
					new_npc.id = infile.val;
				}
				else if (infile.key == "location") {
					new_npc.pos.x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					new_npc.pos.y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				}
			}
			else if (infile.section == "event") {
				if (infile.key == "type") {
					events.back().type = infile.val;
				}
				else if (infile.key == "location") {
					events.back().location.x = atoi(infile.nextValue().c_str());
					events.back().location.y = atoi(infile.nextValue().c_str());
					events.back().location.w = atoi(infile.nextValue().c_str());
					events.back().location.h = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "hotspot") {
					events.back().hotspot.x = atoi(infile.nextValue().c_str());
					events.back().hotspot.y = atoi(infile.nextValue().c_str());
					events.back().hotspot.w = atoi(infile.nextValue().c_str());
					events.back().hotspot.h = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "tooltip") {
					events.back().tooltip = msg->get(infile.val);
				}
				else if (infile.key == "power_path") {
					events.back().power_src.x = atoi(infile.nextValue().c_str());
					events.back().power_src.y = atoi(infile.nextValue().c_str());
					string dest = infile.nextValue();
					if (dest == "hero") {
						events.back().targetHero = true;
					}
					else {
						events.back().power_dest.x = atoi(dest.c_str());
						events.back().power_dest.y = atoi(infile.nextValue().c_str());
					}
				}
				else if (infile.key == "power_damage") {
					events.back().damagemin = atoi(infile.nextValue().c_str());
					events.back().damagemax = atoi(infile.nextValue().c_str());
				}
				else if (infile.key == "cooldown") {
					events.back().cooldown = atoi(infile.val.c_str());
				}
				else {
					// new event component
					Event_Component *e = &(events.back()).components[events.back().comp_num];
					e->type = infile.key;

					if (infile.key == "intermap") {
						e->s = infile.nextValue();
						e->x = atoi(infile.nextValue().c_str());
						e->y = atoi(infile.nextValue().c_str());
					}
					else if (infile.key == "intramap") {
						e->x = atoi(infile.nextValue().c_str());
						e->y = atoi(infile.nextValue().c_str());
					}
					else if (infile.key == "mapmod") {
						e->s = infile.nextValue();
						e->x = atoi(infile.nextValue().c_str());
						e->y = atoi(infile.nextValue().c_str());
						e->z = atoi(infile.nextValue().c_str());

						// add repeating mapmods
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;
							e->x = atoi(infile.nextValue().c_str());
							e->y = atoi(infile.nextValue().c_str());
							e->z = atoi(infile.nextValue().c_str());

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "soundfx") {
						e->s = infile.val;
					}
					else if (infile.key == "loot") {
						e->s = infile.nextValue();
						e->x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->z = atoi(infile.nextValue().c_str());

						// add repeating loot
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;
							e->x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
							e->y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
							e->z = atoi(infile.nextValue().c_str());

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "msg") {
						e->s = msg->get(infile.val);
					}
					else if (infile.key == "shakycam") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "requires_status") {
						e->s = infile.nextValue();

						// add repeating requires_status
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "requires_not") {
						e->s = infile.nextValue();

						// add repeating requires_not
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "requires_item") {
						e->x = atoi(infile.nextValue().c_str());

						// add repeating requires_item
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->x = atoi(repeat_val.c_str());

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "set_status") {
						e->s = infile.nextValue();

						// add repeating set_status
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "unset_status") {
						e->s = infile.nextValue();

						// add repeating unset_status
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->s = repeat_val;

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "remove_item") {
						e->x = atoi(infile.nextValue().c_str());

						// add repeating remove_item
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;
							e->x = atoi(repeat_val.c_str());

							repeat_val = infile.nextValue();
						}
					}
					else if (infile.key == "reward_xp") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "power") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "spawn") {

						e->s = infile.nextValue();
						e->x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;

						// add repeating spawn
						string repeat_val = infile.nextValue();
						while (repeat_val != "") {
							events.back().comp_num++;
							e = &events.back().components[events.back().comp_num];
							e->type = infile.key;

							e->s = repeat_val;
							e->x = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
							e->y = atoi(infile.nextValue().c_str()) * UNITS_PER_TILE + UNITS_PER_TILE/2;

							repeat_val = infile.nextValue();
						}
					}
					events.back().comp_num++;
				}
			}
		}

		infile.close();

		// reached end of file.  Handle any final sections.
		if (enemy_awaiting_queue) {
			enemies.push(new_enemy);
			enemy_awaiting_queue = false;
		}
		if (npc_awaiting_queue) {
			npcs.push(new_npc);
			npc_awaiting_queue = false;
		}
		if (group_awaiting_queue){
			push_enemy_group(new_group);
			group_awaiting_queue = false;
		}
	}

	if (this->new_music) {
		loadMusic();
		this->new_music = false;
	}
	tset.load(this->tileset);

	return 0;
}
Esempio n. 4
0
/**
 * load
 */
int MapRenderer::load(string filename) {
	FileParser infile;
	string val;
	string data_format;
	maprow *cur_layer;

	clearEvents();
	clearLayers();
	clearQueues();

	cur_layer = NULL;
	show_tooltip = false;

	if (!infile.open(mods->locate("maps/" + filename))) {
		cerr << "Unable to open maps/" << filename << endl;
		return 0;
	}

	while (infile.next()) {
		if (infile.new_section) {
			data_format = "dec"; // default

			if (enemy_awaiting_queue) {
				enemies.push(new_enemy);
				enemy_awaiting_queue = false;
			}
			if (npc_awaiting_queue) {
				npcs.push(new_npc);
				npc_awaiting_queue = false;
			}
			if (group_awaiting_queue) {
				push_enemy_group(new_group);
				group_awaiting_queue = false;
			}

			// for sections that are stored in collections, add a new object here
			if (infile.section == "enemy") {
				new_enemy.clear();
				enemy_awaiting_queue = true;
			}
			else if (infile.section == "enemygroup") {
				new_group.clear();
				group_awaiting_queue = true;
			}
			else if (infile.section == "npc") {
				new_npc.clear();
				npc_awaiting_queue = true;
			}
			else if (infile.section == "event") {
				events.push_back(Map_Event());
			}

		}
		if (infile.section == "header") {
			if (infile.key == "title") {
				this->title = msg->get(infile.val);
			}
			else if (infile.key == "width") {
				this->w = toInt(infile.val);
			}
			else if (infile.key == "height") {
				this->h = toInt(infile.val);
			}
			else if (infile.key == "tileset") {
				this->tileset = infile.val;
			}
			else if (infile.key == "music") {
				if (this->music_filename == infile.val) {
					this->new_music = false;
				}
				else {
					this->music_filename = infile.val;
					this->new_music = true;
				}
			}
			else if (infile.key == "location") {
				spawn.x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				spawn.y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				spawn_dir = toInt(infile.nextValue());
			}
		}
		else if (infile.section == "layer") {
			if (infile.key == "type") {
				cur_layer = new maprow[w];
				if (infile.val == "background") background = cur_layer;
				else if (infile.val == "fringe") fringe = cur_layer;
				else if (infile.val == "object") object = cur_layer;
				else if (infile.val == "foreground") foreground = cur_layer;
				else if (infile.val == "collision") collision = cur_layer;
			}
			else if (infile.key == "format") {
				data_format = infile.val;
			}
			else if (infile.key == "data") {
				// layer map data handled as a special case
				// The next h lines must contain layer data.  TODO: err
				for (int j=0; j<h; j++) {
					val = infile.getRawLine() + ',';
					for (int i=0; i<w; i++)
						cur_layer[i][j] = eatFirstInt(val, ',', (data_format == "hex" ? std::hex : std::dec));
				}
				if (cur_layer == collision)
					collider.setmap(collision, w, h);
			}
		}
		else if (infile.section == "enemy") {
			if (infile.key == "type") {
				new_enemy.type = infile.val;
			}
			else if (infile.key == "location") {
				new_enemy.pos.x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				new_enemy.pos.y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
			}
			else if (infile.key == "direction") {
				new_enemy.direction = toInt(infile.val);
			}
			else if (infile.key == "waypoints") {
				string none = "";
				string a = infile.nextValue();
				string b = infile.nextValue();

				while (a != none) {
					Point p;
					p.x = toInt(a) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
					p.y = toInt(b) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
					new_enemy.waypoints.push(p);
					a = infile.nextValue();
					b = infile.nextValue();
				}
			} else if (infile.key == "wander_area") {
				new_enemy.wander = true;
				new_enemy.wander_area.x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
				new_enemy.wander_area.y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
				new_enemy.wander_area.w = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
				new_enemy.wander_area.h = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE / 2;
			}
		}
		else if (infile.section == "enemygroup") {
			if (infile.key == "type") {
				new_group.category = infile.val;
			}
			else if (infile.key == "level") {
				new_group.levelmin = toInt(infile.nextValue());
				new_group.levelmax = toInt(infile.nextValue());
			}
			else if (infile.key == "location") {
				new_group.pos.x = toInt(infile.nextValue());
				new_group.pos.y = toInt(infile.nextValue());
				new_group.area.x = toInt(infile.nextValue());
				new_group.area.y = toInt(infile.nextValue());
			}
			else if (infile.key == "number") {
				new_group.numbermin = toInt(infile.nextValue());
				new_group.numbermax = toInt(infile.nextValue());
			}
			else if (infile.key == "chance") {
				new_group.chance = toInt(infile.nextValue()) / 100.0f;
				if (new_group.chance > 1.0f) {
					new_group.chance = 1.0f;
				}
				if (new_group.chance < 0.0f) {
					new_group.chance = 0.0f;
				}
			}
		}
		else if (infile.section == "npc") {
			if (infile.key == "type") {
				new_npc.id = infile.val;
			}
			else if (infile.key == "location") {
				new_npc.pos.x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
				new_npc.pos.y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
			}
		}
		else if (infile.section == "event") {
			if (infile.key == "type") {
				events.back().type = infile.val;
			}
			else if (infile.key == "location") {
				events.back().location.x = toInt(infile.nextValue());
				events.back().location.y = toInt(infile.nextValue());
				events.back().location.w = toInt(infile.nextValue());
				events.back().location.h = toInt(infile.nextValue());
			}
			else if (infile.key == "hotspot") {
				if (infile.val == "location") {
					events.back().hotspot.x = events.back().location.x;
					events.back().hotspot.y = events.back().location.y;
					events.back().hotspot.w = events.back().location.w;
					events.back().hotspot.h = events.back().location.h;
				}
				else {
					events.back().hotspot.x = toInt(infile.nextValue());
					events.back().hotspot.y = toInt(infile.nextValue());
					events.back().hotspot.w = toInt(infile.nextValue());
					events.back().hotspot.h = toInt(infile.nextValue());
				}
			}
			else if (infile.key == "tooltip") {
				events.back().tooltip = msg->get(infile.val);
			}
			else if (infile.key == "power_path") {
				events.back().power_src.x = toInt(infile.nextValue());
				events.back().power_src.y = toInt(infile.nextValue());
				string dest = infile.nextValue();
				if (dest == "hero") {
					events.back().targetHero = true;
				}
				else {
					events.back().power_dest.x = toInt(dest);
					events.back().power_dest.y = toInt(infile.nextValue());
				}
			}
			else if (infile.key == "power_damage") {
				events.back().damagemin = toInt(infile.nextValue());
				events.back().damagemax = toInt(infile.nextValue());
			}
			else if (infile.key == "cooldown") {
				events.back().cooldown = toInt(infile.val);
			}
			else {
				// new event component
				Event_Component *e = &(events.back()).components[events.back().comp_num];
				e->type = infile.key;

				if (infile.key == "intermap") {
					e->s = infile.nextValue();
					e->x = toInt(infile.nextValue());
					e->y = toInt(infile.nextValue());
				}
				else if (infile.key == "intramap") {
					e->x = toInt(infile.nextValue());
					e->y = toInt(infile.nextValue());
				}
				else if (infile.key == "mapmod") {
					e->s = infile.nextValue();
					e->x = toInt(infile.nextValue());
					e->y = toInt(infile.nextValue());
					e->z = toInt(infile.nextValue());

					// add repeating mapmods
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;
						e->x = toInt(infile.nextValue());
						e->y = toInt(infile.nextValue());
						e->z = toInt(infile.nextValue());

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "soundfx") {
					e->s = infile.val;
				}
				else if (infile.key == "loot") {
					e->s = infile.nextValue();
					e->x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					e->y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					e->z = toInt(infile.nextValue());

					// add repeating loot
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;
						e->x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->z = toInt(infile.nextValue());

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "msg") {
					e->s = msg->get(infile.val);
				}
				else if (infile.key == "shakycam") {
					e->x = toInt(infile.val);
				}
				else if (infile.key == "requires_status") {
					e->s = infile.nextValue();

					// add repeating requires_status
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "requires_not") {
					e->s = infile.nextValue();

					// add repeating requires_not
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "requires_item") {
					e->x = toInt(infile.nextValue());

					// add repeating requires_item
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->x = toInt(repeat_val);

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "set_status") {
					e->s = infile.nextValue();

					// add repeating set_status
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "unset_status") {
					e->s = infile.nextValue();

					// add repeating unset_status
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->s = repeat_val;

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "remove_item") {
					e->x = toInt(infile.nextValue());

					// add repeating remove_item
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;
						e->x = toInt(repeat_val);

						repeat_val = infile.nextValue();
					}
				}
				else if (infile.key == "reward_xp") {
					e->x = toInt(infile.val);
				}
				else if (infile.key == "power") {
					e->x = toInt(infile.val);
				}
				else if (infile.key == "spawn") {

					e->s = infile.nextValue();
					e->x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
					e->y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;

					// add repeating spawn
					string repeat_val = infile.nextValue();
					while (repeat_val != "") {
						events.back().comp_num++;
						e = &events.back().components[events.back().comp_num];
						e->type = infile.key;

						e->s = repeat_val;
						e->x = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->y = toInt(infile.nextValue()) * UNITS_PER_TILE + UNITS_PER_TILE/2;

						repeat_val = infile.nextValue();
					}
				}
				events.back().comp_num++;
			}
		}
	}

	infile.close();

	// reached end of file.  Handle any final sections.
	if (enemy_awaiting_queue) {
		enemies.push(new_enemy);
		enemy_awaiting_queue = false;
	}
	if (npc_awaiting_queue) {
		npcs.push(new_npc);
		npc_awaiting_queue = false;
	}
	if (group_awaiting_queue){
		push_enemy_group(new_group);
		group_awaiting_queue = false;
	}

	if (this->new_music) {
		loadMusic();
		this->new_music = false;
	}
	tset.load(this->tileset);

	// some events automatically trigger when the map loads
	// e.g. change map state based on campaign status
	executeOnLoadEvents();

	return 0;
}
Esempio n. 5
0
/**
 * load
 */
int MapIso::load(string filename) {
	FileParser infile;
	string val;
	string cur_layer;
	string data_format;
  
	clearEvents();
  
    event_count = 0;
  
	if (infile.open(("maps/" + filename).c_str())) {
		while (infile.next()) {
			if (infile.new_section) {
				data_format = "dec"; // default
				
				if (enemy_awaiting_queue) {
					enemies.push(new_enemy);
					enemy_awaiting_queue = false;
				}
				if (npc_awaiting_queue) {
					npcs.push(new_npc);
					npc_awaiting_queue = false;
				}
				
				// for sections that are stored in collections, add a new object here
				if (infile.section == "enemy") {
					clearEnemy(new_enemy);
					enemy_awaiting_queue = true;
				}
				else if (infile.section == "npc") {
					clearNPC(new_npc);
					npc_awaiting_queue = true;
				}
				else if (infile.section == "event") {
					event_count++;
				}
				
			}
			if (infile.section == "header") {
				if (infile.key == "title") {
					this->title = infile.val;
				}
				else if (infile.key == "width") {
					this->w = atoi(infile.val.c_str());
				}
				else if (infile.key == "height") {
					this->h = atoi(infile.val.c_str());
				}
				else if (infile.key == "tileset") {
					this->tileset = infile.val;
				}
				else if (infile.key == "music") {
					if (this->music_filename == infile.val) {
						this->new_music = false;
					}
					else {
						this->music_filename = infile.val;
						this->new_music = true;
					}
				}
				else if (infile.key == "spawnpoint") {
					val = infile.val + ",";
					spawn.x = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
					spawn.y = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
					spawn_dir = eatFirstInt(val, ',');
				}
			}
			else if (infile.section == "layer") {
				if (infile.key == "id") {
					cur_layer = infile.val;
				}
				else if (infile.key == "format") {
					data_format = infile.val;
				}
				else if (infile.key == "data") {
					// layer map data handled as a special case

					// The next h lines must contain layer data.  TODO: err
					if (data_format == "hex") {
						for (int j=0; j<h; j++) {
							val = infile.getRawLine() + ',';
							for (int i=0; i<w; i++) {
								if (cur_layer == "background") background[i][j] = eatFirstHex(val, ',');
								else if (cur_layer == "object") object[i][j] = eatFirstHex(val, ',');
								else if (cur_layer == "collision") collision[i][j] = eatFirstHex(val, ',');
							}
						}
					}
					else if (data_format == "dec") {
						for (int j=0; j<h; j++) {
							val = infile.getRawLine() + ',';
							for (int i=0; i<w; i++) {
								if (cur_layer == "background") background[i][j] = eatFirstInt(val, ',');
								else if (cur_layer == "object") object[i][j] = eatFirstInt(val, ',');
								else if (cur_layer == "collision") collision[i][j] = eatFirstInt(val, ',');
							}
						}
					}
				}
			}
			else if (infile.section == "enemy") {
				
				if (infile.key == "type") {
					new_enemy.type = infile.val;
				}
				else if (infile.key == "spawnpoint") {
					val = infile.val + ",";
					new_enemy.pos.x = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
					new_enemy.pos.y = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
					new_enemy.direction = eatFirstInt(val, ',');
				}
			}
			else if (infile.section == "npc") {
				if (infile.key == "id") {
					new_npc.id = infile.val;
				}
				else if (infile.key == "position") {
					val = infile.val + ",";
					new_npc.pos.x = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
					new_npc.pos.y = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
				}
			
			}
			else if (infile.section == "event") {
				if (infile.key == "type") {
					events[event_count-1].type = infile.val;
				}
				else if (infile.key == "location") {
					val = infile.val + ",";
					events[event_count-1].location.x = eatFirstInt(val, ',');
					events[event_count-1].location.y = eatFirstInt(val, ',');
					events[event_count-1].location.w = eatFirstInt(val, ',');
					events[event_count-1].location.h = eatFirstInt(val, ',');
				}
				else {
					// new event component
					Event_Component *e = &events[event_count-1].components[events[event_count-1].comp_num];
					e->type = infile.key;
					
					if (infile.key == "intermap") {
						val = infile.val + ",";
						e->s = eatFirstString(val, ',');
						e->x = eatFirstInt(val, ',');
						e->y = eatFirstInt(val, ',');
					}
					else if (infile.key == "mapmod") {
						val = infile.val + ",";
						e->s = eatFirstString(val, ',');
						e->x = eatFirstInt(val, ',');
						e->y = eatFirstInt(val, ',');
						e->z = eatFirstInt(val, ',');
					}
					else if (infile.key == "soundfx") {
						e->s = infile.val;
					}
					else if (infile.key == "loot") {
						val = infile.val + ",";
						e->s = eatFirstString(val, ',');
						e->x = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->y = eatFirstInt(val, ',') * UNITS_PER_TILE + UNITS_PER_TILE/2;
						e->z = eatFirstInt(val, ',');
	
					}
					else if (infile.key == "msg") {
						e->s = infile.val;
					}
					else if (infile.key == "shakycam") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "requires_status") {
						e->s = infile.val;
					}
					else if (infile.key == "requires_not") {
						e->s = infile.val;
					}
					else if (infile.key == "requires_item") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "set_status") {
						e->s = infile.val;
					}
					else if (infile.key == "unset_status") {
						e->s = infile.val;
					}
					else if (infile.key == "remove_item") {
						e->x = atoi(infile.val.c_str());
					}
					else if (infile.key == "reward_xp") {
						e->x = atoi(infile.val.c_str());
					}
					
					events[event_count-1].comp_num++;
				}
			}
		}

		infile.close();
		
		// reached end of file.  Handle any final sections.
		if (enemy_awaiting_queue) {
			enemies.push(new_enemy);
			enemy_awaiting_queue = false;
		}
		if (npc_awaiting_queue) {
			npcs.push(new_npc);
			npc_awaiting_queue = false;
		}
	}

	collider.setmap(collision);
	collider.map_size.x = w;
	collider.map_size.y = h;
	
	if (this->new_music) {
		loadMusic();
		this->new_music = false;
	}
	tset.load(this->tileset);

	return 0;
}