diff options
author | Stijn Buys <ingar@osirion.org> | 2010-09-19 22:49:55 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2010-09-19 22:49:55 +0000 |
commit | e8f7c4a06fce9e41fb23ffc42a566501a78210cb (patch) | |
tree | 3adf0b1f3eae778e9645450d0e51821142f7983b | |
parent | cc18095cded14f5e7e3f049e47fca2224134b647 (diff) |
player can only sell cargo on bases that allow it, trademenu layout updates
-rw-r--r-- | src/client/inventorylistview.h | 4 | ||||
-rw-r--r-- | src/client/playerview.cc | 4 | ||||
-rw-r--r-- | src/client/trademenu.cc | 30 | ||||
-rw-r--r-- | src/client/trademenu.h | 4 | ||||
-rw-r--r-- | src/core/inventory.cc | 4 | ||||
-rw-r--r-- | src/core/inventory.h | 12 | ||||
-rw-r--r-- | src/core/item.cc | 6 | ||||
-rw-r--r-- | src/core/item.h | 10 | ||||
-rw-r--r-- | src/game/base/cargo.cc | 38 | ||||
-rw-r--r-- | src/game/base/game.cc | 16 |
10 files changed, 89 insertions, 39 deletions
diff --git a/src/client/inventorylistview.h b/src/client/inventorylistview.h index 0d14072..ff10aa1 100644 --- a/src/client/inventorylistview.h +++ b/src/client/inventorylistview.h @@ -21,6 +21,10 @@ public: void set_inventory(core::Inventory *inventory, core::InfoType *infotype); + inline const core::Inventory *inventory() const { + return listview_inventory; + } + protected: virtual void draw(); diff --git a/src/client/playerview.cc b/src/client/playerview.cc index 2c2ecaa..b5629f0 100644 --- a/src/client/playerview.cc +++ b/src/client/playerview.cc @@ -146,9 +146,9 @@ void PlayerView::show_menu(const std::string & args) // hide other menus view_buymenu->hide(); view_entitymenu->hide(); - // show trade menu + // show trade menu + view_trademenu->set_itemtype(core::InfoType::find(typestr)); view_trademenu->show(); - view_trademenu->set_item_type(core::InfoType::find(typestr)); } else { con_print << "usage: view trade [string] show the trade menu for this type of items" << std::endl; } diff --git a/src/client/trademenu.cc b/src/client/trademenu.cc index 7ac3e7c..47655f6 100644 --- a/src/client/trademenu.cc +++ b/src/client/trademenu.cc @@ -79,7 +79,7 @@ TradeMenu::TradeMenu(ui::Widget *parent, const char * label) : ui::Window(parent std::string test("test"); - set_item_type(0); + set_itemtype(0); hide(); } @@ -89,10 +89,11 @@ TradeMenu::~TradeMenu() } -void TradeMenu::set_item_type(core::InfoType *item_type) +void TradeMenu::set_itemtype(core::InfoType *item_type) { // reset menu_namelabel->set_text(0); + menu_itemtype = item_type; core::Inventory *inventory_player = 0; core::Inventory *inventory_view = 0; @@ -123,11 +124,13 @@ void TradeMenu::set_item(ui::ListItem *item) menu_modelview->hide(); return; } + + long amount = 1; // reserved if (item->parent() == menu_inventorylistview) { menu_traderlistview->deselect(); // item in ship inventory selected (SELL) - menu_namelabel->set_text("Sell " + item->text()); + menu_namelabel->set_text("Sell " + item->info()->name()); menu_buyallbutton->hide(); menu_buybutton->hide(); @@ -137,20 +140,29 @@ void TradeMenu::set_item(ui::ListItem *item) menu_sellbutton->show(); menu_sellbutton->set_command("remote sell " + item->info()->type()->label() + " " + item->info()->label() + " 1"); + + const core::Item *trader_item = (menu_traderlistview->inventory() ? menu_traderlistview->inventory()->find(item->info()) : 0); + if (trader_item) { + std::ostringstream str; + str << "Price: " << std::setw(8) << amount * trader_item->price() << '\n' << "Volume: " << std::setw(7) << std::setprecision(2) << amount * trader_item->info()->volume(); + menu_tradertext->set_text(str.str()); + } else { + menu_tradertext->set_text("^1Can not sell here"); + } } else if (item->parent() == menu_traderlistview) { menu_inventorylistview->deselect(); // item in trader inventory selected (BUY) - menu_namelabel->set_text("Buy " + item->text()); + menu_namelabel->set_text("Buy " + item->info()->name()); menu_buyallbutton->show(); menu_buyallbutton->set_command("remote buy " + item->info()->type()->label() + " " + item->info()->label() + " -1"); menu_buybutton->show(); - menu_buybutton->set_command("remote buy " + item->info()->type()->label() + " " + item->info()->label() + " 1; "); + menu_buybutton->set_command("remote buy " + item->info()->type()->label() + " " + item->info()->label() + " 1"); std::ostringstream str; - str << "Price: " << std::setw(8) << item->item()->price() << '\n' << "Volume: " << std::setw(7) << std::setprecision(2) << item->info()->volume(); + str << "Price: " << std::setw(8) << amount * item->item()->price() << '\n' << "Volume: " << std::setw(7) << std::setprecision(2) << amount * item->info()->volume(); menu_tradertext->set_text(str.str()); menu_sellallbutton->hide(); @@ -246,6 +258,12 @@ bool TradeMenu::on_emit(Widget *sender, const Event event, void *data) void TradeMenu::draw() { + // sanity check + if ((menu_inventorylistview->inventory() != (core::localcontrol() ? core::localcontrol()->inventory() : 0 )) || + (menu_traderlistview->inventory() != (core::localplayer()->view() ? core::localplayer()->view()->inventory() : 0 ))) { + set_itemtype(menu_itemtype); + } + std::stringstream str; str << "Credit: " << std::setw(8) << core::localplayer()->credits(); diff --git a/src/client/trademenu.h b/src/client/trademenu.h index 6ee53b5..4682ce7 100644 --- a/src/client/trademenu.h +++ b/src/client/trademenu.h @@ -29,7 +29,7 @@ public: ~TradeMenu(); /// set the item type to trade - void set_item_type(core::InfoType *item_type); + void set_itemtype(core::InfoType *item_type); protected: /// resize event handler @@ -60,6 +60,8 @@ private: InventoryListView *menu_traderlistview; ui::Text menu_infotext; + + core::InfoType *menu_itemtype; }; } diff --git a/src/core/inventory.cc b/src/core/inventory.cc index 8f8c41a..03cd21e 100644 --- a/src/core/inventory.cc +++ b/src/core/inventory.cc @@ -65,10 +65,10 @@ void Inventory::remove(Item *item) } } -Item *Inventory::find(const Info *info) +Item *Inventory::find(const Info *info) const { // sarch the inventory for a specified item type - for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { + for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { Item *item = (*it); if (item->info() == info) { return item; diff --git a/src/core/inventory.h b/src/core/inventory.h index aff9790..805d943 100644 --- a/src/core/inventory.h +++ b/src/core/inventory.h @@ -72,6 +72,11 @@ public: return inventory_capacity - inventory_capacity_used; } + /** + * @brief search the inventory for a specific item type + */ + Item *find(const Info *info) const; + /* ---- mutators --------------------------------------------------- */ /** @@ -88,12 +93,7 @@ public: * @brief removes all items from the inventory and delete them */ void clear(); - - /** - * @brief search the inventory for a specific item type - */ - Item *find(const Info *info); - + /** * @brief set the timestamp */ diff --git a/src/core/item.cc b/src/core/item.cc index ca51775..77b0f9c 100644 --- a/src/core/item.cc +++ b/src/core/item.cc @@ -25,17 +25,17 @@ Item::~Item() item_amount = 0; } -void Item::set_amount(const int amount) +void Item::set_amount(const long amount) { item_amount = amount; } -void Item::inc_amount(const int amount) +void Item::inc_amount(const long amount) { item_amount += amount; } -void Item::dec_amount(const int amount) +void Item::dec_amount(const long amount) { item_amount -= amount; } diff --git a/src/core/item.h b/src/core/item.h index cc81249..9c17dc9 100644 --- a/src/core/item.h +++ b/src/core/item.h @@ -28,7 +28,7 @@ public: /** * @brief associated amount */ - inline int amount() const { return item_amount; } + inline long amount() const { return item_amount; } /** * @brief information card @@ -45,18 +45,18 @@ public: /** * @brief set associated amount */ - void set_amount(const int amount); + void set_amount(const long amount); - void inc_amount(const int amount); + void inc_amount(const long amount); - void dec_amount(const int amount); + void dec_amount(const long amount); void set_price(const long price); private: const Info *item_info; long item_price; - int item_amount; + long item_amount; }; } // namespace core diff --git a/src/game/base/cargo.cc b/src/game/base/cargo.cc index 536a701..382b03c 100644 --- a/src/game/base/cargo.cc +++ b/src/game/base/cargo.cc @@ -60,7 +60,7 @@ bool Cargo::init() } else if (cargoini.got_key_string("model", str)) { cargo->set_modelname(str); continue; - + } else if (cargoini.got_key_long("price", l)) { cargo->set_price(l); continue; @@ -136,6 +136,7 @@ void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int return; } + // seller is the player core::Item *seller_item = seller->inventory()->find(this); if (!seller_item) { if (seller->owner()) { @@ -144,6 +145,13 @@ void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int return; } + // buyer is the station or planer + core::Item *buyer_item = buyer->inventory()->find(this); + if (!buyer_item) { + seller->owner()->send("^B" + buyer->name() + " ^Bdoes not buy " + name()); + return; + } + int negotiated_amount = amount; if (negotiated_amount < 0) { negotiated_amount = seller_item->amount(); @@ -151,16 +159,17 @@ void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int 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(); - } + int negotiated_price = buyer_item->price(); - seller_item->set_amount(seller_item->amount() - negotiated_amount); + seller_item->dec_amount(negotiated_amount); seller->owner()->set_credits(seller->owner()->credits() + negotiated_price * negotiated_amount); seller->inventory()->set_dirty(); + if (buyer_item->amount() >= 0) { + buyer_item->inc_amount(negotiated_amount); + buyer->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"; @@ -201,7 +210,7 @@ void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int } return; } else { - assert(seller_item->info() == this); + assert(seller_item->info() == this); } int negotiated_amount = amount; @@ -241,13 +250,22 @@ void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int } } - if (negotiated_amount < 0) { + if (negotiated_amount <= 0) { // unlimited amount of zero-cost cargo buyer->owner()->send("^WNo unlimited amounts of zero-cost cargo available!"); return; } - + // if amount is set to -1. the base has a limitless supply + if (seller_item->amount() == 0) { + buyer->owner()->send("^WCargo not available!"); + return; + } else if ((seller_item->amount() > 0) && (negotiated_amount > seller_item->amount())) { + negotiated_amount = seller_item->amount(); + seller_item->dec_amount(negotiated_amount); + seller->inventory()->set_dirty(); + } + // buyer is the player core::Item *buyer_item = buyer->inventory()->find(this); if (!buyer_item) { diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 83d0be8..803657d 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -227,6 +227,10 @@ void Game::func_give(core::Player *player, const std::string &args) } Ship * ship = new Ship(player, shipmodel); + core::Entity *view = player->view(); + if (view && (view == player->control())) { + view = ship; + } // FIME move this into a method in the Ship class ship->set_zone(player->control()->zone()); @@ -235,12 +239,12 @@ void Game::func_give(core::Player *player, const std::string &args) ship->get_axis().assign(player->control()->axis()); ship->set_speed(player->control()->speed()); ship->set_thrust(player->control()->thrust()); + //target_thrust is protected + //ship->target_thrust = player->control()->target_thrust()); - if (player->view() == player->control()) { - player->set_view(ship); - } player->remove_asset(player->control()); player->set_control(ship); + player->set_view(view); player->sound("game/buy-ship"); @@ -983,7 +987,11 @@ bool Game::load_menus(core::Entity *entity, const std::string &menufilename) } else if (inifile.got_key_long("price", l)) { if (item) { item->set_price(l); - } + } + } else if (inifile.got_key_long("amount", l)) { + if (item) { + item->set_amount(l); + } } else { inifile.unkown_key(); } |