forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature.cpp
2566 lines (2305 loc) · 85.5 KB
/
creature.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
/* 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.h"
#include "creature_factory.h"
#include "level.h"
#include "ranged_weapon.h"
#include "statistics.h"
#include "options.h"
#include "model.h"
#include "effect.h"
#include "item_factory.h"
#include "location.h"
#include "controller.h"
#include "player_message.h"
#include "attack.h"
#include "vision.h"
#include "square_type.h"
#include "square_apply_type.h"
#include "equipment.h"
#include "shortest_path.h"
#include "spell_map.h"
#include "minion_task_map.h"
#include "tribe.h"
#include "creature_attributes.h"
#include "position.h"
template <class Archive>
void Creature::MoraleOverride::serialize(Archive& ar, const unsigned int version) {
}
SERIALIZABLE(Creature::MoraleOverride);
template <class Archive>
void Creature::serialize(Archive& ar, const unsigned int version) {
ar
& SUBCLASS(Renderable)
& SUBCLASS(UniqueEntity)
& SVAR(attributes)
& SVAR(position)
& SVAR(model)
& SVAR(time)
& SVAR(equipment)
& SVAR(shortestPath)
& SVAR(knownHiding)
& SVAR(tribe)
& SVAR(health)
& SVAR(morale)
& SVAR(deathTime)
& SVAR(lastTick)
& SVAR(collapsed)
& SVAR(hidden)
& SVAR(lastAttacker)
& SVAR(deathReason)
& SVAR(swapPositionCooldown)
& SVAR(unknownAttacker)
& SVAR(privateEnemies)
& SVAR(holding)
& SVAR(controller)
& SVAR(controllerStack)
& SVAR(creatureVisions)
& SVAR(kills)
& SVAR(difficultyPoints)
& SVAR(points)
& SVAR(numAttacksThisTurn)
& SVAR(moraleOverrides)
& SVAR(visibleEnemies)
& SVAR(visibleCreatures)
& SVAR(vision)
& SVAR(personalEvents)
& SVAR(lastCombatTime);
}
SERIALIZABLE(Creature);
SERIALIZATION_CONSTRUCTOR_IMPL(Creature);
Creature::Creature(const ViewObject& object, Tribe* t, const CreatureAttributes& attr,
const ControllerFactory& f)
: Renderable(object), attributes(attr), tribe(t), controller(f.get(this)) {
for (auto id : ENUM_ALL(AttrType))
CHECK(attributes->attr[id] > 0);
CHECK(getUniqueId() != 0);
modViewObject().setCreatureId(getUniqueId());
updateVision();
}
Creature::Creature(Tribe* t, const CreatureAttributes& attr, const ControllerFactory& f)
: Creature(ViewObject(*attr.viewId, ViewLayer::CREATURE, (*attr.name).bare()), t, attr, f) {
}
Creature::~Creature() {
}
vector<vector<Creature*>> Creature::stack(const vector<Creature*>& creatures) {
map<string, vector<Creature*>> stacks;
for (Creature* c : creatures)
stacks[c->getSpeciesName()].push_back(c);
return getValues(stacks);
}
const ViewObject& Creature::getViewObjectFor(const Tribe* observer) const {
if (attributes->illusionViewObject && observer->isEnemy(this))
return *attributes->illusionViewObject;
else
return getViewObject();
}
bool Creature::isFireResistant() const {
return attributes->fireCreature || isAffected(LastingEffect::FIRE_RESISTANT);
}
void Creature::addSpell(Spell* spell) {
attributes->getSpellMap().add(spell);
}
vector<Spell*> Creature::getSpells() const {
return attributes->getSpellMap().getAll();
}
double Creature::getSpellDelay(Spell* spell) const {
CHECK(!isReady(spell));
return attributes->getSpellMap().getReadyTime(spell) - getTime();
}
bool Creature::isReady(Spell* spell) const {
return attributes->getSpellMap().getReadyTime(spell) < getTime();
}
static double getWillpowerMult(double sorcerySkill) {
return 2 * pow(0.25, sorcerySkill);
}
CreatureAction Creature::castSpell(Spell* spell) const {
if (!attributes->getSpellMap().contains(spell))
return CreatureAction("You don't know this spell.");
CHECK(!spell->isDirected());
if (!isReady(spell))
return CreatureAction("You can't cast this spell yet.");
return CreatureAction(this, [=] (Creature *c) {
spell->addMessage(c);
Effect::applyToCreature(c, spell->getEffectType(), EffectStrength::NORMAL);
model->getStatistics().add(StatId::SPELL_CAST);
c->attributes->getSpellMap().setReadyTime(spell, getTime() + spell->getDifficulty()
* getWillpowerMult(getSkillValue(Skill::get(SkillId::SORCERY))));
c->spendTime(1);
});
}
CreatureAction Creature::castSpell(Spell* spell, Vec2 dir) const {
CHECK(attributes->getSpellMap().contains(spell));
CHECK(spell->isDirected());
CHECK(dir.length8() == 1);
if (!isReady(spell))
return CreatureAction("You can't cast this spell yet.");
return CreatureAction(this, [=] (Creature *c) {
monsterMessage(getName().the() + " casts a spell");
playerMessage("You cast " + spell->getName());
Effect::applyDirected(c, dir, spell->getDirEffectType(), EffectStrength::NORMAL);
model->getStatistics().add(StatId::SPELL_CAST);
c->attributes->getSpellMap().setReadyTime(spell, getTime() + spell->getDifficulty()
* getWillpowerMult(getSkillValue(Skill::get(SkillId::SORCERY))));
c->spendTime(1);
});
}
void Creature::addCreatureVision(CreatureVision* creatureVision) {
creatureVisions.push_back(creatureVision);
}
void Creature::removeCreatureVision(CreatureVision* vision) {
removeElement(creatureVisions, vision);
}
void Creature::pushController(PController ctrl) {
controllerStack.push_back(std::move(controller));
setController(std::move(ctrl));
}
void Creature::setController(PController ctrl) {
if (ctrl->isPlayer())
modViewObject().setModifier(ViewObject::Modifier::PLAYER);
controller = std::move(ctrl);
getLevel()->updatePlayer();
}
void Creature::popController() {
if (controller->isPlayer())
modViewObject().removeModifier(ViewObject::Modifier::PLAYER);
CHECK(!controllerStack.empty());
controller = std::move(controllerStack.back());
controllerStack.pop_back();
getLevel()->updatePlayer();
}
bool Creature::isDead() const {
return !!deathTime;
}
double Creature::getDeathTime() const {
return *deathTime;
}
const Creature* Creature::getLastAttacker() const {
return lastAttacker;
}
optional<string> Creature::getDeathReason() const {
if (deathReason)
return deathReason;
if (lastAttacker)
return "killed by " + lastAttacker->getName().a();
return none;
}
vector<const Creature*> Creature::getKills() const {
return kills;
}
void Creature::spendTime(double t) {
time += 100.0 * t / (double) getAttr(AttrType::SPEED);
hidden = false;
}
CreatureAction Creature::forceMove(Vec2 dir) const {
return forceMove(getPosition().plus(dir));
}
CreatureAction Creature::forceMove(Position pos) const {
const_cast<Creature*>(this)->forceMovement = true;
CreatureAction action = move(pos);
const_cast<Creature*>(this)->forceMovement = false;
if (action)
return action.prepend([this] (Creature* c) { c->forceMovement = true; })
.append([this] (Creature* c) { c->forceMovement = false; });
else
return action;
}
CreatureAction Creature::move(Vec2 dir) const {
return move(getPosition().plus(dir));
}
CreatureAction Creature::move(Position pos) const {
Vec2 direction = getPosition().getDir(pos);
if (holding)
return CreatureAction("You can't break free!");
if (direction.length8() != 1)
return CreatureAction();
if (!position.canMoveCreature(direction)) {
auto action = swapPosition(direction);
if (!action) // this is so the player gets relevant info why the move failed
return action;
}
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " moving " << direction;
if (isAffected(LastingEffect::ENTANGLED) || isAffected(LastingEffect::TIED_UP)) {
playerMessage("You can't break free!");
self->spendTime(1);
return;
}
if (position.canMoveCreature(direction))
self->position.moveCreature(direction);
else
swapPosition(direction).perform(self);
self->attributes->stationary = false;
double oldTime = getTime();
if (collapsed) {
you(MsgType::CRAWL, getPosition().getName());
self->spendTime(3);
} else
self->spendTime(1);
self->modViewObject().addMovementInfo({direction, oldTime, getTime(), ViewObject::MovementInfo::MOVE});
});
}
void Creature::displace(double time, Vec2 dir) {
position.moveCreature(dir);
controller->onDisplaced();
modViewObject().addMovementInfo({dir, time, time + 1, ViewObject::MovementInfo::MOVE});
}
int Creature::getDebt(const Creature* debtor) const {
return controller->getDebt(debtor);
}
bool Creature::canTakeItems(const vector<Item*>& items) const {
return isHumanoid();
}
void Creature::takeItems(vector<PItem> items, const Creature* from) {
vector<Item*> ref = extractRefs(items);
equipment->addItems(std::move(items));
controller->onItemsGiven(ref, from);
}
void Creature::you(MsgType type, const vector<string>& param) const {
controller->you(type, param);
}
void Creature::you(MsgType type, const string& param) const {
controller->you(type, param);
}
void Creature::you(const string& param) const {
controller->you(param);
}
void Creature::playerMessage(const PlayerMessage& message) const {
controller->privateMessage(message);
}
Controller* Creature::getController() {
return controller.get();
}
bool Creature::hasFreeMovement() const {
return !isAffected(LastingEffect::SLEEP) &&
!isAffected(LastingEffect::STUNNED) &&
!isAffected(LastingEffect::ENTANGLED) &&
!isAffected(LastingEffect::TIED_UP);
}
CreatureAction Creature::swapPosition(Vec2 direction, bool force) const {
if (Creature* other = getPosition().plus(direction).getCreature())
return swapPosition(other, force);
else
return CreatureAction();
}
CreatureAction Creature::swapPosition(Creature* other, bool force) const {
Vec2 direction = position.getDir(other->getPosition());
CHECK(direction.length8() == 1);
if (!other->hasFreeMovement() && !force)
return CreatureAction(other->getName().the() + " cannot move.");
if ((swapPositionCooldown && !isPlayer()) || other->attributes->stationary || other->isInvincible() ||
(other->isPlayer() && !force) || (other->isEnemy(this) && !force) ||
!other->getPosition().canEnterEmpty(this) || !getPosition().canEnterEmpty(other))
return CreatureAction();
return CreatureAction(this, [=](Creature* self) {
self->swapPositionCooldown = 4;
if (!force)
other->playerMessage("Excuse me!");
playerMessage("Excuse me!");
self->position.swapCreatures(other);
other->modViewObject().addMovementInfo({-direction, getTime(), other->getTime(),
ViewObject::MovementInfo::MOVE});
});
}
void Creature::makeMove() {
numAttacksThisTurn = 0;
CHECK(!isDead());
if (holding && holding->isDead())
holding = nullptr;
if (isAffected(LastingEffect::SLEEP)) {
controller->sleeping();
spendTime(1);
return;
}
if (isAffected(LastingEffect::STUNNED)) {
spendTime(1);
return;
}
updateVisibleCreatures();
updateViewObject();
if (swapPositionCooldown)
--swapPositionCooldown;
MEASURE(controller->makeMove(), "creature move time");
Debug() << getName().bare() << " morale " << getMorale();
if (!hidden)
modViewObject().removeModifier(ViewObject::Modifier::HIDDEN);
unknownAttacker.clear();
if (attributes->fireCreature && Random.roll(5))
getPosition().setOnFire(1);
}
CreatureAction Creature::wait() const {
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " waiting";
bool keepHiding = hidden;
self->spendTime(1);
self->hidden = keepHiding;
});
}
const Equipment& Creature::getEquipment() const {
return *equipment;
}
Equipment& Creature::getEquipment() {
return *equipment;
}
vector<PItem> Creature::steal(const vector<Item*> items) {
return equipment->removeItems(items);
}
Item* Creature::getAmmo() const {
for (Item* item : equipment->getItems())
if (item->getClass() == ItemClass::AMMO)
return item;
return nullptr;
}
Level* Creature::getLevel() const {
return getPosition().getLevel();
}
Position Creature::getPosition() const {
return position;
}
void Creature::globalMessage(const PlayerMessage& playerCanSee) const {
globalMessage(playerCanSee, "");
}
void Creature::globalMessage(const PlayerMessage& playerCanSee, const PlayerMessage& cant) const {
position.globalMessage(this, playerCanSee, cant);
}
void Creature::monsterMessage(const PlayerMessage& playerCanSee, const PlayerMessage& cant) const {
if (!isPlayer())
position.globalMessage(this, playerCanSee, cant);
}
void Creature::monsterMessage(const PlayerMessage& playerCanSee) const {
monsterMessage(playerCanSee, "");
}
void Creature::addSkill(Skill* skill) {
if (!hasSkill(skill)) {
attributes->skills.insert(skill->getId());
playerMessage(skill->getHelpText());
}
}
bool Creature::hasSkill(Skill* skill) const {
return attributes->skills.hasDiscrete(skill->getId());
}
double Creature::getSkillValue(const Skill* skill) const {
return attributes->skills.getValue(skill->getId());
}
const EnumSet<SkillId>& Creature::getDiscreteSkills() const {
return attributes->skills.getAllDiscrete();
}
vector<Item*> Creature::getPickUpOptions() const {
if (!isHumanoid())
return vector<Item*>();
else
return getPosition().getItems();
}
string Creature::getPluralTheName(Item* item, int num) const {
if (num == 1)
return item->getTheName(false, isBlind());
else
return toString(num) + " " + item->getTheName(true, isBlind());
}
string Creature::getPluralAName(Item* item, int num) const {
if (num == 1)
return item->getAName(false, isBlind());
else
return toString(num) + " " + item->getAName(true, isBlind());
}
CreatureAction Creature::pickUp(const vector<Item*>& items) const {
if (!isHumanoid())
return CreatureAction("You can't pick up anything!");
double weight = getInventoryWeight();
for (Item* it : items)
weight += it->getWeight();
if (weight > 2 * getModifier(ModifierType::INV_LIMIT))
return CreatureAction("You are carrying too much to pick this up.");
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " pickup ";
for (auto stack : stackItems(items)) {
monsterMessage(getName().the() + " picks up " + getPluralAName(stack[0], stack.size()));
playerMessage("You pick up " + getPluralTheName(stack[0], stack.size()));
}
self->equipment->addItems(self->getPosition().removeItems(items));
if (getInventoryWeight() > getModifier(ModifierType::INV_LIMIT))
playerMessage("You are overloaded.");
GlobalEvents.addPickupEvent(this, items);
self->spendTime(1);
});
}
vector<vector<Item*>> Creature::stackItems(vector<Item*> items) const {
map<string, vector<Item*> > stacks = groupBy<Item*, string>(items,
[this] (Item* const& item) { return item->getNameAndModifiers(false, isBlind()); });
return getValues(stacks);
}
CreatureAction Creature::drop(const vector<Item*>& items) const {
if (!isHumanoid())
return CreatureAction("You can't drop this item!");
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " drop";
for (auto stack : stackItems(items)) {
monsterMessage(getName().the() + " drops " + getPluralAName(stack[0], stack.size()));
playerMessage("You drop " + getPluralTheName(stack[0], stack.size()));
}
for (auto item : items) {
self->getPosition().dropItem(self->equipment->removeItem(item));
}
GlobalEvents.addDropEvent(this, items);
self->spendTime(1);
});
}
void Creature::drop(vector<PItem> items) {
getPosition().dropItems(std::move(items));
}
bool Creature::canEquipIfEmptySlot(const Item* item, string* reason) const {
if (!isHumanoid()) {
if (reason)
*reason = "Only humanoids can equip items!";
return false;
}
if (attributes->cantEquip) {
if (reason)
*reason = "You can't equip items!";
return false;
}
if (numGood(BodyPart::ARM) == 0) {
if (reason)
*reason = "You have no healthy arms!";
return false;
}
if (numGood(BodyPart::ARM) == 1 && item->isWieldedTwoHanded()) {
if (reason)
*reason = "You need two hands to wield " + item->getAName() + "!";
return false;
}
return item->canEquip();
}
bool Creature::canEquip(const Item* item) const {
return canEquipIfEmptySlot(item, nullptr) && equipment->canEquip(item);
}
bool Creature::isEquipmentAppropriate(const Item* item) const {
return item->getClass() != ItemClass::WEAPON || item->getMinStrength() <= getAttr(AttrType::STRENGTH);
}
CreatureAction Creature::equip(Item* item) const {
string reason;
if (!canEquipIfEmptySlot(item, &reason))
return CreatureAction(reason);
if (contains(equipment->getItem(item->getEquipmentSlot()), item))
return CreatureAction();
return CreatureAction(this, [=](Creature *self) {
Debug() << getName().the() << " equip " << item->getName();
EquipmentSlot slot = item->getEquipmentSlot();
if (self->equipment->getItem(slot).size() >= self->equipment->getMaxItems(slot)) {
Item* previousItem = self->equipment->getItem(slot)[0];
self->equipment->unequip(previousItem);
previousItem->onUnequip(self);
}
self->equipment->equip(item, slot);
playerMessage("You equip " + item->getTheName(false, isBlind()));
monsterMessage(getName().the() + " equips " + item->getAName());
item->onEquip(self);
if (model)
model->onEquip(self, item);
self->spendTime(1);
});
}
CreatureAction Creature::unequip(Item* item) const {
if (!equipment->isEquiped(item))
return CreatureAction("This item is not equiped.");
if (!isHumanoid())
return CreatureAction("You can't remove this item!");
if (numGood(BodyPart::ARM) == 0)
return CreatureAction("You have no healthy arms!");
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " unequip";
CHECK(equipment->isEquiped(item)) << "Item not equiped.";
EquipmentSlot slot = item->getEquipmentSlot();
self->equipment->unequip(item);
playerMessage("You " + string(slot == EquipmentSlot::WEAPON ? " sheathe " : " remove ") +
item->getTheName(false, isBlind()));
monsterMessage(getName().the() + (slot == EquipmentSlot::WEAPON ? " sheathes " : " removes ") +
item->getAName());
item->onUnequip(self);
self->spendTime(1);
});
}
CreatureAction Creature::heal(Vec2 direction) const {
const Creature* other = getPosition().plus(direction).getCreature();
if (!hasSkill(Skill::get(SkillId::HEALING)) || !other || other->getHealth() >= 0.9999 || other == this)
return CreatureAction();
return CreatureAction(this, [=](Creature* self) {
Creature* other = getPosition().plus(direction).getCreature();
other->playerMessage("\"Let me help you my friend.\"");
other->you(MsgType::ARE, "healed by " + getName().the());
other->heal();
self->spendTime(1);
});
}
CreatureAction Creature::bumpInto(Vec2 direction) const {
if (const Creature* other = getPosition().plus(direction).getCreature())
return CreatureAction(this, [=](Creature* self) {
other->controller->onBump(self);
self->spendTime(1);
});
else
return CreatureAction();
}
CreatureAction Creature::applySquare() const {
if (getPosition().getApplyType(this))
return CreatureAction(this, [=](Creature* self) {
Debug() << getName().the() << " applying " << getPosition().getName();
self->getPosition().onApply(self);
self->spendTime(self->getPosition().getApplyTime());
});
else
return CreatureAction();
}
CreatureAction Creature::hide() const {
if (!hasSkill(Skill::get(SkillId::AMBUSH)))
return CreatureAction("You don't have this skill.");
if (!getPosition().canHide())
return CreatureAction("You can't hide here.");
return CreatureAction(this, [=](Creature* self) {
playerMessage("You hide behind the " + getPosition().getName());
self->knownHiding.clear();
self->modViewObject().setModifier(ViewObject::Modifier::HIDDEN);
for (Creature* other : getLevel()->getAllCreatures())
if (other->canSee(this) && other->isEnemy(this)) {
self->knownHiding.insert(other);
if (!isBlind())
you(MsgType::CAN_SEE_HIDING, other->getName().the());
}
self->spendTime(1);
self->hidden = true;
});
}
CreatureAction Creature::chatTo(Creature* other) const {
CHECK(other);
return CreatureAction(this, [=](Creature* self) {
playerMessage("You chat with " + other->getName().the());
other->onChat(self);
self->spendTime(1);
});
return CreatureAction();
}
void Creature::onChat(Creature* from) {
if (isEnemy(from) && attributes->chatReactionHostile) {
if (attributes->chatReactionHostile->front() == '\"')
from->playerMessage(*attributes->chatReactionHostile);
else
from->playerMessage(getName().the() + " " + *attributes->chatReactionHostile);
}
if (!isEnemy(from) && attributes->chatReactionFriendly) {
if (attributes->chatReactionFriendly->front() == '\"')
from->playerMessage(*attributes->chatReactionFriendly);
else
from->playerMessage(getName().the() + " " + *attributes->chatReactionFriendly);
}
}
void Creature::learnLocation(const Location* loc) {
controller->learnLocation(loc);
}
CreatureAction Creature::stealFrom(Vec2 direction, const vector<Item*>& items) const {
if (getPosition().plus(direction).getCreature())
return CreatureAction(this, [=](Creature *self) {
Creature* other = NOTNULL(getPosition().plus(direction).getCreature());
self->equipment->addItems(other->steal(items));
});
return CreatureAction();
}
bool Creature::isHidden() const {
return hidden;
}
bool Creature::knowsHiding(const Creature* c) const {
return knownHiding.count(const_cast<Creature*>(c)) == 1; // OBSOLETE: change to entity set
}
bool Creature::affects(LastingEffect effect) const {
switch (effect) {
case LastingEffect::RAGE:
case LastingEffect::PANIC: return !isAffected(LastingEffect::SLEEP);
case LastingEffect::POISON: return !isAffected(LastingEffect::POISON_RESISTANT) && !isNotLiving();
case LastingEffect::TIED_UP:
case LastingEffect::ENTANGLED: return isCorporal();
default: return true;
}
}
void Creature::onAffected(LastingEffect effect, bool msg) {
switch (effect) {
case LastingEffect::FLYING:
if (msg) you(MsgType::ARE, "flying!");
break;
case LastingEffect::STUNNED:
if (msg) you(MsgType::ARE, "stunned");
break;
case LastingEffect::PANIC:
removeEffect(LastingEffect::RAGE, false);
if (msg) you(MsgType::PANIC, "");
break;
case LastingEffect::RAGE:
removeEffect(LastingEffect::PANIC, false);
if (msg) you(MsgType::RAGE, "");
break;
case LastingEffect::HALLU:
if (!isBlind() && msg)
playerMessage("The world explodes into colors!");
break;
case LastingEffect::BLIND:
if (msg) you(MsgType::ARE, "blind!");
modViewObject().setModifier(ViewObject::Modifier::BLIND);
break;
case LastingEffect::INVISIBLE:
if (!isBlind() && msg)
you(MsgType::TURN_INVISIBLE, "");
modViewObject().setModifier(ViewObject::Modifier::INVISIBLE);
break;
case LastingEffect::POISON:
if (msg) you(MsgType::ARE, "poisoned");
modViewObject().setModifier(ViewObject::Modifier::POISONED);
break;
case LastingEffect::STR_BONUS: if (msg) you(MsgType::FEEL, "stronger"); break;
case LastingEffect::DEX_BONUS: if (msg) you(MsgType::FEEL, "more agile"); break;
case LastingEffect::SPEED:
if (msg) you(MsgType::ARE, "moving faster");
removeEffect(LastingEffect::SLOWED, false);
break;
case LastingEffect::SLOWED:
if (msg) you(MsgType::ARE, "moving more slowly");
removeEffect(LastingEffect::SPEED, false);
break;
case LastingEffect::TIED_UP: if (msg) you(MsgType::ARE, "tied up"); break;
case LastingEffect::ENTANGLED: if (msg) you(MsgType::ARE, "entangled in a web"); break;
case LastingEffect::SLEEP: if (msg) you(MsgType::FALL_ASLEEP, ""); break;
case LastingEffect::POISON_RESISTANT:
if (msg) you(MsgType::ARE, "now poison resistant");
removeEffect(LastingEffect::POISON, true);
break;
case LastingEffect::FIRE_RESISTANT: if (msg) you(MsgType::ARE, "now fire resistant"); break;
case LastingEffect::INSANITY: if (msg) you(MsgType::BECOME, "insane"); break;
case LastingEffect::MAGIC_SHIELD: if (msg) you(MsgType::FEEL, "protected"); break;
case LastingEffect::DARKNESS_SOURCE: break;
}
}
void Creature::onRemoved(LastingEffect effect, bool msg) {
switch (effect) {
case LastingEffect::POISON:
if (msg)
you(MsgType::ARE, "cured from poisoning");
modViewObject().removeModifier(ViewObject::Modifier::POISONED);
break;
default: onTimedOut(effect, msg); break;
}
}
void Creature::onTimedOut(LastingEffect effect, bool msg) {
switch (effect) {
case LastingEffect::STUNNED: break;
case LastingEffect::SLOWED: if (msg) you(MsgType::ARE, "moving faster again"); break;
case LastingEffect::SLEEP: if (msg) you(MsgType::WAKE_UP, ""); break;
case LastingEffect::SPEED: if (msg) you(MsgType::ARE, "moving more slowly again"); break;
case LastingEffect::STR_BONUS: if (msg) you(MsgType::ARE, "weaker again"); break;
case LastingEffect::DEX_BONUS: if (msg) you(MsgType::ARE, "less agile again"); break;
case LastingEffect::PANIC:
case LastingEffect::RAGE:
case LastingEffect::HALLU: if (msg) playerMessage("Your mind is clear again"); break;
case LastingEffect::ENTANGLED: if (msg) you(MsgType::BREAK_FREE, "the web"); break;
case LastingEffect::TIED_UP: if (msg) you(MsgType::BREAK_FREE, ""); break;
case LastingEffect::BLIND:
if (msg)
you("can see again");
modViewObject().removeModifier(ViewObject::Modifier::BLIND);
break;
case LastingEffect::INVISIBLE:
if (msg)
you(MsgType::TURN_VISIBLE, "");
modViewObject().removeModifier(ViewObject::Modifier::INVISIBLE);
break;
case LastingEffect::POISON:
if (msg)
you(MsgType::ARE, "no longer poisoned");
modViewObject().removeModifier(ViewObject::Modifier::POISONED);
break;
case LastingEffect::POISON_RESISTANT: if (msg) you(MsgType::ARE, "no longer poison resistant"); break;
case LastingEffect::FIRE_RESISTANT: if (msg) you(MsgType::ARE, "no longer fire resistant"); break;
case LastingEffect::FLYING:
if (msg) you(MsgType::FALL, "on the " + getPosition().getName());
bleed(0.1);
break;
case LastingEffect::INSANITY: if (msg) you(MsgType::BECOME, "sane again"); break;
case LastingEffect::MAGIC_SHIELD: if (msg) you(MsgType::FEEL, "less protected"); break;
case LastingEffect::DARKNESS_SOURCE: break;
}
}
void Creature::addEffect(LastingEffect effect, double time, bool msg) {
if (attributes->lastingEffects[effect] < getTime() + time && affects(effect)) {
if (!isAffected(effect))
onAffected(effect, msg);
attributes->lastingEffects[effect] = getTime() + time;
}
}
void Creature::removeEffect(LastingEffect effect, bool msg) {
if (!isAffected(effect))
return;
attributes->lastingEffects[effect] = 0;
if (!isAffected(effect))
onRemoved(effect, msg);
}
void Creature::addPermanentEffect(LastingEffect effect, bool msg) {
if (!isAffected(effect))
onAffected(effect, msg);
++attributes->permanentEffects[effect];
}
void Creature::removePermanentEffect(LastingEffect effect, bool msg) {
--attributes->permanentEffects[effect];
CHECK(attributes->permanentEffects[effect] >= 0);
if (!isAffected(effect))
onRemoved(effect, msg);
}
double Creature::getTimeRemaining(LastingEffect effect) const {
CHECK(isAffected(effect));
return attributes->lastingEffects[effect] - getTime();
}
bool Creature::isAffected(LastingEffect effect) const {
return attributes->lastingEffects[effect] >= getTime() || attributes->permanentEffects[effect] > 0;
}
bool Creature::isAffectedPermanently(LastingEffect effect) const {
return attributes->permanentEffects[effect] > 0;
}
bool Creature::isBlind() const {
return isAffected(LastingEffect::BLIND) || (numLost(BodyPart::HEAD) > 0 && numBodyParts(BodyPart::HEAD) == 0);
}
bool Creature::isDarknessSource() const {
return isAffected(LastingEffect::DARKNESS_SOURCE);
}
int attrBonus = 3;
map<BodyPart, int> dexPenalty {
{BodyPart::ARM, 2},
{BodyPart::LEG, 10},
{BodyPart::WING, 3},
{BodyPart::HEAD, 3}};
map<BodyPart, int> strPenalty {
{BodyPart::ARM, 2},
{BodyPart::LEG, 5},
{BodyPart::WING, 2},
{BodyPart::HEAD, 3}};
// penalty to strength and dexterity per extra attacker in a single turn
int simulAttackPen(int attackers) {
return max(0, (attackers - 1) * 2);
}
int Creature::getAttr(AttrType type) const {
int def = attributes->getRawAttr(type);
for (Item* item : equipment->getItems())
if (equipment->isEquiped(item))
def += CHECK_RANGE(item->getAttr(type), -10000000, 10000000, getName().bare());
switch (type) {
case AttrType::STRENGTH:
if (health < 1)
def *= 0.666 + health / 3;
if (isAffected(LastingEffect::STR_BONUS))
def += CHECK_RANGE(attrBonus, -10000000, 10000000, getName().bare());
for (auto elem : strPenalty)
def -= elem.second * (numInjured(elem.first) + numLost(elem.first));
def -= simulAttackPen(numAttacksThisTurn);
break;
case AttrType::DEXTERITY:
if (health < 1)
def *= 0.666 + health / 3;
if (isAffected(LastingEffect::DEX_BONUS))
def += attrBonus;
for (auto elem : dexPenalty)
def -= elem.second * (numInjured(elem.first) + numLost(elem.first));
def -= simulAttackPen(numAttacksThisTurn);
break;
case AttrType::SPEED: {
double totWeight = getInventoryWeight();
if (!attributes->carryAnything && totWeight > getAttr(AttrType::STRENGTH))
def -= 20.0 * totWeight / def;
if (isAffected(LastingEffect::SLOWED))
def /= 1.5;
if (isAffected(LastingEffect::SPEED))
def *= 1.5;
CHECK(def > 0);
break;}
}
return max(0, def);
}
int Creature::accuracyBonus() const {
if (Item* weapon = getWeapon())
return -max(0, weapon->getMinStrength() - getAttr(AttrType::STRENGTH));
else
return 0;
}
int Creature::getModifier(ModifierType type) const {
int def = 0;
for (Item* item : equipment->getItems())
if (equipment->isEquiped(item))
def += CHECK_RANGE(item->getModifier(type), -10000000, 10000000, getName().bare());
for (SkillId skill : ENUM_ALL(SkillId))
def += CHECK_RANGE(Skill::get(skill)->getModifier(this, type), -10000000, 10000000, getName().bare());
switch (type) {
case ModifierType::FIRED_DAMAGE:
case ModifierType::THROWN_DAMAGE:
def += getAttr(AttrType::DEXTERITY);
if (isAffected(LastingEffect::PANIC))
def -= attrBonus;
if (isAffected(LastingEffect::RAGE))
def += attrBonus;
break;
case ModifierType::DAMAGE:
def += getAttr(AttrType::STRENGTH);
if (!getWeapon())
def += attributes->barehandedDamage;
if (isAffected(LastingEffect::PANIC))
def -= attrBonus;
if (isAffected(LastingEffect::RAGE))
def += attrBonus;
break;
case ModifierType::DEFENSE:
def += getAttr(AttrType::STRENGTH);
if (isAffected(LastingEffect::PANIC))
def += attrBonus;
if (isAffected(LastingEffect::RAGE))
def -= attrBonus;
if (isAffected(LastingEffect::SLEEP))
def *= 0.66;
if (isAffected(LastingEffect::MAGIC_SHIELD))
def += 20;
break;
case ModifierType::FIRED_ACCURACY:
case ModifierType::THROWN_ACCURACY:
def += getAttr(AttrType::DEXTERITY);
break;
case ModifierType::ACCURACY:
def += accuracyBonus();
def += getAttr(AttrType::DEXTERITY);
if (isAffected(LastingEffect::SLEEP))
def = 0;
break;
case ModifierType::INV_LIMIT:
if (attributes->carryAnything)
return 1000000;
return getAttr(AttrType::STRENGTH) * 2;
}
return max(0, def);
}
int Creature::getPoints() const {
return points;
}