/* base/shipdealer.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "auxiliary/functions.h" #include "base/game.h" #include "base/planet.h" #include "base/shipdealer.h" #include "base/station.h" #include "sys/sys.h" namespace game { ShipDealer::ShipDealer() { } ShipDealer::~ShipDealer() { dealer_models.clear(); } ShipModel *ShipDealer::add(const std::string &modelname) { ShipModel *model = ShipModel::find(modelname); if (!model) { con_warn << "Ship model '" + modelname + "' not found" << std::endl; return 0; } for (Models::iterator it = dealer_models.begin(); it != dealer_models.end(); it++) { if ((*it) == model) { con_warn << "Ship dealer already has " + model->name() << std::endl; return 0; } } dealer_models.push_back(model); return model; } ShipModel *ShipDealer::find(const std::string &modelname) const { for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) { if ((*it)->label().compare(modelname) == 0) { return (*it); } } return 0; } ShipModel *ShipDealer::find(ShipModel *shipmodel) const { for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) { if ((*it) == shipmodel) { return (*it); } } return 0; } // a player buys a ship void ShipDealer::func_buy(core::Player *player, const std::string &args) { core::Entity *dock = player->view(); ShipDealer *shipdealer = 0; // find the ship model ShipModel *shipmodel = ShipModel::find(args); if (!shipmodel) { if (!Game::g_devel->value()) { player->send("Cheats disabled"); return; } else { shipmodel = ShipModel::search(args); } } if (!shipmodel) { std::string helpstr; for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) { if (helpstr.size()) helpstr.append("^N|^B"); helpstr.append((*smit).second->label()); } player->send("Usage: buy ship [^B" + helpstr + "^N]"); return; } // find the ship dealer we're buying the ship from if (player->view()) { if (player->view()->moduletype() == station_enttype) { shipdealer = static_cast(player->view())->shipdealer(); } else if (player->view()->moduletype() == planet_enttype) { shipdealer = static_cast(player->view())->shipdealer(); } } if (!Game::g_devel->value()) { if (!shipdealer) { player->send("No ship dealer available"); return; } if (!shipdealer->find(shipmodel)) { player->send("Ship dealer does not sell the " + shipmodel->name()); return; } // check price if (shipmodel->price() > player->credits()) { std::stringstream msgstr; msgstr << "You require " << (shipmodel->price() - player->credits()) << " additional credits to buy the " << shipmodel->name(); player->send(msgstr.str()); return; } player->add_credits(-shipmodel->price()); } // player has only ship for now if (player->control()) { player->remove_asset(player->control()); } Ship * ship = new Ship(player, shipmodel); if (dock) { ship->set_zone(dock->zone()); ship->get_location().assign(dock->location()); ship->set_state(core::Entity::Docked); ship->get_axis().assign(dock->axis()); ship->get_axis().change_direction(180.0f); player->set_control(ship); player->set_view(dock); } else { ship->set_zone(player->zone()); player->set_control(ship); } // send the ship purchased message std::stringstream msgstr; msgstr << "^BPurchased " << aux::article(shipmodel->name()); if (!Game::g_devel->value()) { msgstr << " for " << shipmodel->price() << " credits"; } player->send(msgstr.str()); player->sound("game/buy-ship"); } }