diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/base/cargo.cc | 67 | ||||
-rw-r--r-- | src/game/base/cargo.h | 2 | ||||
-rw-r--r-- | src/game/base/game.cc | 57 | ||||
-rw-r--r-- | src/game/base/game.h | 1 |
4 files changed, 118 insertions, 9 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"; diff --git a/src/game/base/cargo.h b/src/game/base/cargo.h index 9b2bca5..1e97d40 100644 --- a/src/game/base/cargo.h +++ b/src/game/base/cargo.h @@ -19,6 +19,8 @@ public: void buy(core::EntityControlable *buyer, core::Entity *seller, const int amount); + void sell(core::EntityControlable *seller, core::Entity *buyer, const int amount); + /* -- static functions -- */ static core::InfoType *cargo_infotype; diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 9f8dabc..83d0be8 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -267,34 +267,71 @@ void Game::func_give(core::Player *player, const std::string &args) } } +// sell request from a player +void Game::func_sell(core::Player *player, const std::string &args) +{ + std::istringstream is(args); + std::string typestr; + std::string labelstr; + + if (!(is >> typestr)) { + player->send("Usage: sell [string] [string] [int] sell an item: specify type, label and amount"); + return; + } else { + aux::to_label(typestr); + } + + if (!(is >> labelstr)) { + player->send("Usage: sell [string] [string] [int] sell an item: specify type, label and amount"); + return; + } else { + aux::to_label(labelstr); + } + + int amount = 0; + if (!(is >> amount)) + amount = 0; + + if (typestr.compare("cargo") == 0) { + Cargo *cargo = Cargo::find(labelstr); + if (cargo) { + cargo->sell(player->control(), player->view(), amount); + } else { + player->send("Unkown cargo type '" + labelstr + "'"); + } + } else { + player->send("Unkown item type '" + typestr + "'"); + } + return; +} + // buy request from a player void Game::func_buy(core::Player *player, const std::string &args) { std::istringstream is(args); std::string typestr; std::string labelstr; - int amount = 0; if (!(is >> typestr)) { - player->send("Usage: buy [string] [string] buy an item, specify type and label"); + player->send("Usage: buy [string] [string] [int] buy an item: specify type, label and amount"); return; } else { aux::to_label(typestr); } if (!(is >> labelstr)) { - labelstr.clear(); + player->send("Usage: buy [string] [string] [int] buy an item: specify type, label and amount"); + return; } else { aux::to_label(labelstr); } - + + int amount = 0; if (!(is >> amount)) amount = 0; if (typestr.compare("ship") == 0) { - ShipDealer::func_buy(player, labelstr); - - + ShipDealer::func_buy(player, labelstr); } else if (typestr.compare("cargo") == 0) { Cargo *cargo = Cargo::find(labelstr); if (cargo) { @@ -305,7 +342,6 @@ void Game::func_buy(core::Player *player, const std::string &args) } else { player->send("Unkown item type '" + typestr + "'"); } - return; } @@ -495,8 +531,11 @@ Game::Game() : core::Module("Project::OSiRiON", true) func->set_info("leave the game and spectate"); func = core::Func::add("buy", Game::func_buy); - func->set_info("[string] [string] buy type of item, name of item"); + func->set_info("[string] [string] [int] buy an item: specify type, label and amount"); + func = core::Func::add("sell", Game::func_sell); + func->set_info("[string] [string] [int] sell an item: specify type, label and amount"); + func = core::Func::add("give", Game::func_give); func->set_info("cheat functions"); diff --git a/src/game/base/game.h b/src/game/base/game.h index e091630..ce37fa6 100644 --- a/src/game/base/game.h +++ b/src/game/base/game.h @@ -107,6 +107,7 @@ private: static void func_respawn(core::Player *player, std::string const &args); static void func_goto(core::Player *player, const std::string &args); static void func_buy(core::Player *player, std::string const &args); + static void func_sell(core::Player *player, const std::string &args); static void func_give(core::Player *player, const std::string &args); }; |