forked from ike3/mangosbot-bots
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LootObjectStack.cpp
339 lines (277 loc) · 9.03 KB
/
LootObjectStack.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
#include "../botpch.h"
#include "LootObjectStack.h"
#include "playerbot.h"
#include "PlayerbotAIConfig.h"
#include "ServerFacade.h"
#include "strategy/values/SharedValueContext.h"
using namespace ai;
using namespace std;
#define MAX_LOOT_OBJECT_COUNT 10
LootTarget::LootTarget(ObjectGuid guid) : guid(guid), asOfTime(time(0))
{
}
LootTarget::LootTarget(LootTarget const& other)
{
guid = other.guid;
asOfTime = other.asOfTime;
}
LootTarget& LootTarget::operator=(LootTarget const& other)
{
if((void*)this == (void*)&other)
return *this;
guid = other.guid;
asOfTime = other.asOfTime;
return *this;
}
bool LootTarget::operator< (const LootTarget& other) const
{
return guid < other.guid;
}
void LootTargetList::shrink(time_t fromTime)
{
for (set<LootTarget>::iterator i = begin(); i != end(); )
{
if (i->asOfTime <= fromTime)
erase(i++);
else
++i;
}
}
LootObject::LootObject(Player* bot, ObjectGuid guid)
: guid(), skillId(SKILL_NONE), reqSkillValue(0), reqItem(0)
{
Refresh(bot, guid);
}
void LootObject::Refresh(Player* bot, ObjectGuid guid)
{
skillId = SKILL_NONE;
reqSkillValue = 0;
reqItem = 0;
this->guid = ObjectGuid();
PlayerbotAI* ai = bot->GetPlayerbotAI();
Creature *creature = ai->GetCreature(guid);
if (creature && sServerFacade.GetDeathState(creature) == CORPSE)
{
if (creature->HasFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE))
this->guid = guid;
if (creature->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE))
{
skillId = creature->GetCreatureInfo()->GetRequiredLootSkill();
uint32 targetLevel = creature->GetLevel();
reqSkillValue = targetLevel < 10 ? 1 : targetLevel < 20 ? (targetLevel - 10) * 10 : targetLevel * 5;
if (ai->HasSkill((SkillType)skillId) && bot->GetSkillValue(skillId) >= reqSkillValue)
this->guid = guid;
}
return;
}
GameObject* go = ai->GetGameObject(guid);
if (go && sServerFacade.isSpawned(go)
#ifdef CMANGOS
&& !go->IsInUse()
#endif
&& (go->GetGoState() == GO_STATE_READY || go->GetGoState() == GO_STATE_ACTIVE))
{
bool isQuestItemOnly = false;
#ifdef MANGOSBOT_TWO
for (int i = 0; i < QUEST_ITEM_OBJECTIVES_COUNT; i++)
{
int itemId = go->GetGOInfo()->questItems[i];
if (IsNeededForQuest(bot, itemId))
{
this->guid = guid;
return;
}
isQuestItemOnly |= itemId > 0;
}
#else
/*if (!guid.IsEmpty())
{
for (auto& entry : GAI_VALUE2(list<int32>, "item drop list", -go->GetEntry()))
{
if (IsNeededForQuest(bot, entry))
{
this->guid = guid;
return;
}
isQuestItemOnly |= entry > 0;
}
}*/
#endif
if (isQuestItemOnly)
return;
uint32 goId = go->GetGOInfo()->id;
uint32 lockId = go->GetGOInfo()->GetLockId();
LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
if (!lockInfo)
return;
for (int i = 0; i < 8; ++i)
{
switch (lockInfo->Type[i])
{
case LOCK_KEY_ITEM:
if (lockInfo->Index[i] > 0)
{
reqItem = lockInfo->Index[i];
this->guid = guid;
}
break;
case LOCK_KEY_SKILL:
if (goId == 13891 || goId == 19535) // Serpentbloom
{
this->guid = guid;
}
else if (SkillByLockType(LockType(lockInfo->Index[i])) > 0)
{
skillId = SkillByLockType(LockType(lockInfo->Index[i]));
reqSkillValue = max((uint32)1, lockInfo->Skill[i]);
this->guid = guid;
}
break;
case LOCK_KEY_NONE:
this->guid = guid;
break;
}
}
}
}
bool LootObject::IsNeededForQuest(Player* bot, uint32 itemId)
{
for (int qs = 0; qs < MAX_QUEST_LOG_SIZE; ++qs)
{
uint32 questId = bot->GetQuestSlotQuestId(qs);
if (questId == 0)
continue;
QuestStatusData& qData = bot->getQuestStatusMap()[questId];
if (qData.m_status != QUEST_STATUS_INCOMPLETE)
continue;
Quest const* qInfo = sObjectMgr.GetQuestTemplate(questId);
if (!qInfo)
continue;
for (int i = 0; i < QUEST_ITEM_OBJECTIVES_COUNT; ++i)
{
if (!qInfo->ReqItemCount[i] || (qInfo->ReqItemCount[i] - qData.m_itemcount[i]) <= 0)
continue;
if (qInfo->ReqItemId[i] != itemId)
continue;
return true;
}
}
return false;
}
WorldObject* LootObject::GetWorldObject(Player* bot)
{
Refresh(bot, guid);
PlayerbotAI* ai = bot->GetPlayerbotAI();
Creature *creature = ai->GetCreature(guid);
if (creature && sServerFacade.GetDeathState(creature) == CORPSE)
return creature;
GameObject* go = ai->GetGameObject(guid);
if (go && sServerFacade.isSpawned(go))
return go;
return NULL;
}
LootObject::LootObject(const LootObject& other)
{
guid = other.guid;
skillId = other.skillId;
reqSkillValue = other.reqSkillValue;
reqItem = other.reqItem;
}
bool LootObject::IsLootPossible(Player* bot)
{
if (IsEmpty() || !GetWorldObject(bot))
return false;
PlayerbotAI* ai = bot->GetPlayerbotAI();
if (reqItem && !bot->HasItemCount(reqItem, 1))
return false;
if (guid.IsCreature())
{
Creature* creature = ai->GetCreature(guid);
if (creature && sServerFacade.GetDeathState(creature) == CORPSE)
{
if (creature->m_loot && skillId != SKILL_SKINNING)
if (!creature->m_loot->CanLoot(bot))
return false;
}
}
AiObjectContext* context = ai->GetAiObjectContext();
if (!AI_VALUE2_LAZY(bool, "should loot object", to_string(guid.GetRawValue())))
return false;
if (guid.IsGameObject())
{
GameObject* go = ai->GetGameObject(guid);
if (go)
{
if (sObjectMgr.IsGameObjectForQuests(guid.GetEntry())) //If object has quest loot bot needs the quest.
if (!go->ActivateToQuest(bot))
return false;
}
}
if (skillId == SKILL_NONE)
return true;
if (skillId == SKILL_FISHING)
return false;
if (!ai->HasSkill((SkillType)skillId))
return false;
if (!reqSkillValue)
return true;
uint32 skillValue = uint32(bot->GetSkillValue(skillId));
if (reqSkillValue > skillValue)
return false;
if (skillId == SKILL_MINING && !bot->HasItemCount(2901, 1))
return false;
if (skillId == SKILL_SKINNING && !bot->HasItemCount(7005, 1))
return false;
return true;
}
bool LootObjectStack::Add(ObjectGuid guid)
{
if (!availableLoot.insert(guid).second)
return false;
if (availableLoot.size() < MAX_LOOT_OBJECT_COUNT)
return true;
vector<LootObject> ordered = OrderByDistance();
for (size_t i = MAX_LOOT_OBJECT_COUNT; i < ordered.size(); i++)
Remove(ordered[i].guid);
return true;
}
void LootObjectStack::Remove(ObjectGuid guid)
{
LootTargetList::iterator i = availableLoot.find(guid);
if (i != availableLoot.end())
availableLoot.erase(i);
}
void LootObjectStack::Clear()
{
availableLoot.clear();
}
bool LootObjectStack::CanLoot(float maxDistance)
{
vector<LootObject> ordered = OrderByDistance(maxDistance);
return !ordered.empty();
}
LootObject LootObjectStack::GetLoot(float maxDistance)
{
vector<LootObject> ordered = OrderByDistance(maxDistance);
return ordered.empty() ? LootObject() : *ordered.begin();
}
vector<LootObject> LootObjectStack::OrderByDistance(float maxDistance)
{
availableLoot.shrink(time(0) - 30);
map<float, LootObject> sortedMap;
LootTargetList safeCopy(availableLoot);
for (LootTargetList::iterator i = safeCopy.begin(); i != safeCopy.end(); i++)
{
ObjectGuid guid = i->guid;
LootObject lootObject(bot, guid);
if (!lootObject.IsLootPossible(bot))
continue;
float distance = sqrt(bot->GetDistance(lootObject.GetWorldObject(bot)));
if (!maxDistance || distance <= maxDistance)
sortedMap[distance] = lootObject;
}
vector<LootObject> result;
for (map<float, LootObject>::iterator i = sortedMap.begin(); i != sortedMap.end(); i++)
result.push_back(i->second);
return result;
}