diff options
Diffstat (limited to 'src/game/base/cargo.cc')
-rw-r--r-- | src/game/base/cargo.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/game/base/cargo.cc b/src/game/base/cargo.cc index 420d4d3..689ae92 100644 --- a/src/game/base/cargo.cc +++ b/src/game/base/cargo.cc @@ -103,16 +103,81 @@ Cargo *Cargo::find(const std::string & label) return (Cargo *) core::Info::find(cargo_infotype, label); } +// main 'sell cargo' function +void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int amount) +{ + if (!buyer || !seller) + return; + + // can only sell at planets and stations + if ((buyer->moduletype() != station_enttype) && (buyer->moduletype() != planet_enttype)) { + seller->owner()->send("^BCan not sell here"); + return; + } + + if (!seller->owner()) + return; + + if (!buyer->inventory() || !seller->inventory()) { + seller->owner()->send("^BCan not sell here"); + return; + } + + if (!amount) { + return; + } + + core::Item *seller_item = seller->inventory()->find(this); + if (!seller_item) { + if (seller->owner()) { + seller->owner()->send("^BYou do not own any " + name()); + } + return; + } + + int negotiated_amount = amount; + if (negotiated_amount < 0) { + negotiated_amount = seller_item->amount(); + } else if (negotiated_amount > seller_item->amount()) { + negotiated_amount = seller_item->amount(); + } + + int negotiated_price = price(); // base price + core::Item *buyer_item = buyer->inventory()->find(this); + if (buyer_item) { + 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(); + + // send a cargo purchased message + std::stringstream msgstr; + msgstr << "^BSold " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name() << " for " << negotiated_price * negotiated_amount << " credits"; + seller->owner()->send(msgstr.str()); + seller->owner()->sound("game/buy"); + +} + // main 'buy cargo' function void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int amount) { 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 (!buyer->inventory() || !seller->inventory()) { + buyer->owner()->send("^BCan not buy here"); return; } @@ -156,6 +221,8 @@ void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int buyer_item->inc_amount(negotiated_amount); buyer->owner()->set_credits(buyer->owner()->credits() - negotiated_price * negotiated_amount); + buyer->inventory()->set_dirty(); + // send a cargo purchased message std::stringstream msgstr; msgstr << "^BPurchased " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name() << " for " << negotiated_price * negotiated_amount << " credits"; |