void Display::saveWorld() { if (isLoaded_ == false) return; stringstream filename; if (saveSuffix_ == "0") getSaveSuffix(); filename <<"Saves\\" << "World" << saveSuffix_ << ".dat"; fstream file(filename.str(), fstream::trunc | fstream::out); file.seekp(0, ios_base::beg); if(file.is_open() == false) { file.clear(); file.open(filename.str(), fstream::app); } Position pos; for(int x=0;x<size_x_;x++) { for(int y=0;y<size_y_;y++) { pos.setX(x); pos.setY(y); if(!m_map.count(pos)) continue; Tile& tile=m_map[pos]; tile.serialize(file); } } game::pHandler.getLocalPlayer().serialize(file); file << "End"; file.close(); }
void MainLoop::getSaveOptions(const vector<pair<Model::GameType, string>>& games, vector<View::ListElem>& options, vector<SaveFileInfo>& allFiles) { for (auto elem : games) { vector<SaveFileInfo> files = getSaveFiles(userPath, getSaveSuffix(elem.first)); files = ::filter(files, [this] (const SaveFileInfo& info) { return isCompatible(getSaveVersion(info));}); append(allFiles, files); if (!files.empty()) { options.emplace_back(elem.second, View::TITLE); append(options, transform2<View::ListElem>(files, [this] (const SaveFileInfo& info) { return getGameName(info);})); } } }
void Display::loadWorld() { stringstream filename; getSaveSuffix(); filename <<"Save\\" << "World" << saveSuffix_ << ".dat"; fstream file(filename.str()); if (file.is_open() == false) { newWorld(); saveWorld(); isLoaded_ = true; return; } file.seekp(0, ios_base::beg); string text; file >> text; while (text != "End") { if (text == "Tile") { file.clear(); Tile tile; tile.deserialize(file); m_map[tile.getPos()] = tile; file.clear(); } if (text == "Player") { game::pHandler.getLocalPlayer().deserialize(file); } text = ""; file.clear(); file >> text; } reloadAll_ = true; isLoaded_ = true; }
string MainLoop::getSavePath(Model* model, Model::GameType gameType) { return userPath + "/" + stripNonAscii(model->getGameIdentifier()) + getSaveSuffix(gameType); }
void Display::newWorldMulti() { Position newPos(0, 0); getSaveSuffix(); game::pHandler.getLocalPlayer().reset(); Tile gold; gold.setGraphic(TG_Gold); gold.setColor(TGC_Gold); gold.setBackground(TGB_Gold); gold.isDestructable(true); gold.isWall(true); gold.isWalkable(false); gold.isClaimable(false); gold.setHealth(150); gold.setMaxHealth(150); Tile stoneFloor; stoneFloor.setColor(TGC_StoneFloor); stoneFloor.setGraphic(TG_StoneFloor); stoneFloor.setBackground(TGB_StoneFloor); stoneFloor.isDestructable(false); stoneFloor.isWall(false); stoneFloor.isWalkable(true); stoneFloor.forceClaim(game::pHandler.getLocalPlayer().getName()); stoneFloor.isClaimable(true); stoneFloor.setHealth(100); stoneFloor.setMaxHealth(100); Tile core; core.setGraphic('C'); core.setColor(TC_Gray); core.setBackground(B_DarkGray); core.isWall(false); core.isWalkable(false); core.isDestructable(true); core.isClaimable(false); core.setMaxHealth(2500); core.setHealth(2500); core.setClaimedBy(game::pHandler.getLocalPlayer().getName()); Tile stone; stone.setGraphic(TG_Stone); stone.setColor(TGC_Stone); stone.setBackground(TGB_Stone); stone.isDestructable(true); stone.setMaxHealth(100); stone.setHealth(100); stone.isWall(true); //Position startPos(rand() % 50, rand() % 20); //Position corePos(rand() % 50, rand() % 20); Position startPos(0, 1); Position corePos(0, 0); int key = rand() % 15; for (int x = 0; x<size_x_; x++) { for (int y = 0; y<size_y_; y++) { newPos.setX(x); newPos.setY(y); if ((rand() % 15) == key) { gold.setGoldAmount((rand() % 500) + 101); gold.setPos(newPos); m_map[newPos] = gold; } else { stone.setPos(newPos); m_map[newPos] = stone; } } } Player other; other.setName("Other"); other.setSpawnPos(Position(74, 28)); game::pHandler.getLocalPlayer().setName("Host"); game::pHandler.getLocalPlayer().setSpawnPos(Position(0,1)); /* Host */ startPos(0, 1); core.setPos(Position(0,0)); stoneFloor.setPos(Position(0,1)); core.forceClaim("Host"); stoneFloor.forceClaim("Host"); m_map[startPos] = stoneFloor; m_map[corePos] = core; game::pHandler.getLocalPlayer().forceHandPosition(Position(0, 1), *this); game::pHandler.addLocalPlayer(game::pHandler.getLocalPlayer()); /* Other*/ core.setPos(Position(74, 29)); stoneFloor.setPos(Position(74, 28)); core.forceClaim("Other"); stoneFloor.forceClaim("Other"); m_map[Position(74, 28)] = stoneFloor; m_map[Position(74, 29)] = core; other.forceHandPosition(Position(74, 28), *this); game::pHandler.addPlayer(other); isLoaded_ = true; reloadAll_ = true; }