forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.h
944 lines (801 loc) · 26.5 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#ifndef FS_ITEM_H
#define FS_ITEM_H
#include "cylinder.h"
#include "items.h"
#include "luascript.h"
#include "thing.h"
class BedItem;
class Container;
class Door;
class MagicField;
class Mailbox;
class Player;
class Podium;
class Teleport;
class TrashHolder;
enum ITEMPROPERTY
{
CONST_PROP_BLOCKSOLID = 0,
CONST_PROP_HASHEIGHT,
CONST_PROP_BLOCKPROJECTILE,
CONST_PROP_BLOCKPATH,
CONST_PROP_ISVERTICAL,
CONST_PROP_ISHORIZONTAL,
CONST_PROP_MOVEABLE,
CONST_PROP_IMMOVABLEBLOCKSOLID,
CONST_PROP_IMMOVABLEBLOCKPATH,
CONST_PROP_IMMOVABLENOFIELDBLOCKPATH,
CONST_PROP_NOFIELDBLOCKPATH,
CONST_PROP_SUPPORTHANGABLE,
};
enum TradeEvents_t
{
ON_TRADE_TRANSFER,
ON_TRADE_CANCEL,
};
enum ItemDecayState_t : uint8_t
{
DECAYING_FALSE = 0,
DECAYING_TRUE,
DECAYING_PENDING,
};
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,
ATTR_NAME = 24,
ATTR_ARTICLE = 25,
ATTR_PLURALNAME = 26,
ATTR_WEIGHT = 27,
ATTR_ATTACK = 28,
ATTR_DEFENSE = 29,
ATTR_EXTRADEFENSE = 30,
ATTR_ARMOR = 31,
ATTR_HITCHANCE = 32,
ATTR_SHOOTRANGE = 33,
ATTR_CUSTOM_ATTRIBUTES = 34,
ATTR_DECAYTO = 35,
ATTR_WRAPID = 36,
ATTR_STOREITEM = 37,
ATTR_ATTACK_SPEED = 38,
ATTR_OPENCONTAINER = 39,
ATTR_PODIUMOUTFIT = 40,
// ATTR_TIER = 41, // mapeditor
ATTR_REFLECT = 42,
ATTR_BOOST = 43,
};
enum Attr_ReadValue
{
ATTR_READ_CONTINUE,
ATTR_READ_ERROR,
ATTR_READ_END,
};
class ItemAttributes
{
public:
ItemAttributes() = default;
void setSpecialDescription(const std::string& desc) { setStrAttr(ITEM_ATTRIBUTE_DESCRIPTION, desc); }
const std::string& getSpecialDescription() const { return getStrAttr(ITEM_ATTRIBUTE_DESCRIPTION); }
void setText(const std::string& text) { setStrAttr(ITEM_ATTRIBUTE_TEXT, text); }
void resetText() { removeAttribute(ITEM_ATTRIBUTE_TEXT); }
const std::string& getText() const { return getStrAttr(ITEM_ATTRIBUTE_TEXT); }
void setDate(int32_t n) { setIntAttr(ITEM_ATTRIBUTE_DATE, n); }
void resetDate() { removeAttribute(ITEM_ATTRIBUTE_DATE); }
time_t getDate() const { return static_cast<time_t>(getIntAttr(ITEM_ATTRIBUTE_DATE)); }
void setWriter(const std::string& writer) { setStrAttr(ITEM_ATTRIBUTE_WRITER, writer); }
void resetWriter() { removeAttribute(ITEM_ATTRIBUTE_WRITER); }
const std::string& getWriter() const { return getStrAttr(ITEM_ATTRIBUTE_WRITER); }
void setActionId(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_ACTIONID, n); }
uint16_t getActionId() const { return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_ACTIONID)); }
void setUniqueId(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_UNIQUEID, n); }
uint16_t getUniqueId() const { return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_UNIQUEID)); }
void setCharges(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_CHARGES, n); }
uint16_t getCharges() const { return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_CHARGES)); }
void setFluidType(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE, n); }
uint16_t getFluidType() const { return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE)); }
void setOwner(uint32_t owner) { setIntAttr(ITEM_ATTRIBUTE_OWNER, owner); }
uint32_t getOwner() const { return getIntAttr(ITEM_ATTRIBUTE_OWNER); }
void setCorpseOwner(uint32_t corpseOwner) { setIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER, corpseOwner); }
uint32_t getCorpseOwner() const { return getIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER); }
void setDuration(int32_t time) { setIntAttr(ITEM_ATTRIBUTE_DURATION, time); }
void decreaseDuration(int32_t time) { increaseIntAttr(ITEM_ATTRIBUTE_DURATION, -time); }
uint32_t getDuration() const { return getIntAttr(ITEM_ATTRIBUTE_DURATION); }
void setDecaying(ItemDecayState_t decayState) { setIntAttr(ITEM_ATTRIBUTE_DECAYSTATE, decayState); }
ItemDecayState_t getDecaying() const
{
return static_cast<ItemDecayState_t>(getIntAttr(ITEM_ATTRIBUTE_DECAYSTATE));
}
struct CustomAttribute
{
typedef boost::variant<boost::blank, std::string, int64_t, double, bool> VariantAttribute;
VariantAttribute value;
CustomAttribute() : value(boost::blank()) {}
bool operator==(const CustomAttribute& otherAttr) const { return value == otherAttr.value; }
bool operator!=(const CustomAttribute& otherAttr) const { return value != otherAttr.value; }
template <typename T>
explicit CustomAttribute(const T& v) : value(v)
{}
template <typename T>
void set(const T& v)
{
value = v;
}
template <typename T>
const T& get();
struct PushLuaVisitor : public boost::static_visitor<>
{
lua_State* L;
explicit PushLuaVisitor(lua_State* L) : boost::static_visitor<>(), L(L) {}
void operator()(const boost::blank&) const { lua_pushnil(L); }
void operator()(const std::string& v) const { LuaScriptInterface::pushString(L, v); }
void operator()(bool v) const { LuaScriptInterface::pushBoolean(L, v); }
void operator()(const int64_t& v) const { lua_pushnumber(L, v); }
void operator()(const double& v) const { lua_pushnumber(L, v); }
};
void pushToLua(lua_State* L) const { boost::apply_visitor(PushLuaVisitor(L), value); }
struct SerializeVisitor : public boost::static_visitor<>
{
PropWriteStream& propWriteStream;
explicit SerializeVisitor(PropWriteStream& propWriteStream) :
boost::static_visitor<>(), propWriteStream(propWriteStream)
{}
void operator()(const boost::blank&) const {}
void operator()(const std::string& v) const { propWriteStream.writeString(v); }
template <typename T>
void operator()(const T& v) const
{
propWriteStream.write<T>(v);
}
};
void serialize(PropWriteStream& propWriteStream) const
{
propWriteStream.write<uint8_t>(static_cast<uint8_t>(value.which()));
boost::apply_visitor(SerializeVisitor(propWriteStream), value);
}
bool unserialize(PropStream& propStream)
{
// This is hard-coded so it's not general, depends on the position of the variants.
uint8_t pos;
if (!propStream.read<uint8_t>(pos)) {
return false;
}
switch (pos) {
case 1: { // std::string
auto [str, ok] = propStream.readString();
if (!ok) {
return false;
}
value = std::string{str};
break;
}
case 2: { // int64_t
int64_t tmp;
if (!propStream.read<int64_t>(tmp)) {
return false;
}
value = tmp;
break;
}
case 3: { // double
double tmp;
if (!propStream.read<double>(tmp)) {
return false;
}
value = tmp;
break;
}
case 4: { // bool
bool tmp;
if (!propStream.read<bool>(tmp)) {
return false;
}
value = tmp;
break;
}
default: {
value = boost::blank();
return false;
}
}
return true;
}
};
private:
bool hasAttribute(itemAttrTypes type) const { return (type & attributeBits) != 0; }
void removeAttribute(itemAttrTypes type);
static std::string emptyString;
static int64_t emptyInt;
static double emptyDouble;
static bool emptyBool;
static Reflect emptyReflect;
typedef std::unordered_map<std::string, CustomAttribute> CustomAttributeMap;
struct Attribute
{
union
{
int64_t integer;
std::string* string;
CustomAttributeMap* custom;
} value;
itemAttrTypes type;
explicit Attribute(itemAttrTypes type) : type(type) { memset(&value, 0, sizeof(value)); }
Attribute(const Attribute& i)
{
type = i.type;
if (ItemAttributes::isIntAttrType(type)) {
value.integer = i.value.integer;
} else if (ItemAttributes::isStrAttrType(type)) {
value.string = new std::string(*i.value.string);
} else if (ItemAttributes::isCustomAttrType(type)) {
value.custom = new CustomAttributeMap(*i.value.custom);
} else {
memset(&value, 0, sizeof(value));
}
}
Attribute(Attribute&& attribute) : value(attribute.value), type(attribute.type)
{
memset(&attribute.value, 0, sizeof(value));
attribute.type = ITEM_ATTRIBUTE_NONE;
}
~Attribute()
{
if (ItemAttributes::isStrAttrType(type)) {
delete value.string;
} else if (ItemAttributes::isCustomAttrType(type)) {
delete value.custom;
}
}
Attribute& operator=(Attribute other)
{
Attribute::swap(*this, other);
return *this;
}
Attribute& operator=(Attribute&& other)
{
if (this != &other) {
if (ItemAttributes::isStrAttrType(type)) {
delete value.string;
} else if (ItemAttributes::isCustomAttrType(type)) {
delete value.custom;
}
value = other.value;
type = other.type;
memset(&other.value, 0, sizeof(value));
other.type = ITEM_ATTRIBUTE_NONE;
}
return *this;
}
static void swap(Attribute& first, Attribute& second)
{
std::swap(first.value, second.value);
std::swap(first.type, second.type);
}
};
std::vector<Attribute> attributes;
uint32_t attributeBits = 0;
std::map<CombatType_t, Reflect> reflect;
std::map<CombatType_t, uint16_t> boostPercent;
const Reflect& getReflect(CombatType_t combatType)
{
auto it = reflect.find(combatType);
return it != reflect.end() ? it->second : emptyReflect;
}
int16_t getBoostPercent(CombatType_t combatType)
{
auto it = boostPercent.find(combatType);
return it != boostPercent.end() ? it->second : 0;
}
const std::string& getStrAttr(itemAttrTypes type) const;
void setStrAttr(itemAttrTypes type, std::string_view value);
int64_t getIntAttr(itemAttrTypes type) const;
void setIntAttr(itemAttrTypes type, int64_t value);
void increaseIntAttr(itemAttrTypes type, int64_t value);
const Attribute* getExistingAttr(itemAttrTypes type) const;
Attribute& getAttr(itemAttrTypes type);
CustomAttributeMap* getCustomAttributeMap()
{
if (!hasAttribute(ITEM_ATTRIBUTE_CUSTOM)) {
return nullptr;
}
return getAttr(ITEM_ATTRIBUTE_CUSTOM).value.custom;
}
template <typename R>
void setCustomAttribute(int64_t key, R value)
{
auto tmp = std::to_string(key);
setCustomAttribute(tmp, value);
}
void setCustomAttribute(int64_t key, CustomAttribute& value)
{
auto tmp = std::to_string(key);
setCustomAttribute(tmp, value);
}
template <typename R>
void setCustomAttribute(std::string_view key, R value)
{
if (hasAttribute(ITEM_ATTRIBUTE_CUSTOM)) {
removeCustomAttribute(key);
} else {
getAttr(ITEM_ATTRIBUTE_CUSTOM).value.custom = new CustomAttributeMap();
}
auto lowercaseKey = boost::algorithm::to_lower_copy(std::string{key});
getAttr(ITEM_ATTRIBUTE_CUSTOM).value.custom->emplace(lowercaseKey, value);
}
void setCustomAttribute(std::string_view key, const CustomAttribute& value)
{
if (hasAttribute(ITEM_ATTRIBUTE_CUSTOM)) {
removeCustomAttribute(key);
} else {
getAttr(ITEM_ATTRIBUTE_CUSTOM).value.custom = new CustomAttributeMap();
}
auto lowercaseKey = boost::algorithm::to_lower_copy(std::string{key});
getAttr(ITEM_ATTRIBUTE_CUSTOM).value.custom->emplace(lowercaseKey, value);
}
const CustomAttribute* getCustomAttribute(int64_t key)
{
auto tmp = std::to_string(key);
return getCustomAttribute(tmp);
}
const CustomAttribute* getCustomAttribute(std::string_view key)
{
if (const CustomAttributeMap* customAttrMap = getCustomAttributeMap()) {
auto lowercaseKey = boost::algorithm::to_lower_copy(std::string{key});
if (auto it = customAttrMap->find(lowercaseKey); it != customAttrMap->end()) {
return &(it->second);
}
}
return nullptr;
}
bool removeCustomAttribute(int64_t key)
{
auto tmp = std::to_string(key);
return removeCustomAttribute(tmp);
}
bool removeCustomAttribute(std::string_view key)
{
if (CustomAttributeMap* customAttrMap = getCustomAttributeMap()) {
auto lowercaseKey = boost::algorithm::to_lower_copy(std::string{key});
if (auto it = customAttrMap->find(lowercaseKey); it != customAttrMap->end()) {
customAttrMap->erase(it);
return true;
}
}
return false;
}
const static uint32_t intAttributeTypes =
ITEM_ATTRIBUTE_ACTIONID | ITEM_ATTRIBUTE_UNIQUEID | ITEM_ATTRIBUTE_DATE | ITEM_ATTRIBUTE_WEIGHT |
ITEM_ATTRIBUTE_ATTACK | ITEM_ATTRIBUTE_DEFENSE | ITEM_ATTRIBUTE_EXTRADEFENSE | ITEM_ATTRIBUTE_ARMOR |
ITEM_ATTRIBUTE_HITCHANCE | ITEM_ATTRIBUTE_SHOOTRANGE | ITEM_ATTRIBUTE_OWNER | ITEM_ATTRIBUTE_DURATION |
ITEM_ATTRIBUTE_DECAYSTATE | ITEM_ATTRIBUTE_CORPSEOWNER | ITEM_ATTRIBUTE_CHARGES | ITEM_ATTRIBUTE_FLUIDTYPE |
ITEM_ATTRIBUTE_DOORID | ITEM_ATTRIBUTE_DECAYTO | ITEM_ATTRIBUTE_WRAPID | ITEM_ATTRIBUTE_STOREITEM |
ITEM_ATTRIBUTE_ATTACK_SPEED | ITEM_ATTRIBUTE_OPENCONTAINER;
const static uint32_t stringAttributeTypes = ITEM_ATTRIBUTE_DESCRIPTION | ITEM_ATTRIBUTE_TEXT |
ITEM_ATTRIBUTE_WRITER | ITEM_ATTRIBUTE_NAME | ITEM_ATTRIBUTE_ARTICLE |
ITEM_ATTRIBUTE_PLURALNAME;
public:
static bool isIntAttrType(itemAttrTypes type) { return (type & intAttributeTypes) == type; }
static bool isStrAttrType(itemAttrTypes type) { return (type & stringAttributeTypes) == type; }
inline static bool isCustomAttrType(itemAttrTypes type) { return (type & ITEM_ATTRIBUTE_CUSTOM) == type; }
const std::vector<Attribute>& getList() const { return attributes; }
friend class Item;
};
class Item : virtual public Thing
{
public:
// Factory member to create item of right type based on type
static Item* CreateItem(const uint16_t type, uint16_t count = 0);
static Container* CreateItemAsContainer(const uint16_t type, uint16_t size);
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 ~Item() = default;
// non-assignable
Item& operator=(const Item&) = delete;
bool equals(const Item* otherItem) const;
Item* getItem() override final { return this; }
const Item* getItem() const override final { return this; }
virtual Teleport* getTeleport() { return nullptr; }
virtual const Teleport* getTeleport() const { return nullptr; }
virtual TrashHolder* getTrashHolder() { return nullptr; }
virtual const TrashHolder* getTrashHolder() const { return nullptr; }
virtual Mailbox* getMailbox() { return nullptr; }
virtual const Mailbox* getMailbox() const { return nullptr; }
virtual Door* getDoor() { return nullptr; }
virtual const Door* getDoor() const { return nullptr; }
virtual MagicField* getMagicField() { return nullptr; }
virtual const MagicField* getMagicField() const { return nullptr; }
virtual BedItem* getBed() { return nullptr; }
virtual const BedItem* getBed() const { return nullptr; }
virtual Podium* getPodium() { return nullptr; }
virtual const Podium* getPodium() const { return nullptr; }
const std::string& getStrAttr(itemAttrTypes type) const
{
if (!attributes) {
return ItemAttributes::emptyString;
}
return attributes->getStrAttr(type);
}
void setStrAttr(itemAttrTypes type, std::string_view value) { getAttributes()->setStrAttr(type, value); }
int64_t getIntAttr(itemAttrTypes type) const
{
if (!attributes) {
return 0;
}
return attributes->getIntAttr(type);
}
void setIntAttr(itemAttrTypes type, int64_t value) { getAttributes()->setIntAttr(type, value); }
void increaseIntAttr(itemAttrTypes type, int64_t value) { getAttributes()->increaseIntAttr(type, value); }
void removeAttribute(itemAttrTypes type)
{
if (attributes) {
attributes->removeAttribute(type);
}
}
bool hasAttribute(itemAttrTypes type) const
{
if (!attributes) {
return false;
}
return attributes->hasAttribute(type);
}
template <typename R>
void setCustomAttribute(std::string_view key, R value)
{
getAttributes()->setCustomAttribute(key, value);
}
void setCustomAttribute(std::string_view key, ItemAttributes::CustomAttribute& value)
{
getAttributes()->setCustomAttribute(key, value);
}
const ItemAttributes::CustomAttribute* getCustomAttribute(int64_t key)
{
if (!attributes) {
return nullptr;
}
return getAttributes()->getCustomAttribute(key);
}
const ItemAttributes::CustomAttribute* getCustomAttribute(const std::string& key)
{
if (!attributes) {
return nullptr;
}
return getAttributes()->getCustomAttribute(key);
}
bool removeCustomAttribute(int64_t key)
{
if (!attributes) {
return false;
}
return getAttributes()->removeCustomAttribute(key);
}
bool removeCustomAttribute(std::string_view key)
{
if (!attributes) {
return false;
}
return getAttributes()->removeCustomAttribute(key);
}
void setSpecialDescription(std::string_view desc) { setStrAttr(ITEM_ATTRIBUTE_DESCRIPTION, desc); }
const std::string& getSpecialDescription() const { return getStrAttr(ITEM_ATTRIBUTE_DESCRIPTION); }
void setText(std::string_view text) { setStrAttr(ITEM_ATTRIBUTE_TEXT, text); }
void resetText() { removeAttribute(ITEM_ATTRIBUTE_TEXT); }
const std::string& getText() const { return getStrAttr(ITEM_ATTRIBUTE_TEXT); }
void setDate(int32_t n) { setIntAttr(ITEM_ATTRIBUTE_DATE, n); }
void resetDate() { removeAttribute(ITEM_ATTRIBUTE_DATE); }
time_t getDate() const { return static_cast<time_t>(getIntAttr(ITEM_ATTRIBUTE_DATE)); }
void setWriter(std::string_view writer) { setStrAttr(ITEM_ATTRIBUTE_WRITER, writer); }
void resetWriter() { removeAttribute(ITEM_ATTRIBUTE_WRITER); }
const std::string& getWriter() const { return getStrAttr(ITEM_ATTRIBUTE_WRITER); }
void setActionId(uint16_t n)
{
if (n < 100) {
n = 100;
}
setIntAttr(ITEM_ATTRIBUTE_ACTIONID, n);
}
uint16_t getActionId() const
{
if (!attributes) {
return 0;
}
return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_ACTIONID));
}
uint16_t getUniqueId() const
{
if (!attributes) {
return 0;
}
return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_UNIQUEID));
}
void setCharges(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_CHARGES, n); }
uint16_t getCharges() const
{
if (!attributes) {
return 0;
}
return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_CHARGES));
}
void setFluidType(uint16_t n) { setIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE, n); }
uint16_t getFluidType() const
{
if (!attributes) {
return 0;
}
return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE));
}
void setOwner(uint32_t owner) { setIntAttr(ITEM_ATTRIBUTE_OWNER, owner); }
uint32_t getOwner() const
{
if (!attributes) {
return 0;
}
return getIntAttr(ITEM_ATTRIBUTE_OWNER);
}
void setCorpseOwner(uint32_t corpseOwner) { setIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER, corpseOwner); }
uint32_t getCorpseOwner() const
{
if (!attributes) {
return 0;
}
return getIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER);
}
void setDuration(int32_t time) { setIntAttr(ITEM_ATTRIBUTE_DURATION, time); }
void decreaseDuration(int32_t time) { increaseIntAttr(ITEM_ATTRIBUTE_DURATION, -time); }
uint32_t getDuration() const
{
if (!attributes) {
return 0;
}
return getIntAttr(ITEM_ATTRIBUTE_DURATION);
}
void setDecaying(ItemDecayState_t decayState) { setIntAttr(ITEM_ATTRIBUTE_DECAYSTATE, decayState); }
ItemDecayState_t getDecaying() const
{
if (!attributes) {
return DECAYING_FALSE;
}
return static_cast<ItemDecayState_t>(getIntAttr(ITEM_ATTRIBUTE_DECAYSTATE));
}
int32_t getDecayTime() const
{
if (hasAttribute(ITEM_ATTRIBUTE_DURATION)) {
return getIntAttr(ITEM_ATTRIBUTE_DURATION);
}
return items[id].decayTime;
}
void setDecayTo(int32_t decayTo) { setIntAttr(ITEM_ATTRIBUTE_DECAYTO, decayTo); }
int32_t getDecayTo() const
{
if (hasAttribute(ITEM_ATTRIBUTE_DECAYTO)) {
return getIntAttr(ITEM_ATTRIBUTE_DECAYTO);
}
return items[id].decayTo;
}
static std::string getNameDescription(const ItemType& it, const Item* item = nullptr, int32_t subType = -1,
bool addArticle = true);
static std::string getWeightDescription(const ItemType& it, uint32_t weight, uint32_t count = 1);
std::string getDescription(int32_t lookDistance) const override final;
std::string getNameDescription() const;
std::string getWeightDescription() const;
// serialization
virtual Attr_ReadValue readAttr(AttrTypes_t attr, PropStream& propStream);
bool unserializeAttr(PropStream& propStream);
virtual bool unserializeItemNode(OTB::Loader&, const OTB::Node&, PropStream& propStream);
virtual void serializeAttr(PropWriteStream& propWriteStream) const;
bool isPushable() const override final { return isMoveable(); }
int32_t getThrowRange() const override final { 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
const Player* getHoldingPlayer() const;
WeaponType_t getWeaponType() const { return items[id].weaponType; }
Ammo_t getAmmoType() const { return items[id].ammoType; }
uint8_t getShootRange() const
{
if (hasAttribute(ITEM_ATTRIBUTE_SHOOTRANGE)) {
return getIntAttr(ITEM_ATTRIBUTE_SHOOTRANGE);
}
return items[id].shootRange;
}
virtual uint32_t getWeight() const;
uint32_t getBaseWeight() const
{
if (hasAttribute(ITEM_ATTRIBUTE_WEIGHT)) {
return getIntAttr(ITEM_ATTRIBUTE_WEIGHT);
}
return items[id].weight;
}
int32_t getAttack() const
{
if (hasAttribute(ITEM_ATTRIBUTE_ATTACK)) {
return getIntAttr(ITEM_ATTRIBUTE_ATTACK);
}
return items[id].attack;
}
uint32_t getAttackSpeed() const
{
if (hasAttribute(ITEM_ATTRIBUTE_ATTACK_SPEED)) {
return getIntAttr(ITEM_ATTRIBUTE_ATTACK_SPEED);
}
return items[id].attackSpeed;
}
int32_t getArmor() const
{
if (hasAttribute(ITEM_ATTRIBUTE_ARMOR)) {
return getIntAttr(ITEM_ATTRIBUTE_ARMOR);
}
return items[id].armor;
}
int32_t getDefense() const
{
if (hasAttribute(ITEM_ATTRIBUTE_DEFENSE)) {
return getIntAttr(ITEM_ATTRIBUTE_DEFENSE);
}
return items[id].defense;
}
int32_t getExtraDefense() const
{
if (hasAttribute(ITEM_ATTRIBUTE_EXTRADEFENSE)) {
return getIntAttr(ITEM_ATTRIBUTE_EXTRADEFENSE);
}
return items[id].extraDefense;
}
int32_t getSlotPosition() const { return items[id].slotPosition; }
int8_t getHitChance() const
{
if (hasAttribute(ITEM_ATTRIBUTE_HITCHANCE)) {
return getIntAttr(ITEM_ATTRIBUTE_HITCHANCE);
}
return items[id].hitChance;
}
uint32_t getWorth() const;
LightInfo getLightInfo() const;
void setReflect(CombatType_t combatType, const Reflect& reflect) { getAttributes()->reflect[combatType] = reflect; }
Reflect getReflect(CombatType_t combatType, bool total = true) const;
void setBoostPercent(CombatType_t combatType, uint16_t value) { getAttributes()->boostPercent[combatType] = value; }
uint16_t getBoostPercent(CombatType_t combatType, bool total = true) const;
bool hasProperty(ITEMPROPERTY prop) const;
bool isBlocking() const { return items[id].blockSolid; }
bool isStackable() const { return items[id].stackable; }
bool isAlwaysOnTop() const { return items[id].alwaysOnTop; }
bool isGroundTile() const { return items[id].isGroundTile(); }
bool isMagicField() const { return items[id].isMagicField(); }
bool isMoveable() const { return items[id].moveable; }
bool isPickupable() const { return items[id].pickupable; }
bool isUseable() const { return items[id].useable; }
bool isHangable() const { return items[id].isHangable; }
bool isRotatable() const
{
const ItemType& it = items[id];
return it.rotatable && it.rotateTo;
}
bool isPodium() const { return items[id].isPodium(); }
bool hasWalkStack() const { return items[id].walkStack; }
bool isSupply() const { return items[id].isSupply(); }
void setStoreItem(bool storeItem) { setIntAttr(ITEM_ATTRIBUTE_STOREITEM, static_cast<int64_t>(storeItem)); }
bool isStoreItem() const
{
if (hasAttribute(ITEM_ATTRIBUTE_STOREITEM)) {
return getIntAttr(ITEM_ATTRIBUTE_STOREITEM) == 1;
}
return items[id].storeItem;
}
const std::string& getName() const
{
if (hasAttribute(ITEM_ATTRIBUTE_NAME)) {
return getStrAttr(ITEM_ATTRIBUTE_NAME);
}
return items[id].name;
}
const std::string getPluralName() const
{
if (hasAttribute(ITEM_ATTRIBUTE_PLURALNAME)) {
return getStrAttr(ITEM_ATTRIBUTE_PLURALNAME);
}
return items[id].getPluralName();
}
const std::string& getArticle() const
{
if (hasAttribute(ITEM_ATTRIBUTE_ARTICLE)) {
return getStrAttr(ITEM_ATTRIBUTE_ARTICLE);
}
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)
{
if (subType == -1 || subType == i->getSubType()) {
return i->getItemCount();
}
return 0;
}
void setDefaultSubtype();
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() const;
virtual bool canRemove() const { return true; }
virtual bool canTransform() const { return true; }
virtual void onRemoved();
virtual void onTradeEvent(TradeEvents_t, Player*) {}
virtual void startDecaying();
bool isLoadedFromMap() const { return loadedFromMap; }
void setLoadedFromMap(bool value) { loadedFromMap = value; }
bool isCleanable() const
{
return !loadedFromMap && canRemove() && isPickupable() && !hasAttribute(ITEM_ATTRIBUTE_UNIQUEID) &&
!hasAttribute(ITEM_ATTRIBUTE_ACTIONID);
}
bool hasMarketAttributes() const;
std::unique_ptr<ItemAttributes>& getAttributes()
{
if (!attributes) {
attributes.reset(new ItemAttributes());
}
return attributes;
}
void incrementReferenceCounter() { ++referenceCounter; }
void decrementReferenceCounter()
{
if (--referenceCounter == 0) {
delete this;
}
}
Cylinder* getParent() const override { return parent; }
void setParent(Cylinder* cylinder) override { parent = cylinder; }
Cylinder* getTopParent();
const Cylinder* getTopParent() const;
Tile* getTile() override;
const Tile* getTile() const override;
bool isRemoved() const override { return !parent || parent->isRemoved(); }
protected:
Cylinder* parent = nullptr;
uint16_t id; // the same id as in ItemType
private:
std::string getWeightDescription(uint32_t weight) const;
std::unique_ptr<ItemAttributes> attributes;
uint32_t referenceCounter = 0;
uint8_t count = 1; // number of stacked items
bool loadedFromMap = false;
// Don't add variables here, use the ItemAttribute class.
};
using ItemList = std::list<Item*>;
using ItemDeque = std::deque<Item*>;
#endif // FS_ITEM_H