diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-18 00:27:11 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-18 00:27:11 +0000 |
commit | 290aab364a310b18bb02567f6e4e424d94148bf9 (patch) | |
tree | 13a860174c635db34e6087aa31c001386cbd6a7c | |
parent | 19b58f22f7e77382183c70d2b6a1c79b5e1584e7 (diff) | |
download | mana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.gz mana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.bz2 mana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.xz mana-290aab364a310b18bb02567f6e4e424d94148bf9.zip |
Created Inventory class. (Really) Small code simplifications and cleanups.
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/game.cpp | 39 | ||||
-rw-r--r-- | src/gui/inventorywindow.cpp (renamed from src/gui/inventory.cpp) | 60 | ||||
-rw-r--r-- | src/gui/inventorywindow.h (renamed from src/gui/inventory.h) | 21 | ||||
-rw-r--r-- | src/gui/item_amount.cpp | 8 | ||||
-rw-r--r-- | src/gui/itemcontainer.cpp | 143 | ||||
-rw-r--r-- | src/gui/itemcontainer.h | 46 | ||||
-rw-r--r-- | src/gui/sell.cpp | 4 | ||||
-rw-r--r-- | src/gui/status.cpp | 2 | ||||
-rw-r--r-- | src/gui/trade.cpp | 60 | ||||
-rw-r--r-- | src/gui/trade.h | 8 | ||||
-rw-r--r-- | src/inventory.cpp | 134 | ||||
-rw-r--r-- | src/inventory.h | 114 |
14 files changed, 373 insertions, 274 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index dd2711ca..1c98073c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -33,8 +33,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ gui/gui.h \ gui/help.cpp \ gui/help.h \ - gui/inventory.cpp \ - gui/inventory.h \ + gui/inventorywindow.cpp \ + gui/inventorywindow.h \ gui/itemcontainer.cpp \ gui/itemcontainer.h \ gui/item_amount.cpp \ @@ -140,6 +140,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ game.h \ graphics.cpp \ graphics.h \ + inventory.cpp \ + inventory.h \ item.cpp \ item.h \ log.cpp \ diff --git a/src/engine.cpp b/src/engine.cpp index a92805b3..f0df72a8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -38,7 +38,7 @@ #include "gui/buy.h" #include "gui/sell.h" #include "gui/buysell.h" -#include "gui/inventory.h" +#include "gui/inventorywindow.h" #include "gui/npc_text.h" #include "gui/npc.h" #include "gui/stats.h" diff --git a/src/game.cpp b/src/game.cpp index 5ab9470f..bbb9d918 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -31,7 +31,7 @@ #include "equipment.h" #include "gui/chat.h" #include "gui/gui.h" -#include "gui/inventory.h" +#include "gui/inventorywindow.h" #include "gui/shop.h" #include "gui/npc.h" #include "gui/stats.h" @@ -78,6 +78,7 @@ OkDialog *deathNotice = NULL; ConfirmDialog *exitConfirm = NULL; Being *target = NULL; +Inventory *inventory = NULL; const int EMOTION_TIME = 150; /**< Duration of emotion icon */ const int MAX_TIME = 10000; @@ -208,6 +209,9 @@ void do_init() void game() { + // Needs to be initialised _before_ the engine is created... + inventory = new Inventory(); + engine = new Engine(); do_init(); @@ -1064,25 +1068,22 @@ void do_parse() } else { - tradeWindow->addItem( - tradeWindow->partnerItems->getFreeSlot(), RFIFOW(6), - false, RFIFOL(2), false); + tradeWindow->addItem(RFIFOW(6), false, RFIFOL(2), false); } break; // Trade: New Item add response case 0x01b1: { - Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + Item *item = inventory->getItem(RFIFOW(2)); switch (RFIFOB(6)) { case 0: // Successfully added item if (item->isEquipment() && item->isEquipped()) { - inventoryWindow->unequipItem(item); + inventory->unequipItem(item); } tradeWindow->addItem( - tradeWindow->myItems->getFreeSlot(), item->getId(), true, RFIFOW(4), item->isEquipment()); item->increaseQuantity(-RFIFOW(4)); @@ -1130,18 +1131,18 @@ void do_parse() // Only called on map load / warp case 0x01ee: // Reset all items to not load them twice on map change - inventoryWindow->items->resetItems(); + inventory->resetItems(); for (int loop = 0; loop < (RFIFOW(2) - 4) / 18; loop++) { - inventoryWindow->items->addItem(RFIFOW(4 + loop * 18), + inventory->addItem(RFIFOW(4 + loop * 18), RFIFOW(4 + loop * 18 + 2), RFIFOW(4 + loop * 18 + 6), false); // Trick because arrows are not considered equipment if (RFIFOW(4 + loop * 18 + 2) == 1199 || RFIFOW(4 + loop * 18 + 2) == 529) { - inventoryWindow->items->getItem( + inventory->getItem( RFIFOW(4 + loop * 18))->setEquipment(true); } } @@ -1152,7 +1153,7 @@ void do_parse() for (int loop = 0; loop < ((RFIFOW(2) - 4) / 20); loop++) { - inventoryWindow->items->addItem(RFIFOW(4 + loop * 20), + inventory->addItem(RFIFOW(4 + loop * 20), RFIFOW(4 + loop * 20 + 2), 1, true); if (RFIFOW(4 + loop * 20 + 8)) { @@ -1163,7 +1164,7 @@ void do_parse() mask *= 2; position++; } - Item *item = inventoryWindow->items->getItem(RFIFOW(4+loop*20)); + Item *item = inventory->getItem(RFIFOW(4+loop*20)); item->setEquipped(true); equipment->setEquipment(position - 1, item); } @@ -1177,7 +1178,7 @@ void do_parse() } else { - inventoryWindow->items->getItem(RFIFOW(2))->setQuantity(RFIFOW(4)); + inventory->getItem(RFIFOW(2))->setQuantity(RFIFOW(4)); } break; // Warp @@ -1468,17 +1469,17 @@ void do_parse() if (RFIFOB(22) > 0) chatWindow->chat_log("Unable to pick up item", BY_SERVER); else { - inventoryWindow->items->addItem(RFIFOW(2), RFIFOW(6), + inventory->addItem(RFIFOW(2), RFIFOW(6), RFIFOW(4), RFIFOW(19) != 0); } break; // Decrease quantity of an item in inventory case 0x00af: - inventoryWindow->items->getItem(RFIFOW(2))->increaseQuantity(-RFIFOW(4)); + inventory->getItem(RFIFOW(2))->increaseQuantity(-RFIFOW(4)); break; // Use an item case 0x01c8: - inventoryWindow->items->getItem(RFIFOW(2))->setQuantity( RFIFOW(10)); + inventory->getItem(RFIFOW(2))->setQuantity( RFIFOW(10)); break; // Skill list TAG case 0x010f: @@ -1579,7 +1580,7 @@ void do_parse() if (item) item->setEquipped(false); - item = inventoryWindow->items->getItem(RFIFOW(2)); + item = inventory->getItem(RFIFOW(2)); item->setEquipped(true); equipment->setEquipment(position - 1, item); @@ -1632,7 +1633,7 @@ void do_parse() position++; } - Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + Item *item = inventory->getItem(RFIFOW(2)); item->setEquipped(false); switch (item->getId()) { case 529: @@ -1658,7 +1659,7 @@ void do_parse() // Arrows equipped case 0x013c: if (RFIFOW(2) > 1) { - Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + Item *item = inventory->getItem(RFIFOW(2)); item->setEquipped(true); equipment->setArrows(item); logger->log("Arrows equipped: %i", RFIFOW(2)); diff --git a/src/gui/inventory.cpp b/src/gui/inventorywindow.cpp index 98f17134..06eb8ce0 100644 --- a/src/gui/inventory.cpp +++ b/src/gui/inventorywindow.cpp @@ -21,12 +21,11 @@ * $Id$ */ +#include "inventorywindow.h" #include "../playerinfo.h" -#include "inventory.h" -#include "../equipment.h" +#include "../inventory.h" #include "button.h" #include "scrollarea.h" -#include "../net/network.h" #include "item_amount.h" #include <string> @@ -37,7 +36,7 @@ InventoryWindow::InventoryWindow(): useButton = new Button("Use"); dropButton = new Button("Drop"); - items = new ItemContainer(); + items = new ItemContainer(inventory); invenScroll = new ScrollArea(items); invenScroll->setPosition(8, 8); invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -96,48 +95,6 @@ void InventoryWindow::logic() weightLabel->adjustSize(); } -int InventoryWindow::useItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00a7); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOL(4) = net_l_value(item->getId()); - // Note: id is dest of item, usually player_node->account_ID ?? - WFIFOSET(8); - while ((out_size > 0)) flush(); - return 0; -} - -int InventoryWindow::dropItem(Item *item, int quantity) -{ - // TODO: Fix wrong coordinates of drops, serverside? - WFIFOW(0) = net_w_value(0x00a2); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOW(4) = net_w_value(quantity); - WFIFOSET(6); - while ((out_size > 0)) flush(); - return 0; -} - -void InventoryWindow::equipItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00a9); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOW(4) = net_w_value(0); - WFIFOSET(6); - while ((out_size > 0)) flush(); -} - -void InventoryWindow::unequipItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00ab); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOSET(4); - while ((out_size > 0)) flush(); - - // Tidy equipment directly to avoid weapon still shown bug, by instance - Equipment::getInstance()->removeEquipment(item); -} - void InventoryWindow::action(const std::string &eventId) { Item *item = items->getItem(); @@ -149,14 +106,14 @@ void InventoryWindow::action(const std::string &eventId) if (eventId == "use") { if (item->isEquipment()) { if (item->isEquipped()) { - unequipItem(item); + inventory->unequipItem(item); } else { - equipItem(item); + inventory->equipItem(item); } } else { - useItem(item); + inventory->useItem(item); } } else if (eventId == "drop") @@ -237,3 +194,8 @@ void InventoryWindow::updateButtons() useButton->setEnabled(!!item); dropButton->setEnabled(!!item); } + +Item* InventoryWindow::getItem() +{ + return items->getItem(); +} diff --git a/src/gui/inventory.h b/src/gui/inventorywindow.h index e9924ead..cb7f08f5 100644 --- a/src/gui/inventory.h +++ b/src/gui/inventorywindow.h @@ -21,8 +21,8 @@ * $Id$ */ -#ifndef _TMW_INVENTORY_H -#define _TMW_INVENTORY_H +#ifndef _TMW_INVENTORYWINDOW_H +#define _TMW_INVENTORYWINDOW_H #include "itemcontainer.h" #include "window.h" @@ -51,33 +51,22 @@ class InventoryWindow : public Window, gcn::ActionListener void logic(); /** - * Equips an item. - */ - void equipItem(Item *item); - - /** - * Unequips an item. - */ - void unequipItem(Item *item); - - /** * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - int dropItem(Item *item, int quantity); - void mouseClick(int x, int y, int button, int count); void mouseMotion(int mx, int my); - ItemContainer *items; + Item* getItem(); private: - int useItem(Item *item); void updateWidgets(); /** Updates widgets size/position */ void updateButtons(); /** Updates button states */ + ItemContainer *items; + gcn::Button *useButton, *dropButton; gcn::ScrollArea *invenScroll; gcn::Label *itemNameLabel; diff --git a/src/gui/item_amount.cpp b/src/gui/item_amount.cpp index 021da3eb..eb3a4c5c 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -22,7 +22,7 @@ */ #include "item_amount.h" -#include "inventory.h" +#include "inventorywindow.h" #include "trade.h" #include "button.h" @@ -38,7 +38,7 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent): itemAmountOkButton = new Button("Okay"); itemAmountCancelButton = new Button("Cancel"); - itemAmountTextBox->setRange(1, inventoryWindow->items->getItem()->getQuantity()); + itemAmountTextBox->setRange(1, inventoryWindow->getItem()->getQuantity()); // Set button events Id itemAmountMinusButton->setEventId("Minus"); @@ -108,12 +108,12 @@ void ItemAmountWindow::action(const std::string& eventId) } else if (eventId == "Drop") { - inventoryWindow->dropItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); + inventory->dropItem(inventoryWindow->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "AddTrade") { - tradeWindow->tradeItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); + tradeWindow->tradeItem(inventoryWindow->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "Plus") diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 51780fca..bda9f786 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -28,7 +28,8 @@ #include "../resources/resourcemanager.h" #include <sstream> -ItemContainer::ItemContainer() +ItemContainer::ItemContainer(Inventory *inventory): + inventory(inventory) { ResourceManager *resman = ResourceManager::getInstance(); Image *itemImg = resman->getImage("graphics/sprites/items.png"); @@ -41,10 +42,6 @@ ItemContainer::ItemContainer() selectedItem = 0; // No item selected - for (int i = 0; i < INVENTORY_SIZE; i++) { - items[i].setInvIndex(i); - } - addMouseListener(this); } @@ -82,41 +79,44 @@ void ItemContainer::draw(gcn::Graphics* graphics) */ for (int i = 2; i < INVENTORY_SIZE; i++) { - if (items[i].getQuantity() > 0) + Item *item = inventory->getItem(i); + + if (item->getQuantity() <= 0) { + continue; + } + + int itemX = ((i - 2) % columns) * gridWidth; + int itemY = ((i - 2) / columns) * gridHeight; + + // Draw selection image below selected item + if (selectedItem == item) { - int itemX = ((i - 2) % columns) * gridWidth; - int itemY = ((i - 2) / columns) * gridHeight; - - // Draw selection image below selected item - if (selectedItem == &items[i]) - { - dynamic_cast<Graphics*>(graphics)->drawImage( - selImg, x + itemX, y + itemY); - } - - // Draw item icon - int idx; - if ((idx = items[i].getInfo()->getImage()) > 0) - { - dynamic_cast<Graphics*>(graphics)->drawImage( - itemset->spriteset[idx - 1], x + itemX, y + itemY); - } - - // Draw item caption - std::stringstream ss; - - if (!items[i].isEquipped()) { - ss << items[i].getQuantity(); - } - else { - ss << "Eq."; - } - - graphics->drawText(ss.str(), - itemX + gridWidth / 2, - itemY + gridHeight - 11, - gcn::Graphics::CENTER); + dynamic_cast<Graphics*>(graphics)->drawImage( + selImg, x + itemX, y + itemY); + } + + // Draw item icon + int idx; + if ((idx = item->getInfo()->getImage()) > 0) + { + dynamic_cast<Graphics*>(graphics)->drawImage( + itemset->spriteset[idx - 1], x + itemX, y + itemY); + } + + // Draw item caption + std::stringstream ss; + + if (!item->isEquipped()) { + ss << item->getQuantity(); } + else { + ss << "Eq."; + } + + graphics->drawText(ss.str(), + itemX + gridWidth / 2, + itemY + gridHeight - 11, + gcn::Graphics::CENTER); } } @@ -137,67 +137,16 @@ void ItemContainer::setWidth(int width) (INVENTORY_SIZE % columns > 0 ? 1 : 0)) * gridHeight); } -int ItemContainer::getIndex(int id) -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == id) { - return i; - } - } - return -1; -} - Item* ItemContainer::getItem() { return selectedItem; } -Item* ItemContainer::getItem(int index) -{ - return &items[index]; -} - -void ItemContainer::addItem(int index, int id, int quantity, bool equipment) -{ - items[index].setId(id); - items[index].increaseQuantity(quantity); - items[index].setEquipment(equipment); -} - -int ItemContainer::getFreeSlot() -{ - for (int i = 2; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == -1) { - return i; - } - } - return -1; -} - -void ItemContainer::resetItems() -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - items[i].setId(-1); - items[i].setQuantity(0); - items[i].setEquipped(false); - } -} - void ItemContainer::selectNone() { selectedItem = 0; } -void ItemContainer::removeItem(int id) -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == id) { - items[i].setId(-1); - items[i].setQuantity(0); - } - } -} - void ItemContainer::mousePress(int mx, int my, int button) { int gridWidth = itemset->spriteset[0]->getWidth() + 4; @@ -212,20 +161,6 @@ void ItemContainer::mousePress(int mx, int my, int button) if (index > INVENTORY_SIZE) { index = INVENTORY_SIZE - 1; } - selectedItem = &items[index]; + selectedItem = inventory->getItem(index); } } - -int ItemContainer::getNumberOfSlotsUsed() -{ - int NumberOfFilledSlot = 0; - for (int i = 0; i < INVENTORY_SIZE; i++) - { - if (items[i].getId() > -1 || items[i].getQuantity() > 0) - { - NumberOfFilledSlot++; - } - } - - return NumberOfFilledSlot; -} diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h index 6e72eba0..6def991a 100644 --- a/src/gui/itemcontainer.h +++ b/src/gui/itemcontainer.h @@ -28,13 +28,10 @@ #include <guichan.hpp> #include "../item.h" - -#include "../item.h" +#include "../inventory.h" #include "../resources/image.h" #include "../graphic/spriteset.h" -#define INVENTORY_SIZE 100 - /** * An item container. Used to show items in inventory and trade dialog. * @@ -46,7 +43,7 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener /** * Constructor. Initializes the graphic. */ - ItemContainer(); + ItemContainer(Inventory *inventory); /** * Destructor. @@ -70,57 +67,20 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener void mousePress(int mx, int my, int button); /** - * Finds the index of an item. - */ - int getIndex(int id); - - /** * Returns the selected item. */ Item* getItem(); /** - * Returns the item at the specified index. - */ - Item* getItem(int index); - - /** - * Returns id of next free slot or -1 if all occupied. - */ - int getFreeSlot(); - - /** - * Adds a new item. - */ - void addItem(int index, int id, int quantity, bool equipment); - - /** * Set selected item to -1. */ void selectNone(); - /** - * Reset all item slots. - */ - void resetItems(); - - /** - * Remove a item from the inventory. - */ - void removeItem(int id); - - /** - * Get the number of slots filled with an item - */ - int getNumberOfSlotsUsed(); - private: + Inventory *inventory; Spriteset *itemset; Image *selImg; Item *selectedItem; - int itemNumber; - Item items[INVENTORY_SIZE]; /**< The holder of items */ - }; #endif diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index 5ce56438..26487b9a 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -22,12 +22,12 @@ */ #include "sell.h" -#include "inventory.h" #include "button.h" #include "slider.h" #include "scrollarea.h" #include "listbox.h" #include "../game.h" +#include "../inventory.h" #include "../net/network.h" #include <sstream> @@ -90,7 +90,7 @@ void SellDialog::reset() void SellDialog::addItem(short index, int price) { - Item *item = inventoryWindow->items->getItem(index); + Item *item = inventory->getItem(index); if (!item) return; diff --git a/src/gui/status.cpp b/src/gui/status.cpp index 4cb4f010..e507c395 100644 --- a/src/gui/status.cpp +++ b/src/gui/status.cpp @@ -23,7 +23,7 @@ #include "status.h" #include "stats.h" -#include "inventory.h" +#include "inventorywindow.h" #include "setup.h" #include "equipmentwindow.h" #include "button.h" diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index ddf9bc0f..87328f0c 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -22,14 +22,14 @@ */ #include "trade.h" -#include "equipment.h" -#include "inventory.h" #include "chat.h" +#include "inventorywindow.h" #include "item_amount.h" #include "button.h" #include "scrollarea.h" #include "textfield.h" #include "../net/network.h" +#include "../equipment.h" #include <sstream> TradeWindow::TradeWindow(): @@ -42,16 +42,16 @@ TradeWindow::TradeWindow(): cancelButton = new Button("Cancel"); tradeButton = new Button("Trade"); - myItems = new ItemContainer(); - myItems->setPosition(2, 2); + myItemContainer = new ItemContainer(&myInventory); + myItemContainer->setPosition(2, 2); - myScroll = new ScrollArea(myItems); + myScroll = new ScrollArea(myItemContainer); myScroll->setPosition(8, 8); - partnerItems = new ItemContainer(); - partnerItems->setPosition(2, 58); + partnerItemContainer = new ItemContainer(&partnerInventory); + partnerItemContainer->setPosition(2, 58); - partnerScroll = new ScrollArea(partnerItems); + partnerScroll = new ScrollArea(partnerItemContainer); partnerScroll->setPosition(8, 64); moneyLabel = new gcn::Label("You get: 0z"); @@ -101,11 +101,11 @@ TradeWindow::TradeWindow(): addButton->setPosition(okButton->getX() - 32, getHeight() - 24); - myItems->setSize(getWidth() - 24 - 12 - 1, + myItemContainer->setSize(getWidth() - 24 - 12 - 1, (INVENTORY_SIZE * 24) / (getWidth() / 24) - 1); myScroll->setSize(getWidth() - 16, (getHeight() - 76) / 2); - partnerItems->setSize(getWidth() - 24 - 12 - 1, + partnerItemContainer->setSize(getWidth() - 24 - 12 - 1, (INVENTORY_SIZE * 24) / (getWidth() / 24) - 1); partnerScroll->setSize(getWidth() - 16, (getHeight() - 76) / 2); @@ -123,9 +123,9 @@ TradeWindow::~TradeWindow() delete okButton; delete cancelButton; delete tradeButton; - delete myItems; + delete myItemContainer; delete myScroll; - delete partnerItems; + delete partnerItemContainer; delete partnerScroll; delete itemNameLabel; delete itemDescriptionLabel; @@ -141,47 +141,47 @@ void TradeWindow::addMoney(int amount) moneyLabel->adjustSize(); } -void TradeWindow::addItem(int index, int id, bool own, int quantity, +void TradeWindow::addItem(int id, bool own, int quantity, bool equipment) { if (own) { - myItems->addItem(index, id, quantity, equipment); + myInventory.addItem(id, quantity, equipment); } else { - partnerItems->addItem(index, id, quantity, equipment); + partnerInventory.addItem(id, quantity, equipment); } } void TradeWindow::removeItem(int id, bool own) { if (own) { - myItems->removeItem(id); + myInventory.removeItem(id); } else { - partnerItems->removeItem(id); + partnerInventory.removeItem(id); } } void TradeWindow::changeQuantity(int index, bool own, int quantity) { if (own) { - myItems->getItem(index)->setQuantity(quantity); + myInventory.getItem(index)->setQuantity(quantity); } else { - partnerItems->getItem(index)->setQuantity(quantity); + partnerInventory.getItem(index)->setQuantity(quantity); } } void TradeWindow::increaseQuantity(int index, bool own, int quantity) { if (own) { - myItems->getItem(index)->increaseQuantity(quantity); + myInventory.getItem(index)->increaseQuantity(quantity); } else { - partnerItems->getItem(index)->increaseQuantity(quantity); + partnerInventory.getItem(index)->increaseQuantity(quantity); } } void TradeWindow::reset() { - myItems->resetItems(); - partnerItems->resetItems(); + myInventory.resetItems(); + partnerInventory.resetItems(); tradeButton->setEnabled(false); okButton->setEnabled(true); ok_other = false; @@ -239,18 +239,18 @@ void TradeWindow::mouseClick(int x, int y, int button, int count) && x <= myScroll->getX() + myScroll->getWidth() - 10 && y >= myScroll->getY() + 16 && y <= myScroll->getY() + myScroll->getHeight() + 15 - && (item = myItems->getItem())) + && (item = myItemContainer->getItem())) { - partnerItems->selectNone(); + partnerItemContainer->selectNone(); // partnerItems selected } else if (x >= partnerScroll->getX() + 3 && x <= partnerScroll->getX() + partnerScroll->getWidth() - 20 && y >= partnerScroll->getY() + 16 && y <= partnerScroll->getY() + partnerScroll->getHeight() + 15 - && (item = partnerItems->getItem())) + && (item = partnerItemContainer->getItem())) { - myItems->selectNone(); + myItemContainer->selectNone(); } else { return; } @@ -267,18 +267,18 @@ void TradeWindow::mouseClick(int x, int y, int button, int count) void TradeWindow::action(const std::string &eventId) { - Item *item = inventoryWindow->items->getItem(); + Item *item = inventoryWindow->getItem(); if (eventId == "add") { if (!item) { return; } - if (tradeWindow->myItems->getFreeSlot() < 1) { + if (myInventory.getFreeSlot() < 1) { return; } - if (myItems->getIndex(item->getId()) != -1) { + if (myInventory.contains(item)) { chatWindow->chat_log("Failed adding item. You can not " "overlap one kind of item on the window.", BY_SERVER); return; diff --git a/src/gui/trade.h b/src/gui/trade.h index 6132a182..b767d47b 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -54,7 +54,7 @@ class TradeWindow : public Window, gcn::ActionListener /** * Add an item to the trade window. */ - void addItem(int index, int id, bool own, int quantity, bool equipment); + void addItem(int id, bool own, int quantity, bool equipment); /** * Remove a item from the trade window. @@ -101,8 +101,10 @@ class TradeWindow : public Window, gcn::ActionListener */ void action(const std::string& eventId); - ItemContainer *myItems; - ItemContainer *partnerItems; + Inventory myInventory; + Inventory partnerInventory; + ItemContainer *myItemContainer; + ItemContainer *partnerItemContainer; private: gcn::Label *itemNameLabel; diff --git a/src/inventory.cpp b/src/inventory.cpp new file mode 100644 index 00000000..c0ddd888 --- /dev/null +++ b/src/inventory.cpp @@ -0,0 +1,134 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "inventory.h" + +#include "equipment.h" +#include "net/network.h" + +Inventory::Inventory() +{ + for (int i = 0; i < INVENTORY_SIZE; i++) { + items[i].setInvIndex(i); + } +} + +Inventory::~Inventory() +{ +} + +void Inventory::resetItems() +{ + for (int i = 0; i < INVENTORY_SIZE; i++) { + items[i].setId(-1); + items[i].setQuantity(0); + items[i].setEquipped(false); + } +} + +void Inventory::removeItem(int id) +{ + for (int i = 0; i < INVENTORY_SIZE; i++) { + if (items[i].getId() == id) { + items[i].setId(-1); + items[i].setQuantity(0); + } + } +} + +bool Inventory::contains(Item *item) +{ + for (int i = 0; i < INVENTORY_SIZE; i++) { + if (items[i].getId() == item->getId()) { + return true; + } + } + + return false; +} + +int Inventory::useItem(Item *item) +{ + WFIFOW(0) = net_w_value(0x00a7); + WFIFOW(2) = net_w_value(item->getInvIndex()); + WFIFOL(4) = net_l_value(item->getId()); + // Note: id is dest of item, usually player_node->account_ID ?? + WFIFOSET(8); + while ((out_size > 0)) flush(); + return 0; +} + +int Inventory::dropItem(Item *item, int quantity) +{ + // TODO: Fix wrong coordinates of drops, serverside? + WFIFOW(0) = net_w_value(0x00a2); + WFIFOW(2) = net_w_value(item->getInvIndex()); + WFIFOW(4) = net_w_value(quantity); + WFIFOSET(6); + while ((out_size > 0)) flush(); + return 0; +} + +void Inventory::equipItem(Item *item) +{ + WFIFOW(0) = net_w_value(0x00a9); + WFIFOW(2) = net_w_value(item->getInvIndex()); + WFIFOW(4) = net_w_value(0); + WFIFOSET(6); + while ((out_size > 0)) flush(); +} + +void Inventory::unequipItem(Item *item) +{ + WFIFOW(0) = net_w_value(0x00ab); + WFIFOW(2) = net_w_value(item->getInvIndex()); + WFIFOSET(4); + while ((out_size > 0)) flush(); + + // Tidy equipment directly to avoid weapon still shown bug, by instance + Equipment::getInstance()->removeEquipment(item); +} + +int Inventory::getFreeSlot() +{ + for (int i = 2; i < INVENTORY_SIZE; i++) { + if (items[i].getId() == -1) { + return i; + } + } + return -1; +} + +int Inventory::getNumberOfSlotsUsed() +{ + int NumberOfFilledSlot = 0; + for (int i = 0; i < INVENTORY_SIZE; i++) + { + if (items[i].getId() > -1 || items[i].getQuantity() > 0) + { + NumberOfFilledSlot++; + } + } + + return NumberOfFilledSlot; +} diff --git a/src/inventory.h b/src/inventory.h new file mode 100644 index 00000000..f5c47c4f --- /dev/null +++ b/src/inventory.h @@ -0,0 +1,114 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _INVENTORY_H +#define _INVENTORY_H + +#include "item.h" + +#define INVENTORY_SIZE 100 + +class Inventory +{ + public: + Inventory(); + ~Inventory(); + + /** + * Returns the item at the specified index. + */ + Item* getItem(int index); + + /** + * Adds a new item in a free slot. + */ + void addItem(int id, int quantity, bool equipment); + + /** + * Adds a new item at a given position. + */ + void addItem(int index, int id, int quantity, bool equipment); + + /** + * Remove a item from the inventory. + */ + void removeItem(int id); + + /** + * Checks if the given item is in the inventory + */ + bool contains(Item *item); + + /** + * Equips an item. + */ + void equipItem(Item *item); + + /** + * Unequips an item. + */ + void unequipItem(Item *item); + + int useItem(Item *item); + + int dropItem(Item *item, int quantity); + + /** + * Returns id of next free slot or -1 if all occupied. + */ + int getFreeSlot(); + + /** + * Reset all item slots. + */ + void resetItems(); + + /** + * Get the number of slots filled with an item + */ + int getNumberOfSlotsUsed(); + + protected: + Item items[INVENTORY_SIZE]; /**< The holder of items */ +}; + +inline Item* Inventory::getItem(int index) +{ + return &items[index]; +} + +inline void Inventory::addItem(int id, int quantity, bool equipment) +{ + addItem(getFreeSlot(), id, quantity, equipment); +} + +inline void Inventory::addItem(int index, int id, int quantity, bool equipment) +{ + items[index].setId(id); + items[index].increaseQuantity(quantity); + items[index].setEquipment(equipment); +} + +extern Inventory *inventory; + +#endif |