From 765927f9f0e9ca751e1d94bd86aaf47200dcdb81 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 26 Sep 2010 14:55:35 +0000 Subject: moved trading definitions from station.ini to the zone.ini --- src/game/base/shipmodel.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/game/base/shipmodel.cc') diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc index e52aab2..6ff6c43 100644 --- a/src/game/base/shipmodel.cc +++ b/src/game/base/shipmodel.cc @@ -5,6 +5,7 @@ */ #include +#include #include "auxiliary/functions.h" #include "base/shipmodel.h" @@ -190,4 +191,66 @@ void ShipModel::list() core::Info::list(shipmodel_infotype); } +// a player buys a ship +void ShipModel::buy(core::EntityControlable *buyer, core::Entity *seller) +{ + if (!buyer || !seller) + return; + + // can only buy at planets and stations + if ((seller->moduletype() != station_enttype) && (seller->moduletype() != planet_enttype)) { + buyer->owner()->send("^BCan not buy here"); + return; + } + + if (!buyer->owner()) + return; + + if (!seller->inventory()) { + buyer->owner()->send("^BCan not buy here"); + return; + } + + core::Player *player = buyer->owner(); + + // seller is the station or planet + core::Item *seller_item = seller->inventory()->find(this); + if (!seller_item) { + if (player) { + player->send("^B" + seller->name() + " ^Bdoes not sell " + name()); + } + return; + } else { + assert(seller_item->info() == this); + } + + // check price + if (price() > player->credits()) { + player->send("^WCan not afford transaction!"); + return; + } + + player->add_credits(-price()); + + // player has only ship for now + if (player->control()) { + player->remove_asset(player->control()); + } + + Ship * ship = new Ship(player, this); + ship->set_zone(seller->zone()); + ship->get_location().assign(seller->location()); + ship->set_state(core::Entity::Docked); + ship->get_axis().assign(seller->axis()); + ship->get_axis().change_direction(180.0f); + player->set_control(ship); + player->set_view(seller); + + // send the ship purchased message + std::stringstream msgstr; + msgstr << "^BPurchased " << aux::article(name()) << " for " << price() << " credits"; + player->send(msgstr.str()); + player->sound("game/buy-ship"); +} + } -- cgit v1.2.3