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