diff options
Diffstat (limited to 'src/game/base/cargo.cc')
| -rw-r--r-- | src/game/base/cargo.cc | 70 | 
1 files changed, 54 insertions, 16 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); | 
