]> icculus.org git repositories - taylor/freespace2.git/blob - src/ship/aibig.cpp
warning cleanup
[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.3  2002/06/01 07:12:33  relnev
11  * a few NDEBUG updates.
12  *
13  * removed a few warnings.
14  *
15  * Revision 1.2  2002/05/07 03:16:51  theoddone33
16  * The Great Newline Fix
17  *
18  * Revision 1.1.1.1  2002/05/03 03:28:10  root
19  * Initial import.
20  *
21  * 
22  * 13    8/31/99 4:24p Andsager
23  * Reduce collisions when attacking big ships.
24  * 
25  * 12    7/19/99 2:18p Mikeb
26  * DA:  DOH!!
27  * 
28  * 11    7/19/99 2:11p Mikeb
29  * DA: sometimes big_ship_attack_normal is not known
30  * 
31  * 10    6/14/99 3:21p Andsager
32  * Allow collisions between ship and its debris.  Fix up collision pairs
33  * when large ship is warping out.
34  * 
35  * 9     6/02/99 5:41p Andsager
36  * Reduce range of secondary weapons not fired from turrets in nebula.
37  * Reduce range of beams fired from turrrets in nebula
38  * 
39  * 8     5/13/99 2:31p Jamesa
40  * DA:  Temp fix when attacking subsys of big ship and BS is blown up.
41  * 
42  * 7     5/06/99 11:46a Andsager
43  * Bug fixes.  Don't get into illegal strafe submode.  Don't choose turret
44  * enemy objnum for beam protected.
45  * 
46  * 6     4/23/99 12:01p Johnson
47  * Added SIF_HUGE_SHIP
48  * 
49  * 5     4/20/99 3:40p Andsager
50  * Changes to big ship ai.  Uses bounding box as limit where to fly to
51  * when flying away.
52  * 
53  * 4     4/16/99 5:54p Dave
54  * Support for on/off style "stream" weapons. Real early support for
55  * target-painting lasers.
56  * 
57  * 3     10/13/98 9:29a Dave
58  * Started neatening up freespace.h. Many variables renamed and
59  * reorganized. Added AlphaColors.[h,cpp]
60  * 
61  * 2     10/07/98 10:53a Dave
62  * Initial checkin.
63  * 
64  * 1     10/07/98 10:51a Dave
65  * 
66  * 77    5/25/98 12:12a Mike
67  * Improve big ship avoidance to avoid big ships when in a nearby dock
68  * mode.  Fixes very dumb looking behavior in sm2-01a.  Verified sm1-08a
69  * not too dumb at start.
70  * 
71  * 76    5/23/98 2:38a Mike
72  * Improve balance of cheat-firing Synaptics.
73  * 
74  * 75    5/23/98 12:40a Mike
75  * Hook for Delta wing to fire Synaptic bombs in sm3-09a.
76  * 
77  * 74    5/22/98 12:08p Mike
78  * Force reset of aspect locking when AI ships switch banks due to
79  * good-time-to-unload-secondaries
80  * 
81  * 73    5/21/98 1:25p Lawrance
82  * Fix exception in ai_bpap() when no verts are in closest octant
83  * 
84  * 72    5/21/98 9:57a Mike
85  * Massively improve firing of bombs at big ships.  Improve willingness to
86  * take incoming fire to deliver bomb.  Make a little easier to gain
87  * aspect lock.
88  * 
89  * 71    5/12/98 10:06a Mike
90  * Fix a jillion bugs preventing ships from firing bombs at Lucifer in
91  * sm3-09.
92  * 
93  * 70    5/10/98 11:30p Mike
94  * Better firing of bombs, less likely to go into strafe mode.
95  * 
96  * 69    5/10/98 3:41a Lawrance
97  * Fix bug that was preventing ships from leaving strafe mode
98  * 
99  * 68    5/08/98 4:39p Mike
100  * Make ships match bank when attacking large ships.
101  * 
102  * 67    5/07/98 11:59p Mike
103  * Remove unneeded code, computation of num_verts.
104  * 
105  * 66    4/29/98 5:01p Mike
106  * Large overhaul in how turrets fire.
107  * 
108  * 65    4/27/98 11:59p Mike
109  * Intermediate checkin.  Getting big ship turrets firing at big ships to
110  * use pick_big_attack_point.
111  * 
112  * 64    4/23/98 12:32a Mike
113  * Fix bogus math which looked at (1-fov) instead of fov.
114  * 
115  * 63    4/13/98 4:52p Allender
116  * remove AI_frametime and us flFrametime instead.  Make lock warning work
117  * in multiplayer for aspect seeking missiles.  Debris fixups
118  * 
119  * 62    4/13/98 3:27p Lawrance
120  * Fix bug that caused ships to leave strafe mode right away since they
121  * hadn't taken damage for a while
122  * 
123  * 61    4/12/98 2:02p Mike
124  * Make small ships avoid big ships.
125  * Turn on Collide_friendly flag.
126  * 
127  * 60    4/01/98 9:21p John
128  * Made NDEBUG, optimized build with no warnings or errors.
129  * 
130  * 59    3/25/98 12:00a Allender
131  * MK: make sure that output from function gets a valid valud (ai_pbap I
132  * think)
133  * 
134  * 58    3/24/98 10:07p Adam
135  * AL: Add another Assert() to try and catch bogus wp->big_attack_point
136  * 
137  * 57    3/24/98 9:56p Adam
138  * AL: Assert that local_attack_point is valid in ai_bpap
139  * 
140  * 56    3/21/98 3:36p Mike
141  * Fix/optimize attacking of big ships.
142  * 
143  * 55    3/16/98 5:17p Mike
144  * Make ships not get stuck on big ships when attacking.
145  * 
146  * 54    3/16/98 12:03a Mike
147  * Add support for kamikaze mode.  Add AIF_NO_DYNAMIC which means
148  * relentlessly pursue current goal.
149  * 
150  * 53    3/12/98 4:04p Lawrance
151  * Leave strafe mode if haven't been hit for a while and no enemy
152  * fighter/bombers are close by.
153  * 
154  * 52    3/09/98 5:12p Mike
155  * Make sure Pl_objp uses are valid.
156  * Throw asteroids at ships in asteroid field, not "Gal"
157  * Support ships warp out if no goals after 30 seconds.
158  * 
159  * 51    3/03/98 11:51p Lawrance
160  * Ensure target is a ship before looking at SIF flags
161  * 
162  * 50    3/02/98 11:35a Mike
163  * Fix logic with ai_do_default_behavior -- return after call.
164  * 
165  * 49    2/19/98 7:37p Lawrance
166  * Fix bug that was causing problems with subsystem path following.
167  * 
168  * 48    2/14/98 3:38p Mike
169  * Make bombs have arm time, drop for 1/2 second, have hull_strength.
170  * Make them not fired until closer to target.
171  * 
172  * 47    2/13/98 3:56p Mike
173  * Make ships more likely to stick to player orders, not get so distracted
174  * by getting hit by someone else.
175  * 
176  * 46    2/11/98 4:30p Lawrance
177  * Improve disarm/disable behavior.
178  * 
179  * 45    2/11/98 1:16a Mike
180  * Mods to AI behavior to make it harder to stay on a ship's tail.  Not
181  * too much improvement.
182  * 
183  * 44    2/05/98 12:51a Mike
184  * Early asteroid stuff.
185  * 
186  * 43    1/30/98 11:28a Comet
187  * Fix indexing problem into verts in ai_bpap()
188  * 
189  * 42    1/29/98 10:46p Mike
190  * In ai_big_pick_attack_point, handle case of octant with no points.
191  * 
192  * 41    1/29/98 1:39p Mike
193  * Better heat seeking homing on big ships.
194  * 
195  * 40    1/27/98 4:18p Mike
196  * Not fire weapons too early when attacking large ships.  Aspect lock
197  * take proper amount of time for AI ships.
198  * 
199  * 39    1/22/98 5:14p Lawrance
200  * clean up ai_big code, clear path info when stop attacking a subsystem
201  * 
202  * 38    1/20/98 11:41p Mike
203  * Ships use new system of preferred big weapons to fire.  Also, support
204  * AIF_UNLOAD_SECONDARIES which means fire as fast as possible.
205  * 
206  * 37    1/20/98 9:47a Mike
207  * Suppress optimized compiler warnings.
208  * Some secondary weapon work.
209  * 
210  * 36    1/17/98 4:45p Mike
211  * Better support for AI selection of secondary weapons.
212  * 
213  * 35    1/16/98 3:57p Mike
214  * Clean up some bugs with attacking of protected ships.  Make turrets
215  * only fire at targets that are within range.
216  * 
217  * 34    1/16/98 11:32a Mike
218  * Fix bug when a ship is attacking a dead subsystem on a protected ship.
219  * 
220  * 33    1/07/98 1:32p Lawrance
221  * Remove some nprintf output
222  * 
223  * 32    1/07/98 1:21p Sandeep
224  * Fix bug where accessing uninited pointer
225  * 
226  * 31    1/06/98 6:58p Lawrance
227  * Attack turrets (sometimes) when fired upon while attacking a ship.
228  * 
229  * 30    12/31/97 6:28p Lawrance
230  * Improve attacking subsystems on moving ships.
231  * 
232  * 29    12/31/97 4:16p Lawrance
233  * Use different fov's for attacking subsystems depending if ship is
234  * moving
235  * 
236  * 28    12/31/97 4:05p Mike
237  * Support for all teams optionally attacking all teams.
238  * Lead big ships when attacking their hull.
239  * 
240  * 27    12/31/97 12:26p Lawrance
241  * Don't enter strafe mode for transports, fix bug that prevented strafe
242  * mode from getting entered.
243  * 
244  * 26    12/30/97 5:20p Mike
245  * Don't fire secondaries at a protected ship if its hull is very weak.
246  * 
247  * 25    12/30/97 4:26p Lawrance
248  * Take out some debug statements
249  * 
250  * 24    12/15/97 7:16p Lawrance
251  * improving subsystem attacking
252  * 
253  * 23    12/02/97 11:58a Lawrance
254  * avoid during strafe mode only when endangered by a weapon
255  * 
256  * 22    12/01/97 5:11p Lawrance
257  * make strafe mode more effective... slow down when approaching and use
258  * afterburner in avoids
259  * 
260  * 21    11/27/97 4:21p Lawrance
261  * set submode to -1 when switching to AIM_NONE from AIM_STRAFE
262  * 
263  * 20    11/24/97 2:27a Lawrance
264  * if attacking a big ship and moving very slow, enter strafe mode if
265  * enemy fighter/bombers are close by
266  * 
267  * 19    11/14/97 11:27a Johnson
268  * if goal_objnum is -1, and in STRAFE, return 1 from
269  * ai_maybe_follow_subsys_path(), so break out of STRAFE code
270  * 
271  * 18    11/13/97 11:51a Johnson
272  * ALAN: comment out Int3() in AiBig... need to test on my own machine
273  * 
274  * 17    11/12/97 11:51p Lawrance
275  * only check target_objnum == goal_objnum when following path
276  * 
277  * 16    11/12/97 11:01p Lawrance
278  * change call to ai_find_path()
279  * ensure goal_objnum == target_objnum in STRAFE
280  * 
281  * 15    11/12/97 11:18a Lawrance
282  * fix bug in path following of subsystems
283  * 
284  * 14    11/11/97 5:22p Mike
285  * Don't allow ships to attack navbuoys.
286  * Make ships lead big ships with dumbfire.
287  * Make ships smarter about leaving evade weapon mode.
288  * Fix a couple Assert() bugs.
289  * 
290  * 13    11/06/97 6:27p Lawrance
291  * fix bug that was messing up retreat point in STRAFE mode
292  * 
293  * 12    11/06/97 4:53p Mike
294  * Limit number of ships that can simultaneously attack another.  Make
295  * turrets inherit AI class from parent ship.
296  * 
297  * 11    11/06/97 12:27a Mike
298  * Better avoid behavior.
299  * Modify ai_turn_towards_vector() to take a flag parameter.
300  * 
301  * 10    11/02/97 10:55p Lawrance
302  * removed some unused code, added some comments
303  * 
304  * 9     11/01/97 4:01p Mike
305  * Adapt to new ai_find_path().
306  * 
307  * 8     10/31/97 11:24a Lawrance
308  * If taget changes from a big ship to small ship while in AIM_STRAFE,
309  * switch to AIM_CHASE
310  * 
311  * 7     10/30/97 10:06p Lawrance
312  * chg some timing values so ship will reach retreat point
313  * 
314  * 6     10/30/97 9:17p Lawrance
315  * work on getting AIM_STRAFE working well with disable/disarm, try to
316  * balance
317  * 
318  * 5     10/30/97 12:32a Lawrance
319  * further work on AIM_STRAFE
320  * 
321  * 4     10/29/97 6:25p Lawrance
322  * add AIM_STRAFE functionality
323  * 
324  * 3     10/26/97 4:19p Lawrance
325  * added ai_get_weapon_dist()
326  * 
327  * 2     10/26/97 3:24p Lawrance
328  * split off large ship ai code into AiBig.cpp
329  *
330  * $NoKeywords: $
331  */
332
333 #include "pstypes.h"
334 #include "fix.h"
335 #include "linklist.h"
336 #include "object.h"
337 #include "physics.h"
338 #include "vecmat.h"
339 #include "ship.h"
340 #include "model.h"
341 #include "2d.h"
342 #include "3d.h"
343 #include "ai.h"
344 #include "aibig.h"
345 #include "floating.h"
346 #include "player.h"
347 #include "freespace.h"
348 #include "weapon.h"
349 #include "missiongoals.h"
350 #include "missionlog.h"
351 #include "timer.h"
352 #include "sound.h"
353 #include "aigoals.h"
354 #include "gamesnd.h"
355 #include "hudmessage.h"
356 #include "missionmessage.h"
357 #include "cmeasure.h"
358 #include "staticrand.h"
359 #include "multimsgs.h"
360 #include "afterburner.h"
361 #include "hudets.h"
362 #include "shipfx.h"
363 #include "shiphit.h"
364 #include "missionparse.h"
365
366 #ifndef PLAT_UNIX
367 #pragma optimize("", off)
368 #pragma auto_inline(off)
369 #endif
370
371 #define SCAN_FIGHTERS_INTERVAL  2000            // how often an AI fighter/bomber should scan for enemy fighter/bombers
372                                                                                                                 // if sitting still and pounding on a big ship.  If enemy fighters are
373                                                                                                                 // close ( < ENTER_STRAFE_THREAT_DIST ), then enter AIM_STRAFE
374
375 #define ENTER_STRAFE_THREAT_DIST_SQUARED        360000  // use squared distance, instead of 600
376
377 #define MIN_DOT_TO_ATTACK_SUBSYS                                0.7f
378 #define MIN_DOT_TO_ATTACK_MOVING_SUBSYS 0.97f
379
380 // AI BIG MAGIC NUMBERS
381 #define STRAFE_RETREAT_COLLIDE_TIME     2.0             // when anticipated collision time is less than this, begin retreat
382 #define STRAFE_RETREAT_COLLIDE_DIST     100             // when perpendicular distance to *surface* is less than this, begin retreat
383 #define STRAFE_RETREAT_BOX_DIST                 300             // distance beyond the bounding box to retreat
384
385 #define EVADE_BOX_BASE_DISTANCE                 300             // standard distance to end evade submode
386 #define EVADE_BOX_MIN_DISTANCE                  200             // minimun distance to end evade submode, after long time
387
388 #define ATTACK_STOP_DISTANCE                            150             // when distance to target is less than this, put on brakes
389
390 #define ATTACK_COLLIDE_BASE_DIST                300             // absolute distance at which to begin checking for possible collision
391 #define ATTACK_COLLIDE_AVOID_DIST               60                      // perpendicular distance to attack surface at which begin avoiding
392 #define ATTACK_COLLIDE_AVOID_TIME               1.0             // anticipated collision time at which to begin evade
393 #define ATTACK_COLLIDE_SLOW_DIST                150             // perpendicular distance to attack surface at which begin slowing down
394 #define ATTACK_COLLIDE_SLOW_TIME                1.5             // anticipated collision time at which to begin slowing down
395
396
397 // forward declarations
398 void    ai_big_evade_ship();
399 void    ai_big_chase_attack(ai_info *aip, ship_info *sip, vector *enemy_pos, float dist_to_enemy);
400 void    ai_big_avoid_ship();
401 int     ai_big_maybe_follow_subsys_path(int do_dot_check=1);
402
403 extern int model_which_octant_distant_many( vector *pnt, int model_num,matrix *model_orient, vector * model_pos, polymodel **pm, int *octs);
404 extern void compute_desired_rvec(vector *rvec, vector *goal_pos, vector *cur_pos);
405 extern void big_ship_collide_recover_start(object *objp, object *big_objp, vector *collide_pos, vector *collision_normal);
406
407
408 //      Called by ai_big_pick_attack_point.
409 //      Generates a random attack point.
410 //      If truly_random flag set (haha), then generate a pretty random number.  Otherwise, generate a static rand which
411 //      tends to not change from frame to frame.
412 //      Try four times and choose nearest point to increase chance of getting a good point.
413 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)
414 {
415         float           nearest_dist;
416         vector  result_point, best_point;
417         vector  rel_point;
418         int             num_tries;
419         model_octant    *octp;
420         polymodel       *pm;
421         int             i, q, octs[4];  
422
423         best_point = objp->pos;
424         nearest_dist = weapon_travel_dist;
425
426         model_which_octant_distant_many(attacker_objp_pos, Ships[objp->instance].modelnum, &objp->orient, &objp->pos, &pm, octs);
427
428         num_tries = (int) (vm_vec_dist(&objp->pos, attacker_objp_pos)/objp->radius);
429
430         if (num_tries >= 4)
431                 num_tries = 1;
432         else
433                 num_tries = 4 - num_tries;
434
435         //      Index #0 is best one.
436         if ( pm->octants[octs[0]].verts ) {
437                 *local_attack_point = *pm->octants[octs[0]].verts[0];   //      Set just in case it doesn't get set below.
438         } else {
439                 vm_vec_zero(local_attack_point);
440         }
441
442         for (q=0; q<4; q++) {
443                 octp = &pm->octants[octs[q]];
444                 if (octp->nverts > 0) {
445
446                         if (num_tries > octp->nverts)
447                                 num_tries = octp->nverts;
448
449                         if (num_tries > octp->nverts)
450                                 num_tries = octp->nverts;
451
452                         int     best_index = -1;
453
454                         for (i=0; i<num_tries; i++) {
455                                 int     index;
456                                 float   dist, dot;
457                                 vector  v2p;
458
459                                 index = (int) (frand() * (octp->nverts));
460
461                                 rel_point = *octp->verts[index];
462                                 vm_vec_unrotate(&result_point, &rel_point, &objp->orient);
463                                 vm_vec_add2(&result_point, &objp->pos);
464
465                                 dist = vm_vec_normalized_dir(&v2p, &result_point, attacker_objp_pos);
466                                 dot = vm_vec_dot(&v2p, attacker_objp_fvec);
467
468                                 if (dot > fov) {
469                                         if (dist < nearest_dist) {
470                                                 best_index = index;
471                                                 nearest_dist = dist;
472                                                 best_point = result_point;
473                                                 *local_attack_point = rel_point;
474                                                 Assert( !vm_is_vec_nan(local_attack_point) );
475                                                 if (dot > (1.0f + fov)/2.0f)    //      If this point is quite good, quit searching for a better one.
476                                                         goto done_1;
477                                         }
478                                 }
479                         }
480                 }
481         }
482 done_1:
483
484         *attack_point = best_point;
485
486         // Cast from attack_objp_pos to local_attack_pos and check for nearest collision.
487         // If no collision, cast to (0,0,0) [center of big ship]**  [best_point initialized to 000]
488
489         // do in world coords to get attack point, then translate to local for local_attack_point
490         vector attack_dir, end_point, temp;
491         float dist;
492         dist = vm_vec_normalized_dir(&attack_dir, attack_point, attacker_objp_pos);
493
494         if (dist > 0.1) {
495                 vm_vec_scale_add(&end_point, attack_point, &attack_dir, 30.0f);
496         } else {
497                 vm_vec_scale_add(&end_point, attack_point, attacker_objp_fvec, 30.0f);
498         }
499         
500         mc_info mc;
501         mc.model_num = Ships[objp->instance].modelnum;
502         mc.orient = &objp->orient;
503         mc.pos = &objp->pos;
504         mc.p0 = attacker_objp_pos;
505         mc.p1 = &end_point;
506         mc.flags = MC_CHECK_MODEL;
507         mc.radius = 0.0f;
508         model_collide(&mc);
509
510         if (mc.num_hits > 0) {
511                 *attack_point = mc.hit_point_world;
512                 vm_vec_sub(&temp, attack_point, &objp->pos);
513                 vm_vec_rotate(local_attack_point, &temp, &objp->orient);
514                 if (surface_normal) {
515                         vm_vec_unrotate(surface_normal, &mc.hit_normal, &objp->orient);
516                 }
517         } else {
518                 vm_vec_zero(local_attack_point);
519                 *attack_point = objp->pos;
520                 if (surface_normal) {
521                         vm_vec_zero(surface_normal);
522                 }
523         }
524 }
525
526 //      Stuff a point to attack based on nearest octant.
527 //      If no points in that octant, leave attack_point unmodified.
528 //
529 //      Note: Default value for fov is 1.0f  1.0f means don't use fov parameter.
530 //      If fov != 1.0f, try up to four times to find a point that's in the field of view.
531 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)
532 {
533         if (!timestamp_elapsed(ssp->turret_pick_big_attack_point_timestamp)) {
534                 vector  result_point;
535                 vm_vec_unrotate(&result_point, &ssp->turret_big_attack_point, &objp->orient);
536                 vm_vec_add(attack_point, &result_point, &objp->pos);
537         } else {
538                 vector  local_attack_point;
539                 ssp->turret_pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
540                 ai_bpap(objp, gpos, gvec, attack_point, &local_attack_point, fov, weapon_travel_dist, NULL);
541                 ssp->turret_big_attack_point = local_attack_point;
542         }
543 }
544
545 //      Stuff a point to attack based on nearest octant.
546 //      If no points in that octant, leave attack_point unmodified.
547 //
548 //      Note: Default value for fov is 1.0f  1.0f means don't use fov parameter.
549 //      If fov != 1.0f, try up to four times to find a point that's in the field of view.
550 //      Note, attacker_objp can be a ship or a weapon.
551 void ai_big_pick_attack_point(object *objp, object *attacker_objp, vector *attack_point, float fov)
552 {
553         Assert(objp->instance > -1);
554         Assert(objp->type == OBJ_SHIP);
555
556         vector  local_attack_point;
557
558         switch (attacker_objp->type) {
559         case OBJ_SHIP: {
560                 ai_info *attacker_aip;
561                 attacker_aip = &Ai_info[Ships[attacker_objp->instance].ai_index];
562                 if (!timestamp_elapsed(attacker_aip->pick_big_attack_point_timestamp)) {
563                         vector  result_point;
564
565                         vm_vec_unrotate(&result_point, &attacker_aip->big_attack_point, &objp->orient);
566                         vm_vec_add(attack_point, &result_point, &objp->pos);
567
568                         return;
569                 }
570         
571                 attacker_aip->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
572                 break;
573                                                 }
574         case OBJ_WEAPON: {
575                 weapon  *wp = &Weapons[attacker_objp->instance];
576
577                 if (!timestamp_elapsed(wp->pick_big_attack_point_timestamp)) {
578                         vector  result_point;
579
580                         vm_vec_unrotate(&result_point, &wp->big_attack_point, &objp->orient);
581                         vm_vec_add(attack_point, &result_point, &objp->pos);
582
583                         return;
584                 }
585                 wp->pick_big_attack_point_timestamp = timestamp(2000 + (int) (frand()*500.0f));
586         
587                 break;
588                                                   }
589         }
590
591         // checks valid line to target
592         vector surface_normal;
593         ai_bpap(objp, &attacker_objp->pos, &attacker_objp->orient.fvec, attack_point, &local_attack_point, fov, 99999.9f, &surface_normal);
594
595         switch (attacker_objp->type) {
596         case OBJ_SHIP: {
597                 ai_info *attacker_aip;
598                 // if we can't find a new local_attack_point don't change local_attack_point
599                 if (vm_vec_mag_squared(&local_attack_point) < 1) {
600                         return;
601                 }
602
603                 attacker_aip = &Ai_info[Ships[attacker_objp->instance].ai_index];
604                 attacker_aip->big_attack_point = local_attack_point;
605                 attacker_aip->big_attack_surface_normal = surface_normal;
606                 break;
607                                                 }
608         case OBJ_WEAPON: {
609                 weapon  *wp = &Weapons[attacker_objp->instance];
610                 wp->big_attack_point = local_attack_point;
611                 Assert( !vm_is_vec_nan(&wp->big_attack_point) );
612                 break;
613                                                   }
614         }
615
616         return;
617 }
618
619 // Handler for SM_EVADE submode ( called from ai_big_chase() )
620 void ai_big_evade_ship()
621 {
622         vector  player_pos, enemy_pos;
623         float           dist;
624         ship            *shipp = &Ships[Pl_objp->instance];
625         ai_info *aip = &Ai_info[shipp->ai_index];
626         vector  randvec, semi_enemy_pos;
627
628         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
629
630         dist = vm_vec_dist_quick(&player_pos, &enemy_pos);
631         vm_vec_rand_vec_quick(&randvec);
632         if ((Missiontime>>14) & 1) {
633                 vm_vec_scale_add(&semi_enemy_pos, &enemy_pos, &randvec, dist/2.0f);
634                 aip->prev_goal_point = semi_enemy_pos;
635         } else {
636                 semi_enemy_pos = aip->prev_goal_point;
637         }
638         
639         accelerate_ship(aip, 1.0f - ((Missiontime>>8) & 0x3f)/128.0f );
640         turn_away_from_point(Pl_objp, &semi_enemy_pos, 0.0f);
641
642         float box_dist;
643         vector box_vec;
644         int is_inside;
645         box_dist = get_world_closest_box_point_with_delta(&box_vec, En_objp, &player_pos, &is_inside, EVADE_BOX_BASE_DISTANCE);
646         if (box_dist > EVADE_BOX_BASE_DISTANCE) {
647                 aip->submode = SM_ATTACK;
648                 aip->submode_start_time = Missiontime;
649         } else if ((box_dist > EVADE_BOX_MIN_DISTANCE) && (Missiontime - aip->submode_start_time > i2f(5)) ) {
650                 aip->submode = SM_ATTACK;
651                 aip->submode_start_time = Missiontime;
652         }
653
654         //TODO TEST
655
656 /*      if (dist > 4*En_objp->radius) {
657                 aip->submode = SM_ATTACK;
658                 aip->submode_start_time = Missiontime;
659         } else if (dist > En_objp->radius) {
660                 if (Missiontime - aip->submode_start_time > i2f(5)) {
661                         aip->submode = SM_ATTACK;
662                         aip->submode_start_time = Missiontime;
663                 }
664         } */
665 }
666
667 // Handler for SM_AVOID submode ( called from ai_big_chase() )
668 void ai_big_avoid_ship()
669 {
670         ai_big_evade_ship();
671 }
672
673 // reset path following information
674 void ai_big_subsys_path_cleanup(ai_info *aip)
675 {
676         if ( aip->ai_flags & AIF_ON_SUBSYS_PATH ) {
677                 aip->ai_flags &= ~AIF_ON_SUBSYS_PATH;
678                 aip->path_goal_dist = -1;
679                 aip->path_start = -1;
680                 aip->path_cur = -1;
681                 aip->path_length = 0;
682         }
683 }
684
685 // Maybe Pl_objp needs to follow a path to get in line-of-sight to a subsystem
686 // input:       do_dot_check    =>      default value 0, flag to indicate whether check should be done to ensure
687 //                                                                              subsystem is within certain field of view.  We don't want to check fov when
688 //                                                                              strafing, since ship is weaving to avoid turret fire
689 int ai_big_maybe_follow_subsys_path(int do_dot_check)
690 {
691         ai_info *aip;
692         float           dot = 1.0f, min_dot;
693         object  *target_objp;
694
695         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
696         target_objp = &Objects[aip->target_objnum];
697
698         if ( (aip->targeted_subsys != NULL) && (aip->target_objnum >= 0) && (aip->targeted_subsys->system_info->path_num >= 0) ) {
699                 polymodel       *pm;
700                 int                     subsys_path_num, subsys_in_sight, checked_sight;
701                 float                   dist;
702
703                 pm = model_get( Ships[Pl_objp->instance].modelnum );
704         
705                 // If attacking a subsystem, ensure that we have an unobstructed line of sight... if not, then move
706                 // towards path linked to subsystem
707                 subsys_in_sight = 0;    // assume Pl_objp doesn't have line of sight to subys
708
709                 // only check for subsystem sight every N milliseconds
710                 checked_sight = 0;
711                 if ( timestamp_elapsed(aip->path_subsystem_next_check) ) {
712                         vector          geye, gsubpos;
713                         eye                     *ep;
714
715                         aip->path_subsystem_next_check = timestamp(1500);
716
717                         // get world pos of eye (stored in geye)
718                         ep = &(pm->view_positions[0] );
719                         model_find_world_point( &geye, &ep->pnt, Ships[Pl_objp->instance].modelnum, 0, &Pl_objp->orient, &Pl_objp->pos );
720                         
721                         // get world pos of subsystem
722                         vm_vec_unrotate(&gsubpos, &aip->targeted_subsys->system_info->pnt, &En_objp->orient);
723                         vm_vec_add2(&gsubpos, &En_objp->pos);
724
725                         checked_sight = 1;
726
727                         // Calc dot between subsys normal (based on using path info), and subsys_to_eye vector.  This is
728                         // useful later when we want to decide whether we have a realistic line-of-sight to the subsystem.
729                         vector subsys_normal, subsys_to_eye;
730                         if ( do_dot_check ) {
731                                 if ( !ship_return_subsys_path_normal(&Ships[target_objp->instance], aip->targeted_subsys, &gsubpos, &subsys_normal) ) {
732                                         vm_vec_normalized_dir(&subsys_to_eye, &geye, &gsubpos);
733                                         dot = vm_vec_dot(&subsys_normal, &subsys_to_eye);
734                                 }
735                         }
736
737                         if ( ship_subsystem_in_sight(En_objp, aip->targeted_subsys, &geye, &gsubpos, 1) ) {
738                                 subsys_in_sight = 1;
739                         }
740                 }
741
742                 // check if subsystem not in sight
743                 min_dot = (target_objp->phys_info.fspeed > 5.0f?MIN_DOT_TO_ATTACK_MOVING_SUBSYS:MIN_DOT_TO_ATTACK_SUBSYS);
744                 if ( (checked_sight && ((!subsys_in_sight) || (dot < min_dot)) ) )  {
745
746                         aip->path_goal_dist = 5;
747                         subsys_path_num = aip->targeted_subsys->system_info->path_num;
748                         if ( (aip->path_start) == -1 || (aip->mp_index != subsys_path_num) ) {
749                                 // maybe create a new path
750                                 if ( subsys_path_num >= 0 ) {
751                                         Assert(aip->target_objnum >= 0);
752                                         ai_find_path(Pl_objp, aip->target_objnum, subsys_path_num, 0, 1);
753                                         if ( aip->path_start >= 0 ) {
754                                                 aip->ai_flags |= AIF_ON_SUBSYS_PATH;
755                                         }
756                                 }
757                         }
758                 }
759
760                 if ( checked_sight && subsys_in_sight && (dot > min_dot) ) {
761                         // we've got a clear shot, stay here for a bit
762                         aip->path_subsystem_next_check = timestamp_rand(5000,8000);
763                 }
764
765                 // If there is a path that we are tracking, see if ship has gotten to within
766                 // aip->path_goal_dist units.  If so, then ship can stop following the path.  This
767                 // is required since we don't want to follow the path all the way to the subsystem,
768                 // and we want ships to stop different distances from their path destination
769                 if ( aip->path_length > 0 ) {
770                         int path_done=0;
771                         int in_view=0;
772
773                         Assert(aip->path_length >= 2);
774                         dist = vm_vec_dist_quick(&Path_points[aip->path_start+aip->path_length-2].pos, &Pl_objp->pos);
775
776                         if ( aip->path_cur >= (aip->path_start+aip->path_length-1) ) {
777                                 path_done=1;
778                         }
779
780                         min_dot = (target_objp->phys_info.fspeed > 5.0f?MIN_DOT_TO_ATTACK_MOVING_SUBSYS:MIN_DOT_TO_ATTACK_SUBSYS);
781                         if ( (checked_sight && subsys_in_sight) && (dot > min_dot) ) {
782                                 in_view=1;
783                         }
784
785                         // if we've reached the destination, then we can stop following the path
786                         if ( path_done || ( dist < aip->path_goal_dist ) || in_view ) {
787                                 ai_big_subsys_path_cleanup(aip);
788                         } else if ( dist > aip->path_goal_dist ) {
789                                 // If we have a path to follow, follow it and return                    
790                                 if ( aip->path_start != -1 ) {
791                                         // for now, only follow the path to the first point
792                                         if ( aip->path_cur < (aip->path_start+aip->path_length-1) ) {
793                                                 if ( aip->goal_objnum != aip->target_objnum ) {
794                                                         //Int3();       // what is going on here? - Get Alan
795                                                         aip->previous_mode = aip->mode;
796                                                         aip->mode = AIM_NONE;
797                                                         aip->submode = -1;
798                                                         return 1;
799                                                 }
800                                                 ai_path();
801                                                 return 1;
802                                         }
803                                 }
804                         }
805                 }
806         }
807
808         return 0;
809 }
810
811 // This function is only called from ai_big_chase_attack() when a ship is flying very slowly and
812 // attacking a big ship.  The ship should scan for enemy fighter/bombers... if any are close, then
813 // return 1, otherwise return 0;
814 //
815 // input: aip   =>      ai_info pointer for Pl_objp
816 //                       sip    =>      ship_info pointer for Pl_objp
817 //
818 // exit:                1       =>      ship should enter strafe mode
819 //                              0       => ship should not change ai mode, no fighter/bomber threats are near
820 //
821 // NOTE: uses SCAN_FIGHTERS_INTERVAL and ENTER_STRAFE_THREAT_DIST_SQUARED which are defined in AiBig.h
822 int ai_big_maybe_start_strafe(ai_info *aip, ship_info *sip)
823 {
824         // if moving slowly (or stopped), and SIF_SMALL_SHIP, then enter STRAFE mode if enemy fighter/bombers
825         // are near
826         if ( sip->flags & SIF_SMALL_SHIP ) {
827                 if ( timestamp_elapsed(aip->scan_for_enemy_timestamp) ) {
828                         ship_obj        *so;
829                         object  *test_objp;
830                         ship            *test_sp;
831                         float           dist_squared;
832                         
833                         aip->scan_for_enemy_timestamp = timestamp(SCAN_FIGHTERS_INTERVAL);
834                         // iterate through ships, and see if any fighter/bomber from opposite team are near
835                         so = GET_FIRST(&Ship_obj_list);
836                         while( so != END_OF_LIST(&Ship_obj_list) ) {
837                                 test_objp = &Objects[so->objnum];
838                                 test_sp = &Ships[test_objp->instance];
839
840                                 if ( test_sp->team != Ships[Pl_objp->instance].team ) {
841                                         if ( Ship_info[test_sp->ship_info_index].flags & SIF_SMALL_SHIP ) {
842                                                 dist_squared = vm_vec_dist_squared(&Pl_objp->pos, &test_objp->pos);
843                                                 if ( dist_squared < ENTER_STRAFE_THREAT_DIST_SQUARED ) {
844                                                         return 1;
845                                                 }
846                                         }
847                                 }
848                                 so = GET_NEXT(so);
849                         }
850                 }
851         }
852         // If we've reached here, there are no enemy fighter/bombers near
853         return 0;
854 }
855
856 //      ATTACK submode handler for chase mode.
857 void ai_big_chase_attack(ai_info *aip, ship_info *sip, vector *enemy_pos, float dist_to_enemy)
858 {
859         int             start_bank;
860         float           dot_to_enemy, time_to_hit;
861         polymodel *po = model_get( sip->modelnum );
862
863         //      Maybe evade an incoming weapon.
864         if (((time_to_hit = ai_endangered_by_weapon(aip)) < 4.0f) && (time_to_hit >= 0.0f)) {
865                 aip->submode = SM_EVADE_WEAPON;
866                 aip->submode_start_time = Missiontime;
867                 aip->prev_goal_point = En_objp->pos;
868         } else {
869                 //      If moving slowly, maybe evade incoming fire.
870                 if (Pl_objp->phys_info.speed < 3.0f) {
871                         object *objp;
872                         for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
873                                 if ((objp->type == OBJ_WEAPON) && (Weapons[objp->instance].team != Ships[Pl_objp->instance].team))
874                                         if (Weapon_info[objp->instance].subtype == WP_LASER) {
875                                                 vector  in_vec;
876                                                 float           dist;
877
878                                                 vm_vec_sub(&in_vec, &objp->pos, &Pl_objp->pos);
879                                                 if (vm_vec_dot(&in_vec, &objp->orient.fvec) > 0.0f) {
880                                                         dist = vm_vec_normalize(&in_vec);
881                                                         if ((dist < 200.0f) && (vm_vec_dot(&in_vec, &objp->orient.fvec) > 0.95f)) {
882                                                                 if ((Objects[objp->parent].signature == objp->parent_sig) && (vm_vec_dist_quick(&objp->pos, &Objects[objp->parent].pos) < 300.0f)) {
883                                                                         set_target_objnum(aip, objp->parent);
884                                                                         aip->submode = SM_ATTACK;
885                                                                         aip->submode_start_time = Missiontime;
886                                                                 } else {
887                                                                         aip->submode = SM_EVADE;
888                                                                         aip->submode_start_time = Missiontime;
889                                                                         aip->prev_goal_point = En_objp->pos;
890                                                                 }
891                                                         }
892                                                 }
893                                         }
894                         }
895                         
896                         // Since ship is moving slowly and attacking a large ship, scan if enemy fighters are near, if so
897                         // then enter strafe mode
898                         if ( ai_big_maybe_start_strafe(aip, sip) ) {
899                                 aip->previous_mode = aip->mode;
900                                 aip->mode = AIM_STRAFE;
901                                 aip->submode_parm0 = Missiontime;       // use parm0 as time strafe mode entered
902                                 aip->submode = AIS_STRAFE_ATTACK;
903                                 aip->submode_start_time = Missiontime;
904                                 return;
905                         }
906
907                 } // end if ( Pl_objp->phys_info.speed < 3.0f ) 
908
909                 // see if Pl_objp needs to reposition to get a good shot at subsystem which is being attacked
910                 if ( ai_big_maybe_follow_subsys_path() ) {
911                         return;
912                 }
913
914                 vector  *rel_pos, vec_to_enemy;
915                 float           weapon_travel_dist;
916
917                 start_bank = Ships[aip->shipnum].weapons.current_primary_bank;
918
919                 if ((po->n_guns) && (start_bank != -1)) {
920                         rel_pos = &po->gun_banks[start_bank].pnt[0];
921                 } else
922                         rel_pos = NULL;
923
924                 dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, enemy_pos, &Pl_objp->pos);
925                 dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
926
927                 vector  rvec_vec, *rvec = &rvec_vec;
928
929                 if (dist_to_enemy > 500.0f)
930                         compute_desired_rvec(rvec, enemy_pos, &Pl_objp->pos);
931                 else
932                         rvec = NULL;
933
934                 ai_turn_towards_vector(enemy_pos, Pl_objp, flFrametime, sip->srotation_time, NULL, rel_pos, 0.0f, 0, rvec);
935
936                 // calc range of primary weapon
937                 weapon_travel_dist = ai_get_weapon_dist(&Ships[Pl_objp->instance].weapons);
938
939                 if ( aip->targeted_subsys != NULL ) {
940                         if (dist_to_enemy > (weapon_travel_dist-20))
941                                 accelerate_ship(aip, 1.0f);
942                         else {
943                                 // AL 12-31-97: Move at least as quickly as your target is moving...
944                                 accelerate_ship(aip, max(1.0f - dot_to_enemy, Objects[aip->target_objnum].phys_info.fspeed/sip->max_speed));
945                         }
946
947                 } else {
948                         float accel;
949                         if (dot_to_enemy < 0.0f) {
950                                 accel = 0.5f;
951                         } else if (dot_to_enemy < 0.75f) {
952                                 accel = 0.75f;
953                         } else {
954                                 if (dist_to_enemy > weapon_travel_dist/2.0f) {
955                                         accel = 1.0f;
956                                 } else {
957                                         accel = 1.0f - dot_to_enemy;
958                                 }
959                         }
960
961                         // use dist normal to enemy here (dont break 50 barrier)
962                         if (dist_to_enemy < ATTACK_STOP_DISTANCE) {
963 //                              accelerate_ship(aip, accel * 0.5f);
964                                 accelerate_ship(aip, -1.0f);
965                         } else {
966                                 accelerate_ship(aip, accel);
967                         }
968
969                 }
970         }
971 }
972
973 // Handler for submode SM_CONTINUOUS_TURN
974 void ai_big_chase_ct()
975 {
976         ai_chase_ct();
977 }
978
979 extern void ai_select_secondary_weapon(object *objp, ship_weapon *swp, int priority1 = -1, int priority2 = -1);
980 extern float set_secondary_fire_delay(ai_info *aip, ship *shipp, weapon_info *swip);
981 extern void ai_choose_secondary_weapon(object *objp, ai_info *aip, object *en_objp);
982 extern int maybe_avoid_big_ship(object *objp, object *ignore_objp, ai_info *aip, vector *goal_point, float delta_time);
983
984 extern void maybe_cheat_fire_synaptic(object *objp, ai_info *aip);
985
986 // Determine if Pl_objp should fire weapons at current target, based on input parameters
987 //
988 // dist_to_enemy        =>              distance (in m) to attack point on current target
989 // dot_to_enemy =>              dot product between fvec of Pl_objp and vector from Pl_objp to attack point
990 //
991 void ai_big_maybe_fire_weapons(float dist_to_enemy, float dot_to_enemy, vector *firing_pos, vector *enemy_pos, vector *enemy_vel)
992 {
993         ai_info         *aip;
994         ship_weapon     *swp;
995
996         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
997         swp = &Ships[Pl_objp->instance].weapons;
998
999         if (dot_to_enemy > 0.95f - 0.5f * En_objp->radius/max(1.0f, En_objp->radius + dist_to_enemy)) {
1000                 aip->time_enemy_in_range += flFrametime;
1001                 
1002                 //      Chance of hitting ship is based on dot product of firing ship's forward vector with vector to ship
1003                 //      and also the size of the target relative to distance to target.
1004                 if (dot_to_enemy > max(0.5f, 0.90f + aip->ai_accuracy/10.0f - En_objp->radius/max(1.0f,dist_to_enemy))) {
1005
1006                         ship *temp_shipp;
1007                         temp_shipp = &Ships[Pl_objp->instance];
1008                         ship_weapon *tswp = &temp_shipp->weapons;
1009
1010                         if ( tswp->num_primary_banks > 0 ) {
1011                                 Assert(tswp->current_primary_bank < tswp->num_primary_banks);
1012                                 weapon_info     *wip = &Weapon_info[tswp->primary_bank_weapons[tswp->current_primary_bank]];
1013
1014                                 if (dist_to_enemy < wip->max_speed * wip->lifetime)
1015                                         ai_fire_primary_weapon(Pl_objp);
1016
1017                                 int     priority1, priority2;
1018
1019                                 priority1 = -1;
1020                                 priority2 = -1;
1021
1022                                 //      Maybe favor selecting a bomb.
1023                                 //      Note, if you're firing a bomb, if it's aspect seeking, the firing conditions can be looser.
1024                                 if (Ship_info[Ships[En_objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))
1025                                         if (En_objp->phys_info.speed * dist_to_enemy < 5000.0f)         //      Don't select a bomb if enemy moving fast relative to distance
1026                                                 priority1 = WIF_BOMB;
1027
1028                                 if (!(En_objp->flags & OF_PROTECTED)) {
1029                                         //ai_select_secondary_weapon(Pl_objp, tswp, priority1, priority2);      //      Note, need to select to get weapon speed and lifetime
1030
1031                                         ai_choose_secondary_weapon(Pl_objp, aip, En_objp);
1032                                         int current_bank = tswp->current_secondary_bank;
1033                                         weapon_info     *swip = &Weapon_info[tswp->secondary_bank_weapons[current_bank]];
1034
1035                                         //      If ship is protected and very low on hits, don't fire missiles.
1036                                         if ((current_bank > -1) &&  (!(En_objp->flags & OF_PROTECTED) || (En_objp->hull_strength > 10*swip->damage))) {
1037                                                 if (aip->ai_flags & AIF_UNLOAD_SECONDARIES) {
1038                                                         if (timestamp_until(swp->next_secondary_fire_stamp[current_bank]) > swip->fire_wait*1000.0f) {
1039                                                                 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (swip->fire_wait*1000.0f));
1040                                                         }
1041                                                 }
1042
1043                                                 if (timestamp_elapsed(swp->next_secondary_fire_stamp[current_bank])) {
1044
1045                                                         float firing_range = swip->max_speed * swip->lifetime;
1046                                                         // reduce firing range of secondaries in nebula
1047                                                         extern int Nebula_sec_range;
1048                                                         if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
1049                                                                 firing_range *= 0.8f;
1050                                                         }
1051
1052                                                         float t = 0.25f;        //      default delay in seconds until next fire.
1053
1054                                                         if (dist_to_enemy < firing_range*1.0f) {
1055
1056
1057                                                                 //vm_vec_scale_add(&future_enemy_pos, enemy_pos, enemy_vel, dist_to_enemy/swip->max_speed);
1058                                                                 //if (vm_vec_dist_quick(&future_enemy_pos, firing_pos) < firing_range * 0.8f) {
1059                                                                         if (ai_fire_secondary_weapon(Pl_objp)) {
1060                                                                                 if (aip->ai_flags & AIF_UNLOAD_SECONDARIES) {
1061                                                                                         t = swip->fire_wait;
1062                                                                                 } else {
1063                                                                                         t = set_secondary_fire_delay(aip, temp_shipp, swip);
1064                                                                                 }
1065
1066                                                                                 swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1067                                                                         }
1068                                                                 //}
1069                                                         }
1070                                                         swp->next_secondary_fire_stamp[current_bank] = timestamp((int) (t*1000.0f));
1071                                                 }
1072                                         }
1073                                 }
1074                         }
1075                 }
1076         } else {
1077                 aip->time_enemy_in_range *= (1.0f - flFrametime);
1078         }
1079 }
1080
1081 // switch ai ship into chase mode
1082 void ai_big_switch_to_chase_mode(ai_info *aip)
1083 {
1084         aip->previous_mode = aip->mode;
1085         aip->mode = AIM_CHASE;
1086         aip->submode = SM_ATTACK;
1087         aip->submode_start_time = Missiontime;
1088 }
1089
1090 extern int ai_big_strafe_maybe_retreat(float dist, vector *target_pos);
1091
1092 // Make object Pl_objp chase object En_objp, which is a big ship, not a small ship.
1093 void ai_big_chase()
1094 {
1095         float                   dist_to_enemy, dot_to_enemy;
1096         vector          player_pos, enemy_pos, vec_to_enemy;
1097         ship_info       *sip = &Ship_info[Ships[Pl_objp->instance].ship_info_index];
1098         ship                    *shipp = &Ships[Pl_objp->instance];     
1099         ai_info         *aip = &Ai_info[shipp->ai_index];
1100         int                     enemy_ship_type;
1101         vector          predicted_enemy_pos;
1102
1103         Assert(aip->mode == AIM_CHASE);
1104
1105         maybe_cheat_fire_synaptic(Pl_objp, aip);
1106
1107         enemy_ship_type = Ship_info[Ships[En_objp->instance].ship_info_index].flags;
1108
1109         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, &enemy_pos);
1110
1111         player_pos = Pl_objp->pos;
1112         ai_big_pick_attack_point(En_objp, Pl_objp, &enemy_pos, 0.8f);
1113
1114         //      Compute the predicted position of the center of the ship, then add the delta to the goal pos.
1115         if (En_objp->phys_info.speed > 3.0f) {
1116                 set_predicted_enemy_pos(&predicted_enemy_pos, Pl_objp, En_objp, aip);
1117                 vm_vec_add2(&enemy_pos, &predicted_enemy_pos);
1118                 vm_vec_sub2(&enemy_pos, &En_objp->pos);
1119         }       else
1120                 predicted_enemy_pos = En_objp->pos;
1121
1122         if (aip->targeted_subsys != NULL) {
1123                 get_subsystem_pos(&enemy_pos, En_objp, aip->targeted_subsys);
1124         }
1125
1126         dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &enemy_pos, &player_pos); // - En_objp->radius;
1127         dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1128
1129         if (aip->ai_flags & AIF_TARGET_COLLISION) {
1130                 if ( ai_big_strafe_maybe_retreat(dist_to_enemy, &enemy_pos) ) {
1131                         aip->mode = AIM_STRAFE;
1132                         aip->submode = AIS_STRAFE_AVOID;
1133                         aip->submode_parm0 = Missiontime;       // use parm0 as time strafe mode entered
1134                         return;
1135                 }
1136         }
1137
1138         if (aip->ai_flags & AIF_KAMIKAZE) {
1139                 //nprintf(("AI", "Kamikaze: %7.3f %7.3f\n", dot_to_enemy, dist_to_enemy));
1140                 accelerate_ship(aip, 1.0f);
1141                 if ((dist_to_enemy < 400.0f) && ai_maybe_fire_afterburner(Pl_objp, aip)) {
1142                         afterburners_start(Pl_objp);
1143                         aip->afterburner_stop_time = Missiontime + 3*F1_0;
1144                 }
1145         }
1146
1147         //      If just acquired target, or target is not in reasonable cone, don't refine believed enemy position.
1148         if ((dot_to_enemy < 0.25f) || (aip->target_time < 1.0f) || (aip->ai_flags & AIF_SEEK_LOCK)) {
1149                 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1150         } else if (aip->targeted_subsys != NULL) {              
1151                 Assert(aip->targeted_subsys != NULL);
1152                 get_subsystem_pos(&enemy_pos, En_objp, aip->targeted_subsys);
1153                 vm_vec_add2(&enemy_pos, &predicted_enemy_pos);
1154                 vm_vec_sub2(&enemy_pos, &En_objp->pos);
1155                 dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &enemy_pos, &player_pos); // - En_objp->radius;
1156                 dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1157                 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy, En_objp->radius);
1158         } else if (En_objp->flags & OF_PROTECTED) {     //      If protected and we're not attacking a subsystem, stop attacking!
1159                 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1160                 aip->target_objnum = -1;
1161                 if (find_enemy(Pl_objp-Objects, MAX_ENEMY_DISTANCE, Skill_level_max_attackers[Game_skill_level]) == -1) {
1162                         ai_do_default_behavior(Pl_objp);
1163                         return;
1164                 }
1165         } else {
1166                 update_aspect_lock_information(aip, &vec_to_enemy, dist_to_enemy - En_objp->radius, En_objp->radius);
1167         }
1168
1169         //      If recently acquired target and not looking at target, just turn a bit.
1170         switch (aip->submode) {
1171         case SM_ATTACK:
1172         case SM_SUPER_ATTACK:
1173                 if (vm_vec_dist_quick(&Pl_objp->pos, &predicted_enemy_pos) > 100.0f + En_objp->radius * 2.0f) {
1174                         if (maybe_avoid_big_ship(Pl_objp, En_objp, aip, &predicted_enemy_pos, 10.0f))
1175                                 return;
1176                 }
1177
1178                 if (aip->target_time < 2.0f)
1179                         if ((dot_to_enemy < 0.9f) || (dist_to_enemy > 300.0f)) {
1180                                 aip->submode = SM_CONTINUOUS_TURN;
1181                                 aip->submode_start_time = Missiontime - fl2f(2.75f);    //      This backdated start time allows immediate switchout.
1182                                 if (dot_to_enemy > 0.0f)
1183                                         aip->target_time += flFrametime * dot_to_enemy;
1184                         }
1185                 break;
1186         }
1187
1188         //
1189         //      Set turn and acceleration based on submode.
1190         //
1191         switch (aip->submode) {
1192         case SM_CONTINUOUS_TURN:
1193                 ai_big_chase_ct();
1194                 break;
1195
1196         case SM_ATTACK:
1197         case SM_SUPER_ATTACK:
1198         case SM_ATTACK_FOREVER:
1199                 ai_big_chase_attack(aip, sip, &enemy_pos, dist_to_enemy);
1200                 break;
1201
1202         case SM_EVADE:
1203                 ai_big_evade_ship();
1204                 break;
1205
1206         case SM_AVOID:
1207                 ai_big_avoid_ship();
1208                 break;
1209
1210         case SM_EVADE_WEAPON:
1211                 evade_weapon();
1212                 break;
1213
1214         default:
1215                 aip->last_attack_time = Missiontime;
1216                 aip->submode = SM_ATTACK;
1217                 break;
1218         }
1219
1220         // maybe Pl_objp was forced into strafe mode, if so return now
1221         if ( aip->mode == AIM_STRAFE )
1222                 return;
1223
1224         //      Maybe choose a new submode.
1225         if (aip->submode != SM_AVOID && aip->submode != SM_EVADE ) {
1226                 //      If a very long time since attacked, attack no matter what!
1227                 if (aip->submode != SM_SUPER_ATTACK) {
1228                         if (Missiontime - aip->last_attack_time > i2f(6)) {
1229                                 aip->submode = SM_SUPER_ATTACK;
1230                                 aip->submode_start_time = Missiontime;
1231                                 aip->last_attack_time = Missiontime;
1232                         }
1233                 }
1234
1235                 //      If a collision is expected, pull out!
1236                 float dist_normal_to_enemy;
1237
1238                 if (vm_vec_mag_squared(&aip->big_attack_surface_normal) > 0.9) {
1239                         dist_normal_to_enemy = fl_abs((dist_to_enemy * vm_vec_dot(&vec_to_enemy, &aip->big_attack_surface_normal)));
1240                 } else {
1241                         // don;t have normal so use a conservative value here
1242                         dist_normal_to_enemy = 0.3f * dist_to_enemy;
1243                 }
1244
1245 //              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));
1246 //              if (Framecount % 30 == 1) {
1247 //                      mprintf(("normal dist; %.1f, time: %.1f\n", dist_normal_to_enemy, time_to_enemy));
1248 //              }
1249                 
1250                 // 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)
1251                 // this will allow us to get closer on a bad normal
1252                 dist_normal_to_enemy = max(0.3f*dist_to_enemy, dist_normal_to_enemy);
1253
1254                 if (dist_to_enemy < ATTACK_COLLIDE_BASE_DIST) {
1255                         // within 50m or 1sec
1256                         float time_to_enemy;
1257                         if (vm_vec_mag_squared(&aip->big_attack_surface_normal) > 0.9) {
1258                                 if (Pl_objp->phys_info.speed > 0.1) {
1259                                         time_to_enemy = dist_normal_to_enemy / fl_abs(vm_vec_dot(&Pl_objp->phys_info.vel, &aip->big_attack_surface_normal));
1260                                 } else {
1261                                         // a big time
1262                                         time_to_enemy = 100.0f;
1263                                 }
1264                         } else {
1265                                 if (Pl_objp->phys_info.speed > 0.1) {
1266                                         time_to_enemy = dist_normal_to_enemy / Pl_objp->phys_info.speed;
1267                                 } else {
1268                                         // a big time
1269                                         time_to_enemy = 100.0f;
1270                                 }
1271                         }
1272
1273                         float speed_dist = max(0.0f, (Pl_objp->phys_info.speed-50) * 2);
1274                         if ((dist_normal_to_enemy < ATTACK_COLLIDE_AVOID_DIST + speed_dist) || (time_to_enemy < ATTACK_COLLIDE_AVOID_TIME) ) {
1275                                 // get away, simulate crsh recovery (don't use avoid)
1276 //                              accelerate_ship(aip, -1.0f);
1277                                 big_ship_collide_recover_start(Pl_objp, En_objp, &Pl_objp->pos, NULL);
1278 //                              aip->submode = SM_AVOID;
1279 //                              aip->submode_start_time = Missiontime;
1280                         } else if ((dist_normal_to_enemy < ATTACK_COLLIDE_SLOW_DIST) || (time_to_enemy < ATTACK_COLLIDE_SLOW_TIME) ) {
1281                                 // slow down
1282                                 accelerate_ship(aip, -1.0f);
1283                         }
1284                 }
1285
1286                 /*
1287                 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)) {
1288                         if (might_collide_with_ship(Pl_objp, En_objp, dot_to_enemy, dist_to_enemy, (aip->targeted_subsys == NULL)*2.0f + 1.5f)) {
1289                                 if ((Missiontime - aip->last_hit_time > F1_0*4) && (dist_to_enemy < Pl_objp->radius*2 + En_objp->radius*2)) {
1290                                         accelerate_ship(aip, -1.0f);
1291                                 } else {
1292                                         aip->submode = SM_AVOID;
1293                                         aip->submode_start_time = Missiontime;
1294                                 }
1295                         }
1296                 } */
1297         }
1298
1299         switch (aip->submode) {
1300         case SM_CONTINUOUS_TURN:
1301                 if (Missiontime - aip->submode_start_time > i2f(3)) {
1302                         aip->last_attack_time = Missiontime;
1303                         aip->submode = SM_ATTACK;
1304                         aip->submode_start_time = Missiontime;
1305                 }
1306                 break;
1307
1308         case SM_ATTACK:
1309         case SM_ATTACK_FOREVER:
1310                 break;
1311                 
1312         case SM_EVADE:
1313                 if (dist_to_enemy > 4*En_objp->radius) {
1314                         aip->submode = SM_ATTACK;
1315                         aip->submode_start_time = Missiontime;
1316                         aip->last_attack_time = Missiontime;
1317                 }
1318                 break;
1319
1320         case SM_SUPER_ATTACK:
1321                 break;
1322
1323         case SM_AVOID:
1324                 // if outside box by > 300 m
1325                 // DA 4/20/98  can never get here attacking a big ship
1326                 {
1327                 int is_inside;
1328                 float box_dist = get_world_closest_box_point_with_delta(NULL, En_objp, &Pl_objp->pos, &is_inside, 0.0f);
1329                 
1330                 if (box_dist > 300) {
1331                         aip->submode = SM_ATTACK;
1332                         aip->last_attack_time = Missiontime;
1333                         aip->submode_start_time = Missiontime;
1334                 }
1335                 }
1336                 break;
1337
1338 /*              if (dot_to_enemy > -0.2f) {
1339                         aip->submode_start_time = Missiontime;
1340                 } else if (Missiontime - aip->submode_start_time > i2f(1)/2) {
1341                         if (might_collide_with_ship(Pl_objp, En_objp, dot_to_enemy, dist_to_enemy, 3.0f)) {
1342                                 aip->submode_start_time = Missiontime;
1343                         } else {
1344                                 aip->submode = SM_ATTACK;
1345                                 aip->last_attack_time = Missiontime;
1346                                 aip->submode_start_time = Missiontime;
1347                         }
1348                 }
1349
1350                 break;*/
1351
1352         case SM_EVADE_WEAPON:
1353                 if (aip->danger_weapon_objnum == -1) {
1354                         aip->submode = SM_ATTACK;
1355                         aip->submode_start_time = Missiontime;
1356                         aip->last_attack_time = Missiontime;
1357                 }
1358                 break;
1359
1360         default:
1361                 aip->submode = SM_ATTACK;
1362                 aip->last_attack_time = Missiontime;
1363                 aip->submode_start_time = Missiontime;
1364         }
1365
1366         //
1367         //      Maybe fire primary weapon and update time_enemy_in_range
1368         //
1369         //nprintf(("AI", "time_enemy_in_range = %7.3f, dot = %7.3f\n", aip->time_enemy_in_range, dot_to_enemy));
1370
1371         // AL: add condition that Pl_objp must not be following a path to fire.  This may be too extreme, but
1372         //     I noticed AI ships firing inappropriately when following a path near a big ship.
1373         //               TODO: investigate why ships fire (and aren't close to hitting ship) when following a path near
1374         //                               a big ship
1375         if (aip->mode != AIM_EVADE && aip->path_start == -1 ) {
1376                 ai_big_maybe_fire_weapons(dist_to_enemy, dot_to_enemy, &player_pos, &predicted_enemy_pos, &En_objp->phys_info.vel);
1377         } else
1378                 aip->time_enemy_in_range *= (1.0f - flFrametime);
1379 }
1380
1381 void ai_big_ship(object *objp)
1382 {
1383         // do nothing
1384 }
1385
1386 // all three parms are output parameters
1387 //      Enemy object is En_objp
1388 // pos  =>              world pos of attack point
1389 // dist =>              distance from Pl_objp front to attack point
1390 // dot  =>              dot of Pl_objp fvec and vector to attack point
1391 //      fire_pos =>     world pos from which firing
1392 void ai_big_attack_get_data(vector *enemy_pos, float *dist_to_enemy, float *dot_to_enemy)
1393 {
1394         vector          player_pos, vec_to_enemy, predicted_enemy_pos;  
1395         ship                    *shipp = &Ships[Pl_objp->instance];     
1396         ai_info         *aip = &Ai_info[shipp->ai_index];
1397         ship_info       *esip = &Ship_info[Ships[En_objp->instance].ship_info_index];
1398
1399         Assert(aip->mode == AIM_STRAFE);
1400
1401         // ensure that Pl_objp is still targeting a big ship
1402         if ( !(esip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
1403                 ai_big_switch_to_chase_mode(aip);
1404                 return;
1405         }
1406
1407         ai_set_positions(Pl_objp, En_objp, aip, &player_pos, enemy_pos);
1408
1409         player_pos = Pl_objp->pos;
1410
1411         if (aip->targeted_subsys != NULL) {
1412                 Assert(aip->targeted_subsys != NULL);
1413                 get_subsystem_pos(enemy_pos, En_objp, aip->targeted_subsys);
1414         } else {
1415                 // checks valid line to target
1416                 ai_big_pick_attack_point(En_objp, Pl_objp, enemy_pos, 0.8f);
1417         }
1418
1419         // Take player pos to be center of ship + ship_radius
1420         vm_vec_scale_add2(&player_pos, &Pl_objp->orient.fvec, Pl_objp->radius); 
1421
1422         //      If seeking lock, try to point directly at ship, else predict position so lasers can hit it.
1423         //      If just acquired target, or target is not in reasonable cone, don't refine believed enemy position.
1424         vm_vec_normalized_dir(&vec_to_enemy, enemy_pos, &player_pos);
1425         *dot_to_enemy=vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1426         if ((*dot_to_enemy < 0.25f) || (aip->target_time < 1.0f) || (aip->ai_flags & AIF_SEEK_LOCK)) {
1427                 predicted_enemy_pos=*enemy_pos;
1428         } else {
1429                 vector  gun_pos, pnt;
1430                 polymodel *po = model_get( Ship_info[shipp->ship_info_index].modelnum );
1431                 float           weapon_speed;
1432
1433                 //      Compute position of gun in absolute space and use that as fire position.
1434                 pnt = po->gun_banks[0].pnt[0];
1435                 vm_vec_unrotate(&gun_pos, &pnt, &Pl_objp->orient);
1436                 vm_vec_add2(&gun_pos, &Pl_objp->pos);
1437                 weapon_speed = ai_get_weapon_speed(&shipp->weapons);
1438                 
1439                 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);
1440         }
1441
1442         *dist_to_enemy = vm_vec_normalized_dir(&vec_to_enemy, &predicted_enemy_pos, &player_pos);
1443         *dot_to_enemy = vm_vec_dot(&vec_to_enemy, &Pl_objp->orient.fvec);
1444         update_aspect_lock_information(aip, &vec_to_enemy, *dist_to_enemy, En_objp->radius);
1445
1446         *enemy_pos = predicted_enemy_pos;
1447 }
1448
1449 // check to see if Pl_objp has gotten too close to attacking point.. if so, break off by entering
1450 // AIS_STRAFE_RETREAT
1451 int ai_big_strafe_maybe_retreat(float dist, vector *target_pos)
1452 {
1453         ai_info *aip;
1454         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1455
1456         vector vec_to_target;
1457         vm_vec_sub(&vec_to_target, target_pos, &Pl_objp->pos);
1458
1459         float dist_to_target, dist_normal_to_target, time_to_target;
1460         dist_to_target = vm_vec_mag_quick(&vec_to_target);
1461         if (vm_vec_mag_quick(&aip->big_attack_surface_normal) > 0.9) {
1462                 dist_normal_to_target = -vm_vec_dotprod(&vec_to_target, &aip->big_attack_surface_normal);
1463         } else {
1464                 dist_normal_to_target = 0.2f * vm_vec_mag_quick(&vec_to_target);
1465         }
1466
1467         dist_normal_to_target = max(0.2f*dist_to_target, dist_normal_to_target);
1468         time_to_target = dist_normal_to_target / Pl_objp->phys_info.speed;
1469
1470         // add distance penalty for going too fast
1471         float speed_to_dist_penalty = max(0.0f, (Pl_objp->phys_info.speed-50));
1472
1473         //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))
1474
1475         // Inside 2 sec retreat, setting goal point to box point + 300m
1476         // If collision, use std collision resolution.
1477         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)) ) {
1478                 if (aip->ai_flags & AIF_TARGET_COLLISION) {
1479                         // use standard collision resolution
1480                         aip->ai_flags &= ~AIF_TARGET_COLLISION;
1481                         big_ship_collide_recover_start(Pl_objp, En_objp, &Pl_objp->pos, NULL);
1482                 } else {
1483                         // too close for comfort so fly to box point + 300
1484                         aip->submode = AIS_STRAFE_RETREAT1;
1485                         aip->submode_start_time = Missiontime;
1486
1487                         float box_dist;
1488                         int is_inside;
1489                         vector goal_point;
1490                         box_dist = get_world_closest_box_point_with_delta(&goal_point, En_objp, &Pl_objp->pos, &is_inside, STRAFE_RETREAT_BOX_DIST);
1491
1492                         // set goal point
1493                         aip->goal_point = goal_point;
1494
1495                         if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1496                                 afterburners_start(Pl_objp);
1497                                 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1498                         }
1499                 }
1500
1501                 return 1;
1502         } else {
1503                 return 0;
1504         }
1505 }
1506
1507 // attack directly to the turret and fire weapons 
1508 void ai_big_strafe_attack()
1509 {
1510         ai_info *aip;
1511         vector  target_pos;
1512         vector  rand_vec;
1513         float           target_dist, target_dot, accel, t;
1514         object  *target_objp;
1515
1516         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1517
1518         if ( ai_big_maybe_follow_subsys_path(0) ) {
1519                 return;
1520         }
1521
1522         ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1523         if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1524                 return;
1525
1526         target_objp = &Objects[aip->target_objnum];
1527
1528         if (aip->ai_flags & AIF_KAMIKAZE) {
1529                 if (target_dist < 1200.0f) {
1530                         //nprintf(("AI", "Kamikaze: %7.3f %7.3f\n", target_dot, target_dist));
1531                         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);
1532                         accelerate_ship(aip, 1.0f);
1533                         if ((target_dist < 400.0f) && ai_maybe_fire_afterburner(Pl_objp, aip)) {
1534                                 afterburners_start(Pl_objp);
1535                                 aip->afterburner_stop_time = Missiontime + 3*F1_0;
1536                         }
1537                         return;
1538                 }
1539         }
1540
1541         if (!(aip->ai_flags & AIF_SEEK_LOCK) || (aip->aspect_locked_time < 2.0f)) {
1542                 t  = ai_endangered_by_weapon(aip);
1543                 if ( t > 0.0f && t < 1.5f ) {
1544                         // set up goal_point for avoid path to turn towards
1545                         aip->goal_point = Pl_objp->pos;
1546                         switch(rand()%4) {
1547                         case 0:
1548                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, 2.0f);
1549                                 break;
1550                         case 1:
1551                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -2.0f);
1552                                 break;
1553                         case 2:
1554                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, 2.0f);
1555                                 break;
1556                         case 3:
1557                                 vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -2.0f);
1558                                 break;
1559                         } // end switch
1560
1561                         vm_vec_scale(&rand_vec, 1000.0f);
1562                         vm_vec_add2(&aip->goal_point, &rand_vec);
1563
1564                         aip->submode = AIS_STRAFE_AVOID;
1565                         aip->submode_start_time = Missiontime;
1566         //              nprintf(("Alan","Ship %s entering AIS_STRAFE_AVOID at frame %d\n", Ships[aip->shipnum].ship_name, Framecount));
1567
1568                         if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1569                                 afterburners_start(Pl_objp);
1570                                 aip->afterburner_stop_time = Missiontime + fl2f(0.5f);
1571                         }
1572                 }
1573         }
1574
1575         // maybe fire weapons at target
1576         ai_big_maybe_fire_weapons(target_dist, target_dot, &Pl_objp->pos, &En_objp->pos, &En_objp->phys_info.vel);
1577         turn_towards_point(Pl_objp, &target_pos, NULL, 0.0f);
1578
1579         // Slow down if we've not been hit for a while
1580         fix last_hit = Missiontime - aip->last_hit_time;
1581         if ( target_dist > 1200 || last_hit < F1_0*6) {
1582                 accel = 1.0f;
1583         } else {
1584                 float attack_time;
1585                 attack_time = f2fl(Missiontime - aip->submode_start_time);
1586                 if ( attack_time > 15 ) {
1587                         accel = 0.2f;
1588                 } else if ( attack_time > 10 ) {
1589                         accel = 0.4f;
1590                 } else if ( attack_time > 8 ) {
1591                         accel = 0.6f;
1592                 } else if ( attack_time > 5 ) {
1593                         accel = 0.8f;
1594                 } else {
1595                         accel = 1.0f;
1596                 }
1597         }
1598
1599         accel = 1.0f;
1600         accelerate_ship(aip, accel);
1601
1602         // if haven't been hit in quite a while, leave strafe mode
1603         fix long_enough;
1604         long_enough = F1_0*20;
1605         if ( (last_hit > long_enough) && ( (Missiontime - aip->submode_parm0) > long_enough) ) {
1606                 ai_big_switch_to_chase_mode(aip);
1607         }
1608 }
1609
1610 // pick a new attack point when entering this state, and keep using it
1611 void ai_big_strafe_avoid()
1612 {
1613         ai_info *aip;
1614         vector  target_pos;
1615         float           target_dist, target_dot;
1616         fix             mode_time;
1617
1618         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1619
1620         mode_time = Missiontime - aip->submode_start_time;
1621
1622         ai_big_attack_get_data(&target_pos, &target_dist, &target_dot);
1623         if ( ai_big_strafe_maybe_retreat(target_dist, &target_pos) )
1624                 return;
1625
1626         if ( mode_time > fl2f(0.5)) {
1627                 aip->submode = AIS_STRAFE_ATTACK;
1628                 aip->submode_start_time = Missiontime;
1629 //              nprintf(("Alan","Ship %s entering AIS_STRAFE_ATTACK at frame %d\n", Ships[aip->shipnum].ship_name, Framecount));
1630         }
1631
1632         turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1633         accelerate_ship(aip, 1.0f);
1634 }
1635
1636 // move towards aip->goal_point in an evasive manner
1637 void ai_big_strafe_retreat1()
1638 {
1639         float dist;
1640         ai_info *aip;
1641         vector  rand_vec;
1642
1643         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1644
1645         dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1646         if ( dist < 70 ) {
1647                 aip->submode = AIS_STRAFE_POSITION;
1648                 aip->submode_start_time = Missiontime;
1649 //              nprintf(("Alan","Ship %s entering AIS_STRAFE_POSITION\n", Ships[aip->shipnum].ship_name));
1650                 return;
1651         }
1652
1653         if (Missiontime - aip->submode_start_time > fl2f(1.50f)) {
1654                 // set up goal_point for avoid path to turn towards
1655                 aip->prev_goal_point = Pl_objp->pos;
1656                 switch(rand()%4) {
1657                 case 0:
1658                         vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec);
1659                         break;
1660                 case 1:
1661                         vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.uvec, -1.0f);
1662                         break;
1663                 case 2:
1664                         vm_vec_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec);
1665                         break;
1666                 case 3:
1667                         vm_vec_scale_add(&rand_vec, &Pl_objp->orient.fvec, &Pl_objp->orient.rvec, -1.0f);
1668                         break;
1669                 } // end switch
1670
1671                 //vm_vec_scale(&rand_vec, 200.0f);
1672                 //vm_vec_add2(&aip->prev_goal_point, &rand_vec);
1673                 vm_vec_scale_add(&aip->prev_goal_point, &aip->goal_point, &rand_vec, 200.0f);
1674
1675                 aip->submode = AIS_STRAFE_RETREAT2;
1676                 aip->submode_start_time = Missiontime;
1677         }
1678
1679         turn_towards_point(Pl_objp, &aip->goal_point, NULL, 0.0f);
1680         accelerate_ship(aip, 1.0f);
1681 }
1682
1683 void ai_big_strafe_retreat2()
1684 {
1685         float dist;
1686         ai_info *aip;
1687
1688         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1689
1690         if (Pl_objp->phys_info.flags & PF_AFTERBURNER_ON ) {
1691                 if (Missiontime > aip->afterburner_stop_time) {
1692                         //nprintf(("AI", "Frame %i, turning off afterburner.\n", AI_FrameCount));
1693                         afterburners_stop(Pl_objp);
1694                 }
1695         }
1696
1697         dist = vm_vec_dist_quick(&Pl_objp->pos, &aip->goal_point);
1698         if ( dist < 70 ) {
1699                 aip->submode = AIS_STRAFE_POSITION;
1700                 aip->submode_start_time = Missiontime;
1701 //              nprintf(("Alan","Ship %s entering AIS_STRAFE_POSITION\n", Ships[aip->shipnum].ship_name));
1702                 return;
1703         }
1704
1705         if (Missiontime - aip->submode_start_time > fl2f(0.70f)) {
1706                 aip->submode = AIS_STRAFE_RETREAT1;
1707                 aip->submode_start_time = Missiontime;
1708
1709                 if ( (Missiontime - aip->last_hit_time) < F1_0*5 ) {
1710                         if (ai_maybe_fire_afterburner(Pl_objp, aip)) {
1711                                 afterburners_start(Pl_objp);
1712                                 aip->afterburner_stop_time = Missiontime + F1_0;
1713                         }
1714                 }
1715         }
1716
1717         turn_towards_point(Pl_objp, &aip->prev_goal_point, NULL, 0.0f);
1718         accelerate_ship(aip, 1.0f);
1719 }
1720
1721 // reposition self to begin another strafing run
1722 void ai_big_strafe_position()
1723 {
1724         ai_info *aip;
1725         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1726         // TODO: loop around or something!!
1727
1728         aip->submode = AIS_STRAFE_ATTACK;
1729         aip->submode_start_time = Missiontime;
1730 //      nprintf(("Alan","Ship %s entering AIS_STRAFE_ATTACK\n", Ships[aip->shipnum].ship_name));
1731 }
1732
1733 //      --------------------------------------------------------------------------
1734 // #define      AIS_STRAFE_ATTACK               201     // fly towards target and attack
1735 // #define      AIS_STRAFE_AVOID                202     // fly evasive vector to avoid incoming fire
1736 // #define      AIS_STRAFE_RETREAT1     203     // fly away from attack point (directly)
1737 // #define      AIS_STRAFE_RETREAT1     204     // fly away from attack point (on an avoid vector)
1738 // #define      AIS_STRAFE_POSITION     205     // re-position to resume strafing attack
1739 //
1740 void ai_big_strafe()
1741 {
1742         ai_info *aip;
1743
1744         aip = &Ai_info[Ships[Pl_objp->instance].ai_index];
1745
1746         Assert(aip->mode == AIM_STRAFE);
1747
1748 /*
1749         if ( aip->goal_objnum != aip->target_objnum ) {
1750                 Int3(); // what is going on here? - Get Alan
1751                 aip->mode = AIM_NONE;
1752                 return;
1753         }
1754 */
1755
1756         // check if target is still a big ship... if not enter chase mode
1757         if ( !(Ship_info[Ships[En_objp->instance].ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) ) {
1758                 ai_big_switch_to_chase_mode(aip);
1759                 return;
1760         }
1761         
1762         switch (aip->submode) {
1763         case AIS_STRAFE_ATTACK:
1764                 ai_big_strafe_attack();
1765                 break;
1766         case AIS_STRAFE_AVOID:
1767                 ai_big_strafe_avoid();
1768                 break;
1769         case AIS_STRAFE_RETREAT1:
1770                 ai_big_strafe_retreat1();
1771                 break;
1772         case AIS_STRAFE_RETREAT2:
1773                 ai_big_strafe_retreat2();
1774                 break;
1775         case AIS_STRAFE_POSITION:
1776                 ai_big_strafe_position();
1777                 break;
1778         default:
1779
1780                 Int3();         //      Illegal submode for AIM_STRAFE
1781                 break;
1782         }
1783 }
1784
1785 // See if Pl_objp should enter strafe mode (This is called from maybe_evade_dumbfire_weapon(), and the
1786 // weapon_objnum is for a weapon that is about to collide with Pl_objp
1787 //
1788 // Check if weapon_objnum was fired by Pl_objp's target, and whether Pl_objp's target is a big ship, if
1789 // so, enter AIM_STRAFE
1790 int ai_big_maybe_enter_strafe_mode(object *pl_objp, int weapon_objnum, int consider_target_only)
1791 {
1792         ai_info         *aip;
1793         ship_info       *sip;
1794         object          *weapon_objp, *parent_objp;
1795
1796         aip = &Ai_info[Ships[pl_objp->instance].ai_index];
1797         Assert(aip->mode != AIM_STRAFE);                // can't happen
1798
1799         // if Pl_objp has no target, then we can't enter strafe mode
1800         if ( aip->target_objnum < 0 ) {
1801                 return 0;
1802         }
1803
1804         // if target is not a ship, stafe mode is not possible
1805         if ( Objects[aip->target_objnum].type != OBJ_SHIP ) {
1806                 return 0;
1807         }
1808
1809         sip = &Ship_info[Ships[Objects[aip->target_objnum].instance].ship_info_index];
1810
1811         // if Pl_objp's target is not a big/capital ship, then cannot enter strafe mode
1812         // AL 12-31-97: Even though transports are considered big ships, don't enter strafe mode on them
1813         if ( !(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) || (sip->flags & SIF_TRANSPORT) ) {
1814                 return 0;
1815         }
1816
1817         //      If Pl_objp not a fighter or bomber, don't enter strafe mode. -- MK, 11/11/97.
1818         if ( !(Ship_info[Ships[pl_objp->instance].ship_info_index].flags & (SIF_FIGHTER | SIF_BOMBER)) ) {
1819                 return 0;
1820         }
1821
1822         Assert(weapon_objnum >= 0 && weapon_objnum < MAX_OBJECTS);
1823         weapon_objp = &Objects[weapon_objnum];
1824         Assert(weapon_objp->type == OBJ_WEAPON);
1825
1826         Assert(weapon_objp->parent >= 0 && weapon_objp->parent < MAX_OBJECTS);
1827         parent_objp = &Objects[weapon_objp->parent];
1828         if ( (parent_objp->signature != weapon_objp->parent_sig) || (parent_objp->type != OBJ_SHIP) ) {
1829                 return 0;
1830         }
1831
1832         // Maybe the ship which fired the weapon isn't the current target
1833         if ( OBJ_INDEX(parent_objp) != aip->target_objnum ) {
1834
1835 //JAS IMPOSSIBLE                if (1) { // consider_target_only ) {
1836 //JAS IMPOSSIBLE                        return 0;
1837 //JAS IMPOSSIBLE                } else {
1838                         // switch targets
1839                         sip = &Ship_info[Ships[parent_objp->instance].ship_info_index];
1840                         if ( !(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) || (sip->flags & SIF_TRANSPORT) ) {
1841                                 return 0;
1842                         }
1843                         set_target_objnum(aip, OBJ_INDEX(parent_objp));
1844 //JAS IMPOSSIBLE                }
1845         }
1846
1847         ai_big_strafe_maybe_attack_turret(pl_objp, weapon_objp);
1848
1849         // if we've got this far, the weapon must have come from the player's target, and it is a 
1850         // big/capital ship... so enter strafe mode
1851         aip->previous_mode = aip->mode;
1852         aip->mode = AIM_STRAFE;
1853         aip->submode_parm0 = Missiontime;       // use parm0 as time strafe mode entered
1854         aip->submode = AIS_STRAFE_AVOID;
1855         aip->submode_start_time = Missiontime;
1856 //      nprintf(("Alan","%s Accepted strafe mode\n", Ships[pl_objp->instance].ship_name));
1857
1858         return 1;
1859 }
1860
1861 // Consider attacking a turret, if a turret actually fired the weapon
1862 // input:       ship_objp       =>      ship that will attack the turret
1863 //                              weapon_objp     =>      
1864 void ai_big_strafe_maybe_attack_turret(object *ship_objp, object *weapon_objp)
1865 {
1866         ai_info *aip;
1867         object  *parent_objp;
1868
1869         Assert(ship_objp->type == OBJ_SHIP);
1870         aip = &Ai_info[Ships[ship_objp->instance].ai_index];
1871
1872         // Make decision to attack turret based on AI class.  The better AI ships will realize that
1873         // it is better to take out the turrets first on a big ship.
1874         if ( (frand()*100) > (aip->ai_courage-15) )
1875                 return;
1876
1877         // If ship is already attacking a subsystem, don't switch
1878         if ( aip->targeted_subsys != NULL ) {
1879                 return;
1880         }
1881
1882         // If the weapon is not from a turret, return
1883         if ( Weapons[weapon_objp->instance].turret_subsys == NULL ) {
1884                 return;
1885         }
1886
1887         // Only attack turret if it sits on current target
1888         Assert(weapon_objp->parent >= 0 && weapon_objp->parent < MAX_OBJECTS);
1889         parent_objp = &Objects[weapon_objp->parent];
1890         if ( (parent_objp->signature != weapon_objp->parent_sig) || (parent_objp->type != OBJ_SHIP) ) {
1891                 return;
1892         }
1893
1894         if ( aip->target_objnum != OBJ_INDEX(parent_objp) ) {
1895                 return;
1896         }
1897
1898         // attack the turret
1899         set_targeted_subsys(aip, Weapons[weapon_objp->instance].turret_subsys, OBJ_INDEX(parent_objp));
1900 }