-
Notifications
You must be signed in to change notification settings - Fork 21
/
item.h
416 lines (345 loc) · 13.3 KB
/
item.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// Item represents an existing item.
//////////////////////////////////////////////////////////////////////
// This program 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 (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#ifndef __OTSERV_ITEM_H__
#define __OTSERV_ITEM_H__
#include "thing.h"
#include "items.h"
#include <iostream>
#include <list>
#include <deque>
#include <vector>
class Creature;
class Player;
class Container;
class Depot;
class Teleport;
class TrashHolder;
class Mailbox;
class Door;
class MagicField;
class BedItem;
enum ITEMPROPERTY
{
BLOCKSOLID = 0,
HASHEIGHT,
BLOCKPROJECTILE,
BLOCKPATH,
ISVERTICAL,
ISHORIZONTAL,
MOVEABLE,
IMMOVABLEBLOCKSOLID,
IMMOVABLEBLOCKPATH,
IMMOVABLENOFIELDBLOCKPATH,
NOFIELDBLOCKPATH,
SUPPORTHANGABLE
};
enum TradeEvents_t
{
ON_TRADE_TRANSFER,
ON_TRADE_CANCEL,
};
enum ItemDecayState_t
{
DECAYING_FALSE = 0,
DECAYING_TRUE,
DECAYING_PENDING
};
/*from iomap.h*/
#pragma pack(1)
struct TeleportDest
{
uint16_t _x;
uint16_t _y;
uint8_t _z;
};
#pragma pack()
enum AttrTypes_t
{
//ATTR_DESCRIPTION = 1,
//ATTR_EXT_FILE = 2,
ATTR_TILE_FLAGS = 3,
ATTR_ACTION_ID = 4,
ATTR_UNIQUE_ID = 5,
ATTR_TEXT = 6,
ATTR_DESC = 7,
ATTR_TELE_DEST = 8,
ATTR_ITEM = 9,
ATTR_DEPOT_ID = 10,
//ATTR_EXT_SPAWN_FILE = 11,
ATTR_RUNE_CHARGES = 12,
//ATTR_EXT_HOUSE_FILE = 13,
ATTR_HOUSEDOORID = 14,
ATTR_COUNT = 15,
ATTR_DURATION = 16,
ATTR_DECAYING_STATE = 17,
ATTR_WRITTENDATE = 18,
ATTR_WRITTENBY = 19,
ATTR_SLEEPERGUID = 20,
ATTR_SLEEPSTART = 21,
ATTR_CHARGES = 22,
ATTR_CONTAINER_ITEMS = 23
};
enum Attr_ReadValue
{
ATTR_READ_CONTINUE,
ATTR_READ_ERROR,
ATTR_READ_END
};
class ItemAttributes
{
public:
ItemAttributes()
{
m_attributes = 0;
m_firstAttr = NULL;
}
virtual ~ItemAttributes()
{
if(m_firstAttr)
deleteAttrs(m_firstAttr);
}
ItemAttributes(const ItemAttributes &i)
{
m_attributes = i.m_attributes;
if(i.m_firstAttr)
m_firstAttr = new Attribute(*i.m_firstAttr);
}
void setSpecialDescription(const std::string& desc) {setStrAttr(ATTR_ITEM_DESC, desc);}
void resetSpecialDescription() {removeAttribute(ATTR_ITEM_DESC);}
const std::string& getSpecialDescription() const {return getStrAttr(ATTR_ITEM_DESC);}
void setText(const std::string& text) {setStrAttr(ATTR_ITEM_TEXT, text);}
void resetText() {removeAttribute(ATTR_ITEM_TEXT);}
const std::string& getText() const {return getStrAttr(ATTR_ITEM_TEXT);}
void setDate(int32_t n) {setIntAttr(ATTR_ITEM_WRITTENDATE, n);}
void resetDate() {removeAttribute(ATTR_ITEM_WRITTENDATE);}
time_t getDate() const {return (time_t)getIntAttr(ATTR_ITEM_WRITTENDATE);}
void setWriter(const std::string& _writer) {setStrAttr(ATTR_ITEM_WRITTENBY, _writer);}
void resetWriter() {removeAttribute(ATTR_ITEM_WRITTENBY);}
const std::string& getWriter() const {return getStrAttr(ATTR_ITEM_WRITTENBY);}
void setActionId(uint16_t n) {if(n < 100) n = 100; setIntAttr(ATTR_ITEM_ACTIONID, n);}
uint16_t getActionId() const {return (uint16_t)getIntAttr(ATTR_ITEM_ACTIONID);}
void setUniqueId(uint16_t n) {if(n < 1000) n = 1000; setIntAttr(ATTR_ITEM_UNIQUEID, n);}
uint16_t getUniqueId() const {return (uint16_t)getIntAttr(ATTR_ITEM_UNIQUEID);}
void setCharges(uint16_t n) {setIntAttr(ATTR_ITEM_CHARGES, n);}
uint16_t getCharges() const {return (uint16_t)getIntAttr(ATTR_ITEM_CHARGES);}
void setFluidType(uint16_t n) {setIntAttr(ATTR_ITEM_FLUIDTYPE, n);}
uint16_t getFluidType() const {return (uint16_t)getIntAttr(ATTR_ITEM_FLUIDTYPE);}
void setOwner(uint32_t _owner) {setIntAttr(ATTR_ITEM_OWNER, _owner);}
uint32_t getOwner() const {return getIntAttr(ATTR_ITEM_OWNER);}
void setCorpseOwner(uint32_t _corpseOwner) {setIntAttr(ATTR_ITEM_CORPSEOWNER, _corpseOwner);}
uint32_t getCorpseOwner() {return getIntAttr(ATTR_ITEM_CORPSEOWNER);}
void setDuration(int32_t time) {setIntAttr(ATTR_ITEM_DURATION, time);}
void decreaseDuration(int32_t time) {increaseIntAttr(ATTR_ITEM_DURATION, -time);}
uint32_t getDuration() const {return getIntAttr(ATTR_ITEM_DURATION);}
void setDecaying(ItemDecayState_t decayState) {setIntAttr(ATTR_ITEM_DECAYING, decayState);}
ItemDecayState_t getDecaying() const {return (ItemDecayState_t)getIntAttr(ATTR_ITEM_DECAYING);}
protected:
enum itemAttrTypes
{
ATTR_ITEM_ACTIONID = 1,
ATTR_ITEM_UNIQUEID = 2,
ATTR_ITEM_DESC = 4,
ATTR_ITEM_TEXT = 8,
ATTR_ITEM_WRITTENDATE = 16,
ATTR_ITEM_WRITTENBY = 32,
ATTR_ITEM_OWNER = 65536,
ATTR_ITEM_DURATION = 131072,
ATTR_ITEM_DECAYING = 262144,
ATTR_ITEM_CORPSEOWNER = 524288,
ATTR_ITEM_CHARGES = 1048576,
ATTR_ITEM_FLUIDTYPE = 2097152,
ATTR_ITEM_DOORID = 4194304
};
bool hasAttribute(itemAttrTypes type) const;
void removeAttribute(itemAttrTypes type);
static std::string emptyString;
class Attribute
{
public:
itemAttrTypes type;
void* value;
Attribute* next;
Attribute(itemAttrTypes _type)
{
type = _type;
value = NULL;
next = NULL;
}
Attribute(const Attribute& i)
{
type = i.type;
if(ItemAttributes::validateIntAttrType(type))
value = i.value;
else if(ItemAttributes::validateStrAttrType(type))
value = (void*)new std::string( *((std::string*)i.value) );
else
value = NULL;
next = NULL;
if(i.next)
next = new Attribute(*i.next);
}
};
uint32_t m_attributes;
Attribute* m_firstAttr;
const std::string& getStrAttr(itemAttrTypes type) const;
void setStrAttr(itemAttrTypes type, const std::string& value);
uint32_t getIntAttr(itemAttrTypes type) const;
void setIntAttr(itemAttrTypes type, int32_t value);
void increaseIntAttr(itemAttrTypes type, int32_t value);
static bool validateIntAttrType(itemAttrTypes type);
static bool validateStrAttrType(itemAttrTypes type);
void addAttr(Attribute* attr);
Attribute* getAttrConst(itemAttrTypes type) const;
Attribute* getAttr(itemAttrTypes type);
void deleteAttrs(Attribute* attr);
};
class Item : virtual public Thing, public ItemAttributes
{
public:
//Factory member to create item of right type based on type
static Item* CreateItem(const uint16_t _type, uint16_t _count = 0);
static Item* CreateItem(PropStream& propStream);
static Items items;
// Constructor for items
Item(const uint16_t _type, uint16_t _count = 0);
Item(const Item &i);
virtual Item* clone() const;
virtual void copyAttributes(Item* item);
virtual ~Item();
virtual Item* getItem() {return this;}
virtual const Item* getItem() const {return this;}
virtual Container* getContainer() {return NULL;}
virtual const Container* getContainer() const {return NULL;}
virtual Teleport* getTeleport() {return NULL;}
virtual const Teleport* getTeleport() const {return NULL;}
virtual TrashHolder* getTrashHolder() {return NULL;}
virtual const TrashHolder* getTrashHolder() const {return NULL;}
virtual Mailbox* getMailbox() {return NULL;}
virtual const Mailbox* getMailbox() const {return NULL;}
virtual Door* getDoor() {return NULL;}
virtual const Door* getDoor() const {return NULL;}
virtual MagicField* getMagicField() {return NULL;}
virtual const MagicField* getMagicField() const {return NULL;}
virtual BedItem* getBed() {return NULL;}
virtual const BedItem* getBed() const {return NULL;}
static std::string getDescription(const ItemType& it, int32_t lookDistance, const Item* item = NULL, int32_t subType = -1, bool addArticle = true);
static std::string getNameDescription(const ItemType& it, const Item* item = NULL, int32_t subType = -1, bool addArticle = true);
static std::string getWeightDescription(const ItemType& it, double weight, uint32_t count = 1);
virtual std::string getDescription(int32_t lookDistance) const;
std::string getNameDescription() const;
std::string getWeightDescription() const;
//serialization
virtual Attr_ReadValue readAttr(AttrTypes_t attr, PropStream& propStream);
virtual bool unserializeAttr(PropStream& propStream);
virtual bool unserializeItemNode(FileLoader& f, NODE node, PropStream& propStream);
virtual bool serializeAttr(PropWriteStream& propWriteStream) const;
virtual bool isPushable() const {return !isNotMoveable();}
virtual int32_t getThrowRange() const {return (isPickupable() ? 15 : 2);}
uint16_t getID() const {return id;}
uint16_t getClientID() const {return items[id].clientId;}
void setID(uint16_t newid);
// Returns the player that is holding this item in his inventory
Player* getHoldingPlayer();
const Player* getHoldingPlayer() const;
WeaponType_t getWeaponType() const {return items[id].weaponType;}
Ammo_t getAmmoType() const {return items[id].ammoType;}
int32_t getShootRange() const {return items[id].shootRange;}
virtual double getWeight() const;
int32_t getAttack() const {return items[id].attack;}
int32_t getArmor() const {return items[id].armor;}
int32_t getDefense() const {return items[id].defense;}
int32_t getExtraDefense() const {return items[id].extraDefense;}
int32_t getSlotPosition() const {return items[id].slotPosition;}
int32_t getHitChance() const {return items[id].hitChance;}
bool isReadable() const {return items[id].canReadText;}
bool canWriteText() const {return items[id].canWriteText;}
uint16_t getMaxWriteLength() const {return items[id].maxTextLen;}
int32_t getWorth() const;
void getLight(LightInfo& lightInfo);
bool hasProperty(enum ITEMPROPERTY prop) const;
bool isBlocking() const {return items[id].blockSolid;}
bool isStackable() const {return items[id].stackable;}
bool isRune() const {return items[id].isRune();}
bool isFluidContainer() const {return (items[id].isFluidContainer());}
bool isAlwaysOnTop() const {return items[id].alwaysOnTop;}
bool isGroundTile() const {return items[id].isGroundTile();}
bool isSplash() const {return items[id].isSplash();}
bool isMagicField() const {return items[id].isMagicField();}
bool isNotMoveable() const {return !items[id].moveable;}
bool isPickupable() const {return items[id].pickupable;}
bool isWeapon() const {return (items[id].weaponType != WEAPON_NONE);}
bool isUseable() const {return items[id].useable;}
bool isHangable() const {return items[id].isHangable;}
bool isRoteable() const {const ItemType& it = items[id]; return it.rotable && it.rotateTo;}
bool isDoor() const {return items[id].isDoor();}
bool isBed() const {return items[id].isBed();}
bool hasCharges() const {return getCharges() > 0;}
bool hasWalkStack() const {return items[id].walkStack;}
bool floorChangeDown() const {return items[id].floorChangeDown;}
bool floorChangeNorth() const {return items[id].floorChangeNorth;}
bool floorChangeSouth() const {return items[id].floorChangeSouth;}
bool floorChangeSouthAlt() const {return items[id].floorChangeSouthAlt;}
bool floorChangeEast() const {return items[id].floorChangeEast;}
bool floorChangeEastAlt() const {return items[id].floorChangeEastAlt;}
bool floorChangeWest() const {return items[id].floorChangeWest;}
const std::string& getName() const {return items[id].name;}
const std::string getPluralName() const {return items[id].getPluralName();}
const std::string& getArticle() const {return items[id].article;}
// get the number of items
uint16_t getItemCount() const {return count;}
void setItemCount(uint8_t n) {count = n;}
static uint32_t countByType(const Item* i, int32_t subType);
void setDefaultSubtype();
bool hasSubType() const;
uint16_t getSubType() const;
void setSubType(uint16_t n);
void setUniqueId(uint16_t n);
void setDefaultDuration()
{
uint32_t duration = getDefaultDuration();
if(duration != 0)
setDuration(duration);
}
uint32_t getDefaultDuration() const {return items[id].decayTime * 1000;}
bool canDecay();
virtual bool canRemove() const {return true;}
virtual bool canTransform() const {return true;}
virtual void onRemoved();
virtual bool onTradeEvent(TradeEvents_t event, Player* owner) {return true;}
virtual void __startDecaying();
bool isLoadedFromMap() const {return loadedFromMap;}
void setLoadedFromMap(bool value) {loadedFromMap = value;}
bool isCleanable() const {return(!loadedFromMap && (getUniqueId() == 0 && getActionId() == 0) && isPickupable() && canRemove()); }
protected:
std::string getWeightDescription(double weight) const;
uint16_t id; // the same id as in ItemType
uint8_t count; // number of stacked items
bool loadedFromMap;
//Don't add variables here, use the ItemAttribute class.
};
typedef std::list<Item*> ItemList;
typedef std::deque<Item*> ItemDeque;
inline uint32_t Item::countByType(const Item* i, int32_t subType)
{
if(subType == -1 || subType == i->getSubType())
return i->getItemCount();
return 0;
}
#endif