-
Notifications
You must be signed in to change notification settings - Fork 89
/
LootMgr.cpp
2861 lines (2488 loc) · 107 KB
/
LootMgr.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 "LootMgr.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "ProgressBar.h"
#include "World.h"
#include "Util.h"
#include "SharedDefines.h"
#include "SpellMgr.h"
#include "DBCStores.h"
#include "SQLStorages.h"
#include "BattleGround/BattleGroundAV.h"
INSTANTIATE_SINGLETON_1(LootMgr);
static eConfigFloatValues const qualityToRate[MAX_ITEM_QUALITY] =
{
CONFIG_FLOAT_RATE_DROP_ITEM_POOR, // ITEM_QUALITY_POOR
CONFIG_FLOAT_RATE_DROP_ITEM_NORMAL, // ITEM_QUALITY_NORMAL
CONFIG_FLOAT_RATE_DROP_ITEM_UNCOMMON, // ITEM_QUALITY_UNCOMMON
CONFIG_FLOAT_RATE_DROP_ITEM_RARE, // ITEM_QUALITY_RARE
CONFIG_FLOAT_RATE_DROP_ITEM_EPIC, // ITEM_QUALITY_EPIC
CONFIG_FLOAT_RATE_DROP_ITEM_LEGENDARY, // ITEM_QUALITY_LEGENDARY
CONFIG_FLOAT_RATE_DROP_ITEM_ARTIFACT, // ITEM_QUALITY_ARTIFACT
};
LootStore LootTemplates_Creature("creature_loot_template", "creature entry", true);
LootStore LootTemplates_Disenchant("disenchant_loot_template", "item disenchant id", true);
LootStore LootTemplates_Fishing("fishing_loot_template", "area id", true);
LootStore LootTemplates_Gameobject("gameobject_loot_template", "gameobject lootid", true);
LootStore LootTemplates_Item("item_loot_template", "item entry with ITEM_FLAG_LOOTABLE", true);
LootStore LootTemplates_Mail("mail_loot_template", "mail template id", false);
LootStore LootTemplates_Milling("milling_loot_template", "item entry (herb)", true);
LootStore LootTemplates_Pickpocketing("pickpocketing_loot_template", "creature pickpocket lootid", true);
LootStore LootTemplates_Prospecting("prospecting_loot_template", "item entry (ore)", true);
LootStore LootTemplates_Reference("reference_loot_template", "reference id", false);
LootStore LootTemplates_Skinning("skinning_loot_template", "creature skinning id", true);
LootStore LootTemplates_Spell("spell_loot_template", "spell id (random item creating)", false);
class LootTemplate::LootGroup // A set of loot definitions for items (refs are not allowed)
{
public:
void AddEntry(LootStoreItem& item); // Adds an entry to the group (at loading stage)
bool HasQuestDrop() const; // True if group includes at least 1 quest drop entry
bool HasQuestDropForPlayer(Player const* player) const;
// The same for active quests of the player
void Process(Loot& loot) const; // Rolls an item from the group (if any) and adds the item to the loot
float RawTotalChance() const; // Overall chance for the group (without equal chanced items)
float TotalChance() const; // Overall chance for the group
void Verify(LootStore const& lootstore, uint32 id, uint32 group_id) const;
void CollectLootIds(LootIdSet& set) const;
void CheckLootRefs(LootIdSet* ref_set) const;
private:
LootStoreItemList ExplicitlyChanced; // Entries with chances defined in DB
LootStoreItemList EqualChanced; // Zero chances - every entry takes the same chance
LootStoreItem const* Roll() const; // Rolls an item from the group, returns nullptr if all miss their chances
};
// Remove all data and free all memory
void LootStore::Clear()
{
for (LootTemplateMap::const_iterator itr = m_LootTemplates.begin(); itr != m_LootTemplates.end(); ++itr)
delete itr->second;
m_LootTemplates.clear();
}
// Checks validity of the loot store
// Actual checks are done within LootTemplate::Verify() which is called for every template
void LootStore::Verify() const
{
for (LootTemplateMap::const_iterator i = m_LootTemplates.begin(); i != m_LootTemplates.end(); ++i)
i->second->Verify(*this, i->first);
}
// Loads a *_loot_template DB table into loot store
// All checks of the loaded template are called from here, no error reports at loot generation required
void LootStore::LoadLootTable()
{
LootTemplateMap::const_iterator tab;
uint32 count = 0;
// Clearing store (for reloading case)
Clear();
// 0 1 2 3 4 5 6
QueryResult* result = WorldDatabase.PQuery("SELECT entry, item, ChanceOrQuestChance, groupid, mincountOrRef, maxcount, condition_id FROM %s", GetName());
if (result)
{
BarGoLink bar(result->GetRowCount());
do
{
Field* fields = result->Fetch();
bar.step();
uint32 entry = fields[0].GetUInt32();
uint32 item = abs(fields[1].GetInt32());
uint8 type = fields[1].GetInt32() < 0 ? LOOTITEM_TYPE_CURRENCY : fields[1].GetInt32();
float chanceOrQuestChance = fields[2].GetFloat();
uint8 group = fields[3].GetUInt8();
int32 mincountOrRef = fields[4].GetInt32();
uint32 maxcount = fields[5].GetUInt32();
uint16 conditionId = fields[6].GetUInt16();
if (type != LOOTITEM_TYPE_CURRENCY && maxcount > std::numeric_limits<uint8>::max())
{
sLog.outErrorDb("Table '%s' entry %d item %d: maxcount value (%u) too large. must be less than %u - skipped", GetName(), entry, item, maxcount, std::numeric_limits<uint8>::max());
continue; // error already printed to log/console.
}
if (conditionId)
{
const PlayerCondition* condition = sConditionStorage.LookupEntry<PlayerCondition>(conditionId);
if (!condition)
{
sLog.outErrorDb("Table `%s` for entry %u, item %u has condition_id %u that does not exist in `conditions`, ignoring", GetName(), entry, item, conditionId);
continue;
}
if (mincountOrRef < 0 && !PlayerCondition::CanBeUsedWithoutPlayer(conditionId))
{
sLog.outErrorDb("Table '%s' entry %u mincountOrRef %i < 0 and has condition %u that requires a player and is not supported, skipped", GetName(), entry, mincountOrRef, conditionId);
continue;
}
}
LootStoreItem storeitem = LootStoreItem(item, type, chanceOrQuestChance, group, conditionId, mincountOrRef, maxcount, type == LOOTITEM_TYPE_CURRENCY);
if (!storeitem.IsValid(*this, entry)) // Validity checks
continue;
// Looking for the template of the entry
// often entries are put together
if (m_LootTemplates.empty() || tab->first != entry)
{
// Searching the template (in case template Id changed)
tab = m_LootTemplates.find(entry);
if (tab == m_LootTemplates.end())
{
std::pair< LootTemplateMap::iterator, bool > pr = m_LootTemplates.insert(LootTemplateMap::value_type(entry, new LootTemplate));
tab = pr.first;
}
}
// else is empty - template Id and iter are the same
// finally iter refers to already existing or just created <entry, LootTemplate>
// Adds current row to the template
tab->second->AddEntry(storeitem);
++count;
}
while (result->NextRow());
delete result;
Verify(); // Checks validity of the loot store
sLog.outString(">> Loaded %u loot definitions (" SIZEFMTD " templates) from table %s", count, m_LootTemplates.size(), GetName());
sLog.outString();
}
else
{
sLog.outString();
sLog.outErrorDb(">> Loaded 0 loot definitions. DB table `%s` is empty.", GetName());
}
}
bool LootStore::HaveQuestLootFor(uint32 loot_id) const
{
LootTemplateMap::const_iterator itr = m_LootTemplates.find(loot_id);
if (itr == m_LootTemplates.end())
return false;
// scan loot for quest items
return itr->second->HasQuestDrop(m_LootTemplates);
}
bool LootStore::HaveQuestLootForPlayer(uint32 loot_id, Player* player) const
{
LootTemplateMap::const_iterator tab = m_LootTemplates.find(loot_id);
if (tab != m_LootTemplates.end())
if (tab->second->HasQuestDropForPlayer(m_LootTemplates, player))
return true;
return false;
}
LootTemplate const* LootStore::GetLootFor(uint32 loot_id) const
{
LootTemplateMap::const_iterator tab = m_LootTemplates.find(loot_id);
if (tab == m_LootTemplates.end())
return nullptr;
return tab->second;
}
void LootStore::LoadAndCollectLootIds(LootIdSet& ids_set)
{
LoadLootTable();
for (LootTemplateMap::const_iterator tab = m_LootTemplates.begin(); tab != m_LootTemplates.end(); ++tab)
ids_set.insert(tab->first);
}
void LootStore::CheckLootRefs(LootIdSet* ref_set) const
{
for (LootTemplateMap::const_iterator ltItr = m_LootTemplates.begin(); ltItr != m_LootTemplates.end(); ++ltItr)
ltItr->second->CheckLootRefs(ref_set);
}
void LootStore::ReportUnusedIds(LootIdSet const& ids_set) const
{
// all still listed ids isn't referenced
if (!ids_set.empty())
{
for (LootIdSet::const_iterator itr = ids_set.begin(); itr != ids_set.end(); ++itr)
sLog.outErrorDb("Table '%s' entry %d isn't %s and not referenced from loot, and then useless.", GetName(), *itr, GetEntryName());
sLog.outString();
}
}
void LootStore::ReportNotExistedId(uint32 id) const
{
sLog.outErrorDb("Table '%s' entry %d (%s) not exist but used as loot id in DB.", GetName(), id, GetEntryName());
}
//
// --------- LootStoreItem ---------
//
// Checks if the entry (quest, non-quest, reference) takes it's chance (at loot generation)
// RATE_DROP_ITEMS is no longer used for all types of entries
bool LootStoreItem::Roll(bool rate) const
{
if (chance >= 100.0f)
return true;
if (mincountOrRef < 0) // reference case
return roll_chance_f(chance * (rate ? sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_ITEM_REFERENCED) : 1.0f));
if (type == LOOTITEM_TYPE_CURRENCY)
return roll_chance_f(chance * (rate ? sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_CURRENCY) : 1.0f));
else
{
ItemPrototype const* pProto = ObjectMgr::GetItemPrototype(itemid);
float qualityModifier = pProto && rate ? sWorld.getConfig(qualityToRate[pProto->Quality]) : 1.0f;
return roll_chance_f(chance * qualityModifier);
}
return false;
}
// Checks correctness of values
bool LootStoreItem::IsValid(LootStore const& store, uint32 entry) const
{
if (group >= 1 << 7) // it stored in 7 bit field
{
sLog.outErrorDb("Table '%s' entry %d item %d: group (%u) must be less %u - skipped", store.GetName(), entry, itemid, group, 1 << 7);
return false;
}
if (group && type == LOOTITEM_TYPE_CURRENCY)
{
sLog.outErrorDb("Table '%s' entry %d currency %d: group is set, but currencies must not have group - skipped", store.GetName(), entry, itemid, group, 1 << 7);
return false;
}
if (mincountOrRef == 0)
{
sLog.outErrorDb("Table '%s' entry %d item %d: wrong mincountOrRef (%d) - skipped", store.GetName(), entry, itemid, mincountOrRef);
return false;
}
if (mincountOrRef > 0) // item (quest or non-quest) entry, maybe grouped
{
if (type == LOOTITEM_TYPE_CURRENCY)
{
CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(itemid);
if (!currency)
{
sLog.outErrorDb("Table '%s' entry %d: currency entry %u not exists - skipped", store.GetName(), entry, itemid);
return false;
}
}
else
{
ItemPrototype const* proto = ObjectMgr::GetItemPrototype(itemid);
if (!proto)
{
sLog.outErrorDb("Table '%s' entry %d item %d: item entry not listed in `item_template` - skipped", store.GetName(), entry, itemid);
return false;
}
}
if (chance == 0 && group == 0) // Zero chance is allowed for grouped entries only
{
sLog.outErrorDb("Table '%s' entry %d item %d: equal-chanced grouped entry, but group not defined - skipped", store.GetName(), entry, itemid);
return false;
}
if (chance != 0 && chance < 0.000001f) // loot with low chance
{
sLog.outErrorDb("Table '%s' entry %d item %d: low chance (%f) - skipped", store.GetName(), entry, itemid, chance);
return false;
}
if (maxcount < (uint32)mincountOrRef) // wrong max count
{
sLog.outErrorDb("Table '%s' entry %d item %d: max count (%u) less that min count (%i) - skipped", store.GetName(), entry, itemid, uint32(maxcount), mincountOrRef);
return false;
}
}
else // mincountOrRef < 0
{
if (needs_quest)
{
sLog.outErrorDb("Table '%s' entry %d item %d: negative chance is given for a reference, skipped", store.GetName(), entry, itemid);
return false;
}
else if (chance == 0) // no chance for the reference
{
sLog.outErrorDb("Table '%s' entry %d item %d: zero chance is given for a reference, reference will never be used, skipped", store.GetName(), entry, itemid);
return false;
}
}
return true; // Referenced template existence is checked at whole store level
}
//
// --------- LootItem ---------
//
// Constructor, copies most fields from LootStoreItem and generates random count
LootItem::LootItem(LootStoreItem const& li, uint32 _lootSlot, uint32 threshold)
{
if (li.needs_quest)
lootItemType = LOOTITEM_TYPE_QUEST;
else if (li.conditionId)
lootItemType = LOOTITEM_TYPE_CONDITIONNAL;
else if (li.currency)
lootItemType = LOOTITEM_TYPE_CURRENCY;
else
lootItemType = LOOTITEM_TYPE_NORMAL;
itemId = li.itemid;
conditionId = li.conditionId;
lootSlot = _lootSlot;
count = urand(li.mincountOrRef, li.maxcount); // constructor called for mincountOrRef > 0 only
isBlocked = false;
currentLooterPass = false;
if (lootItemType == LOOTITEM_TYPE_CURRENCY)
{
freeForAll = false;
isUnderThreshold = false;
randomSuffix = 0;
randomPropertyId = 0;
count = uint32(count * sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_CURRENCY_AMOUNT));
}
else
{
itemProto = ObjectMgr::GetItemPrototype(li.itemid);
if (itemProto)
{
freeForAll = (itemProto->Flags & ITEM_FLAG_PARTY_LOOT);
displayID = itemProto->DisplayInfoID;
isUnderThreshold = itemProto->Quality < threshold;
}
else
{
sLog.outError("LootItem::LootItem()> item ID(%u) have no prototype!", li.itemid); // maybe i must make an assert here
freeForAll = false;
displayID = 0;
isUnderThreshold = false;
}
randomSuffix = GenerateEnchSuffixFactor(itemId);
randomPropertyId = Item::GenerateItemRandomPropertyId(itemId);
}
}
LootItem::LootItem(uint32 _itemId, uint32 _count, uint32 _randomSuffix, int32 _randomPropertyId, uint32 _lootSlot)
{
itemId = _itemId;
lootSlot = _lootSlot;
conditionId = 0;
lootItemType = LOOTITEM_TYPE_NORMAL;
count = _count;
randomSuffix = _randomSuffix;
randomPropertyId = _randomPropertyId;
isBlocked = false;
isUnderThreshold = false;
currentLooterPass = false;
if (lootItemType == LOOTITEM_TYPE_CURRENCY)
freeForAll = false;
else
{
itemProto = ObjectMgr::GetItemPrototype(_itemId);
if (itemProto)
{
freeForAll = (itemProto->Flags & ITEM_FLAG_PARTY_LOOT);
displayID = itemProto->DisplayInfoID;
}
else
{
sLog.outError("LootItem::LootItem()> item ID(%u) have no prototype!", _itemId); // maybe i must make an assert here
freeForAll = false;
displayID = 0;
}
}
}
// Basic checks for player/item compatibility - if false no chance to see the item in the loot
bool LootItem::AllowedForPlayer(Player const* player, WorldObject const* lootTarget) const
{
if (!itemProto)
return false;
// not show loot for not own team
if ((itemProto->Flags2 & ITEM_FLAG2_HORDE_ONLY) && player->GetTeam() != HORDE)
return false;
if ((itemProto->Flags2 & ITEM_FLAG2_ALLIANCE_ONLY) && player->GetTeam() != ALLIANCE)
return false;
switch (lootItemType)
{
case LOOTITEM_TYPE_NORMAL:
break;
case LOOTITEM_TYPE_CONDITIONNAL:
// DB conditions check
if (!sObjectMgr.IsPlayerMeetToCondition(conditionId, player, player->GetMap(), lootTarget, CONDITION_FROM_LOOT))
return false;
break;
case LOOTITEM_TYPE_QUEST:
// Checking quests for quest-only drop (check only quests requirements in this case)
if (!player->HasQuestForItem(itemId))
return false;
break;
case LOOTITEM_TYPE_CURRENCY:
{
CurrencyTypesEntry const * currency = sCurrencyTypesStore.LookupEntry(itemId);
if (!itemId)
return false;
if (!player->isGameMaster())
{
if (currency->Category == CURRENCY_CATEGORY_META)
return false;
if (currency->Category == CURRENCY_CATEGORY_ARCHAEOLOGY && !player->HasSkill(SKILL_ARCHAEOLOGY))
return false;
}
break;
}
default:
break;
}
// Not quest only drop (check quest starting items for already accepted non-repeatable quests)
if (itemProto->StartQuest && player->GetQuestStatus(itemProto->StartQuest) != QUEST_STATUS_NONE && !player->HasQuestForItem(itemId))
return false;
return true;
}
LootSlotType LootItem::GetSlotTypeForSharedLoot(Player const* player, Loot const* loot) const
{
// ignore looted, FFA (each player get own copy) and not allowed items
if (IsLootedFor(player->GetObjectGuid()) || !AllowedForPlayer(player, loot->GetLootTarget()))
return MAX_LOOT_SLOT_TYPE;
if (freeForAll)
return loot->m_lootMethod == NOT_GROUP_TYPE_LOOT ? LOOT_SLOT_OWNER : LOOT_SLOT_NORMAL; // player have not yet looted a free for all item
if (!lootedBy.empty())
return MAX_LOOT_SLOT_TYPE; // a not free for all item should not be looted more than once
if (lootItemType == LOOTITEM_TYPE_QUEST)
{
if (loot->m_lootMethod == NOT_GROUP_TYPE_LOOT || loot->m_lootMethod == FREE_FOR_ALL)
return LOOT_SLOT_OWNER;
else
{
// Check if its turn of that player to loot a not party loot. The loot may be released or the item may be passed by currentLooter
if (loot->m_isReleased || currentLooterPass || loot->m_currentLooterGuid == player->GetObjectGuid())
return LOOT_SLOT_OWNER;
return MAX_LOOT_SLOT_TYPE;
}
}
switch (loot->m_lootMethod)
{
case FREE_FOR_ALL:
return LOOT_SLOT_NORMAL;
case GROUP_LOOT:
case NEED_BEFORE_GREED:
{
if (!isBlocked)
{
if (loot->m_isReleased || player->GetObjectGuid() == loot->m_currentLooterGuid)
return LOOT_SLOT_NORMAL;
else
return MAX_LOOT_SLOT_TYPE;
}
return LOOT_SLOT_VIEW;
}
case MASTER_LOOT:
{
if (isUnderThreshold)
{
if (loot->m_isReleased || player->GetObjectGuid() == loot->m_currentLooterGuid)
return LOOT_SLOT_NORMAL;
return MAX_LOOT_SLOT_TYPE;
}
else
{
if (player->GetObjectGuid() == loot->m_masterOwnerGuid)
return LOOT_SLOT_MASTER;
return LOOT_SLOT_VIEW;
}
}
case ROUND_ROBIN:
{
if (loot->m_isReleased || player->GetObjectGuid() == loot->m_currentLooterGuid)
return LOOT_SLOT_OWNER;
return MAX_LOOT_SLOT_TYPE;
}
case NOT_GROUP_TYPE_LOOT:
return LOOT_SLOT_OWNER;
default:
return MAX_LOOT_SLOT_TYPE;
}
}
//
// ------- Loot Roll -------
//
// Send the roll for the whole group
void GroupLootRoll::SendStartRoll()
{
WorldPacket data(SMSG_LOOT_START_ROLL, (8 + 4 + 4 + 4 + 4 + 4 + 4 + 1));
data << m_loot->GetLootGuid(); // creature guid what we're looting
data << uint32(m_loot->GetLootTarget()->GetMapId()); // 3.3.3 mapid
data << uint32(m_itemSlot); // item slot in loot
data << uint32(m_lootItem->itemId); // the itemEntryId for the item that shall be rolled for
data << uint32(m_lootItem->randomSuffix); // randomSuffix
data << uint32(m_lootItem->randomPropertyId); // item random property ID
data << uint32(m_lootItem->count); // items in stack
data << uint32(LOOT_ROLL_TIMEOUT); // the countdown time to choose "need" or "greed"
size_t voteMaskPos = data.wpos();
data << uint8(0); // roll type mask, allowed choices (placeholder)
data << uint8(m_loot->ownerSet.size());
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
if (itr->second.vote == ROLL_NOT_VALID)
continue;
Player* plr = sObjectMgr.GetPlayer(itr->first);
if (!plr || !plr->GetSession())
continue;
// dependent from player
RollVoteMask mask = m_voteMask;
// In NEED_BEFORE_GREED need disabled for non-usable item for player
if (m_loot->m_lootMethod == NEED_BEFORE_GREED && plr->CanUseItem(m_lootItem->itemProto) != EQUIP_ERR_OK)
mask = RollVoteMask(mask & ~ROLL_VOTE_MASK_NEED);
data.put<uint8>(voteMaskPos, uint8(mask));
plr->GetSession()->SendPacket(&data);
}
}
// Send all passed message
void GroupLootRoll::SendAllPassed()
{
WorldPacket data(SMSG_LOOT_ALL_PASSED, (8 + 4 + 4 + 4 + 4));
data << m_loot->GetLootGuid(); // creature guid what we're looting
data << uint32(m_itemSlot); // item slot in loot
data << uint32(m_lootItem->itemId); // the itemEntryId for the item that shall be rolled for
data << uint32(m_lootItem->randomSuffix); // randomSuffix
data << uint32(m_lootItem->randomPropertyId); // item random property ID
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
if (itr->second.vote == ROLL_NOT_VALID)
continue;
Player* plr = sObjectMgr.GetPlayer(itr->first);
if (!plr || !plr->GetSession())
continue;
plr->GetSession()->SendPacket(&data);
}
}
// Send roll 'value' of the whole group and the winner to the whole group
void GroupLootRoll::SendLootRollWon(ObjectGuid const& targetGuid, uint32 rollNumber, RollVote rollType)
{
WorldPacket data(SMSG_LOOT_ROLL_WON, (8 + 4 + 4 + 4 + 4 + 8 + 1 + 1));
data << m_loot->GetLootGuid(); // creature guid what we're looting
data << uint32(m_itemSlot); // item slot in loot
data << uint32(m_lootItem->itemId); // the itemEntryId for the item that shall be rolled for
data << uint32(m_lootItem->randomSuffix); // randomSuffix
data << uint32(m_lootItem->randomPropertyId); // item random property ID
data << targetGuid; // guid of the player who won.
data << uint8(rollNumber); // rollnumber related to SMSG_LOOT_ROLL
data << uint8(rollType); // Rolltype related to SMSG_LOOT_ROLL
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
switch (itr->second.vote)
{
case ROLL_PASS:
break;
case ROLL_NOT_EMITED_YET:
case ROLL_NOT_VALID:
SendRoll(itr->first, 128, 128);
break;
default:
SendRoll(itr->first, itr->second.number, itr->second.vote);
break;
}
}
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
if (itr->second.vote == ROLL_NOT_VALID)
continue;
Player* plr = sObjectMgr.GetPlayer(itr->first);
if (!plr || !plr->GetSession())
continue;
plr->GetSession()->SendPacket(&data);
}
}
// Send roll of targetGuid to the whole group (included targuetGuid)
void GroupLootRoll::SendRoll(ObjectGuid const& targetGuid, uint32 rollNumber, uint32 rollType)
{
WorldPacket data(SMSG_LOOT_ROLL, (8 + 4 + 8 + 4 + 4 + 4 + 1 + 1 + 1));
data << m_loot->GetLootGuid(); // creature guid what we're looting
data << uint32(m_itemSlot); // item slot in loot
data << targetGuid;
data << uint32(m_lootItem->itemId); // the itemEntryId for the item that shall be rolled for
data << uint32(m_lootItem->randomSuffix); // randomSuffix
data << uint32(m_lootItem->randomPropertyId); // item random property ID
data << uint8(rollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
data << uint8(rollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
data << uint8(0); // auto pass on loot
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
if (itr->second.vote == ROLL_NOT_VALID)
continue;
Player* plr = sObjectMgr.GetPlayer(itr->first);
if (!plr || !plr->GetSession())
continue;
plr->GetSession()->SendPacket(&data);
}
}
GroupLootRoll::~GroupLootRoll()
{
if (m_isStarted)
SendAllPassed();
}
// Try to start the group roll for the specified item (it may fail for quest item or any condition
// If this method return false the roll have to be removed from the container to avoid any problem
bool GroupLootRoll::TryToStart(Loot& loot, uint32 itemSlot)
{
if (!m_isStarted)
{
if (itemSlot >= loot.m_lootItems.size())
return false;
// initialize the data needed for the roll
m_lootItem = loot.GetLootItemInSlot(itemSlot);
if (m_lootItem->lootItemType == LOOTITEM_TYPE_QUEST)
return false;
m_loot = &loot;
m_itemSlot = itemSlot;
m_lootItem->isBlocked = true; // block the item while rolling
uint32 playerCount = 0;
for (GuidSet::const_iterator itr = m_loot->m_ownerSet.begin(); itr != m_loot->m_ownerSet.end(); ++itr)
{
Player* plr = sObjectMgr.GetPlayer(*itr);
if (!plr || !m_lootItem->AllowedForPlayer(plr, loot.GetLootTarget())) // check if player meet the condition to be able to roll this item
{
m_rollVoteMap[*itr].vote = ROLL_NOT_VALID;
continue;
}
m_rollVoteMap[*itr].vote = ROLL_NOT_EMITED_YET; // initialize player vote map
++playerCount;
}
// initialize item prototype and check enchant possibilities for this group
m_voteMask = ROLL_VOTE_MASK_ALL;
if (m_lootItem->itemProto->Flags2 & ITEM_FLAG2_NEED_ROLL_DISABLED)
m_voteMask = RollVoteMask(m_voteMask & ~ROLL_VOTE_MASK_NEED);
if (!m_lootItem->itemProto->DisenchantID || uint32(m_lootItem->itemProto->RequiredDisenchantSkill) > m_loot->m_maxEnchantSkill)
m_voteMask = RollVoteMask(m_voteMask & ~ROLL_VOTE_MASK_DISENCHANT);
if (playerCount > 1) // check if more than one player can loot this item
{
// start the roll
SendStartRoll();
m_endTime = time(NULL) + (LOOT_ROLL_TIMEOUT / 1000);
m_isStarted = true;
return true;
}
// no need to start roll if one or less player can loot this item so place it under threshold
m_lootItem->isUnderThreshold = true;
m_lootItem->isBlocked = false;
}
return false;
}
// Add vote from playerGuid
bool GroupLootRoll::PlayerVote(Player* player, RollVote vote)
{
ObjectGuid const& playerGuid = player->GetObjectGuid();
RollVoteMap::iterator voterItr = m_rollVoteMap.find(playerGuid);
if (voterItr == m_rollVoteMap.end())
return false;
voterItr->second.vote = vote;
if (vote != ROLL_PASS && vote != ROLL_NOT_VALID)
voterItr->second.number = urand(1, 100);
switch (vote)
{
case ROLL_PASS: // Player choose pass
{
SendRoll(playerGuid, 128, 128);
break;
}
case ROLL_NEED: // player choose Need
{
SendRoll(playerGuid, 0, 0);
player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED, 1);
break;
}
case ROLL_GREED: // player choose Greed
{
SendRoll(playerGuid, 128, 2);
player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED, 1);
break;
}
case ROLL_DISENCHANT: // player choose Disenchant
{
SendRoll(playerGuid, 128, 3);
player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED, 1);
break;
}
default: // Roll removed case
return false;
break;
}
return true;
}
// check if we can found a winner for this roll or if timer is expired
bool GroupLootRoll::UpdateRoll()
{
RollVoteMap::const_iterator winnerItr = m_rollVoteMap.end();
if (AllPlayerVoted(winnerItr) || m_endTime <= time(nullptr))
{
Finish(winnerItr);
return true;
}
return false;
}
/**
* \brief: Check if all player have voted and return true in that case. Also return current winner.
* \param: RollVoteMap::const_iterator& winnerItr > will be different than m_rollCoteMap.end() if winner exist. (Someone voted greed or need)
* \returns: bool > true if all players voted
**/
bool GroupLootRoll::AllPlayerVoted(RollVoteMap::const_iterator& winnerItr)
{
uint32 notVoted = 0;
bool isSomeoneNeed = false;
winnerItr = m_rollVoteMap.end();
for (RollVoteMap::const_iterator itr = m_rollVoteMap.begin(); itr != m_rollVoteMap.end(); ++itr)
{
switch (itr->second.vote)
{
case ROLL_NEED:
if (!isSomeoneNeed || winnerItr == m_rollVoteMap.end() || itr->second.number > winnerItr->second.number)
{
isSomeoneNeed = true; // first passage will force to set winner because need is prioritized
winnerItr = itr;
}
break;
case ROLL_GREED:
case ROLL_DISENCHANT:
if (!isSomeoneNeed) // if at least one need is detected then winner can't be a greed
{
if (winnerItr == m_rollVoteMap.end() || itr->second.number > winnerItr->second.number)
winnerItr = itr;
}
break;
// Explicitly passing excludes a player from winning loot, so no action required.
case ROLL_PASS:
break;
case ROLL_NOT_EMITED_YET:
++notVoted;
break;
}
}
return notVoted == 0;
}
// terminate the roll
void GroupLootRoll::Finish(RollVoteMap::const_iterator& winnerItr)
{
m_lootItem->isBlocked = false;
if (winnerItr == m_rollVoteMap.end())
{
SendAllPassed();
m_loot->m_isReleased = true;
}
else
{
SendLootRollWon(winnerItr->first, winnerItr->second.number, winnerItr->second.vote);
Player* plr = sObjectMgr.GetPlayer(winnerItr->first);
if (plr && plr->GetSession())
{
if (winnerItr->second.vote == ROLL_NEED)
plr->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, m_lootItem->itemId, winnerItr->second.number);
else
plr->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, m_lootItem->itemId, winnerItr->second.number);
m_loot->SendItem(plr, m_itemSlot);
}
else
{
// hum the winner is not available
m_loot->m_isReleased = true;
}
}
m_isStarted = false;
}
GroupLootRoll* Loot::GetRollForSlot(uint32 itemSlot)
{
GroupLootRollMap::iterator rollItr = m_roll.find(itemSlot);
if (rollItr == m_roll.end())
return nullptr;
return &rollItr->second;
}
//
// --------- Loot ---------
//
// Inserts the item into the loot (called by LootTemplate processors)
void Loot::AddItem(LootStoreItem const& item)
{
if (m_lootItems.size() < MAX_NR_LOOT_ITEMS) // Normal drop
{
LootItem* lootItem = new LootItem(item, m_maxSlot++, uint32(m_threshold));
if (!lootItem->isUnderThreshold) // set flag for later to know that we have an over threshold item
m_haveItemOverThreshold = true;
m_lootItems.push_back(lootItem);
}
}
// Insert item into the loot explicit way. (used for container item and Item::LoadFromDB)
void Loot::AddItem(uint32 itemid, uint32 count, uint32 randomSuffix, int32 randomPropertyId)
{
if (m_lootItems.size() < MAX_NR_LOOT_ITEMS) // Normal drop
{
LootItem* lootItem = new LootItem(itemid, count, randomSuffix, randomPropertyId, m_maxSlot++);
m_lootItems.push_back(lootItem);
}
}
// Calls processor of corresponding LootTemplate (which handles everything including references)
bool Loot::FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner, bool personal, bool noEmptyError)
{
// Must be provided
if (!loot_owner)
return false;
LootTemplate const* tab = store.GetLootFor(loot_id);
if (!tab)
{
if (!noEmptyError)
sLog.outErrorDb("Table '%s' loot id #%u used but it doesn't have records.", store.GetName(), loot_id);
return false;
}
m_lootItems.reserve(MAX_NR_LOOT_ITEMS);
tab->Process(*this, store, store.IsRatesAllowed()); // Processing is done there, callback via Loot::AddItem()
// Must now check if current looter have right to loot all item or he will lockout that item until he look and release the loot
if (!m_currentLooterGuid.IsEmpty() && m_ownerSet.size() > 1 && m_lootMethod != FREE_FOR_ALL) // only for group that are not free for all
{
Player* currentLooter = ObjectAccessor::FindPlayer(m_currentLooterGuid);
for (LootItemList::const_iterator lootItemItr = m_lootItems.begin(); lootItemItr != m_lootItems.end(); ++lootItemItr)
{
LootItem* lootItem = *lootItemItr;
// Normal loot have no condition to check
if (lootItem->lootItemType == LOOTITEM_TYPE_NORMAL || lootItem->lootItemType == LOOTITEM_TYPE_CURRENCY)
continue;
if (!currentLooter || !lootItem->AllowedForPlayer(currentLooter, m_lootTarget))
lootItem->currentLooterPass = true; // Some item may not be allowed for current looter, must set this flag to avoid item not distributed to other player
}
}
return true;
}
// Is there is any loot available for provided player
bool Loot::IsLootedFor(Player const* player) const
{
if (m_gold != 0)
return false;
ObjectGuid const& playerGuid = player->GetObjectGuid();
for (LootItemList::const_iterator lootItemItr = m_lootItems.begin(); lootItemItr != m_lootItems.end(); ++lootItemItr)
{
LootItem* lootItem = *lootItemItr;
if (lootItem->GetSlotTypeForSharedLoot(player, this) != MAX_LOOT_SLOT_TYPE)
return false;
}
return true;
}
bool Loot::IsLootedForAll() const
{
for (GuidSet::const_iterator itr = m_ownerSet.begin(); itr != m_ownerSet.end(); ++itr)
{
Player* player = ObjectAccessor::FindPlayer(*itr);
if (!player)
continue;
if (!IsLootedFor(player))
return false;
}
return true;
}
bool Loot::CanLoot(Player const* player)
{
ObjectGuid const& playerGuid = player->GetObjectGuid();
// not in Guid list of possible owner mean cheat or big problem
GuidSet::const_iterator itr = m_ownerSet.find(playerGuid);
if (itr == m_ownerSet.end())
return false;