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
|
/*
* The Mana Server
* Copyright (C) 2004-2010 The Mana World Development Team
*
* This file is part of The Mana Server.
*
* The Mana Server 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
* any later version.
*
* The Mana Server 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 The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
#include "attribute.h"
#include "game-server/being.h"
#include "utils/logger.h"
#include <cassert>
AttributeModifiersEffect::AttributeModifiersEffect(StackableType stackableType,
ModifierEffectType effectType) :
mCacheVal(0),
mMod(effectType == Multiplicative ? 1 : 0),
mStackableType(stackableType),
mEffectType(effectType)
{
assert(effectType == Multiplicative
|| effectType == Additive);
assert(stackableType == Stackable
|| stackableType == NonStackable
|| stackableType == NonStackableBonus);
LOG_DEBUG("Layer created with effectType " << effectType
<< " and stackableType " << stackableType << ".");
}
AttributeModifiersEffect::~AttributeModifiersEffect()
{
// ?
/*mStates.clear();*/
LOG_WARN("DELETION of attribute effect!");
}
bool AttributeModifiersEffect::add(unsigned short duration,
double value,
double prevLayerValue,
int level)
{
LOG_DEBUG("Adding modifier with value " << value <<
" with a previous layer value of " << prevLayerValue << ". "
"Current mod at this layer: " << mMod << ".");
bool ret = false;
mStates.push_back(new AttributeModifierState(duration, value, level));
switch (mStackableType) {
case Stackable:
switch (mEffectType) {
case Additive:
if (value)
{
ret = true;
mMod += value;
mCacheVal = prevLayerValue + mMod;
}
break;
case Multiplicative:
if (value != 1)
{
ret = true;
mMod *= value;
mCacheVal = prevLayerValue * mMod;
}
break;
default:
LOG_FATAL("Attribute modifiers effect: unhandled type '"
<< mEffectType << "' as a stackable!");
assert(0);
break;
}
break;
case NonStackable:
switch (mEffectType) {
case Additive:
if (value > mMod)
{
ret = true;
mMod = value;
if (mMod > prevLayerValue)
mCacheVal = mMod;
}
break;
default:
LOG_FATAL("Attribute modifiers effect: unhandled type '"
<< mEffectType << "' as a non-stackable!");
assert(0);
}
// A multiplicative type would also be nonsensical
break;
case NonStackableBonus:
switch (mEffectType) {
case Additive:
case Multiplicative:
if (value > mMod)
{
ret = true;
mMod = value;
mCacheVal = mEffectType == Additive ? prevLayerValue + mMod
: prevLayerValue * mMod;
}
break;
default:
LOG_FATAL("Attribute modifiers effect: unhandled type '"
<< mEffectType << "' as a non-stackable bonus!");
assert(0);
}
break;
default:
LOG_FATAL("Attribute modifiers effect: unknown stackable type '"
<< mStackableType << "'!");
assert(0);
}
return ret;
}
bool durationCompare(const AttributeModifierState *lhs,
const AttributeModifierState *rhs)
{
return lhs->mDuration < rhs->mDuration;
}
bool AttributeModifiersEffect::remove(double value, unsigned id,
bool fullCheck)
{
/* We need to find and check this entry exists, and erase the entry
from the list too. */
if (!fullCheck)
mStates.sort(durationCompare); /* Search only through those with a duration of 0. */
bool ret = false;
for (std::list< AttributeModifierState * >::iterator it = mStates.begin();
it != mStates.end() && (fullCheck || !(*it)->mDuration);)
{
/* Check for a match */
if ((*it)->mValue != value || (*it)->mId != id)
{
++it;
continue;
}
delete *it;
mStates.erase(it++);
/* If this is stackable, we need to update for every modifier affected */
if (mStackableType == Stackable)
updateMod(value);
ret = true;
if (!id)
break;
}
/*
* Non stackables only need to be updated once, since this is recomputed
* from scratch. This is done at the end after modifications have been
* made as necessary.
*/
if (ret && mStackableType != Stackable)
updateMod();
return ret;
}
void AttributeModifiersEffect::updateMod(double value)
{
if (mStackableType == Stackable)
{
if (mEffectType == Additive)
{
mMod -= value;
}
else if (mEffectType == Multiplicative)
{
if (value)
mMod /= value;
else
{
mMod = 1;
for (std::list< AttributeModifierState * >::const_iterator
it = mStates.begin(),
it_end = mStates.end();
it != it_end;
++it)
mMod *= (*it)->mValue;
}
}
else LOG_ERROR("Attribute modifiers effect: unhandled type '"
<< mEffectType << "' as a stackable in cache update!");
}
else if (mStackableType == NonStackable || mStackableType == NonStackableBonus)
{
if (mMod == value)
{
mMod = 0;
for (std::list< AttributeModifierState * >::const_iterator
it = mStates.begin(),
it_end = mStates.end();
it != it_end;
++it)
if ((*it)->mValue > mMod)
mMod = (*it)->mValue;
}
}
else
{
LOG_ERROR("Attribute modifiers effect: unknown stackable type '"
<< mStackableType << "' in cache update!");
}
}
bool AttributeModifiersEffect::recalculateModifiedValue(double newPrevLayerValue)
{
double oldValue = mCacheVal;
switch (mEffectType) {
case Additive:
switch (mStackableType) {
case Stackable:
case NonStackableBonus:
mCacheVal = newPrevLayerValue + mMod;
break;
case NonStackable:
mCacheVal = newPrevLayerValue < mMod ? mMod : newPrevLayerValue;
break;
default:
LOG_FATAL("Unknown effect type '" << mEffectType << "'!");
assert(0);
} break;
case Multiplicative:
mCacheVal = mStackableType == Stackable ? newPrevLayerValue * mMod : newPrevLayerValue * mMod;
break;
default:
LOG_FATAL("Unknown effect type '" << mEffectType << "'!");
assert(0);
}
return oldValue != mCacheVal;
}
bool Attribute::add(unsigned short duration, double value,
unsigned layer, int level)
{
assert(mMods.size() > layer);
LOG_DEBUG("Adding modifier to attribute with duration " << duration <<
", value " << value << ", at layer " << layer << " with id "
<< level);
if (mMods.at(layer)->add(duration, value,
(layer ? mMods.at(layer - 1)->getCachedModifiedValue()
: mBase)
, level))
{
while (++layer < mMods.size())
{
if (!mMods.at(layer)->recalculateModifiedValue(
mMods.at(layer - 1)->getCachedModifiedValue()))
{
LOG_DEBUG("Modifier added, but modified value not changed.");
return false;
}
}
LOG_DEBUG("Modifier added. Base value: " << mBase << ", new modified "
"value: " << getModifiedAttribute() << ".");
return true;
}
LOG_DEBUG("Failed to add modifier!");
return false;
}
bool Attribute::remove(double value, unsigned layer,
int lvl, bool fullcheck)
{
assert(mMods.size() > layer);
if (mMods.at(layer)->remove(value, lvl, fullcheck))
{
for (; layer < mMods.size(); ++layer)
if (!mMods.at(layer)->recalculateModifiedValue(
layer ? mMods.at(layer - 1)->getCachedModifiedValue()
: mBase))
return false;
return true;
}
return false;
}
bool AttributeModifiersEffect::tick()
{
bool ret = false;
std::list<AttributeModifierState *>::iterator it = mStates.begin();
while (it != mStates.end())
{
if ((*it)->tick())
{
double value = (*it)->mValue;
LOG_DEBUG("Modifier of value " << value << " expiring!");
delete *it;
mStates.erase(it++);
updateMod(value);
ret = true;
}
++it;
}
return ret;
}
Attribute::Attribute(const AttributeManager::AttributeInfo &info):
mBase(0),
mMinValue(info.minimum),
mMaxValue(info.maximum)
{
const std::vector<AttributeModifier> &modifiers = info.modifiers;
LOG_DEBUG("Construction of new attribute with '" << modifiers.size()
<< "' layers.");
for (unsigned i = 0; i < modifiers.size(); ++i)
{
LOG_DEBUG("Adding layer with stackable type "
<< modifiers[i].stackableType
<< " and effect type " << modifiers[i].effectType << ".");
mMods.push_back(new AttributeModifiersEffect(modifiers[i].stackableType,
modifiers[i].effectType));
LOG_DEBUG("Layer added.");
}
mBase = checkBounds(mBase);
}
Attribute::~Attribute()
{
// for (std::vector<AttributeModifiersEffect *>::iterator it = mMods.begin(),
// it_end = mMods.end(); it != it_end; ++it)
// {
// ?
//delete *it;
// }
}
bool Attribute::tick()
{
bool ret = false;
double prev = mBase;
for (std::vector<AttributeModifiersEffect *>::iterator it = mMods.begin(),
it_end = mMods.end(); it != it_end; ++it)
{
if ((*it)->tick())
{
LOG_DEBUG("Attribute layer " << mMods.begin() - it
<< " has expiring modifiers.");
ret = true;
}
if (ret)
if (!(*it)->recalculateModifiedValue(prev)) ret = false;
prev = (*it)->getCachedModifiedValue();
}
return ret;
}
void Attribute::clearMods()
{
for (std::vector<AttributeModifiersEffect *>::iterator it = mMods.begin(),
it_end = mMods.end(); it != it_end; ++it)
(*it)->clearMods(mBase);
}
void Attribute::setBase(double base)
{
base = checkBounds(base);
LOG_DEBUG("Setting base attribute from " << mBase << " to " << base << ".");
double prev = mBase = base;
std::vector<AttributeModifiersEffect *>::iterator it = mMods.begin();
while (it != mMods.end())
{
if ((*it)->recalculateModifiedValue(prev))
prev = (*it++)->getCachedModifiedValue();
else
break;
}
}
void AttributeModifiersEffect::clearMods(double baseValue)
{
mStates.clear();
mCacheVal = baseValue;
mMod = mEffectType == Additive ? 0 : 1;
}
double Attribute::checkBounds(double baseValue)
{
LOG_DEBUG("Checking bounds for value: " << baseValue);
if (baseValue > mMaxValue)
baseValue = mMaxValue;
else if (baseValue < mMinValue)
baseValue = mMinValue;
return baseValue;
}
|