forked from jwvhewitt/gearhead-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaibrain.pp
1917 lines (1700 loc) · 65.1 KB
/
aibrain.pp
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
unit aibrain;
{ This unit handles behavior for the various enemy units in }
{ Gearhead Arena. }
{
GearHead: Arena, a roguelike mecha CRPG
Copyright (C) 2005 Joseph Hewitt
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
The full text of the LGPL can be found in license.txt.
This library 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 Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
interface
uses gears,locale;
Procedure ClearHotMaps;
Procedure GetAIInput( Mek: GearPtr; GB: GameBoardPtr );
Procedure ConfusedInput( Mek: GearPtr; GB: GameBoardPtr );
Procedure BrownianMotion( GB: GameBoardPtr );
Procedure LancemateUsefulAction( Mek: GearPtr; GB: GameBoardPtr );
Function MOVE_MODEL_TOWARDS_SPOT( Mek: GearPtr; GB: GameBoardPtr; GX,GY: Integer ): Boolean;
implementation
{$IFDEF SDLMODE}
uses ability,action,arenacfe,damage,effects,movement,gearutil,
ghchars,ghmodule,ghweapon,ghparser,ghprop,interact,rpgdice,skilluse,
texutil,ui4gh,sdlmap,sdlgfx;
{$ELSE}
uses ability,action,arenacfe,damage,effects,movement,gearutil,
ghchars,ghmodule,ghweapon,ghparser,ghprop,interact,rpgdice,skilluse,
texutil,ui4gh,conmap,context;
{$ENDIF}
const
Hot_Terr: Array [0..NumMoveMode,1..NumTerr] of SmallInt = (
( 2, 3, 4,-1, 2, 2, 3, 3, 4, 5, { CHARA WALK }
3,-1,-1, 2,-1, 2, 2,-1, 2, 2,
-1,-1,-1,-1, 2, 2,-1, 2,-1, 2,
-1,-1,-1,-1,-1, -1,-1,-1, 2, 4,
2,-1 ),
( 2, 3, 4, 5, 2, 2, 3, 3, 4, 5, { MECHA WALK }
3,-1,-1, 2,-1, 2, 2,-1, 2, 2,
5, 5,-1,-1, 2, 2,-1, 2,-1, 2,
-1,-1,-1,-1,-1, -1,-1,-1, 2, 4,
2,-1 ),
( 2,-1,-1,-1, 3, 1, 4, 3, 4, 5, { ROLL }
4,-1,-1, 2,-1, 2, 2,-1, 2, 2,
5, 5,-1,-1, 2, 2,-1, 2,-1, 2,
-1,-1,-1,-1,-1, -1,-1,-1, 2,-1,
2,-1 ),
( 2, 3, 4, 2, 2, 2, 3, 3, 4, 5, { SKIM }
2,-1,-1, 2,-1, 2, 2,-1, 2, 2,
2, 2,-1,-1, 2, 2,-1, 2,-1, 2,
-1,-1,-1,-1,-1, -1,-1,-1, 2, 4,
2,-1 ),
( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, { FLY }
2, 2,-1, 2,-1, 2, 2,-1, 2, 2,
2, 2,-1,-1, 2, 2,-1, 2,-1, 2,
-1,-1,-1,-1,-1, -1,-1,-1, 2, 2,
2,-1 )
);
NumFFMap = 10;
ORD_SeekEnemy = 0;
ORD_SeekSingleModel = 1;
ORD_SeekSpot = 2;
ORD_SeekEdge = 3;
ORD_SeekTeam = 4;
Hot_Map_Validity = 10;
CORD_Flirt = 1;
CORD_Chat = 2;
var
{ The HOTMAP is used for pathfinding and general tactical }
{ decision-making. }
{ HOTMAP shows what squares the NPC will want to move into. }
{ COLDMAP shows what squares the NPC won't want to move into. }
MapUpdate: Array [1..NumFFMap] of LongInt;
MapTeam: Array [1..NumFFMap] of Integer;
MapOrder: Array [1..NumFFMap] of Integer;
MapMoveMode: Array [1..NumFFMap] of Integer;
HotMap: Array [1..NumFFMap , 1..XMax , 1..YMax ] of Integer;
ColdMap: Array [1..NumFFMap , 1..XMax , 1..YMax ] of Integer;
NPC_Chatter_Standard: SAttPtr;
Procedure NPC_CombatTaunt( GB: GameBoardPtr; NPC: GearPtr; Msg_Label: String );
{ NPC is going to say something... maybe. Search the NPC gear for things to say, }
{ then search the standard chatter list for things to say, then pick one of them }
{ and say it. }
{ Note that this will not nessecarily be an actual taunt. It's more likely to be }
{ something completely different... but I didn't feel like calling this procedure }
{ "NPC_Mutters"... }
var
MList: SAttPtr;
Procedure HarvestMessages( LList: SAttPtr; head: String );
{ Look through LList and collect all strings that match HEAD. }
var
M: SAttPtr;
begin
M := LList;
while M <> Nil do begin
if HeadMatchesString( head , M^.Info ) then StoreSAtt( MList , RetrieveAString( M^.Info ) );
M := M^.Next;
end;
end;
var
T,V: Integer;
begin
{ Make sure combat taunts are enabled. }
if No_Combat_Taunts then begin
SetNAtt( FindRoot( NPC )^.NA , NAG_EpisodeData , NAS_ChatterRecharge , GB^.ComTime + 100000 );
Exit;
end;
{ Make sure we have a proper NPC, and not a mecha. }
NPC := LocatePilot( NPC );
if NPC = Nil then Exit;
{ Initialize our message list to NIL. }
MList := Nil;
{ First, search through NPC itself looking for appropriate messages. }
HarvestMessages( NPC^.SA , msg_label );
{ Next, pick some contenders from the standard chatter list. }
{ Only characters with CIDs get this. }
if ( UpCase( SAttValue( NPC^.SA , 'JOB' ) ) <> 'ANIMAL' ) and ( ( Msg_Label = 'CHAT_EJECT' ) or ( ( NAttValue( NPC^.NA , NAG_Personal , NAS_CID ) <> 0 ) and ( ( MList = Nil ) or ( Random( 2 ) = 1 ) ) ) ) then begin
HarvestMessages( NPC_Chatter_Standard , msg_label + '_ALL' );
for t := 1 to Num_Personality_Traits do begin
V := NAttValue( NPC^.NA , NAG_CharDescription , -T );
if V > 0 then begin
HarvestMessages( NPC_Chatter_Standard , msg_label + '_T' + BStr( T ) + '+' );
end else if V < 0 then begin
HarvestMessages( NPC_Chatter_Standard , msg_label + '_T' + BStr( T ) + '-' );
end;
end;
end;
{ If at least one phrase was found, and the NPC is visible, it can say something. }
if ( MList <> Nil ) and ( ( Msg_Label = 'CHAT_EJECT' ) or MekVisible( GB , FindRoot( NPC ) ) ) then begin
DialogMsg( '[' + GearName( NPC ) + ']: ' + SelectRandomSAtt( MList )^.Info );
end;
{ Add the chatter recharge time. }
SetNAtt( FindRoot( NPC )^.NA , NAG_EpisodeData , NAS_ChatterRecharge , GB^.ComTime + ( 2500 div CStat( NPC , STAT_Charm ) ) );
{ Get rid of our message list. }
DisposeSAtt( MList );
end;
Function HotTileBlocksLOS( GB: GameBoardPtr; X , Y , MM: Integer ): Boolean;
{ Return TRUE if the given tile should block AI movement. }
{ BUGS: X,Y must lie on the map; MM must be in range 0..NumMoveMode. }
begin
HotTileBlocksLOS := TileBlocksLOS( GB , X , Y , 5 ) or ( Hot_Terr[ MM , GB^.Map[ X , Y ].Terr ] < 0 );
end;
Procedure HotMapFloodFill( GB: GameBoardPtr; N,MM: Integer );
{ Given a map that's had the hot spots filled in, use the }
{ flood fill algorithm to determine paths etc. }
var
X,Y: Integer;
Flag: Boolean;
DV,DH,DD,DP: Integer; { Distance Vertical, Horizontal, and Diagnol. }
begin
flag := True;
while flag do begin
flag := False;
for y := 2 to (YMax-1) do begin
for x := 2 to (XMax-1) do begin
if not HotTileBlocksLOS( GB , X , Y , MM ) then begin
dH := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + HotMap[ N , X - 1 , Y ];
dV := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + HotMap[ N , X , Y - 1 ];
if DV < DH then DH := DV;
DD := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + 1 + HotMap[ N , X - 1 , Y - 1];
if DD < DH then DH := DD;
DP := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + 1 + HotMap[ N , X + 1 , Y - 1];
if DP < DH then DH := DP;
if DH < HotMap[ N , X , Y ] then begin
HotMap[ N , X , Y ] := DH;
flag := True;
end;
end;
end;
end;
for y := ( YMax - 1 ) downto 2 do begin
for x := ( XMax - 1 ) downto 2 do begin
if not HotTileBlocksLOS( GB , X , Y , MM ) then begin
dH := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + HotMap[ N , X + 1 , Y ];
dV := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + HotMap[ N , X , Y + 1 ];
if DV < DH then DH := DV;
DD := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + 1 + HotMap[ N , X + 1 , Y + 1];
if DD < DH then DH := DD;
DP := Hot_Terr[ MM , GB^.Map[X,Y].terr ] + 1 + HotMap[ N , X - 1 , Y + 1];
if DP < DH then DH := DP;
if DH < HotMap[ N , X , Y ] then begin
HotMap[ N , X , Y ] := DH;
flag := True;
end;
end;
end;
end;
end;
end;
Procedure Calc_FollowMap( GB: GameBoardPtr; N,UID,MM: Integer );
{ Unlike the SEEK AND DESTROY map, this map should have only }
{ one hot spot. }
var
M: GearPtr;
P: Point;
begin
M := GB^.Meks;
while M <> Nil do begin
P := GearCurrentLocation( M );
if IsMasterGear( M ) and OnTheMap( P.X , P.Y ) and GearOperational( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end else if ( M^.G = GG_MetaTerrain ) and ( M^.Stat[ STAT_Pass ] <= -100 ) and NotDestroyed( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end;
if NAttValue( M^.NA , NAG_EpisodeData , NAS_UID ) = UID then begin
P := GearCurrentLocation( M );
if OnTheMap( P.X , P.Y ) then begin
HotMap[ N , P.X , P.Y ] := 0;
HotMapFloodFill( GB , N , MM );
end;
end;
M := M^.Next;
end;
end;
Procedure Calc_SpotMap( GB: GameBoardPtr; N,PDat,MM: Integer );
{ Calculate a map seeking the described point. }
var
M: GearPtr;
P: Point;
begin
{ Set the position for all blocking gears on the coldmap. }
M := GB^.Meks;
while M <> Nil do begin
P := GearCurrentLocation( M );
if IsMasterGear( M ) and OnTheMap( P.X , P.Y ) and GearOperational( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end else if ( M^.G = GG_MetaTerrain ) and ( M^.Stat[ STAT_Pass ] <= -100 ) and NotDestroyed( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end;
M := M^.Next;
end;
{ Set the hot spot. }
P.X := PDat mod ( XMax + 1 );
P.Y := PDat div ( XMax + 1 );
if OnTheMap( P.X , P.Y ) then HotMap[ N , P.X , P.Y ] := 0;
{ Increase the timer for this map, since we're dealing with a }
{ stationary position, so the data should remain good for a }
{ nice long time. }
MapUpdate[ N ] := GB^.ComTime + 100;
HotMapFloodFill( GB , N , MM );
end;
Procedure Calc_EdgeMap( GB: GameBoardPtr; N,PDat,MM: Integer );
{ Calculate a map seeking the described edges. }
var
M: GearPtr;
P: Point;
X,Y: Integer;
begin
{ Set the position for all blocking gears on the coldmap. }
M := GB^.Meks;
while M <> Nil do begin
P := GearCurrentLocation( M );
if IsMasterGear( M ) and OnTheMap( P.X , P.Y ) and GearOperational( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end else if ( M^.G = GG_MetaTerrain ) and ( M^.Stat[ STAT_Pass ] <= -100 ) and NotDestroyed( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end;
M := M^.Next;
end;
{ Set the hot spots. }
for X := 1 to XMax do begin
if AngDir[ PDat , 2 ] = -1 then HotMap[ N , X , 1 ] := 0
else if AngDir[ PDat , 2 ] = 1 then HotMap[ N , X , YMax ] := 0;
end;
for Y := 1 to YMax do begin
if AngDir[ PDat , 1 ] = -1 then HotMap[ N , 1, Y ] := 0
else if AngDir[ PDat , 1 ] = 1 then HotMap[ N , XMax , Y ] := 0;
end;
{ Increase the timer for this map, since we're dealing with a }
{ stationary position, so the data should remain good for a }
{ nice long time. }
MapUpdate[ N ] := GB^.ComTime + 100;
HotMapFloodFill( GB , N , MM );
end;
Procedure Calc_SDMap( GB: GameBoardPtr; N,Team,MM: Integer );
{ Calculate a SEEK AND DESTROY hotmap for a member of TEAM using }
{ movement mode MM. }
var
M: GearPtr;
Flag: Boolean;
P: Point;
begin
{ Set the position for all enemies of the listed team to 0 on hotmap. }
{ Set the position for all blocking gears on the coldmap. }
M := GB^.Meks;
flag := True;
while M <> Nil do begin
P := GearCurrentLocation( M );
if IsMasterGear( M ) and OnTheMap( P.X , P.Y ) and GearOperational( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
if TeamCanSeeTarget( GB , Team , M ) and AreEnemies( GB , Team , NAttValue( M^.NA , NAG_Location , NAS_Team ) ) then begin
HotMap[ N , P.X , P.Y ] := 0;
Flag := False;
end;
end else if ( M^.G = GG_MetaTerrain ) and ( M^.Stat[ STAT_Pass ] <= -100 ) and NotDestroyed( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end;
M := M^.Next;
end;
{ If no visible enemies found, no point in continuing with the }
{ algorithm. }
if Flag then Exit;
HotMapFloodFill( GB , N , MM );
end;
Procedure Calc_TeamMap( GB: GameBoardPtr; N,Team,MM: Integer );
{ Calculate a FOLLOW TEAM hotmap for a member of TEAM using }
{ movement mode MM. }
var
M: GearPtr;
Flag: Boolean;
P: Point;
begin
{ Set the position for all enemies of the listed team to 0 on hotmap. }
{ Set the position for all blocking gears on the coldmap. }
M := GB^.Meks;
flag := True;
while M <> Nil do begin
P := GearCurrentLocation( M );
if IsMasterGear( M ) and OnTheMap( P.X , P.Y ) and GearOperational( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
if ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = Team ) then begin
HotMap[ N , P.X , P.Y ] := 0;
Flag := False;
end;
end else if ( M^.G = GG_MetaTerrain ) and ( M^.Stat[ STAT_Pass ] <= -100 ) and NotDestroyed( M ) then begin
Inc( ColdMap[ N , P.X , P.Y ] );
end;
M := M^.Next;
end;
{ If no visible enemies found, no point in continuing with the }
{ algorithm. }
if Flag then Exit;
HotMapFloodFill( GB , N , MM );
end;
Procedure CalculateHotMap( GB: GameBoardPtr; N,Team,MM,O: Integer );
{ Calculate a hot map, choosing type based on the ORDER given. }
var
X,Y: Integer;
begin
MapUpdate[n] := GB^.ComTime + Hot_Map_Validity;
MapTeam[n] := Team;
MapMoveMode[n] := MM;
MapOrder[n] := O;
{ Clear out the entire map, first. }
For X := 1 to XMax do begin
For Y := 1 to YMax do begin
HotMap[ N , X , Y ] := 9999;
ColdMap[ N , X , Y ] := 0;
end;
end;
UpdateShadowMap( GB );
if O = ORD_SeekSingleModel then begin
Calc_FollowMap( GB , N , Team , MM );
end else if O = ORD_SeekSpot then begin
Calc_SpotMap( GB , N , Team , MM );
end else if O = ORD_SeekEdge then begin
Calc_EdgeMap( GB , N , Team , MM );
end else if O = ORD_SeekTeam then begin
Calc_TeamMap( GB , N , Team , MM );
end else begin
Calc_SDMap( GB , N , Team , MM );
end;
end;
Procedure ClearHotMaps;
{ Clear all the currently processed floodfill maps. }
var
t: Integer;
begin
for t := 1 to NumFFMap do MapUpdate[ T ] := -1;
end;
Function GetHotMap( GB: GameBoardPtr; Team,MM,O: Integer ): Integer;
{ Locate an appropriate HotMap for the data provided. }
var
RepMap,GoodMap,T: Integer;
begin
RepMap := 1;
GoodMap := 0;
for T := 1 to NumFFMap do begin
if ( MapUpdate[ t ] >= GB^.ComTime ) and ( MapTeam[ t ] = Team ) and ( MapMoveMode[ t ] = MM ) and ( MapOrder[ t ] = O ) then begin
GoodMap := T;
end;
if MapUpdate[ t ] < MapUpdate[ RepMap ] then RepMap := T;
end;
if GoodMap <> 0 then begin
GetHotMap := GoodMap;
end else begin
CalculateHotMap( GB , RepMap , Team , MM , O );
GetHotMap := RepMap;
end;
end;
procedure AIAttacker( GB: GameBoardPtr; Mek,Weapon,Target: GearPtr );
{ MEK wants to fire WEP at TAR. }
var
AttSkillVal, DefSkillVal: Integer; { Used to calculate the chances of hitting. }
{ Thanks to Peter Cordes }
AtOp: Integer;
T2,T3: GearPtr;
begin
{ Calculate AttSkillVal, DefSkillVal, and set AtOp to 0. }
if Target^.G = GG_Mecha then DefSkillVal := SkillValue( Target, 5 )
else DefSkillVal := SkillValue( Target, 10 );
AttSkillVal := SkillValue( Mek , AttackSkillNeeded( Weapon ) ) + CalcTotalModifiers( gb , Weapon , Target , 0 , WeaponAttackAttributes( Weapon ) );
AtOp := 0;
{ If the odds of hitting are good enough, the attacker may try }
{ to make a called shot. }
if not NoCalledShots(WeaponAttackAttributes(Weapon),0) and (AttSkillVal-DefSkillVal > 4+Random(10)) and (Random(3) <> 1) and ((Weapon^.S <> GS_Ballistic) or (Weapon^.S <> GS_BeamGun) or (Random(6)+Random(6) > Weapon^.Stat[STAT_BurstValue])) then begin
T2 := Target^.SubCom;
if WeaponDC( Weapon , 0 ) > 5 then begin
{ If using a powerful weapon, do aimed shot at the torso. }
while ( T2 <> Nil ) and (( T2^.G <> GG_Module ) or ( T2^.S <> GS_Body )) do T2 := T2^.Next;
end else if T2 <> Nil then begin
{ If using a less powerful weapon, do aimed shot at the part with the lowest armor. }
T3 := Nil;
while T2 <> Nil do begin
if T3 = Nil then T3 := T2
else if GearCurrentArmor( T2 ) < GearCurrentArmor( T3 ) then T3 := T2;
T2 := T2^.Next;
end;
T2 := T3;
end;
if T2 <> Nil then Target := T2;
end else if ( Weapon^.G = GG_Weapon ) then begin
{ If not making a called shot, the attacker will take }
{ advantage of rapid fire if possible. }
if ( Weapon^.S = GS_Ballistic ) and ( Weapon^.Stat[ STAT_BurstValue ] > 0 ) then begin
AtOp := Weapon^.Stat[ STAT_BurstValue ];
end else if ( Weapon^.S = GS_BeamGun ) and ( Weapon^.Stat[ STAT_BurstValue ] > 0 ) then begin
AtOp := Weapon^.Stat[ STAT_BurstValue ];
end else if Weapon^.S = GS_Missile then begin
AtOp := Random( Weapon^.Stat[ STAT_Magazine ] );
end;
end;
AttackerFrontEnd( GB , Mek , Weapon , Target , AtOp );
{ Set the initiative recharge counter. }
SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_InitRecharge , GB^.ComTime + ReactionTime( Mek ) );
end;
procedure SelectMoveMode( Mek: GearPtr; GB: GameBoardPtr );
{ Set the mek's MoveMode attribute to the highest }
{ active movemode that this mek has. }
var
T,MM,MaxSpeed: Integer;
begin
MM := 0;
MaxSpeed := 0;
for T := 1 to NumMoveMode do begin
if ( BaseMoveRate( Mek , T ) > MaxSpeed ) and MoveLegal( Mek , T , NAV_NormSpeed , GB^.Comtime ) then begin
if not ( ( GB <> Nil ) and ( GB^.Scale > Mek^.Scale ) and ( T = MM_Fly ) and ( JumpTime( Mek ) > 0 ) ) then begin
MM := T;
MaxSpeed := BaseMoveRate( Mek , T );
end;
end;
end;
if MM <> 0 then SetNAtt( Mek^.NA , NAG_Action , NAS_MoveMode , MM);
end;
Function SelectBestWeapon( GB: GameBoardPtr; Mek,Target: GearPtr ): GearPtr;
{ Select the best weapon for attacking TARGET with. }
var
weapon: GearPtr;
BestWeight: LongInt;
function SafeToFire( Part: GearPtr ): Boolean;
{ Return TRUE if firing this weapon won't cause a huge problem, }
{ or FALSE otherwise. }
var
Found: Boolean;
R: Integer;
P: Point;
M2: GearPtr;
begin
{ First off, dumb things won't care if it's safe to fire or not. }
if ( Mek^.G = GG_Character ) and ( Mek^.Stat[ STAT_Knowledge ] < RollStep( 5 ) ) then begin
Exit( True );
end;
{ Check to see if this is a blast weapon. }
if HasAttackAttribute( WeaponAttackAttributes( Part ) , AA_BlastAttack ) then begin
{ If there's an ally within the blast radius, this weapon isn't safe. }
P := GearCurrentLocation( Target );
M2 := GB^.Meks;
R := BlastRadius( GB , Part , WeaponAttackAttributes( Part ) );
Found := False;
while M2 <> Nil do begin
if AreAllies( GB , Mek , M2 ) and ( Range( M2 , P.X , P.Y ) < R ) then Found := True;
M2 := M2^.Next;
end;
SafeToFire := Not Found;
end else begin
{ The blastattack check is the only one I'm doing yet. }
SafeToFire := True;
end;
end;
function WGoodness( Part, Target: GearPtr ): Integer;
{ This is the heuristic SeekBigWeapon uses }
var
WG,AS,AM : Integer;
AttSkillVal, DefSkillVal : Integer;
begin
{ Can your lancemates size up the abilities of a defender in a }
{ mecha, very far away, in heavy cover? Sure they can! }
{ Just look at Piloting or Dodge, not talents, parrying, ... }
if Target^.G = GG_Mecha then DefSkillVal := SkillValue( Target, 5 )
else DefSkillVal := SkillValue( Target, 10 );
{ NOTE: the actual attack code does RollStep(SkillValue) + }
{ modifiers, so this is an approximation }
{ Missiles will have BustValue = 0. Don't fire }
{ until you can see the whites of their eyes. }
AS := SkillValue( FindRoot(Part) , AttackSkillNeeded( Part ) );
AM := CalcTotalModifiers( gb , Part , Target , Part^.Stat[ STAT_BurstValue ] , WeaponAttackAttributes( Part ) );
AttSkillVal := AS + AM;
WG := Part^.V * (1+Part^.Stat[ STAT_BurstValue ]) + 2*(AttSkillVal-DefSkillVal);
for AS := 1 to Part^.Scale do WG := WG * 3;
WGoodness := WG;
end;
procedure SeekBigWeapon( Part: GearPtr );
{ Seek a weapon which is capable of hitting target. }
{ Select the best weapon, weighted by accuracy and damage }
{ Things we don't do: }
{ * scale factors }
{ * avoid wasting ammo/recharge time on easy-to-kill targets }
{ (it would be worth working on the target selection }
{ algorithm if you were going to do anything about this. }
{ Maybe find your best weapon and look for targets to }
{ fire it at. This could help a lot with LINE weapons. }
{ Or even find a weight for every weapon/target pair...) }
var
{Range : Integer;}
Weight : Integer;
begin
while ( Part <> Nil ) do begin
if ( Part^.G = GG_Module ) or ( Part^.G = GG_Weapon ) then begin
if ReadyToFire( GB , Mek , Part ) and RangeArcCheck( GB , Mek , Part , Target ) and SafeToFire( Part ) then begin
if Weapon = Nil then begin
Weapon := Part;
BestWeight := WGoodness(Part, Target);
end else begin
Weight := WGoodness(Part, Target);
{ BestWeight is a variable in the parent procedure. I don't know how else to do this in Pascal... }
if Weight > BestWeight then begin
Weapon := Part;
BestWeight := Weight;
end;
end; end;
end;
if ( Part^.SubCom <> Nil ) then SeekBigWeapon( Part^.SubCom );
if ( Part^.InvCom <> Nil ) then SeekBigWeapon( Part^.InvCom );
Part := Part^.Next;
end;
end;
begin
Weapon := Nil;
BestWeight := -10000;
SeekBigWeapon( Mek^.SubCom );
SeekBigWeapon( Mek^.InvCom );
SelectBestWeapon := Weapon;
end;
Procedure AttackTargetOfOppurtunity( GB: GameBoardPtr; Mek: GearPtr );
{ Look for the most oppurtune target to attack. }
var
weapon: GearPtr;
TL,Target: GearPtr;
BestWeight: Longint;
{ *** PROCEDURES BLOCK *** }
procedure SeekFarWeapon( Part: GearPtr );
{ Find the weapon with the longest current range. }
begin
while ( Part <> Nil ) do begin
if ( Part^.G = GG_Module ) or ( Part^.G = GG_Weapon ) then begin
if ReadyToFire( GB , Mek , Part ) then begin
if Weapon = Nil then Weapon := Part
else if WeaponRange( GB , Part ) > WeaponRange( GB , Weapon ) then Weapon := Part;
end;
end;
if ( Part^.SubCom <> Nil ) then SeekFarWeapon( Part^.SubCom );
if ( Part^.InvCom <> Nil ) then SeekFarWeapon( Part^.InvCom );
Part := Part^.Next;
end;
end;
function SafeToFire( Part: GearPtr ): Boolean;
{ Return TRUE if firing this weapon won't cause a huge problem, }
{ or FALSE otherwise. }
var
Found: Boolean;
N, R, T, TT: Integer;
P, P1, P2: Point;
M2: GearPtr;
begin
{ First off, dumb things won't care if it's safe to fire or not. }
if ( Mek^.G = GG_Character ) and ( Mek^.Stat[ STAT_Knowledge ] < Random( 6 ) ) then begin
Exit( True );
end;
Found := False;
{ Check to see if this is a blast weapon. }
if HasAttackAttribute( WeaponAttackAttributes( Part ) , AA_BlastAttack ) then begin
{ If there's an ally within the blast radius, this weapon isn't safe. }
P := GearCurrentLocation( Target );
M2 := GB^.Meks;
R := BlastRadius( GB , Part , WeaponAttackAttributes( Part ) );
while M2 <> Nil do begin
if AreAllies( GB , Mek , M2 ) and ( Range( M2 , P.X , P.Y ) < R ) then Found := True;
M2 := M2^.Next;
end;
end else if HasAttackAttribute( WeaponAttackAttributes( Part ) , AA_LineAttack ) then begin
{ code from effects.pp:DoLineAttack() }
P1 := GearCurrentLocation( Part );
P1.Z := MekAltitude( GB , Part );
P2 := GearCurrentLocation( Target );
P2.Z := MekAltitude( GB, Target );
P.Z := P1.Z;
R := WeaponRange( GB, Part );
T := 0;
UpdateShadowMap( GB );
while ( T < R ) do begin
Inc( T );
P := SolveLine( P1.X , P1.Y , P1.Z , P2.X , P2.Y , P2.Z , T );
if OnTheMap( P.X , P.Y ) then begin
N := NumGearsXY( GB , P.X , P.Y );
if N > 0 then begin
for tt := 1 to N do begin
M2 := FindGearXY( GB , P.X , P.Y , TT );
if (Abs(MekAltitude( GB , M2 ) - P.Z ) <= 1) and NotDestroyed( M2 ) and AreAllies( GB , Mek , M2 ) then Found := True;
end;
end;
if TileBlocksLOS( GB , P.X , P.Y , P.Z ) then T := R;
end;
end;
end;
{ The blastattack check is the only one I'm doing yet. }
{ Line attack added. Nothing sucks like your non-sentient robot }
{ killing you with a plasma cannon in one shot }
SafeToFire := Not Found;
end;
begin
{ First, check to make sure that the mecha hasn't attacked }
{ too recently. }
if NAttValue( Mek^.NA , NAG_EpisodeData , NAS_InitRecharge ) > GB^.ComTime then Exit;
{ Start by finding a good weapon to fire with. }
{ Preference will be given to the weapon with the longest range. }
Weapon := Nil;
BestWeight := -10000;
SeekFarWeapon( Mek^.SubCom );
SeekFarWeapon( Mek^.InvCom );
if Weapon <> Nil then begin
{ Next, see if we have an appropriate target. }
Target := Nil;
TL := gb^.meks;
while TL <> Nil do begin
if AreEnemies( GB , Mek , TL ) and OnTheMap( TL ) and RangeArcCheck( GB , Mek , Weapon , TL ) and GearActive( TL ) and MekCanSeeTarget( GB , Mek , TL ) then begin
if Target = Nil then Target := TL
else if Range( gb , Target , Mek ) > Range( gb , TL , Mek ) then Target := TL;
end;
TL := TL^.Next;
end;
{ If a target was found, fire at that target with the }
{ biggest weapon in the arsenal. }
if Target <> Nil then begin
Weapon := SelectBestWeapon( GB , Mek , Target );
if Weapon <> Nil then AIAttacker( GB , Mek , Weapon , Target );
end;
end;
end;
Procedure Wander( Mek: GearPtr; GB: GameBoardPtr );
{ Just wander about. This procedure is often called when }
{ conventional pathfinding methods have failed. It is also }
{ used as the "SEEK" part of "SEEK AND DESTROY". }
var
P: Point;
CD: Integer;
begin
{ Determine current facing and position. }
P := GearCurrentLocation( Mek );
CD := NAttValue( Mek^.NA , NAG_Location , NAS_D );
{ If the current direction of travel doesn't lead off the map, }
{ and isn't blocked, just move foreword. }
if OnTheMap( P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ) and ( Random( 5 ) <> 1 ) and not MoveBlocked( Mek , GB ) then begin
if Mek^.G = GG_Mecha then begin
PrepAction( GB , Mek , NAV_FullSpeed );
end else begin
PrepAction( GB , Mek , NAV_NormSpeed );
end;
end else begin
if Random( 2 ) = 1 then begin
PrepAction( GB , Mek , NAV_TurnRight );
end else begin
PrepAction( GB , Mek , NAV_TurnLeft );
end;
end;
end;
Function XMoveTowardsGoal( GB: GameBoardPtr; Mek: GearPtr; HM,OptMax,OptMin: Integer ): Boolean;
{ Using hotmap N as a guide, attempt to move towards the goal. }
{ OPTMAX and OPTMIN describe the maximum and minumum hotmap values the mek will }
{ try to reach. If its current location has a hot value lower than OptMax, it }
{ will stay in that spot. If its current location is lower than OptMin, it will }
{ attempt to move to a location with a higher hot value. }
Function ThisMoveIsOkay( MAct,X,Y: Integer ): Boolean;
{ This function combines two checks: Whether or not the target tile is }
{ unblocked, and whether or not the specified moveaction is legal. }
begin
ThisMoveIsOkay := MoveLegal( Mek , MAct , GB^.ComTime ) and not IsBlocked( Mek , GB , X , Y );
end;
var
P,P2: Point;
T,D,Best,CD: Integer;
begin
P := GearCurrentLocation( Mek );
{ If our current direction of travel is bringing us closer to the }
{ target, keep moving in that direction, even if it isn't the }
{ optimal path. }
CD := NAttValue( Mek^.NA , NAG_Location , NAS_D );
if HotMap[ HM , P.X ,P.Y ] < OptMin then begin
{ Too close to the target- move farther away. }
P2.X := P.X + AngDir[ ( CD + 4 ) mod 8 , 1 ];
P2.Y := P.Y + AngDir[ ( CD + 4 ) mod 8 , 2 ];
if OnTheMap( P2.X , P2.Y ) and ( HotMap[ HM , P2.X , P2.Y ] > HotMap[ HM , P.X , P.Y ] ) and ThisMoveIsOkay( NAV_Reverse , P2.X , P2.Y ) then begin
PrepAction( GB , Mek , NAV_Reverse );
XMoveTowardsGoal := True;
end else if OnTheMap( P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ) and ( HotMap[ HM , P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ] > HotMap[ HM , P.X , P.Y ] ) and ThisMoveIsOkay( NAV_NormSpeed , P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ) then begin
if Mek^.G = GG_Character then begin
PrepAction( GB , Mek , NAV_NormSpeed );
end else begin
PrepAction( GB , Mek , NAV_FullSpeed );
end;
XMoveTowardsGoal := True;
end else begin
D := -1;
Best := HotMap[ HM , P.X , P.Y ];
for t := 0 to 7 do begin
if OnTheMap( P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ) then begin
if ( HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ] > Best ) and ( ColdMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ] < 1 ) and not IsBlocked( Mek , GB , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ) then begin
Best := HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ];
D := T;
end;
end;
end;
if D <> -1 then begin
if CD <> D then begin
Best := NAV_TurnRight;
for t := 1 to 4 do begin
if (( CD + T ) mod 8 ) = D then Best := NAV_TurnRight
else if (( CD + 8 - T ) mod 8 ) = D then Best := NAV_TurnLeft;
end;
PrepAction( GB , Mek , Best );
end else begin
PrepAction( GB , Mek , NAV_FullSpeed );
end;
XMoveTowardsGoal := True;
end else begin
{ If we don't have a good place to go, }
{ try changing move modes to the lowest. }
if Random( 2 ) = 1 then begin
GearUp( Mek );
end;
XMoveTowardsGoal := False;
end;
end;
end else if ( HotMap[ HM , P.X ,P.Y ] < OptMax ) and ( ( Random( 15 ) < HotMap[ HM , P.X ,P.Y ] ) or IsInCover( GB , Mek ) ) then begin
{ Within optimal range. Turn to hopefully face a target. }
D := -1;
Best := HotMap[ HM , P.X , P.Y ];
for t := 0 to 7 do begin
if OnTheMap( P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ) then begin
if ( HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ] < Best ) then begin
Best := HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ];
D := T;
end;
end;
end;
{ If a good direction was found, turn to face it. }
{ Otherwise assume that we're already facing a good direction. }
if D <> -1 then begin
if CD <> D then begin
Best := NAV_TurnRight;
for t := 1 to 4 do begin
if (( CD + T ) mod 8 ) = D then Best := NAV_TurnRight
else if (( CD + 8 - T ) mod 8 ) = D then Best := NAV_TurnLeft;
end;
PrepAction( GB , Mek , Best );
end else begin
WaitAMinute( GB , Mek , ReactionTime( Mek ) );
end;
end else begin
{ If we don't have a good place to go, }
{ try changing move modes to the lowest. }
if Random( 2 ) = 1 then begin
GearUp( Mek );
end;
WaitAMinute( GB , Mek , ReactionTime( Mek ) );
end;
XMoveTowardsGoal := True;
end else if OnTheMap( P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ) and ( HotMap[ HM , P.X + AngDir[ CD , 1 ] , P.Y + AngDir[ CD , 2 ] ] < HotMap[ HM , P.X , P.Y ] ) and ( HotMap[ HM , P.X , P.Y ] < Random( 4 ) ) and not MoveBlocked( Mek , GB ) then begin
if (( Mek^.G = GG_Mecha ) or ( CurrentStamina( Mek ) > 10 ) ) and MoveLegal( Mek , NAV_FullSpeed , GB^.ComTime ) then begin
PrepAction( GB , Mek , NAV_FullSpeed );
end else if MoveLegal( Mek , NAV_NormSpeed , GB^.ComTime ) then begin
PrepAction( GB , Mek , NAV_NormSpeed );
end else begin
Exit( False );
end;
XMoveTowardsGoal := True;
end else begin
{ Check to see if there's a better direction we can be moving in. }
D := -1;
Best := HotMap[ HM , P.X , P.Y ];
for t := 0 to 7 do begin
if OnTheMap( P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ) then begin
if ( HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ] < Best ) and ( ColdMap[ HM , P.X + AngDir[T,1] , P.Y + AngDir[T,2] ] < 1 ) and not IsBlocked( Mek,GB,P.X + AngDir[T,1] , P.Y + AngDir[T,2]) then begin
Best := HotMap[ HM , P.X + AngDir[ T , 1 ] , P.Y + AngDir[ T , 2 ] ];
D := T;
end;
end;
end;
if D <> -1 then begin
if CD <> D then begin
Best := NAV_TurnRight;
for t := 1 to 4 do begin
if (( CD + T ) mod 8 ) = D then Best := NAV_TurnRight
else if (( CD + 8 - T ) mod 8 ) = D then Best := NAV_TurnLeft;
end;
PrepAction( GB , Mek , Best );
end else begin
if MoveBlocked( Mek , GB ) then begin
Exit( False );
end else if ( HotMap[ HM , P.X ,P.Y ] > ( OptMax * 3 div 2 ) ) and (( Mek^.G = GG_Mecha ) or ( CurrentStamina( Mek ) > 10 ) ) and MoveLegal( Mek , NAV_FullSpeed, GB^.ComTime ) then begin
PrepAction( GB , Mek , NAV_FullSpeed );
end else if MoveLegal( Mek , NAV_NormSpeed, GB^.ComTime ) then begin
PrepAction( GB , Mek , NAV_NormSpeed );
end else begin
Exit( False );
end;
end;
XMoveTowardsGoal := True;
end else begin
{ If we don't have a good place to go, }
{ try changing move modes to the lowest. }
if Random( 2 ) = 1 then begin
GearUp( Mek );
end;
XMoveTowardsGoal := False;
end;
end;
end;
Procedure MoveTowardsGoal( GB: GameBoardPtr; Mek: GearPtr; HM: Integer );
{ Front-end for the Extended Move Towards Goal. }
begin
if not XMoveTowardsGoal( GB , Mek , HM , 0 , 0 ) then Wander( Mek , GB );
end;
Function DoorPresent( GB: GameBoardPtr; X,Y: Integer ): Boolean;
{ Return TRUE if there's a door here which a passive NPC shouldn't pass. }
begin
if not OnTheMap( X , Y ) then begin
DoorPresent := False;
end else begin
DoorPresent := GB^.Map[ X , Y ].terr = TERRAIN_Threshold;
end;
end;
Procedure MillAround( GB: GameBoardPtr; Mek: GearPtr );
{ Stay in roughly the same position, wander around a little bit. }
var
P: Point;
D: Integer;
begin
{ No reason to panic. Just stand around; }
{ maybe move if it's okay. }
if Random( 3 ) = 1 then begin
PrepAction( GB , Mek , NAV_Stop );
end else if Random( 3 ) = 1 then begin
{ The passive character may walk sometimes. }
P := GearCurrentLocation( Mek );
D := NAttValue( Mek^.NA , NAG_Location , NAS_D );
P.X := P.X + AngDir[ D , 1 ];
P.Y := P.Y + AngDir[ D , 2 ];
if OnTheMap( P.X , P.Y ) then begin
{ Don't move foreword if the move is blocked }
{ or if the move would take the character over }
{ the threshold of a door. }
if IsBlocked( Mek , GB , P.X , P.Y ) or DoorPresent( GB , P.X , P.Y ) then begin
PrepAction( GB , Mek , NAV_Stop );
end else begin
PrepAction( GB , Mek , NAV_NormSpeed );
end;
end;
end else begin
if Random( 2 ) = 1 then begin
PrepAction( GB , Mek , NAV_TurnRight );