2 * $Logfile: /Freespace2/code/Ship/AiBig.cpp $
7 * C module for AI code related to large ships
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 13 8/31/99 4:24p Andsager
15 * Reduce collisions when attacking big ships.
17 * 12 7/19/99 2:18p Mikeb
20 * 11 7/19/99 2:11p Mikeb
21 * DA: sometimes big_ship_attack_normal is not known
23 * 10 6/14/99 3:21p Andsager
24 * Allow collisions between ship and its debris. Fix up collision pairs
25 * when large ship is warping out.
27 * 9 6/02/99 5:41p Andsager
28 * Reduce range of secondary weapons not fired from turrets in nebula.
29 * Reduce range of beams fired from turrrets in nebula
31 * 8 5/13/99 2:31p Jamesa
32 * DA: Temp fix when attacking subsys of big ship and BS is blown up.
34 * 7 5/06/99 11:46a Andsager
35 * Bug fixes. Don't get into illegal strafe submode. Don't choose turret
36 * enemy objnum for beam protected.
38 * 6 4/23/99 12:01p Johnson
41 * 5 4/20/99 3:40p Andsager
42 * Changes to big ship ai. Uses bounding box as limit where to fly to
45 * 4 4/16/99 5:54p Dave
46 * Support for on/off style "stream" weapons. Real early support for
47 * target-painting lasers.
49 * 3 10/13/98 9:29a Dave
50 * Started neatening up freespace.h. Many variables renamed and
51 * reorganized. Added AlphaColors.[h,cpp]
53 * 2 10/07/98 10:53a Dave
56 * 1 10/07/98 10:51a Dave
58 * 77 5/25/98 12:12a Mike
59 * Improve big ship avoidance to avoid big ships when in a nearby dock
60 * mode. Fixes very dumb looking behavior in sm2-01a. Verified sm1-08a
61 * not too dumb at start.
63 * 76 5/23/98 2:38a Mike
64 * Improve balance of cheat-firing Synaptics.
66 * 75 5/23/98 12:40a Mike
67 * Hook for Delta wing to fire Synaptic bombs in sm3-09a.
69 * 74 5/22/98 12:08p Mike
70 * Force reset of aspect locking when AI ships switch banks due to
71 * good-time-to-unload-secondaries
73 * 73 5/21/98 1:25p Lawrance
74 * Fix exception in ai_bpap() when no verts are in closest octant
76 * 72 5/21/98 9:57a Mike
77 * Massively improve firing of bombs at big ships. Improve willingness to
78 * take incoming fire to deliver bomb. Make a little easier to gain
81 * 71 5/12/98 10:06a Mike
82 * Fix a jillion bugs preventing ships from firing bombs at Lucifer in
85 * 70 5/10/98 11:30p Mike
86 * Better firing of bombs, less likely to go into strafe mode.
88 * 69 5/10/98 3:41a Lawrance
89 * Fix bug that was preventing ships from leaving strafe mode
91 * 68 5/08/98 4:39p Mike
92 * Make ships match bank when attacking large ships.
94 * 67 5/07/98 11:59p Mike
95 * Remove unneeded code, computation of num_verts.
97 * 66 4/29/98 5:01p Mike
98 * Large overhaul in how turrets fire.
100 * 65 4/27/98 11:59p Mike
101 * Intermediate checkin. Getting big ship turrets firing at big ships to
102 * use pick_big_attack_point.
104 * 64 4/23/98 12:32a Mike
105 * Fix bogus math which looked at (1-fov) instead of fov.
107 * 63 4/13/98 4:52p Allender
108 * remove AI_frametime and us flFrametime instead. Make lock warning work
109 * in multiplayer for aspect seeking missiles. Debris fixups
111 * 62 4/13/98 3:27p Lawrance
112 * Fix bug that caused ships to leave strafe mode right away since they
113 * hadn't taken damage for a while
115 * 61 4/12/98 2:02p Mike
116 * Make small ships avoid big ships.
117 * Turn on Collide_friendly flag.
119 * 60 4/01/98 9:21p John
120 * Made NDEBUG, optimized build with no warnings or errors.
122 * 59 3/25/98 12:00a Allender
123 * MK: make sure that output from function gets a valid valud (ai_pbap I
126 * 58 3/24/98 10:07p Adam
127 * AL: Add another Assert() to try and catch bogus wp->big_attack_point
129 * 57 3/24/98 9:56p Adam
130 * AL: Assert that local_attack_point is valid in ai_bpap
132 * 56 3/21/98 3:36p Mike
133 * Fix/optimize attacking of big ships.
135 * 55 3/16/98 5:17p Mike
136 * Make ships not get stuck on big ships when attacking.
138 * 54 3/16/98 12:03a Mike
139 * Add support for kamikaze mode. Add AIF_NO_DYNAMIC which means
140 * relentlessly pursue current goal.
142 * 53 3/12/98 4:04p Lawrance
143 * Leave strafe mode if haven't been hit for a while and no enemy
144 * fighter/bombers are close by.
146 * 52 3/09/98 5:12p Mike
147 * Make sure Pl_objp uses are valid.
148 * Throw asteroids at ships in asteroid field, not "Gal"
149 * Support ships warp out if no goals after 30 seconds.
151 * 51 3/03/98 11:51p Lawrance
152 * Ensure target is a ship before looking at SIF flags
154 * 50 3/02/98 11:35a Mike
155 * Fix logic with ai_do_default_behavior -- return after call.
157 * 49 2/19/98 7:37p Lawrance
158 * Fix bug that was causing problems with subsystem path following.
160 * 48 2/14/98 3:38p Mike
161 * Make bombs have arm time, drop for 1/2 second, have hull_strength.
162 * Make them not fired until closer to target.
164 * 47 2/13/98 3:56p Mike
165 * Make ships more likely to stick to player orders, not get so distracted
166 * by getting hit by someone else.
168 * 46 2/11/98 4:30p Lawrance
169 * Improve disarm/disable behavior.
171 * 45 2/11/98 1:16a Mike
172 * Mods to AI behavior to make it harder to stay on a ship's tail. Not
173 * too much improvement.
175 * 44 2/05/98 12:51a Mike
176 * Early asteroid stuff.
178 * 43 1/30/98 11:28a Comet
179 * Fix indexing problem into verts in ai_bpap()
181 * 42 1/29/98 10:46p Mike
182 * In ai_big_pick_attack_point, handle case of octant with no points.
184 * 41 1/29/98 1:39p Mike
185 * Better heat seeking homing on big ships.
187 * 40 1/27/98 4:18p Mike
188 * Not fire weapons too early when attacking large ships. Aspect lock
189 * take proper amount of time for AI ships.
191 * 39 1/22/98 5:14p Lawrance
192 * clean up ai_big code, clear path info when stop attacking a subsystem
194 * 38 1/20/98 11:41p Mike
195 * Ships use new system of preferred big weapons to fire. Also, support
196 * AIF_UNLOAD_SECONDARIES which means fire as fast as possible.
198 * 37 1/20/98 9:47a Mike
199 * Suppress optimized compiler warnings.
200 * Some secondary weapon work.
202 * 36 1/17/98 4:45p Mike
203 * Better support for AI selection of secondary weapons.
205 * 35 1/16/98 3:57p Mike
206 * Clean up some bugs with attacking of protected ships. Make turrets
207 * only fire at targets that are within range.
209 * 34 1/16/98 11:32a Mike
210 * Fix bug when a ship is attacking a dead subsystem on a protected ship.
212 * 33 1/07/98 1:32p Lawrance
213 * Remove some nprintf output
215 * 32 1/07/98 1:21p Sandeep
216 * Fix bug where accessing uninited pointer
218 * 31 1/06/98 6:58p Lawrance
219 * Attack turrets (sometimes) when fired upon while attacking a ship.
221 * 30 12/31/97 6:28p Lawrance
222 * Improve attacking subsystems on moving ships.
224 * 29 12/31/97 4:16p Lawrance
225 * Use different fov's for attacking subsystems depending if ship is
228 * 28 12/31/97 4:05p Mike
229 * Support for all teams optionally attacking all teams.
230 * Lead big ships when attacking their hull.
232 * 27 12/31/97 12:26p Lawrance
233 * Don't enter strafe mode for transports, fix bug that prevented strafe
234 * mode from getting entered.
236 * 26 12/30/97 5:20p Mike
237 * Don't fire secondaries at a protected ship if its hull is very weak.
239 * 25 12/30/97 4:26p Lawrance
240 * Take out some debug statements
242 * 24 12/15/97 7:16p Lawrance
243 * improving subsystem attacking
245 * 23 12/02/97 11:58a Lawrance
246 * avoid during strafe mode only when endangered by a weapon
248 * 22 12/01/97 5:11p Lawrance
249 * make strafe mode more effective... slow down when approaching and use
250 * afterburner in avoids
252 * 21 11/27/97 4:21p Lawrance
253 * set submode to -1 when switching to AIM_NONE from AIM_STRAFE
255 * 20 11/24/97 2:27a Lawrance
256 * if attacking a big ship and moving very slow, enter strafe mode if
257 * enemy fighter/bombers are close by
259 * 19 11/14/97 11:27a Johnson
260 * if goal_objnum is -1, and in STRAFE, return 1 from
261 * ai_maybe_follow_subsys_path(), so break out of STRAFE code
263 * 18 11/13/97 11:51a Johnson
264 * ALAN: comment out Int3() in AiBig... need to test on my own machine
266 * 17 11/12/97 11:51p Lawrance
267 * only check target_objnum == goal_objnum when following path
269 * 16 11/12/97 11:01p Lawrance
270 * change call to ai_find_path()
271 * ensure goal_objnum == target_objnum in STRAFE
273 * 15 11/12/97 11:18a Lawrance
274 * fix bug in path following of subsystems
276 * 14 11/11/97 5:22p Mike
277 * Don't allow ships to attack navbuoys.
278 * Make ships lead big ships with dumbfire.
279 * Make ships smarter about leaving evade weapon mode.
280 * Fix a couple Assert() bugs.
282 * 13 11/06/97 6:27p Lawrance
283 * fix bug that was messing up retreat point in STRAFE mode
285 * 12 11/06/97 4:53p Mike
286 * Limit number of ships that can simultaneously attack another. Make
287 * turrets inherit AI class from parent ship.
289 * 11 11/06/97 12:27a Mike
290 * Better avoid behavior.
291 * Modify ai_turn_towards_vector() to take a flag parameter.
293 * 10 11/02/97 10:55p Lawrance
294 * removed some unused code, added some comments
296 * 9 11/01/97 4:01p Mike
297 * Adapt to new ai_find_path().
299 * 8 10/31/97 11:24a Lawrance
300 * If taget changes from a big ship to small ship while in AIM_STRAFE,
301 * switch to AIM_CHASE
303 * 7 10/30/97 10:06p Lawrance
304 * chg some timing values so ship will reach retreat point
306 * 6 10/30/97 9:17p Lawrance
307 * work on getting AIM_STRAFE working well with disable/disarm, try to
310 * 5 10/30/97 12:32a Lawrance
311 * further work on AIM_STRAFE
313 * 4 10/29/97 6:25p Lawrance
314 * add AIM_STRAFE functionality
316 * 3 10/26/97 4:19p Lawrance
317 * added ai_get_weapon_dist()
319 * 2 10/26/97 3:24p Lawrance
320 * split off large ship ai code into AiBig.cpp
327 #include "linklist.h"
337 #include "floating.h"
339 #include "freespace.h"
341 #include "missiongoals.h"
342 #include "missionlog.h"
347 #include "hudmessage.h"
348 #include "missionmessage.h"
349 #include "cmeasure.h"
350 #include "staticrand.h"
351 #include "multimsgs.h"
352 #include "afterburner.h"
356 #include "missionparse.h"
358 #pragma optimize("", off)
359 #pragma auto_inline(off)
361 #define SCAN_FIGHTERS_INTERVAL 2000 // how often an AI fighter/bomber should scan for enemy fighter/bombers
362 // if sitting still and pounding on a big ship. If enemy fighters are
363 // close ( < ENTER_STRAFE_THREAT_DIST ), then enter AIM_STRAFE
365 #define ENTER_STRAFE_THREAT_DIST_SQUARED 360000 // use squared distance, instead of 600
367 #define MIN_DOT_TO_ATTACK_SUBSYS 0.7f
368 #define MIN_DOT_TO_ATTACK_MOVING_SUBSYS 0.97f
370 // AI BIG MAGIC NUMBERS
371 #define STRAFE_RETREAT_COLLIDE_TIME 2.0 // when anticipated collision time is less than this, begin retreat
372 #define STRAFE_RETREAT_COLLIDE_DIST 100 // when perpendicular distance to *surface* is less than this, begin retreat
373 #define STRAFE_RETREAT_BOX_DIST 300 // distance beyond the bounding box to retreat
375 #define EVADE_BOX_BASE_DISTANCE 300 // standard distance to end evade submode
376 #define EVADE_BOX_MIN_DISTANCE 200 // minimun distance to end evade submode, after long time
378 #define ATTACK_STOP_DISTANCE 150 // when distance to target is less than this, put on brakes
380 #define ATTACK_COLLIDE_BASE_DIST 300 // absolute distance at which to begin checking for possible collision
381 #define ATTACK_COLLIDE_AVOID_DIST 60 // perpendicular distance to attack surface at which begin avoiding
382 #define ATTACK_COLLIDE_AVOID_TIME 1.0 // anticipated collision time at which to begin evade
383 #define ATTACK_COLLIDE_SLOW_DIST 150 // perpendicular distance to attack surface at which begin slowing down
384 #define ATTACK_COLLIDE_SLOW_TIME 1.5 // anticipated collision time at which to begin slowing down
387 // forward declarations
388 void ai_big_evade_ship();
389 void ai_big_chase_attack(ai_info *aip, ship_info *sip, vector *enemy_pos, float dist_to_enemy);
390 void ai_big_avoid_ship();
391 int ai_big_maybe_follow_subsys_path(int do_dot_check=1);
393 extern int model_which_octant_distant_many( vector *pnt, int model_num,matrix *model_orient, vector * model_pos, polymodel **pm, int *octs);
394 extern void compute_desired_rvec(vector *rvec, vector *goal_pos, vector *cur_pos);
395 extern void big_ship_collide_recover_start(object *objp, object *big_objp, vector *collide_pos, vector *collision_normal);
398 // Called by ai_big_pick_attack_point.
399 // Generates a random attack point.
400 // If truly_random flag set (haha), then generate a pretty random number. Otherwise, generate a static rand which
401 // tends to not change from frame to frame.
402 // Try four times and choose nearest point to increase chance of getting a good point.
403 void ai_bpap(object *objp, vector *attacker_objp_pos, vector *attacker_objp_fvec, vector *attack_point, vector *local_attack_point, float fov, float weapon_travel_dist, vector *surface_normal)
406 vector result_point, best_point;
413 best_point = objp->pos;
414 nearest_dist = weapon_travel_dist;
416 model_which_octant_distant_many(attacker_objp_pos, Ships[objp->instance].modelnum, &objp->orient, &objp->pos, &pm, octs);
418 num_tries = (int) (vm_vec_dist(&objp->pos, attacker_objp_pos)/objp->radius);
423 num_tries = 4 - num_tries;
425 // Index #0 is best one.
426 if ( pm->octants[octs[0]].verts ) {
427 *local_attack_point = *pm->octants[octs[0]].verts[0]; // Set just in case it doesn't get set below.
429 vm_vec_zero(local_attack_point);
432 for (q=0; q<4; q++) {
433 octp = &pm->octants[octs[q]];
434 if (octp->nverts > 0) {
436 if (num_tries > octp->nverts)
437 num_tries = octp->nverts;
439 if (num_tries > octp->nverts)
440 num_tries = octp->nverts;
444 for (i=0; i<num_tries; i++) {
449 index = (int) (frand() * (octp->nverts));
451 rel_point = *octp->verts[index];
452 vm_vec_unrotate(&result_point, &rel_point, &objp->orient);
453 vm_vec_add2(&result_point, &objp->pos);
455 dist = vm_vec_normalized_dir(&v2p, &result_point, attacker_objp_pos);
456 dot = vm_vec_dot(&v2p, attacker_objp_fvec);
459 if (dist < nearest_dist) {
462 best_point = result_point;
463 *local_attack_point = rel_point;
464 Assert( !vm_is_vec_nan(local_attack_point) );
465 if (dot > (1.0f + fov)/2.0f) // If this point is quite good, quit searching for a better one.
474 *attack_point = best_point;
476 // Cast from attack_objp_pos to local_attack_pos and check for nearest collision.
477 // If no collision, cast to (0,0,0) [center of big ship]** [best_point initialized to 000]
479 // do in world coords to get attack point, then translate to local for local_attack_point
480 vector attack_dir, end_point, temp;
482 dist = vm_vec_normalized_dir(&attack_dir, attack_point, attacker_objp_pos);
485 vm_vec_scale_add(&end_point, attack_point, &attack_dir, 30.0f);
487 vm_vec_scale_add(&end_point, attack_point, attacker_objp_fvec, 30.0f);
491 mc.model_num = Ships[objp->instance].modelnum;
492 mc.orient = &objp->orient;
494 mc.p0 = attacker_objp_pos;
496 mc.flags = MC_CHECK_MODEL;
500 if (mc.num_hits > 0) {
501 *attack_point = mc.hit_point_world;
502 vm_vec_sub(&temp, attack_point, &objp->pos);
503 vm_vec_rotate(local_attack_point, &temp, &objp->orient);
504 if (surface_normal) {
505 vm_vec_unrotate(surface_normal, &mc.hit_normal, &objp->orient);
508 vm_vec_zero(local_attack_point);
509 *attack_point = objp->pos;
510 if (surface_normal) {
511 vm_vec_zero(surface_normal);
516 // Stuff a point to attack based on nearest octant.
517 // If no points in that octant, leave attack_point unmodified.
519 // Note: Default value for fov is 1.0f 1.0f means don't use fov parameter.
520 // If fov != 1.0f, try up to four times to find a point that's in the field of view.
521 void ai_big_pick_attack_point_turret(object *objp, ship_subsys *ssp, vector *gpos, vector *gvec, vector *attack_point, float fov, float weapon_travel_dist)
523 if (!timestamp_elapsed(ssp->turret_pick_big_attack_point_timestamp)) {
525 vm_vec_unrotate(&result_point, &ssp->turret_big_attack_point, &objp->orient);
526 vm_vec_add(attack_point, &result_point, &objp->pos);
528 vector local_attack_point;
529 ssp->turret_pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
530 ai_bpap(objp, gpos, gvec, attack_point, &local_attack_point, fov, weapon_travel_dist, NULL);
531 ssp->turret_big_attack_point = local_attack_point;
535 // Stuff a point to attack based on nearest octant.
536 // If no points in that octant, leave attack_point unmodified.
538 // Note: Default value for fov is 1.0f 1.0f means don't use fov parameter.
539 // If fov != 1.0f, try up to four times to find a point that's in the field of view.
540 // Note, attacker_objp can be a ship or a weapon.
541 void ai_big_pick_attack_point(object *objp, object *attacker_objp, vector *attack_point, float fov)
543 Assert(objp->instance > -1);
544 Assert(objp->type == OBJ_SHIP);
546 vector local_attack_point;
548 switch (attacker_objp->type) {
550 ai_info *attacker_aip;
551 attacker_aip = &Ai_info[Ships[attacker_objp->instance].ai_index];
552 if (!timestamp_elapsed(attacker_aip->pick_big_attack_point_timestamp)) {
555 vm_vec_unrotate(&result_point, &attacker_aip->big_attack_point, &objp->orient);
556 vm_vec_add(attack_point, &result_point, &objp->pos);
561 attacker_aip->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
565 weapon *wp = &Weapons[attacker_objp->instance];
567 if (!timestamp_elapsed(wp->pick_big_attack_point_timestamp)) {
570 vm_vec_unrotate(&result_point, &wp->big_attack_point, &objp->orient);
571 vm_vec_add(attack_point, &result_point, &objp->pos);
575 wp->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
581 // checks valid line to target
582 vector surface_normal;
583 ai_bpap(objp, &attacker_objp->pos, &attacker_objp->orient.fvec, attack_point, &local_attack_point, fov, 99999.9f, &surface_normal);
585 switch (attacker_objp->type) {
587 ai_info *attacker_aip;
588 // if we can't find a new local_attack_point don't change local_attack_point
589 if (vm_vec_mag_squared(&local_attack_point) < 1) {
593 attacker_aip = &Ai_info[Ships[attacker_objp->instance].ai_index];
594 attacker_aip->big_attack_point = local_attack_point;
595 attacker_aip->big_attack_surface_normal = surface_normal;
599 weapon *wp = &Weapons[attacker_objp->instance];
600 wp->big_attack_point = local_attack_point;
601 Assert( !vm_is_vec_nan(&wp->big_attack_point) );
609 // Handler for SM_EVADE submode ( called from ai_big_chase() )
610 void ai_big_evade_ship()
612 vector player_pos, enemy_pos;
614 ship *shipp = &Ships[Pl_objp->instance];
615 ai_info *aip = &Ai_info[shipp->ai_index];
616 vector randvec, semi_enemy_pos;
618 ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
620 dist = vm_vec_dist_quick(&player_pos, &enemy_pos);
621 vm_vec_rand_vec_quick(&randvec);
622 if ((Missiontime>>14) & 1) {
623 vm_vec_scale_add(&semi_enemy_pos, &enemy_pos, &randvec, dist/2.0f);
624 aip->prev_goal_point = semi_enemy_pos;
626 semi_enemy_pos = aip->prev_goal_point;
629 accelerate_ship(aip, 1.0f - ((Missiontime>>8) & 0x3f)/128.0f );
630 turn_away_from_point(Pl_objp, &semi_enemy_pos, 0.0f);
635 box_dist = get_world_closest_box_point_with_delta(&box_vec, En_objp, &player_pos, &is_inside, EVADE_BOX_BASE_DISTANCE);
636 if (box_dist > EVADE_BOX_BASE_DISTANCE) {
637 aip->submode = SM_ATTACK;
638 aip->submode_start_time = Missiontime;
639 } else if ((box_dist > EVADE_BOX_MIN_DISTANCE) && (Missiontime - aip->submode_start_time > i2f(5)) ) {
640 aip->submode = SM_ATTACK;
641 aip->submode_start_time = Missiontime;
646 /* if (dist > 4*En_objp->radius) {
647 aip->submode = SM_ATTACK;
648 aip->submode_start_time = Missiontime;
649 } else if (dist > En_objp->radius) {
650 if (Missiontime - aip->submode_start_time > i2f(5)) {
651 aip->submode = SM_ATTACK;
652 aip->submode_start_time = Missiontime;
657 // Handler for SM_AVOID submode ( called from ai_big_chase() )
658 void ai_big_avoid_ship()
663 // reset path following information
664 void ai_big_subsys_path_cleanup(ai_info *aip)
666 if ( aip->ai_flags & AIF_ON_SUBSYS_PATH ) {
667 aip->ai_flags &= ~AIF_ON_SUBSYS_PATH;
668 aip->path_goal_dist = -1;
669 aip->path_start = -1;
671 aip->path_length = 0;
675 // Maybe Pl_objp needs to follow a path to get in line-of-sight to a subsystem
676 // input: do_dot_check => default value 0, flag to indicate whether check should be done to ensure
677 // subsystem is within certain field of view. We don't want to check fov when
678 // strafing, since ship is weaving to avoid turret fire
679 int ai_big_maybe_follow_subsys_path(int do_dot_check)
682 float dot = 1.0f, min_dot;
685 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
686 target_objp = &Objects[aip->target_objnum];
688 if ( (aip->targeted_subsys != NULL) && (aip->target_objnum >= 0) && (aip->targeted_subsys->system_info->path_num >= 0) ) {
690 int subsys_path_num, subsys_in_sight, checked_sight;
693 pm = model_get( Ships[Pl_objp->instance].modelnum );
695 // If attacking a subsystem, ensure that we have an unobstructed line of sight... if not, then move
696 // towards path linked to subsystem
697 subsys_in_sight = 0; // assume Pl_objp doesn't have line of sight to subys
699 // only check for subsystem sight every N milliseconds
701 if ( timestamp_elapsed(aip->path_subsystem_next_check) ) {
702 vector geye, gsubpos;
705 aip->path_subsystem_next_check = timestamp(1500);
707 // get world pos of eye (stored in geye)
708 ep = &(pm->view_positions[0] );
709 model_find_world_point( &geye, &ep->pnt, Ships[Pl_objp->instance].modelnum, 0, &Pl_objp->orient, &Pl_objp->pos );
711 // get world pos of subsystem
712 vm_vec_unrotate(&gsubpos, &aip->targeted_subsys->system_info->pnt, &En_objp->orient);
713 vm_vec_add2(&gsubpos, &En_objp->pos);
717 // Calc dot between subsys normal (based on using path info), and subsys_to_eye vector. This is
718 // useful later when we want to decide whether we have a realistic line-of-sight to the subsystem.
719 vector subsys_normal, subsys_to_eye;
720 if ( do_dot_check ) {
721 if ( !ship_return_subsys_path_normal(&Ships[target_objp->instance], aip->targeted_subsys, &gsubpos, &subsys_normal) ) {
722 vm_vec_normalized_dir(&subsys_to_eye, &geye, &gsubpos);
723 dot = vm_vec_dot(&subsys_normal, &subsys_to_eye);
727 if ( ship_subsystem_in_sight(En_objp, aip->targeted_subsys, &geye, &gsubpos, 1) ) {
732 // check if subsystem not in sight
733 min_dot = (target_objp->phys_info.fspeed > 5.0f?MIN_DOT_TO_ATTACK_MOVING_SUBSYS:MIN_DOT_TO_ATTACK_SUBSYS);
734 if ( (checked_sight && ((!subsys_in_sight) || (dot < min_dot)) ) ) {
736 aip->path_goal_dist = 5;
737 subsys_path_num = aip->targeted_subsys->system_info->path_num;
738 if ( (aip->path_start) == -1 || (aip->mp_index != subsys_path_num) ) {
739 // maybe create a new path
740 if ( subsys_path_num >= 0 ) {
741 Assert(aip->target_objnum >= 0);
742 ai_find_path(Pl_objp, aip->target_objnum, subsys_path_num, 0, 1);
743 if ( aip->path_start >= 0 ) {
744 aip->ai_flags |= AIF_ON_SUBSYS_PATH;
750 if ( checked_sight && subsys_in_sight && (dot > min_dot) ) {
751 // we've got a clear shot, stay here for a bit
752 aip->path_subsystem_next_check = timestamp_rand(5000,8000);
755 // If there is a path that we are tracking, see if ship has gotten to within
756 // aip->path_goal_dist units. If so, then ship can stop following the path. This
757 // is required since we don't want to follow the path all the way to the subsystem,
758 // and we want ships to stop different distances from their path destination
759 if ( aip->path_length > 0 ) {
763 Assert(aip->path_length >= 2);
764 dist = vm_vec_dist_quick(&Path_points[aip->path_start+aip->path_length-2].pos, &Pl_objp->pos);
766 if ( aip->path_cur >= (aip->path_start+aip->path_length-1) ) {
770 min_dot = (target_objp->phys_info.fspeed > 5.0f?MIN_DOT_TO_ATTACK_MOVING_SUBSYS:MIN_DOT_TO_ATTACK_SUBSYS);
771 if ( (checked_sight && subsys_in_sight) && (dot > min_dot) ) {
775 // if we've reached the destination, then we can stop following the path
776 if ( path_done || ( dist < aip->path_goal_dist ) || in_view ) {
777 ai_big_subsys_path_cleanup(aip);
778 } else if ( dist > aip->path_goal_dist ) {
779 // If we have a path to follow, follow it and return
780 if ( aip->path_start != -1 ) {
781 // for now, only follow the path to the first point
782 if ( aip->path_cur < (aip->path_start+aip->path_length-1) ) {
783 if ( aip->goal_objnum != aip->target_objnum ) {
784 //Int3(); // what is going on here? - Get Alan
785 aip->previous_mode = aip->mode;
786 aip->mode = AIM_NONE;
801 // This function is only called from ai_big_chase_attack() when a ship is flying very slowly and
802 // attacking a big ship. The ship should scan for enemy fighter/bombers... if any are close, then
803 // return 1, otherwise return 0;
805 // input: aip => ai_info pointer for Pl_objp
806 // sip => ship_info pointer for Pl_objp
808 // exit: 1 => ship should enter strafe mode
809 // 0 => ship should not change ai mode, no fighter/bomber threats are near
811 // NOTE: uses SCAN_FIGHTERS_INTERVAL and ENTER_STRAFE_THREAT_DIST_SQUARED which are defined in AiBig.h
812 int ai_big_maybe_start_strafe(ai_info *aip, ship_info *sip)
814 // if moving slowly (or stopped), and SIF_SMALL_SHIP, then enter STRAFE mode if enemy fighter/bombers
816 if ( sip->flags & SIF_SMALL_SHIP ) {
817 if ( timestamp_elapsed(aip->scan_for_enemy_timestamp) ) {
823 aip->scan_for_enemy_timestamp = timestamp(SCAN_FIGHTERS_INTERVAL);
824 // iterate through ships, and see if any fighter/bomber from opposite team are near
825 so = GET_FIRST(&Ship_obj_list);
826 while( so != END_OF_LIST(&Ship_obj_list) ) {
827 test_objp = &Objects[so->objnum];
828 test_sp = &Ships[test_objp->instance];
830 if ( test_sp->team != Ships[Pl_objp->instance].team ) {
831 if ( Ship_info[test_sp->ship_info_index].flags & SIF_SMALL_SHIP ) {
832 dist_squared = vm_vec_dist_squared(&Pl_objp->pos, &test_objp->pos);
833 if ( dist_squared < ENTER_STRAFE_THREAT_DIST_SQUARED ) {
842 // If we've reached here, there are no enemy fighter/bombers near
846 // ATTACK submode handler for chase mode.
847 void ai_big_chase_attack(ai_info *aip, ship_info *sip, vector *enemy_pos, float dist_to_enemy)
850 float dot_to_enemy, time_to_hit;
851 polymodel *po = model_get( sip->modelnum );
853 // Maybe evade an incoming weapon.
854 if (((time_to_hit = ai_endangered_by_weapon(aip)) < 4.0f) && (time_to_hit >= 0.0f)) {
855 aip->submode = SM_EVADE_WEAPON;
856 aip->submode_start_time = Missiontime;
857 aip->prev_goal_point = En_objp->pos;
859 // If moving slowly, maybe evade incoming fire.
860 if (Pl_objp->phys_info.speed < 3.0f) {
862 for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
863 if ((objp->type == OBJ_WEAPON) && (Weapons[objp->instance].team != Ships[Pl_objp->instance].team))
864 if (Weapon_info[objp->instance].subtype == WP_LASER) {
868 vm_vec_sub(&in_vec, &objp->pos, &Pl_objp->pos);
869 if (vm_vec_dot(&in_vec, &objp->orient.fvec) > 0.0f) {
870 dist = vm_vec_normalize(&in_vec);
871 if ((dist < 200.0f) && (vm_vec_dot(&in_vec, &objp->orient.fvec) > 0.95f)) {
872 if ((Objects[objp->parent].signature == objp->parent_sig) && (vm_vec_dist_quick(&objp->pos, &Objects[objp->parent].pos) < 300.0f)) {
873 set_target_objnum(aip, objp->parent);
874 aip->submode = SM_ATTACK;
875 aip->submode_start_time = Missiontime;
877 aip->submode = SM_EVADE;
878 aip->submode_start_time = Missiontime;
879 aip->prev_goal_point = En_objp->pos;
886 // Since ship is moving slowly and attacking a large ship, scan if enemy fighters are near, if so
887 // then enter strafe mode
888 if ( ai_big_maybe_start_strafe(aip, sip) ) {
889 aip->previous_mode = aip->mode;
890 aip->mode = AIM_STRAFE;
891 aip->submode_parm0 = Missiontime; // use parm0 as time strafe mode entered
892 aip->submode = AIS_STRAFE_ATTACK;
893 aip->submode_start_time = Missiontime;
897 } // end if ( Pl_objp->phys_info.speed < 3.0f )
899 // see if Pl_objp needs to reposition to get a good shot at subsystem which is being attacked
900 if ( ai_big_maybe_follow_subsys_path() ) {
904 vector *rel_pos, vec_to_enemy;
905 float weapon_travel_dist;
907 start_bank = Ships[aip->shipnum].weapons.current_primary_bank;
909 if ((po->n_guns) && (start_bank != -1)) {
910 rel_pos = &po->gun_banks[start_bank].pnt[0];
914 dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, enemy_pos, &Pl_objp->pos);
915 dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
917 vector rvec_vec, *rvec = &rvec_vec;
919 if (dist_to_enemy > 500.0f)
920 compute_desired_rvec(rvec, enemy_pos, &Pl_objp->pos);
924 ai_turn_towards_vector(enemy_pos, Pl_objp, flFrametime, sip->srotation_time, NULL, rel_pos, 0.0f, 0, rvec);
926 // calc range of primary weapon
927 weapon_travel_dist = ai_get_weapon_dist(&Ships[Pl_objp->instance].weapons);
929 if ( aip->targeted_subsys != NULL ) {
930 if (dist_to_enemy > (weapon_travel_dist-20))
931 accelerate_ship(aip, 1.0f);
933 // AL 12-31-97: Move at least as quickly as your target is moving...
934 accelerate_ship(aip, max(1.0f - dot_to_enemy, Objects[aip->target_objnum].phys_info.fspeed/sip->max_speed));
939 if (dot_to_enemy < 0.0f) {
941 } else if (dot_to_enemy < 0.75f) {
944 if (dist_to_enemy > weapon_travel_dist/2.0f) {
947 accel = 1.0f - dot_to_enemy;
951 // use dist normal to enemy here (dont break 50 barrier)
952 if (dist_to_enemy < ATTACK_STOP_DISTANCE) {
953 // accelerate_ship(aip, accel * 0.5f);
954 accelerate_ship(aip, -1.0f);
956 accelerate_ship(aip, accel);
963 // Handler for submode SM_CONTINUOUS_TURN
964 void ai_big_chase_ct()
969 extern void ai_select_secondary_weapon(object *objp, ship_weapon *swp, int priority1 = -1, int priority2 = -1);
970 extern float set_secondary_fire_delay(ai_info *aip, ship *shipp, weapon_info *swip);
971 extern void ai_choose_secondary_weapon(object *objp, ai_info *aip, object *en_objp);
972 extern int maybe_avoid_big_ship(object *objp, object *ignore_objp, ai_info *aip, vector *goal_point, float delta_time);
974 extern void maybe_cheat_fire_synaptic(object *objp, ai_info *aip);
976 // Determine if Pl_objp should fire weapons at current target, based on input parameters
978 // dist_to_enemy => distance (in m) to attack point on current target
979 // dot_to_enemy => dot product between fvec of Pl_objp and vector from Pl_objp to attack point
981 void ai_big_maybe_fire_weapons(float dist_to_enemy, float dot_to_enemy, vector *firing_pos, vector *enemy_pos, vector *enemy_vel)
986 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
987 swp = &Ships[Pl_objp->instance].weapons;
989 if (dot_to_enemy > 0.95f - 0.5f * En_objp->radius/max(1.0f, En_objp->radius + dist_to_enemy)) {
990 aip->time_enemy_in_range += flFrametime;
992 // Chance of hitting ship is based on dot product of firing ship's forward vector with vector to ship
993 // and also the size of the target relative to distance to target.
994 if (dot_to_enemy > max(0.5f, 0.90f + aip->ai_accuracy/10.0f - En_objp->radius/max(1.0f,dist_to_enemy))) {
997 temp_shipp = &Ships[Pl_objp->instance];
998 ship_weapon *tswp = &temp_shipp->weapons;
1000 if ( tswp->num_primary_banks > 0 ) {
1001 Assert(tswp->current_primary_bank < tswp->num_primary_banks);
1002 weapon_info *wip = &Weapon_info[tswp->primary_bank_weapons[tswp->current_primary_bank]];
1004 if (dist_to_enemy < wip->max_speed * wip->lifetime)
1005 ai_fire_primary_weapon(Pl_objp);
1007 int priority1, priority2;
1012 // Maybe favor selecting a bomb.
1013 // Note, if you're firing a bomb, if it's aspect seeking, the firing conditions can be looser.
1014 if (Ship_info[Ships[En_objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))
1015 if (En_objp->phys_info.speed * dist_to_enemy < 5000.0f) // Don't select a bomb if enemy moving fast relative to distance
1016 priority1 = WIF_BOMB;
1018 if (!(En_objp->flags & OF_PROTECTED)) {
1019 //ai_select_secondary_weapon(Pl_objp, tswp, priority1, priority2); // Note, need to select to get weapon speed and lifetime
1021 ai_choose_secondary_weapon(Pl_objp, aip, En_objp);
1022 int current_bank = tswp->current_secondary_bank;
1023 weapon_info *swip = &Weapon_info[tswp->secondary_bank_weapons[current_bank]];
1025 // If ship is protected and very low on hits, don't fire missiles.
1026 if ((current_bank > -1) && (!(En_objp->flags & OF_PROTECTED) || (En_objp->hull_strength > 10*swip->damage))) {
1027 if (aip->ai_flags & AIF_UNLOAD_SECONDARIES) {
1028 if (timestamp_until(swp->next_secondary_fire_stamp[current_bank]) > swip->fire_wait*1000.0f) {
1029 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (swip->fire_wait*1000.0f));
1033 if (timestamp_elapsed(swp->next_secondary_fire_stamp[current_bank])) {
1035 float firing_range = swip->max_speed * swip->lifetime;
1036 // reduce firing range of secondaries in nebula
1037 extern int Nebula_sec_range;
1038 if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
1039 firing_range *= 0.8f;
1042 float t = 0.25f; // default delay in seconds until next fire.
1044 if (dist_to_enemy < firing_range*1.0f) {
1047 //vm_vec_scale_add(&future_enemy_pos, enemy_pos, enemy_vel, dist_to_enemy/swip->max_speed);
1048 //if (vm_vec_dist_quick(&future_enemy_pos, firing_pos) < firing_range * 0.8f) {
1049 if (ai_fire_secondary_weapon(Pl_objp)) {
1050 if (aip->ai_flags & AIF_UNLOAD_SECONDARIES) {
1051 t = swip->fire_wait;
1053 t = set_secondary_fire_delay(aip, temp_shipp, swip);
1056 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1060 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1067 aip->time_enemy_in_range *= (1.0f - flFrametime);
1071 // switch ai ship into chase mode
1072 void ai_big_switch_to_chase_mode(ai_info *aip)
1074 aip->previous_mode = aip->mode;
1075 aip->mode = AIM_CHASE;
1076 aip->submode = SM_ATTACK;
1077 aip->submode_start_time = Missiontime;
1080 extern int ai_big_strafe_maybe_retreat(float dist, vector *target_pos);
1082 // Make object Pl_objp chase object En_objp, which is a big ship, not a small ship.
1085 float dist_to_enemy, dot_to_enemy;
1086 vector player_pos, enemy_pos, vec_to_enemy;
1087 ship_info *sip = &Ship_info[Ships[Pl_objp->instance].ship_info_index];
1088 ship *shipp = &Ships[Pl_objp->instance];
1089 ai_info *aip = &Ai_info[shipp->ai_index];
1090 int enemy_ship_type;
1091 vector predicted_enemy_pos;
1093 Assert(aip->mode == AIM_CHASE);
1095 maybe_cheat_fire_synaptic(Pl_objp, aip);
1097 enemy_ship_type = Ship_info[Ships[En_objp->instance].ship_info_index].flags;
1099 ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
1101 player_pos = Pl_objp->pos;
1102 ai_big_pick_attack_point(En_objp, Pl_objp, &enemy_pos, 0.8f);
1104 // Compute the predicted position of the center of the ship, then add the delta to the goal pos.
1105 if (En_objp->phys_info.speed > 3.0f) {
1106 set_predicted_enemy_pos(&predicted_enemy_pos, Pl_objp, En_objp, aip);
1107 vm_vec_add2(&enemy_pos, &predicted_enemy_pos);
1108 vm_vec_sub2(&enemy_pos, &En_objp->pos);
1110 predicted_enemy_pos = En_objp->pos;
1112 if (aip->targeted_subsys != NULL) {
1113 get_subsystem_pos(&enemy_pos, En_objp, aip->targeted_subsys);
1116 dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &enemy_pos, &player_pos); // - En_objp->radius;
1117 dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1119 if (aip->ai_flags & AIF_TARGET_COLLISION) {
1120 if ( ai_big_strafe_maybe_retreat(dist_to_enemy, &enemy_pos) ) {
1121 aip->mode = AIM_STRAFE;
1122 aip->submode = AIS_STRAFE_AVOID;
1123 aip->submode_parm0 = Missiontime; // use parm0 as time strafe mode entered
1128 if (aip->ai_flags & AIF_KAMIKAZE) {
1129 //nprintf(("AI", "Kamikaze: %7.3f %7.3f\n", dot_to_enemy, dist_to_enemy));
1130 accelerate_ship(aip, 1.0f);
1131 if ((dist_to_enemy < 400.0f) && ai_maybe_fire_afterburner(Pl_objp, aip)) {
1132 afterburners_start(Pl_objp);
1133 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1137 // If just acquired target, or target is not in reasonable cone, don't refine believed enemy position.
1138 if ((dot_to_enemy < 0.25f) || (aip->target_time < 1.0f) || (aip->ai_flags & AIF_SEEK_LOCK)) {
1139 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1140 } else if (aip->targeted_subsys != NULL) {
1141 Assert(aip->targeted_subsys != NULL);
1142 get_subsystem_pos(&enemy_pos, En_objp, aip->targeted_subsys);
1143 vm_vec_add2(&enemy_pos, &predicted_enemy_pos);
1144 vm_vec_sub2(&enemy_pos, &En_objp->pos);
1145 dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &enemy_pos, &player_pos); // - En_objp->radius;
1146 dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1147 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy, En_objp->radius);
1148 } else if (En_objp->flags & OF_PROTECTED) { // If protected and we're not attacking a subsystem, stop attacking!
1149 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1150 aip->target_objnum = -1;
1151 if (find_enemy(Pl_objp-Objects, MAX_ENEMY_DISTANCE, Skill_level_max_attackers[Game_skill_level]) == -1) {
1152 ai_do_default_behavior(Pl_objp);
1156 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1159 // If recently acquired target and not looking at target, just turn a bit.
1160 switch (aip->submode) {
1162 case SM_SUPER_ATTACK:
1163 if (vm_vec_dist_quick(&Pl_objp->pos, &predicted_enemy_pos) > 100.0f + En_objp->radius * 2.0f) {
1164 if (maybe_avoid_big_ship(Pl_objp, En_objp, aip, &predicted_enemy_pos, 10.0f))
1168 if (aip->target_time < 2.0f)
1169 if ((dot_to_enemy < 0.9f) || (dist_to_enemy > 300.0f)) {
1170 aip->submode = SM_CONTINUOUS_TURN;
1171 aip->submode_start_time = Missiontime - fl2f(2.75f); // This backdated start time allows immediate switchout.
1172 if (dot_to_enemy > 0.0f)
1173 aip->target_time += flFrametime * dot_to_enemy;
1179 // Set turn and acceleration based on submode.
1181 switch (aip->submode) {
1182 case SM_CONTINUOUS_TURN:
1187 case SM_SUPER_ATTACK:
1188 case SM_ATTACK_FOREVER:
1189 ai_big_chase_attack(aip, sip, &enemy_pos, dist_to_enemy);
1193 ai_big_evade_ship();
1197 ai_big_avoid_ship();
1200 case SM_EVADE_WEAPON:
1205 aip->last_attack_time = Missiontime;
1206 aip->submode = SM_ATTACK;
1210 // maybe Pl_objp was forced into strafe mode, if so return now
1211 if ( aip->mode == AIM_STRAFE )
1214 // Maybe choose a new submode.
1215 if (aip->submode != SM_AVOID && aip->submode != SM_EVADE ) {
1216 // If a very long time since attacked, attack no matter what!
1217 if (aip->submode != SM_SUPER_ATTACK) {
1218 if (Missiontime - aip->last_attack_time > i2f(6)) {
1219 aip->submode = SM_SUPER_ATTACK;
1220 aip->submode_start_time = Missiontime;
1221 aip->last_attack_time = Missiontime;
1225 // If a collision is expected, pull out!
1226 float dist_normal_to_enemy;
1228 if (vm_vec_mag_squared(&aip->big_attack_surface_normal) > 0.9) {
1229 dist_normal_to_enemy = fl_abs((dist_to_enemy * vm_vec_dot(&vec_to_enemy, &aip->big_attack_surface_normal)));
1231 // don;t have normal so use a conservative value here
1232 dist_normal_to_enemy = 0.3f * dist_to_enemy;
1235 // float time_to_enemy = dist_normal_to_enemy / Pl_objp->phys_info.speed * fl_abs(vm_vec_dot(&Pl_objp->phys_info.vel, &aip->big_attack_surface_normal));
1236 // if (Framecount % 30 == 1) {
1237 // mprintf(("normal dist; %.1f, time: %.1f\n", dist_normal_to_enemy, time_to_enemy));
1240 // since we're not in strafe and we may get a bad normal, cap dist_normal_to_enemy as min(0.3*dist_to_enemy, self)
1241 // this will allow us to get closer on a bad normal
1242 dist_normal_to_enemy = max(0.3f*dist_to_enemy, dist_normal_to_enemy);
1244 if (dist_to_enemy < ATTACK_COLLIDE_BASE_DIST) {
1245 // within 50m or 1sec
1246 float time_to_enemy;
1247 if (vm_vec_mag_squared(&aip->big_attack_surface_normal) > 0.9) {
1248 if (Pl_objp->phys_info.speed > 0.1) {
1249 time_to_enemy = dist_normal_to_enemy / fl_abs(vm_vec_dot(&Pl_objp->phys_info.vel, &aip->big_attack_surface_normal));
1252 time_to_enemy = 100.0f;
1255 if (Pl_objp->phys_info.speed > 0.1) {
1256 time_to_enemy = dist_normal_to_enemy / Pl_objp->phys_info.speed;
1259 time_to_enemy = 100.0f;
1263 float speed_dist = max(0.0f, (Pl_objp->phys_info.speed-50) * 2);
1264 if ((dist_normal_to_enemy < ATTACK_COLLIDE_AVOID_DIST + speed_dist) || (time_to_enemy < ATTACK_COLLIDE_AVOID_TIME) ) {
1265 // get away, simulate crsh recovery (don't use avoid)
1266 // accelerate_ship(aip, -1.0f);
1267 big_ship_collide_recover_start(Pl_objp, En_objp, &Pl_objp->pos, NULL);
1268 // aip->submode = SM_AVOID;
1269 // aip->submode_start_time = Missiontime;
1270 } else if ((dist_normal_to_enemy < ATTACK_COLLIDE_SLOW_DIST) || (time_to_enemy < ATTACK_COLLIDE_SLOW_TIME) ) {
1272 accelerate_ship(aip, -1.0f);
1277 if ((dot_to_enemy > 1.0f - 0.1f * En_objp->radius/(dist_to_enemy + 1.0f)) && (Pl_objp->phys_info.speed > dist_to_enemy/5.0f)) {
1278 if (might_collide_with_ship(Pl_objp, En_objp, dot_to_enemy, dist_to_enemy, (aip->targeted_subsys == NULL)*2.0f + 1.5f)) {
1279 if ((Missiontime - aip->last_hit_time > F1_0*4) && (dist_to_enemy < Pl_objp->radius*2 + En_objp->radius*2)) {
1280 accelerate_ship(aip, -1.0f);
1282 aip->submode = SM_AVOID;
1283 aip->submode_start_time = Missiontime;
1289 switch (aip->submode) {
1290 case SM_CONTINUOUS_TURN:
1291 if (Missiontime - aip->submode_start_time > i2f(3)) {
1292 aip->last_attack_time = Missiontime;
1293 aip->submode = SM_ATTACK;
1294 aip->submode_start_time = Missiontime;
1299 case SM_ATTACK_FOREVER:
1303 if (dist_to_enemy > 4*En_objp->radius) {
1304 aip->submode = SM_ATTACK;
1305 aip->submode_start_time = Missiontime;
1306 aip->last_attack_time = Missiontime;
1310 case SM_SUPER_ATTACK:
1314 // if outside box by > 300 m
1315 // DA 4/20/98 can never get here attacking a big ship
1318 float box_dist = get_world_closest_box_point_with_delta(NULL, En_objp, &Pl_objp->pos, &is_inside, 0.0f);
1320 if (box_dist > 300) {
1321 aip->submode = SM_ATTACK;
1322 aip->last_attack_time = Missiontime;
1323 aip->submode_start_time = Missiontime;
1328 /* if (dot_to_enemy > -0.2f) {
1329 aip->submode_start_time = Missiontime;
1330 } else if (Missiontime - aip->submode_start_time > i2f(1)/2) {
1331 if (might_collide_with_ship(Pl_objp, En_objp, dot_to_enemy, dist_to_enemy, 3.0f)) {
1332 aip->submode_start_time = Missiontime;
1334 aip->submode = SM_ATTACK;
1335 aip->last_attack_time = Missiontime;
1336 aip->submode_start_time = Missiontime;
1342 case SM_EVADE_WEAPON:
1343 if (aip->danger_weapon_objnum == -1) {
1344 aip->submode = SM_ATTACK;
1345 aip->submode_start_time = Missiontime;
1346 aip->last_attack_time = Missiontime;
1351 aip->submode = SM_ATTACK;
1352 aip->last_attack_time = Missiontime;
1353 aip->submode_start_time = Missiontime;
1357 // Maybe fire primary weapon and update time_enemy_in_range
1359 //nprintf(("AI", "time_enemy_in_range = %7.3f, dot = %7.3f\n", aip->time_enemy_in_range, dot_to_enemy));
1361 // AL: add condition that Pl_objp must not be following a path to fire. This may be too extreme, but
1362 // I noticed AI ships firing inappropriately when following a path near a big ship.
1363 // TODO: investigate why ships fire (and aren't close to hitting ship) when following a path near
1365 if (aip->mode != AIM_EVADE && aip->path_start == -1 ) {
1366 ai_big_maybe_fire_weapons(dist_to_enemy, dot_to_enemy, &player_pos, &predicted_enemy_pos, &En_objp->phys_info.vel);
1368 aip->time_enemy_in_range *= (1.0f - flFrametime);
1371 void ai_big_ship(object *objp)
1376 // all three parms are output parameters
1377 // Enemy object is En_objp
1378 // pos => world pos of attack point
1379 // dist => distance from Pl_objp front to attack point
1380 // dot => dot of Pl_objp fvec and vector to attack point
1381 // fire_pos => world pos from which firing
1382 void ai_big_attack_get_data(vector *enemy_pos, float *dist_to_enemy, float *dot_to_enemy)
1384 vector player_pos, vec_to_enemy, predicted_enemy_pos;
1385 ship *shipp = &Ships[Pl_objp->instance];
1386 ai_info *aip = &Ai_info[shipp->ai_index];
1387 ship_info *esip = &Ship_info[Ships[En_objp->instance].ship_info_index];
1389 Assert(aip->mode == AIM_STRAFE);
1391 // ensure that Pl_objp is still targeting a big ship
1392 if ( !(esip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
1393 ai_big_switch_to_chase_mode(aip);
1397 ai_set_positions(Pl_objp, En_objp, aip, &player_pos, enemy_pos);
1399 player_pos = Pl_objp->pos;
1401 if (aip->targeted_subsys != NULL) {
1402 Assert(aip->targeted_subsys != NULL);
1403 get_subsystem_pos(enemy_pos, En_objp, aip->targeted_subsys);
1405 // checks valid line to target
1406 ai_big_pick_attack_point(En_objp, Pl_objp, enemy_pos, 0.8f);
1409 // Take player pos to be center of ship + ship_radius
1410 vm_vec_scale_add2(&player_pos, &Pl_objp->orient.fvec, Pl_objp->radius);
1412 // If seeking lock, try to point directly at ship, else predict position so lasers can hit it.
1413 // If just acquired target, or target is not in reasonable cone, don't refine believed enemy position.
1414 vm_vec_normalized_dir(&vec_to_enemy, enemy_pos, &player_pos);
1415 *dot_to_enemy=vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1416 if ((*dot_to_enemy < 0.25f) || (aip->target_time < 1.0f) || (aip->ai_flags & AIF_SEEK_LOCK)) {
1417 predicted_enemy_pos=*enemy_pos;
1419 vector gun_pos, pnt;
1420 polymodel *po = model_get( Ship_info[shipp->ship_info_index].modelnum );
1423 // Compute position of gun in absolute space and use that as fire position.
1424 pnt = po->gun_banks[0].pnt[0];
1425 vm_vec_unrotate(&gun_pos, &pnt, &Pl_objp->orient);
1426 vm_vec_add2(&gun_pos, &Pl_objp->pos);
1427 weapon_speed = ai_get_weapon_speed(&shipp->weapons);
1429 set_predicted_enemy_pos_turret(&predicted_enemy_pos, &gun_pos, Pl_objp, enemy_pos, &En_objp->phys_info.vel, weapon_speed, aip->time_enemy_in_range);
1432 *dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &predicted_enemy_pos, &player_pos);
1433 *dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1434 update_aspect_lock_information(aip, &vec_to_enemy, *dist_to_enemy, En_objp->radius);
1436 *enemy_pos = predicted_enemy_pos;
1439 // check to see if Pl_objp has gotten too close to attacking point.. if so, break off by entering
1440 // AIS_STRAFE_RETREAT
1441 int ai_big_strafe_maybe_retreat(float dist, vector *target_pos)
1444 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1446 vector vec_to_target;
1447 vm_vec_sub(&vec_to_target, target_pos, &Pl_objp->pos);
1449 float dist_to_target, dist_normal_to_target, time_to_target;
1450 dist_to_target = vm_vec_mag_quick(&vec_to_target);
1451 if (vm_vec_mag_quick(&aip->big_attack_surface_normal) > 0.9) {
1452 dist_normal_to_target = -vm_vec_dotprod(&vec_to_target, &aip->big_attack_surface_normal);
1454 dist_normal_to_target = 0.2f * vm_vec_mag_quick(&vec_to_target);
1457 dist_normal_to_target = max(0.2f*dist_to_target, dist_normal_to_target);
1458 time_to_target = dist_normal_to_target / Pl_objp->phys_info.speed;
1460 // add distance penalty for going too fast
1461 float speed_to_dist_penalty = max(0.0f, (Pl_objp->phys_info.speed-50));
1463 //if ((dot_to_enemy > 1.0f - 0.1f * En_objp->radius/(dist_to_enemy + 1.0f)) && (Pl_objp->phys_info.speed > dist_to_enemy/5.0f))
1465 // Inside 2 sec retreat, setting goal point to box point + 300m
1466 // If collision, use std collision resolution.
1467 if ( !(aip->ai_flags & AIF_KAMIKAZE) && ((aip->ai_flags & AIF_TARGET_COLLISION) || (time_to_target < STRAFE_RETREAT_COLLIDE_TIME) || (dist_normal_to_target < STRAFE_RETREAT_COLLIDE_DIST + speed_to_dist_penalty)) ) {
1468 if (aip->ai_flags & AIF_TARGET_COLLISION) {
1469 // use standard collision resolution
1470 aip->ai_flags &= ~AIF_TARGET_COLLISION;
1471 big_ship_collide_recover_start(Pl_objp, En_objp, &Pl_objp->pos, NULL);
1473 // too close for comfort so fly to box point + 300
1474 aip->submode = AIS_STRAFE_RETREAT1;
1475 aip->submode_start_time = Missiontime;
1480 box_dist = get_world_closest_box_point_with_delta(&goal_point, En_objp, &Pl_objp->pos, &is_inside, STRAFE_RETREAT_BOX_DIST);
1483 aip->goal_point = goal_point;
1485 if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1486 afterburners_start(Pl_objp);
1487 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1497 // attack directly to the turret and fire weapons
1498 void ai_big_strafe_attack()
1503 float target_dist, target_dot, accel, t;
1504 object *target_objp;
1506 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1508 if ( ai_big_maybe_follow_subsys_path(0) ) {
1512 ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1513 if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1516 target_objp = &Objects[aip->target_objnum];
1518 if (aip->ai_flags & AIF_KAMIKAZE) {
1519 if (target_dist < 1200.0f) {
1520 //nprintf(("AI", "Kamikaze: %7.3f %7.3f\n", target_dot, target_dist));
1521 ai_turn_towards_vector(&target_pos, Pl_objp, flFrametime, Ship_info[Ships[Pl_objp->instance].ship_info_index].srotation_time, NULL, NULL, 0.0f, 0);
1522 accelerate_ship(aip, 1.0f);
1523 if ((target_dist < 400.0f) && ai_maybe_fire_afterburner(Pl_objp, aip)) {
1524 afterburners_start(Pl_objp);
1525 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1531 if (!(aip->ai_flags & AIF_SEEK_LOCK) || (aip->aspect_locked_time < 2.0f)) {
1532 t = ai_endangered_by_weapon(aip);
1533 if ( t > 0.0f && t < 1.5f ) {
1534 // set up goal_point for avoid path to turn towards
1535 aip->goal_point = Pl_objp->pos;
1538 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, 2.0f);
1541 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -2.0f);
1544 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, 2.0f);
1547 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -2.0f);
1551 vm_vec_scale(&rand_vec, 1000.0f);
1552 vm_vec_add2(&aip->goal_point, &rand_vec);
1554 aip->submode = AIS_STRAFE_AVOID;
1555 aip->submode_start_time = Missiontime;
1556 // nprintf(("Alan","Ship %s entering AIS_STRAFE_AVOID at frame %d\n", Ships[aip->shipnum].ship_name, Framecount));
1558 if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1559 afterburners_start(Pl_objp);
1560 aip->afterburner_stop_time = Missiontime + fl2f(0.5f);
1565 // maybe fire weapons at target
1566 ai_big_maybe_fire_weapons(target_dist, target_dot, &Pl_objp->pos, &En_objp->pos, &En_objp->phys_info.vel);
1567 turn_towards_point(Pl_objp, &target_pos, NULL, 0.0f);
1569 // Slow down if we've not been hit for a while
1570 fix last_hit = Missiontime - aip->last_hit_time;
1571 if ( target_dist > 1200 || last_hit < F1_0*6) {
1575 attack_time = f2fl(Missiontime - aip->submode_start_time);
1576 if ( attack_time > 15 ) {
1578 } else if ( attack_time > 10 ) {
1580 } else if ( attack_time > 8 ) {
1582 } else if ( attack_time > 5 ) {
1590 accelerate_ship(aip, accel);
1592 // if haven't been hit in quite a while, leave strafe mode
1594 long_enough = F1_0*20;
1595 if ( (last_hit > long_enough) && ( (Missiontime - aip->submode_parm0) > long_enough) ) {
1596 ai_big_switch_to_chase_mode(aip);
1600 // pick a new attack point when entering this state, and keep using it
1601 void ai_big_strafe_avoid()
1605 float target_dist, target_dot;
1608 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1610 mode_time = Missiontime - aip->submode_start_time;
1612 ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1613 if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1616 if ( mode_time > fl2f(0.5)) {
1617 aip->submode = AIS_STRAFE_ATTACK;
1618 aip->submode_start_time = Missiontime;
1619 // nprintf(("Alan","Ship %s entering AIS_STRAFE_ATTACK at frame %d\n", Ships[aip->shipnum].ship_name, Framecount));
1622 turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1623 accelerate_ship(aip, 1.0f);
1626 // move towards aip->goal_point in an evasive manner
1627 void ai_big_strafe_retreat1()
1633 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1635 dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1637 aip->submode = AIS_STRAFE_POSITION;
1638 aip->submode_start_time = Missiontime;
1639 // nprintf(("Alan","Ship %s entering AIS_STRAFE_POSITION\n", Ships[aip->shipnum].ship_name));
1643 if (Missiontime - aip->submode_start_time > fl2f(1.50f)) {
1644 // set up goal_point for avoid path to turn towards
1645 aip->prev_goal_point = Pl_objp->pos;
1648 vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec);
1651 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -1.0f);
1654 vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec);
1657 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -1.0f);
1661 //vm_vec_scale(&rand_vec, 200.0f);
1662 //vm_vec_add2(&aip->prev_goal_point, &rand_vec);
1663 vm_vec_scale_add(&aip->prev_goal_point, &aip->goal_point, &rand_vec, 200.0f);
1665 aip->submode = AIS_STRAFE_RETREAT2;
1666 aip->submode_start_time = Missiontime;
1669 turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1670 accelerate_ship(aip, 1.0f);
1673 void ai_big_strafe_retreat2()
1678 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1680 if (Pl_objp->phys_info.flags & PF_AFTERBURNER_ON ) {
1681 if (Missiontime > aip->afterburner_stop_time) {
1682 //nprintf(("AI", "Frame %i, turning off afterburner.\n", AI_FrameCount));
1683 afterburners_stop(Pl_objp);
1687 dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1689 aip->submode = AIS_STRAFE_POSITION;
1690 aip->submode_start_time = Missiontime;
1691 // nprintf(("Alan","Ship %s entering AIS_STRAFE_POSITION\n", Ships[aip->shipnum].ship_name));
1695 if (Missiontime - aip->submode_start_time > fl2f(0.70f)) {
1696 aip->submode = AIS_STRAFE_RETREAT1;
1697 aip->submode_start_time = Missiontime;
1699 if ( (Missiontime - aip->last_hit_time) < F1_0*5 ) {
1700 if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1701 afterburners_start(Pl_objp);
1702 aip->afterburner_stop_time = Missiontime + F1_0;
1707 turn_towards_point(Pl_objp, &aip->prev_goal_point, NULL, 0.0f);
1708 accelerate_ship(aip, 1.0f);
1711 // reposition self to begin another strafing run
1712 void ai_big_strafe_position()
1715 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1716 // TODO: loop around or something!!
1718 aip->submode = AIS_STRAFE_ATTACK;
1719 aip->submode_start_time = Missiontime;
1720 // nprintf(("Alan","Ship %s entering AIS_STRAFE_ATTACK\n", Ships[aip->shipnum].ship_name));
1723 // --------------------------------------------------------------------------
1724 // #define AIS_STRAFE_ATTACK 201 // fly towards target and attack
1725 // #define AIS_STRAFE_AVOID 202 // fly evasive vector to avoid incoming fire
1726 // #define AIS_STRAFE_RETREAT1 203 // fly away from attack point (directly)
1727 // #define AIS_STRAFE_RETREAT1 204 // fly away from attack point (on an avoid vector)
1728 // #define AIS_STRAFE_POSITION 205 // re-position to resume strafing attack
1730 void ai_big_strafe()
1734 aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1736 Assert(aip->mode == AIM_STRAFE);
1739 if ( aip->goal_objnum != aip->target_objnum ) {
1740 Int3(); // what is going on here? - Get Alan
1741 aip->mode = AIM_NONE;
1746 // check if target is still a big ship... if not enter chase mode
1747 if ( !(Ship_info[Ships[En_objp->instance].ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) ) {
1748 ai_big_switch_to_chase_mode(aip);
1752 switch (aip->submode) {
1753 case AIS_STRAFE_ATTACK:
1754 ai_big_strafe_attack();
1756 case AIS_STRAFE_AVOID:
1757 ai_big_strafe_avoid();
1759 case AIS_STRAFE_RETREAT1:
1760 ai_big_strafe_retreat1();
1762 case AIS_STRAFE_RETREAT2:
1763 ai_big_strafe_retreat2();
1765 case AIS_STRAFE_POSITION:
1766 ai_big_strafe_position();
1770 Int3(); // Illegal submode for AIM_STRAFE
1775 // See if Pl_objp should enter strafe mode (This is called from maybe_evade_dumbfire_weapon(), and the
1776 // weapon_objnum is for a weapon that is about to collide with Pl_objp
1778 // Check if weapon_objnum was fired by Pl_objp's target, and whether Pl_objp's target is a big ship, if
1779 // so, enter AIM_STRAFE
1780 int ai_big_maybe_enter_strafe_mode(object *pl_objp, int weapon_objnum, int consider_target_only)
1784 object *weapon_objp, *parent_objp;
1786 aip = &Ai_info[Ships[pl_objp->instance].ai_index];
1787 Assert(aip->mode != AIM_STRAFE); // can't happen
1789 // if Pl_objp has no target, then we can't enter strafe mode
1790 if ( aip->target_objnum < 0 ) {
1794 // if target is not a ship, stafe mode is not possible
1795 if ( Objects[aip->target_objnum].type != OBJ_SHIP ) {
1799 sip = &Ship_info[Ships[Objects[aip->target_objnum].instance].ship_info_index];
1801 // if Pl_objp's target is not a big/capital ship, then cannot enter strafe mode
1802 // AL 12-31-97: Even though transports are considered big ships, don't enter strafe mode on them
1803 if ( !(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) || (sip->flags & SIF_TRANSPORT) ) {
1807 // If Pl_objp not a fighter or bomber, don't enter strafe mode. -- MK, 11/11/97.
1808 if ( !(Ship_info[Ships[pl_objp->instance].ship_info_index].flags & (SIF_FIGHTER | SIF_BOMBER)) ) {
1812 Assert(weapon_objnum >= 0 && weapon_objnum < MAX_OBJECTS);
1813 weapon_objp = &Objects[weapon_objnum];
1814 Assert(weapon_objp->type == OBJ_WEAPON);
1816 Assert(weapon_objp->parent >= 0 && weapon_objp->parent < MAX_OBJECTS);
1817 parent_objp = &Objects[weapon_objp->parent];
1818 if ( (parent_objp->signature != weapon_objp->parent_sig) || (parent_objp->type != OBJ_SHIP) ) {
1822 // Maybe the ship which fired the weapon isn't the current target
1823 if ( OBJ_INDEX(parent_objp) != aip->target_objnum ) {
1825 //JAS IMPOSSIBLE if (1) { // consider_target_only ) {
1826 //JAS IMPOSSIBLE return 0;
1827 //JAS IMPOSSIBLE } else {
1829 sip = &Ship_info[Ships[parent_objp->instance].ship_info_index];
1830 if ( !(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) || (sip->flags & SIF_TRANSPORT) ) {
1833 set_target_objnum(aip, OBJ_INDEX(parent_objp));
1837 ai_big_strafe_maybe_attack_turret(pl_objp, weapon_objp);
1839 // if we've got this far, the weapon must have come from the player's target, and it is a
1840 // big/capital ship... so enter strafe mode
1841 aip->previous_mode = aip->mode;
1842 aip->mode = AIM_STRAFE;
1843 aip->submode_parm0 = Missiontime; // use parm0 as time strafe mode entered
1844 aip->submode = AIS_STRAFE_AVOID;
1845 aip->submode_start_time = Missiontime;
1846 // nprintf(("Alan","%s Accepted strafe mode\n", Ships[pl_objp->instance].ship_name));
1851 // Consider attacking a turret, if a turret actually fired the weapon
1852 // input: ship_objp => ship that will attack the turret
1854 void ai_big_strafe_maybe_attack_turret(object *ship_objp, object *weapon_objp)
1857 object *parent_objp;
1859 Assert(ship_objp->type == OBJ_SHIP);
1860 aip = &Ai_info[Ships[ship_objp->instance].ai_index];
1862 // Make decision to attack turret based on AI class. The better AI ships will realize that
1863 // it is better to take out the turrets first on a big ship.
1864 if ( (frand()*100) > (aip->ai_courage-15) )
1867 // If ship is already attacking a subsystem, don't switch
1868 if ( aip->targeted_subsys != NULL ) {
1872 // If the weapon is not from a turret, return
1873 if ( Weapons[weapon_objp->instance].turret_subsys == NULL ) {
1877 // Only attack turret if it sits on current target
1878 Assert(weapon_objp->parent >= 0 && weapon_objp->parent < MAX_OBJECTS);
1879 parent_objp = &Objects[weapon_objp->parent];
1880 if ( (parent_objp->signature != weapon_objp->parent_sig) || (parent_objp->type != OBJ_SHIP) ) {
1884 if ( aip->target_objnum != OBJ_INDEX(parent_objp) ) {
1888 // attack the turret
1889 set_targeted_subsys(aip, Weapons[weapon_objp->instance].turret_subsys, OBJ_INDEX(parent_objp));