From 83c9d657773fa4f829b533791697ed07e0d9d962 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 11 Jul 2011 19:33:27 +0000 Subject: Initial support for saving player data in multiplayer games, have ships remember their docks and spawns. --- src/game/base/game.cc | 195 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 157 insertions(+), 38 deletions(-) (limited to 'src/game/base/game.cc') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 4bc94f0..f954c53 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include "auxiliary/functions.h" @@ -68,28 +69,30 @@ void Game::func_join(core::Player *player, std::string const &args) if (player->control()) return; - player->set_credits(Default::credits); - Ship *ship = new Ship(player, Default::shipmodel); - ship->set_zone(player->zone()); - player->set_control(ship); - - core::Entity *dock = ship->zone()->default_view(); - if (dock) { - ship->get_location().assign(dock->location() + (dock->axis().forward() *((ship->radius() + dock->radius())*2.0f))); - ship->get_axis().assign(dock->axis()); - ship->set_state(core::Entity::Docked); - ship->reset(); - player->set_view(dock); - } - std::string message("^B"); message.append(player->name()); message.append("^B joins the game."); core::server()->broadcast(message); - player->send("^BYou received " + aux::article(Default::shipmodel->name())); - player->sound("game/buy-ship"); + // load existing player data + core::server()->module()->player_load(player); + + if (!player->control()) { + player->set_credits(Default::credits); + Ship *ship = new Ship(player, Default::shipmodel); + ship->set_zone(player->zone()); + player->set_control(ship); + core::Entity *dock = ship->zone()->default_view(); + if (dock) { + ship->set_dock(dock); + player->set_view(dock); + } + + player->send("^BYou received " + aux::article(Default::shipmodel->name())); + player->sound("game/buy-ship"); + } + player->set_dirty(); } @@ -98,6 +101,8 @@ void Game::func_spectate(core::Player *player, std::string const &args) { if (!player->control()) return; + + core::server()->module()->player_save(player); std::string message("^B"); message.append(player->name()); @@ -194,10 +199,8 @@ void Game::func_target_dock(core::Player *player, core::Entity *entity) RaceTrack *race = static_cast(entity); race->func_dock(ship); } else { - ship->get_location().assign(entity->location()); - ship->set_state(core::Entity::Docked); - ship->reset(); - + ship->set_dock(entity); + if (player->control() == ship) { player->set_view(entity); if (owner) { @@ -205,7 +208,7 @@ void Game::func_target_dock(core::Player *player, core::Entity *entity) } else { player->send("^BDocking at " + entity->name()); } - } + } } } @@ -760,12 +763,13 @@ void Game::func_launch(core::Player *player, std::string const &args) return; assert(player->view()->zone() == player->control()->zone()); + assert(player->control()->moduletype() == ship_enttype); + + Ship *ship = static_cast(player->control()); - core::Entity *dock = player->view(); - - if (dock->moduletype() == ship_enttype) { + if (ship->dock()->moduletype() == ship_enttype) { - switch(static_cast(dock)->state()) { + switch(static_cast(ship->dock())->state()) { case core::Entity::Normal: case core::Entity::Docked: @@ -795,21 +799,13 @@ void Game::func_launch(core::Player *player, std::string const &args) } } - assert(player->control()->moduletype() == ship_enttype); - - Ship *ship = static_cast(player->control()); + player->send("^BLaunching from " + ship->dock()->name()); + + ship->launch(); - if (dock->type() == core::Entity::Globe) - ship->get_location().assign(dock->location() + (dock->axis().forward() * (planet_safe_distance + ship->radius() + dock->radius()))); - else - ship->get_location().assign(dock->location() + (dock->axis().forward() * (ship->radius() + dock->radius()))); - - ship->get_axis().assign(dock->axis()); - ship->set_state(core::Entity::Normal); - ship->reset(); player->set_view(0); - player->send("^BLaunching from " + dock->name()); + } // respawn @@ -1695,12 +1691,135 @@ void Game::player_connect(core::Player *player) player->set_zone(Default::zone); player->set_view(0); // not docked + // FIXME load player func_spectate(player, args); } void Game::player_disconnect(core::Player *player) { - player->remove_asset(player->control()); + if (player->control()) { + core::server()->module()->player_save(player); + } } +void Game::player_load(core::Player *player) +{ + if (!player->guid().is_valid()) { + return; + } + + if (player->control()) + 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 + filename.append("players"); + filename += '/'; + // second level directory + filename.append(directory); + filename += '/'; + // guid.ini + filename.append(guid); + filename.append(".ini"); + } + +} + +void Game::player_save(core::Player *player) +{ + if (!player->guid().is_valid()) { + 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 + filename.append("players"); + if (!sys::directory_exists(filename)) { + sys::mkdir(filename); + } + filename += '/'; + // second level directory + filename.append(directory); + if (!sys::directory_exists(filename)) { + sys::mkdir(filename); + } + filename += '/'; + // guid.ini + filename.append(guid); + 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 { + con_warn << "Could not write " << filename << std::endl; + } + } +} + } // namespace game -- cgit v1.2.3