/** * Loads a saved game's contents from a YAML file. * @note Assumes the saved game is blank. * @param filename YAML filename. * @param rule Ruleset for the saved game. */ void SavedGame::load(const std::string &filename, Ruleset *rule) { std::string s = Options::getUserFolder() + filename + ".sav"; std::ifstream fin(s.c_str()); if (!fin) { throw Exception("Failed to load savegame"); } YAML::Parser parser(fin); YAML::Node doc; // Get brief save info parser.GetNextDocument(doc); std::string v; doc["version"] >> v; if (v != Options::getVersion()) { throw Exception("Version mismatch"); } _time->load(doc["time"]); // Get full save data parser.GetNextDocument(doc); int a = 0; doc["difficulty"] >> a; _difficulty = (GameDifficulty)a; doc["funds"] >> _funds; for (YAML::Iterator i = doc["countries"].begin(); i != doc["countries"].end(); ++i) { std::string type; (*i)["type"] >> type; Country *c = new Country(rule->getCountry(type), false); c->load(*i); _countries.push_back(c); } for (YAML::Iterator i = doc["regions"].begin(); i != doc["regions"].end(); ++i) { std::string type; (*i)["type"] >> type; Region *r = new Region(rule->getRegion(type)); r->load(*i); _regions.push_back(r); } for (YAML::Iterator i = doc["ufos"].begin(); i != doc["ufos"].end(); ++i) { std::string type; (*i)["type"] >> type; Ufo *u = new Ufo(rule->getUfo(type)); u->load(*i); _ufos.push_back(u); } doc["craftId"] >> _craftId; for (YAML::Iterator i = doc["waypoints"].begin(); i != doc["waypoints"].end(); ++i) { Waypoint *w = new Waypoint(); w->load(*i); _waypoints.push_back(w); } doc["ufoId"] >> _ufoId; doc["waypointId"] >> _waypointId; doc["soldierId"] >> _soldierId; for (YAML::Iterator i = doc["bases"].begin(); i != doc["bases"].end(); ++i) { Base *b = new Base(rule); b->load(*i, this); _bases.push_back(b); } for(YAML::Iterator it=doc["discovered"].begin();it!=doc["discovered"].end();++it) { std::string research; *it >> research; _discovered.push_back(rule->getResearchProject(research)); } if (const YAML::Node *pName = doc.FindValue("battleGame")) { _battleGame = new SavedBattleGame(); _battleGame->load(*pName, rule, this); } fin.close(); }
/** * Loads a saved game's contents from a YAML file. * @note Assumes the saved game is blank. * @param filename YAML filename. * @param rule Ruleset for the saved game. */ void SavedGame::load(const std::string &filename, Ruleset *rule) { unsigned int size = 0; std::string s = USER_DIR + filename + ".sav"; std::ifstream fin(s.c_str()); if (!fin) { throw Exception("Failed to load savegame"); } YAML::Parser parser(fin); YAML::Node doc; // Get brief save info parser.GetNextDocument(doc); std::string v; doc["version"] >> v; if (v != "0.2") { throw Exception("Version mismatch"); } _time->load(doc["time"]); // Get full save data parser.GetNextDocument(doc); int a; doc["difficulty"] >> a; _difficulty = (GameDifficulty)a; doc["funds"] >> _funds; size = doc["countries"].size(); for (unsigned int i = 0; i < size; i++) { std::string type; doc["countries"][i]["type"] >> type; Country *c = new Country(rule->getCountry(type), false); c->load(doc["countries"][i]); _countries.push_back(c); } size = doc["regions"].size(); for (unsigned int i = 0; i < size; i++) { std::string type; doc["regions"][i]["type"] >> type; Region *r = new Region(rule->getRegion(type)); r->load(doc["regions"][i]); _regions.push_back(r); } size = doc["ufos"].size(); for (unsigned int i = 0; i < size; i++) { std::string type; doc["ufos"][i]["type"] >> type; Ufo *u = new Ufo(rule->getUfo(type)); u->load(doc["ufos"][i]); _ufos.push_back(u); } doc["craftId"] >> _craftId; size = doc["waypoints"].size(); for (unsigned int i = 0; i < size; i++) { Waypoint *w = new Waypoint(); w->load(doc["waypoints"][i]); _waypoints.push_back(w); } doc["ufoId"] >> _ufoId; doc["waypointId"] >> _waypointId; size = doc["bases"].size(); for (unsigned int i = 0; i < size; i++) { Base *b = new Base(rule); b->load(doc["bases"][i], this); _bases.push_back(b); } if (const YAML::Node *pName = doc.FindValue("battleGame")) { _battleGame = new SavedBattleGame(); _battleGame->load(*pName); } fin.close(); }