This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
/
enmity_container.cpp
456 lines (376 loc) · 15.7 KB
/
enmity_container.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
/*
===========================================================================
Copyright (c) 2010-2015 Darkstar Dev Teams
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 3 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, see http://www.gnu.org/licenses/
This file is part of DarkStar-server source code.
===========================================================================
*/
#include "../common/showmsg.h"
#include "../common/utils.h"
#include "ai/ai_container.h"
#include "alliance.h"
#include "enmity_container.h"
#include "entities/battleentity.h"
#include "entities/charentity.h"
#include "entities/mobentity.h"
#include "packets/entity_update.h"
#include "status_effect_container.h"
#include "utils/battleutils.h"
#include "utils/zoneutils.h"
/************************************************************************
* *
* *
* *
************************************************************************/
CEnmityContainer::CEnmityContainer(CMobEntity* holder) : m_EnmityHolder(holder)
{
}
CEnmityContainer::~CEnmityContainer()
{
Clear();
}
/************************************************************************
* *
* Clear Enmity List *
* *
************************************************************************/
void CEnmityContainer::Clear(uint32 EntityID)
{
if (EntityID == 0)
{
m_EnmityList.clear();
return;
}
else
{
m_EnmityList.erase(EntityID);
}
m_tameable = true;
}
void CEnmityContainer::LogoutReset(uint32 EntityID)
{
auto enmity_obj = m_EnmityList.find(EntityID);
if (enmity_obj != m_EnmityList.end())
{
enmity_obj->second.PEnmityOwner = nullptr;
enmity_obj->second.active = false;
}
}
/************************************************************************
* *
* Минимальное (базовое) значение ненависти *
* *
************************************************************************/
void CEnmityContainer::AddBaseEnmity(CBattleEntity* PChar)
{
m_EnmityList.emplace(PChar->id, EnmityObject_t {PChar, 0, 0, false, 0});
}
/************************************************************************
* *
* Calculate Enmity Bonus
* *
************************************************************************/
float CEnmityContainer::CalculateEnmityBonus(CBattleEntity* PEntity)
{
int enmityBonus = PEntity->getMod(Mod::ENMITY);
if (auto PChar = dynamic_cast<CCharEntity*>(PEntity))
{
enmityBonus += PChar->PMeritPoints->GetMeritValue(MERIT_ENMITY_INCREASE, PChar) - PChar->PMeritPoints->GetMeritValue(MERIT_ENMITY_DECREASE, PChar);
if (PChar->StatusEffectContainer->HasStatusEffect(EFFECT_SOULEATER))
{
enmityBonus -= PChar->PMeritPoints->GetMeritValue(MERIT_MUTED_SOUL, PChar);
}
}
float bonus = (100.f + std::clamp(enmityBonus, -50, 100)) / 100.f;
return bonus;
}
/************************************************************************
* *
* Add entity to hate list *
* *
************************************************************************/
void CEnmityContainer::UpdateEnmity(CBattleEntity* PEntity, int32 CE, int32 VE, bool withMaster, bool tameable)
{
// you're too far away so i'm ignoring you
if (!IsWithinEnmityRange(PEntity))
{
CE = 0;
VE = 0;
}
auto enmity_obj = m_EnmityList.find(PEntity->id);
if (enmity_obj != m_EnmityList.end())
{
if (enmity_obj->second.PEnmityOwner == nullptr) enmity_obj->second.PEnmityOwner = PEntity;
float bonus = CalculateEnmityBonus(PEntity);
int32 newCE = (int32)(enmity_obj->second.CE + (CE > 0 ? CE * bonus : CE));
int32 newVE = (int32)(enmity_obj->second.VE + (VE > 0 ? VE * bonus : VE));
//Check for cap limit
enmity_obj->second.CE = std::clamp(newCE, 0, EnmityCap);
enmity_obj->second.VE = std::clamp(newVE, 0, EnmityCap);
enmity_obj->second.active = true;
if (CE + VE > 0 && PEntity->getMod(Mod::TREASURE_HUNTER) > enmity_obj->second.maxTH)
enmity_obj->second.maxTH = PEntity->getMod(Mod::TREASURE_HUNTER);
}
else if (CE >= 0 && VE >= 0)
{
bool initial = true;
for (auto&& enmityObject : m_EnmityList)
{
if (enmityObject.second.active)
{
initial = false;
break;
}
}
int16 maxTH = CE + VE > 0 ? PEntity->getMod(Mod::TREASURE_HUNTER) : 0;
if (initial)
CE += 200;
float bonus = CalculateEnmityBonus(PEntity);
CE = std::clamp((int32)(CE * bonus), 0, EnmityCap);
VE = std::clamp((int32)(VE * bonus), 0, EnmityCap);
m_EnmityList.emplace(PEntity->id, EnmityObject_t {PEntity, CE, VE, true, maxTH});
if (withMaster && PEntity->PMaster != nullptr)
{
// add master to the enmity list (pet and charmed mob)
if (PEntity->objtype == TYPE_PET || (PEntity->objtype == TYPE_MOB && PEntity->PMaster != nullptr && PEntity->PMaster->objtype == TYPE_PC))
{
AddBaseEnmity(PEntity->PMaster);
}
}
}
if (!tameable)
{
m_tameable = false;
}
}
bool CEnmityContainer::HasID(uint32 TargetID)
{
return std::find_if(m_EnmityList.begin(), m_EnmityList.end(), [TargetID](auto elem)
{
return elem.first == TargetID && elem.second.active;
}) != m_EnmityList.end();
}
/************************************************************************
* *
* *
* *
************************************************************************/
void CEnmityContainer::UpdateEnmityFromCure(CBattleEntity* PEntity, uint8 level, int32 CureAmount, bool isCureV)
{
if (!IsWithinEnmityRange(PEntity))
return;
int32 CE = 0;
int32 VE = 0;
float bonus = CalculateEnmityBonus(PEntity);
float tranquilHeartReduction = 1.f - battleutils::HandleTranquilHeart(PEntity);
if (isCureV)
{
CE = (int32)(400.f * bonus * tranquilHeartReduction);
VE = (int32)(800.f * bonus * tranquilHeartReduction);
}
else
{
CureAmount = (CureAmount < 1 ? 1 : CureAmount);
CE = (int32)(40.f / battleutils::GetEnmityModCure(level) * CureAmount * bonus * tranquilHeartReduction);
VE = (int32)(240.f / battleutils::GetEnmityModCure(level) * CureAmount * bonus * tranquilHeartReduction);
}
auto enmity_obj = m_EnmityList.find(PEntity->id);
if (enmity_obj != m_EnmityList.end())
{
enmity_obj->second.CE = std::clamp(enmity_obj->second.CE + CE, 0, EnmityCap);
enmity_obj->second.VE = std::clamp(enmity_obj->second.VE + VE, 0, EnmityCap);
enmity_obj->second.active = true;
}
else
{
m_EnmityList.emplace(PEntity->id, EnmityObject_t {PEntity, std::clamp(CE, 0, EnmityCap), std::clamp(VE, 0, EnmityCap), true, 0});
}
}
/************************************************************************
* *
* Lower enmity by percent % *
* *
************************************************************************/
void CEnmityContainer::LowerEnmityByPercent(CBattleEntity* PEntity, uint8 percent, CBattleEntity* HateReceiver)
{
auto enmity_obj = m_EnmityList.find(PEntity->id);
if (enmity_obj != m_EnmityList.end())
{
float mod = ((float)(percent) / 100.0f);
auto CEValue = (int16)(enmity_obj->second.CE * mod);
enmity_obj->second.CE -= (CEValue < 0 ? 0 : CEValue);
auto VEValue = (int16)(enmity_obj->second.VE * mod);
enmity_obj->second.VE -= (VEValue < 0 ? 0 : VEValue);
// transfer hate if HateReceiver not nullptr
if (HateReceiver != nullptr)
{
UpdateEnmity(HateReceiver, CEValue, VEValue);
}
}
}
/************************************************************************
* *
* Returns the CE or VE for the current entity *
* *
************************************************************************/
int32 CEnmityContainer::GetCE(CBattleEntity* PEntity) const
{
auto PEnmity = m_EnmityList.find(PEntity->id);
return PEnmity != m_EnmityList.end() ? PEnmity->second.CE : 0;
}
int32 CEnmityContainer::GetVE(CBattleEntity* PEntity) const
{
auto PEnmity = m_EnmityList.find(PEntity->id);
return PEnmity != m_EnmityList.end() ? PEnmity->second.VE : 0;
}
/************************************************************************
* *
* Sets the CE or VE for the current entity *
* *
************************************************************************/
void CEnmityContainer::SetCE(CBattleEntity* PEntity, const int32 amount)
{
auto PEnmity = m_EnmityList.find(PEntity->id);
if (PEnmity != m_EnmityList.end())
{
PEnmity->second.CE = std::min(amount, EnmityCap);
}
else
{
AddBaseEnmity(PEntity);
SetCE(PEntity, amount);
}
}
void CEnmityContainer::SetVE(CBattleEntity* PEntity, const int32 amount)
{
auto PEnmity = m_EnmityList.find(PEntity->id);
if (PEnmity != m_EnmityList.end())
{
PEnmity->second.VE = std::min(amount, EnmityCap);
}
else
{
AddBaseEnmity(PEntity);
SetVE(PEntity, amount);
}
}
/************************************************************************
* *
* *
* *
************************************************************************/
void CEnmityContainer::UpdateEnmityFromDamage(CBattleEntity* PEntity, int32 Damage)
{
Damage = (Damage < 1 ? 1 : Damage);
int16 damageMod = battleutils::GetEnmityModDamage(m_EnmityHolder->GetMLevel());
int32 CE = (int32)(80.f / damageMod * Damage);
int32 VE = (int32)(240.f / damageMod * Damage);
UpdateEnmity(PEntity, CE, VE);
if (m_EnmityHolder && m_EnmityHolder->m_HiPCLvl < PEntity->GetMLevel())
m_EnmityHolder->m_HiPCLvl = PEntity->GetMLevel();
}
/************************************************************************
* *
* *
* *
************************************************************************/
void CEnmityContainer::UpdateEnmityFromAttack(CBattleEntity* PEntity, int32 Damage)
{
if (auto enmity_obj = m_EnmityList.find(PEntity->id); enmity_obj != m_EnmityList.end())
{
float reduction = (100.f - std::min<int16>(PEntity->getMod(Mod::ENMITY_LOSS_REDUCTION), 100)) / 100.f;
int32 CE = (int32)(-1800.f * Damage / PEntity->GetMaxHP() * reduction);
enmity_obj->second.CE = std::clamp(enmity_obj->second.CE + CE, 0, EnmityCap);
}
}
/************************************************************************
* *
* Decay Enmity, Get Entity with the highest enmity *
* *
************************************************************************/
CBattleEntity* CEnmityContainer::GetHighestEnmity()
{
if (m_EnmityList.empty())
{
return nullptr;
}
uint32 HighestEnmity = 0;
auto highest = m_EnmityList.end();
bool active = false;
for (auto it = m_EnmityList.begin(); it != m_EnmityList.end(); ++it)
{
const EnmityObject_t& PEnmityObject = it->second;
uint32 Enmity = PEnmityObject.CE + PEnmityObject.VE;
if (Enmity >= HighestEnmity && ((PEnmityObject.active == active) || (PEnmityObject.active && !active)))
{
auto POwner = PEnmityObject.PEnmityOwner;
if (!POwner || (POwner->allegiance != m_EnmityHolder->allegiance))
{
active = PEnmityObject.active;
HighestEnmity = Enmity;
highest = it;
}
}
}
CBattleEntity* PEntity = nullptr;
if (highest != m_EnmityList.end())
{
PEntity = highest->second.PEnmityOwner;
if (!PEntity)
{
PEntity = zoneutils::GetChar(highest->first);
}
if (!PEntity || PEntity->getZone() != m_EnmityHolder->getZone() ||
PEntity->PInstance != m_EnmityHolder->PInstance)
{
m_EnmityList.erase(highest);
PEntity = GetHighestEnmity();
}
}
return PEntity;
}
void CEnmityContainer::DecayEnmity()
{
for (auto it = m_EnmityList.begin(); it != m_EnmityList.end(); ++it)
{
EnmityObject_t& PEnmityObject = it->second;
constexpr int decay_amount = (int)(60 / server_tick_rate);
PEnmityObject.VE -= PEnmityObject.VE > decay_amount ? decay_amount : PEnmityObject.VE;
//ShowDebug("%d: active: %d CE: %d VE: %d\n", it->first, PEnmityObject.active, PEnmityObject.CE, PEnmityObject.VE);
}
}
bool CEnmityContainer::IsWithinEnmityRange(CBattleEntity* PEntity) const
{
float maxRange = square(m_EnmityHolder->m_Type == MOBTYPE_NOTORIOUS ? 28.f : 25.f);
return distanceSquared(m_EnmityHolder->loc.p, PEntity->loc.p) <= maxRange;
}
int16 CEnmityContainer::GetHighestTH() const
{
CBattleEntity* PEntity = nullptr;
int16 THLvl = 0;
for (auto it = m_EnmityList.cbegin(); it != m_EnmityList.cend(); ++it)
{
const EnmityObject_t& PEnmityObject = it->second;
PEntity = PEnmityObject.PEnmityOwner;
if (PEntity != nullptr && !PEntity->isDead() && IsWithinEnmityRange(PEntity) && PEnmityObject.maxTH > THLvl)
THLvl = PEnmityObject.maxTH;
}
return THLvl;
}
EnmityList_t* CEnmityContainer::GetEnmityList()
{
return &m_EnmityList;
}
bool CEnmityContainer::IsTameable()
{
return m_tameable;
}