-
Notifications
You must be signed in to change notification settings - Fork 155
/
aicode.cpp
15627 lines (12993 loc) · 495 KB
/
aicode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
/**
* @file
* Contains the actual AI code that does interesting stuff to objects.
*
* The code in Ai.cpp is just for bookkeeping, allocating AI slots and linking them to ships.
*/
#include "ai/ai.h"
#include "ai/ai_profiles.h"
#include "ai/aibig.h"
#include "ai/aigoals.h"
#include "ai/aiinternal.h"
#include "asteroid/asteroid.h"
#include "autopilot/autopilot.h"
#include "cmeasure/cmeasure.h"
#include "debugconsole/console.h"
#include "freespace2/freespace.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "hud/hud.h"
#include "hud/hudets.h"
#include "hud/hudlock.h"
#include "iff_defs/iff_defs.h"
#include "io/joy_ff.h"
#include "io/timer.h"
#include "localization/localize.h"
#include "math/fvi.h"
#include "math/staticrand.h"
#include "mission/missiongoals.h"
#include "mission/missionlog.h"
#include "mission/missionmessage.h"
#include "mission/missionparse.h"
#include "mission/missiontraining.h"
#include "model/model.h"
#include "network/multi.h"
#include "network/multimsgs.h"
#include "network/multiutil.h"
#include "object/deadobjectdock.h"
#include "object/objcollide.h"
#include "object/object.h"
#include "object/objectdock.h"
#include "object/objectshield.h"
#include "object/waypoint.h"
#include "parse/parselo.h"
#include "physics/physics.h"
#include "playerman/player.h"
#include "render/3d.h"
#include "ship/afterburner.h"
#include "ship/awacs.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "ship/shiphit.h"
#include "weapon/beam.h"
#include "weapon/flak.h"
#include "weapon/swarm.h"
#include "weapon/weapon.h"
#include <map>
#include <limits.h>
#ifdef _MSC_VER
#pragma optimize("", off)
#pragma auto_inline(off)
#endif
#define UNINITIALIZED_VALUE -99999.9f
#define INSTRUCTOR_SHIP_NAME NOX("instructor")
#define AICODE_SMALL_MAGNITUDE 0.001f // cosider a vector NULL if mag is less than this
#define NEXT_REARM_TIMESTAMP (60*1000) // Ships will re-request rearm, typically, after this long.
#define CIRCLE_STRAFE_MAX_DIST 300.0f //Maximum distance for circle strafe behavior.
// AIM_CHASE submode defines
// SM_STEALTH_FIND
#define SM_SF_AHEAD 0
#define SM_SF_BEHIND 1
#define SM_SF_BAIL 2
// SM_STEALTH_SWEEP
#define SM_SS_SET_GOAL -1
#define SM_SS_BOX0 0
#define SM_SS_LR 1
#define SM_SS_UL 2
#define SM_SS_BOX1 3
#define SM_SS_UR 4
#define SM_SS_LL 5
#define SM_SS_BOX2 6
#define SM_SS_DONE 7
//XSTR:OFF
char *Mode_text[MAX_AI_BEHAVIORS] = {
"CHASE",
"EVADE",
"GET_BEHIND",
"CHASE_LONG",
"SQUIGGLE",
"GUARD",
"AVOID",
"WAYPOINTS",
"DOCK",
"NONE",
"BIGSHIP",
"PATH",
"BE_REARMED",
"SAFETY",
"EV_WEAPON",
"STRAFE",
"PLAY_DEAD",
"BAY_EMERGE",
"BAY_DEPART",
"SENTRYGUN",
"WARP_OUT",
};
// Submode text is only valid for CHASE mode.
char *Submode_text[] = {
"undefined",
"CONT_TURN",
"ATTACK ",
"E_SQUIG ",
"E_BRAKE ",
"EVADE ",
"SUP_ATTAK",
"AVOID ",
"BEHIND ",
"GET_AWAY ",
"E_WEAPON ",
"FLY_AWAY ",
"ATK_4EVER",
"STLTH_FND",
"STLTH_SWP",
"BIG_APPR",
"BIG_CIRC",
"BIG_PARL"
};
char *Strafe_submode_text[5] = {
"ATTACK",
"AVOID",
"RETREAT1",
"RETREAT2",
"POSITION"
};
//XSTR:ON
// few forward decs i needed - kazan
object * get_wing_leader(int wingnum);
int get_wing_index(object *objp, int wingnum);
control_info AI_ci;
object *Pl_objp;
object *En_objp;
#define REARM_SOUND_DELAY (3*F1_0) // Amount of time to delay rearm/repair after mode start
#define REARM_BREAKOFF_DELAY (3*F1_0) // Amount of time to wait after fully rearmed to breakoff.
#define MIN_DIST_TO_WAYPOINT_GOAL 5.0f
#define MAX_GUARD_DIST 250.0f
#define BIG_GUARD_RADIUS 500.0f
#define MAX_EVADE_TIME (15 * 1000) // Max time to evade a weapon.
// defines for repair ship stuff.
#define MAX_REPAIR_SPEED 25.0f
#define MAX_UNDOCK_ABORT_SPEED 2.0f
// defines for EMP effect stuff
#define MAX_EMP_INACCURACY 50.0f
// defines for stealth
#define MAX_STEALTH_INACCURACY 50.0f // at max view dist
#define STEALTH_MAX_VIEW_DIST 400 // dist at which 1) stealth no longer visible 2) firing inaccuracy is greatest
#define STEALTH_VIEW_CONE_DOT 0.707 // (half angle of 45 degrees)
ai_class *Ai_classes = NULL;
int Ai_firing_enabled = 1;
int Num_ai_classes;
int Num_alloced_ai_classes;
int AI_FrameCount = 0;
int AI_watch_object = 0; // Debugging, object to spew debug info for.
int Mission_all_attack = 0; // !0 means all teams attack all teams.
// Constant for flag, Name of flag, In flags or flags2
ai_flag_name Ai_flag_names[] = {
{AIF_NO_DYNAMIC, "no-dynamic", 1, },
{AIF_FREE_AFTERBURNER_USE, "free-afterburner-use", 1, },
};
const char *Skill_level_names(int level, int translate)
{
const char *str = NULL;
#if NUM_SKILL_LEVELS != 5
#error Number of skill levels is wrong!
#endif
if(translate){
switch( level ) {
case 0:
str = XSTR("Very Easy", 469);
break;
case 1:
str = XSTR("Easy", 470);
break;
case 2:
str = XSTR("Medium", 471);
break;
case 3:
str = XSTR("Hard", 472);
break;
case 4:
str = XSTR("Insane", 473);
break;
default:
Int3();
}
} else {
switch( level ) {
case 0:
str = NOX("Very Easy");
break;
case 1:
str = NOX("Easy");
break;
case 2:
str = NOX("Medium");
break;
case 3:
str = NOX("Hard");
break;
case 4:
str = NOX("Insane");
break;
default:
Int3();
}
}
return str;
}
#define DELAY_TARGET_TIME (12*1000) // time in milliseconds until a ship can target a new enemy after an order.
pnode Path_points[MAX_PATH_POINTS];
pnode *Ppfp; // Free pointer in path points.
float AI_frametime;
char** Ai_class_names = NULL;
typedef struct {
int team;
int weapon_index;
int max_fire_count;
char *shipname;
} huge_fire_info;
SCP_vector<huge_fire_info> Ai_huge_fire_info;
int Ai_last_arrive_path; // index of ship_bay path used by last arrival from a fighter bay
// forward declarations
int ai_return_path_num_from_dockbay(object *dockee_objp, int dockbay_index);
void create_model_exit_path(object *pl_objp, object *mobjp, int path_num, int count=1);
void copy_xlate_model_path_points(object *objp, model_path *mp, int dir, int count, int path_num, pnode *pnp, int randomize_pnt=-1);
void ai_cleanup_rearm_mode(object *objp);
void ai_cleanup_dock_mode_subjective(object *objp);
void ai_cleanup_dock_mode_objective(object *objp);
// The object that is declared to be the leader of the group formation for
// the "autopilot"
object *Autopilot_flight_leader = NULL;
/**
* Sets the timestamp used to tell is it is a good time for this team to rearm.
* Once the timestamp is no longer valid, then rearming is not a "good time"
*
* @param team Team (friendly, hostile, neutral)
* @param time Time to rearm
*/
void ai_set_rearm_status(int team, int time)
{
Assert( time >= 0 );
Iff_info[team].ai_rearm_timestamp = timestamp(time * 1000);
}
/**
* "safe" is currently defined by the mission designer using the good/bad time to rearm sexpressions. This status is currently team based.
*
* @return true(1) or false(0) if it is "safe" for the given object to rearm.
* @todo This function could be easily expended to further the definition of "safe"
*/
int ai_good_time_to_rearm(object *objp)
{
int team;
Assert(objp->type == OBJ_SHIP);
team = Ships[objp->instance].team;
return timestamp_valid(Iff_info[team].ai_rearm_timestamp);
}
/**
* Entry point from sexpression code to set internal data for use by AI code.
*/
void ai_good_secondary_time( int team, int weapon_index, int max_fire_count, char *shipname )
{
Assert(shipname != NULL);
int index;
huge_fire_info new_info;
new_info.weapon_index = weapon_index;
new_info.team = team;
new_info.max_fire_count = max_fire_count;
new_info.shipname = ai_get_goal_target_name( shipname, &index );
Ai_huge_fire_info.push_back(new_info);
}
/**
* Called internally to the AI code to tell whether or not weapon_num can be fired
* from firer_objp at target_objp. This function will resolve the team for the firer.
*
* @return -1 when conditions don't allow firer to fire weapon_num on target_objp
* @return >=0 when conditions allow firer to fire. Return value is max number of weapon_nums which can be fired on target_objp
*/
int is_preferred_weapon(int weapon_num, object *firer_objp, object *target_objp)
{
int firer_team, target_signature;
ship *firer_ship;
SCP_vector<huge_fire_info>::iterator hfi;
Assert( firer_objp->type == OBJ_SHIP );
firer_ship = &Ships[firer_objp->instance];
firer_team = firer_ship->team;
// get target object's signature and try to find it in the list.
target_signature = target_objp->signature;
for (hfi = Ai_huge_fire_info.begin();hfi != Ai_huge_fire_info.end(); ++hfi ) {
int ship_index, signature;
if ( hfi->weapon_index == -1 )
continue;
ship_index = ship_name_lookup( hfi->shipname );
if ( ship_index == -1 )
continue;
signature = Objects[Ships[ship_index].objnum].signature;
// sigatures, weapon_index, and team must match
if ( (signature == target_signature) && (hfi->weapon_index == weapon_num) && (hfi->team == firer_team) )
break;
}
// return -1 if not found
if ( hfi == Ai_huge_fire_info.end() )
return -1;
// otherwise, we can return the max number of weapons we can fire against target_objps
return hfi->max_fire_count;
}
/**
* Clear out secondary firing information between levels
*/
void ai_init_secondary_info()
{
Ai_huge_fire_info.clear();
}
/**
* Garbage collect the ::Path_points buffer.
*
* Scans all objects, looking for used Path_points records.
* Compresses Path_points buffer, updating aip->path_start and aip->path_cur indices.
* Updates Ppfp to point to first free record.
* This function is fairly fast. Its worst-case running time is proportional to
* 3*MAX_PATH_POINTS + MAX_OBJECTS
*
* @todo Things to do to optimize this function:
* 1. if (t != 0) xlt++; can be replaced by xlt += t; assuming t can only be 0 or 1.
* 2. When pp_xlate is getting stuffed the first time, note highest index and use that
* instead of MAX_PATH_POINTS in following two for loops.
*/
void garbage_collect_path_points()
{
int i;
int pp_xlate[MAX_PATH_POINTS];
object *A;
ship_obj *so;
// Scan all objects and create Path_points xlate table.
for (i=0; i<MAX_PATH_POINTS; i++)
pp_xlate[i] = 0;
// in pp_xlate, mark all used Path_point records
for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
A = &Objects[so->objnum];
ship *shipp = &Ships[A->instance];
if (shipp->ai_index != -1) {
ai_info *aip = &Ai_info[shipp->ai_index];
if ((aip->path_length > 0) && (aip->path_start > -1)) {
for (i=aip->path_start; i<aip->path_start + aip->path_length; i++) {
Assert(pp_xlate[i] == 0); // If this is not 0, then two paths use this point!
pp_xlate[i] = 1;
}
}
}
}
// Now, stuff xlate index in pp_xlate. This is the number to translate any path_start
// or path_cur index to.
int xlt = 0;
for (i=0; i<MAX_PATH_POINTS; i++) {
int t = pp_xlate[i];
pp_xlate[i] = xlt;
if (t != 0)
xlt++;
}
// Update global Path_points free pointer.
Ppfp = &Path_points[xlt];
// Now, using pp_xlate, fixup all aip->path_cur and aip->path_start indices
for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
A = &Objects[so->objnum];
ship *shipp = &Ships[A->instance];
if (shipp->ai_index != -1) {
ai_info *aip = &Ai_info[shipp->ai_index];
if ((aip->path_length > 0) && (aip->path_start > -1)) {
Assert(aip->path_start < MAX_PATH_POINTS);
aip->path_start = pp_xlate[aip->path_start];
Assert((aip->path_cur >= 0) && (aip->path_cur < MAX_PATH_POINTS));
aip->path_cur = pp_xlate[aip->path_cur];
}
}
}
// Now, compress the buffer.
for (i=0; i<MAX_PATH_POINTS; i++)
if (i != pp_xlate[i])
Path_points[pp_xlate[i]] = Path_points[i];
}
/**
* Hash two values together, return result.
* Hash function: curval shifted right circular by one, newval xored in.
*/
int hash(unsigned int curval, int newval)
{
int addval = curval & 1;
curval >>= 1;
if (addval)
curval |= 0x80000000;
curval ^= newval;
return curval;
}
/**
* Hash some information in an object together.
* On 2/20/97, the information is position and orientation.
*/
int create_object_hash(object *objp)
{
int *ip;
unsigned int hashval = 0;
int i;
ip = (int *) &objp->orient;
for (i=0; i<9; i++) {
hashval = hash(hashval, *ip);
ip++;
}
ip = (int *) &objp->pos;
for (i=0; i<3; i++) {
hashval = hash(hashval, *ip);
ip++;
}
return hashval;
}
void free_ai_stuff()
{
if(Ai_classes != NULL)
vm_free(Ai_classes);
if(Ai_class_names != NULL)
vm_free(Ai_class_names);
}
/**
* Initialize an AI class's nonrequired values to defaults
* Boolean overrides are unset, others initialized to FLT_MIN or INT_MIN (used as the "not set" state)
*/
void init_ai_class(ai_class *aicp)
{
int i;
for (i = 0; i < NUM_SKILL_LEVELS; i++)
{
aicp->ai_cmeasure_fire_chance[i] = FLT_MIN;
aicp->ai_in_range_time[i] = FLT_MIN;
aicp->ai_link_ammo_levels_maybe[i] = FLT_MIN;
aicp->ai_link_ammo_levels_always[i] = FLT_MIN;
aicp->ai_primary_ammo_burst_mult[i] = FLT_MIN;
aicp->ai_link_energy_levels_maybe[i] = FLT_MIN;
aicp->ai_link_energy_levels_always[i] = FLT_MIN;
aicp->ai_predict_position_delay[i] = INT_MIN;
aicp->ai_shield_manage_delay[i] = FLT_MIN;
aicp->ai_ship_fire_delay_scale_friendly[i] = FLT_MIN;
aicp->ai_ship_fire_delay_scale_hostile[i] = FLT_MIN;
aicp->ai_ship_fire_secondary_delay_scale_friendly[i] = FLT_MIN;
aicp->ai_ship_fire_secondary_delay_scale_hostile[i] = FLT_MIN;
aicp->ai_turn_time_scale[i] = FLT_MIN;
aicp->ai_glide_attack_percent[i] = FLT_MIN;
aicp->ai_circle_strafe_percent[i] = FLT_MIN;
aicp->ai_glide_strafe_percent[i] = FLT_MIN;
aicp->ai_random_sidethrust_percent[i] = FLT_MIN;
aicp->ai_stalemate_time_thresh[i] = FLT_MIN;
aicp->ai_stalemate_dist_thresh[i] = FLT_MIN;
aicp->ai_chance_to_use_missiles_on_plr[i] = INT_MIN;
aicp->ai_max_aim_update_delay[i] = FLT_MIN;
aicp->ai_turret_max_aim_update_delay[i] = FLT_MIN;
}
aicp->ai_profile_flags = 0;
aicp->ai_profile_flags_set = 0;
aicp->ai_profile_flags2 = 0;
aicp->ai_profile_flags2_set = 0;
//AI Class autoscale overrides
//INT_MIN and FLT_MIN represent the "not set" state
for (i = 0; i < NUM_SKILL_LEVELS; i++)
{
aicp->ai_aburn_use_factor[i] = INT_MIN;
aicp->ai_shockwave_evade_chance[i] = FLT_MIN;
aicp->ai_get_away_chance[i] = FLT_MIN;
aicp->ai_secondary_range_mult[i] = FLT_MIN;
}
aicp->ai_class_autoscale = true; //Retail behavior is to do the stupid autoscaling
}
void set_aic_flag(ai_class *aicp, char *name, int flag, int type)
{
int* flags = (type == AIP_FLAG) ? &(aicp->ai_profile_flags) : &(aicp->ai_profile_flags2);
int* set = (type == AIP_FLAG) ? &(aicp->ai_profile_flags_set) : &(aicp->ai_profile_flags2_set);
if (optional_string(name))
{
bool val;
stuff_boolean(&val);
if (val)
*flags |= flag;
else
*flags &= ~flag;
*set |= flag;
}
else
*set &= ~flag;
}
void parse_ai_class()
{
ai_class *aicp = &Ai_classes[Num_ai_classes];
init_ai_class(aicp);
required_string("$Name:");
stuff_string(aicp->name, F_NAME, NAME_LENGTH);
Ai_class_names[Num_ai_classes] = aicp->name;
required_string("$accuracy:");
parse_float_list(aicp->ai_accuracy, NUM_SKILL_LEVELS);
required_string("$evasion:");
parse_float_list(aicp->ai_evasion, NUM_SKILL_LEVELS);
required_string("$courage:");
parse_float_list(aicp->ai_courage, NUM_SKILL_LEVELS);
required_string("$patience:");
parse_float_list(aicp->ai_patience, NUM_SKILL_LEVELS);
if (optional_string("$Afterburner Use Factor:"))
parse_int_list(aicp->ai_aburn_use_factor, NUM_SKILL_LEVELS);
if (optional_string("$Shockwave Evade Chances Per Second:"))
parse_float_list(aicp->ai_shockwave_evade_chance, NUM_SKILL_LEVELS);
if (optional_string("$Get Away Chance:"))
parse_float_list(aicp->ai_get_away_chance, NUM_SKILL_LEVELS);
if (optional_string("$Secondary Range Multiplier:"))
parse_float_list(aicp->ai_secondary_range_mult, NUM_SKILL_LEVELS);
if (optional_string("$Autoscale by AI Class Index:"))
stuff_boolean(&aicp->ai_class_autoscale);
//Parse optional values for stuff imported from ai_profiles
if (optional_string("$AI Countermeasure Firing Chance:"))
parse_float_list(aicp->ai_cmeasure_fire_chance, NUM_SKILL_LEVELS);
if (optional_string("$AI In Range Time:"))
parse_float_list(aicp->ai_in_range_time, NUM_SKILL_LEVELS);
if (optional_string("$AI Always Links Ammo Weapons:"))
parse_float_list(aicp->ai_link_ammo_levels_always, NUM_SKILL_LEVELS);
if (optional_string("$AI Maybe Links Ammo Weapons:"))
parse_float_list(aicp->ai_link_ammo_levels_maybe, NUM_SKILL_LEVELS);
if (optional_string("$Primary Ammo Burst Multiplier:"))
parse_float_list(aicp->ai_primary_ammo_burst_mult, NUM_SKILL_LEVELS);
if (optional_string("$AI Always Links Energy Weapons:"))
parse_float_list(aicp->ai_link_energy_levels_always, NUM_SKILL_LEVELS);
if (optional_string("$AI Maybe Links Energy Weapons:"))
parse_float_list(aicp->ai_link_energy_levels_maybe, NUM_SKILL_LEVELS);
// represented in fractions of F1_0
if (optional_string("$Predict Position Delay:")) {
int iLoop;
float temp_list[NUM_SKILL_LEVELS];
parse_float_list(temp_list, NUM_SKILL_LEVELS);
for (iLoop = 0; iLoop < NUM_SKILL_LEVELS; iLoop++)
aicp->ai_predict_position_delay[iLoop] = fl2f(temp_list[iLoop]);
}
if (optional_string("$AI Shield Manage Delay:") || optional_string("$AI Shield Manage Delays:"))
parse_float_list(aicp->ai_shield_manage_delay, NUM_SKILL_LEVELS);
if (optional_string("$Friendly AI Fire Delay Scale:"))
parse_float_list(aicp->ai_ship_fire_delay_scale_friendly, NUM_SKILL_LEVELS);
if (optional_string("$Hostile AI Fire Delay Scale:"))
parse_float_list(aicp->ai_ship_fire_delay_scale_hostile, NUM_SKILL_LEVELS);
if (optional_string("$Friendly AI Secondary Fire Delay Scale:"))
parse_float_list(aicp->ai_ship_fire_secondary_delay_scale_friendly, NUM_SKILL_LEVELS);
if (optional_string("$Hostile AI Secondary Fire Delay Scale:"))
parse_float_list(aicp->ai_ship_fire_secondary_delay_scale_hostile, NUM_SKILL_LEVELS);
if (optional_string("$AI Turn Time Scale:"))
parse_float_list(aicp->ai_turn_time_scale, NUM_SKILL_LEVELS);
if (optional_string("$Glide Attack Percent:")) {
parse_float_list(aicp->ai_glide_attack_percent, NUM_SKILL_LEVELS);
for (int i = 0; i < NUM_SKILL_LEVELS; i++) {
if (aicp->ai_glide_attack_percent[i] < 0.0f || aicp->ai_glide_attack_percent[i] > 100.0f) {
aicp->ai_glide_attack_percent[i] = 0.0f;
Warning(LOCATION, "$Glide Attack Percent should be between 0 and 100.0 (read %f). Setting to 0.", aicp->ai_glide_attack_percent[i]);
}
aicp->ai_glide_attack_percent[i] /= 100.0;
}
}
if (optional_string("$Circle Strafe Percent:")) {
parse_float_list(aicp->ai_circle_strafe_percent, NUM_SKILL_LEVELS);
for (int i = 0; i < NUM_SKILL_LEVELS; i++) {
if (aicp->ai_circle_strafe_percent[i] < 0.0f || aicp->ai_circle_strafe_percent[i] > 100.0f) {
aicp->ai_circle_strafe_percent[i] = 0.0f;
Warning(LOCATION, "$Circle Strafe Percent should be between 0 and 100.0 (read %f). Setting to 0.", aicp->ai_circle_strafe_percent[i]);
}
aicp->ai_circle_strafe_percent[i] /= 100.0;
}
}
if (optional_string("$Glide Strafe Percent:")) {
parse_float_list(aicp->ai_glide_strafe_percent, NUM_SKILL_LEVELS);
for (int i = 0; i < NUM_SKILL_LEVELS; i++) {
if (aicp->ai_glide_strafe_percent[i] < 0.0f || aicp->ai_glide_strafe_percent[i] > 100.0f) {
aicp->ai_glide_strafe_percent[i] = 0.0f;
Warning(LOCATION, "$Glide Strafe Percent should be between 0 and 100.0 (read %f). Setting to 0.", aicp->ai_glide_strafe_percent[i]);
}
aicp->ai_glide_strafe_percent[i] /= 100.0;
}
}
if (optional_string("$Random Sidethrust Percent:")) {
parse_float_list(aicp->ai_random_sidethrust_percent, NUM_SKILL_LEVELS);
for (int i = 0; i < NUM_SKILL_LEVELS; i++) {
if (aicp->ai_random_sidethrust_percent[i] < 0.0f || aicp->ai_random_sidethrust_percent[i] > 100.0f) {
aicp->ai_random_sidethrust_percent[i] = 0.0f;
Warning(LOCATION, "$Random Sidethrust Percent should be between 0 and 100.0 (read %f). Setting to 0.", aicp->ai_random_sidethrust_percent[i]);
}
aicp->ai_random_sidethrust_percent[i] /= 100.0;
}
}
if (optional_string("$Stalemate Time Threshold:"))
parse_float_list(aicp->ai_stalemate_time_thresh, NUM_SKILL_LEVELS);
if (optional_string("$Stalemate Distance Threshold:"))
parse_float_list(aicp->ai_stalemate_dist_thresh, NUM_SKILL_LEVELS);
if (optional_string("$Chance AI Has to Fire Missiles at Player:"))
parse_int_list(aicp->ai_chance_to_use_missiles_on_plr, NUM_SKILL_LEVELS);
if (optional_string("$Max Aim Update Delay:"))
parse_float_list(aicp->ai_max_aim_update_delay, NUM_SKILL_LEVELS);
if (optional_string("$Turret Max Aim Update Delay:"))
parse_float_list(aicp->ai_turret_max_aim_update_delay, NUM_SKILL_LEVELS);
set_aic_flag(aicp, "$big ships can attack beam turrets on untargeted ships:", AIPF_BIG_SHIPS_CAN_ATTACK_BEAM_TURRETS_ON_UNTARGETED_SHIPS, AIP_FLAG);
set_aic_flag(aicp, "$smart primary weapon selection:", AIPF_SMART_PRIMARY_WEAPON_SELECTION, AIP_FLAG);
set_aic_flag(aicp, "$smart secondary weapon selection:", AIPF_SMART_SECONDARY_WEAPON_SELECTION, AIP_FLAG);
set_aic_flag(aicp, "$smart shield management:", AIPF_SMART_SHIELD_MANAGEMENT, AIP_FLAG);
set_aic_flag(aicp, "$smart afterburner management:", AIPF_SMART_AFTERBURNER_MANAGEMENT, AIP_FLAG);
set_aic_flag(aicp, "$allow rapid secondary dumbfire:", AIPF_ALLOW_RAPID_SECONDARY_DUMBFIRE, AIP_FLAG);
set_aic_flag(aicp, "$huge turret weapons ignore bombs:", AIPF_HUGE_TURRET_WEAPONS_IGNORE_BOMBS, AIP_FLAG);
set_aic_flag(aicp, "$don't insert random turret fire delay:", AIPF_DONT_INSERT_RANDOM_TURRET_FIRE_DELAY, AIP_FLAG);
set_aic_flag(aicp, "$prevent turrets targeting too distant bombs:", AIPF_PREVENT_TARGETING_BOMBS_BEYOND_RANGE, AIP_FLAG);
set_aic_flag(aicp, "$smart subsystem targeting for turrets:", AIPF_SMART_SUBSYSTEM_TARGETING_FOR_TURRETS, AIP_FLAG);
set_aic_flag(aicp, "$allow turrets target weapons freely:", AIPF_ALLOW_TURRETS_TARGET_WEAPONS_FREELY, AIP_FLAG);
set_aic_flag(aicp, "$allow vertical dodge:", AIPF_ALLOW_VERTICAL_DODGE, AIP_FLAG);
set_aic_flag(aicp, "$no extra collision avoidance vs player:", AIPF2_NO_SPECIAL_PLAYER_AVOID, AIP_FLAG2);
set_aic_flag(aicp, "$all ships manage shields:", AIPF2_ALL_SHIPS_MANAGE_SHIELDS, AIP_FLAG2);
}
void reset_ai_class_names()
{
ai_class *aicp;
for (int i = 0; i < Num_ai_classes; i++) {
aicp = &Ai_classes[i];
Ai_class_names[i] = aicp->name;
}
}
#define AI_CLASS_INCREMENT 10
void parse_aitbl()
{
read_file_text("ai.tbl", CF_TYPE_TABLES);
reset_parse();
//Just in case parse_aitbl is called twice
free_ai_stuff();
Num_ai_classes = 0;
Num_alloced_ai_classes = AI_CLASS_INCREMENT;
Ai_classes = (ai_class*) vm_malloc(Num_alloced_ai_classes * sizeof(ai_class));
Ai_class_names = (char**) vm_malloc(Num_alloced_ai_classes * sizeof(char*));
required_string("#AI Classes");
while (required_string_either("#End", "$Name:")) {
parse_ai_class();
Num_ai_classes++;
if(Num_ai_classes >= Num_alloced_ai_classes)
{
Num_alloced_ai_classes += AI_CLASS_INCREMENT;
Ai_classes = (ai_class*) vm_realloc(Ai_classes, Num_alloced_ai_classes * sizeof(ai_class));
// Ai_class_names doesn't realloc all that well so we have to do it the hard way.
// Luckily, it's contents can be easily replaced so we don't have to save anything.
vm_free(Ai_class_names);
Ai_class_names = (char **) vm_malloc(Num_alloced_ai_classes * sizeof(char*));
reset_ai_class_names();
}
}
atexit(free_ai_stuff);
}
LOCAL int ai_inited = 0;
//========================= BOOK-KEEPING FUNCTIONS =======================
/**
* Called once at game start-up
*/
void ai_init()
{
if ( !ai_inited ) {
// Do the first time initialization stuff here
try
{
parse_aitbl();
}
catch (const parse::ParseException& e)
{
mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "ai.tbl", e.what()));
}
ai_inited = 1;
}
init_semirand();
ai_level_init();
}
/**
* This inits the AI. You should be able to call this between
* levels to reset everything.
*/
void ai_level_init()
{
int i;
// Do the stuff to reset all ai stuff here
for (i=0; i<MAX_AI_INFO ; i++) {
Ai_info[i].shipnum = -1;
}
Ai_goal_signature = 0;
for (i = 0; i < Num_iffs; i++)
Iff_info[i].ai_rearm_timestamp = timestamp(-1);
// clear out the stuff needed for AI firing powerful secondary weapons
ai_init_secondary_info();
Ai_last_arrive_path=0;
}
// BEGIN STEALTH
// -----------------------------------------------------------------------------
/**
* Check if object is a stealth ship
*/
bool is_object_stealth_ship(object* objp)
{
if (objp->type == OBJ_SHIP) {
if (Ships[objp->instance].flags2 & SF2_STEALTH) {
return true;
}
}
// not stealth ship
return false;
}
/**
* Init necessary AI info for new stealth target
*/
void init_ai_stealth_info(ai_info *aip, object *stealth_objp)
{
Assert(is_object_stealth_ship(stealth_objp));
// set necessary ai info for new stealth target
aip->stealth_last_pos = stealth_objp->pos;
aip->stealth_velocity = stealth_objp->phys_info.vel;
aip->stealth_last_visible_stamp = timestamp();
}
// -----------------------------------------------------------------------------
// Check whether Pl_objp can see a stealth ship object
#define STEALTH_NOT_IN_FRUSTUM 0
#define STEALTH_IN_FRUSTUM 1
#define STEALTH_FULLY_TARGETABLE 2
/**
* Return dist scaler based on skill level
*/
float get_skill_stealth_dist_scaler()
{
switch (Game_skill_level) {
case 0: // very easy
return 0.65f;
case 1: // easy
return 0.9f;
case 2: // medium
return 1.0f;
case 3: // hard
return 1.1f;
case 4: // insane
return 1.3f;
default:
Int3();
}
return 1.0f;
}
/**
* Return multiplier on dot based on skill level
*/
float get_skill_stealth_dot_scaler()
{
switch (Game_skill_level) {
case 0: // very easy
return 1.3f;
case 1: // easy
return 1.1f;
case 2: // medium
return 1.0f;
case 3: // hard
return 0.9f;
case 4: // insane
return 0.7f;
default:
Int3();
}
return 1.0f;
}
int ai_is_stealth_visible(object *viewer_objp, object *stealth_objp)
{
ship *shipp;
vec3d vec_to_stealth;
float dot_to_stealth, dist_to_stealth, max_stealth_dist;
Assert(stealth_objp->type == OBJ_SHIP);
shipp = &Ships[stealth_objp->instance];
Assert(viewer_objp->type == OBJ_SHIP);
// check if stealth ship
Assert(shipp->flags2 & SF2_STEALTH);
// check if in neb and below awac level for visible
if ( !ship_is_visible_by_team(stealth_objp, &Ships[viewer_objp->instance]) ) {
vm_vec_sub(&vec_to_stealth, &stealth_objp->pos, &viewer_objp->pos);
dist_to_stealth = vm_vec_mag_quick(&vec_to_stealth);
dot_to_stealth = vm_vec_dot(&viewer_objp->orient.vec.fvec, &vec_to_stealth) / dist_to_stealth;
// get max dist at which stealth is visible
max_stealth_dist = get_skill_stealth_dist_scaler() * STEALTH_MAX_VIEW_DIST;
// now check if within view frustum
float needed_dot_to_stealth;
if (dist_to_stealth < 100) {
needed_dot_to_stealth = 0.0f;
} else {
needed_dot_to_stealth = get_skill_stealth_dot_scaler() * float(STEALTH_VIEW_CONE_DOT) * (dist_to_stealth / max_stealth_dist);
}
if (dot_to_stealth > needed_dot_to_stealth) {
if (dist_to_stealth < max_stealth_dist) {
return STEALTH_IN_FRUSTUM;
}
}