From 57d83f1a4baa1ffb40dc43466b2cb34b5a3405ce Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 11 Jul 2011 20:53:36 +0000 Subject: Load previously saved player data. --- src/game/base/game.cc | 278 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 206 insertions(+), 72 deletions(-) diff --git a/src/game/base/game.cc b/src/game/base/game.cc index f954c53..0aa1cbc 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -1060,8 +1060,7 @@ bool Game::load_world() if (inifile.in_section("world")) { - if (inifile.got_key_string("zone", label)) { - aux::to_label(label); + if (inifile.got_key_label("zone", label)) { zone = new core::Zone(label); core::Zone::add(zone); } else { @@ -1644,12 +1643,13 @@ bool Game::load_settings() if (inifile.got_key_long("credits", l)) { Default::credits = l; - } else if (inifile.got_key_string("zone", str)) { - aux::to_label(str); + + } else if (inifile.got_key_label("zone", str)) { Default::zone = core::Zone::find(str); - } else if (inifile.got_key_string("ship", str)) { - aux::to_label(str); + + } else if (inifile.got_key_label("ship", str)) { Default::shipmodel = ShipModel::find(str); + } else { inifile.unknown_key(); } @@ -1712,19 +1712,157 @@ void Game::player_load(core::Player *player) return; if (core::server()->mode() == core::GameServer::MultiPlayer) { + std::string guid(player->guid().str()); std::string directory(guid.substr(0,4)); - std::string filename(filesystem::writedir()); - // create players/ directory + std::string filename; filename.append("players"); filename += '/'; - // second level directory filename.append(directory); filename += '/'; - // guid.ini filename.append(guid); - filename.append(".ini"); + + filesystem::IniFile inifile; + inifile.open(filename); + if (!inifile.is_open()) { + return; + } + + con_debug << " player " << player->id() << ": " << "loading data" << std::endl; + + Ship *ship = 0; + long l; + std::string str; + math::Vector3f v; + math::Axis axis; + bool ship_is_docked = false; + + while (inifile.getline()) { + + if (inifile.got_section()) { + + if (inifile.got_section("player")) { + continue; + } else if (inifile.got_section("ship")) { + continue; + } else { + inifile.unknown_section(); + } + + } else if (inifile.got_key()) { + + if (inifile.in_section("player")) { + + if (inifile.got_key_long("credits", l)) { + player->set_credits(l); + continue; + + } else if (inifile.got_key_string("name", str)) { + continue; + + } else { + inifile.unknown_key(); + } + } else if (inifile.in_section("ship")) { + + if (inifile.got_key_label("model", str)) { + if (ship) { + continue; + } + + ShipModel *shipmodel = ShipModel::find(str); + if (!shipmodel) { + continue; + } + + ship = new Ship(player, shipmodel); + continue; + + } else if (inifile.got_key_label("zone", str)) { + ship->set_zone(core::Zone::find(str)); + continue; + + } else if (inifile.got_key_vector3f("location", v)) { + if (ship) + ship->get_location().assign(v); + continue; + + } else if (inifile.got_key_vector3f("forward", axis[0])) { + continue; + + } else if (inifile.got_key_vector3f("left", axis[1])) { + continue; + + } else if (inifile.got_key_vector3f("up", axis[2])) { + continue; + + } else if (inifile.got_key_bool("docked", ship_is_docked)) { + continue; + + } else if (inifile.got_key_string("spawn", str)) { + if (str.size() < 3) { + con_warn << " Spawn with invalid label '" << str << "'\n"; + continue; + } + size_t pos = str.find(':'); + std::string zonelabel(str.substr(0, pos)); + std::string entitylabel(str.substr(pos + 1, str.size() - pos)); + + core::Zone *spawn_zone = core::Zone::find(zonelabel); + if (!spawn_zone) { + con_warn << " Spawn with unkown zone '" << zonelabel << "'\n"; + continue; + } + + core::Entity *spawn_entity = spawn_zone->find_entity(entitylabel); + if (!spawn_entity) { + con_warn << " Spawn with unkown entity '" << str << "'\n"; + continue; + } + + if (!spawn_entity->flag_is_set(core::Entity::Dockable)) { + con_warn << " Spawn '" << str << "' is not dockable\n"; + continue; + } + + if (ship) { + ship->set_spawn(spawn_entity); + } + } + } + } + } + + inifile.close(); + + if (ship) { + + if (!ship->zone()) { + ship->set_zone(Default::zone); + } + + if (!ship->spawn()) { + ship->set_spawn(Default::view); + } + + if (ship_is_docked) { + ship->set_zone(ship->spawn()->zone()); + ship->set_dock(ship->spawn()); + + player->set_control(ship); + player->set_view(ship->spawn()); + } else { + ship->set_axis(axis); + ship->set_state(core::Entity::Normal); + ship->reset(); + + player->set_zone(ship->zone()); + player->set_control(ship); + } + + + } } } @@ -1734,7 +1872,13 @@ void Game::player_save(core::Player *player) if (!player->guid().is_valid()) { return;} + if ((!player->control()) || (player->control()->moduletype() != ship_enttype)) { + return; + } + if (core::server()->mode() == core::GameServer::MultiPlayer) { + con_debug << " player " << player->id() << ": " << "saving data" << std::endl; + std::string guid(player->guid().str()); std::string directory(guid.substr(0,4)); @@ -1756,69 +1900,59 @@ void Game::player_save(core::Player *player) filename.append(".ini"); std::ofstream ofs(filename.c_str()); - if (ofs.is_open()) { - // *--- HEADER - ofs << "; " << guid << ".ini" << std::endl; - ofs << "; Project::OSiRiON player data" << std::endl; - ofs << std::endl; - - // *--- PLAYER - ofs << "[player]" << std::endl; - - // player name - ofs << "name=" << player->name() << std::endl; - - // credit - ofs << "credits=" << player->credits() << std::endl; - - // current zone - ofs << "zone="; - if (player->zone()) { - ofs << player->zone()->label(); - } - ofs << std::endl; - - // *--- SHIP - // FIXME special case: docked at another player's vessel - ofs << std::endl; - if ((player->control()) && (player->control()->moduletype() == ship_enttype)) { - Ship * ship = static_cast(player->control()); - ofs << "[ship]" << std::endl; - ofs << "model=" << ship->shipmodel()->label() << std::endl; - // ship zone - ofs << "zone=" << ship->zone()->label()<< std::endl; - // ship location - ofs << "location=" << ship->location() << std::endl; - // ship orientation - ofs << "foward=" << std::setprecision(8) << ship->axis().forward() << std::endl; - ofs << "left=" << std::setprecision(8) << ship->axis().left() << std::endl; - // ship dock - ofs << "dock="; - if (ship->dock() && (ship->dock() == ship->spawn())) { - ofs << ship->dock()->zone()->label() << ":" << ship->dock()->label(); - - } else if (ship->state() == core::Entity::Destroyed) { - if (ship->spawn()) { - ofs << ship->spawn()->zone()->label() << ":" << ship->spawn()->label(); - } - } - ofs << std::endl; - // ship spawn - ofs << "spawn="; - if (ship->spawn()) { - ofs << ship->spawn()->zone()->label() << ":" << ship->spawn()->label(); - } - ofs << std::endl; - - // TODO inventory - } - - ofs.close(); - - con_debug << "Saving data for player id " << player->id() << std::endl; - } else { + if (!ofs.is_open()) { con_warn << "Could not write " << filename << std::endl; + return; } + // *--- HEADER + ofs << "; " << guid << ".ini" << std::endl; + ofs << "; Project::OSiRiON player data" << std::endl; + ofs << std::endl; + + // *--- PLAYER + ofs << "[player]" << std::endl; + + // player name + ofs << "name=" << player->name() << std::endl; + + // credit + ofs << "credits=" << player->credits() << std::endl; + + // *--- SHIP + ofs << std::endl; + + Ship * ship = static_cast(player->control()); + ofs << "[ship]" << std::endl; + ofs << "model=" << ship->shipmodel()->label() << std::endl; + // ship zone + ofs << "zone=" << ship->zone()->label()<< std::endl; + // ship location + ofs << "location=" << ship->location() << std::endl; + // ship orientation + ofs << "foward=" << ship->axis().forward() << std::endl; + ofs << "left=" << ship->axis().left() << std::endl; + ofs << "up=" << ship->axis().up() << std::endl; + // ship is docked at spawn on load + ofs << "docked="; + if (ship->dock() && (ship->dock() == ship->spawn())) { + ofs << "yes"; + } else if (ship->state() == core::Entity::Destroyed) { + ofs << "yes"; + } else { + ofs << "no"; + } + + ofs << std::endl; + // ship spawn + ofs << "spawn="; + if (ship->spawn()) { + ofs << ship->spawn()->zone()->label() << ":" << ship->spawn()->label(); + } + ofs << std::endl; + + // TODO inventory + + ofs.close(); } } -- cgit v1.2.3