]> icculus.org git repositories - taylor/freespace2.git/blob - src/ship/aibig.cpp
Initial revision
[taylor/freespace2.git] / src / ship / aibig.cpp
1 /*
2  * $Logfile: /Freespace2/code/Ship/AiBig.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * C module for AI code related to large ships
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 13    8/31/99 4:24p Andsager
15  * Reduce collisions when attacking big ships.
16  * 
17  * 12    7/19/99 2:18p Mikeb
18  * DA:  DOH!!
19  * 
20  * 11    7/19/99 2:11p Mikeb
21  * DA: sometimes big_ship_attack_normal is not known
22  * 
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.
26  * 
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
30  * 
31  * 8     5/13/99 2:31p Jamesa
32  * DA:  Temp fix when attacking subsys of big ship and BS is blown up.
33  * 
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.
37  * 
38  * 6     4/23/99 12:01p Johnson
39  * Added SIF_HUGE_SHIP
40  * 
41  * 5     4/20/99 3:40p Andsager
42  * Changes to big ship ai.  Uses bounding box as limit where to fly to
43  * when flying away.
44  * 
45  * 4     4/16/99 5:54p Dave
46  * Support for on/off style "stream" weapons. Real early support for
47  * target-painting lasers.
48  * 
49  * 3     10/13/98 9:29a Dave
50  * Started neatening up freespace.h. Many variables renamed and
51  * reorganized. Added AlphaColors.[h,cpp]
52  * 
53  * 2     10/07/98 10:53a Dave
54  * Initial checkin.
55  * 
56  * 1     10/07/98 10:51a Dave
57  * 
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.
62  * 
63  * 76    5/23/98 2:38a Mike
64  * Improve balance of cheat-firing Synaptics.
65  * 
66  * 75    5/23/98 12:40a Mike
67  * Hook for Delta wing to fire Synaptic bombs in sm3-09a.
68  * 
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
72  * 
73  * 73    5/21/98 1:25p Lawrance
74  * Fix exception in ai_bpap() when no verts are in closest octant
75  * 
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
79  * aspect lock.
80  * 
81  * 71    5/12/98 10:06a Mike
82  * Fix a jillion bugs preventing ships from firing bombs at Lucifer in
83  * sm3-09.
84  * 
85  * 70    5/10/98 11:30p Mike
86  * Better firing of bombs, less likely to go into strafe mode.
87  * 
88  * 69    5/10/98 3:41a Lawrance
89  * Fix bug that was preventing ships from leaving strafe mode
90  * 
91  * 68    5/08/98 4:39p Mike
92  * Make ships match bank when attacking large ships.
93  * 
94  * 67    5/07/98 11:59p Mike
95  * Remove unneeded code, computation of num_verts.
96  * 
97  * 66    4/29/98 5:01p Mike
98  * Large overhaul in how turrets fire.
99  * 
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.
103  * 
104  * 64    4/23/98 12:32a Mike
105  * Fix bogus math which looked at (1-fov) instead of fov.
106  * 
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
110  * 
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
114  * 
115  * 61    4/12/98 2:02p Mike
116  * Make small ships avoid big ships.
117  * Turn on Collide_friendly flag.
118  * 
119  * 60    4/01/98 9:21p John
120  * Made NDEBUG, optimized build with no warnings or errors.
121  * 
122  * 59    3/25/98 12:00a Allender
123  * MK: make sure that output from function gets a valid valud (ai_pbap I
124  * think)
125  * 
126  * 58    3/24/98 10:07p Adam
127  * AL: Add another Assert() to try and catch bogus wp->big_attack_point
128  * 
129  * 57    3/24/98 9:56p Adam
130  * AL: Assert that local_attack_point is valid in ai_bpap
131  * 
132  * 56    3/21/98 3:36p Mike
133  * Fix/optimize attacking of big ships.
134  * 
135  * 55    3/16/98 5:17p Mike
136  * Make ships not get stuck on big ships when attacking.
137  * 
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.
141  * 
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.
145  * 
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.
150  * 
151  * 51    3/03/98 11:51p Lawrance
152  * Ensure target is a ship before looking at SIF flags
153  * 
154  * 50    3/02/98 11:35a Mike
155  * Fix logic with ai_do_default_behavior -- return after call.
156  * 
157  * 49    2/19/98 7:37p Lawrance
158  * Fix bug that was causing problems with subsystem path following.
159  * 
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.
163  * 
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.
167  * 
168  * 46    2/11/98 4:30p Lawrance
169  * Improve disarm/disable behavior.
170  * 
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.
174  * 
175  * 44    2/05/98 12:51a Mike
176  * Early asteroid stuff.
177  * 
178  * 43    1/30/98 11:28a Comet
179  * Fix indexing problem into verts in ai_bpap()
180  * 
181  * 42    1/29/98 10:46p Mike
182  * In ai_big_pick_attack_point, handle case of octant with no points.
183  * 
184  * 41    1/29/98 1:39p Mike
185  * Better heat seeking homing on big ships.
186  * 
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.
190  * 
191  * 39    1/22/98 5:14p Lawrance
192  * clean up ai_big code, clear path info when stop attacking a subsystem
193  * 
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.
197  * 
198  * 37    1/20/98 9:47a Mike
199  * Suppress optimized compiler warnings.
200  * Some secondary weapon work.
201  * 
202  * 36    1/17/98 4:45p Mike
203  * Better support for AI selection of secondary weapons.
204  * 
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.
208  * 
209  * 34    1/16/98 11:32a Mike
210  * Fix bug when a ship is attacking a dead subsystem on a protected ship.
211  * 
212  * 33    1/07/98 1:32p Lawrance
213  * Remove some nprintf output
214  * 
215  * 32    1/07/98 1:21p Sandeep
216  * Fix bug where accessing uninited pointer
217  * 
218  * 31    1/06/98 6:58p Lawrance
219  * Attack turrets (sometimes) when fired upon while attacking a ship.
220  * 
221  * 30    12/31/97 6:28p Lawrance
222  * Improve attacking subsystems on moving ships.
223  * 
224  * 29    12/31/97 4:16p Lawrance
225  * Use different fov's for attacking subsystems depending if ship is
226  * moving
227  * 
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.
231  * 
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.
235  * 
236  * 26    12/30/97 5:20p Mike
237  * Don't fire secondaries at a protected ship if its hull is very weak.
238  * 
239  * 25    12/30/97 4:26p Lawrance
240  * Take out some debug statements
241  * 
242  * 24    12/15/97 7:16p Lawrance
243  * improving subsystem attacking
244  * 
245  * 23    12/02/97 11:58a Lawrance
246  * avoid during strafe mode only when endangered by a weapon
247  * 
248  * 22    12/01/97 5:11p Lawrance
249  * make strafe mode more effective... slow down when approaching and use
250  * afterburner in avoids
251  * 
252  * 21    11/27/97 4:21p Lawrance
253  * set submode to -1 when switching to AIM_NONE from AIM_STRAFE
254  * 
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
258  * 
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
262  * 
263  * 18    11/13/97 11:51a Johnson
264  * ALAN: comment out Int3() in AiBig... need to test on my own machine
265  * 
266  * 17    11/12/97 11:51p Lawrance
267  * only check target_objnum == goal_objnum when following path
268  * 
269  * 16    11/12/97 11:01p Lawrance
270  * change call to ai_find_path()
271  * ensure goal_objnum == target_objnum in STRAFE
272  * 
273  * 15    11/12/97 11:18a Lawrance
274  * fix bug in path following of subsystems
275  * 
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.
281  * 
282  * 13    11/06/97 6:27p Lawrance
283  * fix bug that was messing up retreat point in STRAFE mode
284  * 
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.
288  * 
289  * 11    11/06/97 12:27a Mike
290  * Better avoid behavior.
291  * Modify ai_turn_towards_vector() to take a flag parameter.
292  * 
293  * 10    11/02/97 10:55p Lawrance
294  * removed some unused code, added some comments
295  * 
296  * 9     11/01/97 4:01p Mike
297  * Adapt to new ai_find_path().
298  * 
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
302  * 
303  * 7     10/30/97 10:06p Lawrance
304  * chg some timing values so ship will reach retreat point
305  * 
306  * 6     10/30/97 9:17p Lawrance
307  * work on getting AIM_STRAFE working well with disable/disarm, try to
308  * balance
309  * 
310  * 5     10/30/97 12:32a Lawrance
311  * further work on AIM_STRAFE
312  * 
313  * 4     10/29/97 6:25p Lawrance
314  * add AIM_STRAFE functionality
315  * 
316  * 3     10/26/97 4:19p Lawrance
317  * added ai_get_weapon_dist()
318  * 
319  * 2     10/26/97 3:24p Lawrance
320  * split off large ship ai code into AiBig.cpp
321  *
322  * $NoKeywords: $
323  */
324
325 #include "pstypes.h"
326 #include "fix.h"
327 #include "linklist.h"
328 #include "object.h"
329 #include "physics.h"
330 #include "vecmat.h"
331 #include "ship.h"
332 #include "model.h"
333 #include "2d.h"
334 #include "3d.h"
335 #include "ai.h"
336 #include "aibig.h"
337 #include "floating.h"
338 #include "player.h"
339 #include "freespace.h"
340 #include "weapon.h"
341 #include "missiongoals.h"
342 #include "missionlog.h"
343 #include "timer.h"
344 #include "sound.h"
345 #include "aigoals.h"
346 #include "gamesnd.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"
353 #include "hudets.h"
354 #include "shipfx.h"
355 #include "shiphit.h"
356 #include "missionparse.h"
357
358 #pragma optimize("", off)
359 #pragma auto_inline(off)
360
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
364
365 #define ENTER_STRAFE_THREAT_DIST_SQUARED        360000  // use squared distance, instead of 600
366
367 #define MIN_DOT_TO_ATTACK_SUBSYS                                0.7f
368 #define MIN_DOT_TO_ATTACK_MOVING_SUBSYS 0.97f
369
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
374
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
377
378 #define ATTACK_STOP_DISTANCE                            150             // when distance to target is less than this, put on brakes
379
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
385
386
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);
392
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);
396
397
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)
404 {
405         float           nearest_dist;
406         vector  result_point, best_point;
407         vector  rel_point;
408         int             num_tries;
409         model_octant    *octp;
410         polymodel       *pm;
411         int             i, q, octs[4];  
412
413         best_point = objp->pos;
414         nearest_dist = weapon_travel_dist;
415
416         model_which_octant_distant_many(attacker_objp_pos, Ships[objp->instance].modelnum, &objp->orient, &objp->pos, &pm, octs);
417
418         num_tries = (int) (vm_vec_dist(&objp->pos, attacker_objp_pos)/objp->radius);
419
420         if (num_tries >= 4)
421                 num_tries = 1;
422         else
423                 num_tries = 4 - num_tries;
424
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.
428         } else {
429                 vm_vec_zero(local_attack_point);
430         }
431
432         for (q=0; q<4; q++) {
433                 octp = &pm->octants[octs[q]];
434                 if (octp->nverts > 0) {
435
436                         if (num_tries > octp->nverts)
437                                 num_tries = octp->nverts;
438
439                         if (num_tries > octp->nverts)
440                                 num_tries = octp->nverts;
441
442                         int     best_index = -1;
443
444                         for (i=0; i<num_tries; i++) {
445                                 int     index;
446                                 float   dist, dot;
447                                 vector  v2p;
448
449                                 index = (int) (frand() * (octp->nverts));
450
451                                 rel_point = *octp->verts[index];
452                                 vm_vec_unrotate(&result_point, &rel_point, &objp->orient);
453                                 vm_vec_add2(&result_point, &objp->pos);
454
455                                 dist = vm_vec_normalized_dir(&v2p, &result_point, attacker_objp_pos);
456                                 dot = vm_vec_dot(&v2p, attacker_objp_fvec);
457
458                                 if (dot > fov) {
459                                         if (dist < nearest_dist) {
460                                                 best_index = index;
461                                                 nearest_dist = 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.
466                                                         goto done_1;
467                                         }
468                                 }
469                         }
470                 }
471         }
472 done_1:
473
474         *attack_point = best_point;
475
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]
478
479         // do in world coords to get attack point, then translate to local for local_attack_point
480         vector attack_dir, end_point, temp;
481         float dist;
482         dist = vm_vec_normalized_dir(&attack_dir, attack_point, attacker_objp_pos);
483
484         if (dist > 0.1) {
485                 vm_vec_scale_add(&end_point, attack_point, &attack_dir, 30.0f);
486         } else {
487                 vm_vec_scale_add(&end_point, attack_point, attacker_objp_fvec, 30.0f);
488         }
489         
490         mc_info mc;
491         mc.model_num = Ships[objp->instance].modelnum;
492         mc.orient = &objp->orient;
493         mc.pos = &objp->pos;
494         mc.p0 = attacker_objp_pos;
495         mc.p1 = &end_point;
496         mc.flags = MC_CHECK_MODEL;
497         mc.radius = 0.0f;
498         model_collide(&mc);
499
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);
506                 }
507         } else {
508                 vm_vec_zero(local_attack_point);
509                 *attack_point = objp->pos;
510                 if (surface_normal) {
511                         vm_vec_zero(surface_normal);
512                 }
513         }
514 }
515
516 //      Stuff a point to attack based on nearest octant.
517 //      If no points in that octant, leave attack_point unmodified.
518 //
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)
522 {
523         if (!timestamp_elapsed(ssp->turret_pick_big_attack_point_timestamp)) {
524                 vector  result_point;
525                 vm_vec_unrotate(&result_point, &ssp->turret_big_attack_point, &objp->orient);
526                 vm_vec_add(attack_point, &result_point, &objp->pos);
527         } else {
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;
532         }
533 }
534
535 //      Stuff a point to attack based on nearest octant.
536 //      If no points in that octant, leave attack_point unmodified.
537 //
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)
542 {
543         Assert(objp->instance > -1);
544         Assert(objp->type == OBJ_SHIP);
545
546         vector  local_attack_point;
547
548         switch (attacker_objp->type) {
549         case OBJ_SHIP: {
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)) {
553                         vector  result_point;
554
555                         vm_vec_unrotate(&result_point, &attacker_aip->big_attack_point, &objp->orient);
556                         vm_vec_add(attack_point, &result_point, &objp->pos);
557
558                         return;
559                 }
560         
561                 attacker_aip->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
562                 break;
563                                                 }
564         case OBJ_WEAPON: {
565                 weapon  *wp = &Weapons[attacker_objp->instance];
566
567                 if (!timestamp_elapsed(wp->pick_big_attack_point_timestamp)) {
568                         vector  result_point;
569
570                         vm_vec_unrotate(&result_point, &wp->big_attack_point, &objp->orient);
571                         vm_vec_add(attack_point, &result_point, &objp->pos);
572
573                         return;
574                 }
575                 wp->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
576         
577                 break;
578                                                   }
579         }
580
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);
584
585         switch (attacker_objp->type) {
586         case OBJ_SHIP: {
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) {
590                         return;
591                 }
592
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;
596                 break;
597                                                 }
598         case OBJ_WEAPON: {
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) );
602                 break;
603                                                   }
604         }
605
606         return;
607 }
608
609 // Handler for SM_EVADE submode ( called from ai_big_chase() )
610 void ai_big_evade_ship()
611 {
612         vector  player_pos, enemy_pos;
613         float           dist;
614         ship            *shipp = &Ships[Pl_objp->instance];
615         ai_info *aip = &Ai_info[shipp->ai_index];
616         vector  randvec, semi_enemy_pos;
617
618         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
619
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;
625         } else {
626                 semi_enemy_pos = aip->prev_goal_point;
627         }
628         
629         accelerate_ship(aip, 1.0f - ((Missiontime>>8) & 0x3f)/128.0f );
630         turn_away_from_point(Pl_objp, &semi_enemy_pos, 0.0f);
631
632         float box_dist;
633         vector box_vec;
634         int is_inside;
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;
642         }
643
644         //TODO TEST
645
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;
653                 }
654         } */
655 }
656
657 // Handler for SM_AVOID submode ( called from ai_big_chase() )
658 void ai_big_avoid_ship()
659 {
660         ai_big_evade_ship();
661 }
662
663 // reset path following information
664 void ai_big_subsys_path_cleanup(ai_info *aip)
665 {
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;
670                 aip->path_cur = -1;
671                 aip->path_length = 0;
672         }
673 }
674
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)
680 {
681         ai_info *aip;
682         float           dot = 1.0f, min_dot;
683         object  *target_objp;
684
685         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
686         target_objp = &Objects[aip->target_objnum];
687
688         if ( (aip->targeted_subsys != NULL) && (aip->target_objnum >= 0) && (aip->targeted_subsys->system_info->path_num >= 0) ) {
689                 polymodel       *pm;
690                 int                     subsys_path_num, subsys_in_sight, checked_sight;
691                 float                   dist;
692
693                 pm = model_get( Ships[Pl_objp->instance].modelnum );
694         
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
698
699                 // only check for subsystem sight every N milliseconds
700                 checked_sight = 0;
701                 if ( timestamp_elapsed(aip->path_subsystem_next_check) ) {
702                         vector          geye, gsubpos;
703                         eye                     *ep;
704
705                         aip->path_subsystem_next_check = timestamp(1500);
706
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 );
710                         
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);
714
715                         checked_sight = 1;
716
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);
724                                 }
725                         }
726
727                         if ( ship_subsystem_in_sight(En_objp, aip->targeted_subsys, &geye, &gsubpos, 1) ) {
728                                 subsys_in_sight = 1;
729                         }
730                 }
731
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)) ) )  {
735
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;
745                                         }
746                                 }
747                         }
748                 }
749
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);
753                 }
754
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 ) {
760                         int path_done=0;
761                         int in_view=0;
762
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);
765
766                         if ( aip->path_cur >= (aip->path_start+aip->path_length-1) ) {
767                                 path_done=1;
768                         }
769
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) ) {
772                                 in_view=1;
773                         }
774
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;
787                                                         aip->submode = -1;
788                                                         return 1;
789                                                 }
790                                                 ai_path();
791                                                 return 1;
792                                         }
793                                 }
794                         }
795                 }
796         }
797
798         return 0;
799 }
800
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;
804 //
805 // input: aip   =>      ai_info pointer for Pl_objp
806 //                       sip    =>      ship_info pointer for Pl_objp
807 //
808 // exit:                1       =>      ship should enter strafe mode
809 //                              0       => ship should not change ai mode, no fighter/bomber threats are near
810 //
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)
813 {
814         // if moving slowly (or stopped), and SIF_SMALL_SHIP, then enter STRAFE mode if enemy fighter/bombers
815         // are near
816         if ( sip->flags & SIF_SMALL_SHIP ) {
817                 if ( timestamp_elapsed(aip->scan_for_enemy_timestamp) ) {
818                         ship_obj        *so;
819                         object  *test_objp;
820                         ship            *test_sp;
821                         float           dist_squared;
822                         
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];
829
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 ) {
834                                                         return 1;
835                                                 }
836                                         }
837                                 }
838                                 so = GET_NEXT(so);
839                         }
840                 }
841         }
842         // If we've reached here, there are no enemy fighter/bombers near
843         return 0;
844 }
845
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)
848 {
849         int             start_bank;
850         float           dot_to_enemy, time_to_hit;
851         polymodel *po = model_get( sip->modelnum );
852
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;
858         } else {
859                 //      If moving slowly, maybe evade incoming fire.
860                 if (Pl_objp->phys_info.speed < 3.0f) {
861                         object *objp;
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) {
865                                                 vector  in_vec;
866                                                 float           dist;
867
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;
876                                                                 } else {
877                                                                         aip->submode = SM_EVADE;
878                                                                         aip->submode_start_time = Missiontime;
879                                                                         aip->prev_goal_point = En_objp->pos;
880                                                                 }
881                                                         }
882                                                 }
883                                         }
884                         }
885                         
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;
894                                 return;
895                         }
896
897                 } // end if ( Pl_objp->phys_info.speed < 3.0f ) 
898
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() ) {
901                         return;
902                 }
903
904                 vector  *rel_pos, vec_to_enemy;
905                 float           weapon_travel_dist;
906
907                 start_bank = Ships[aip->shipnum].weapons.current_primary_bank;
908
909                 if ((po->n_guns) && (start_bank != -1)) {
910                         rel_pos = &po->gun_banks[start_bank].pnt[0];
911                 } else
912                         rel_pos = NULL;
913
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);
916
917                 vector  rvec_vec, *rvec = &rvec_vec;
918
919                 if (dist_to_enemy > 500.0f)
920                         compute_desired_rvec(rvec, enemy_pos, &Pl_objp->pos);
921                 else
922                         rvec = NULL;
923
924                 ai_turn_towards_vector(enemy_pos, Pl_objp, flFrametime, sip->srotation_time, NULL, rel_pos, 0.0f, 0, rvec);
925
926                 // calc range of primary weapon
927                 weapon_travel_dist = ai_get_weapon_dist(&Ships[Pl_objp->instance].weapons);
928
929                 if ( aip->targeted_subsys != NULL ) {
930                         if (dist_to_enemy > (weapon_travel_dist-20))
931                                 accelerate_ship(aip, 1.0f);
932                         else {
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));
935                         }
936
937                 } else {
938                         float accel;
939                         if (dot_to_enemy < 0.0f) {
940                                 accel = 0.5f;
941                         } else if (dot_to_enemy < 0.75f) {
942                                 accel = 0.75f;
943                         } else {
944                                 if (dist_to_enemy > weapon_travel_dist/2.0f) {
945                                         accel = 1.0f;
946                                 } else {
947                                         accel = 1.0f - dot_to_enemy;
948                                 }
949                         }
950
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);
955                         } else {
956                                 accelerate_ship(aip, accel);
957                         }
958
959                 }
960         }
961 }
962
963 // Handler for submode SM_CONTINUOUS_TURN
964 void ai_big_chase_ct()
965 {
966         ai_chase_ct();
967 }
968
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);
973
974 extern void maybe_cheat_fire_synaptic(object *objp, ai_info *aip);
975
976 // Determine if Pl_objp should fire weapons at current target, based on input parameters
977 //
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
980 //
981 void ai_big_maybe_fire_weapons(float dist_to_enemy, float dot_to_enemy, vector *firing_pos, vector *enemy_pos, vector *enemy_vel)
982 {
983         ai_info         *aip;
984         ship_weapon     *swp;
985
986         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
987         swp = &Ships[Pl_objp->instance].weapons;
988
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;
991                 
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))) {
995
996                         ship *temp_shipp;
997                         temp_shipp = &Ships[Pl_objp->instance];
998                         ship_weapon *tswp = &temp_shipp->weapons;
999
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]];
1003
1004                                 if (dist_to_enemy < wip->max_speed * wip->lifetime)
1005                                         ai_fire_primary_weapon(Pl_objp);
1006
1007                                 int     priority1, priority2;
1008
1009                                 priority1 = -1;
1010                                 priority2 = -1;
1011
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;
1017
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
1020
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]];
1024
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));
1030                                                         }
1031                                                 }
1032
1033                                                 if (timestamp_elapsed(swp->next_secondary_fire_stamp[current_bank])) {
1034
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;
1040                                                         }
1041
1042                                                         float t = 0.25f;        //      default delay in seconds until next fire.
1043
1044                                                         if (dist_to_enemy < firing_range*1.0f) {
1045
1046
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;
1052                                                                                 } else {
1053                                                                                         t = set_secondary_fire_delay(aip, temp_shipp, swip);
1054                                                                                 }
1055
1056                                                                                 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1057                                                                         }
1058                                                                 //}
1059                                                         }
1060                                                         swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1061                                                 }
1062                                         }
1063                                 }
1064                         }
1065                 }
1066         } else {
1067                 aip->time_enemy_in_range *= (1.0f - flFrametime);
1068         }
1069 }
1070
1071 // switch ai ship into chase mode
1072 void ai_big_switch_to_chase_mode(ai_info *aip)
1073 {
1074         aip->previous_mode = aip->mode;
1075         aip->mode = AIM_CHASE;
1076         aip->submode = SM_ATTACK;
1077         aip->submode_start_time = Missiontime;
1078 }
1079
1080 extern int ai_big_strafe_maybe_retreat(float dist, vector *target_pos);
1081
1082 // Make object Pl_objp chase object En_objp, which is a big ship, not a small ship.
1083 void ai_big_chase()
1084 {
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;
1092
1093         Assert(aip->mode == AIM_CHASE);
1094
1095         maybe_cheat_fire_synaptic(Pl_objp, aip);
1096
1097         enemy_ship_type = Ship_info[Ships[En_objp->instance].ship_info_index].flags;
1098
1099         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
1100
1101         player_pos = Pl_objp->pos;
1102         ai_big_pick_attack_point(En_objp, Pl_objp, &enemy_pos, 0.8f);
1103
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);
1109         }       else
1110                 predicted_enemy_pos = En_objp->pos;
1111
1112         if (aip->targeted_subsys != NULL) {
1113                 get_subsystem_pos(&enemy_pos, En_objp, aip->targeted_subsys);
1114         }
1115
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);
1118
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
1124                         return;
1125                 }
1126         }
1127
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;
1134                 }
1135         }
1136
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);
1153                         return;
1154                 }
1155         } else {
1156                 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1157         }
1158
1159         //      If recently acquired target and not looking at target, just turn a bit.
1160         switch (aip->submode) {
1161         case SM_ATTACK:
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))
1165                                 return;
1166                 }
1167
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;
1174                         }
1175                 break;
1176         }
1177
1178         //
1179         //      Set turn and acceleration based on submode.
1180         //
1181         switch (aip->submode) {
1182         case SM_CONTINUOUS_TURN:
1183                 ai_big_chase_ct();
1184                 break;
1185
1186         case SM_ATTACK:
1187         case SM_SUPER_ATTACK:
1188         case SM_ATTACK_FOREVER:
1189                 ai_big_chase_attack(aip, sip, &enemy_pos, dist_to_enemy);
1190                 break;
1191
1192         case SM_EVADE:
1193                 ai_big_evade_ship();
1194                 break;
1195
1196         case SM_AVOID:
1197                 ai_big_avoid_ship();
1198                 break;
1199
1200         case SM_EVADE_WEAPON:
1201                 evade_weapon();
1202                 break;
1203
1204         default:
1205                 aip->last_attack_time = Missiontime;
1206                 aip->submode = SM_ATTACK;
1207                 break;
1208         }
1209
1210         // maybe Pl_objp was forced into strafe mode, if so return now
1211         if ( aip->mode == AIM_STRAFE )
1212                 return;
1213
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;
1222                         }
1223                 }
1224
1225                 //      If a collision is expected, pull out!
1226                 float dist_normal_to_enemy;
1227
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)));
1230                 } else {
1231                         // don;t have normal so use a conservative value here
1232                         dist_normal_to_enemy = 0.3f * dist_to_enemy;
1233                 }
1234
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));
1238 //              }
1239                 
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);
1243
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));
1250                                 } else {
1251                                         // a big time
1252                                         time_to_enemy = 100.0f;
1253                                 }
1254                         } else {
1255                                 if (Pl_objp->phys_info.speed > 0.1) {
1256                                         time_to_enemy = dist_normal_to_enemy / Pl_objp->phys_info.speed;
1257                                 } else {
1258                                         // a big time
1259                                         time_to_enemy = 100.0f;
1260                                 }
1261                         }
1262
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) ) {
1271                                 // slow down
1272                                 accelerate_ship(aip, -1.0f);
1273                         }
1274                 }
1275
1276                 /*
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);
1281                                 } else {
1282                                         aip->submode = SM_AVOID;
1283                                         aip->submode_start_time = Missiontime;
1284                                 }
1285                         }
1286                 } */
1287         }
1288
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;
1295                 }
1296                 break;
1297
1298         case SM_ATTACK:
1299         case SM_ATTACK_FOREVER:
1300                 break;
1301                 
1302         case SM_EVADE:
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;
1307                 }
1308                 break;
1309
1310         case SM_SUPER_ATTACK:
1311                 break;
1312
1313         case SM_AVOID:
1314                 // if outside box by > 300 m
1315                 // DA 4/20/98  can never get here attacking a big ship
1316                 {
1317                 int is_inside;
1318                 float box_dist = get_world_closest_box_point_with_delta(NULL, En_objp, &Pl_objp->pos, &is_inside, 0.0f);
1319                 
1320                 if (box_dist > 300) {
1321                         aip->submode = SM_ATTACK;
1322                         aip->last_attack_time = Missiontime;
1323                         aip->submode_start_time = Missiontime;
1324                 }
1325                 }
1326                 break;
1327
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;
1333                         } else {
1334                                 aip->submode = SM_ATTACK;
1335                                 aip->last_attack_time = Missiontime;
1336                                 aip->submode_start_time = Missiontime;
1337                         }
1338                 }
1339
1340                 break;*/
1341
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;
1347                 }
1348                 break;
1349
1350         default:
1351                 aip->submode = SM_ATTACK;
1352                 aip->last_attack_time = Missiontime;
1353                 aip->submode_start_time = Missiontime;
1354         }
1355
1356         //
1357         //      Maybe fire primary weapon and update time_enemy_in_range
1358         //
1359         //nprintf(("AI", "time_enemy_in_range = %7.3f, dot = %7.3f\n", aip->time_enemy_in_range, dot_to_enemy));
1360
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
1364         //                               a big ship
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);
1367         } else
1368                 aip->time_enemy_in_range *= (1.0f - flFrametime);
1369 }
1370
1371 void ai_big_ship(object *objp)
1372 {
1373         // do nothing
1374 }
1375
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)
1383 {
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];
1388
1389         Assert(aip->mode == AIM_STRAFE);
1390
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);
1394                 return;
1395         }
1396
1397         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, enemy_pos);
1398
1399         player_pos = Pl_objp->pos;
1400
1401         if (aip->targeted_subsys != NULL) {
1402                 Assert(aip->targeted_subsys != NULL);
1403                 get_subsystem_pos(enemy_pos, En_objp, aip->targeted_subsys);
1404         } else {
1405                 // checks valid line to target
1406                 ai_big_pick_attack_point(En_objp, Pl_objp, enemy_pos, 0.8f);
1407         }
1408
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); 
1411
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;
1418         } else {
1419                 vector  gun_pos, pnt;
1420                 polymodel *po = model_get( Ship_info[shipp->ship_info_index].modelnum );
1421                 float           weapon_speed;
1422
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);
1428                 
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);
1430         }
1431
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);
1435
1436         *enemy_pos = predicted_enemy_pos;
1437 }
1438
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)
1442 {
1443         ai_info *aip;
1444         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1445
1446         vector vec_to_target;
1447         vm_vec_sub(&vec_to_target, target_pos, &Pl_objp->pos);
1448
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);
1453         } else {
1454                 dist_normal_to_target = 0.2f * vm_vec_mag_quick(&vec_to_target);
1455         }
1456
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;
1459
1460         // add distance penalty for going too fast
1461         float speed_to_dist_penalty = max(0.0f, (Pl_objp->phys_info.speed-50));
1462
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))
1464
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);
1472                 } else {
1473                         // too close for comfort so fly to box point + 300
1474                         aip->submode = AIS_STRAFE_RETREAT1;
1475                         aip->submode_start_time = Missiontime;
1476
1477                         float box_dist;
1478                         int is_inside;
1479                         vector goal_point;
1480                         box_dist = get_world_closest_box_point_with_delta(&goal_point, En_objp, &Pl_objp->pos, &is_inside, STRAFE_RETREAT_BOX_DIST);
1481
1482                         // set goal point
1483                         aip->goal_point = goal_point;
1484
1485                         if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1486                                 afterburners_start(Pl_objp);
1487                                 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1488                         }
1489                 }
1490
1491                 return 1;
1492         } else {
1493                 return 0;
1494         }
1495 }
1496
1497 // attack directly to the turret and fire weapons 
1498 void ai_big_strafe_attack()
1499 {
1500         ai_info *aip;
1501         vector  target_pos;
1502         vector  rand_vec;
1503         float           target_dist, target_dot, accel, t;
1504         object  *target_objp;
1505
1506         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1507
1508         if ( ai_big_maybe_follow_subsys_path(0) ) {
1509                 return;
1510         }
1511
1512         ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1513         if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1514                 return;
1515
1516         target_objp = &Objects[aip->target_objnum];
1517
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;
1526                         }
1527                         return;
1528                 }
1529         }
1530
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;
1536                         switch(rand()%4) {
1537                         case 0:
1538                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, 2.0f);
1539                                 break;
1540                         case 1:
1541                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -2.0f);
1542                                 break;
1543                         case 2:
1544                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, 2.0f);
1545                                 break;
1546                         case 3:
1547                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -2.0f);
1548                                 break;
1549                         } // end switch
1550
1551                         vm_vec_scale(&rand_vec, 1000.0f);
1552                         vm_vec_add2(&aip->goal_point, &rand_vec);
1553
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));
1557
1558                         if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1559                                 afterburners_start(Pl_objp);
1560                                 aip->afterburner_stop_time = Missiontime + fl2f(0.5f);
1561                         }
1562                 }
1563         }
1564
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);
1568
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) {
1572                 accel = 1.0f;
1573         } else {
1574                 float attack_time;
1575                 attack_time = f2fl(Missiontime - aip->submode_start_time);
1576                 if ( attack_time > 15 ) {
1577                         accel = 0.2f;
1578                 } else if ( attack_time > 10 ) {
1579                         accel = 0.4f;
1580                 } else if ( attack_time > 8 ) {
1581                         accel = 0.6f;
1582                 } else if ( attack_time > 5 ) {
1583                         accel = 0.8f;
1584                 } else {
1585                         accel = 1.0f;
1586                 }
1587         }
1588
1589         accel = 1.0f;
1590         accelerate_ship(aip, accel);
1591
1592         // if haven't been hit in quite a while, leave strafe mode
1593         fix long_enough;
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);
1597         }
1598 }
1599
1600 // pick a new attack point when entering this state, and keep using it
1601 void ai_big_strafe_avoid()
1602 {
1603         ai_info *aip;
1604         vector  target_pos;
1605         float           target_dist, target_dot;
1606         fix             mode_time;
1607
1608         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1609
1610         mode_time = Missiontime - aip->submode_start_time;
1611
1612         ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1613         if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1614                 return;
1615
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));
1620         }
1621
1622         turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1623         accelerate_ship(aip, 1.0f);
1624 }
1625
1626 // move towards aip->goal_point in an evasive manner
1627 void ai_big_strafe_retreat1()
1628 {
1629         float dist;
1630         ai_info *aip;
1631         vector  rand_vec;
1632
1633         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1634
1635         dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1636         if ( dist < 70 ) {
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));
1640                 return;
1641         }
1642
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;
1646                 switch(rand()%4) {
1647                 case 0:
1648                         vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec);
1649                         break;
1650                 case 1:
1651                         vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -1.0f);
1652                         break;
1653                 case 2:
1654                         vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec);
1655                         break;
1656                 case 3:
1657                         vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -1.0f);
1658                         break;
1659                 } // end switch
1660
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);
1664
1665                 aip->submode = AIS_STRAFE_RETREAT2;
1666                 aip->submode_start_time = Missiontime;
1667         }
1668
1669         turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1670         accelerate_ship(aip, 1.0f);
1671 }
1672
1673 void ai_big_strafe_retreat2()
1674 {
1675         float dist;
1676         ai_info *aip;
1677
1678         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1679
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);
1684                 }
1685         }
1686
1687         dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1688         if ( dist < 70 ) {
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));
1692                 return;
1693         }
1694
1695         if (Missiontime - aip->submode_start_time > fl2f(0.70f)) {
1696                 aip->submode = AIS_STRAFE_RETREAT1;
1697                 aip->submode_start_time = Missiontime;
1698
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;
1703                         }
1704                 }
1705         }
1706
1707         turn_towards_point(Pl_objp, &aip->prev_goal_point, NULL, 0.0f);
1708         accelerate_ship(aip, 1.0f);
1709 }
1710
1711 // reposition self to begin another strafing run
1712 void ai_big_strafe_position()
1713 {
1714         ai_info *aip;
1715         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1716         // TODO: loop around or something!!
1717
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));
1721 }
1722
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
1729 //
1730 void ai_big_strafe()
1731 {
1732         ai_info *aip;
1733
1734         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1735
1736         Assert(aip->mode == AIM_STRAFE);
1737
1738 /*
1739         if ( aip->goal_objnum != aip->target_objnum ) {
1740                 Int3(); // what is going on here? - Get Alan
1741                 aip->mode = AIM_NONE;
1742                 return;
1743         }
1744 */
1745
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);
1749                 return;
1750         }
1751         
1752         switch (aip->submode) {
1753         case AIS_STRAFE_ATTACK:
1754                 ai_big_strafe_attack();
1755                 break;
1756         case AIS_STRAFE_AVOID:
1757                 ai_big_strafe_avoid();
1758                 break;
1759         case AIS_STRAFE_RETREAT1:
1760                 ai_big_strafe_retreat1();
1761                 break;
1762         case AIS_STRAFE_RETREAT2:
1763                 ai_big_strafe_retreat2();
1764                 break;
1765         case AIS_STRAFE_POSITION:
1766                 ai_big_strafe_position();
1767                 break;
1768         default:
1769
1770                 Int3();         //      Illegal submode for AIM_STRAFE
1771                 break;
1772         }
1773 }
1774
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
1777 //
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)
1781 {
1782         ai_info         *aip;
1783         ship_info       *sip;
1784         object          *weapon_objp, *parent_objp;
1785
1786         aip = &Ai_info[Ships[pl_objp->instance].ai_index];
1787         Assert(aip->mode != AIM_STRAFE);                // can't happen
1788
1789         // if Pl_objp has no target, then we can't enter strafe mode
1790         if ( aip->target_objnum < 0 ) {
1791                 return 0;
1792         }
1793
1794         // if target is not a ship, stafe mode is not possible
1795         if ( Objects[aip->target_objnum].type != OBJ_SHIP ) {
1796                 return 0;
1797         }
1798
1799         sip = &Ship_info[Ships[Objects[aip->target_objnum].instance].ship_info_index];
1800
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) ) {
1804                 return 0;
1805         }
1806
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)) ) {
1809                 return 0;
1810         }
1811
1812         Assert(weapon_objnum >= 0 && weapon_objnum < MAX_OBJECTS);
1813         weapon_objp = &Objects[weapon_objnum];
1814         Assert(weapon_objp->type == OBJ_WEAPON);
1815
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) ) {
1819                 return 0;
1820         }
1821
1822         // Maybe the ship which fired the weapon isn't the current target
1823         if ( OBJ_INDEX(parent_objp) != aip->target_objnum ) {
1824
1825 //JAS IMPOSSIBLE                if (1) { // consider_target_only ) {
1826 //JAS IMPOSSIBLE                        return 0;
1827 //JAS IMPOSSIBLE                } else {
1828                         // switch targets
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) ) {
1831                                 return 0;
1832                         }
1833                         set_target_objnum(aip, OBJ_INDEX(parent_objp));
1834 //JAS IMPOSSIBLE                }
1835         }
1836
1837         ai_big_strafe_maybe_attack_turret(pl_objp, weapon_objp);
1838
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));
1847
1848         return 1;
1849 }
1850
1851 // Consider attacking a turret, if a turret actually fired the weapon
1852 // input:       ship_objp       =>      ship that will attack the turret
1853 //                              weapon_objp     =>      
1854 void ai_big_strafe_maybe_attack_turret(object *ship_objp, object *weapon_objp)
1855 {
1856         ai_info *aip;
1857         object  *parent_objp;
1858
1859         Assert(ship_objp->type == OBJ_SHIP);
1860         aip = &Ai_info[Ships[ship_objp->instance].ai_index];
1861
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) )
1865                 return;
1866
1867         // If ship is already attacking a subsystem, don't switch
1868         if ( aip->targeted_subsys != NULL ) {
1869                 return;
1870         }
1871
1872         // If the weapon is not from a turret, return
1873         if ( Weapons[weapon_objp->instance].turret_subsys == NULL ) {
1874                 return;
1875         }
1876
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) ) {
1881                 return;
1882         }
1883
1884         if ( aip->target_objnum != OBJ_INDEX(parent_objp) ) {
1885                 return;
1886         }
1887
1888         // attack the turret
1889         set_targeted_subsys(aip, Weapons[weapon_objp->instance].turret_subsys, OBJ_INDEX(parent_objp));
1890 }