From 290aab364a310b18bb02567f6e4e424d94148bf9 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Mon, 18 Jul 2005 00:27:11 +0000 Subject: Created Inventory class. (Really) Small code simplifications and cleanups. --- src/Makefile.am | 6 +- src/engine.cpp | 2 +- src/game.cpp | 39 ++++---- src/gui/inventory.cpp | 239 -------------------------------------------- src/gui/inventory.h | 90 ----------------- src/gui/inventorywindow.cpp | 201 +++++++++++++++++++++++++++++++++++++ src/gui/inventorywindow.h | 79 +++++++++++++++ src/gui/item_amount.cpp | 8 +- src/gui/itemcontainer.cpp | 143 ++++++++------------------ src/gui/itemcontainer.h | 46 +-------- src/gui/sell.cpp | 4 +- src/gui/status.cpp | 2 +- src/gui/trade.cpp | 60 +++++------ src/gui/trade.h | 8 +- src/inventory.cpp | 134 +++++++++++++++++++++++++ src/inventory.h | 114 +++++++++++++++++++++ 16 files changed, 637 insertions(+), 538 deletions(-) delete mode 100644 src/gui/inventory.cpp delete mode 100644 src/gui/inventory.h create mode 100644 src/gui/inventorywindow.cpp create mode 100644 src/gui/inventorywindow.h create mode 100644 src/inventory.cpp create mode 100644 src/inventory.h 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/inventory.cpp deleted file mode 100644 index 98f17134..00000000 --- a/src/gui/inventory.cpp +++ /dev/null @@ -1,239 +0,0 @@ -/* - * 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 "../playerinfo.h" -#include "inventory.h" -#include "../equipment.h" -#include "button.h" -#include "scrollarea.h" -#include "../net/network.h" -#include "item_amount.h" -#include - -InventoryWindow::InventoryWindow(): - Window("Inventory") -{ - setContentSize(322, 160); - useButton = new Button("Use"); - dropButton = new Button("Drop"); - - items = new ItemContainer(); - invenScroll = new ScrollArea(items); - invenScroll->setPosition(8, 8); - invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - - useButton->setEventId("use"); - dropButton->setEventId("drop"); - useButton->addActionListener(this); - dropButton->addActionListener(this); - - itemNameLabel = new gcn::Label("Name:"); - itemDescriptionLabel = new gcn::Label("Description:"); - weightLabel = new gcn::Label("Total Weight: - Maximum Weight: "); - weightLabel->setPosition(8, 8); - invenScroll->setPosition(8, - weightLabel->getY() + weightLabel->getHeight() + 5); - - add(useButton); - add(dropButton); - add(invenScroll); - add(itemNameLabel); - add(itemDescriptionLabel); - add(weightLabel); - - setResizable(true); - setMinWidth(240); - setMinHeight(150); - - updateWidgets(); - useButton->setSize(48, useButton->getHeight()); -} - -InventoryWindow::~InventoryWindow() -{ - delete useButton; - delete dropButton; - delete invenScroll; - delete items; - delete itemNameLabel; - delete itemDescriptionLabel; - delete weightLabel; -} - -void InventoryWindow::logic() -{ - Window::logic(); - - // It would be nicer if this update could be event based, needs some - // redesign of InventoryWindow and ItemContainer probably. - updateButtons(); - - // Update weight information - char tempstr[128]; - sprintf(tempstr, "Total Weight: %2i - Maximum Weight: %2i", - char_info->totalWeight, char_info->maxWeight); - weightLabel->setCaption(tempstr); - 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(); - - if (!item) { - return; - } - - if (eventId == "use") { - if (item->isEquipment()) { - if (item->isEquipped()) { - unequipItem(item); - } - else { - equipItem(item); - } - } - else { - useItem(item); - } - } - else if (eventId == "drop") - { - // Choose amount of items to drop - new ItemAmountWindow(AMOUNT_ITEM_DROP, this); - } -} - -void InventoryWindow::mouseClick(int x, int y, int button, int count) -{ - Window::mouseClick(x, y, button, count); - - Item *item = items->getItem(); - - if (!item) { - return; - } - - // Show Name and Description - std::string SomeText; - SomeText = "Name: " + item->getInfo()->getName(); - itemNameLabel->setCaption(SomeText); - itemNameLabel->adjustSize(); - SomeText = "Description: " + item->getInfo()->getDescription(); - itemDescriptionLabel->setCaption(SomeText); - itemDescriptionLabel->adjustSize(); -} - -void InventoryWindow::mouseMotion(int mx, int my) -{ - int tmpWidth = getWidth(), tmpHeight = getHeight(); - Window::mouseMotion(mx, my); - if (getWidth() != tmpWidth || getHeight() != tmpHeight) { - updateWidgets(); - } -} - -void InventoryWindow::updateWidgets() -{ - int width = getContent()->getWidth(); - int height = getContent()->getHeight(); - int columns = width / 24; - - if (columns < 1) - { - columns = 1; - } - - // Resize widgets - useButton->setPosition(8, height - 24); - dropButton->setPosition(48 + 16, height - 24); - invenScroll->setSize(width - 16, height - 90); - - itemNameLabel->setPosition(8, - invenScroll->getY() + invenScroll->getHeight() + 4); - itemDescriptionLabel->setPosition(8, - itemNameLabel->getY() + itemNameLabel->getHeight() + 4); -} - -void InventoryWindow::updateButtons() -{ - Item *item; - - if ((item = items->getItem()) && item->isEquipment()) - { - if (item->isEquipped()) { - useButton->setCaption("Unequip"); - } - else { - useButton->setCaption("Equip"); - } - } - else { - useButton ->setCaption("Use"); - } - - useButton->setEnabled(!!item); - dropButton->setEnabled(!!item); -} diff --git a/src/gui/inventory.h b/src/gui/inventory.h deleted file mode 100644 index e9924ead..00000000 --- a/src/gui/inventory.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 _TMW_INVENTORY_H -#define _TMW_INVENTORY_H - -#include "itemcontainer.h" -#include "window.h" - -/** - * Inventory dialog. - * - * \ingroup Interface - */ -class InventoryWindow : public Window, gcn::ActionListener -{ - public: - /** - * Constructor. - */ - InventoryWindow(); - - /** - * Destructor. - */ - ~InventoryWindow(); - - /** - * Logic (updates buttons and weight information) - */ - 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; - - private: - int useItem(Item *item); - void updateWidgets(); /** Updates widgets size/position */ - void updateButtons(); /** Updates button states */ - - gcn::Button *useButton, *dropButton; - gcn::ScrollArea *invenScroll; - gcn::Label *itemNameLabel; - gcn::Label *itemDescriptionLabel; - gcn::Label *weightLabel; -}; - -extern InventoryWindow *inventoryWindow; - -#endif diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp new file mode 100644 index 00000000..06eb8ce0 --- /dev/null +++ b/src/gui/inventorywindow.cpp @@ -0,0 +1,201 @@ +/* + * 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 "inventorywindow.h" +#include "../playerinfo.h" +#include "../inventory.h" +#include "button.h" +#include "scrollarea.h" +#include "item_amount.h" +#include + +InventoryWindow::InventoryWindow(): + Window("Inventory") +{ + setContentSize(322, 160); + useButton = new Button("Use"); + dropButton = new Button("Drop"); + + items = new ItemContainer(inventory); + invenScroll = new ScrollArea(items); + invenScroll->setPosition(8, 8); + invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + useButton->setEventId("use"); + dropButton->setEventId("drop"); + useButton->addActionListener(this); + dropButton->addActionListener(this); + + itemNameLabel = new gcn::Label("Name:"); + itemDescriptionLabel = new gcn::Label("Description:"); + weightLabel = new gcn::Label("Total Weight: - Maximum Weight: "); + weightLabel->setPosition(8, 8); + invenScroll->setPosition(8, + weightLabel->getY() + weightLabel->getHeight() + 5); + + add(useButton); + add(dropButton); + add(invenScroll); + add(itemNameLabel); + add(itemDescriptionLabel); + add(weightLabel); + + setResizable(true); + setMinWidth(240); + setMinHeight(150); + + updateWidgets(); + useButton->setSize(48, useButton->getHeight()); +} + +InventoryWindow::~InventoryWindow() +{ + delete useButton; + delete dropButton; + delete invenScroll; + delete items; + delete itemNameLabel; + delete itemDescriptionLabel; + delete weightLabel; +} + +void InventoryWindow::logic() +{ + Window::logic(); + + // It would be nicer if this update could be event based, needs some + // redesign of InventoryWindow and ItemContainer probably. + updateButtons(); + + // Update weight information + char tempstr[128]; + sprintf(tempstr, "Total Weight: %2i - Maximum Weight: %2i", + char_info->totalWeight, char_info->maxWeight); + weightLabel->setCaption(tempstr); + weightLabel->adjustSize(); +} + +void InventoryWindow::action(const std::string &eventId) +{ + Item *item = items->getItem(); + + if (!item) { + return; + } + + if (eventId == "use") { + if (item->isEquipment()) { + if (item->isEquipped()) { + inventory->unequipItem(item); + } + else { + inventory->equipItem(item); + } + } + else { + inventory->useItem(item); + } + } + else if (eventId == "drop") + { + // Choose amount of items to drop + new ItemAmountWindow(AMOUNT_ITEM_DROP, this); + } +} + +void InventoryWindow::mouseClick(int x, int y, int button, int count) +{ + Window::mouseClick(x, y, button, count); + + Item *item = items->getItem(); + + if (!item) { + return; + } + + // Show Name and Description + std::string SomeText; + SomeText = "Name: " + item->getInfo()->getName(); + itemNameLabel->setCaption(SomeText); + itemNameLabel->adjustSize(); + SomeText = "Description: " + item->getInfo()->getDescription(); + itemDescriptionLabel->setCaption(SomeText); + itemDescriptionLabel->adjustSize(); +} + +void InventoryWindow::mouseMotion(int mx, int my) +{ + int tmpWidth = getWidth(), tmpHeight = getHeight(); + Window::mouseMotion(mx, my); + if (getWidth() != tmpWidth || getHeight() != tmpHeight) { + updateWidgets(); + } +} + +void InventoryWindow::updateWidgets() +{ + int width = getContent()->getWidth(); + int height = getContent()->getHeight(); + int columns = width / 24; + + if (columns < 1) + { + columns = 1; + } + + // Resize widgets + useButton->setPosition(8, height - 24); + dropButton->setPosition(48 + 16, height - 24); + invenScroll->setSize(width - 16, height - 90); + + itemNameLabel->setPosition(8, + invenScroll->getY() + invenScroll->getHeight() + 4); + itemDescriptionLabel->setPosition(8, + itemNameLabel->getY() + itemNameLabel->getHeight() + 4); +} + +void InventoryWindow::updateButtons() +{ + Item *item; + + if ((item = items->getItem()) && item->isEquipment()) + { + if (item->isEquipped()) { + useButton->setCaption("Unequip"); + } + else { + useButton->setCaption("Equip"); + } + } + else { + useButton ->setCaption("Use"); + } + + useButton->setEnabled(!!item); + dropButton->setEnabled(!!item); +} + +Item* InventoryWindow::getItem() +{ + return items->getItem(); +} diff --git a/src/gui/inventorywindow.h b/src/gui/inventorywindow.h new file mode 100644 index 00000000..cb7f08f5 --- /dev/null +++ b/src/gui/inventorywindow.h @@ -0,0 +1,79 @@ +/* + * 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 _TMW_INVENTORYWINDOW_H +#define _TMW_INVENTORYWINDOW_H + +#include "itemcontainer.h" +#include "window.h" + +/** + * Inventory dialog. + * + * \ingroup Interface + */ +class InventoryWindow : public Window, gcn::ActionListener +{ + public: + /** + * Constructor. + */ + InventoryWindow(); + + /** + * Destructor. + */ + ~InventoryWindow(); + + /** + * Logic (updates buttons and weight information) + */ + void logic(); + + /** + * Called when receiving actions from the widgets. + */ + void action(const std::string& eventId); + + void mouseClick(int x, int y, int button, int count); + + void mouseMotion(int mx, int my); + + Item* getItem(); + + private: + void updateWidgets(); /** Updates widgets size/position */ + void updateButtons(); /** Updates button states */ + + ItemContainer *items; + + gcn::Button *useButton, *dropButton; + gcn::ScrollArea *invenScroll; + gcn::Label *itemNameLabel; + gcn::Label *itemDescriptionLabel; + gcn::Label *weightLabel; +}; + +extern InventoryWindow *inventoryWindow; + +#endif 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 -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)->drawImage( - selImg, x + itemX, y + itemY); - } - - // Draw item icon - int idx; - if ((idx = items[i].getInfo()->getImage()) > 0) - { - dynamic_cast(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)->drawImage( + selImg, x + itemX, y + itemY); + } + + // Draw item icon + int idx; + if ((idx = item->getInfo()->getImage()) > 0) + { + dynamic_cast(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 #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. @@ -69,58 +66,21 @@ 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 @@ -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 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 -- cgit v1.2.3-70-g09d2