-
Notifications
You must be signed in to change notification settings - Fork 293
/
Item.cpp
1378 lines (1151 loc) · 46.1 KB
/
Item.cpp
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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
*/
#include "Entities/Item.h"
#include "Globals/ObjectMgr.h"
#include "Entities/ObjectGuid.h"
#include "WorldPacket.h"
#include "Database/DatabaseEnv.h"
#include "Entities/ItemEnchantmentMgr.h"
#include "Server/SQLStorages.h"
#include "Loot/LootMgr.h"
#include "Spells/SpellTargetDefines.h"
#include "Spells/SpellEffectDefines.h"
void AddItemsSetItem(Player* player, Item* item)
{
ItemPrototype const* proto = item->GetProto();
uint32 setid = proto->ItemSet;
ItemSetEntry const* set = sItemSetStore.LookupEntry(setid);
if (!set)
{
sLog.outErrorDb("Item set %u for item (id %u) not found, mods not applied.", setid, proto->ItemId);
return;
}
if (set->required_skill_id && player->GetSkillValue(set->required_skill_id) < set->required_skill_value)
return;
ItemSetEffect* eff = nullptr;
for (auto& x : player->ItemSetEff)
{
if (x && x->setid == setid)
{
eff = x;
break;
}
}
if (!eff)
{
eff = new ItemSetEffect;
memset(eff, 0, sizeof(ItemSetEffect));
eff->setid = setid;
size_t x = 0;
for (; x < player->ItemSetEff.size(); ++x)
if (!player->ItemSetEff[x])
break;
if (x < player->ItemSetEff.size())
player->ItemSetEff[x] = eff;
else
player->ItemSetEff.push_back(eff);
}
++eff->item_count;
for (uint32 x = 0; x < 8; ++x)
{
if (!set->spells[x])
continue;
// not enough for spell
if (set->items_to_triggerspell[x] > eff->item_count)
continue;
uint32 z = 0;
for (; z < 8; ++z)
if (eff->spells[z] && eff->spells[z]->Id == set->spells[x])
break;
if (z < 8)
continue;
// new spell
for (auto& spell : eff->spells)
{
if (!spell) // free slot
{
SpellEntry const* spellInfo = sSpellTemplate.LookupEntry<SpellEntry>(set->spells[x]);
if (!spellInfo)
{
sLog.outError("WORLD: unknown spell id %u in items set %u effects", set->spells[x], setid);
break;
}
// spell casted only if fit form requirement, in other case will casted at form change
player->ApplyEquipSpell(spellInfo, nullptr, true);
spell = spellInfo;
break;
}
}
}
}
void RemoveItemsSetItem(Player* player, ItemPrototype const* proto)
{
uint32 setid = proto->ItemSet;
ItemSetEntry const* set = sItemSetStore.LookupEntry(setid);
if (!set)
{
sLog.outErrorDb("Item set #%u for item #%u not found, mods not removed.", setid, proto->ItemId);
return;
}
ItemSetEffect* eff = nullptr;
size_t setindex = 0;
for (; setindex < player->ItemSetEff.size(); ++setindex)
{
if (player->ItemSetEff[setindex] && player->ItemSetEff[setindex]->setid == setid)
{
eff = player->ItemSetEff[setindex];
break;
}
}
// can be in case now enough skill requirement for set appling but set has been appliend when skill requirement not enough
if (!eff)
return;
--eff->item_count;
for (uint32 x = 0; x < 8; ++x)
{
if (!set->spells[x])
continue;
// enough for spell
if (set->items_to_triggerspell[x] <= eff->item_count)
continue;
for (auto& spell : eff->spells)
{
if (spell && spell->Id == set->spells[x])
{
// spell can be not active if not fit form requirement
player->ApplyEquipSpell(spell, nullptr, false);
spell = nullptr;
break;
}
}
}
if (!eff->item_count) // all items of a set were removed
{
MANGOS_ASSERT(eff == player->ItemSetEff[setindex]);
delete eff;
player->ItemSetEff[setindex] = nullptr;
}
}
bool ItemCanGoIntoBag(ItemPrototype const* pProto, ItemPrototype const* pBagProto)
{
if (!pProto || !pBagProto)
return false;
switch (pBagProto->Class)
{
case ITEM_CLASS_CONTAINER:
switch (pBagProto->SubClass)
{
case ITEM_SUBCLASS_CONTAINER:
return true;
case ITEM_SUBCLASS_SOUL_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_SOUL_SHARDS))
return false;
return true;
case ITEM_SUBCLASS_HERB_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_HERBS))
return false;
return true;
case ITEM_SUBCLASS_ENCHANTING_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_ENCHANTING_SUPP))
return false;
return true;
case ITEM_SUBCLASS_MINING_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_MINING_SUPP))
return false;
return true;
case ITEM_SUBCLASS_ENGINEERING_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_ENGINEERING_SUPP))
return false;
return true;
case ITEM_SUBCLASS_GEM_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_GEMS))
return false;
return true;
case ITEM_SUBCLASS_LEATHERWORKING_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_LEATHERWORKING_SUPP))
return false;
return true;
case ITEM_SUBCLASS_INSCRIPTION_CONTAINER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_INSCRIPTION_SUPP))
return false;
return true;
default:
return false;
}
case ITEM_CLASS_QUIVER:
switch (pBagProto->SubClass)
{
case ITEM_SUBCLASS_QUIVER:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_ARROWS))
return false;
return true;
case ITEM_SUBCLASS_AMMO_POUCH:
if (!(pProto->BagFamily & BAG_FAMILY_MASK_BULLETS))
return false;
return true;
default:
return false;
}
}
return false;
}
Item::Item()
{
m_objectType |= TYPEMASK_ITEM;
m_objectTypeId = TYPEID_ITEM;
m_updateFlag = UPDATEFLAG_HIGHGUID;
m_valuesCount = ITEM_END;
m_slot = 0;
uState = ITEM_NEW;
uQueuePos = -1;
m_container = nullptr;
mb_in_trade = false;
m_lootState = ITEM_LOOT_NONE;
m_usedInSpell = false;
}
bool Item::Create(uint32 guidlow, uint32 itemid, Player const* owner)
{
Object::_Create(guidlow, 0, HIGHGUID_ITEM);
SetEntry(itemid);
SetObjectScale(DEFAULT_OBJECT_SCALE);
SetGuidValue(ITEM_FIELD_OWNER, owner ? owner->GetObjectGuid() : ObjectGuid());
SetGuidValue(ITEM_FIELD_CONTAINED, ObjectGuid());
ItemPrototype const* itemProto = ObjectMgr::GetItemPrototype(itemid);
if (!itemProto)
return false;
SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
for (uint8 i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
SetSpellCharges(i, itemProto->Spells[i].SpellCharges);
SetUInt32Value(ITEM_FIELD_DURATION, itemProto->Duration);
return true;
}
void Item::UpdateDuration(Player* owner, uint32 diff)
{
if (!GetUInt32Value(ITEM_FIELD_DURATION))
return;
// DEBUG_LOG("Item::UpdateDuration Item (Entry: %u Duration %u Diff %u)", GetEntry(), GetUInt32Value(ITEM_FIELD_DURATION), diff);
if (GetUInt32Value(ITEM_FIELD_DURATION) <= diff && !IsUsedInSpell())
{
if (uint32 newItemId = sObjectMgr.GetItemExpireConvert(GetEntry()))
owner->ConvertItem(this, newItemId);
else
owner->DestroyItem(GetBagSlot(), GetSlot(), true);
return;
}
uint32 oldDuration = GetUInt32Value(ITEM_FIELD_DURATION);
SetUInt32Value(ITEM_FIELD_DURATION, oldDuration <= diff ? 1 : oldDuration - diff);
SetState(ITEM_CHANGED, owner); // save new time in database
}
void Item::SaveToDB()
{
static const char* UPDATE_ITEM = "UPDATE item_instance SET owner_guid = ?, itemEntry = ?, creatorGuid = ?, giftCreatorGuid = ?, count = ?, duration = ?, charges = ?, flags = ?, enchantments = ?, randomPropertyId = ?, durability = ?, playedTime = ?, text = ? WHERE guid = ?";
static const char* INSERT_ITEM = "REPLACE INTO item_instance (owner_guid, itemEntry, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, durability, playedTime, text, guid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
static const char* UPDATE_GIFT = "UPDATE character_gifts SET guid = ? WHERE item_guid = ?";
static const char* DELETE_TEXT = "DELETE FROM item_text WHERE id = ?";
static const char* DELETE_ITEM = "DELETE FROM item_instance WHERE guid = ?";
static const char* DELETE_GIFT = "DELETE FROM character_gifts WHERE item_guid = ?";
static const char* DELETE_LOOT = "DELETE FROM item_loot WHERE guid = ?";
uint32 guid = GetGUIDLow();
switch (uState)
{
case ITEM_NEW:
case ITEM_CHANGED:
{
static SqlStatementID insItem, updItem;
std::unique_ptr<SqlStatement> stmt;
//get thread id
//std::hash<std::thread::id> myHashObject{};
//uint32 threadID = myHashObject(std::this_thread::get_id());
if (uState == ITEM_NEW)
{
stmt.reset(new SqlStatement(CharacterDatabase.CreateStatement(insItem, INSERT_ITEM)));
//printf("> Saving new item(%s) for guid[%u]. Thread is [%u]\n", GetProto()->Name1, GetOwnerGuid().GetCounter(), threadID);
}
else
{
stmt.reset(new SqlStatement(CharacterDatabase.CreateStatement(updItem, UPDATE_ITEM)));
//printf("> Updating item(%s) for guid[%u]. Thread is [%u]\n", GetProto()->Name1, GetOwnerGuid().GetCounter(), threadID);
}
stmt->addUInt32(GetOwnerGuid().GetCounter());
stmt->addUInt32(GetEntry());
stmt->addUInt32(GetGuidValue(ITEM_FIELD_CREATOR).GetCounter());
stmt->addUInt32(GetGuidValue(ITEM_FIELD_GIFTCREATOR).GetCounter());
stmt->addUInt32(GetCount());
stmt->addUInt32(GetUInt32Value(ITEM_FIELD_DURATION));
std::ostringstream ssSpells;
for (uint8 i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
ssSpells << GetSpellCharges(i) << ' ';
stmt->addString(ssSpells.str());
stmt->addUInt32(GetUInt32Value(ITEM_FIELD_FLAGS));
std::ostringstream ssEnchants;
for (uint8 i = 0; i < MAX_ENCHANTMENT_SLOT; ++i)
{
ssEnchants << GetEnchantmentId(EnchantmentSlot(i)) << ' ';
ssEnchants << GetEnchantmentDuration(EnchantmentSlot(i)) << ' ';
ssEnchants << GetEnchantmentCharges(EnchantmentSlot(i)) << ' ';
}
stmt->addString(ssEnchants.str());
stmt->addInt16(GetItemRandomPropertyId());
stmt->addUInt16(GetUInt32Value(ITEM_FIELD_DURABILITY));
stmt->addUInt32(GetUInt32Value(ITEM_FIELD_CREATE_PLAYED_TIME));
stmt->addString(GetText());
stmt->addUInt32(guid);
stmt->Execute();
if (uState == ITEM_CHANGED && HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED))
{
static SqlStatementID updGifts;
stmt.reset(new SqlStatement(CharacterDatabase.CreateStatement(updGifts, UPDATE_GIFT)));
stmt->PExecute(GetOwnerGuid().GetCounter(), GetGUIDLow());
}
break;
}
case ITEM_REMOVED:
{
static SqlStatementID delInst;
static SqlStatementID delGifts;
static SqlStatementID delLoot;
SqlStatement stmt = CharacterDatabase.CreateStatement(delInst, DELETE_ITEM);
stmt.PExecute(guid);
if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED))
{
stmt = CharacterDatabase.CreateStatement(delGifts, DELETE_GIFT);
stmt.PExecute(GetGUIDLow());
}
if (HasSavedLoot())
{
stmt = CharacterDatabase.CreateStatement(delLoot, DELETE_LOOT);
stmt.PExecute(GetGUIDLow());
}
delete this;
return;
}
case ITEM_UNCHANGED:
return;
}
if (m_lootState == ITEM_LOOT_CHANGED || m_lootState == ITEM_LOOT_REMOVED)
{
static SqlStatementID delLoot;
SqlStatement stmt = CharacterDatabase.CreateStatement(delLoot, "DELETE FROM item_loot WHERE guid = ?");
stmt.PExecute(GetGUIDLow());
}
if (m_loot && (m_lootState == ITEM_LOOT_NEW || m_lootState == ITEM_LOOT_CHANGED))
{
if (Player* owner = GetOwner())
{
static SqlStatementID saveGold;
static SqlStatementID saveLoot;
// save money as 0 itemid data
if (m_loot->GetGoldAmount())
{
SqlStatement stmt = CharacterDatabase.CreateStatement(saveGold, "INSERT INTO item_loot (guid,owner_guid,itemid,amount,suffix,property) VALUES (?, ?, 0, ?, 0, 0)");
stmt.PExecute(GetGUIDLow(), owner->GetGUIDLow(), m_loot->GetGoldAmount());
}
SqlStatement stmt = CharacterDatabase.CreateStatement(saveLoot, "INSERT INTO item_loot (guid,owner_guid,itemid,amount,suffix,property) VALUES (?, ?, ?, ?, ?, ?)");
// save items and quest items (at load its all will added as normal, but this not important for item loot case)
LootItemList lootList;
m_loot->GetLootItemsListFor(owner, lootList);
for (LootItemList::const_iterator lootItr = lootList.begin(); lootItr != lootList.end(); ++lootItr)
{
LootItem* lootItem = *lootItr;
stmt.addUInt32(GetGUIDLow());
stmt.addUInt32(owner->GetGUIDLow());
stmt.addUInt32(lootItem->itemId);
stmt.addUInt8(lootItem->count);
stmt.addUInt32(lootItem->randomSuffix);
stmt.addInt32(lootItem->randomPropertyId);
stmt.Execute();
}
}
}
if (m_lootState != ITEM_LOOT_NONE && m_lootState != ITEM_LOOT_TEMPORARY)
SetLootState(ITEM_LOOT_UNCHANGED);
SetState(ITEM_UNCHANGED);
}
bool Item::LoadFromDB(uint32 guidLow, Field* fields, ObjectGuid ownerGuid)
{
// 0 1 2 3 4 5 6 7 8 9 10 11
// itemEntry, creatorGuid, giftCreatorGuid, count, duration, charges, flags, enchantments, randomPropertyId, durability, playedTime, text
// create item before any checks for store correct guid
// and allow use "FSetState(ITEM_REMOVED); SaveToDB();" for deleting item from DB
Object::_Create(guidLow, 0, HIGHGUID_ITEM);
// Set entry, MUST be before proto check
SetEntry(fields[0].GetUInt32());
SetObjectScale(1.0f);
ItemPrototype const* proto = GetProto();
if (!proto)
return false;
// set owner (not if item is only loaded for gbank/auction/mail)
if (ownerGuid)
SetOwnerGuid(ownerGuid);
bool need_save = false; // need explicit save data at load fixes
SetGuidValue(ITEM_FIELD_CREATOR, ObjectGuid(HIGHGUID_PLAYER, fields[1].GetUInt32()));
SetGuidValue(ITEM_FIELD_GIFTCREATOR, ObjectGuid(HIGHGUID_PLAYER, fields[2].GetUInt32()));
SetCount(fields[3].GetUInt32());
uint32 duration = fields[4].GetUInt32();
SetUInt32Value(ITEM_FIELD_DURATION, duration);
// update duration if need, and remove if not need
if ((proto->Duration == 0) != (duration == 0))
{
SetUInt32Value(ITEM_FIELD_DURATION, proto->Duration);
need_save = true;
}
Tokens tokens = StrSplit(fields[5].GetCppString(), " ");
if (tokens.size() == MAX_ITEM_PROTO_SPELLS)
for (uint8 i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
SetSpellCharges(i, atoi(tokens[i].c_str()));
SetUInt32Value(ITEM_FIELD_FLAGS, fields[6].GetUInt32());
// update max durability (and durability) if need
if (proto->MaxDurability != GetUInt32Value(ITEM_FIELD_MAXDURABILITY))
{
SetUInt32Value(ITEM_FIELD_MAXDURABILITY, proto->MaxDurability);
if (GetUInt32Value(ITEM_FIELD_DURABILITY) > proto->MaxDurability)
SetUInt32Value(ITEM_FIELD_DURABILITY, proto->MaxDurability);
need_save = true;
}
// Remove bind flag for items vs NO_BIND set
if (IsSoulBound() && proto->Bonding == NO_BIND)
{
ApplyModFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_BINDED, false);
need_save = true;
}
_LoadIntoDataField(fields[7].GetString(), ITEM_FIELD_ENCHANTMENT_1_1, MAX_ENCHANTMENT_SLOT * MAX_ENCHANTMENT_OFFSET);
SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID, fields[8].GetInt16());
// recalculate suffix factor
if (GetItemRandomPropertyId() < 0)
UpdateItemSuffixFactor();
uint32 durability = fields[9].GetUInt16();
SetUInt32Value(ITEM_FIELD_DURABILITY, durability);
// update max durability (and durability) if need
SetUInt32Value(ITEM_FIELD_MAXDURABILITY, proto->MaxDurability);
// do not overwrite durability for wrapped items
if (durability > proto->MaxDurability && !HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED))
{
SetUInt32Value(ITEM_FIELD_DURABILITY, proto->MaxDurability);
need_save = true;
}
SetUInt32Value(ITEM_FIELD_CREATE_PLAYED_TIME, fields[10].GetUInt32());
SetText(fields[11].GetCppString());
// set correct wrapped state
if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED))
{
// wrapped item must be wrapper (used version that not stackable)
if (!(proto->Flags & ITEM_FLAG_IS_WRAPPER) || GetMaxStackCount() > 1)
{
RemoveFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED);
need_save = true;
static SqlStatementID delGifts;
// also cleanup for sure gift table
SqlStatement stmt = CharacterDatabase.CreateStatement(delGifts, "DELETE FROM character_gifts WHERE item_guid = ?");
stmt.PExecute(GetGUIDLow());
}
}
if (need_save) // normal item changed state set not work at loading
{
static SqlStatementID updItem;
SqlStatement stmt = CharacterDatabase.CreateStatement(updItem, "UPDATE item_instance SET duration = ?, flags = ?, durability = ?, owner_guid= ? WHERE guid = ?");
stmt.addUInt32(GetUInt32Value(ITEM_FIELD_DURATION));
stmt.addUInt32(GetUInt32Value(ITEM_FIELD_FLAGS));
stmt.addUInt32(GetUInt32Value(ITEM_FIELD_DURABILITY));
stmt.addUInt32(GetOwnerGuid().GetCounter());
stmt.addUInt32(guidLow);
stmt.Execute();
}
return true;
}
void Item::LoadLootFromDB(Field* fields)
{
uint32 item_id = fields[1].GetUInt32();
uint32 item_amount = fields[2].GetUInt32();
uint32 item_suffix = fields[3].GetUInt32();
int32 item_propid = fields[4].GetInt32();
// money value special case
if (item_id == 0)
{
m_loot->SetGoldAmount(item_amount);
SetLootState(ITEM_LOOT_UNCHANGED);
return;
}
// normal item case
ItemPrototype const* proto = ObjectMgr::GetItemPrototype(item_id);
if (!proto)
{
CharacterDatabase.PExecute("DELETE FROM item_loot WHERE guid = '%u' AND itemid = '%u'", GetGUIDLow(), item_id);
sLog.outError("Item::LoadLootFromDB: %s has an unknown item (id: #%u) in item_loot, deleted.", GetOwnerGuid().GetString().c_str(), item_id);
return;
}
m_loot->AddItem(item_id, item_amount, item_suffix, item_propid);
SetLootState(ITEM_LOOT_UNCHANGED);
}
void Item::DeleteFromDB()
{
static SqlStatementID delItem ;
SqlStatement stmt = CharacterDatabase.CreateStatement(delItem, "DELETE FROM item_instance WHERE guid = ?");
stmt.PExecute(GetGUIDLow());
}
void Item::DeleteFromInventoryDB() const
{
static SqlStatementID delInv ;
SqlStatement stmt = CharacterDatabase.CreateStatement(delInv, "DELETE FROM character_inventory WHERE item = ?");
stmt.PExecute(GetGUIDLow());
}
ItemPrototype const* Item::GetProto() const
{
return ObjectMgr::GetItemPrototype(GetEntry());
}
Player* Item::GetOwner()const
{
return sObjectMgr.GetPlayer(GetOwnerGuid());
}
uint32 Item::GetSkill() const
{
const static uint32 item_weapon_skills[MAX_ITEM_SUBCLASS_WEAPON] =
{
SKILL_AXES, SKILL_2H_AXES, SKILL_BOWS, SKILL_GUNS, SKILL_MACES,
SKILL_2H_MACES, SKILL_POLEARMS, SKILL_SWORDS, SKILL_2H_SWORDS, 0,
SKILL_STAVES, 0, 0, SKILL_UNARMED, 0,
SKILL_DAGGERS, SKILL_THROWN, SKILL_ASSASSINATION, SKILL_CROSSBOWS, SKILL_WANDS,
SKILL_FISHING
};
const static uint32 item_armor_skills[MAX_ITEM_SUBCLASS_ARMOR] =
{
0, SKILL_CLOTH, SKILL_LEATHER, SKILL_MAIL, SKILL_PLATE_MAIL, 0, SKILL_SHIELD, 0, 0, 0, 0
};
ItemPrototype const* proto = GetProto();
switch (proto->Class)
{
case ITEM_CLASS_WEAPON:
{
if (proto->SubClass >= MAX_ITEM_SUBCLASS_WEAPON)
return 0;
return item_weapon_skills[proto->SubClass];
}
case ITEM_CLASS_ARMOR:
{
if (proto->SubClass >= MAX_ITEM_SUBCLASS_ARMOR)
return 0;
return item_armor_skills[proto->SubClass];
}
default:
return 0;
}
}
uint32 Item::GetSpell() const
{
ItemPrototype const* proto = GetProto();
switch (proto->Class)
{
case ITEM_CLASS_WEAPON:
switch (proto->SubClass)
{
case ITEM_SUBCLASS_WEAPON_AXE: return 196;
case ITEM_SUBCLASS_WEAPON_AXE2: return 197;
case ITEM_SUBCLASS_WEAPON_BOW: return 264;
case ITEM_SUBCLASS_WEAPON_GUN: return 266;
case ITEM_SUBCLASS_WEAPON_MACE: return 198;
case ITEM_SUBCLASS_WEAPON_MACE2: return 199;
case ITEM_SUBCLASS_WEAPON_POLEARM: return 200;
case ITEM_SUBCLASS_WEAPON_SWORD: return 201;
case ITEM_SUBCLASS_WEAPON_SWORD2: return 202;
case ITEM_SUBCLASS_WEAPON_STAFF: return 227;
case ITEM_SUBCLASS_WEAPON_DAGGER: return 1180;
case ITEM_SUBCLASS_WEAPON_THROWN: return 2567;
case ITEM_SUBCLASS_WEAPON_SPEAR: return 3386;
case ITEM_SUBCLASS_WEAPON_CROSSBOW: return 5011;
case ITEM_SUBCLASS_WEAPON_WAND: return 5009;
default: return 0;
}
case ITEM_CLASS_ARMOR:
switch (proto->SubClass)
{
case ITEM_SUBCLASS_ARMOR_CLOTH: return 9078;
case ITEM_SUBCLASS_ARMOR_LEATHER: return 9077;
case ITEM_SUBCLASS_ARMOR_MAIL: return 8737;
case ITEM_SUBCLASS_ARMOR_PLATE: return 750;
case ITEM_SUBCLASS_ARMOR_SHIELD: return 9116;
default: return 0;
}
}
return 0;
}
int32 Item::GenerateItemRandomPropertyId(uint32 item_id)
{
ItemPrototype const* itemProto = sItemStorage.LookupEntry<ItemPrototype>(item_id);
if (!itemProto)
return 0;
// item must have one from this field values not null if it can have random enchantments
if ((!itemProto->RandomProperty) && (!itemProto->RandomSuffix))
return 0;
// Random Property case
if (itemProto->RandomProperty)
{
uint32 randomPropId = GetItemEnchantMod(itemProto->RandomProperty);
ItemRandomPropertiesEntry const* random_id = sItemRandomPropertiesStore.LookupEntry(randomPropId);
if (!random_id)
{
sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in 'ItemRandomProperties.dbc'", randomPropId);
return 0;
}
return random_id->ID;
}
// Random Suffix case
uint32 randomPropId = GetItemEnchantMod(itemProto->RandomSuffix);
ItemRandomSuffixEntry const* random_id = sItemRandomSuffixStore.LookupEntry(randomPropId);
if (!random_id)
{
sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in sItemRandomSuffixStore.", randomPropId);
return 0;
}
return -int32(random_id->ID);
}
void Item::SetItemRandomProperties(int32 randomPropId)
{
if (!randomPropId)
return;
if (randomPropId > 0)
{
ItemRandomPropertiesEntry const* item_rand = sItemRandomPropertiesStore.LookupEntry(randomPropId);
if (item_rand)
{
if (GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != int32(item_rand->ID))
{
SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID, item_rand->ID);
SetState(ITEM_CHANGED);
}
for (uint32 i = PROP_ENCHANTMENT_SLOT_2; i < PROP_ENCHANTMENT_SLOT_2 + 3; ++i)
SetEnchantment(EnchantmentSlot(i), item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_2], 0, 0);
}
}
else
{
ItemRandomSuffixEntry const* item_rand = sItemRandomSuffixStore.LookupEntry(-randomPropId);
if (item_rand)
{
if (GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != -int32(item_rand->ID) ||
!GetItemSuffixFactor())
{
SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID, -int32(item_rand->ID));
UpdateItemSuffixFactor();
SetState(ITEM_CHANGED);
}
for (uint32 i = PROP_ENCHANTMENT_SLOT_0; i < PROP_ENCHANTMENT_SLOT_0 + 3; ++i)
SetEnchantment(EnchantmentSlot(i), item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_0], 0, 0);
}
}
}
void Item::UpdateItemSuffixFactor()
{
uint32 suffixFactor = GenerateEnchSuffixFactor(GetEntry());
if (GetItemSuffixFactor() == suffixFactor)
return;
SetUInt32Value(ITEM_FIELD_PROPERTY_SEED, suffixFactor);
return;
}
void Item::SetState(ItemUpdateState state, Player* forplayer)
{
if (uState == ITEM_NEW && state == ITEM_REMOVED)
{
// pretend the item never existed
if (forplayer || GetOwnerGuid())
RemoveFromUpdateQueueOf(forplayer);
delete this;
return;
}
if (state != ITEM_UNCHANGED)
{
// new items must stay in new state until saved
if (uState != ITEM_NEW) uState = state;
if (forplayer || GetOwnerGuid())
AddToUpdateQueueOf(forplayer);
}
else
{
// unset in queue
// the item must be removed from the queue manually
uQueuePos = -1;
uState = ITEM_UNCHANGED;
}
}
void Item::AddToUpdateQueueOf(Player* player)
{
if (IsInUpdateQueue())
return;
if (!player)
{
player = GetOwner();
if (!player)
{
sLog.outError("Item::AddToUpdateQueueOf - %s current owner (%s) not in world!",
GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str());
return;
}
}
if (player->GetObjectGuid() != GetOwnerGuid())
{
sLog.outError("Item::AddToUpdateQueueOf - %s current owner (%s) and inventory owner (%s) don't match!",
GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str(), player->GetGuidStr().c_str());
return;
}
if (player->m_itemUpdateQueueBlocked)
return;
player->m_itemUpdateQueue.push_back(this);
uQueuePos = player->m_itemUpdateQueue.size() - 1;
}
void Item::RemoveFromUpdateQueueOf(Player* player)
{
if (!IsInUpdateQueue())
return;
if (!player)
{
player = GetOwner();
if (!player)
{
sLog.outError("Item::RemoveFromUpdateQueueOf - %s current owner (%s) not in world!",
GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str());
return;
}
}
if (player->GetObjectGuid() != GetOwnerGuid())
{
sLog.outError("Item::RemoveFromUpdateQueueOf - %s current owner (%s) and inventory owner (%s) don't match!",
GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str(), player->GetGuidStr().c_str());
return;
}
if (player->m_itemUpdateQueueBlocked)
return;
player->m_itemUpdateQueue[uQueuePos] = nullptr;
uQueuePos = -1;
}
uint8 Item::GetBagSlot() const
{
return m_container ? m_container->GetSlot() : uint8(INVENTORY_SLOT_BAG_0);
}
bool Item::IsEquipped() const
{
return !IsInBag() && m_slot < EQUIPMENT_SLOT_END;
}
bool Item::CanBeTraded(bool mail) const
{
if ((!mail || !IsBoundAccountWide()) && IsSoulBound())
return false;
if (IsBag() && (Player::IsBagPos(GetPos()) || !((Bag const*)this)->IsEmpty()))
return false;
if (Player* owner = GetOwner())
{
if (owner->CanUnequipItem(GetPos(), false) != EQUIP_ERR_OK)
return false;
if (owner->GetLootGuid() == GetObjectGuid())
return false;
}
if (HasGeneratedLoot())
return false;
if (IsBoundByEnchant())
return false;
return true;
}
bool Item::IsBoundByEnchant() const
{
// Check all enchants for soulbound
for (uint32 enchant_slot = PERM_ENCHANTMENT_SLOT; enchant_slot < MAX_ENCHANTMENT_SLOT; ++enchant_slot)
{
uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
if (!enchant_id)
continue;
SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
if (!enchantEntry)
continue;
if (enchantEntry->flags & ENCHANTMENT_SOULBOUND)
return true;
}
return false;
}
bool Item::IsMainHandOnlyEnchant(EnchantmentSlot slot) const
{
SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(GetEnchantmentId(slot));
if (enchantEntry && enchantEntry->flags & ENCHANTMENT_MAINHAND_ONLY)
return true;
return false;
}
bool Item::CanEnterArenaEnchant(EnchantmentSlot slot) const
{
SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(GetEnchantmentId(slot));
if (enchantEntry && enchantEntry->flags & ENCHANTMENT_ALLOW_ENTERING_ARENA)
return true;
return false;
}
bool Item::IsFitToSpellRequirements(SpellEntry const* spellInfo) const
{
ItemPrototype const* proto = GetProto();
// Enchant spells only use Effect[0] (patch 3.3.2)
if (proto->IsVellum() && spellInfo->Effect[EFFECT_INDEX_0] == SPELL_EFFECT_ENCHANT_ITEM)
{
// EffectItemType[0] is the associated scroll itemID, if a scroll can be made
if (spellInfo->EffectItemType[EFFECT_INDEX_0] == 0)
return false;
// Other checks do not apply to vellum enchants, so return final result
return ((proto->SubClass == ITEM_SUBCLASS_WEAPON_ENCHANTMENT && spellInfo->EquippedItemClass == ITEM_CLASS_WEAPON) ||
(proto->SubClass == ITEM_SUBCLASS_ARMOR_ENCHANTMENT && spellInfo->EquippedItemClass == ITEM_CLASS_ARMOR));
}
if (spellInfo->EquippedItemClass != -1) // -1 == any item class
{
if (spellInfo->EquippedItemClass != int32(proto->Class))
return false; // wrong item class
if (spellInfo->EquippedItemSubClassMask != 0) // 0 == any subclass
{
if ((spellInfo->EquippedItemSubClassMask & (1 << proto->SubClass)) == 0)
return false; // subclass not present in mask
}
}
// Only check for item enchantments (TARGET_FLAG_ITEM), all other spells are either NPC spells
// or spells where slot requirements are already handled with AttributesEx3 fields
// and special code (Titan's Grip, Windfury Attack). Check clearly not applicable for Lava Lash.
if (spellInfo->EquippedItemInventoryTypeMask != 0 && (spellInfo->Targets & TARGET_FLAG_ITEM)) // 0 == any inventory type
{
if ((spellInfo->EquippedItemInventoryTypeMask & (1 << proto->InventoryType)) == 0)
return false; // inventory type not present in mask
}
return true;
}
bool Item::IsTargetValidForItemUse(Unit* pUnitTarget) const
{
ItemRequiredTargetMapBounds bounds = sObjectMgr.GetItemRequiredTargetMapBounds(GetProto()->ItemId);
if (bounds.first == bounds.second)
return true;
if (!pUnitTarget)
return false;
for (ItemRequiredTargetMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
if (itr->second.IsFitToRequirements(pUnitTarget))
return true;
return false;
}