Mana
Loading...
Searching...
No Matches
inventoryhandler.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "eventlistener.h"
25#include "inventory.h"
26#include "item.h"
27#include "log.h"
28#include "playerinfo.h"
29
30#include "gui/inventorywindow.h"
31
33#include "net/net.h"
34
36
37#include "resources/iteminfo.h"
38
39#include "utils/gettext.h"
40
41#include <vector>
42
43namespace TmwAthena {
44
45class EquipBackend final : public Equipment::Backend
46{
47 public:
49 {
50 memset(mEquipment, -1, sizeof(mEquipment));
51 }
52
53 Item *getEquipment(int index) const override
54 {
55 int invyIndex = mEquipment[index];
56 return PlayerInfo::getInventory()->getItem(invyIndex);
57 }
58
59 std::string getSlotName(int slotIndex) const override
60 {
61 switch (slotIndex)
62 {
64 return std::string(_("Torso"));
65 case EQUIP_ARMS_SLOT:
66 return std::string(_("Arms"));
67 case EQUIP_HEAD_SLOT:
68 return std::string(_("Head"));
69 case EQUIP_LEGS_SLOT:
70 return std::string(_("Legs"));
71 case EQUIP_FEET_SLOT:
72 return std::string(_("Feet"));
74 return std::string(_("Ring 1/2"));
76 return std::string(_("Ring 2/2"));
78 return std::string(_("Necklace"));
80 return std::string(_("Hand 1/2"));
82 return std::string(_("Hand 2/2"));
84 return std::string(_("Ammo"));
85 default:
86 return std::string();
87 }
88 }
89
90 void clear() override
91 {
92 for (int i = 0; i < EQUIP_VECTOR_END; i++)
93 {
94 if (mEquipment[i] != -1)
95 {
96 if (Item *item = PlayerInfo::getInventory()->getItem(i))
97 item->setEquipped(false);
98 }
99
100 mEquipment[i] = -1;
101 }
102 }
103
104 void setEquipment(int index, int inventoryIndex)
105 {
106 Inventory *inventory = PlayerInfo::getInventory();
107 Item *newItem = inventory->getItem(inventoryIndex);
108
109 if (!newItem && inventoryIndex >= 0)
110 {
111 Log::info("EquipBackend: Warning, trying to equip "
112 "non-existing item from inventory index %i at "
113 "equipment slot %i.", inventoryIndex, index);
114 return;
115 }
116
117 // Unequip existing item
118 if (Item *oldItem = inventory->getItem(mEquipment[index]))
119 oldItem->setEquipped(false);
120
121 if (newItem)
122 newItem->setEquipped(true);
123
124 mEquipment[index] = inventoryIndex;
126 }
127
128 void triggerUnequip(int slotIndex) const override
129 {
130 if (Item *item = getEquipment(slotIndex))
131 item->doEvent(Event::DoUnequip);
132 }
133
134 int getSlotNumber() const override
135 { return EQUIP_VECTOR_END; }
136
137 private:
139};
140
145{
146 int slot;
147 int id;
149 bool equip;
150};
151
153 public EventListener
154{
155 public:
156 enum {
158 CART
159 };
160
162
163 ~InventoryHandler() override;
164
165 void handleMessage(MessageIn &msg) override;
166
167 void event(Event::Channel channel, const Event &event) override;
168
169 size_t getSize(int type) const override;
170
171 // Note the slot type id is equal to the slot Index for tA.
172 bool isWeaponSlot(unsigned int slotTypeId) const override
173 {
174 return (slotTypeId == EQUIP_FIGHT1_SLOT ||
175 slotTypeId == EQUIP_FIGHT1_SLOT);
176 }
177
178 bool isAmmoSlot(unsigned int slotTypeId) const override
179 {
180 return slotTypeId == EQUIP_PROJECTILE_SLOT;
181 }
182
184 {
185 return &mEquips;
186 }
187
188 private:
190 std::vector<InventoryItem> mInventoryItems;
191 Inventory *mStorage = nullptr;
193};
194
195} // namespace TmwAthena
Definition event.h:42
@ DoUnequip
Definition event.h:78
Channel
Definition event.h:45
Inventory dialog.
void updateButtons()
Updates the buttons.
Item * getItem(int index) const
Returns the item at the specified index.
Definition inventory.cpp:44
Represents one or more instances of a certain item type.
Definition item.h:35
void setEquipped(bool equipped)
Sets whether this item is equipped.
Definition item.h:74
int mEquipment[EQUIP_VECTOR_END]
Item * getEquipment(int index) const override
std::string getSlotName(int slotIndex) const override
void triggerUnequip(int slotIndex) const override
void setEquipment(int index, int inventoryIndex)
int getSlotNumber() const override
size_t getSize(int type) const override
void event(Event::Channel channel, const Event &event) override
bool isWeaponSlot(unsigned int slotTypeId) const override
void handleMessage(MessageIn &msg) override
Equipment::Backend * getEquipmentBackend() override
bool isAmmoSlot(unsigned int slotTypeId) const override
std::vector< InventoryItem > mInventoryItems
Used for parsing an incoming message from eAthena.
Definition messagein.h:35
InventoryWindow * inventoryWindow
Definition game.cpp:96
#define _(s)
Definition gettext.h:38
void info(const char *log_text,...) LOG_PRINTF_ATTR
Inventory * getInventory()
Returns the player's inventory.
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
@ EQUIP_ARMS_SLOT
Definition iteminfo.h:167
@ EQUIP_LEGS_SLOT
Definition iteminfo.h:171
@ EQUIP_FIGHT2_SLOT
Definition iteminfo.h:184
@ EQUIP_RING2_SLOT
Definition iteminfo.h:176
@ EQUIP_FEET_SLOT
Definition iteminfo.h:173
@ EQUIP_FIGHT1_SLOT
Definition iteminfo.h:183
@ EQUIP_RING1_SLOT
Definition iteminfo.h:175
@ EQUIP_HEAD_SLOT
Definition iteminfo.h:169
@ EQUIP_VECTOR_END
Definition iteminfo.h:188
@ EQUIP_NECKLACE_SLOT
Definition iteminfo.h:178
@ EQUIP_PROJECTILE_SLOT
Definition iteminfo.h:187
@ EQUIP_TORSO_SLOT
Definition iteminfo.h:165
Used to cache storage data until we get size data for it.