/
LootMgr.cpp
1553 lines (1343 loc) · 56.2 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"
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 NULL 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 ? LOOT_ITEM_TYPE_ITEM : LOOT_ITEM_TYPE_CURRENCY;
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 == LOOT_ITEM_TYPE_ITEM && 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);
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 NULL;
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 == LOOT_ITEM_TYPE_ITEM)
{
ItemPrototype const* pProto = ObjectMgr::GetItemPrototype(itemid);
float qualityModifier = pProto && rate ? sWorld.getConfig(qualityToRate[pProto->Quality]) : 1.0f;
return roll_chance_f(chance * qualityModifier);
}
else if (type == LOOT_ITEM_TYPE_CURRENCY)
return roll_chance_f(chance * (rate ? sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_CURRENCY) : 1.0f));
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 == LOOT_ITEM_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 == LOOT_ITEM_TYPE_ITEM)
{
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;
}
}
else if (type == LOOT_ITEM_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
{
sLog.outErrorDb("Table '%s' entry %d: has unknown item %u with type %u - skipped", store.GetName(), entry, itemid, type);
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)
{
itemid = li.itemid;
type = li.type;
conditionId = li.conditionId;
currency = type == LOOT_ITEM_TYPE_CURRENCY;
count = urand(li.mincountOrRef, li.maxcount); // constructor called for mincountOrRef > 0 only
is_looted = 0;
is_blocked = 0;
is_underthreshold = 0;
is_counted = 0;
if (currency)
{
freeforall = false;
needs_quest = false;
randomSuffix = 0;
randomPropertyId = 0;
count = uint32(count * sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_CURRENCY_AMOUNT));
}
else
{
ItemPrototype const* proto = ObjectMgr::GetItemPrototype(itemid);
freeforall = proto && (proto->Flags & ITEM_FLAG_PARTY_LOOT);
needs_quest = li.needs_quest;
randomSuffix = GenerateEnchSuffixFactor(itemid);
randomPropertyId = Item::GenerateItemRandomPropertyId(itemid);
}
}
LootItem::LootItem(uint32 itemid_, uint8 type_, uint32 count_, uint32 randomSuffix_, int32 randomPropertyId_)
{
itemid = itemid_;
type = type_;
conditionId = 0;
count = count_;
randomSuffix = randomSuffix_;
randomPropertyId = randomPropertyId_;
is_looted = 0;
is_blocked = 0;
is_underthreshold = 0;
is_counted = 0;
currency = type == LOOT_ITEM_TYPE_CURRENCY;
needs_quest = false;
if (currency)
freeforall = false;
else
{
ItemPrototype const* proto = ObjectMgr::GetItemPrototype(itemid);
freeforall = proto && (proto->Flags & ITEM_FLAG_PARTY_LOOT);
}
}
// 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
{
// DB conditions check
if (conditionId && !sObjectMgr.IsPlayerMeetToCondition(conditionId, player, player->GetMap(), lootTarget, CONDITION_FROM_LOOT))
return false;
if (type == LOOT_ITEM_TYPE_ITEM)
{
ItemPrototype const* pProto = ObjectMgr::GetItemPrototype(itemid);
if (!pProto)
return false;
// not show loot for not own team
if ((pProto->Flags2 & ITEM_FLAG2_HORDE_ONLY) && player->GetTeam() != HORDE)
return false;
if ((pProto->Flags2 & ITEM_FLAG2_ALLIANCE_ONLY) && player->GetTeam() != ALLIANCE)
return false;
if (needs_quest)
{
// Checking quests for quest-only drop (check only quests requirements in this case)
if (!player->HasQuestForItem(itemid))
return false;
}
else
{
// Not quest only drop (check quest starting items for already accepted non-repeatable quests)
if (pProto->StartQuest && player->GetQuestStatus(pProto->StartQuest) != QUEST_STATUS_NONE && !player->HasQuestForItem(itemid))
return false;
}
}
else if (type == LOOT_ITEM_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;
}
}
return true;
}
LootSlotType LootItem::GetSlotTypeForSharedLoot(PermissionTypes permission, Player* viewer, WorldObject const* lootTarget, bool condition_ok /*= false*/) const
{
// ignore currencies, looted items, FFA (each player get own copy) and not allowed items
if (currency || is_looted || freeforall || (conditionId && !condition_ok) || !AllowedForPlayer(viewer, lootTarget))
return MAX_LOOT_SLOT_TYPE;
switch (permission)
{
case ALL_PERMISSION:
return LOOT_SLOT_NORMAL;
case GROUP_PERMISSION:
return (is_blocked || is_underthreshold) ? LOOT_SLOT_NORMAL : LOOT_SLOT_VIEW;
case MASTER_PERMISSION:
return !is_underthreshold ? LOOT_SLOT_MASTER : LOOT_SLOT_NORMAL;
case OWNER_PERMISSION:
return LOOT_SLOT_OWNER;
default:
return MAX_LOOT_SLOT_TYPE;
}
}
//
// --------- Loot ---------
//
// Inserts the item into the loot (called by LootTemplate processors)
void Loot::AddItem(LootStoreItem const& item)
{
if (item.needs_quest) // Quest drop
{
if (m_questItems.size() < MAX_NR_QUEST_ITEMS)
m_questItems.push_back(LootItem(item));
}
else if (items.size() < MAX_NR_LOOT_ITEMS) // Non-quest drop
{
items.push_back(LootItem(item));
// non-conditional one-player only items are counted here,
// currencies are counter in FillCurrencyLoot,
// free for all items are counted in FillFFALoot(),
// non-ffa conditionals are counted in FillNonQuestNonFFANonCurrencyConditionalLoot()
if (!item.conditionId && item.type == LOOT_ITEM_TYPE_ITEM)
{
ItemPrototype const* proto = ObjectMgr::GetItemPrototype(item.itemid);
if (!proto || !(proto->Flags & ITEM_FLAG_PARTY_LOOT))
++unlootedCount;
}
}
}
// 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;
}
items.reserve(MAX_NR_LOOT_ITEMS);
m_questItems.reserve(MAX_NR_QUEST_ITEMS);
tab->Process(*this, store, store.IsRatesAllowed()); // Processing is done there, callback via Loot::AddItem()
// Setting access rights for group loot case
Group* pGroup = loot_owner->GetGroup();
if (!personal && pGroup)
{
for (GroupReference* itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next())
if (Player* pl = itr->getSource())
FillNotNormalLootFor(pl);
}
// ... for personal loot
else
FillNotNormalLootFor(loot_owner);
return true;
}
void Loot::FillNotNormalLootFor(Player* pl)
{
uint32 plguid = pl->GetGUIDLow();
QuestItemMap::const_iterator qmapitr = m_playerCurrencies.find(plguid);
if (qmapitr == m_playerCurrencies.end())
FillCurrencyLoot(pl);
qmapitr = m_playerQuestItems.find(plguid);
if (qmapitr == m_playerQuestItems.end())
FillQuestLoot(pl);
qmapitr = m_playerFFAItems.find(plguid);
if (qmapitr == m_playerFFAItems.end())
FillFFALoot(pl);
qmapitr = m_playerNonQuestNonFFANonCurrencyConditionalItems.find(plguid);
if (qmapitr == m_playerNonQuestNonFFANonCurrencyConditionalItems.end())
FillNonQuestNonFFANonCurrencyConditionalLoot(pl);
}
QuestItemList* Loot::FillCurrencyLoot(Player* player)
{
QuestItemList* ql = new QuestItemList();
for (uint8 i = 0; i < items.size(); ++i)
{
LootItem& item = items[i];
if (!item.is_looted && item.currency && item.AllowedForPlayer(player, m_lootTarget))
{
ql->push_back(QuestItem(i));
++unlootedCount;
}
}
if (ql->empty())
{
delete ql;
return NULL;
}
m_playerCurrencies[player->GetGUIDLow()] = ql;
return ql;
}
QuestItemList* Loot::FillFFALoot(Player* player)
{
QuestItemList* ql = new QuestItemList();
for (uint8 i = 0; i < items.size(); ++i)
{
LootItem& item = items[i];
if (!item.is_looted && item.freeforall && item.AllowedForPlayer(player, m_lootTarget))
{
ql->push_back(QuestItem(i));
++unlootedCount;
}
}
if (ql->empty())
{
delete ql;
return NULL;
}
m_playerFFAItems[player->GetGUIDLow()] = ql;
return ql;
}
QuestItemList* Loot::FillQuestLoot(Player* player)
{
if (items.size() == MAX_NR_LOOT_ITEMS) return NULL;
QuestItemList* ql = new QuestItemList();
for (uint8 i = 0; i < m_questItems.size(); ++i)
{
LootItem& item = m_questItems[i];
if (!item.is_looted && item.AllowedForPlayer(player, m_lootTarget))
{
ql->push_back(QuestItem(i));
// questitems get blocked when they first apper in a
// player's quest vector
//
// increase once if one looter only, looter-times if free for all
if (item.freeforall || !item.is_blocked)
++unlootedCount;
item.is_blocked = true;
if (items.size() + ql->size() == MAX_NR_LOOT_ITEMS)
break;
}
}
if (ql->empty())
{
delete ql;
return NULL;
}
m_playerQuestItems[player->GetGUIDLow()] = ql;
return ql;
}
QuestItemList* Loot::FillNonQuestNonFFANonCurrencyConditionalLoot(Player* player)
{
QuestItemList* ql = new QuestItemList();
for (uint8 i = 0; i < items.size(); ++i)
{
LootItem& item = items[i];
if (!item.is_looted && !item.freeforall && !item.currency && item.conditionId && item.AllowedForPlayer(player, m_lootTarget))
{
ql->push_back(QuestItem(i));
if (!item.is_counted)
{
++unlootedCount;
item.is_counted = true;
}
}
}
if (ql->empty())
{
delete ql;
return NULL;
}
m_playerNonQuestNonFFANonCurrencyConditionalItems[player->GetGUIDLow()] = ql;
return ql;
}
//===================================================
void Loot::NotifyItemRemoved(uint8 lootIndex)
{
// notify all players that are looting this that the item was removed
// convert the index to the slot the player sees
GuidSet::iterator i_next;
for (GuidSet::iterator i = m_playersLooting.begin(); i != m_playersLooting.end(); i = i_next)
{
i_next = i;
++i_next;
if (Player* pl = ObjectAccessor::FindPlayer(*i))
pl->SendNotifyLootItemRemoved(lootIndex);
else
m_playersLooting.erase(i);
}
}
void Loot::NotifyMoneyRemoved()
{
// notify all players that are looting this that the money was removed
GuidSet::iterator i_next;
for (GuidSet::iterator i = m_playersLooting.begin(); i != m_playersLooting.end(); i = i_next)
{
i_next = i;
++i_next;
if (Player* pl = ObjectAccessor::FindPlayer(*i))
pl->SendNotifyLootMoneyRemoved();
else
m_playersLooting.erase(i);
}
}
void Loot::NotifyQuestItemRemoved(uint8 questIndex)
{
// when a free for all questitem is looted
// all players will get notified of it being removed
// (other questitems can be looted by each group member)
// bit inefficient but isnt called often
GuidSet::iterator i_next;
for (GuidSet::iterator i = m_playersLooting.begin(); i != m_playersLooting.end(); i = i_next)
{
i_next = i;
++i_next;
if (Player* pl = ObjectAccessor::FindPlayer(*i))
{
QuestItemMap::const_iterator pq = m_playerQuestItems.find(pl->GetGUIDLow());
if (pq != m_playerQuestItems.end() && pq->second)
{
// find where/if the player has the given item in it's vector
QuestItemList& pql = *pq->second;
uint8 j;
for (j = 0; j < pql.size(); ++j)
if (pql[j].index == questIndex)
break;
if (j < pql.size())
pl->SendNotifyLootItemRemoved(items.size() + j);
}
}
else
m_playersLooting.erase(i);
}
}
void Loot::generateMoneyLoot(uint32 minAmount, uint32 maxAmount)
{
if (maxAmount > 0)
{
if (maxAmount <= minAmount)
gold = uint32(maxAmount * sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_MONEY));
else if ((maxAmount - minAmount) < 32700)
gold = uint32(urand(minAmount, maxAmount) * sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_MONEY));
else
gold = uint32(urand(minAmount >> 8, maxAmount >> 8) * sWorld.getConfig(CONFIG_FLOAT_RATE_DROP_MONEY)) << 8;
}
}
LootItem* Loot::LootItemInSlot(uint32 lootSlot, Player* player, QuestItem** qitem, QuestItem** ffaitem, QuestItem** conditem, QuestItem** currency)
{
LootItem* item = NULL;
bool is_looted = true;
if (lootSlot >= items.size())
{
uint32 questSlot = lootSlot - items.size();
QuestItemMap::const_iterator itr = m_playerQuestItems.find(player->GetGUIDLow());
if (itr != m_playerQuestItems.end() && questSlot < itr->second->size())
{
QuestItem* qitem2 = &itr->second->at(questSlot);
if (qitem)
*qitem = qitem2;
item = &m_questItems[qitem2->index];
is_looted = qitem2->is_looted;
}
}
else
{
item = &items[lootSlot];
is_looted = item->is_looted;
if (item->currency)
{
QuestItemMap::const_iterator itr = m_playerCurrencies.find(player->GetGUIDLow());
if (itr != m_playerCurrencies.end())
{
for (QuestItemList::const_iterator iter = itr->second->begin(); iter != itr->second->end(); ++iter)
{
if (iter->index == lootSlot)
{
QuestItem* currency2 = (QuestItem*) & (*iter);
if (currency)
*currency = currency2;
is_looted = currency2->is_looted;
break;
}
}
}
}
else if (item->freeforall)
{
QuestItemMap::const_iterator itr = m_playerFFAItems.find(player->GetGUIDLow());
if (itr != m_playerFFAItems.end())
{
for (QuestItemList::const_iterator iter = itr->second->begin(); iter != itr->second->end(); ++iter)
if (iter->index == lootSlot)
{
QuestItem* ffaitem2 = (QuestItem*) & (*iter);
if (ffaitem)
*ffaitem = ffaitem2;
is_looted = ffaitem2->is_looted;
break;
}
}
}
else if (item->conditionId)
{
QuestItemMap::const_iterator itr = m_playerNonQuestNonFFANonCurrencyConditionalItems.find(player->GetGUIDLow());
if (itr != m_playerNonQuestNonFFANonCurrencyConditionalItems.end())
{
for (QuestItemList::const_iterator iter = itr->second->begin(); iter != itr->second->end(); ++iter)
{
if (iter->index == lootSlot)
{
QuestItem* conditem2 = (QuestItem*) & (*iter);
if (conditem)
*conditem = conditem2;
is_looted = conditem2->is_looted;
break;
}
}
}
}
}
if (is_looted)
return NULL;
return item;
}
uint32 Loot::GetMaxSlotInLootFor(Player* player) const
{
QuestItemMap::const_iterator itr = m_playerQuestItems.find(player->GetGUIDLow());
return items.size() + (itr != m_playerQuestItems.end() ? itr->second->size() : 0);
}
ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li)
{
if (li.type == LOOT_ITEM_TYPE_ITEM)
{
b << uint32(li.itemid);
b << uint32(li.count); // nr of items of this type
b << uint32(ObjectMgr::GetItemPrototype(li.itemid)->DisplayInfoID);
b << uint32(li.randomSuffix);
b << uint32(li.randomPropertyId);
}
else if (li.type == LOOT_ITEM_TYPE_CURRENCY)
{
b << uint32(li.itemid);
b << uint32(li.count);
}
// b << uint8(0); // slot type - will send after this function call
return b;
}
ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv)
{
if (lv.permission == NONE_PERMISSION)
{
b << uint32(0); // gold
b << uint8(0); // item count
b << uint8(0); // currency count
return b;
}
Loot& l = lv.loot;
uint8 itemsShown = 0;
uint8 currenciesShown = 0;
b << uint32(l.gold); // gold
size_t count_pos = b.wpos(); // pos of item count byte
b << uint8(0); // item count placeholder
size_t currency_count_pos = b.wpos(); // pos of currency count byte
b << uint8(0); // currency count placeholder
for (uint8 i = 0; i < l.items.size(); ++i)
{
LootSlotType slot_type = l.items[i].GetSlotTypeForSharedLoot(lv.permission, lv.viewer, l.GetLootTarget());
if (slot_type >= MAX_LOOT_SLOT_TYPE)
continue;
b << uint8(i) << l.items[i];
b << uint8(slot_type); // 0 - get 1 - look only 2 - master selection
++itemsShown;
}
QuestItemMap const& lootPlayerNonQuestNonFFAConditionalItems = l.GetPlayerNonQuestNonFFANonCurrencyConditionalItems();
QuestItemMap::const_iterator nn_itr = lootPlayerNonQuestNonFFAConditionalItems.find(lv.viewer->GetGUIDLow());
if (nn_itr != lootPlayerNonQuestNonFFAConditionalItems.end())
{
QuestItemList* conditional_list = nn_itr->second;
for (QuestItemList::const_iterator ci = conditional_list->begin() ; ci != conditional_list->end(); ++ci)
{
LootItem& item = l.items[ci->index];
LootSlotType slot_type = item.GetSlotTypeForSharedLoot(lv.permission, lv.viewer, l.GetLootTarget(), !ci->is_looted);
if (slot_type >= MAX_LOOT_SLOT_TYPE)
continue;
b << uint8(ci->index) << item;
b << uint8(slot_type); // allow loot
++itemsShown;
}
}
// in next cases used same slot type for all items
LootSlotType slot_type = lv.permission == OWNER_PERMISSION ? LOOT_SLOT_OWNER : LOOT_SLOT_NORMAL;
QuestItemMap const& lootPlayerQuestItems = l.GetPlayerQuestItems();
QuestItemMap::const_iterator q_itr = lootPlayerQuestItems.find(lv.viewer->GetGUIDLow());
if (q_itr != lootPlayerQuestItems.end())
{
QuestItemList* q_list = q_itr->second;
for (QuestItemList::const_iterator qi = q_list->begin() ; qi != q_list->end(); ++qi)
{
LootItem& item = l.m_questItems[qi->index];
if (!qi->is_looted && !item.is_looted)
{
b << uint8(l.items.size() + (qi - q_list->begin()));
b << item;
b << uint8(slot_type); // allow loot
++itemsShown;
}
}
}
QuestItemMap const& lootPlayerFFAItems = l.GetPlayerFFAItems();
QuestItemMap::const_iterator ffa_itr = lootPlayerFFAItems.find(lv.viewer->GetGUIDLow());
if (ffa_itr != lootPlayerFFAItems.end())
{
QuestItemList* ffa_list = ffa_itr->second;
for (QuestItemList::const_iterator fi = ffa_list->begin() ; fi != ffa_list->end(); ++fi)
{
LootItem& item = l.items[fi->index];
if (!fi->is_looted && !item.is_looted)
{
b << uint8(fi->index) << item;
b << uint8(slot_type); // allow loot
++itemsShown;
}
}
}
QuestItemMap const& lootPlayerCurrencies = l.GetPlayerCurrencies();
QuestItemMap::const_iterator currency_itr = lootPlayerCurrencies.find(lv.viewer->GetGUIDLow());
if (currency_itr != lootPlayerCurrencies.end())
{
QuestItemList* currency_list = currency_itr->second;
for (QuestItemList::const_iterator ci = currency_list->begin() ; ci != currency_list->end(); ++ci)
{
LootItem& item = l.items[ci->index];
if (!ci->is_looted && !item.is_looted)
{
b << uint8(ci->index) << item;
++currenciesShown;
}
}
}
// update number of items and currencies shown
b.put<uint8>(count_pos, itemsShown);
b.put<uint8>(currency_count_pos, currenciesShown);
return b;
}
//
// --------- LootTemplate::LootGroup ---------
//
// Adds an entry to the group (at loading stage)
void LootTemplate::LootGroup::AddEntry(LootStoreItem& item)
{
if (item.chance != 0)
ExplicitlyChanced.push_back(item);
else
EqualChanced.push_back(item);
}
// Rolls an item from the group, returns NULL if all miss their chances
LootStoreItem const* LootTemplate::LootGroup::Roll() const
{
if (!ExplicitlyChanced.empty()) // First explicitly chanced entries are checked
{
float Roll = rand_chance_f();
for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i) // check each explicitly chanced entry in the template and modify its chance based on quality.
{