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