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')
-rw-r--r--src/game/base/cargo.cc70
-rw-r--r--src/game/base/ship.cc6
-rw-r--r--src/game/base/shipmodel.h2
3 files changed, 61 insertions, 17 deletions
diff --git a/src/game/base/cargo.cc b/src/game/base/cargo.cc
index 689ae92..536a701 100644
--- a/src/game/base/cargo.cc
+++ b/src/game/base/cargo.cc
@@ -4,6 +4,8 @@
the terms and conditions of the GNU General Public License version 2
*/
+#include <assert.h>
+
#include "base/game.h"
#include "base/cargo.h"
#include "filesystem/inifile.h"
@@ -36,6 +38,7 @@ bool Cargo::init()
Cargo *cargo = 0;
std::string str;
long l;
+ float f;
while (cargoini.getline()) {
if (cargoini.got_key()) {
@@ -61,6 +64,11 @@ bool Cargo::init()
} else if (cargoini.got_key_long("price", l)) {
cargo->set_price(l);
continue;
+
+ } else if (cargoini.got_key_float("volume", f)) {
+ cargo->set_volume(f);
+ continue;
+
} else {
cargoini.unkown_key();
}
@@ -88,6 +96,7 @@ bool Cargo::init()
Cargo::Cargo() : core::Info(cargo_infotype)
{
+ set_volume(1);
}
Cargo::~Cargo()
@@ -148,7 +157,6 @@ void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int
negotiated_price = buyer_item->price();
}
-
seller_item->set_amount(seller_item->amount() - negotiated_amount);
seller->owner()->set_credits(seller->owner()->credits() + negotiated_price * negotiated_amount);
seller->inventory()->set_dirty();
@@ -184,40 +192,70 @@ void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int
if (!amount) {
return;
}
-
- core::Item *buyer_item = buyer->inventory()->find(this);
- core::Item *seller_item = seller->inventory()->find(this);
-
+
+ // seller is the station or planet
+ core::Item *seller_item = seller->inventory()->find(this);
if (!seller_item) {
if (buyer->owner()) {
buyer->owner()->send("^B" + seller->name() + " ^Bdoes not sell " + name());
}
return;
+ } else {
+ assert(seller_item->info() == this);
}
int negotiated_amount = amount;
int negotiated_price = seller_item->price();
long cash = buyer->owner()->credits();
- // negative means 'as much as possible'
- if (negotiated_amount < 0) {
- negotiated_amount = cash / negotiated_price;
+ // check if the player has enough cash
+ if (negotiated_price > 0) {
+
+ // negative amount means 'as much as possible'
+ if (negotiated_amount < 0) {
+ negotiated_amount = cash / negotiated_price;
+ }
+
+ // maximum amount the player can afford
+ if (cash < negotiated_amount * negotiated_price) {
+ negotiated_amount = cash / negotiated_price;
+ }
+
+ if (negotiated_amount < 1) {
+ buyer->owner()->send("^WCan not afford transaction!");
+ return;
+ }
}
- if (cash < negotiated_amount * negotiated_price) {
- negotiated_amount = cash / negotiated_price;
+ // check cargo size - ignore zero volume cargo
+ if (volume() > 0 ) {
+
+ // maximum cargo size
+ if (negotiated_amount * volume() > buyer->inventory()->capacity_available()) {
+ negotiated_amount = buyer->inventory()->capacity_available() / volume();
+ }
+
+ if (negotiated_amount < 1) {
+ buyer->owner()->send("^WNot enough cargo space available!");
+ return;
+ }
}
- if (!negotiated_amount) {
- buyer->owner()->send("^WCan not afford transaction!");
- return;
- }
- // TODO cargo size check
+ if (negotiated_amount < 0) {
+ // unlimited amount of zero-cost cargo
+ buyer->owner()->send("^WNo unlimited amounts of zero-cost cargo available!");
+ return;
+ }
+
+ // buyer is the player
+ core::Item *buyer_item = buyer->inventory()->find(this);
if (!buyer_item) {
buyer_item = new core::Item(this);
buyer->inventory()->add(buyer_item);
- }
+ } else {
+ assert(buyer_item->info() == this);
+ }
buyer_item->inc_amount(negotiated_amount);
buyer->owner()->set_credits(buyer->owner()->credits() - negotiated_price * negotiated_amount);
diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc
index 23929ea..d51a119 100644
--- a/src/game/base/ship.cc
+++ b/src/game/base/ship.cc
@@ -39,14 +39,20 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable(
ship_jumpdepart = 0;
if (owner) {
+ // this ship is owned by a player
set_owner(owner);
get_color().assign(owner->color());
get_color_second().assign(owner->color_second());
std::string str(aux::text_strip(owner->name()));
aux::to_label(str);
set_label(str);
+
+ // add an inventory
set_inventory(new core::Inventory());
+ inventory()->set_capacity(shipmodel->maxcargo());
+
} else {
+ set_name(shipmodel->name());
set_label(shipmodel->label());
}
diff --git a/src/game/base/shipmodel.h b/src/game/base/shipmodel.h
index c5da05e..ef7ff15 100644
--- a/src/game/base/shipmodel.h
+++ b/src/game/base/shipmodel.h
@@ -51,7 +51,7 @@ public:
return shipmodel_turnspeed;
}
- /// size of the cargo hold
+ /// size of the cargo hold, in cubic meters
inline const float maxcargo() const {
return shipmodel_maxcargo;
}