diff options
author | Stijn Buys <ingar@osirion.org> | 2008-11-10 18:02:13 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-11-10 18:02:13 +0000 |
commit | 03d5bc014c0e815c6aaeec16d81e225e08732ab0 (patch) | |
tree | 05aa0464284059bdc05d96f3cc17e081a0da78ee /src/game/base/shipdealer.cc | |
parent | a1eb1b4dc4d81df724ee43fc4e895dd22e81760f (diff) |
adds jumpgate, station, ship dealer
Diffstat (limited to 'src/game/base/shipdealer.cc')
-rw-r--r-- | src/game/base/shipdealer.cc | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/game/base/shipdealer.cc b/src/game/base/shipdealer.cc new file mode 100644 index 0000000..1a827ec --- /dev/null +++ b/src/game/base/shipdealer.cc @@ -0,0 +1,145 @@ +/* + 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(); + ShipModel *shipmodel = 0; + ShipDealer *shipdealer = 0; + + // find the ship model + std::string shipname; + std::string helpstr; + std::istringstream is(args); + is >> shipname; + aux::to_label(shipname); + + if (shipname.size()) { + for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) { + if (shipname == (*smit).first) { + shipmodel = (*smit).second; + break; + } + + if (helpstr.size()) + helpstr.append("^N|^B"); + helpstr.append((*smit).second->label()); + } + } + + if (!shipmodel) { + player->send("Usage: buy [^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<Station *>(player->view())->shipdealer(); + } else if (player->view()->moduletype() == planet_enttype) { + shipdealer = static_cast<Planet *>(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; + } + } + + // 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->location().assign(dock->location()); + ship->set_eventstate(core::Entity::Docked); + ship->entity_axis.assign(dock->axis()); + ship->entity_axis.change_direction(180.0f); + player->set_control(ship); + player->set_view(dock); + } else { + ship->set_zone(player->zone()); + player->set_control(ship); + } + + player->send("^BPurchased " + aux::article(shipmodel->name())); + player->sound("game/buy-ship"); + + +} + + +} |