Ejemplo n.º 1
0
/**
 * Advance to the next key pair
 * Take note if a new section header is encountered
 *
 * @return false if EOF, otherwise true
 */
bool FileParser::next() {

	string starts_with;
	new_section = false;
	
	while (!infile.eof()) {

		line = getLine(infile);

		// skip ahead if this line is empty
		if (line.length() == 0) continue;

		starts_with = line.at(0);
		
		// skip ahead if this line is a comment
		if (starts_with == "#") continue;
		
		// set new section if this line is a section declaration
		if (starts_with == "[") {
			new_section = true;
			section = parse_section_title(line);
			
			// keep searching for a key-pair
			continue;
		}
		
		// this is a keypair. Perform basic parsing and return
		parse_key_pair(line, key, val);
		return true;
		
	}
	
	// hit the end of file
	return false;
}
Ejemplo n.º 2
0
/**
 * Load the quests in the specific quest file
 */
void QuestLog::load(string filename) {

	ifstream infile;
	string line;
	string key;
	string val;
	string starts_with;
	string section = "";
	int event_count = 0;
	
	infile.open(("quests/" + filename).c_str(), ios::in);

	if (infile.is_open()) {
		while (!infile.eof()) {
			line = getLine(infile);

			if (line.length() > 0) {
				starts_with = line.at(0);
				
				if (starts_with == "#") {
					// skip comments
				}
				else if (starts_with == "[") {
					section = parse_section_title(line);
					if (section == "quest") {
						quest_count++;
						event_count = 0;
					}
				}
				else { // this is data.  treatment depends on key
					parse_key_pair(line, key, val);          
					key = trim(key, ' ');
					val = trim(val, ' ');
					
					quests[quest_count-1][event_count].type = key;
					quests[quest_count-1][event_count].s = val;
					event_count++;
					
					// requires_status=s
					// requires_not=s
					// quest_text=s			
				}
			}
		}
	}
	infile.close();	
}
Ejemplo n.º 3
0
bool ezFileParser::next() 
{
    
    std::string starts_with;
	new_section = false;
    
	while (!infile.eof()) {
        
		line = getLine(infile);
        
		
		if (line.length() == 0) 
            continue;
        
		starts_with = line.at(0);
        
	
		if (starts_with == "#") 
            continue;
        
		
		if (starts_with == "[") 
        {
			new_section = true;
			section = parse_section_title(line);
            
			
			continue;
		}
        
		
		parse_key_pair(line, key, val);
        
		return true;
        
	}
    
	return false;
}
Ejemplo n.º 4
0
/**
 * load
 */
int MapIso::load(string filename) {
	ifstream infile;
	string line;
	string starts_with;
	string section;
	string key;
	string val;
	string cur_layer;
	string data_format;
  
	clearEvents();
  
    event_count = 0;
  
	infile.open(("maps/" + filename).c_str(), ios::in);

	if (infile.is_open()) {
		while (!infile.eof()) {

			line = getLine(infile);

			if (line.length() > 0) {
				starts_with = line.at(0);
				
				if (starts_with == "#") {
					// skip comments
				}
				else if (starts_with == "[") {
					section = trim(parse_section_title(line), ' ');
					
					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 (section == "enemy") {
						clearEnemy(new_enemy);
						enemy_awaiting_queue = true;
					}
					else if (section == "npc") {
						clearNPC(new_npc);
						npc_awaiting_queue = true;
					}
					else if (section == "event") {
						event_count++;
					}
					
				}
				else { // this is data.  treatment depends on section type
					parse_key_pair(line, key, val);          
					key = trim(key, ' ');
					val = trim(val, ' ');

					if (section == "header") {
						if (key == "title") {
							this->title = val;
						}
						else if (key == "width") {
							this->w = atoi(val.c_str());
						}
						else if (key == "height") {
							this->h = atoi(val.c_str());
						}
						else if (key == "tileset") {
							this->tileset = val;
						}
						else if (key == "music") {
							if (this->music_filename == val) {
								this->new_music = false;
							}
							else {
								this->music_filename = val;
								this->new_music = true;
							}
						}
						else if (key == "spawnpoint") {
							val = 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 (section == "layer") {
						if (key == "id") {
							cur_layer = val;
						}
						else if (key == "format") {
							data_format = val;
						}
						else if (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++) {
									line = getLine(infile);
									line = line + ',';
									for (int i=0; i<w; i++) {
										if (cur_layer == "background") background[i][j] = eatFirstHex(line, ',');
										else if (cur_layer == "object") object[i][j] = eatFirstHex(line, ',');
										else if (cur_layer == "collision") collision[i][j] = eatFirstHex(line, ',');
									}
								}
							}
							else if (data_format == "dec") {
								for (int j=0; j<h; j++) {
									line = getLine(infile);
									line = line + ',';
									for (int i=0; i<w; i++) {
										if (cur_layer == "background") background[i][j] = eatFirstInt(line, ',');
										else if (cur_layer == "object") object[i][j] = eatFirstInt(line, ',');
										else if (cur_layer == "collision") collision[i][j] = eatFirstInt(line, ',');
									}
								}
							}
						}
					}
					else if (section == "enemy") {
						
						if (key == "type") {
							new_enemy.type = val;
						}
						else if (key == "spawnpoint") {
							val = 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 (section == "npc") {
						if (key == "id") {
							new_npc.id = val;
						}
						else if (key == "position") {
							val = 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 (section == "event") {
						if (key == "type") {
							events[event_count-1].type = val;
						}
						else if (key == "location") {
							val = 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 = key;
							
							if (key == "intermap") {
								val = val + ",";
								e->s = eatFirstString(val, ',');
								e->x = eatFirstInt(val, ',');
								e->y = eatFirstInt(val, ',');
							}
							else if (key == "mapmod") {
								val = val + ",";
								e->s = eatFirstString(val, ',');
								e->x = eatFirstInt(val, ',');
								e->y = eatFirstInt(val, ',');
								e->z = eatFirstInt(val, ',');
							}
							else if (key == "soundfx") {
								e->s = val;
							}
							else if (key == "loot") {
								val = 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 (key == "msg") {
								e->s = val;
							}
							else if (key == "shakycam") {
								e->x = atoi(val.c_str());
							}
							else if (key == "requires_status") {
								e->s = val;
							}
							else if (key == "requires_not") {
								e->s = val;
							}
							else if (key == "requires_item") {
								e->x = atoi(val.c_str());
							}
							else if (key == "set_status") {
								e->s = val;
							}
							else if (key == "unset_status") {
								e->s = val;
							}
							else if (key == "remove_item") {
								e->x = atoi(val.c_str());
							}
							else if (key == "reward_xp") {
								e->x = atoi(val.c_str());
							}
							
							events[event_count-1].comp_num++;
						}
							
					}
				}
			}
		}
		
		// 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;
		}
	}

	infile.close();

	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;
}
Ejemplo n.º 5
0
/**
 * Advance to the next key pair
 * Take note if a new section header is encountered
 *
 * @return false if EOF, otherwise true
 */
bool FileParser::next() {

	std::string starts_with;
	new_section = false;

	while (current_index < filenames.size()) {
		while (infile.good()) {
			if (include_fp) {
				if (include_fp->next()) {
					new_section = include_fp->new_section;
					section = include_fp->section;
					key = include_fp->key;
					val = include_fp->val;
					return true;
				}
				else {
					include_fp->close();
					delete include_fp;
					include_fp = NULL;
					continue;
				}
			}

			line = trim(getLine(infile));
			line_number++;

			// skip ahead if this line is empty
			if (line.length() == 0) continue;

			starts_with = line.at(0);

			// skip ahead if this line is a comment
			if (starts_with == "#") continue;

			// set new section if this line is a section declaration
			if (starts_with == "[") {
				new_section = true;
				section = parse_section_title(line);

				// keep searching for a key-pair
				continue;
			}

			// skip the string used to combine files
			if (line == "APPEND") continue;

			// read from a separate file
			std::size_t first_space = line.find(' ');

			if (first_space != std::string::npos) {
				std::string directive = line.substr(0, first_space);

				if (directive == "INCLUDE") {
					std::string tmp = line.substr(first_space+1);

					include_fp = new FileParser();
					if (!include_fp || !include_fp->open(tmp)) {
						delete include_fp;
						include_fp = NULL;
					}
					continue;
				}
			}

			// this is a keypair. Perform basic parsing and return
			parse_key_pair(line, key, val);
			return true;
		}

		infile.close();
		infile.clear();

		current_index++;
		if (current_index == filenames.size()) return false;

		line_number = 0;
		const std::string current_filename = filenames[current_index];
		infile.open(current_filename.c_str(), std::ios::in);
		if (!infile.is_open()) {
			if (!errormessage.empty())
				logError("FileParser: %s: %s", errormessage.c_str(), current_filename.c_str());
			infile.clear();
			return false;
		}
		// a new file starts a new section
		new_section = true;
	}

	// hit the end of file
	return false;
}