From ae3127e1f98b74bab9b40ed774d4a520507a325b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 12 Jul 2011 13:03:01 +0000 Subject: Save and load player inventories, corrected a typo in saving the forward vector of the ship axis. --- src/game/base/game.cc | 155 +++++++++++++++++++++++++++++++++------------ src/game/base/jumppoint.cc | 2 +- src/game/base/ship.cc | 4 +- 3 files changed, 117 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 0aa1cbc..1342ea0 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -1260,7 +1260,7 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("faction", strval)) { Faction *faction = Faction::find(strval); if (!faction) { - zoneini.unknown_error("unkown faction '" + strval + "'"); + zoneini.unknown_error("unknown faction '" + strval + "'"); } else { faction->apply(navpoint); } @@ -1284,14 +1284,14 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("faction", strval)) { Faction *faction = Faction::find(strval); if (!faction) { - zoneini.unknown_error("unkown faction '" + strval + "'"); + zoneini.unknown_error("unknown faction '" + strval + "'"); } else { faction->apply(jumppoint); } } else if (zoneini.got_key_label("template", strval)) { Template *entitytemplate = Template::find(strval); if (!entitytemplate) { - zoneini.unknown_error("unkown template '" + strval + "'"); + zoneini.unknown_error("unknown template '" + strval + "'"); } else { entitytemplate->apply(jumppoint); } @@ -1319,7 +1319,7 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("faction", strval)) { Faction *faction = Faction::find(strval); if (!faction) { - zoneini.unknown_error("unkown faction '" + strval + "'"); + zoneini.unknown_error("unknown faction '" + strval + "'"); } else { faction->apply(planet); } @@ -1337,21 +1337,21 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("faction", strval)) { Faction *faction = Faction::find(strval); if (!faction) { - zoneini.unknown_error("unkown faction '" + strval + "'"); + zoneini.unknown_error("unknown faction '" + strval + "'"); } else { faction->apply(station); } } else if (zoneini.got_key_label("template", strval)) { Template *entitytemplate = Template::find(strval); if (!entitytemplate) { - zoneini.unknown_error("unkown template '" + strval + "'"); + zoneini.unknown_error("unknown template '" + strval + "'"); } else { entitytemplate->apply(station); } } else if (zoneini.got_key_label("ship", strval)) { ShipModel *shipmodel = ShipModel::find(strval); if (!shipmodel) { - zoneini.unknown_error("unkown ship type '" + strval + "'"); + zoneini.unknown_error("unknown ship type '" + strval + "'"); } else { shipmodel->apply(station); } @@ -1383,7 +1383,7 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("faction", strval)) { Faction *faction = Faction::find(strval); if (!faction) { - zoneini.unknown_error("unkown faction '" + strval + "'"); + zoneini.unknown_error("unknown faction '" + strval + "'"); } else { entity->set_color(faction->color()); entity->set_color_second(faction->color_second()); @@ -1391,14 +1391,14 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("template", strval)) { Template *entitytemplate = Template::find(strval); if (!entitytemplate) { - zoneini.unknown_error("unkown template '" + strval + "'"); + zoneini.unknown_error("unknown template '" + strval + "'"); } else { entitytemplate->apply(entity); } } else if (zoneini.got_key_label("ship", strval)) { ShipModel *shipmodel = ShipModel::find(strval); if (!shipmodel) { - zoneini.unknown_error("unkown ship type '" + strval + "'"); + zoneini.unknown_error("unknown ship type '" + strval + "'"); } else { shipmodel->apply(entity); } @@ -1406,7 +1406,7 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_key_label("ship", strval)) { ShipModel *shipmodel = ShipModel::find(strval); if (!shipmodel) { - zoneini.unknown_error("unkown ship type '" + strval + "'"); + zoneini.unknown_error("unknown ship type '" + strval + "'"); } else { // TODO apply ship model } @@ -1431,7 +1431,7 @@ bool Game::load_zone(core::Zone *zone) inventory->add(item); } } else { - zoneini.unknown_error("unkown cargo type '" + strval + "'"); + zoneini.unknown_error("unknown cargo type '" + strval + "'"); } } else if (zoneini.got_key_long("price", l)) { @@ -1463,7 +1463,7 @@ bool Game::load_zone(core::Zone *zone) inventory->add(item); } } else { - zoneini.unknown_error("unkown ship type '" + strval + "'"); + zoneini.unknown_error("unknown ship type '" + strval + "'"); } } else if (zoneini.got_key_long("price", l)) { @@ -1733,10 +1733,16 @@ void Game::player_load(core::Player *player) Ship *ship = 0; long l; + bool b; std::string str; math::Vector3f v; + math::Vector3f location; math::Axis axis; bool ship_is_docked = false; + core::Zone *zone = 0; + core::Item *item = 0; + std::string itemtype; + std::string itemlabel; while (inifile.getline()) { @@ -1746,6 +1752,13 @@ void Game::player_load(core::Player *player) continue; } else if (inifile.got_section("ship")) { continue; + } else if (inifile.got_section("item")) { + if (ship) { + item = 0; + itemtype.clear(); + itemlabel.clear(); + } + continue; } else { inifile.unknown_section(); } @@ -1764,6 +1777,7 @@ void Game::player_load(core::Player *player) } else { inifile.unknown_key(); } + } else if (inifile.in_section("ship")) { if (inifile.got_key_label("model", str)) { @@ -1780,12 +1794,10 @@ void Game::player_load(core::Player *player) continue; } else if (inifile.got_key_label("zone", str)) { - ship->set_zone(core::Zone::find(str)); + zone = core::Zone::find(str); continue; - } else if (inifile.got_key_vector3f("location", v)) { - if (ship) - ship->get_location().assign(v); + } else if (inifile.got_key_vector3f("location", location)) { continue; } else if (inifile.got_key_vector3f("forward", axis[0])) { @@ -1802,34 +1814,81 @@ void Game::player_load(core::Player *player) } else if (inifile.got_key_string("spawn", str)) { if (str.size() < 3) { - con_warn << " Spawn with invalid label '" << str << "'\n"; + inifile.unknown_error("spawn with invalid label '" + str + "'"); continue; } size_t pos = str.find(':'); + if ((pos == std::string::npos) || (pos < 1) || (pos >= (str.size() - 1))) { + inifile.unknown_error("spawn with invalid label '" + str + "'"); + continue; + } std::string zonelabel(str.substr(0, pos)); std::string entitylabel(str.substr(pos + 1, str.size() - pos)); + + aux::to_label(zonelabel); + aux::to_label(entitylabel); core::Zone *spawn_zone = core::Zone::find(zonelabel); if (!spawn_zone) { - con_warn << " Spawn with unkown zone '" << zonelabel << "'\n"; + inifile.unknown_error("spawn with invalid zone'" + zonelabel + "'"); continue; } core::Entity *spawn_entity = spawn_zone->find_entity(entitylabel); if (!spawn_entity) { - con_warn << " Spawn with unkown entity '" << str << "'\n"; + inifile.unknown_error("spawn with invalid entity'" + str + "'"); continue; } if (!spawn_entity->flag_is_set(core::Entity::Dockable)) { - con_warn << " Spawn '" << str << "' is not dockable\n"; + inifile.unknown_error("spawn '" + str + "' is not dockable"); continue; } if (ship) { ship->set_spawn(spawn_entity); } + } else { + inifile.unknown_key(); + } + + } else if (inifile.in_section("item")) { + + if (inifile.got_key_label("type", itemtype)) { + continue; + + } else if (inifile.got_key_label("label", itemlabel)) { + if (!item && ship) { + core::InfoType *item_infotype = core::InfoType::find(itemtype); + if (!itemtype.size() || !item_infotype) { + inifile.unknown_error("invalid item type '" + itemtype +"'"); + continue; + } + + core::Info *item_info = core::Info::find(item_infotype, itemlabel); + if (!itemlabel.size() || !item_info) { + inifile.unknown_error("invalid item label '" + itemlabel +"'"); + continue; + } + item = new core::Item(item_info); + ship->inventory()->add(item); + } + } else if (inifile.got_key_long("amount", l)) { + if (item) { + item->set_amount(l); + } + } else if (inifile.got_key_long("price", l)) { + if (item) { + item->set_price(l); + } + } else if (inifile.got_key_bool("tradeable", b)) { + if (item) { + item->set_flag(core::Item::Tradeable); + } + } else { + inifile.unknown_key(); } + } } } @@ -1837,9 +1896,10 @@ void Game::player_load(core::Player *player) inifile.close(); if (ship) { - - if (!ship->zone()) { - ship->set_zone(Default::zone); + ship->inventory()->set_dirty(); + + if (!zone) { + zone = Default::zone; } if (!ship->spawn()) { @@ -1853,15 +1913,14 @@ void Game::player_load(core::Player *player) player->set_control(ship); player->set_view(ship->spawn()); } else { + ship->set_location(location); ship->set_axis(axis); ship->set_state(core::Entity::Normal); + ship->set_zone(zone); ship->reset(); - player->set_zone(ship->zone()); player->set_control(ship); } - - } } @@ -1904,24 +1963,22 @@ void Game::player_save(core::Player *player) con_warn << "Could not write " << filename << std::endl; return; } - // *--- HEADER + + /* ---- HEADER ----------------------------------------------- */ ofs << "; " << guid << ".ini" << std::endl; ofs << "; Project::OSiRiON player data" << std::endl; - ofs << std::endl; - - // *--- PLAYER - ofs << "[player]" << std::endl; + /* ---- PLAYER ----------------------------------------------- */ + ofs << std::endl; + ofs << "[player]" << std::endl; // player name - ofs << "name=" << player->name() << std::endl; - + ofs << "name=" << player->name() << std::endl; // credit - ofs << "credits=" << player->credits() << std::endl; - - // *--- SHIP - ofs << std::endl; + ofs << "credits=" << player->credits() << std::endl; + /* ---- SHIP ------------------------------------------------- */ Ship * ship = static_cast(player->control()); + ofs << std::endl; ofs << "[ship]" << std::endl; ofs << "model=" << ship->shipmodel()->label() << std::endl; // ship zone @@ -1929,7 +1986,7 @@ void Game::player_save(core::Player *player) // ship location ofs << "location=" << ship->location() << std::endl; // ship orientation - ofs << "foward=" << ship->axis().forward() << std::endl; + ofs << "forward=" << 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 @@ -1941,7 +1998,6 @@ void Game::player_save(core::Player *player) } else { ofs << "no"; } - ofs << std::endl; // ship spawn ofs << "spawn="; @@ -1950,8 +2006,23 @@ void Game::player_save(core::Player *player) } ofs << std::endl; - // TODO inventory - + /* ---- INVENTORY -------------------------------------------- */ + assert (ship->inventory()); + for (core::Inventory::Items::iterator it = ship->inventory()->items().begin(); + it != ship->inventory()->items().end(); ++it) + { + core::Item *item = (*it); + + ofs << std::endl; + ofs << "[item]" << std::endl; + // item InfoType label + ofs << "type=" << item->info()->type()->label() << std::endl; + // item Info label + ofs << "label=" << item->info()->label() << std::endl; + ofs << "amount=" << item->amount() << std::endl; + ofs << "price=" << item->price() << std::endl; + ofs << "tradeable=" << (item->flag_is_set(core::Item::Tradeable) ? "yes" : "no") << std::endl; + } ofs.close(); } } diff --git a/src/game/base/jumppoint.cc b/src/game/base/jumppoint.cc index 12516c0..08c7e67 100644 --- a/src/game/base/jumppoint.cc +++ b/src/game/base/jumppoint.cc @@ -53,7 +53,7 @@ void JumpPoint::validate() return; } size_t pos = targetlabel().find(':'); - if ((pos < 1) || (pos >= (targetlabel().size() - 1))) { + if ((pos == std::string::npos) || (pos < 1) || (pos >= (targetlabel().size() - 1))) { con_warn << " Jumppoint with invalid target label '" << targetlabel() << "'\n"; return; } diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index 0bc1b7c..459b8fd 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -299,20 +299,22 @@ void Ship::set_state(int state) if (state != core::Entity::Docked) ship_dock = 0; } + void Ship::set_dock(core::Entity *dock) { if (!dock) return; - ship_dock = dock; get_location().assign(dock->location()); get_axis().assign(dock->axis()); + ship_dock = dock; set_state(core::Entity::Docked); // if the dock is not owned by a player. set it as spawn const core::Player *owner = (dock->type() == core::Entity::Controlable ? static_cast(dock)->owner() : 0 ); if (!owner) set_spawn(dock); + reset(); } -- cgit v1.2.3