Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/base/shipmodel.cc')
-rw-r--r--src/game/base/shipmodel.cc63
1 files changed, 63 insertions, 0 deletions
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 <iomanip>
+#include <assert.h>
#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");
+}
+
}