diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-17 21:10:44 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-17 21:10:44 +0000 |
commit | 9bf3338f82bfbd425debb2855d015c7bd0e33989 (patch) | |
tree | ba390f42ebb7149e91c3494bc6a4ef3ac919ae22 /src | |
parent | fbf19c2cdde592b126fe91c0dfff480f1318f50c (diff) | |
download | mana-9bf3338f82bfbd425debb2855d015c7bd0e33989.tar.gz mana-9bf3338f82bfbd425debb2855d015c7bd0e33989.tar.bz2 mana-9bf3338f82bfbd425debb2855d015c7bd0e33989.tar.xz mana-9bf3338f82bfbd425debb2855d015c7bd0e33989.zip |
Added Item and Equipment class and made the appropriate places make use of them. Plus some cleanups.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/equipment.cpp | 37 | ||||
-rw-r--r-- | src/equipment.h | 99 | ||||
-rw-r--r-- | src/game.cpp | 137 | ||||
-rw-r--r-- | src/gui/equipment.cpp | 78 | ||||
-rw-r--r-- | src/gui/equipment.h | 22 | ||||
-rw-r--r-- | src/gui/inventory.cpp | 122 | ||||
-rw-r--r-- | src/gui/inventory.h | 28 | ||||
-rw-r--r-- | src/gui/item_amount.cpp | 6 | ||||
-rw-r--r-- | src/gui/itemcontainer.cpp | 130 | ||||
-rw-r--r-- | src/gui/itemcontainer.h | 83 | ||||
-rw-r--r-- | src/gui/sell.cpp | 13 | ||||
-rw-r--r-- | src/gui/trade.cpp | 100 | ||||
-rw-r--r-- | src/gui/trade.h | 2 | ||||
-rw-r--r-- | src/gui/updatewindow.cpp | 2 | ||||
-rw-r--r-- | src/item.cpp | 39 | ||||
-rw-r--r-- | src/item.h | 119 |
17 files changed, 543 insertions, 480 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 6851a7e7..e5cd02f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,5 @@ +AUTOMAKE_OPTIONS = subdir-objects + bin_PROGRAMS = tmw tmw_SOURCES = graphic/spriteset.cpp \ graphic/spriteset.h \ @@ -132,12 +134,16 @@ tmw_SOURCES = graphic/spriteset.cpp \ configuration.h \ engine.cpp \ engine.h \ + equipment.cpp \ + equipment.h \ floor_item.cpp \ floor_item.h \ game.cpp \ game.h \ graphics.cpp \ graphics.h \ + item.cpp \ + item.h \ log.cpp \ log.h \ main.cpp \ diff --git a/src/equipment.cpp b/src/equipment.cpp new file mode 100644 index 00000000..6a470e03 --- /dev/null +++ b/src/equipment.cpp @@ -0,0 +1,37 @@ +/* + * 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 "equipment.h" + +Equipment *Equipment::instance = 0; + +Equipment::Equipment() +{ + for (int i = 0; i < EQUIPMENT_SIZE; i++) { + equipment[i] = 0; + } +} + +Equipment::~Equipment() +{ +} diff --git a/src/equipment.h b/src/equipment.h new file mode 100644 index 00000000..a1cdb13c --- /dev/null +++ b/src/equipment.h @@ -0,0 +1,99 @@ +/* + * 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 _EQUIPMENT_H +#define _EQUIPMENT_H + +#include "item.h" + +#define EQUIPMENT_SIZE 10 + +class Equipment +{ + public: + static Equipment* getInstance(); + + Item* getEquipment(int index); + void setEquipment(int index, Item *item); + + void removeEquipment(int index); + void removeEquipment(Item *item); + + Item* getArrows(); + void setArrows(Item *arrows); + + protected: + Equipment(); + ~Equipment(); + + Item *equipment[EQUIPMENT_SIZE]; + Item *arrows; + + private: + static Equipment *instance; +}; + +inline Equipment *Equipment::getInstance() +{ + if (!instance) + instance = new Equipment(); + + return instance; +} + +inline Item* Equipment::getEquipment(int index) +{ + return equipment[index]; +} + +inline void Equipment::setEquipment(int index, Item *item) +{ + equipment[index] = item; +} + +inline void Equipment::removeEquipment(int index) +{ + equipment[index] = 0; +} + +inline void Equipment::removeEquipment(Item *item) +{ + for (int i = 0; i < EQUIPMENT_SIZE; i++) { + if (equipment[i] == item) { + equipment[i] = 0; + break; + } + } +} + +inline Item* Equipment::getArrows() +{ + return arrows; +} + +inline void Equipment::setArrows(Item *arrows) +{ + this->arrows = arrows; +} + +#endif diff --git a/src/game.cpp b/src/game.cpp index 48a4fd8d..0b5411a1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -28,6 +28,7 @@ #include "engine.h" #include "log.h" #include "map.h" +#include "equipment.h" #include "gui/chat.h" #include "gui/gui.h" #include "gui/inventory.h" @@ -765,6 +766,7 @@ void do_parse() FloorItem *floorItem = NULL; int len, n_items; Map *tiledMap = engine->getCurrentMap(); + Equipment *equipment = Equipment::getInstance(); // We need at least 2 bytes to identify a packet if (in_size >= 2) { @@ -1067,34 +1069,31 @@ void do_parse() break; // Trade: New Item add response case 0x01b1: - switch (RFIFOB(6)) { - case 0: - // Successfully added item - if (inventoryWindow->items->isEquipment(RFIFOW(2)) - && inventoryWindow->items->isEquipped( - RFIFOW(2))) - { - inventoryWindow->unequipItem(RFIFOW(2)); - } - tradeWindow->addItem( - tradeWindow->myItems->getFreeSlot(), - inventoryWindow->items->getId(RFIFOW(2)), - true, RFIFOW(4), - inventoryWindow->items->isEquipment( - RFIFOW(2))); - inventoryWindow->changeQuantity(RFIFOW(2), - (inventoryWindow->items->getQuantity(RFIFOW(2)) - - RFIFOW(4))); - break; - case 1: - // Add item failed - player overweighted - chatWindow->chat_log("Failed adding item. Trade " - "partner is over weighted.", BY_SERVER); - break; - default: - //printf("Unhandled 0x00ea byte!\n"); - break; + Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + switch (RFIFOB(6)) + { + case 0: + // Successfully added item + if (item->isEquipment() && item->isEquipped()) + { + inventoryWindow->unequipItem(item); + } + tradeWindow->addItem( + tradeWindow->myItems->getFreeSlot(), + item->getId(), true, RFIFOW(4), + item->isEquipment()); + item->increaseQuantity(-RFIFOW(4)); + break; + case 1: + // Add item failed - player overweighted + chatWindow->chat_log("Failed adding item. Trade " + "partner is over weighted.", BY_SERVER); + break; + default: + //printf("Unhandled 0x00ea byte!\n"); + break; + } } break; // Trade received Ok message @@ -1133,15 +1132,15 @@ void do_parse() for (int loop = 0; loop < (RFIFOW(2) - 4) / 18; loop++) { - inventoryWindow->addItem(RFIFOW(4 + loop * 18), + inventoryWindow->items->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->setEquipment( - RFIFOW(4 + loop * 18), true); + inventoryWindow->items->getItem( + RFIFOW(4 + loop * 18))->setEquipment(true); } } break; @@ -1151,7 +1150,7 @@ void do_parse() for (int loop = 0; loop < ((RFIFOW(2) - 4) / 20); loop++) { - inventoryWindow->addItem(RFIFOW(4 + loop * 20), + inventoryWindow->items->addItem(RFIFOW(4 + loop * 20), RFIFOW(4 + loop * 20 + 2), 1, true); if (RFIFOW(4 + loop * 20 + 8)) { @@ -1162,12 +1161,9 @@ void do_parse() mask *= 2; position++; } - equipmentWindow->addEquipment(position - 1, - RFIFOW(4+loop*20+2)); - equipmentWindow->equipments[position - 1].inventoryIndex = - RFIFOW(4+loop*20); - inventoryWindow->items->setEquipped( - RFIFOW(4+loop*20), true); + Item *item = inventoryWindow->items->getItem(RFIFOW(4+loop*20)); + item->setEquipped(true); + equipment->setEquipment(position - 1, item); } } break; @@ -1179,7 +1175,7 @@ void do_parse() } else { - inventoryWindow->changeQuantity(RFIFOW(2), RFIFOW(4)); + inventoryWindow->items->getItem(RFIFOW(2))->setQuantity(RFIFOW(4)); } break; // Warp @@ -1470,33 +1466,17 @@ void do_parse() if (RFIFOB(22) > 0) chatWindow->chat_log("Unable to pick up item", BY_SERVER); else { - if(RFIFOW(19)) { - inventoryWindow->addItem(RFIFOW(2), RFIFOW(6), - RFIFOW(4), true); - } - else { - inventoryWindow->addItem(RFIFOW(2), RFIFOW(6), - RFIFOW(4), false); - } - if (equipmentWindow->getArrows() == RFIFOW(6)) - { - equipmentWindow->arrowsNumber += RFIFOW(4); - } + inventoryWindow->items->addItem(RFIFOW(2), RFIFOW(6), + RFIFOW(4), RFIFOW(19) != 0); } break; // Decrease quantity of an item in inventory case 0x00af: - inventoryWindow->increaseQuantity(RFIFOW(2), -RFIFOW(4)); - // If the item is arrow decrease number from equipment - // window when equipped - if (inventoryWindow->items->isEquipped(RFIFOW(2)) && ( - inventoryWindow->items->getId(RFIFOW(2)) == 529 || - inventoryWindow->items->getId(RFIFOW(2)) == 1199 ) ) - equipmentWindow->arrowsNumber -= RFIFOW(4); + inventoryWindow->items->getItem(RFIFOW(2))->increaseQuantity(-RFIFOW(4)); break; // Use an item case 0x01c8: - inventoryWindow->changeQuantity(RFIFOW(2), RFIFOW(10)); + inventoryWindow->items->getItem(RFIFOW(2))->setQuantity( RFIFOW(10)); break; // Skill list TAG case 0x010f: @@ -1593,21 +1573,17 @@ void do_parse() position++; } logger->log("Position %i", position-1); - int equippedId = equipmentWindow->equipments[position - 1].id; - if (equippedId > 0) - inventoryWindow->items->setEquipped( - equipmentWindow->equipments[position - 1].inventoryIndex, - false); - - inventoryWindow->items->setEquipped(RFIFOW(2), - true); - equipmentWindow->addEquipment(position - 1, - inventoryWindow->items->getId(RFIFOW(2))); - equipmentWindow->equipments[position - 1].inventoryIndex = RFIFOW(2); + Item *item = equipment->getEquipment(position - 1); + if (item) + item->setEquipped(false); + + item = inventoryWindow->items->getItem(RFIFOW(2)); + item->setEquipped(true); + equipment->setEquipment(position - 1, item); // Trick to use the proper graphic until I find // the right packet - switch (inventoryWindow->items->getId(RFIFOW(2))) { + switch (item->getId()) { case 521: case 522: case 536: @@ -1638,12 +1614,13 @@ void do_parse() mask *= 2; position++; } - inventoryWindow->items->setEquipped(RFIFOW(2), false); - switch (inventoryWindow->items->getId(RFIFOW(2))) { + + Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + item->setEquipped(false); + switch (item->getId()) { case 529: case 1199: - equipmentWindow->setArrows(0); - equipmentWindow->arrowsNumber = 0; + equipment->setArrows(NULL); break; case 521: case 522: @@ -1654,7 +1631,7 @@ void do_parse() player_node->weapon = 0; break; default: - equipmentWindow->removeEquipment(position - 1); + equipment->removeEquipment(position - 1); break; } logger->log("Unequipping: %i %i(%i) %i", RFIFOW(2),RFIFOW(4),RFIFOB(6), position -1); @@ -1664,12 +1641,10 @@ void do_parse() // Arrows equipped case 0x013c: if (RFIFOW(2) > 1) { - inventoryWindow->items->setEquipped(RFIFOW(2), true); - equipmentWindow->setArrows( - inventoryWindow->items->getId(RFIFOW(2))); - equipmentWindow->arrowsNumber = - inventoryWindow->items->getQuantity(RFIFOW(2)); - logger->log("Arrows equipped: %i", RFIFOW(2)); + Item *item = inventoryWindow->items->getItem(RFIFOW(2)); + item->setEquipped(true); + equipment->setArrows(item); + logger->log("Arrows equipped: %i", RFIFOW(2)); } break; // Various messages diff --git a/src/gui/equipment.cpp b/src/gui/equipment.cpp index 37623637..dbfc4f8d 100644 --- a/src/gui/equipment.cpp +++ b/src/gui/equipment.cpp @@ -22,16 +22,16 @@ */ #include <guichan.hpp> -#include "equipment.h" +#include "gui/equipment.h" +#include "../equipment.h" #include "../log.h" #include "../resources/resourcemanager.h" -#include "../resources/itemmanager.h" #include "../resources/image.h" #include <sstream> EquipmentWindow::EquipmentWindow(): - Window("Equipment"), arrows(0) + Window("Equipment") { setContentSize(200, 90); setPosition(40, 40); @@ -40,11 +40,6 @@ EquipmentWindow::EquipmentWindow(): Image *itemImg = resman->getImage("graphics/sprites/items.png"); if (!itemImg) logger->error("Unable to load items.png"); itemset = new Spriteset(itemImg, 32, 32); - - for (int i = 0; i < 10; i++ ) { - equipments[i].id = 0; - equipments[i].inventoryIndex = -1; - } } EquipmentWindow::~EquipmentWindow() @@ -59,62 +54,41 @@ void EquipmentWindow::draw(gcn::Graphics *graphics) // Draw window graphics Window::draw(graphics); + Equipment *equipment = Equipment::getInstance(); + Item *item; + Image *image; + for (int i = 0; i < 8; i++) { - if (equipments[i].id > 0) { - Image *image = itemset->spriteset[itemDb->getItemInfo( - equipments[i].id)->getImage() - 1]; - dynamic_cast<Graphics*>(graphics)->drawImage( - image, x + 36 * (i % 4) + 10, y + 36 * (i / 4) + 25); - } graphics->setColor(gcn::Color(0, 0, 0)); graphics->drawRectangle(gcn::Rectangle(10 + 36 * (i % 4), 36 * (i / 4) + 25, 32, 32)); - } - graphics->setColor(gcn::Color(0, 0, 0)); - graphics->drawRectangle(gcn::Rectangle(160, 25, 32, 32)); - if (arrows) { - Image *image = itemset->spriteset[ - itemDb->getItemInfo(arrows)->getImage() - 1]; + if (!(item = equipment->getEquipment(i))) { + continue; + } + + image = itemset->spriteset[item->getInfo()->getImage() - 1]; dynamic_cast<Graphics*>(graphics)->drawImage( - image, x + 160, y + 25); - std::stringstream n; - n << arrowsNumber; - graphics->drawText(n.str(), 170, 62, - gcn::Graphics::CENTER); + image, x + 36 * (i % 4) + 10, y + 36 * (i / 4) + 25); } -} - -void EquipmentWindow::action(const std::string &eventId) -{ -} -void EquipmentWindow::addEquipment(int index, int id) -{ - equipments[index].id = id; -} - -void EquipmentWindow::removeEquipment(int index) -{ - equipments[index].id = 0; -} + graphics->setColor(gcn::Color(0, 0, 0)); + graphics->drawRectangle(gcn::Rectangle(160, 25, 32, 32)); -void EquipmentWindow::setInventoryIndex(int index, int inventoryIndex) -{ - equipments[index].inventoryIndex = inventoryIndex; -} + if (!(item = equipment->getArrows())) { + return; + } -int EquipmentWindow::getInventoryIndex(int index) -{ - return equipments[index].inventoryIndex; -} + image = itemset->spriteset[item->getInfo()->getImage() - 1]; -void EquipmentWindow::setArrows(int id) -{ - arrows = id; + dynamic_cast<Graphics*>(graphics)->drawImage( + image, x + 160, y + 25); + std::stringstream n; + n << item->getQuantity(); + graphics->drawText(n.str(), 170, 62, + gcn::Graphics::CENTER); } -int EquipmentWindow::getArrows() +void EquipmentWindow::action(const std::string &eventId) { - return arrows; } diff --git a/src/gui/equipment.h b/src/gui/equipment.h index 87f543ca..5670d065 100644 --- a/src/gui/equipment.h +++ b/src/gui/equipment.h @@ -27,11 +27,6 @@ #include "../graphic/spriteset.h" #include "window.h" -typedef struct { - int id; - int inventoryIndex; -} EQUIPMENT_HOLDER; - /** * Equipment dialog. * @@ -58,25 +53,8 @@ class EquipmentWindow : public Window, gcn::ActionListener { * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - - void addEquipment(int index, int id); - - void removeEquipment(int index); - - void setInventoryIndex(int index, int inventoryIndex); - - int getInventoryIndex(int index); - void setArrows(int id); - - int getArrows(); - - EQUIPMENT_HOLDER equipments[10]; - - int arrowsNumber; - private: - int arrows; Spriteset *itemset; }; diff --git a/src/gui/inventory.cpp b/src/gui/inventory.cpp index 73bf67f5..98f17134 100644 --- a/src/gui/inventory.cpp +++ b/src/gui/inventory.cpp @@ -23,12 +23,11 @@ #include "../playerinfo.h" #include "inventory.h" -#include "equipment.h" +#include "../equipment.h" #include "button.h" #include "scrollarea.h" #include "../net/network.h" #include "item_amount.h" -#include "../resources/itemmanager.h" #include <string> InventoryWindow::InventoryWindow(): @@ -97,119 +96,94 @@ void InventoryWindow::logic() weightLabel->adjustSize(); } -int InventoryWindow::addItem(int index, int id, int quantity, bool equipment) -{ - items->addItem(index, id, quantity, equipment); - return 0; -} - -int InventoryWindow::removeItem(int id) -{ - items->removeItem(id); - return 0; -} - -int InventoryWindow::changeQuantity(int index, int quantity) -{ - items->changeQuantity(index, quantity); - return 0; -} - -int InventoryWindow::increaseQuantity(int index, int quantity) -{ - items->increaseQuantity(index, quantity); - return 0; -} - -int InventoryWindow::useItem(int index, int id) +int InventoryWindow::useItem(Item *item) { WFIFOW(0) = net_w_value(0x00a7); - WFIFOW(2) = net_w_value(index); - WFIFOL(4) = net_l_value(id); + 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(int index, int quantity) +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(index); + 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(int index) +void InventoryWindow::equipItem(Item *item) { WFIFOW(0) = net_w_value(0x00a9); - WFIFOW(2) = net_w_value(index); + WFIFOW(2) = net_w_value(item->getInvIndex()); WFIFOW(4) = net_w_value(0); WFIFOSET(6); while ((out_size > 0)) flush(); } -void InventoryWindow::unequipItem(int index) +void InventoryWindow::unequipItem(Item *item) { WFIFOW(0) = net_w_value(0x00ab); - WFIFOW(2) = net_w_value(index); + 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 - for (int i = 0; i < 8; i++) - { - if ( equipmentWindow->getInventoryIndex(i) == index ) - { - equipmentWindow->removeEquipment(i); - } - } + Equipment::getInstance()->removeEquipment(item); } void InventoryWindow::action(const std::string &eventId) { - int selectedItem = items->getIndex(); - - if (selectedItem != -1) { - if (eventId == "use") { - if (items->isEquipment(selectedItem)) { - if (items->isEquipped(selectedItem)) { - unequipItem(selectedItem); - } - else { - equipItem(selectedItem); - } + Item *item = items->getItem(); + + if (!item) { + return; + } + + if (eventId == "use") { + if (item->isEquipment()) { + if (item->isEquipped()) { + unequipItem(item); } else { - useItem(selectedItem, items->getId()); + equipItem(item); } } - else if (eventId == "drop") - { - // Choose amount of items to drop - new ItemAmountWindow(AMOUNT_ITEM_DROP, this); + 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); - if (items->getIndex() != -1) - { - // Show Name and Description - std::string SomeText; - SomeText = "Name: " + itemDb->getItemInfo(items->getId())->getName(); - itemNameLabel->setCaption(SomeText); - itemNameLabel->adjustSize(); - SomeText = "Description: " + itemDb->getItemInfo(items->getId())->getDescription(); - itemDescriptionLabel->setCaption(SomeText); - itemDescriptionLabel->adjustSize(); + 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) @@ -245,9 +219,11 @@ void InventoryWindow::updateWidgets() void InventoryWindow::updateButtons() { - if (items->getIndex() != -1 && items->isEquipment(items->getIndex())) + Item *item; + + if ((item = items->getItem()) && item->isEquipment()) { - if (items->isEquipped(items->getIndex())) { + if (item->isEquipped()) { useButton->setCaption("Unequip"); } else { @@ -258,6 +234,6 @@ void InventoryWindow::updateButtons() useButton ->setCaption("Use"); } - useButton->setEnabled(items->getIndex() != -1); - dropButton->setEnabled(items->getIndex() != -1); + useButton->setEnabled(!!item); + dropButton->setEnabled(!!item); } diff --git a/src/gui/inventory.h b/src/gui/inventory.h index 4fcc6870..e9924ead 100644 --- a/src/gui/inventory.h +++ b/src/gui/inventory.h @@ -51,41 +51,21 @@ class InventoryWindow : public Window, gcn::ActionListener void logic(); /** - * Add an item the inventory. - */ - int addItem(int index, int id, int quantity, bool equipment); - - /** - * Remove a item from the inventory. - */ - int removeItem(int id); - - /** * Equips an item. */ - void equipItem(int index); + void equipItem(Item *item); /** * Unequips an item. */ - void unequipItem(int index); - - /** - * Change quantity of an item. - */ - int changeQuantity(int index, int quantity); - - /** - * Increase quantity of an item. - */ - int increaseQuantity(int index, int quantity); + void unequipItem(Item *item); /** * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - int dropItem(int index, int quantity); + int dropItem(Item *item, int quantity); void mouseClick(int x, int y, int button, int count); @@ -94,7 +74,7 @@ class InventoryWindow : public Window, gcn::ActionListener ItemContainer *items; private: - int useItem(int index, int id); + int useItem(Item *item); void updateWidgets(); /** Updates widgets size/position */ void updateButtons(); /** Updates button states */ diff --git a/src/gui/item_amount.cpp b/src/gui/item_amount.cpp index 036fdc15..021da3eb 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -38,7 +38,7 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent): itemAmountOkButton = new Button("Okay"); itemAmountCancelButton = new Button("Cancel"); - itemAmountTextBox->setRange(1, inventoryWindow->items->getQuantity()); + itemAmountTextBox->setRange(1, inventoryWindow->items->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->getIndex(), itemAmountTextBox->getInt()); + inventoryWindow->dropItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "AddTrade") { - tradeWindow->tradeItem(inventoryWindow->items->getIndex(), itemAmountTextBox->getInt()); + tradeWindow->tradeItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "Plus") diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 534364dc..51780fca 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -26,7 +26,6 @@ #include "../log.h" #include "../graphics.h" #include "../resources/resourcemanager.h" -#include "../resources/itemmanager.h" #include <sstream> ItemContainer::ItemContainer() @@ -40,14 +39,10 @@ ItemContainer::ItemContainer() selImg = resman->getImage("graphics/gui/selection.png"); if (!selImg) logger->error("Unable to load selection.png"); - selectedItem = -1; // No item selected + selectedItem = 0; // No item selected - for (int i = 0; i < INVENTORY_SIZE; i++) - { - items[i].id = -1; - items[i].quantity = 0; - items[i].equipment = false; - items[i].equipped = false; + for (int i = 0; i < INVENTORY_SIZE; i++) { + items[i].setInvIndex(i); } addMouseListener(this); @@ -76,9 +71,9 @@ void ItemContainer::draw(gcn::Graphics* graphics) // Reset selected item when quantity not above 0 (should probably be made // sure somewhere else) - if (items[selectedItem].quantity <= 0) + if (selectedItem && selectedItem->getQuantity() <= 0) { - selectedItem = -1; + selectedItem = 0; } /* @@ -87,32 +82,31 @@ void ItemContainer::draw(gcn::Graphics* graphics) */ for (int i = 2; i < INVENTORY_SIZE; i++) { - if (items[i].quantity > 0) + if (items[i].getQuantity() > 0) { int itemX = ((i - 2) % columns) * gridWidth; int itemY = ((i - 2) / columns) * gridHeight; // Draw selection image below selected item - if (selectedItem == i) + if (selectedItem == &items[i]) { dynamic_cast<Graphics*>(graphics)->drawImage( selImg, x + itemX, y + itemY); } // Draw item icon - if (itemDb->getItemInfo(items[i].id)->getImage() > 0) + int idx; + if ((idx = items[i].getInfo()->getImage()) > 0) { - Image *image = itemset->spriteset[itemDb->getItemInfo( - items[i].id)->getImage() - 1]; dynamic_cast<Graphics*>(graphics)->drawImage( - image, x + itemX, y + itemY); + itemset->spriteset[idx - 1], x + itemX, y + itemY); } // Draw item caption std::stringstream ss; - if (!items[i].equipped) { - ss << items[i].quantity; + if (!items[i].isEquipped()) { + ss << items[i].getQuantity(); } else { ss << "Eq."; @@ -143,63 +137,37 @@ void ItemContainer::setWidth(int width) (INVENTORY_SIZE % columns > 0 ? 1 : 0)) * gridHeight); } -int ItemContainer::getIndex() -{ - return selectedItem; -} - int ItemContainer::getIndex(int id) { for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].id == id) { + if (items[i].getId() == id) { return i; } } return -1; } -int ItemContainer::getId() -{ - if (selectedItem != -1) { - return items[selectedItem].id; - } - else { - return 0; - } -} - -int ItemContainer::getId(int index) -{ - return items[index].id; -} - -int ItemContainer::getQuantity() +Item* ItemContainer::getItem() { - if (selectedItem != -1) { - return items[selectedItem].quantity; - } - else { - return 0; - } - + return selectedItem; } -int ItemContainer::getQuantity(int index) +Item* ItemContainer::getItem(int index) { - return items[index].quantity; + return &items[index]; } void ItemContainer::addItem(int index, int id, int quantity, bool equipment) { - items[index].id = id; - items[index].quantity += quantity; - items[index].equipment = 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].id == -1) { + if (items[i].getId() == -1) { return i; } } @@ -209,37 +177,27 @@ int ItemContainer::getFreeSlot() void ItemContainer::resetItems() { for (int i = 0; i < INVENTORY_SIZE; i++) { - items[i].id = -1; - items[i].quantity = 0; - items[i].equipped = false; + items[i].setId(-1); + items[i].setQuantity(0); + items[i].setEquipped(false); } } void ItemContainer::selectNone() { - selectedItem = -1; + selectedItem = 0; } void ItemContainer::removeItem(int id) { for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].id == id) { - items[i].id = -1; - items[i].quantity = 0; + if (items[i].getId() == id) { + items[i].setId(-1); + items[i].setQuantity(0); } } } -void ItemContainer::changeQuantity(int index, int quantity) -{ - items[index].quantity = quantity; -} - -void ItemContainer::increaseQuantity(int index, int quantity) -{ - items[index].quantity += quantity; -} - void ItemContainer::mousePress(int mx, int my, int button) { int gridWidth = itemset->spriteset[0]->getWidth() + 4; @@ -249,41 +207,21 @@ void ItemContainer::mousePress(int mx, int my, int button) if (button == gcn::MouseInput::LEFT) { - selectedItem = mx / gridWidth + ((my / gridHeight) * columns) + 2; - } + int index = mx / gridWidth + ((my / gridHeight) * columns) + 2; - if (selectedItem > INVENTORY_SIZE) - { - selectedItem = INVENTORY_SIZE; + if (index > INVENTORY_SIZE) { + index = INVENTORY_SIZE - 1; + } + selectedItem = &items[index]; } } -bool ItemContainer::isEquipment(int index) -{ - return items[index].equipment; -} - -bool ItemContainer::isEquipped(int index) -{ - return items[index].equipped; -} - -void ItemContainer::setEquipped(int index, bool equipped) -{ - items[index].equipped = equipped; -} - -void ItemContainer::setEquipment(int index, bool equipment) -{ - items[index].equipment = equipment; -} - int ItemContainer::getNumberOfSlotsUsed() { int NumberOfFilledSlot = 0; for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].id > -1 || items[i].quantity > 0) + if (items[i].getId() > -1 || items[i].getQuantity() > 0) { NumberOfFilledSlot++; } diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h index a0e3b79a..6e72eba0 100644 --- a/src/gui/itemcontainer.h +++ b/src/gui/itemcontainer.h @@ -26,36 +26,22 @@ #include <iostream> #include <guichan.hpp> + +#include "../item.h" + +#include "../item.h" #include "../resources/image.h" #include "../graphic/spriteset.h" #define INVENTORY_SIZE 100 /** - * The holder of a item. - */ -struct ITEM_HOLDER -{ - int id; /**< The id of the item */ - int quantity; /**< The number of items */ - bool equipment; /**< Whether this item is equipment */ - bool equipped; /**< Whether this item is equipped */ -}; - -/** * An item container. Used to show items in inventory and trade dialog. * * \ingroup GUI */ class ItemContainer : public gcn::Widget, public gcn::MouseListener { - private: - Spriteset *itemset; - Image *selImg; - int selectedItem; - int itemNumber; - ITEM_HOLDER items[INVENTORY_SIZE]; /**< The holder of items */ - public: /** * Constructor. Initializes the graphic. @@ -84,34 +70,19 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener void mousePress(int mx, int my, int button); /** - * Returns index of the selected item. - */ - int getIndex(); - - /** * Finds the index of an item. */ int getIndex(int id); /** - * Returns the id of the selected item. - */ - int getId(); - - /** - * Returns the id of an item. + * Returns the selected item. */ - int getId(int index); + Item* getItem(); /** - * Returns the quantity of the selected item. + * Returns the item at the specified index. */ - int getQuantity(); - - /** - * Returns the quantity of an item. - */ - int getQuantity(int index); + Item* getItem(int index); /** * Returns id of next free slot or -1 if all occupied. @@ -139,39 +110,17 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener void removeItem(int id); /** - * Change quantity of an item. - */ - void changeQuantity(int index, int quantity); - - /** - * Increase quantity of an item. - */ - void increaseQuantity(int index, int quantity); - - /** - * Returns whether the item at the specified index is equipment. - */ - bool isEquipment(int index); - - /** - * Returns whether the item at the specified index is equipped. - */ - bool isEquipped(int index); - - /** - * Sets whether the item at the specified index is equipped. - */ - void setEquipped(int index, bool equipped); - - /** - * Sets whether the item at the specified index is equipment. - */ - void setEquipment(int index, bool equipment); - - /** * Get the number of slots filled with an item */ int getNumberOfSlotsUsed(); + + private: + 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 5693e091..5ce56438 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -29,7 +29,6 @@ #include "listbox.h" #include "../game.h" #include "../net/network.h" -#include "../resources/itemmanager.h" #include <sstream> @@ -91,15 +90,19 @@ void SellDialog::reset() void SellDialog::addItem(short index, int price) { - int id = inventoryWindow->items->getId(index); + Item *item = inventoryWindow->items->getItem(index); + + if (!item) + return; + ITEM_SHOP item_shop; sprintf(item_shop.name, "%s %i gp", - itemDb->getItemInfo(id)->getName().c_str(), price); + item->getInfo()->getName().c_str(), price); item_shop.price = price; item_shop.index = index; - item_shop.id = id; - item_shop.quantity = inventoryWindow->items->getQuantity(index); + item_shop.id = item->getId();; + item_shop.quantity = item->getQuantity(); shopInventory.push_back(item_shop); itemList->adjustSize(); diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 4516400f..ddf9bc0f 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -30,7 +30,6 @@ #include "scrollarea.h" #include "textfield.h" #include "../net/network.h" -#include "../resources/itemmanager.h" #include <sstream> TradeWindow::TradeWindow(): @@ -164,18 +163,18 @@ void TradeWindow::removeItem(int id, bool own) void TradeWindow::changeQuantity(int index, bool own, int quantity) { if (own) { - myItems->changeQuantity(index, quantity); + myItems->getItem(index)->setQuantity(quantity); } else { - partnerItems->changeQuantity(index, quantity); + partnerItems->getItem(index)->setQuantity(quantity); } } void TradeWindow::increaseQuantity(int index, bool own, int quantity) { if (own) { - myItems->increaseQuantity(index, quantity); + myItems->getItem(index)->increaseQuantity(quantity); } else { - partnerItems->increaseQuantity(index, quantity); + partnerItems->getItem(index)->increaseQuantity(quantity); } } @@ -220,10 +219,10 @@ void TradeWindow::receivedOk(bool own) } } -void TradeWindow::tradeItem(int index, int quantity) +void TradeWindow::tradeItem(Item *item, int quantity) { WFIFOW(0) = net_w_value(0x00e8); - WFIFOW(2) = net_w_value(index); + WFIFOW(2) = net_w_value(item->getInvIndex()); WFIFOL(4) = net_l_value(quantity); WFIFOSET(8); while ((out_size > 0)) flush(); @@ -231,75 +230,66 @@ void TradeWindow::tradeItem(int index, int quantity) void TradeWindow::mouseClick(int x, int y, int button, int count) { - Window::mouseClick(x, y, button, count); + Item *item; + // myItems selected if (x >= myScroll->getX() + 3 && x <= myScroll->getX() + myScroll->getWidth() - 10 && y >= myScroll->getY() + 16 - && y <= myScroll->getY() + myScroll->getHeight() + 15) + && y <= myScroll->getY() + myScroll->getHeight() + 15 + && (item = myItems->getItem())) { - if (myItems->getIndex() != -1) - { partnerItems->selectNone(); - - // Show Name and Description - std::string SomeText; - SomeText = "Name: " + - itemDb->getItemInfo(myItems->getId())->getName(); - itemNameLabel->setCaption(SomeText); - itemNameLabel->adjustSize(); - SomeText = "Description: " + - itemDb->getItemInfo(myItems->getId())->getDescription(); - itemDescriptionLabel->setCaption(SomeText); - itemDescriptionLabel->adjustSize(); - } // partnerItems selected } else if (x >= partnerScroll->getX() + 3 && x <= partnerScroll->getX() + partnerScroll->getWidth() - 20 && y >= partnerScroll->getY() + 16 - && y <= partnerScroll->getY() + partnerScroll->getHeight() + 15) + && y <= partnerScroll->getY() + partnerScroll->getHeight() + 15 + && (item = partnerItems->getItem())) { - if (partnerItems->getIndex() != -1) - { myItems->selectNone(); - - // Show Name and Description - std::string SomeText; - SomeText = "Name: " + - itemDb->getItemInfo(partnerItems->getId())->getName(); - itemNameLabel->setCaption(SomeText); - itemNameLabel->adjustSize(); - SomeText = "Description: " + - itemDb->getItemInfo(partnerItems->getId())->getDescription(); - itemDescriptionLabel->setCaption(SomeText); - itemDescriptionLabel->adjustSize(); - } + } else { + 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 TradeWindow::action(const std::string &eventId) { + Item *item = inventoryWindow->items->getItem(); + if (eventId == "add") { - if (inventoryWindow->items->getIndex() >= 0 && - inventoryWindow->items->getIndex() <= INVENTORY_SIZE) { - if (tradeWindow->myItems->getFreeSlot() >= 0) { - if (tradeWindow->myItems->getIndex( - inventoryWindow->items->getId()) == -1) { - if (inventoryWindow->items->getQuantity() == 1) { - tradeItem(inventoryWindow->items->getIndex(), 1); - } - else { - // Choose amount of items to trade - new ItemAmountWindow(AMOUNT_TRADE_ADD, this); - } - } else { - chatWindow->chat_log("Failed adding item. You can not " - "overlap one kind of item on the window.", BY_SERVER); - } - } + if (!item) { + return; + } + + if (tradeWindow->myItems->getFreeSlot() < 1) { + return; + } + + if (myItems->getIndex(item->getId()) != -1) { + chatWindow->chat_log("Failed adding item. You can not " + "overlap one kind of item on the window.", BY_SERVER); + return; + } + + if (item->getQuantity() == 1) { + tradeItem(item, 1); + } + else { + // Choose amount of items to trade + new ItemAmountWindow(AMOUNT_TRADE_ADD, this); } } else if (eventId == "cancel") diff --git a/src/gui/trade.h b/src/gui/trade.h index 5b824548..6132a182 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -89,7 +89,7 @@ class TradeWindow : public Window, gcn::ActionListener /** * Send trade packet. */ - void TradeWindow::tradeItem(int index, int quantity); + void TradeWindow::tradeItem(Item *item, int quantity); /** * Called on mouse click. diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 7c925077..4911261e 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -211,7 +211,7 @@ int downloadThread(void *ptr) if (memoryTransfer) { downloadedBytes = 0; - curl_easy_setopt(curl, CURLOPT_FAILONERROR, TRUE); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memoryWrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL); } diff --git a/src/item.cpp b/src/item.cpp new file mode 100644 index 00000000..e49de630 --- /dev/null +++ b/src/item.cpp @@ -0,0 +1,39 @@ +/* + * 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 "item.h" +#include "resources/itemmanager.h" + +Item::Item(int id, int quantity, bool equipment, bool equipped): + id(id), quantity(quantity), equipment(equipment), equipped(equipped) +{ +} + +Item::~Item() +{ +} + +ItemInfo* Item::getInfo() +{ + return itemDb->getItemInfo(id); +} diff --git a/src/item.h b/src/item.h new file mode 100644 index 00000000..3b51874d --- /dev/null +++ b/src/item.h @@ -0,0 +1,119 @@ +/* + * 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 _ITEM_H +#define _ITEM_H + +#include "resources/iteminfo.h" + +class Item +{ + public: + Item(int id=-1, int quantity=0, + bool equipment=false, bool equipped=false); + + ~Item(); + + void setId(int id); + int getId(); + + void setQuantity(int quantity); + void increaseQuantity(int amount); + int getQuantity(); + + void setEquipment(bool equipment); + bool isEquipment(); + + void setEquipped(bool equipped); + bool isEquipped(); + + int getInvIndex(); + void setInvIndex(int index); + + ItemInfo* getInfo(); + + protected: + int id; + int quantity; + bool equipment; + bool equipped; + + int invIndex; +}; + +inline void Item::setId(int id) +{ + this->id = id; +} + +inline int Item::getId() +{ + return id; +} + +inline void Item::setQuantity(int quantity) +{ + this->quantity = quantity; +} + +inline void Item::increaseQuantity(int amount) +{ + this->quantity += amount; +} + +inline int Item::getQuantity() +{ + return quantity; +} + +inline void Item::setEquipment(bool equipment) +{ + this->equipment = equipment; +} + +inline bool Item::isEquipment() +{ + return equipment; +} + +inline void Item::setEquipped(bool equipped) +{ + this->equipped = equipped; +} + +inline bool Item::isEquipped() +{ + return equipped; +} + +inline int Item::getInvIndex() +{ + return invIndex; +} + +inline void Item::setInvIndex(int index) +{ + this->invIndex = index; +} + +#endif |