forked from miki151/keeperrl
/
monster_ai.cpp
1105 lines (973 loc) · 32.9 KB
/
monster_ai.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
#include "stdafx.h"
#include "monster_ai.h"
#include "square.h"
#include "level.h"
#include "collective.h"
#include "village_control.h"
#include "task.h"
template <class Archive>
void MonsterAI::serialize(Archive& ar, const unsigned int version) {
ar& SVAR(behaviours)
& SVAR(weights)
& SVAR(creature)
& SVAR(pickItems);
CHECK_SERIAL;
}
SERIALIZABLE(MonsterAI);
template <class Archive>
void Behaviour::serialize(Archive& ar, const unsigned int version) {
ar & SVAR(creature);
CHECK_SERIAL;
}
Behaviour::Behaviour(Creature* c) : creature(c) {
}
const Creature* Behaviour::getClosestEnemy() {
int dist = 1000000000;
const Creature* result = nullptr;
for (const Creature* other : creature->getVisibleEnemies()) {
if ((other->getPosition() - creature->getPosition()).length8() < dist && other->getTribe() != Tribes::get(TribeId::PEST)) {
result = other;
dist = (other->getPosition() - creature->getPosition()).length8();
}
}
return result;
}
Item* Behaviour::getBestWeapon() {
Item* best = nullptr;
int damage = -1;
for (Item* item : creature->getEquipment().getItems(Item::typePredicate(ItemType::WEAPON)))
if (item->getModifier(AttrType::DAMAGE) > damage) {
damage = item->getModifier(AttrType::DAMAGE);
best = item;
}
return best;
}
MoveInfo Behaviour::tryToApplyItem(EffectType type, double maxTurns) {
for (int i : All(creature->getSpells())) {
SpellInfo spell = creature->getSpells()[i];
if (spell.type == type && creature->canCastSpell(i))
return { 1, [=]() {
creature->globalMessage(creature->getTheName() + " casts a spell.");
creature->castSpell(i);
}};
}
auto items = creature->getEquipment().getItems(Item::effectPredicate(type));
for (Item* item : items)
if (creature->canApplyItem(item) && item->getApplyTime() <= maxTurns)
return { 1, [=]() {
creature->globalMessage(creature->getTheName() + " " + item->getApplyMsgThirdPerson(),
item->getNoSeeApplyMsg());
creature->applyItem(item);
}};
return {0, nullptr};
}
class Heal : public Behaviour {
public:
Heal(Creature* c) : Behaviour(c) {}
virtual double itemValue(const Item* item) {
if (item->getEffectType() == EffectType::HEAL) {
return 0.5;
}
else
return 0;
}
virtual MoveInfo getMove() {
for (Vec2 v : Vec2::directions8())
if (creature->canHeal(v) && creature->isFriend(creature->getConstSquare(v)->getCreature())) {
creature->getConstSquare(v)->getCreature()->privateMessage("\"Let me help you my friend.\"");
creature->heal(v);
}
if (!creature->isHumanoid() || creature->getHealth() == 1)
return {0, nullptr};
MoveInfo move = tryToApplyItem(EffectType::HEAL, 1);
if (move.move)
return { min(1.0, 1.5 - creature->getHealth()), move.move };
move = tryToApplyItem(EffectType::HEAL, 3);
if (move.move)
return { 0.3 * min(1.0, 1.5 - creature->getHealth()), move.move };
if (creature->getConstSquare()->getApplyType(creature) == SquareApplyType::SLEEP)
return { 0.4 * min(1.0, 1.5 - creature->getHealth()), [this] {
creature->applySquare();
}};
Vec2 bedRadius(10, 10);
Level* l = creature->getLevel();
if (creature->canSleep()) {
if (!hasBed || hasBed->level != creature->getLevel()) {
for (Vec2 v : Rectangle(creature->getPosition() - bedRadius, creature->getPosition() + bedRadius))
if (l->inBounds(v) && l->getSquare(v)->getApplyType(creature) == SquareApplyType::SLEEP)
if (Optional<Vec2> move = creature->getMoveTowards(v))
return { 0.4 * min(1.0, 1.5 - creature->getHealth()), [=] {
creature->move(*move);
hasBed = {v, creature->getLevel() };
}};
} else
if (Optional<Vec2> move = creature->getMoveTowards(hasBed->pos))
return { 0.4 * min(1.0, 1.5 - creature->getHealth()), [=] {
creature->move(*move);
}};
}
return {0, nullptr};
}
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(hasBed);
CHECK_SERIAL;
}
SERIALIZATION_CONSTRUCTOR(Heal);
private:
struct BedInfo {
Vec2 pos;
const Level* level;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(pos) & BOOST_SERIALIZATION_NVP(level);
}
};
Optional<BedInfo> SERIAL(hasBed);
};
class Rest : public Behaviour {
public:
Rest(Creature* c) : Behaviour(c) {}
virtual MoveInfo getMove() {
return {0.1, [=] { creature->wait(); }};
}
SERIALIZATION_CONSTRUCTOR(Rest);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour);
}
};
class MoveRandomly : public Behaviour {
public:
MoveRandomly(Creature* c, int _memSize)
: Behaviour(c), memSize(_memSize) {}
virtual MoveInfo getMove() {
if (!visited(creature->getPosition()))
updateMem(creature->getPosition());
Vec2 direction(0, 0);
double val = 0.0001;
Vec2 pos = creature->getPosition();
for (Vec2 dir : Vec2::directions8(true))
if (!visited(pos + dir) && creature->canMove(dir)) {
direction = dir;
break;
}
if (direction == Vec2(0, 0))
for (Vec2 dir : Vec2::directions8(true))
if (creature->canMove(dir)) {
direction = dir;
break;
}
if (direction == Vec2(0, 0))
return {val, [this]() { creature->wait(); }};
else
return {val, [this, direction]() { creature->move(direction); updateMem(creature->getPosition());}};
}
void updateMem(Vec2 pos) {
memory.push_back(pos);
if (memory.size() > memSize)
memory.pop_front();
}
bool visited(Vec2 pos) {
return contains(memory, pos);
}
SERIALIZATION_CONSTRUCTOR(MoveRandomly);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(memory) & SVAR(memSize);
CHECK_SERIAL;
}
private:
deque<Vec2> SERIAL(memory);
int SERIAL(memSize);
};
class AttackPest : public Behaviour {
public:
AttackPest(Creature* c) : Behaviour(c) {}
virtual MoveInfo getMove() override {
if (creature->getTribe() == Tribes::get(TribeId::PEST))
return NoMove;
const Creature* other = nullptr;
for (Vec2 v : Vec2::directions8(true))
if (const Creature* c = creature->getConstSquare(v)->getCreature())
if (c->getTribe() == Tribes::get(TribeId::PEST)) {
other = c;
break;
}
if (!other)
return NoMove;
if (creature->canAttack(other))
return {1.0, [this, other]() {
creature->attack(other);
}};
else
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(AttackPest);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour);
}
};
class BirdFlyAway : public Behaviour {
public:
BirdFlyAway(Creature* c, double _maxDist) : Behaviour(c), maxDist(_maxDist) {}
virtual MoveInfo getMove() override {
const Creature* enemy = getClosestEnemy();
bool fly = Random.roll(15) || ( enemy && (enemy->getPosition() - creature->getPosition()).lengthD() < maxDist);
if (creature->canFlyAway() && fly)
return {1.0, [this] () {
creature->flyAway();
}};
else
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(BirdFlyAway);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(maxDist);
CHECK_SERIAL;
}
private:
double SERIAL(maxDist);
};
class GoldLust : public Behaviour {
public:
GoldLust(Creature* c) : Behaviour(c) {}
virtual double itemValue(const Item* item) {
if (item->getType() == ItemType::GOLD)
return 1;
else
return 0;
}
SERIALIZATION_CONSTRUCTOR(GoldLust);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour);
}
};
class Fighter : public Behaviour, public EventListener {
public:
Fighter(Creature* c, double powerR, bool _chase) : Behaviour(c), maxPowerRatio(powerR), chase(_chase) {
courage = c->getCourage();
}
virtual ~Fighter() {
}
virtual void onKillEvent(const Creature* victim, const Creature* killer) override {
if (victim != creature && victim->getName() == creature->getName() && creature->canSee(victim)) {
courage -= 0.1;
}
if (lastSeen && victim == lastSeen->creature)
lastSeen = Nothing();
}
virtual void onThrowEvent(const Creature* thrower, const Item* item, const vector<Vec2>& trajectory) override {
if (!creature->isHumanoid())
return;
string name = creature->getName();
if (contains(trajectory, creature->getPosition())
&& item->getName().size() > name.size() && item->getName().substr(0, name.size()) == name) {
creature->globalMessage(creature->getTheName() + " screams in terror!", "You hear a scream of terror.");
if (Random.roll(2))
creature->addEffect(Creature::RAGE, (30));
else
courage -= 0.5;
}
}
virtual const Level* getListenerLevel() const override {
return creature->getLevel();
}
virtual MoveInfo getMove() override {
const Creature* other = getClosestEnemy();
if (other != nullptr) {
double myDamage = creature->getAttr(AttrType::DAMAGE);
Item* weapon = getBestWeapon();
if (!creature->getWeapon() && weapon)
myDamage += weapon->getModifier(AttrType::DAMAGE);
double powerRatio = courage * myDamage / other->getAttr(AttrType::DAMAGE);
bool significantEnemy = myDamage < 5 * other->getAttr(AttrType::DAMAGE);
double weight = 1. - creature->getHealth() * 0.9;
if (powerRatio < maxPowerRatio)
weight += 2 - powerRatio * 2;
weight = min(1.0, max(0.0, weight));
if (creature->isAffected(Creature::PANIC))
weight = 1;
if (other->isAffected(Creature::SLEEP) || other->isStationary())
weight = 0;
Debug() << creature->getName() << " panic weight " << weight;
if (weight >= 0.5) {
double dist = creature->getPosition().dist8(other->getPosition());
if (dist < 7) {
if (dist == 1)
EventListener::addSurrenderEvent(creature, other);
if (MoveInfo move = getPanicMove(other, weight))
return move;
else
return getAttackMove(other, significantEnemy && chase);
}
return NoMove;
} else
return getAttackMove(other, significantEnemy && chase);
} else
return getLastSeenMove();
}
MoveInfo getPanicMove(const Creature* other, double weight) {
if (auto teleMove = tryToApplyItem(EffectType::TELEPORT, 1))
return {weight, teleMove.move};
if (other->getPosition().dist8(creature->getPosition()) > 3)
if (auto move = getFireMove(other->getPosition() - creature->getPosition()))
return {weight, move.move};
if (auto move = creature->getMoveAway(other->getPosition(), chase))
return {weight, [this, move, other] () {
EventListener::addCombatEvent(creature);
EventListener::addCombatEvent(other);
lastSeen = {creature->getPosition(), creature->getTime(), creature->getLevel(), LastSeen::PANIC, other};
creature->move(*move);
}};
else
return NoMove;
}
virtual double itemValue(const Item* item) {
if (contains({EffectType::INVISIBLE, EffectType::SLOW, EffectType::BLINDNESS, EffectType::SLEEP,
EffectType::POISON, EffectType::TELEPORT, EffectType::STR_BONUS, EffectType::DEX_BONUS},
item->getEffectType()))
return 1;
if (item->getType() == ItemType::AMMO && creature->hasSkill(Skill::archery))
return 0.1;
if (item->getType() != ItemType::WEAPON || !creature->hasSkillToUseWeapon(item))
return 0;
if (item->getModifier(AttrType::THROWN_DAMAGE) > 0)
return (double)item->getModifier(AttrType::THROWN_DAMAGE) / 50;
int damage = item->getModifier(AttrType::DAMAGE);
Item* best = getBestWeapon();
if (best && best != item && best->getModifier(AttrType::DAMAGE) >= damage)
return 0;
return (double)damage / 50;
}
bool checkFriendlyFire(Vec2 enemyDir) {
Vec2 dir = enemyDir.shorten();
for (Vec2 v = dir; v != enemyDir; v += dir) {
const Creature* c = creature->getConstSquare(v)->getCreature();
if (c && !creature->isEnemy(c))
return true;
}
return false;
}
double getThrowValue(Item* it) {
if (contains({EffectType::POISON, EffectType::SLOW, EffectType::BLINDNESS, EffectType::SLEEP},
it->getEffectType()))
return 100;
return it->getModifier(AttrType::THROWN_DAMAGE);
}
MoveInfo getThrowMove(Vec2 enemyDir) {
if (enemyDir.x != 0 && enemyDir.y != 0 && abs(enemyDir.x) != abs(enemyDir.y))
return {0, nullptr};
if (checkFriendlyFire(enemyDir))
return {0, nullptr};
Vec2 dir = enemyDir.shorten();
Item* best = nullptr;
int damage = 0;
auto items = creature->getEquipment().getItems([this](Item* item) {
return !creature->getEquipment().isEquiped(item);});
for (Item* item : items)
if (getThrowValue(item) > damage) {
damage = getThrowValue(item);
best = item;
}
if (best && creature->canThrowItem(best))
return {1.0, [this, dir, best]() {
EventListener::addCombatEvent(creature);
creature->globalMessage(creature->getTheName() + " throws " + best->getAName());
creature->throwItem(best, dir);
}};
else
return NoMove;
}
MoveInfo getFireMove(Vec2 dir) {
if (dir.x != 0 && dir.y != 0 && abs(dir.x) != abs(dir.y))
return {0, nullptr};
if (checkFriendlyFire(dir))
return {0, nullptr};
if (creature->canFire(dir.shorten()))
return {1.0, [this, dir]() {
EventListener::addCombatEvent(creature);
creature->globalMessage(creature->getTheName() + " fires an arrow ");
creature->fire(dir.shorten());
}};
return NoMove;
}
MoveInfo getLastSeenMove() {
if (!lastSeen) {
return NoMove;
}
double lastSeenTimeout = 20;
if (lastSeen->level != creature->getLevel() ||
lastSeen->time < creature->getTime() - lastSeenTimeout ||
lastSeen->pos == creature->getPosition()) {
lastSeen = Nothing();
return NoMove;
}
if (chase && lastSeen->type == LastSeen::ATTACK)
if (Optional<Vec2> move = creature->getMoveTowards(lastSeen->pos))
return {0.5, [this, move]() {
EventListener::addCombatEvent(creature);
Debug() << creature->getTheName() << " moving to last seen " << (lastSeen->pos - creature->getPosition());
creature->move(*move);
}};
if (lastSeen->type == LastSeen::PANIC && lastSeen->pos.dist8(creature->getPosition()) < 4)
if (Optional<Vec2> move = creature->getMoveAway(lastSeen->pos, chase))
return {0.5, [this, move]() {
EventListener::addCombatEvent(creature);
Debug() << creature->getTheName() << " escaping last seen " << (lastSeen->pos - creature->getPosition());
creature->move(*move);
}};
return NoMove;
}
MoveInfo getAttackMove(const Creature* other, bool chase) {
int radius = 4;
int distance = 10000;
CHECK(other);
if (other->isInvincible())
return NoMove;
Debug() << creature->getName() << " enemy " << other->getName();
Vec2 enemyDir = (other->getPosition() - creature->getPosition());
distance = enemyDir.length8();
if (creature->isHumanoid() && !creature->getEquipment().getItem(EquipmentSlot::WEAPON)) {
Item* weapon = getBestWeapon();
if (weapon != nullptr && creature->canEquip(weapon, nullptr))
return {3.0 / (2.0 + distance), [this, weapon, other]() {
EventListener::addCombatEvent(creature);
EventListener::addCombatEvent(other);
creature->globalMessage(creature->getTheName() + " draws " + weapon->getAName());
creature->equip(weapon);
}};
}
if (distance <= 5)
for (EffectType effect : {EffectType::INVISIBLE, EffectType::STR_BONUS, EffectType::DEX_BONUS,
EffectType::SPEED, EffectType::SUMMON_SPIRIT})
if (MoveInfo move = tryToApplyItem(effect, 1))
return move;
if (distance > 1) {
if (MoveInfo move = getFireMove(enemyDir))
return move;
if (MoveInfo move = getThrowMove(enemyDir))
return move;
if (chase && other->getTribe() != Tribes::get(TribeId::WILDLIFE)) {
Optional<Vec2> move = creature->getMoveTowards(creature->getPosition() + enemyDir);
lastSeen = Nothing();
if (move)
return {max(0., 1.0 - double(distance) / 10), [this, enemyDir, move, other]() {
EventListener::addCombatEvent(creature);
EventListener::addCombatEvent(other);
lastSeen = {creature->getPosition() + enemyDir, creature->getTime(), creature->getLevel(),
LastSeen::ATTACK, other};
creature->move(*move);
}};
}
}
if (distance == 1) {
if (creature->canAttack(other))
return {1.0, [this, other]() {
EventListener::addCombatEvent(creature);
EventListener::addCombatEvent(other);
creature->attack(other);
}};
}
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(Fighter);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(maxPowerRatio) & SVAR(courage)
& SVAR(chase) & SVAR(lastSeen);
CHECK_SERIAL;
}
private:
double SERIAL(maxPowerRatio);
double SERIAL(courage);
bool SERIAL(chase);
struct LastSeen {
Vec2 pos;
double time;
const Level* level;
enum { ATTACK, PANIC} type;
const Creature* creature;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& BOOST_SERIALIZATION_NVP(pos)
& BOOST_SERIALIZATION_NVP(time)
& BOOST_SERIALIZATION_NVP(level)
& BOOST_SERIALIZATION_NVP(type)
& BOOST_SERIALIZATION_NVP(creature);
}
};
Optional<LastSeen> SERIAL(lastSeen);
};
class GuardTarget : public Behaviour {
public:
GuardTarget(Creature* c, double minD, double maxD) : Behaviour(c), minDist(minD), maxDist(maxD) {}
SERIALIZATION_CONSTRUCTOR(GuardTarget);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(minDist) & SVAR(maxDist);
CHECK_SERIAL;
}
protected:
MoveInfo getMoveTowards(Vec2 target) {
double dist = (creature->getPosition() - target).lengthD();
if (dist <= minDist)
return NoMove;
double exp = 1.5;
double weight = pow((dist - minDist) / (maxDist - minDist), exp);
Optional<Vec2> move = creature->getMoveTowards(target);
if (move)
return {weight, [this, move] () {
creature->move(*move);
}};
else
return NoMove;
}
private:
double SERIAL(minDist);
double SERIAL(maxDist);
};
class GuardArea : public Behaviour {
public:
GuardArea(Creature* c, const Location* l) : Behaviour(c), location(l), area(l->getBounds()) {}
virtual MoveInfo getMove() override {
if (creature->getLevel() != location->getLevel())
return NoMove;
if (!creature->getPosition().inRectangle(area)) {
for (Vec2 v : Vec2::directions8())
if ((creature->getPosition() + v).inRectangle(area) && creature->canMove(v))
return {1.0, [this, v] () {
creature->move(v);
}};
Optional<Vec2> move = creature->getMoveTowards(
Vec2((area.getPX() + area.getKX()) / 2, (area.getPY() + area.getKY()) / 2));
if (move)
return {1.0, [this, move] () {
creature->move(*move);
}};
}
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(GuardArea);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(location) & SVAR(area);
CHECK_SERIAL;
}
private:
const Location* SERIAL(location);
Rectangle SERIAL(area);
};
class GuardSquare : public GuardTarget {
public:
GuardSquare(Creature* c, Vec2 _pos, double minDist, double maxDist) : GuardTarget(c, minDist, maxDist), pos(_pos) {}
virtual MoveInfo getMove() override {
return getMoveTowards(pos);
}
SERIALIZATION_CONSTRUCTOR(GuardSquare);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(GuardTarget) & SVAR(pos);
CHECK_SERIAL;
}
private:
Vec2 SERIAL(pos);
};
class Wait : public Behaviour {
public:
Wait(Creature* c) : Behaviour(c) {}
virtual MoveInfo getMove() override {
return {1.0, [this] () {
creature->wait();
}};
}
SERIALIZATION_CONSTRUCTOR(Wait);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour);
}
};
class DoorEater : public Behaviour {
public:
DoorEater(Creature* c) : Behaviour(c) {}
virtual MoveInfo getMove() override {
Optional<Vec2> closestDoor;
for (Vec2 v : Rectangle(-10, -10, 10, 10))
if (creature->getLevel()->inBounds(creature->getPosition() + v)
&& creature->getSquare(v)->canLock() && creature->getSquare(v)->canDestroy(creature))
if (!closestDoor || v.length8() < closestDoor->length8())
closestDoor = v;
if (closestDoor) {
if (closestDoor->length8() == 1)
return {1.0, [this, closestDoor] () {
creature->globalMessage(creature->getTheName() + " eats the door.", "You hear chewing.");
creature->destroy(*closestDoor);
}};
else if (auto move = creature->getMoveTowards(creature->getPosition() + *closestDoor))
return {1.0, [this, move] () {
creature->move(*move);
}};
}
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(DoorEater);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour);
}
};
class Summoned : public GuardTarget, public EventListener {
public:
Summoned(Creature* c, Creature* _target, double minDist, double maxDist, double ttl)
: GuardTarget(c, minDist, maxDist), target(_target), dieTime(target->getTime() + ttl) {
}
virtual ~Summoned() {
}
virtual void onChangeLevelEvent(const Creature* c, const Level* from,
Vec2 pos, const Level* to, Vec2 toPos) override {
if (c == target)
levelChanges[from] = pos;
}
virtual MoveInfo getMove() override {
if (target->isDead() || creature->getTime() > dieTime) {
return {1.0, [=] {
creature->die(nullptr);
}};
}
if (target->getLevel() == creature->getLevel()) {
if (MoveInfo move = getMoveTowards(target->getPosition()))
return move.setValue(0.5);
else
return NoMove;
}
if (!levelChanges.count(creature->getLevel()))
return NoMove;
Vec2 stairs = levelChanges.at(creature->getLevel());
if (stairs == creature->getPosition() && creature->getSquare()->getApplyType(creature))
return {0.5, [=] {
creature->applySquare();
}};
else
return getMoveTowards(stairs);
}
SERIALIZATION_CONSTRUCTOR(Summoned);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& SUBCLASS(GuardTarget)
& SUBCLASS(EventListener)
& SVAR(target)
& SVAR(levelChanges)
& SVAR(dieTime);
CHECK_SERIAL;
}
private:
Creature* SERIAL(target);
map<const Level*, Vec2> SERIAL(levelChanges);
double SERIAL(dieTime);
};
class Thief : public Behaviour {
public:
Thief(Creature* c) : Behaviour(c) {}
virtual MoveInfo getMove() override {
if (!creature->hasSkill(Skill::stealing))
return {0, nullptr};
for (const Creature* other : robbed) {
if (creature->canSee(other)) {
MoveInfo teleMove = tryToApplyItem(EffectType::TELEPORT, 1);
Debug() << "Gotta get out";
if (teleMove.move != nullptr)
return teleMove;
Optional<Vec2> move = creature->getMoveAway(other->getPosition());
if (move)
return {1.0, [this, move] () {
creature->move(*move);
}};
}
}
for (Vec2 dir : Vec2::directions8(true)) {
const Creature* other = creature->getConstSquare(dir)->getCreature();
if (other && !contains(robbed, other)) {
vector<Item*> allGold;
for (Item* it : other->getEquipment().getItems())
if (it->getType() == ItemType::GOLD)
allGold.push_back(it);
if (allGold.size() > 0)
return {1.0, [=]() {
creature->stealFrom(dir, allGold);
other->privateMessage(creature->getTheName() + " steals all your gold!");
robbed.push_back(other);
}};
}
}
return {0, nullptr};
}
SERIALIZATION_CONSTRUCTOR(Thief);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(robbed);
CHECK_SERIAL;
}
private:
vector<const Creature*> SERIAL(robbed);
};
class ByCollective : public Behaviour {
public:
ByCollective(Creature* c, Collective* col) : Behaviour(c), collective(col) {}
virtual MoveInfo getMove() override {
return collective->getMove(creature);
}
SERIALIZATION_CONSTRUCTOR(ByCollective);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(collective);
CHECK_SERIAL;
}
private:
Collective* SERIAL(collective);
};
class ChooseRandom : public Behaviour {
public:
ChooseRandom(Creature* c, vector<Behaviour*> beh, vector<double> w) : Behaviour(c), behaviours(beh), weights(w) {}
virtual MoveInfo getMove() override {
return chooseRandom(behaviours, weights)->getMove();
}
SERIALIZATION_CONSTRUCTOR(ChooseRandom);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(behaviours) & SVAR(weights);
CHECK_SERIAL;
}
private:
vector<Behaviour*> SERIAL(behaviours);
vector<double> SERIAL(weights);
};
class GoToHeart : public Behaviour {
public:
GoToHeart(Creature* c, Vec2 _heartPos) : Behaviour(c), heartPos(_heartPos) {}
virtual MoveInfo getMove() override {
if (Optional<Vec2> move = creature->getMoveTowards(heartPos))
return {1.0, [this, move] () {
creature->move(*move);
}};
else
return NoMove;
};
SERIALIZATION_CONSTRUCTOR(GoToHeart);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(heartPos);
CHECK_SERIAL;
}
private:
Vec2 SERIAL(heartPos);
};
class ByVillageControl : public Behaviour {
public:
ByVillageControl(Creature* c, VillageControl* control, Location* l) :
Behaviour(c), villageControl(control) {
if (l)
guardArea.reset(new GuardArea(c, l));
}
virtual MoveInfo getMove() override {
if (MoveInfo move = villageControl->getMove(creature))
return move;
else if (guardArea)
return guardArea->getMove();
else
return NoMove;
}
SERIALIZATION_CONSTRUCTOR(ByVillageControl);
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(Behaviour) & SVAR(villageControl) & SVAR(guardArea);
CHECK_SERIAL;
}
private:
VillageControl* SERIAL(villageControl);
PBehaviour SERIAL(guardArea);
};
template <class Archive>
void MonsterAI::registerTypes(Archive& ar) {
REGISTER_TYPE(ar, Heal);
REGISTER_TYPE(ar, Rest);
REGISTER_TYPE(ar, MoveRandomly);
REGISTER_TYPE(ar, AttackPest);
REGISTER_TYPE(ar, BirdFlyAway);
REGISTER_TYPE(ar, GoldLust);
REGISTER_TYPE(ar, Fighter);
REGISTER_TYPE(ar, DoorEater);
REGISTER_TYPE(ar, GuardTarget);
REGISTER_TYPE(ar, GuardArea);
REGISTER_TYPE(ar, GuardSquare);
REGISTER_TYPE(ar, Summoned);
REGISTER_TYPE(ar, Wait);
REGISTER_TYPE(ar, Thief);
REGISTER_TYPE(ar, ByCollective);
REGISTER_TYPE(ar, ChooseRandom);
REGISTER_TYPE(ar, GoToHeart);
REGISTER_TYPE(ar, ByVillageControl);
}
REGISTER_TYPES(MonsterAI);
MonsterAI::MonsterAI(Creature* c, const vector<Behaviour*>& beh, const vector<int>& w, bool pick) :
weights(w), creature(c), pickItems(pick) {
CHECK(beh.size() == w.size());
for (auto b : beh)
behaviours.push_back(PBehaviour(b));
}
void MonsterAI::makeMove() {
vector<pair<MoveInfo, int>> moves;
for (int i : All(behaviours)) {
MoveInfo move = behaviours[i]->getMove();
move.value *= weights[i];
moves.emplace_back(move, weights[i]);
if (pickItems) {
for (auto elem : Item::stackItems(creature->getPickUpOptions())) {
Item* item = elem.second[0];
if (!item->getShopkeeper() && creature->canPickUp(elem.second)) {
MoveInfo pickupMove { behaviours[i]->itemValue(item) * weights[i], [=]() {
creature->globalMessage(creature->getTheName() + " picks up " + elem.first, "");
creature->pickUp(elem.second);
}};
moves.emplace_back(pickupMove, weights[i]);
}
}
}
}
/*vector<Item*> inventory = creature->getEquipment().getItems([this](Item* item) { return !creature->getEquipment().isEquiped(item);});
for (Item* item : inventory) {
bool useless = true;
for (PBehaviour& behaviour : behaviours)
if (behaviour->itemValue(item) > 0)
useless = false;
if (useless)
moves.push_back({ 0.01, [=]() {
creature->globalMessage(creature->getTheName() + " drops " + item->getAName(), "");
creature->drop({item});
}});
}*/
MoveInfo winner {0, nullptr};
for (int i : All(moves)) {
MoveInfo& move = moves[i].first;
if (move.value > winner.value)
winner = move;
if (i < moves.size() - 1 && move.value > moves[i + 1].second)
break;
}
CHECK(winner.value > 0);
winner.move();
}
PMonsterAI MonsterAIFactory::getMonsterAI(Creature* c) {
return PMonsterAI(maker(c));
}
MonsterAIFactory::MonsterAIFactory(MakerFun _maker) : maker(_maker) {
}
MonsterAIFactory MonsterAIFactory::monster() {
return stayInLocation(nullptr);
}
MonsterAIFactory MonsterAIFactory::collective(Collective* col) {
return MonsterAIFactory([=](Creature* c) {
return new MonsterAI(c, {
new Heal(c),
new Fighter(c, 0.6, true),
new ByCollective(c, col),
new ChooseRandom(c, {new Rest(c), new MoveRandomly(c, 3)}, {3, 1}),
new AttackPest(c)},
{ 6, 5, 2, 1, 1}, false);