-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbotai_actions.c
1089 lines (968 loc) · 31.8 KB
/
botai_actions.c
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
#ifdef LUA_BOT
#include "botai_sensors.h"
#include "botai_actions.h"
// sensors.c
extern VECTOR FollowTargetPos;
extern VECTOR MissileOrigDirVector;
extern bool AllSensorsClear;
extern bool DEBUG_AVOIDANCE;
extern bool FriendlyFire;
extern bool MissileAvoidanceSet;
extern bool ReverseNetwork;
extern bool initialised;
extern float DistWall[NUM_SENSORS];
extern float IFiredTitan;
extern float PrevHull;
extern float PrevShield;
extern float TargetShipDistance;
extern float WhenWillHitSlide[NUM_SENSORS];
extern float accuracy;
extern int AvoidMove;
extern int CurrentNode;
extern int FollowTargetGroup;
extern int GettingPickup;
extern int HomingMissile;
extern int PrevDeaths;
extern int PrevHomingMissile;
extern int PrevKills;
extern int TargetMine;
extern int TargetNode;
extern int TargetShipHealth;
extern int TargetShipID;
extern char * WepMessages[];
void BOTAI_ClearActions()
{
BOTAI_SetAction( &bot.forward, 0.0F, "ClearActions(): forward" );
BOTAI_SetAction( &bot.up, 0.0F, "ClearActions(): up" );
BOTAI_SetAction( &bot.right, 0.0F, "ClearActions(): right" );
BOTAI_SetAction( &bot.pitch, 0.0F, "ClearActions(): pitch" );
BOTAI_SetAction( &bot.yaw, 0.0F, "ClearActions(): yaw" );
BOTAI_SetAction( &bot.roll, 0.0F, "ClearActions(): roll" );
BOTAI_SetAction( &bot.turbo, 0.0F, "ClearActions(): turbo" );
BOTAI_SetAction( &bot.fire_primary, 0.0F, "ClearActions(): fire_primary" );
BOTAI_SetAction( &bot.fire_secondary, 0.0F, "ClearActions(): fire_secondary" );
}
bool BOTAI_SetAction( float * action, float value, char * str )
{
// enforce bounds
if( value < -1.0F )
value = -1.0F;
else if( value > 1.0F )
value = 1.0F;
// try and set action
if( *action != 0.0F && value != 0.0F )
{
DebugPrintf("%s already set\n", str);
return false;
}
else
{
DebugPrintf("%s set\n", str);
*action = value;
return true;
}
}
bool BOTAI_FireTitan()
{
int i;
int wall;
float dist;
float closest = BIGDISTANCE;
VECTOR TempVector;
VECTOR Move_Dir;
VECTOR ColPnt;
u_int16_t TmpGroup;
// no target to fire at
if( TargetShipID < 0 )
return false;
// do we have the titan?
if( TITANSTARMISSILE != Ships[WhoIAm].Secondary && SecondaryAmmo[TITANSTARMISSILE] <= 0)
return false;
else
Ships[WhoIAm].Secondary = TITANSTARMISSILE;
// too close to target or too far away
dist = DistanceVector2Vector(&Ships[WhoIAm].Object.Pos, &Ships[TargetShipID].Object.Pos);
if(dist < 500.0F || dist > 2000.0F)
{
//DebugPrintf("too close or too far to target to fire titan\n");
return false;
}
// for each r/l/u/d wall sensor
for(i = 0; i < 4; i++)
{
// identify the closest wall
if(DistWall[i] < closest)
{
closest = DistWall[i];
wall = i;
}
}
// slide to the wall
if(closest > 150.0F)
{
switch(wall)
{
case 0:
BOTAI_SetAction( &bot.right, 1.0F, "FireTitan() right" );
break;
case 1:
BOTAI_SetAction( &bot.right, -1.0F, "FireTitan() left" );
break;
case 2:
BOTAI_SetAction( &bot.up, 1.0F, "FireTitan() up" );
break;
case 3:
BOTAI_SetAction( &bot.up, -1.0F, "FireTitan() down" );
break;
}
}
// aim towards wall
else
{
// calculate collision point
ApplyMatrix( &Ships[WhoIAm].Object.FinalMat, &Forward, &Move_Dir );
Move_Dir.x *= MaxColDistance;
Move_Dir.y *= MaxColDistance;
Move_Dir.z *= MaxColDistance;
BackgroundCollide( &MCloadheadert0, &Mloadheader, &Ships[WhoIAm].Object.Pos, Ships[WhoIAm].Object.Group, &Move_Dir, (VECTOR *) &ColPnt, &TmpGroup, &TempVector, &TempVector, false, NULL );
// distance between target and collision point should be less than distance between me and target
if( DistanceVector2Vector(&Ships[TargetShipID].Object.Pos, &ColPnt) > TargetShipDistance
// and target needs to be reachable by the collision point
|| ( !BOTAI_ClearLOS(&Ships[TargetShipID].Object.Pos, Ships[TargetShipID].Object.Group, &ColPnt) )
// and i have line of sight to the target
|| ( !BOTAI_ClearLOS(&Ships[WhoIAm].Object.Pos, Ships[WhoIAm].Object.Group, &Ships[TargetShipID].Object.Pos) ) )
{
//DebugPrintf("aiming at target\n");
BOTAI_AimAtTarget( &Ships[WhoIAm].Object.FinalInvMat, &Ships[WhoIAm].Object.Pos, &Ships[TargetShipID].Object.Pos);
}
// can see target so aim front sensor towards wall
else if(DistWall[8] > 500.0F)
{
//DebugPrintf("aiming toward wall\n");
switch(wall)
{
case 0:
BOTAI_SetAction( &bot.yaw, 1.0F, "FireTitan() yaw right" );
break;
case 1:
BOTAI_SetAction( &bot.yaw, -1.0F, "FireTitan() yaw left" );
break;
case 2:
BOTAI_SetAction( &bot.pitch, -1.0F, "FireTitan() pitch up" );
break;
case 3:
BOTAI_SetAction( &bot.pitch, 1.0F, "FireTitan() pitch down" );
break;
}
// helps to break alternating between too close and too far
BOTAI_SetAction( &bot.forward, -1.0F, "FireTitan() reverse" );
}
// can see target but i'm too close to wall
else if(DistWall[8] < 400.0F)
{
//DebugPrintf("aiming away from wall\n");
switch(wall)
{
case 0:
BOTAI_SetAction( &bot.yaw, -1.0F, "FireTitan() yaw left" );
break;
case 1:
BOTAI_SetAction( &bot.yaw, 1.0F, "FireTitan() yaw right" );
break;
case 2:
BOTAI_SetAction( &bot.pitch, 1.0F, "FireTitan() pitch down" );
break;
case 3:
BOTAI_SetAction( &bot.pitch, -1.0F, "FireTitan() pitch up" );
break;
}
// helps to break alternating between too close and too far
BOTAI_SetAction( &bot.forward, -1.0F, "FireTitan() reverse" );
}
// just right
else
{
//DebugPrintf("chocs away old boy!\n");
Ships[WhoIAm].Secondary = TITANSTARMISSILE;
BOTAI_SetAction( &bot.fire_secondary, 1.0F, "FireTitan() fire secondary" );
BOTAI_SetAction( &bot.forward, -1.0F, "FireTitan() reverse" );
BOTAI_SetAction( &bot.right, -1.0F, "FireTitan() left" );
IFiredTitan = 180.0F;
}
}
return true;
}
void BOTAI_FireMissiles()
{
// don't fire missiles if the target isn't in front of me and don't shoot directly into a wall
//if(!BOTAI_InViewCone( &Ships[WhoIAm].Object.Pos, &Ships[WhoIAm].Object.FinalMat, &Ships[TargetShipID].Object.Pos, (float) COSD( 45.0F ) ) && DistWall[8] < 200.0F)
// return;
// don't fire missiles unless we have a line of sight to the target
if( TargetShipID < 0 )
return;
if( !BOTAI_ClearLOS(&Ships[WhoIAm].Object.Pos, Ships[WhoIAm].Object.Group, &Ships[TargetShipID].Object.Pos))
return;
// set order of preference
if(SCATTERMISSILE == Ships[WhoIAm].Secondary || SecondaryAmmo[SCATTERMISSILE] > 0)
Ships[WhoIAm].Secondary = SCATTERMISSILE;
else if(SOLARISMISSILE == Ships[WhoIAm].Secondary || SecondaryAmmo[SOLARISMISSILE] > 0)
Ships[WhoIAm].Secondary = SOLARISMISSILE;
else if(MULTIPLEMISSILE == Ships[WhoIAm].Secondary || SecondaryAmmo[MULTIPLEMISSILE] > 0)
Ships[WhoIAm].Secondary = MULTIPLEMISSILE;
else if(MUGMISSILE == Ships[WhoIAm].Secondary || SecondaryAmmo[MUGMISSILE] > 0)
Ships[WhoIAm].Secondary = MUGMISSILE;
// fire missiles, but don't fire titan here
if(TITANSTARMISSILE != Ships[WhoIAm].Secondary)
BOTAI_SetAction( &bot.fire_secondary, 1.0F, "FireMissiles() fire secondary" );
}
#define TOL 50.0F
/* triple-chords to target pos in a straight line; returns true if reached target */
bool BOTAI_MoveToTargetNew(VECTOR * TPos)
{
VECTOR Slide;
VECTOR TriChordVector;
VECTOR WantedVector;
VECTOR NormVector;
VECTOR DirVector;
VECTOR FwdDirVector;
float xAngle;
float yAngle;
float Cos;
////DebugPrintf("moving to %f %f %f\n", TPos->x, TPos->y, TPos->z);
// -- calculate direction vector from my position to target
NormVector.x = TPos->x - Ships[WhoIAm].Object.Pos.x;
NormVector.y = TPos->y - Ships[WhoIAm].Object.Pos.y;
NormVector.z = TPos->z - Ships[WhoIAm].Object.Pos.z;
DirVector = NormVector;
// apply that vector to my matrix to make direction relative to my direction and rotation
//ApplyMatrix( &Ships[WhoIAm].Object.FinalInvMat, &NormVector, &DirVector );
NormaliseVector( &DirVector );
//DebugPrintf("straight x = %f y = %f z = %f\n", DirVector.x, DirVector.y, DirVector.z);
// calculate the desired vector
// forward right
Slide.x = 1.0F;
Slide.y = 0.0F;
Slide.z = 1.0F;
// calculate the triple chord direction vector
ApplyMatrix( &Ships[WhoIAm].Object.FinalMat, &Slide, &TriChordVector );
NormaliseVector( &TriChordVector );
//DebugPrintf("trivec x = %f y = %f z = %f\n", TriChordVector.x, TriChordVector.y, TriChordVector.z);
// difference between wanted vector and slide vector
WantedVector.x = DirVector.x - TriChordVector.x;
WantedVector.y = DirVector.y - TriChordVector.y;
WantedVector.z = DirVector.z - TriChordVector.z;
////DebugPrintf("W x = %f y = %f z = %f\n", WantedVector.x, WantedVector.y, WantedVector.z);
// angle between trichord vector and wanted vector, == 1.0F when aligned perfectly
Cos = DotProduct( &DirVector, &TriChordVector );
////DebugPrintf("Cos = %f\n", Cos);
return false;
// left/right angle
xAngle = (float) acos( WantedVector.x );
xAngle = 90.0F - R2D( xAngle );
if( WantedVector.z < 0.0F )
xAngle = 180.0F - xAngle;
if( xAngle > 180.0F )
xAngle -= 360.0F;
////DebugPrintf("X Angle %f\n", xAngle);
// up/down angle
yAngle = (float) acos( WantedVector.y );
yAngle = 90.0F - R2D( yAngle );
yAngle *= -1.0F;
if( yAngle > 180.0F )
yAngle -= 360.0F;
////DebugPrintf("Y Angle %f\n", yAngle);
if( xAngle > 0.0F )
BOTAI_SetAction( &bot.yaw, 1.0F, "MoveToTargetNew() yaw right" );
else if( xAngle < 0.0F )
BOTAI_SetAction( &bot.yaw, -1.0F, "MoveToTargetNew() yaw left" );
if( yAngle < 0.0F && ( WantedVector.z > 0.0F ))
BOTAI_SetAction( &bot.pitch, -1.0F, "MoveToTargetNew() pitch up" );
else if( yAngle > 0.0F && ( WantedVector.z > 0.0F ))
BOTAI_SetAction( &bot.pitch, 1.0F, "MoveToTargetNew() pitch down" );
// Reached Target
if(Ships[WhoIAm].Object.Pos.x < TPos->x + TOL && Ships[WhoIAm].Object.Pos.x > TPos->x - TOL
&& Ships[WhoIAm].Object.Pos.y < TPos->y + TOL && Ships[WhoIAm].Object.Pos.y > TPos->y - TOL
&& Ships[WhoIAm].Object.Pos.z < TPos->z + TOL && Ships[WhoIAm].Object.Pos.z > TPos->z - TOL)
return true;
else
return false;
}
/* (buggy) always triple chords up/right/forward to target */
bool BOTAI_MoveToTarget(VECTOR * TPos)
{
VECTOR NormVector;
VECTOR DirVector;
float xAngle;
float yAngle;
//DebugPrintf("moving to %f %f %f\n", TPos->x, TPos->y, TPos->z);
// set slide movements and check if at target pos
if(BOTAI_SlideToTarget(TPos))
return true;
// adjust the aim
// -- Calculate angles
NormVector.x = TPos->x - Ships[WhoIAm].Object.Pos.x;
NormVector.y = TPos->y - Ships[WhoIAm].Object.Pos.y;
NormVector.z = TPos->z - Ships[WhoIAm].Object.Pos.z;
ApplyMatrix( &Ships[WhoIAm].Object.FinalInvMat, &NormVector, &DirVector );
NormaliseVector( &DirVector );
////DebugPrintf("x = %f y = %f z = %f\n", DirVector.x, DirVector.y, DirVector.z);
// left/right angle
xAngle = (float) acos( DirVector.x );
xAngle = 90.0F - R2D( xAngle );
if( DirVector.z < 0.0F )
xAngle = 180.0F - xAngle;
if( xAngle > 180.0F )
xAngle -= 360.0F;
////DebugPrintf("X Angle %f\n", xAngle);
// up/down angle
yAngle = (float) acos( DirVector.y );
yAngle = 90.0F - R2D( yAngle );
yAngle *= -1.0F;
if( yAngle > 180.0F )
yAngle -= 360.0F;
////DebugPrintf("Y Angle %f\n", yAngle);
// -- angle aim
// sliding right
if( bot.right == 1.0F)
{
// aim left
if(xAngle < 30.0F)
BOTAI_SetAction( &bot.yaw, -1.0F, "MoveToTarget() yaw left" );
// aim right
else if(xAngle > 40.0F)
BOTAI_SetAction( &bot.yaw, 1.0F, "MoveToTarget() yaw right" );
}
// sliding left
else if( bot.right == -1.0F)
{
// aim left
if(xAngle < -40.0F)
BOTAI_SetAction( &bot.yaw, -1.0F, "MoveToTarget() yaw left" );
// aim right
else if(xAngle > -30.0F)
BOTAI_SetAction( &bot.yaw, 1.0F, "MoveToTarget() yaw right" );
}
// sliding up
if(bot.up == 1.0F)
{
// aim up
if(yAngle < -40.0F)
BOTAI_SetAction( &bot.pitch, -1.0F, "MoveToTarget() pitch up" );
// aim down
else if(yAngle > -30.0F)
BOTAI_SetAction( &bot.pitch, 1.0F, "MoveToTarget() pitch down" );
}
// sliding down
else if(bot.up == -1.0F)
{
// aim up
if(yAngle > 30.0F)
BOTAI_SetAction( &bot.pitch, -1.0F, "MoveToTarget() pitch up" );
// aim down
else if(yAngle < 40.0F)
BOTAI_SetAction( &bot.pitch, 1.0F, "MoveToTarget() pitch down" );
}
return false;
}
/* Slides to target pos without aiming at it; returns true if reached target */
bool BOTAI_SlideToTarget(VECTOR * TPos)
{
VECTOR NormVector;
VECTOR DirVector;
float xAngle;
float yAngle;
//DebugPrintf("sliding to target\n");
// -- Calculate angles
NormVector.x = TPos->x - Ships[WhoIAm].Object.Pos.x;
NormVector.y = TPos->y - Ships[WhoIAm].Object.Pos.y;
NormVector.z = TPos->z - Ships[WhoIAm].Object.Pos.z;
ApplyMatrix( &Ships[WhoIAm].Object.FinalInvMat, &NormVector, &DirVector );
NormaliseVector( &DirVector );
////DebugPrintf("x = %f y = %f z = %f\n", DirVector.x, DirVector.y, DirVector.z);
// left/right angle
xAngle = (float) acos( DirVector.x );
xAngle = 90.0F - R2D( xAngle );
if( DirVector.z < 0.0F )
xAngle = 180.0F - xAngle;
if( xAngle > 180.0F )
xAngle -= 360.0F;
////DebugPrintf("X Angle %f\n", xAngle);
// up/down angle
yAngle = (float) acos( DirVector.y );
yAngle = 90.0F - R2D( yAngle );
yAngle *= -1.0F;
if( yAngle > 180.0F )
yAngle -= 360.0F;
////DebugPrintf("Y Angle %f\n", yAngle);
////DebugPrintf("z %f\n", DirVector.z);
// -- execute slide movements
// sliding right
if(xAngle > 0.0F && DistWall[0] > 100.0F)
BOTAI_SetAction( &bot.right, 1.0F, "SlideToTarget() right" );
// sliding left
else if(xAngle < 0.0F && DistWall[1] > 100.0F)
BOTAI_SetAction( &bot.right, -1.0F, "SlideToTarget() left" );
// sliding up
if(DistWall[2] > 100.0F)
{
if(yAngle < 0.0F )
BOTAI_SetAction( &bot.up, 1.0F, "SlideToTarget() up" );
}
// sliding down
if(DistWall[3] > 100.0F)
{
if(yAngle > 0.0F)
BOTAI_SetAction( &bot.up, -1.0F, "SlideToTarget() down" );
}
// forward
if(DirVector.z > 0.0F && DistWall[8] > 100.0F)
BOTAI_SetAction( &bot.forward, 1.0F, "SlideToTarget() forward" );
// backward
else if(DirVector.z < 0.0F && DistWall[9] > 100.0F )
BOTAI_SetAction( &bot.forward, -1.0F, "SlideToTarget() reverse" );
// Reached Target
if(Ships[WhoIAm].Object.Pos.x < TPos->x + TOL && Ships[WhoIAm].Object.Pos.x > TPos->x - TOL
&& Ships[WhoIAm].Object.Pos.y < TPos->y + TOL && Ships[WhoIAm].Object.Pos.y > TPos->y - TOL
&& Ships[WhoIAm].Object.Pos.z < TPos->z + TOL && Ships[WhoIAm].Object.Pos.z > TPos->z - TOL)
return true;
else
return false;
}
bool BOTAI_ShootEnemyMines()
{
int i;
float dist = 0.0F;
float TeamDist = 0.0F;
float nearest = BIGDISTANCE;
float mindist = BIGDISTANCE;
TargetMine = -1;
// search all secondary bullets
for(i = 0; i< MAXSECONDARYWEAPONBULLETS; i++)
{
// for mines that aren't my own
if(SecBulls[i].SecType == SEC_MINE && SecBulls[i].Owner !=WhoIAm && SecBulls[i].Used)
{
// that i can see
if( BOTAI_ClearLOS( &Ships[WhoIAm].Object.Pos, Ships[WhoIAm].Object.Group, &SecBulls[i].Pos ))
{
// find the nearest
dist = DistanceVector2Vector(&Ships[WhoIAm].Object.Pos, &SecBulls[i].Pos);
if(dist < nearest)
{
TargetMine = i;
nearest = dist;
}
}
}
}
// if i have a mine target
if(TargetMine > -1)
{
// don't hit any team members
if( TeamGame )
{
// i will hit team mate with the bullet
if( BOTAI_FriendlyFireCheck() )
return false;
// team mate will endure shockwave damage
for(i = 0; i < MAX_PLAYERS; i++)
{
if(TeamNumber[WhoIAm] != TeamNumber[i])
continue;
TeamDist = DistanceVector2Vector(&Ships[i].Object.Pos, &SecBulls[TargetMine].Pos);
if( TeamDist < dist )
return false;
}
}
// aim at mine
BOTAI_AimAtTarget(&Ships[WhoIAm].Object.FinalInvMat, &Ships[WhoIAm].Object.Pos, &SecBulls[TargetMine].Pos);
// try not to get too close to mines
switch(SecBulls[TargetMine].Weapon)
{
// ideal safest distance
case QUANTUMMINE:
dist = (BALL_RADIUS * QUANTUM_SHOCKWAVE) + SHIP_RADIUS;
break;
default:
dist = (BALL_RADIUS * PURGE_SHOCKWAVE) + SHIP_RADIUS;
break;
}
// however, if i only have short range weapons then i need to be a bit closer to hit it
BOTAI_SelectWeapon( true );
switch( Ships[WhoIAm].Primary )
{
case PYROLITE_RIFLE:
case SUSS_GUN:
if( dist > 1300.0F )
dist = 1300.0F;
mindist = dist;
break;
}
//DebugPrintf("i have a mine target at %f distance\n", nearest);
//DebugPrintf("min dist = %f\n", (BALL_RADIUS * QUANTUM_SHOCKWAVE) + SHIP_RADIUS);
// back up to safest distance or until i hit a wall
if( nearest < dist && DistWall[9] > 100.0F )
BOTAI_SetAction( &bot.forward, -1.0F, "ShootEnemyMines() reverse" );
// too far away from the mine with the current weapon i have so move forward
else if( nearest > mindist && DistWall[8] > 100.0F )
BOTAI_SetAction( &bot.forward, 1.0F, "ShootEnemyMines() forward" );
// at an ok distance
else
{
// release trojax
if(Ships[WhoIAm].Primary == TROJAX && PowerLevel > 10.0F)
BOTAI_SetAction( &bot.fire_primary, 0.0F, "ShootEnemyMines() fire primary off" );
// shoot primary
else
BOTAI_SetAction( &bot.fire_primary, 1.0F, "ShootEnemyMines() fire primary" );
}
return true;
}
else
return false;
}
bool BOTAI_CollectNearestPickup(bool SlideOnly)
{
// Pickup identified
if(GettingPickup > -1)
{
// Make sure we search for nodes after
CurrentNode = -1;
// if we lost LOS
if(!BOTAI_ClearLOS( &Ships[WhoIAm].Object.Pos, Ships[WhoIAm].Object.Group, &Pickups[GettingPickup].Pos ) || Pickups[GettingPickup].Type == (u_int16_t) -1)
{
DebugPrintf("lost pickup los %s\n", WepMessages[ Pickups[GettingPickup].Type ]);
GettingPickup = -1;
return false;
}
// otherwise, move to the target and collect
if(!SlideOnly)
{
DebugPrintf("moving to pickup %s\n", WepMessages[ Pickups[GettingPickup].Type ]);
if( BOTAI_MoveToTarget(&Pickups[GettingPickup].Pos) )
GettingPickup = -1;
}
// try and slide near it
else
{
DebugPrintf("sliding to pickup %s\n", WepMessages[ Pickups[GettingPickup].Type ]);
if( BOTAI_SlideToTarget(&Pickups[GettingPickup].Pos) )
GettingPickup = -1;
}
return true;
}
else
return false;
}
bool BOTAI_AimAtTarget( MATRIX * InvMat , VECTOR * SPos, VECTOR * TPos )
{
VECTOR WantedDir;
VECTOR TempDir;
float Angle;
float OnTarget = true;
WantedDir.x = ( TPos->x - SPos->x );
WantedDir.y = ( TPos->y - SPos->y );
WantedDir.z = ( TPos->z - SPos->z );
ApplyMatrix( InvMat, &WantedDir, &TempDir );
NormaliseVector( &TempDir );
Angle = (float) acos( TempDir.x );
Angle = 90.0F - R2D( Angle );
if( TempDir.z < 0.0F )
Angle = 180.0F - Angle;
if( Angle > 180.0F )
Angle -= 360.0F;
if( Angle > 0.0F )
{
BOTAI_SetAction( &bot.yaw, 1.0F, "AimAtTarget() yaw right" );
if( Angle > 3.0F )
OnTarget = false;
}
else if( Angle < 0.0F )
{
BOTAI_SetAction( &bot.yaw, -1.0F, "AimAtTarget() yaw left" );
if( Angle < -3.0F )
OnTarget = false;
}
Angle = (float) acos( TempDir.y );
Angle = 90.0F - R2D( Angle );
Angle *= -1.0F;
if( Angle > 180.0F )
Angle -= 360.0F;
if( Angle < 0.0F && ( TempDir.z > 0.0F ))
{
BOTAI_SetAction( &bot.pitch, -1.0F, "AimAtTarget() pitch up" );
if(Angle < -3.0F)
OnTarget = false;
}
else if( Angle > 0.0F && ( TempDir.z > 0.0F ))
{
BOTAI_SetAction( &bot.pitch, 1.0F, "AimAtTarget() pitch down" );
if(Angle > 3.0F)
OnTarget = false;
}
return OnTarget;
}
void BOTAI_AvoidHomingMissiles()
{
VECTOR NormVector;
VECTOR DirVector;
float xAngle;
float yAngle;
if(HomingMissile > -1)
{
// -- decide what movement to perform
// new missile
//if(!MissileAvoidanceSet)
{
//DebugPrintf("set details for missile %d\n", HomingMissile);
MissileAvoidanceSet = true;
NormVector.x = SecBulls[HomingMissile].Pos.x - Ships[WhoIAm].Object.Pos.x;
NormVector.y = SecBulls[HomingMissile].Pos.y - Ships[WhoIAm].Object.Pos.y;
NormVector.z = SecBulls[HomingMissile].Pos.z - Ships[WhoIAm].Object.Pos.z;
ApplyMatrix( &Ships[WhoIAm].Object.FinalInvMat, &NormVector, &DirVector );
NormaliseVector( &DirVector );
MissileOrigDirVector = DirVector;
}
// left/right angle
xAngle = (float) acos( MissileOrigDirVector.x );
xAngle = 90.0F - R2D( xAngle );
if( MissileOrigDirVector.z < 0.0F )
xAngle = 180.0F - xAngle;
if( xAngle > 180.0F )
xAngle -= 360.0F;
//DebugPrintf("x = %f\n", xAngle);
// up/down angle
yAngle = (float) acos( MissileOrigDirVector.y );
yAngle = 90.0F - R2D( yAngle );
yAngle *= -1.0F;
if( yAngle > 180.0F )
yAngle -= 360.0F;
//DebugPrintf("y = %f\n", yAngle);
// -- execute slide movements
// target is directly in front or behind me
if((xAngle > -22.5F && xAngle < 22.5F) || (xAngle < -157.5F || xAngle > 157.5F))
{
// slide either left or right, doesn't matter
BOTAI_SetAction( &bot.right, 1.0F, "AvoidHomingMissiles() right" );
}
// target is directly to the side of me
else if((xAngle > 67.5F && xAngle < 112.5F) || (xAngle < -67.5F && xAngle > -112.5F))
{
// forward or backward, doesn't matter
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidHomingMissiles() forward" );
}
// target is to my front left
else if(xAngle > -67.5F && xAngle < -22.5F)
{
// move forward right
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidHomingMissiles() forward" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidHomingMissiles() right" );
}
// target is to my rear left
else if(xAngle > -157.5F && xAngle < -112.5F)
{
// move back left
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidHomingMissiles() reverse" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidHomingMissiles() left" );
}
// target is to my front right
else if(xAngle > 22.5F && xAngle < 67.5F)
{
// move front left
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidHomingMissiles() forward" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidHomingMissiles() left" );
}
// target is to my rear right
else if(xAngle > 112.5F && xAngle < 157.5F)
{
// move back right
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidHomingMissiles() reverse" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidHomingMissiles() right" );
}
if(yAngle > 0.0F)
BOTAI_SetAction( &bot.up, -1.0F, "AvoidHomingMissiles() down" );
else
BOTAI_SetAction( &bot.up, 1.0F, "AvoidHomingMissiles() up" );
}
}
void BOTAI_AvoidBullets()
{
int i = 0;
float dist = 0.0F;
float longest = 0.0F;
// bullet will hit us if we stay in current position
if(BOTAI_WhenWillBulletHitMe(&Ships[WhoIAm].Object.Pos) > -1.0F)
{
if(AvoidMove < 0)
{
// find perfect spot
for(i = 0; i < NUM_SENSORS; i++)
{
if(WhenWillHitSlide[i] > -1.0F)
DebugPrintf("will hit %d\n", i);
// nothing will hit and there's space to move
if(WhenWillHitSlide[i] < 0.0F && DistWall[i] > MIN_WALLDIST)
{
// move to the one with most room
if(DistWall[i] > dist)
{
//DebugPrintf("using %d instead of %d because %f > %f\n", i, AvoidMove, DistWall[i], dist);
AvoidMove = i;// BOTAI_CenterCheck(i);
DebugPrintf("using %d\n", AvoidMove);
dist = DistWall[i];
}
}
}
// couldn't find perfect so try safest
if(AvoidMove < 0)
{
for(i = 0; i < NUM_SENSORS; i++)
{
// something will hit but i have space and some time
if(WhenWillHitSlide[i] > longest && DistWall[i] > MIN_WALLDIST)
{
AvoidMove = i;
longest = WhenWillHitSlide[i];
}
}
//if(DEBUG_AVOIDANCE)
DebugPrintf("couldn't find perfect spot: using %d\n", AvoidMove);
}
//else if(DEBUG_AVOIDANCE)
DebugPrintf("found perfect spot: %d\n", AvoidMove);
}
// carry out the movement
switch(AvoidMove)
{
case -1:
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
break;
case 0:
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
break;
case 1:
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
break;
case 2:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
break;
case 3:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
break;
case 4:
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
break;
case 5:
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
break;
case 6:
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
break;
case 7:
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
break;
case 8:
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 9:
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 10:
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 11:
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
break;
case 12:
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
break;
case 13:
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
break;
case 14:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 15:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 16:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 17:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.forward, -1.0F, "AvoidBullets() reverse" );
break;
case 18:
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 19:
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 20:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 21:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 22:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 23:
BOTAI_SetAction( &bot.up, 1.0F, "AvoidBullets() up" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 24:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
BOTAI_SetAction( &bot.right, 1.0F, "AvoidBullets() right" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
case 25:
BOTAI_SetAction( &bot.up, -1.0F, "AvoidBullets() down" );
BOTAI_SetAction( &bot.right, -1.0F, "AvoidBullets() left" );
BOTAI_SetAction( &bot.forward, 1.0F, "AvoidBullets() forward" );
break;
default: DebugPrintf("invalid move: %d\n", AvoidMove); // should never get here
break;
}
}
else
AvoidMove = -1;
if(AvoidMove < 0 && DEBUG_AVOIDANCE)
DebugPrintf("safe......................\n");
}
void BOTAI_SelectWeapon( bool MineTarget )
{
if( MineTarget )
{
if( PrimaryWeaponsGot[ LASER ] && !LaserOverheated)
Ships[WhoIAm].Primary = LASER;
else if( GeneralAmmo > 0.0F )
Ships[WhoIAm].Primary = PULSAR;
else if( PrimaryWeaponsGot[ PYROLITE_RIFLE ] && PyroliteAmmo > 0.0F )
Ships[WhoIAm].Primary = PYROLITE_RIFLE;
else if( PrimaryWeaponsGot[ SUSS_GUN ] && SussGunAmmo > 0.0F )
Ships[WhoIAm].Primary = SUSS_GUN;
else
Ships[WhoIAm].Primary = PULSAR;
}
else if(TargetShipID > -1)
{
// target is weak and i have lasers, finish him!
if(TargetShipHealth < 64 && PrimaryWeaponsGot[ LASER ] && !LaserOverheated)
Ships[WhoIAm].Primary = LASER;
// use pyro if they're close
else if(PrimaryWeaponsGot[ PYROLITE_RIFLE ] && TargetShipDistance < 1000.0F)
Ships[WhoIAm].Primary = PYROLITE_RIFLE;
// use lasers if they're far away or if they're closer and i don't have trojax
else if( (PrimaryWeaponsGot[ LASER ] && !LaserOverheated && TargetShipDistance > 1500.0F)
|| (PrimaryWeaponsGot[ LASER ] && !LaserOverheated && !PrimaryWeaponsGot[ TROJAX ]) )
Ships[WhoIAm].Primary = LASER;
// use trojax
else if(PrimaryWeaponsGot[ TROJAX ])
Ships[WhoIAm].Primary = TROJAX;
// last choice pulsars
else
Ships[WhoIAm].Primary = PULSAR;
}
}
void BOTAI_AimAtTargetShip()
{