1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef ITEM_H
24#define ITEM_H
25
26#include <framework/global.h>
27
28#include "thing.h"
29#include "effect.h"
30#include "itemtype.h"
31
32enum ItemAttr : uint8
33{
34 ATTR_END = 0,
35 //ATTR_DESCRIPTION = 1,
36 //ATTR_EXT_FILE = 2,
37 ATTR_TILE_FLAGS = 3,
38 ATTR_ACTION_ID = 4,
39 ATTR_UNIQUE_ID = 5,
40 ATTR_TEXT = 6,
41 ATTR_DESC = 7,
42 ATTR_TELE_DEST = 8,
43 ATTR_ITEM = 9,
44 ATTR_DEPOT_ID = 10,
45 //ATTR_EXT_SPAWN_FILE = 11,
46 ATTR_RUNE_CHARGES = 12,
47 //ATTR_EXT_HOUSE_FILE = 13,
48 ATTR_HOUSEDOORID = 14,
49 ATTR_COUNT = 15,
50 ATTR_DURATION = 16,
51 ATTR_DECAYING_STATE = 17,
52 ATTR_WRITTENDATE = 18,
53 ATTR_WRITTENBY = 19,
54 ATTR_SLEEPERGUID = 20,
55 ATTR_SLEEPSTART = 21,
56 ATTR_CHARGES = 22,
57 ATTR_CONTAINER_ITEMS = 23,
58 ATTR_NAME = 30,
59 ATTR_PLURALNAME = 31,
60 ATTR_ATTACK = 33,
61 ATTR_EXTRAATTACK = 34,
62 ATTR_DEFENSE = 35,
63 ATTR_EXTRADEFENSE = 36,
64 ATTR_ARMOR = 37,
65 ATTR_ATTACKSPEED = 38,
66 ATTR_HITCHANCE = 39,
67 ATTR_SHOOTRANGE = 40,
68 ATTR_ARTICLE = 41,
69 ATTR_SCRIPTPROTECTED = 42,
70 ATTR_DUALWIELD = 43,
71 ATTR_ATTRIBUTE_MAP = 128
72};
73
74// @bindclass
75#pragma pack(push,1) // disable memory alignment
76class Item : public Thing
77{
78public:
79 Item();
80 virtual ~Item() { }
81
82 static ItemPtr create(int id, int countOrSubtype = 1);
83 static ItemPtr createFromOtb(int id);
84
85 void draw(const Point& dest, bool animate = true, LightView* lightView = nullptr);
86 void draw(const Rect& dest, bool animate = true);
87
88 void setId(uint32 id);
89 void setOtbId(uint16 id);
90 void setCountOrSubType(int value) { m_countOrSubType = value; }
91 void setCount(int count) { m_countOrSubType = count; }
92 void setSubType(int subType) { m_countOrSubType = subType; }
93 void setColor(const Color& c) { m_color = c; }
94 void setTooltip(const std::string& str) { m_tooltip = str; }
95 void setQuickLootFlags(uint32 flags) { m_quickLootFlags = flags; }
96 void setShader(const std::string& str) { m_shader = str; }
97
98 int getCountOrSubType() { return m_countOrSubType; }
99 int getSubType();
100 int getCount();
101 uint32 getId() { return m_clientId; }
102 uint16 getClientId() { return m_clientId; }
103 uint16 getServerId() { return m_serverId; }
104 std::string getName();
105 bool isValid();
106 std::string getTooltip() { return m_tooltip; }
107 uint32 getQuickLootFlags() { return m_quickLootFlags; }
108 std::string getShader() { return m_shader; }
109
110 void unserializeItem(const BinaryTreePtr& in);
111 void serializeItem(const OutputBinaryTreePtr& out);
112
113 void setDepotId(uint16 depotId) { m_attribs.set(ATTR_DEPOT_ID, depotId); }
114 uint16 getDepotId() { return m_attribs.get<uint16>(ATTR_DEPOT_ID); }
115
116 void setDoorId(uint8 doorId) { m_attribs.set(ATTR_HOUSEDOORID, doorId); }
117 uint8 getDoorId() { return m_attribs.get<uint8>(ATTR_HOUSEDOORID); }
118
119 uint16 getUniqueId() { return m_attribs.get<uint16>(ATTR_UNIQUE_ID); }
120 uint16 getActionId() { return m_attribs.get<uint16>(ATTR_ACTION_ID); }
121 void setActionId(uint16 actionId) { m_attribs.set(ATTR_ACTION_ID, actionId); }
122 void setUniqueId(uint16 uniqueId) { m_attribs.set(ATTR_UNIQUE_ID, uniqueId); }
123
124 std::string getText() { return m_attribs.get<std::string>(ATTR_TEXT); }
125 std::string getDescription() { return m_attribs.get<std::string>(ATTR_DESC); }
126 void setDescription(std::string desc) { m_attribs.set(ATTR_DESC, desc); }
127 void setText(std::string txt) { m_attribs.set(ATTR_TEXT, txt); }
128
129 Position getTeleportDestination() { return m_attribs.get<Position>(ATTR_TELE_DEST); }
130 void setTeleportDestination(const Position& pos) { m_attribs.set(ATTR_TELE_DEST, pos); }
131
132 void setAsync(bool enable) { m_async = enable; }
133
134 bool isHouseDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
135 bool isDepot() { return m_attribs.has(ATTR_DEPOT_ID); }
136 bool isContainer() { return m_attribs.has(ATTR_CONTAINER_ITEMS); }
137 bool isDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
138 bool isTeleport() { return m_attribs.has(ATTR_TELE_DEST); }
139 bool isMoveable();
140 bool isGround();
141
142 ItemPtr clone();
143 ItemPtr asItem() { return static_self_cast<Item>(); }
144 bool isItem() { return true; }
145
146 ItemVector getContainerItems() { return m_containerItems; }
147 ItemPtr getContainerItem(int slot) { return m_containerItems[slot]; }
148 void addContainerItemIndexed(const ItemPtr& i, int slot) { m_containerItems[slot] = i; }
149 void addContainerItem(const ItemPtr& i) { m_containerItems.push_back(i); }
150 void removeContainerItem(int slot) { m_containerItems[slot] = nullptr; }
151 void clearContainerItems() { m_containerItems.clear(); }
152
153 void calculatePatterns(int& xPattern, int& yPattern, int& zPattern);
154 int calculateAnimationPhase(bool animate);
155 int getExactSize(int layer = 0, int xPattern = 0, int yPattern = 0, int zPattern = 0, int animationPhase = 0);
156
157 const ThingTypePtr& getThingType();
158 ThingType *rawGetThingType();
159
160 void setCustomAttribute(uint16 key, uint64 value) {
161 m_customAttribs.set(key, value);
162 }
163 uint64 getCustomAttribute(uint16 key) {
164 return m_customAttribs.get<uint64>(key);
165 }
166
167private:
168 uint16 m_clientId;
169 uint16 m_serverId;
170 uint16 m_countOrSubType;
171 stdext::packed_storage<uint8> m_attribs;
172 ItemVector m_containerItems;
173 Color m_color;
174 bool m_async;
175 std::string m_tooltip;
176 std::string m_shader;
177
178 uint32 m_quickLootFlags;
179 uint8 m_phase;
180 ticks_t m_lastPhase;
181
182 stdext::packed_storage<uint16> m_customAttribs;
183};
184
185#pragma pack(pop)
186
187#endif
188