-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathcreature_attributes.cpp
411 lines (339 loc) · 13 KB
/
creature_attributes.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
/* Copyright (C) 2013-2014 Michal Brzozowski ([email protected])
This file is part of KeeperRL.
KeeperRL 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.
KeeperRL 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/ . */
#include "stdafx.h"
#include "creature_attributes.h"
#include "creature.h"
#include "sound.h"
#include "player_message.h"
#include "item.h"
#include "item_factory.h"
#include "body.h"
#include "attack_level.h"
#include "attack_type.h"
#include "view_object.h"
#include "spell_map.h"
#include "effect.h"
#include "minion_trait.h"
#include "view_id.h"
#include "companion_info.h"
#include "game.h"
#include "content_factory.h"
void CreatureAttributes::initializeLastingEffects() {
for (LastingEffect effect : ENUM_ALL(LastingEffect))
lastingEffects[effect] = GlobalTime(-500);
}
void CreatureAttributes::randomize() {
int chosen = Random.get(genderAlternatives.size() + 1);
if (chosen > 0) {
gender = genderAlternatives[chosen - 1].first;
viewId = genderAlternatives[chosen - 1].second;
}
}
bool CreatureAttributes::isInstantPrisoner() const {
return instantPrisoner;
}
CreatureAttributes::CreatureAttributes(function<void(CreatureAttributes&)> fun) {
fun(*this);
initializeLastingEffects();
}
CreatureAttributes::~CreatureAttributes() {}
CreatureAttributes::CreatureAttributes(const CreatureAttributes&) = default;
CreatureAttributes::CreatureAttributes(CreatureAttributes&&) noexcept = default;
CreatureAttributes& CreatureAttributes::operator =(const CreatureAttributes&) = default;
CreatureAttributes& CreatureAttributes::operator =(CreatureAttributes&&) = default;
template <class Archive>
void CreatureAttributes::serializeImpl(Archive& ar, const unsigned int version) {
ar(NAMED(viewId), NAMED(illusionViewObject), NAMED(name), NAMED(attr), NAMED(chatReactionFriendly));
ar(NAMED(chatReactionHostile), NAMED(passiveAttack), OPTION(gender), OPTION(viewIdUpgrades), OPTION(promotionCost));
ar(NAMED(body), OPTION(deathDescription), NAMED(hatedByEffect), OPTION(instantPrisoner));
ar(OPTION(cantEquip), OPTION(aiType), OPTION(canJoinCollective), OPTION(genderAlternatives), NAMED(promotionGroup));
ar(OPTION(boulder), OPTION(noChase), OPTION(isSpecial), OPTION(spellSchools), OPTION(spells));
ar(SKIP(permanentEffects), OPTION(lastingEffects), OPTION(minionActivities), OPTION(expLevel), OPTION(inventory));
ar(OPTION(noAttackSound), OPTION(maxLevelIncrease), NAMED(creatureId), NAMED(petReaction));
ar(OPTION(automatonParts), OPTION(specialAttr), NAMED(deathEffect), NAMED(chatEffect), OPTION(companions));
ar(OPTION(maxPromotions), OPTION(afterKilledSomeone), SKIP(permanentBuffs), OPTION(killedAchievement));
ar(OPTION(killedByAchievement), OPTION(steedAchievement), OPTION(fixedAttr), OPTION(grantsExperience));
if (version >= 1)
ar(OPTION(noCopulation));
if (version >= 2) {
ar(OPTION(copulationEffect));
ar(OPTION(copulationClientEffect));
}
for (auto& a : attr)
a.second = max(0, a.second);
}
template <class Archive>
void CreatureAttributes::serialize(Archive& ar, const unsigned int version) {
serializeImpl(ar, version);
}
SERIALIZABLE(CreatureAttributes);
SERIALIZATION_CONSTRUCTOR_IMPL(CreatureAttributes);
CreatureAttributes& CreatureAttributes::setCreatureId(CreatureId id) {
creatureId = id;
return *this;
}
const optional<CreatureId>& CreatureAttributes::getCreatureId() const {
return creatureId;
}
CreatureName& CreatureAttributes::getName() {
return name;
}
const CreatureName& CreatureAttributes::getName() const {
return name;
}
void CreatureAttributes::increaseBaseAttr(AttrType type, int v) {
attr[type] += v;
attr[type] = max(0, attr[type]);
}
HashMap<AttrType, int>& CreatureAttributes::getAllAttr() {
return attr;
}
void CreatureAttributes::setBaseAttr(AttrType type, int v) {
attr[type] = max(0, v);
}
void CreatureAttributes::setAIType(AIType type) {
aiType = type;
}
AIType CreatureAttributes::getAIType() const {
return aiType;
}
const TString& CreatureAttributes::getDeathDescription(const ContentFactory* factory) const {
if (!!deathDescription)
return *deathDescription;
return body->getDeathDescription(factory);
}
void CreatureAttributes::setDeathDescription(TString c) {
deathDescription = std::move(c);
}
const Gender& CreatureAttributes::getGender() const {
return gender;
}
int CreatureAttributes::getRawAttr(AttrType type) const {
int ret = getValueMaybe(attr, type).value_or(0) + getValueMaybe(expLevel, type).value_or(0);
if (type == AttrType("DEFENSE")) {
int defense = 0;
for (auto& elem : expLevel)
defense = max(defense, (int) elem.second);
ret += defense;
}
return ret;
}
double CreatureAttributes::getExpLevel(AttrType type) const {
return getValueMaybe(expLevel, type).value_or(0);
}
const HashMap<AttrType, double>& CreatureAttributes::getExpLevel() const {
return expLevel;
}
const HashMap<AttrType, int>& CreatureAttributes::getMaxExpLevel() const {
return maxLevelIncrease;
}
void CreatureAttributes::increaseMaxExpLevel(AttrType type, int increase) {
maxLevelIncrease[type] = max(0, maxLevelIncrease[type] + increase);
expLevel[type] = min<double>(expLevel[type], maxLevelIncrease[type]);
}
void CreatureAttributes::increaseExpLevel(AttrType type, double increase) {
increase = max(0.0, min(increase, (double) maxLevelIncrease[type] - expLevel[type]));
expLevel[type] += increase;
}
bool CreatureAttributes::isTrainingMaxedOut(AttrType type) const {
return getExpLevel(type) >= getValueMaybe(maxLevelIncrease, type).value_or(0);
}
vector<SpellSchoolId> CreatureAttributes::getSpellSchools() const {
return spellSchools;
}
void CreatureAttributes::addSpellSchool(SpellSchoolId id) {
spellSchools.push_back(id);
}
Body& CreatureAttributes::getBody() {
return *body;
}
const Body& CreatureAttributes::getBody() const {
return *body;
}
optional<SoundId> CreatureAttributes::getAttackSound(AttackType type, bool damage) const {
if (!noAttackSound)
switch (type) {
case AttackType::HIT:
case AttackType::CRUSH: return damage ? SoundId("BLUNT_DAMAGE") : SoundId("BLUNT_NO_DAMAGE");
case AttackType::CUT:
case AttackType::STAB: return damage ? SoundId("BLADE_DAMAGE") : SoundId("BLADE_NO_DAMAGE");
default: return none;
}
else
return none;
}
TString CreatureAttributes::getDescription(const ContentFactory* factory) const {
return setSubjectGender(body->getDescription(factory), name.name);
}
void CreatureAttributes::add(BodyPart p, int count, const ContentFactory* factory) {
for (auto effect : ENUM_ALL(LastingEffect))
if (body->isIntrinsicallyAffected(effect, factory))
--permanentEffects[effect];
body->addWithoutUpdatingPermanentEffects(p, count);
for (auto effect : ENUM_ALL(LastingEffect))
if (body->isIntrinsicallyAffected(effect, factory))
++permanentEffects[effect];
}
static TString getVerbalReaction(const TString& reaction, const Creature* me) {
return reaction.text.visit(
[&](const string& s) -> TString {
if (s.front() == '\"')
return TString(s);
else
return TString(me->getName().the().data() + " "_s + s);
},
[&](TSentence s) -> TString {
s.params.push_back(me->getName().the());
return s;
}
);
}
optional<TString> CreatureAttributes::getPetReaction(const Creature* me) const {
if (petReaction)
return getVerbalReaction(*petReaction, me);
return none;
}
void CreatureAttributes::chatReaction(Creature* me, Creature* other) {
if (me->isEnemy(other) && chatReactionHostile)
other->privateMessage(getVerbalReaction(*chatReactionHostile, me));
if (!me->isEnemy(other) && chatReactionFriendly)
other->privateMessage(getVerbalReaction(*chatReactionFriendly, me));
if (chatEffect)
chatEffect->apply(other->getPosition(), me);
}
bool CreatureAttributes::isAffected(LastingEffect effect, GlobalTime time) const {
//PROFILE;
if (auto suppressor = LastingEffects::getSuppressor(effect))
if (isAffected(*suppressor, time))
return false;
return lastingEffects[effect] > time || isAffectedPermanently(effect);
}
GlobalTime CreatureAttributes::getTimeOut(LastingEffect effect) const {
return lastingEffects[effect];
}
void CreatureAttributes::copyLastingEffects(const CreatureAttributes& attr) {
lastingEffects = attr.lastingEffects;
permanentEffects[LastingEffect::STEED] = attr.permanentEffects[LastingEffect::STEED];
}
bool CreatureAttributes::considerTimeout(LastingEffect effect, GlobalTime current) {
if (lastingEffects[effect] > GlobalTime(0) && lastingEffects[effect] <= current) {
clearLastingEffect(effect);
if (!isAffected(effect, current))
return true;
}
return false;
}
void CreatureAttributes::addLastingEffect(LastingEffect effect, GlobalTime endTime) {
if (lastingEffects[effect] < endTime)
lastingEffects[effect] = endTime;
}
static bool consumeProb() {
return true;
}
template <typename T>
void consumeAttr(T& mine, const T& his, vector<TString>& adjectives, const TString& adj, const int& cap) {
int hisCapped = (his > cap) ? cap : his;
if (consumeProb() && mine < hisCapped) {
mine = hisCapped;
if (!adj.empty())
adjectives.push_back(adj);
}
}
void consumeAttr(Gender& mine, const Gender& his, vector<TString>& adjectives) {
if (consumeProb() && mine != his) {
mine = his;
adjectives.emplace_back(get(mine, TStringId("MORE_MASCULINE"), TStringId("MORE_FEMININE"), TStringId("MORE_NEUTER")));
}
}
template <typename T>
void consumeAttr(heap_optional<T>& mine, const heap_optional<T>& his, vector<TString>& adjectives) {
if (consumeProb() && !mine && his)
mine = *his;
}
void CreatureAttributes::consumeEffects(Creature* self, const EnumMap<LastingEffect, int>& permanentEffects) {
for (LastingEffect effect : ENUM_ALL(LastingEffect))
if (permanentEffects[effect] > 0 && !isAffectedPermanently(effect) && consumeProb() &&
LastingEffects::canConsume(effect)) {
self->addPermanentEffect(effect);
}
}
void CreatureAttributes::consume(Creature* self, CreatureAttributes& other) {
self->you(MsgType::CONSUME, other.name.the());
self->addPersonalEvent(TSentence("ABSORBS", self->getName().a(), other.name.a()));
vector<TString> adjectives;
body->consumeBodyParts(self, other.getBody(), adjectives);
auto factory = self->getGame()->getContentFactory();
for (auto& t: factory->attrInfo)
consumeAttr(attr[t.first], other.attr[t.first], adjectives,
TSentence("MORE_ADJECTIVE", t.second.adjective), t.second.absorptionCap);
consumeAttr(passiveAttack, other.passiveAttack, adjectives);
consumeAttr(gender, other.gender, adjectives);
if (!adjectives.empty()) {
self->you(MsgType::BECOME, combineWithAnd(adjectives));
self->addPersonalEvent(TSentence("BECOMES", getName().the(), combineWithAnd(adjectives)));
}
consumeEffects(self, other.permanentEffects);
}
bool CreatureAttributes::isBoulder() const {
return boulder;
}
ViewObject CreatureAttributes::createViewObject() const {
return ViewObject(viewId, ViewLayer::CREATURE, name.bare());
}
const heap_optional<ViewObject>& CreatureAttributes::getIllusionViewObject() const {
return illusionViewObject;
}
heap_optional<ViewObject>& CreatureAttributes::getIllusionViewObject() {
return illusionViewObject;
}
bool CreatureAttributes::canEquip() const {
return !cantEquip;
}
bool CreatureAttributes::isAffectedPermanently(LastingEffect effect) const {
return permanentEffects[effect] > 0;
}
void CreatureAttributes::clearLastingEffect(LastingEffect effect) {
lastingEffects[effect] = GlobalTime(0);
}
void CreatureAttributes::addPermanentEffect(LastingEffect effect, int count) {
permanentEffects[effect] += count;
}
void CreatureAttributes::removePermanentEffect(LastingEffect effect, int count) {
permanentEffects[effect] -= count;
}
const MinionActivityMap& CreatureAttributes::getMinionActivities() const {
return minionActivities;
}
MinionActivityMap& CreatureAttributes::getMinionActivities() {
return minionActivities;
}
bool CreatureAttributes::getCanJoinCollective() const {
return canJoinCollective;
}
void CreatureAttributes::setCanJoinCollective(bool b) {
canJoinCollective = b;
}
optional<BuffId> CreatureAttributes::getHatedByEffect() const {
return hatedByEffect;
}
#include "pretty_archive.h"
template<> void CreatureAttributes::serialize(PrettyInputArchive& ar1, unsigned version) {
map<LastingOrBuff, int> permanentEffects;
serializeImpl(ar1, version);
ar1(OPTION(permanentEffects));
ar1(endInput());
for (auto& elem : permanentEffects)
elem.first.visit(
[&](LastingEffect e) { this->permanentEffects[e] = elem.second ; },
[&](BuffId e) { this->permanentBuffs.push_back(e); }
);
initializeLastingEffects();
}