This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmonster.go
1810 lines (1703 loc) · 46.4 KB
/
monster.go
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
package main
import (
"fmt"
"log"
"github.com/anaseto/gruid"
)
type monsterState int
const (
Resting monsterState = iota
Hunting
Wandering
Watching
)
func (m monsterState) String() string {
var st string
switch m {
case Resting:
st = "resting"
case Wandering:
st = "wandering"
case Hunting:
st = "hunting"
case Watching:
st = "watching"
}
return st
}
type monsterStatus int
const (
MonsConfused monsterStatus = iota
MonsExhausted
MonsParalysed
MonsSatiated
MonsLignified
)
const NMonsStatus = int(MonsLignified) + 1
func (st monsterStatus) String() (text string) {
switch st {
case MonsConfused:
text = "confused"
case MonsExhausted:
text = "exhausted"
case MonsParalysed:
text = "paralysed"
case MonsSatiated:
text = "satiated"
case MonsLignified:
text = "lignified"
}
return text
}
type monsterKind int
const (
MonsGuard monsterKind = iota
MonsYack
MonsSatowalgaPlant
MonsMadNixe
MonsBlinkingFrog
MonsWorm
MonsMirrorSpecter
MonsTinyHarpy
//MonsOgre
MonsOricCelmist
MonsHarmonicCelmist
//MonsBrizzia
MonsDog
//MonsGiantBee
MonsHighGuard
//MonsHydra
//MonsSkeletonWarrior
MonsSpider
MonsWingedMilfid
//MonsLich
MonsEarthDragon
MonsAcidMound
MonsExplosiveNadre
//MonsMindCelmist
MonsVampire
MonsTreeMushroom
//MonsMarevorHelith
MonsButterfly
MonsCrazyImp
MonsHazeCat
)
func (mk monsterKind) String() string {
return MonsData[mk].name
}
func (mk monsterKind) Letter() rune {
return MonsData[mk].letter
}
func (mk monsterKind) BaseAttack() int {
return 1
}
func (mk monsterKind) Dangerousness() int {
return MonsData[mk].dangerousness
}
func (mk monsterKind) Ranged() bool {
switch mk {
//case MonsLich, MonsCyclop, MonsHighGuard, MonsSatowalgaPlant, MonsMadNixe, MonsVampire, MonsTreeMushroom:
case MonsHighGuard, MonsSatowalgaPlant, MonsMadNixe, MonsVampire, MonsTreeMushroom:
return true
default:
return false
}
}
func (mk monsterKind) Smiting() bool {
switch mk {
//case MonsMirrorSpecter, MonsMindCelmist:
case MonsMirrorSpecter, MonsOricCelmist, MonsHarmonicCelmist:
return true
default:
return false
}
}
func (mk monsterKind) Peaceful() bool {
switch mk {
case MonsButterfly, MonsEarthDragon, MonsCrazyImp:
return true
default:
return false
}
}
func (mk monsterKind) GoodSmell() bool {
switch mk {
case MonsDog, MonsHazeCat, MonsVampire, MonsExplosiveNadre:
return true
default:
return false
}
}
func (mk monsterKind) Notable() bool {
switch mk {
case MonsCrazyImp, MonsEarthDragon, MonsHazeCat:
return true
default:
return false
}
}
func (mk monsterKind) CanOpenDoors() bool {
switch mk {
case MonsGuard, MonsHighGuard, MonsMadNixe, MonsOricCelmist, MonsHarmonicCelmist, MonsVampire, MonsWingedMilfid:
return true
default:
return false
}
}
func (mk monsterKind) Patrolling() bool {
switch mk {
case MonsGuard, MonsHighGuard, MonsMadNixe, MonsOricCelmist, MonsHarmonicCelmist:
return true
default:
return false
}
}
func (mk monsterKind) CanFly() bool {
switch mk {
case MonsWingedMilfid, MonsMirrorSpecter, MonsButterfly, MonsTinyHarpy:
return true
default:
return false
}
}
func (mk monsterKind) CanSwim() bool {
switch mk {
case MonsBlinkingFrog, MonsVampire, MonsDog:
return true
default:
return false
}
}
func (mk monsterKind) CanAttackOnTree() bool {
switch {
case mk.Size() == MonsLarge:
return true
case mk.CanFly():
return true
case mk == MonsBlinkingFrog || mk == MonsHazeCat:
return true
default:
return false
}
}
func (mk monsterKind) ShallowSleep() bool {
switch mk {
case MonsCrazyImp, MonsHazeCat:
return true
default:
return false
}
}
func (mk monsterKind) ResistsLignification() bool {
switch mk {
case MonsSatowalgaPlant, MonsTreeMushroom:
return true
default:
return false
}
}
func (mk monsterKind) ReflectsTeleport() bool {
switch mk {
case MonsBlinkingFrog:
return true
default:
return false
}
}
func (mk monsterKind) Desc() string {
return MonsDesc[mk]
}
func (mk monsterKind) Indefinite(capital bool) (text string) {
switch mk {
//case MonsMarevorHelith:
//text = mk.String()
default:
text = Indefinite(mk.String(), capital)
}
return text
}
func (mk monsterKind) Definite(capital bool) (text string) {
switch mk {
//case MonsMarevorHelith:
//text = mk.String()
default:
if capital {
text = fmt.Sprintf("The %s", mk.String())
} else {
text = fmt.Sprintf("the %s", mk.String())
}
}
return text
}
func (mk monsterKind) Size() monsize {
return MonsData[mk].size
}
type monsize int
const (
MonsSmall monsize = iota
MonsMedium
MonsLarge
)
func (ms monsize) String() (text string) {
switch ms {
case MonsSmall:
text = "small"
case MonsMedium:
text = "average"
case MonsLarge:
text = "large"
}
return text
}
type monsterData struct {
size monsize
letter rune
name string
dangerousness int
}
var MonsData = []monsterData{
MonsGuard: {MonsMedium, 'g', "guard", 3},
MonsTinyHarpy: {MonsSmall, 't', "tiny harpy", 3},
//MonsOgre: {10, 2, 20, 3, 'O', "ogre", 7},
MonsOricCelmist: {MonsMedium, 'o', "oric celmist", 9},
MonsHarmonicCelmist: {MonsMedium, 'h', "harmonic celmist", 9},
MonsWorm: {MonsSmall, 'w', "farmer worm", 4},
//MonsBrizzia: {15, 1, 10, 3, 'z', "brizzia", 6},
MonsAcidMound: {MonsSmall, 'a', "acid mound", 4},
MonsDog: {MonsMedium, 'd', "dog", 5},
MonsYack: {MonsMedium, 'y', "yack", 5},
//MonsGiantBee: {5, 1, 10, 1, 'B', "giant bee", 6},
MonsHighGuard: {MonsMedium, 'G', "high guard", 5},
//MonsHydra: {10, 1, 10, 4, 'H', "hydra", 10},
//MonsSkeletonWarrior: {10, 1, 10, 3, 'S', "skeleton warrior", 6},
MonsSpider: {MonsSmall, 's', "spider", 15},
MonsWingedMilfid: {MonsMedium, 'W', "winged milfid", 6},
MonsBlinkingFrog: {MonsMedium, 'F', "blinking frog", 6},
//MonsLich: {10, MonsMedium, 'L', "lich", 15},
MonsEarthDragon: {MonsLarge, 'D', "earth dragon", 18},
MonsMirrorSpecter: {MonsMedium, 'm', "mirror specter", 11},
MonsExplosiveNadre: {MonsMedium, 'n', "explosive nadre", 8},
MonsSatowalgaPlant: {MonsLarge, 'P', "satowalga plant", 7},
MonsMadNixe: {MonsMedium, 'N', "mad nixe", 14},
//MonsMindCelmist: {10, 1, 20, 2, 'c', "mind celmist", 12},
MonsVampire: {MonsMedium, 'V', "vampire", 13},
MonsTreeMushroom: {MonsLarge, 'T', "tree mushroom", 17},
//MonsMarevorHelith: {10, MonsMedium, 'M', "Marevor Helith", 18},
MonsButterfly: {MonsSmall, 'b', "kerejat", 2},
MonsCrazyImp: {MonsSmall, 'i', "Crazy Imp", 19},
MonsHazeCat: {MonsSmall, 'c', "haze cat", 16},
}
var MonsDesc = []string{
MonsGuard: "Guards are low rank soldiers who patrol between Dayoriah Clan's buildings.",
MonsTinyHarpy: "Tiny harpies are little humanoid flying creatures. They are aggressive when hungry, but peaceful when satiated. This Underground harpy species eats fruits (including bananas) and other vegetables.",
//MonsOgre: "Ogres are big clunky humanoids that can hit really hard.",
MonsOricCelmist: "Oric celmists are mages that can create magical barriers in cells adjacent to you, complicating your escape.\n\nDayoriah Clan's oric celmists are famous for their knowledge of oric magic force manipulations. They are the ones who instigated the steal of Marevor's Gem Portal Artifact. According to Marevor, they plan on doing some dangerous oric experiments with the Artifact, though that's all you can say about it, because his boring explanations were a bit over your head.",
MonsHarmonicCelmist: "Harmonic celmists are mages specialized in manipulation of sound and light. They can illuminate you with harmonic light, making it more difficult to hide from them. They also use alert harmonic sounds around you.\n\nHarmonies are usually mainly used for sneaking around in the shadows, but they can also be used to reveal ennemies, sadly for you. Although harmonies are often considered as less prestigious magic energies than oric energies, the Dayoriah Clan knows how to make good use of them, as they clearly showed when they stole Marevor's Gem Portal Artifact.",
MonsWorm: "Farmer worms are ugly creeping creatures. They furrow as they move, helping new foliage to grow.",
//MonsBrizzia: "Brizzias are big slow moving biped creatures. They are quite hardy, and when hurt they can cause nausea, impeding the use of potions.",
MonsAcidMound: "Acid mounds are acidic creatures. They can corrode your magaras, reducing their number of charges.",
MonsDog: "Dogs are carnivore quadrupeds. They can bark, and smell you from up to 5 tiles away when hunting or watching for you.",
MonsYack: "Yacks are quite large herbivorous quadrupeds. They tend to eat grass peacefully, but upon seeing you they may attack, pushing you up to 5 cells away.",
//MonsGiantBee: "Giant bees are fragile but extremely fast moving creatures. Their bite can sometimes enrage you.",
MonsHighGuard: "High guards watch over a particular location. They can throw javelins.",
//MonsHydra: "Hydras are enormous creatures with four heads that can hit you each at once.",
//MonsSkeletonWarrior: "Skeleton warriors are good fighters, clad in chain mail.",
MonsSpider: "Spiders are small creatures, with panoramic vision and whose bite can confuse you.",
MonsWingedMilfid: "Winged milfids are humanoids that can fly over you and make you swap positions. They tend to be very aggressive creatures.",
MonsBlinkingFrog: "Blinking frogs are big frog-like creatures, whose bite can make you blink away. The science behind their attack is not clear, but many think it relies on some kind of oric deviation magic. They can jump to attack from below.",
//MonsLich: "Liches are non-living mages wearing a leather armour. They can throw a bolt of torment at you, halving your HP.",
MonsEarthDragon: "Earth dragons are big creatures from a dragon species that wander in the Underground. They are peaceful creatures, but they may hurt you inadvertently, pushing you up to 6 tiles away (3 if confused). They naturally emit powerful oric energies, allowing them to eat rocks and dig tunnels. Their oric energies can confuse you if you're close enough, for example if they hurt you or you jump over them.",
MonsMirrorSpecter: "Mirror specters are very insubstantial creatures, which can absorb your mana.",
MonsExplosiveNadre: "Nadres are dragon-like biped creatures that are famous for exploding upon dying. Explosive nadres are a tiny nadre race that explodes upon attacking. The explosion confuses any adjacent creatures and occasionally destroys walls.",
MonsSatowalgaPlant: "Satowalga Plants are immobile bushes that throw viscous acidic projectiles at you, destroying some of your magara charges. They attack at half normal speed.",
MonsMadNixe: "Nixes are magical humanoids. Usually, they specialize in illusion harmonic magic, but the so called mad nixes are a perverted variant who learned the oric arts to create a spell that can attract their foes to them, so that they can kill them without pursuing them.",
//MonsMindCelmist: "Mind celmists are mages that use magical smitting mind attacks that bypass armour. They can occasionally confuse or slow you. They try to avoid melee.",
MonsVampire: "Vampires are humanoids that drink blood to survive. Their nauseous spitting can cause confusion, impeding the use of magaras for a few turns.",
MonsTreeMushroom: "Tree mushrooms are big clunky creatures. They can throw lignifying spores at you, leaving you unable to move for a few turns, though the spores will also provide some protection against harm.",
//MonsMarevorHelith: "Marevor Helith is an ancient undead nakrus very fond of teleporting people away. He is a well-known expert in the field of magaras - items that many people simply call magical objects. His current research focus is monolith creation. Marevor, a repentant necromancer, is now searching for his old disciple Jaixel in the Underground to help him overcome the past.",
MonsButterfly: "Underground's butterflies, called kerejats, wander peacefully around, illuminating their surroundings.",
MonsCrazyImp: "Crazy Imp is a crazy creature that likes to sing with its small guitar. It seems to be fond of monkeys and quite capable at finding them by smell. While singing it may attract unwanted attention.",
MonsHazeCat: "Haze cats are a special variety of cats found in the Underground. They have very good night vision and are always alert.",
}
type bandInfo struct {
Path []gruid.Point
I int
Kind monsterBand
Beh mbehaviour
}
type monsterBand int
const (
LoneGuard monsterBand = iota
LoneHighGuard
LoneYack
LoneOricCelmist
LoneHarmonicCelmist
LoneSatowalgaPlant
LoneBlinkingFrog
LoneWorm
LoneMirrorSpecter
LoneDog
LoneExplosiveNadre
LoneWingedMilfid
LoneMadNixe
LoneTreeMushroom
LoneEarthDragon
LoneButterfly
LoneVampire
LoneHarpy
LoneHazeCat
LoneAcidMound
LoneSpider
PairGuard
PairYack
PairFrog
PairDog
PairTreeMushroom
PairSpider
PairHazeCat
PairSatowalga
PairWorm
PairOricCelmist
PairHarmonicCelmist
PairVampire
PairNixe
PairExplosiveNadre
PairWingedMilfid
SpecialLoneVampire
SpecialLoneNixe
SpecialLoneMilfid
SpecialLoneOricCelmist
SpecialLoneHarmonicCelmist
SpecialLoneHighGuard
SpecialLoneHarpy
SpecialLoneTreeMushroom
SpecialLoneMirrorSpecter
SpecialLoneAcidMound
SpecialLoneHazeCat
SpecialLoneSpider
SpecialLoneBlinkingFrog
SpecialLoneExplosiveNadre
SpecialLoneYack
SpecialLoneDog
UniqueCrazyImp
)
type monsterBandData struct {
Distribution map[monsterKind]int
Band bool
Monster monsterKind
}
var MonsBands = []monsterBandData{
LoneGuard: {Monster: MonsGuard},
LoneHighGuard: {Monster: MonsHighGuard},
LoneYack: {Monster: MonsYack},
LoneOricCelmist: {Monster: MonsOricCelmist},
LoneHarmonicCelmist: {Monster: MonsHarmonicCelmist},
LoneSatowalgaPlant: {Monster: MonsSatowalgaPlant},
LoneBlinkingFrog: {Monster: MonsBlinkingFrog},
LoneWorm: {Monster: MonsWorm},
LoneMirrorSpecter: {Monster: MonsMirrorSpecter},
LoneDog: {Monster: MonsDog},
LoneExplosiveNadre: {Monster: MonsExplosiveNadre},
LoneWingedMilfid: {Monster: MonsWingedMilfid},
LoneMadNixe: {Monster: MonsMadNixe},
LoneTreeMushroom: {Monster: MonsTreeMushroom},
LoneEarthDragon: {Monster: MonsEarthDragon},
LoneButterfly: {Monster: MonsButterfly},
LoneVampire: {Monster: MonsVampire},
LoneHarpy: {Monster: MonsTinyHarpy},
LoneHazeCat: {Monster: MonsHazeCat},
LoneAcidMound: {Monster: MonsAcidMound},
LoneSpider: {Monster: MonsSpider},
PairGuard: {Band: true, Distribution: map[monsterKind]int{MonsGuard: 2}},
PairYack: {Band: true, Distribution: map[monsterKind]int{MonsYack: 2}},
PairFrog: {Band: true, Distribution: map[monsterKind]int{MonsBlinkingFrog: 2}},
PairDog: {Band: true, Distribution: map[monsterKind]int{MonsDog: 2}},
PairTreeMushroom: {Band: true, Distribution: map[monsterKind]int{MonsTreeMushroom: 2}},
PairSpider: {Band: true, Distribution: map[monsterKind]int{MonsSpider: 2}},
PairHazeCat: {Band: true, Distribution: map[monsterKind]int{MonsHazeCat: 2}},
PairSatowalga: {Band: true, Distribution: map[monsterKind]int{MonsSatowalgaPlant: 2}},
PairWorm: {Band: true, Distribution: map[monsterKind]int{MonsWorm: 2}},
PairVampire: {Band: true, Distribution: map[monsterKind]int{MonsVampire: 2}},
PairOricCelmist: {Band: true, Distribution: map[monsterKind]int{MonsOricCelmist: 2}},
PairHarmonicCelmist: {Band: true, Distribution: map[monsterKind]int{MonsHarmonicCelmist: 2}},
PairNixe: {Band: true, Distribution: map[monsterKind]int{MonsMadNixe: 2}},
PairExplosiveNadre: {Band: true, Distribution: map[monsterKind]int{MonsExplosiveNadre: 2}},
PairWingedMilfid: {Band: true, Distribution: map[monsterKind]int{MonsWingedMilfid: 2}},
SpecialLoneVampire: {Monster: MonsVampire},
SpecialLoneNixe: {Monster: MonsMadNixe},
SpecialLoneMilfid: {Monster: MonsWingedMilfid},
SpecialLoneOricCelmist: {Monster: MonsOricCelmist},
SpecialLoneHarmonicCelmist: {Monster: MonsHarmonicCelmist},
SpecialLoneHighGuard: {Monster: MonsHighGuard},
SpecialLoneHarpy: {Monster: MonsTinyHarpy},
SpecialLoneTreeMushroom: {Monster: MonsTreeMushroom},
SpecialLoneMirrorSpecter: {Monster: MonsMirrorSpecter},
SpecialLoneAcidMound: {Monster: MonsAcidMound},
SpecialLoneHazeCat: {Monster: MonsHazeCat},
SpecialLoneSpider: {Monster: MonsSpider},
SpecialLoneBlinkingFrog: {Monster: MonsBlinkingFrog},
SpecialLoneDog: {Monster: MonsDog},
SpecialLoneExplosiveNadre: {Monster: MonsExplosiveNadre},
SpecialLoneYack: {Monster: MonsYack},
UniqueCrazyImp: {Monster: MonsCrazyImp},
}
type monster struct {
Kind monsterKind
Band int
Index int
Dir gruid.Point
Attack int
Dead bool
State monsterState
Statuses [NMonsStatus]int
P gruid.Point
LastKnownPos gruid.Point
Target gruid.Point
Path []gruid.Point // cache
FireReady bool
Seen bool
LOS map[gruid.Point]bool
LastKnownState monsterState
Swapped bool
Watching int
Left bool
Search gruid.Point
Alerted bool
Waiting int
}
func (m *monster) Init() {
m.Attack = m.Kind.BaseAttack()
m.P = invalidPos
m.LOS = make(map[gruid.Point]bool)
m.LastKnownPos = invalidPos
m.Search = invalidPos
if RandInt(2) == 0 {
m.Left = true
}
switch m.Kind {
case MonsButterfly:
m.MakeWander()
case MonsSatowalgaPlant:
m.StartWatching()
}
}
func (m *monster) CanPass(g *game, p gruid.Point) bool {
if !valid(p) {
return false
}
c := g.Dungeon.Cell(p)
return c.IsPassable() ||
c.IsDoorPassable() && m.Kind.CanOpenDoors() ||
c.IsLevitatePassable() && m.Kind.CanFly() ||
c.IsSwimPassable() && (m.Kind.CanSwim() || m.Kind.CanFly()) ||
terrain(c) == HoledWallCell && m.Kind.Size() == MonsSmall
}
func (m *monster) StartWatching() {
m.State = Watching
m.Watching = 0
}
func (m *monster) Status(st monsterStatus) bool {
return m.Statuses[st] > 0
}
func (m *monster) Exists() bool {
return m != nil && !m.Dead
}
func (m *monster) Alternate() {
if m.Left {
if RandInt(4) > 0 {
m.Dir = leftDir(m.Dir)
} else {
m.Dir = rightDir(m.Dir)
m.Left = false
}
} else {
if RandInt(3) > 0 {
m.Dir = rightDir(m.Dir)
} else {
m.Dir = leftDir(m.Dir)
m.Left = true
}
}
}
func (m *monster) TeleportAway(g *game) {
p := m.P
i := 0
count := 0
for {
count++
if count > maxIterations {
panic("TeleportOther")
}
p = g.FreePassableCell()
if distance(p, m.P) < 15 && i < maxIterations {
i++
continue
}
break
}
switch m.State {
case Hunting:
// TODO: change the target or state?
case Resting, Wandering:
m.MakeWander()
m.Target = m.P
}
if g.Player.Sees(m.P) {
g.PrintfStyled("%s teleports away.", logNotable, m.Kind.Definite(true))
}
opos := m.P
m.MoveTo(g, p)
if g.Player.Sees(opos) {
g.md.TeleportAnimation(opos, p, false)
}
}
func (m *monster) MoveTo(g *game, p gruid.Point) {
if g.Player.Sees(p) {
m.UpdateKnowledge(g, p)
} else if g.Player.Sees(m.P) {
if distance(m.P, p) == 1 {
// You know the direction, so you know where the
// monster should be.
m.UpdateKnowledge(g, p)
} else {
delete(g.LastMonsterKnownAt, m.P)
m.LastKnownPos = invalidPos
}
}
if !g.Player.Sees(m.P) && g.Player.Sees(p) {
if !m.Seen {
m.Seen = true
g.Printf("%s (%v) comes into view.", m.Kind.Indefinite(true), m.State)
}
g.StopAuto()
}
recomputeLOS := g.Player.Sees(m.P) && terrain(g.Dungeon.Cell(m.P)) == DoorCell ||
g.Player.Sees(p) && terrain(g.Dungeon.Cell(p)) == DoorCell
m.PlaceAt(g, p)
if recomputeLOS {
g.ComputeLOS()
}
c := g.Dungeon.Cell(p)
if terrain(c) == ChasmCell && !m.Kind.CanFly() || terrain(c) == WaterCell && !m.Kind.CanSwim() && !m.Kind.CanFly() {
m.Dead = true
if g.Player.Sees(m.P) {
g.HandleKill(m)
switch terrain(c) {
case ChasmCell:
g.Printf("%s falls into the abyss.", m.Kind.Definite(true))
case WaterCell:
g.Printf("%s drowns.", m.Kind.Definite(true))
}
}
}
}
func (m *monster) PlaceAtStart(g *game, p gruid.Point) {
i := idx(p)
if g.MonstersPosCache[i] > 0 {
log.Printf("used monster starting position: %v", p)
// should not happen
return
}
m.P = p
g.MonstersPosCache[i] = m.Index + 1
npos := m.RandomFreeNeighbor(g)
if npos != m.P {
m.Dir = dirnorm(m.P, npos)
} else {
m.Dir = gruid.Point{1, 0}
}
}
func (m *monster) PlaceAt(g *game, p gruid.Point) {
if !valid(m.P) {
log.Printf("monster place at: bad position %v", m.P)
// should not happen
return
}
if p == invalidPos {
log.Printf("monster new place at invalid position")
// should not happen
return
}
if p == m.P {
// should not happen
log.Printf("monster place at: same position %v", m.P)
return
}
if p == g.Player.P {
log.Printf("monster place at: player position %v", p)
// should not happen
return
}
i := idx(p)
j := idx(m.P)
m.Dir = dirnorm(m.P, p)
mons := g.MonsterAt(p)
g.MonstersPosCache[i], g.MonstersPosCache[j] = g.MonstersPosCache[j], g.MonstersPosCache[i]
if mons.Exists() {
m.P, mons.P = mons.P, m.P
mons.Swapped = true
} else {
m.P = p
}
m.Waiting = 0
}
func (g *game) MonsterAt(p gruid.Point) *monster {
pi := idx(p)
if pi < 0 || pi >= len(g.MonstersPosCache) {
log.Printf("monster at: bad index %v for pos %v", pi, p)
// should not happen
return nil
}
i := g.MonstersPosCache[pi]
if i <= 0 {
return nil
}
m := g.Monsters[i-1]
//if m.P != p {
//log.Printf("monster position mismatch: %v vs %v", m.P, p)
//}
return m
}
func (m *monster) EndTurn(g *game) {
g.PushEventD(&monsterTurnEvent{Index: m.Index}, 1)
}
func (m *monster) AttackAction(g *game) bool {
m.Dir = dirnorm(m.P, g.Player.P)
switch m.Kind {
case MonsExplosiveNadre:
g.StoryPrint("Nadre explosion")
m.Explode(g)
return false
default:
if g.Player.HasStatus(StatusDispersal) {
m.Blink(g)
} else {
m.HitPlayer(g)
}
}
return true
}
func (m *monster) Explode(g *game) {
m.Dead = true
neighbors := g.cardinalNeighbors(m.P)
g.Printf("%s %s explodes with a loud boom.", g.ExplosionSound(), m.Kind.Definite(true))
g.md.ExplosionAnimation(FireExplosion, m.P)
g.MakeNoise(ExplosionNoise, m.P)
for _, p := range append(neighbors, m.P) {
c := g.Dungeon.Cell(p)
if c.Flammable() {
g.Burn(p)
}
mons := g.MonsterAt(p)
if mons.Exists() && !mons.Status(MonsConfused) {
mons.EnterConfusion(g)
if mons.State != Hunting && mons.State != Watching {
mons.StartWatching()
}
} else if g.Player.P == p {
m.InflictDamage(g, 1, 1)
} else if c.IsDestructible() && RandInt(3) > 0 {
if c.IsDiggable() {
g.Dungeon.SetCell(p, RubbleCell)
} else {
g.Dungeon.SetCell(p, GroundCell)
}
if terrain(c) == BarrelCell {
delete(g.Objects.Barrels, p)
}
g.Stats.Digs++
g.UpdateKnowledge(p, terrain(c))
if g.Player.Sees(p) {
g.md.WallExplosionAnimation(p)
}
g.MakeNoise(WallNoise, p)
g.Fog(p, 1)
}
}
}
func (m *monster) NaturalAwake(g *game) {
m.Target = m.NextTarget(g)
switch g.Bands[m.Band].Beh {
case BehGuard:
m.StartWatching()
default:
m.MakeWander()
}
m.GatherBand(g)
}
func (m *monster) RandomFreeNeighbor(g *game) gruid.Point {
p := m.P
neighbors := [4]gruid.Point{p.Add(gruid.Point{1, 0}), p.Add(gruid.Point{-1, 0}), p.Add(gruid.Point{0, -1}), p.Add(gruid.Point{0, 1})}
fnb := []gruid.Point{}
for _, nbpos := range neighbors {
if !valid(nbpos) {
continue
}
c := g.Dungeon.Cell(nbpos)
if c.IsPassable() {
fnb = append(fnb, nbpos)
}
}
if len(fnb) == 0 {
return m.P
}
samedir := fnb[RandInt(len(fnb))]
for _, p := range fnb {
// invariant: pos != m.Pos
if inViewCone(m.Dir, m.P, m.P.Add(p)) {
samedir = p
break
}
}
if RandInt(4) > 0 {
return samedir
}
return fnb[RandInt(len(fnb))]
}
type mbehaviour int
const (
BehPatrol mbehaviour = iota
BehGuard
BehWander
BehExplore
BehCrazyImp
)
var SearchAroundCache []gruid.Point
func (m *monster) SearchAround(g *game, p gruid.Point, radius int) gruid.Point {
dij := &monPath{g: g, monster: m}
nodes := g.PR.DijkstraMap(dij, []gruid.Point{p}, radius)
SearchAroundCache = SearchAroundCache[:0]
for _, n := range nodes {
SearchAroundCache = append(SearchAroundCache, n.P)
}
if len(SearchAroundCache) > 0 {
p := SearchAroundCache[RandInt(len(SearchAroundCache))]
return p
}
return invalidPos
}
func (m *monster) NextTarget(g *game) (p gruid.Point) {
band := g.Bands[m.Band]
p = m.P
switch band.Beh {
case BehWander:
if distance(m.P, band.Path[0]) < 8+RandInt(8) {
p = m.SearchAround(g, m.P, 4)
if p != invalidPos {
break
}
}
if m.Search != invalidPos && RandInt(2) == 0 {
p = m.SearchAround(g, m.Search, 7)
if p != invalidPos {
break
}
}
p = m.SearchAround(g, band.Path[0], 7)
if p != invalidPos {
break
}
p = band.Path[0]
case BehExplore:
if m.Kind.CanOpenDoors() {
if m.Search != invalidPos && RandInt(4) == 0 {
p = m.SearchAround(g, m.Search, 7)
} else {
p = m.SearchAround(g, p, 5)
}
if p != invalidPos {
break
}
}
p = band.Path[RandInt(len(band.Path))]
case BehGuard:
if m.Search != invalidPos && distance(m.Search, m.P) < 5 && RandInt(2) == 0 {
p = m.SearchAround(g, m.Search, 3)
if p != invalidPos {
break
}
}
p = band.Path[0]
case BehPatrol:
if m.Search != invalidPos && RandInt(4) > 0 {
p = m.SearchAround(g, m.Search, 7)
if p != m.P && p != invalidPos {
break
}
}
if band.Path[0] == m.Target {
p = band.Path[1]
} else if band.Path[1] == m.Target {
p = band.Path[0]
} else if distance(band.Path[0], m.P) < distance(band.Path[1], m.P) {
p = band.Path[0]
if RandInt(4) == 0 {
p = band.Path[1]
}
} else {
p = band.Path[1]
if RandInt(4) == 0 {
p = band.Path[0]
}
}
case BehCrazyImp:
path := m.APath(g, m.P, g.Player.P)
if len(path) == 0 {
p = m.SearchAround(g, m.P, 3)
} else {
p = g.Player.P
}
}
return p
}
func (m *monster) HandleMonsSpecifics(g *game) (done bool) {
switch m.Kind {
case MonsSatowalgaPlant:
switch m.State {
case Hunting:
if m.Target != invalidPos && m.Target != m.P {
m.Dir = dirnorm(m.P, m.Target)
}
if !m.SeesPlayer(g) {
m.StartWatching()
}
default:
if RandInt(4) > 0 {
m.Alternate()
}
}
// oklob plants are static ranged-only
return true
case MonsGuard, MonsHighGuard:
if m.State != Wandering && m.State != Watching {
break
}
for p, on := range g.Objects.Lights {
if !on && distance(p, m.P) <= DefaultMonsterLOSRange {
m.ComputeLOS(g)
break
}
}
for p, on := range g.Objects.Lights {
if !on && p == m.P {
g.Dungeon.SetCell(m.P, LightCell)
g.Objects.Lights[m.P] = true
if g.Player.Sees(m.P) {
g.Printf("%s makes a new fire.", m.Kind.Definite(true))
} else {
g.UpdateKnowledge(m.P, ExtinguishedLightCell)
}
return true
} else if !on && m.SeesLight(g, p) {
m.Target = p
}
}
case MonsCrazyImp:
if g.Player.Sees(m.P) && RandInt(2) == 0 && !m.Status(MonsConfused) && !m.Status(MonsExhausted) {
g.PrintStyled("Crazy Imp: “♫ larilon, larila ♫ ♪”", logSpecial)
g.MakeNoise(SingingNoise, m.P)
//g.ui.MusicAnimation(m.Pos)
m.Exhaust(g)
}
}
return false
}
const DogSmellDist = 5
const DisguiseSmellDist = 3
func (m *monster) HandleWatching(g *game) {
turns := 4
if m.Kind == MonsHazeCat {
turns = 3
}
if m.Watching+RandInt(2) < turns {
m.Alternate()
m.Watching++
if m.Kind == MonsDog {
dij := &monPath{g: g, monster: m}
g.PR.DijkstraMap(dij, []gruid.Point{m.P}, DogSmellDist)
if c := g.PR.DijkstraMapAt(g.Player.P); c <= DogSmellDist {
m.Target = g.Player.P
m.MakeWander()
}
}
} else {
// pick a random cell: more escape strategies for the player
m.Target = m.NextTarget(g)
switch g.Bands[m.Band].Beh {
case BehGuard:
m.Alternate()
if m.P != m.Target {
m.MakeWander()
m.GatherBand(g)