]> icculus.org git repositories - taylor/freespace2.git/blob - src/ship/aigoals.cpp
Initial revision
[taylor/freespace2.git] / src / ship / aigoals.cpp
1 /*
2  * $Logfile: /Freespace2/code/Ship/AiGoals.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * File to deal with manipulating AI goals, etc.
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 12    7/19/99 12:02p Andsager
15  * Allow AWACS on any ship subsystem. Fix sexp_set_subsystem_strength to
16  * only blow up subsystem if its strength is > 0
17  * 
18  * 11    7/09/99 5:54p Dave
19  * Seperated cruiser types into individual types. Added tons of new
20  * briefing icons. Campaign screen.
21  * 
22  * 10    7/07/99 2:55p Andsager
23  * fix ai_get_subsystem_type to recognize "radara" as an awacs type
24  * subsystem as model_read.
25  * 
26  * 9     6/03/99 8:58p Andsager
27  * Fix bug where player's target is lost.  Alpha wing had clear wing
28  * goals, and this was applied to alpha1.
29  * 
30  * 8     5/27/99 12:14p Andsager
31  * Some fixes for live debris when more than one subsys on ship with live
32  * debris.  Set subsys strength (when 0) blows off subsystem.
33  * sexp_hits_left_subsystem works for SUBSYSTEM_UNKNOWN.
34  * 
35  * 7     4/23/99 12:01p Johnson
36  * Added SIF_HUGE_SHIP
37  * 
38  * 6     3/26/99 4:49p Dave
39  * Made cruisers able to dock with stuff. Made docking points and paths
40  * visible in fred.
41  * 
42  * 5     1/07/99 1:52p Andsager
43  * Initial check in of Sexp_variables
44  * 
45  * 4     12/23/98 2:53p Andsager
46  * Added ship activation and gas collection subsystems, removed bridge
47  * 
48  * 3     11/05/98 5:55p Dave
49  * Big pass at reducing #includes
50  * 
51  * 2     10/07/98 10:53a Dave
52  * Initial checkin.
53  * 
54  * 1     10/07/98 10:51a Dave
55  * 
56  * 173   6/09/98 10:31a Hoffoss
57  * Created index numbers for all xstr() references.  Any new xstr() stuff
58  * added from here on out should be added to the end if the list.  The
59  * current list count can be found in FreeSpace.cpp (search for
60  * XSTR_SIZE).
61  * 
62  * 172   6/01/98 11:43a John
63  * JAS & MK:  Classified all strings for localization.
64  * 
65  * 171   5/21/98 9:57a Mike
66  * Massively improve firing of bombs at big ships.  Improve willingness to
67  * take incoming fire to deliver bomb.  Make a little easier to gain
68  * aspect lock.
69  * 
70  * 170   5/12/98 10:06a Mike
71  * Fix a jillion bugs preventing ships from firing bombs at Lucifer in
72  * sm3-09.
73  * 
74  * 169   5/11/98 11:20a Sandeep
75  * 
76  * 168   5/10/98 12:02a Mike
77  * Only Alpha wing will form on player's wing, not any player wing.
78  * 
79  * 167   4/23/98 1:49a Allender
80  * major rearm/repair fixes for multiplayer.  Fixed respawning of AI ships
81  * to not respawn until 5 seconds after they die.  Send escort information
82  * to ingame joiners
83  * 
84  * 166   4/23/98 12:31a Mike
85  * Fix the sm1-04a Comet:Omega bug.  Was putting a goal on hold if a ship
86  * was docked, but should only have done that if not docked with self.
87  * 
88  * 165   4/22/98 5:00p Allender
89  * new multiplayer dead popup.  big changes to the comm menu system for  *
90  * team vs. team.  Start of debriefing stuff for team vs. team  Make form
91  * on my wing work with individual ships who have high priority 
92  * orders
93  * 
94  * 164   4/18/98 9:53p Mike
95  * Add debug code to track down Comet warpout problems in sm1-04a, then
96  * comment it out for checkin.
97  * 
98  * 163   4/09/98 12:36p Allender
99  * nasty repair problems fixed when ordering it to warp out while
100  * repairing (and on way) and when aborting process while he's completing
101  * the rearm
102  * 
103  * 162   3/31/98 5:19p John
104  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
105  * bunch of debug stuff out of player file.  Made model code be able to
106  * unload models and malloc out only however many models are needed.
107  *  
108  * 
109  * 161   3/19/98 4:43p Allender
110  * allow player ships to follow certain orders
111  * 
112  * 160   3/19/98 2:55p Allender
113  * when ship gets disabled, have him put dock goals on hold
114  * 
115  * 159   3/09/98 9:35p Mike
116  * Suppress warning messages.
117  * 
118  * 158   3/09/98 5:12p Mike
119  * Make sure Pl_objp uses are valid.
120  * Throw asteroids at ships in asteroid field, not "Gal"
121  * Support ships warp out if no goals after 30 seconds.
122  * 
123  * 157   3/07/98 3:50p Lawrance
124  * Add lead indicator for asteroids
125  * 
126  * 156   3/06/98 2:58p Allender
127  * moved assert to more appropriate place
128  * 
129  * 155   3/06/98 11:55a Lawrance
130  * Don't process mission orders for player ships
131  * 
132  * 154   3/05/98 9:35a Allender
133  * don't allow ships to be repaired when departing or dying
134  * 
135  * 153   2/26/98 10:08p Hoffoss
136  * Rewrote state saving and restoring to fix bugs and simplify the code.
137  * 
138  * 152   2/23/98 8:59p Allender
139  * fixed two docking bugs:  1) don't move cargo when ship it's docked with
140  * is undocking.  2) aigoal code was clearing dock goals when it shouldn't
141  * have been
142  * 
143  * 151   2/22/98 4:17p John
144  * More string externalization classification... 190 left to go!
145  * 
146  * 150   2/19/98 12:04a Allender
147  * more multiplayer rearm/dock stuff.  Fix bug in HUD code when finding
148  * support ship reaming player.  show repair as order on Hud extended
149  * target info
150  * 
151  * 149   2/18/98 10:34p Allender
152  * repair/rearm system (for single and multi) about finished.
153  * dock/undock and ai goals packets implemented for multiplayer
154  * 
155  * 148   2/16/98 10:13p Allender
156  * initial work on being able to target weapons (bombs specifically).
157  * Work on getting rearm/repair working under multiplayer
158  * 
159  * 147   2/16/98 4:07p Sandeep
160  * fixed a bug with deleting waypoints
161  * 
162  * 146   2/16/98 3:43p Sandeep
163  * Fixed bug with Ai_warp still expected to have an object reference in a
164  * bit of the code.
165  * 
166  * 145   2/13/98 3:21p Lawrance
167  * Set up allowed goals for escape pods.
168  * 
169  * 144   2/13/98 2:57p Allender
170  * correct code using unitialized variable
171  * 
172  * 143   2/09/98 10:46a Allender
173  * fixed bug with docking where multiple orders to dock with same object
174  * caused strageness
175  * 
176  * 142   2/05/98 12:52p Jim
177  * fix race conditions to get to subsystem index for attacking subsystem
178  * goals
179  * 
180  * 141   2/02/98 4:02p Mike
181  * Allender: Fix bug which caused ships to sometimes undock just as they
182  * were docking.
183  * 
184  * 140   1/30/98 11:00a Allender
185  * tidied up the code to get an index for a new goal.  Made all code (for
186  * both ships and wings) go through this function.
187  * 
188  * 139   1/30/98 10:01a Allender
189  * made large ships able to attack other ships.  Made goal code recognize
190  * when ships removed from wings during ship select
191  * 
192  * 138   1/29/98 12:14p Lawrance
193  * show 'waypoints' as a mission order.
194  * 
195  * 137   1/20/98 9:33a Allender
196  * fixed bug with wing goals from players
197  * 
198  * 136   1/16/98 1:11p Sandeep
199  * fix bug when trying to purge goals with no ship_name
200  * 
201  * 135   1/16/98 11:43a Allender
202  * made it so certain orders (see PURGE_ORDERS) cause other ai goals to
203  * get purged.  This step it to help the AI do the right thing.
204  * 
205  * 134   1/16/98 11:33a Mike
206  * Fix bug in targeting subsystems on protected ships.
207  * 
208  * 133   1/13/98 5:36p Lawrance
209  * Change Ai_goal_text[] to not return anything for waypoints.
210  * 
211  * 132   1/05/98 10:10p Mike
212  * Comment out an obsolete function (discovered while searching for
213  * something else.)
214  * 
215  * 131   1/02/98 1:55p Duncan
216  * added back in case for undocking when checking for goal complete status
217  * 
218  * 130   12/30/97 4:48p Allender
219  * work with ignore my target command.  Added new keyboard hotkey.  Made
220  * it work globally
221  * 
222  * 129   12/26/97 12:15p Mike
223  * Put in debug code for tracking down ships disabling, rather than
224  * destroying, enemies.
225  * 
226  * 128   12/15/97 5:25p Allender
227  * fix problem with docked ships receiving another dock goal.  They now
228  * properly undock
229  * 
230  * 127   12/12/97 5:21p Lawrance
231  * fix some problems with ai mode changing when arriving/departing from
232  * docking bays
233  * 
234  * 126   12/04/97 12:24p Allender
235  * made support ship orders other than rearm be super low priority.
236  * Support ship orders now won't get removed incorrectly either
237  * 
238  * 125   11/26/97 9:55a Allender
239  * minor changed to comm window for rearming.  Fixed some repair bugs --
240  * player's repair was getting aborted by code when he was hit.  Repair
241  * ship wasn't properly removing repair goals
242  * 
243  * 124   11/23/97 6:21p Lawrance
244  * update goal text, used on HUD
245  * 
246  * 123   11/17/97 6:39p Lawrance
247  * add AI_goal_text[] array, used by HUD code to show text description of
248  * order
249  * 
250  * 122   11/09/97 2:21p Mike
251  * Comment out Int3() and email Mark about it.
252  * 
253  * 121   11/05/97 9:30p Mike
254  * Add play dead mode.
255  * Enable undock to complete when dockee moves.
256  * Make ships in waypoint mode react to enemy fire.
257  * Support ships not form on player's wing.
258  * 
259  * 120   11/05/97 4:43p Allender
260  * reworked medal/rank system to read all data from tables.  Made Fred
261  * read medals.tbl.  Changed ai-warp to ai-warp-out which doesn't require
262  * waypoint for activation
263  * 
264  * 119   10/31/97 4:03p Allender
265  * test code to help find rearm/repair problems
266  * 
267  * 118   10/29/97 10:02p Allender
268  * be sure that player starting wings that arrive late also form on the
269  * players wing if there are no goals
270  * 
271  * 117   10/29/97 9:32p Allender
272  * remove undock goal if we can't find docked sihp when docker trying to
273  * undock.  Could be that docked ship was destroyed
274  * 
275  * 116   10/29/97 3:41p Sandeep
276  * Allender put in dock debug code
277  * 
278  * 115   10/28/97 10:30a Allender
279  * rest AIF_FORMATION_OBJECT bit when new order is given to a ship
280  * 
281  * 114   10/24/97 10:40a Allender
282  * player wings with no initial orders now form on players wing by
283  * default.
284  * 
285  * 113   10/23/97 4:41p Allender
286  * lots of new rearm/repair code.  Rearm requests now queue as goals for
287  * support ship.  Warp in of new support ships functional.  Support for
288  * stay-still and play-dead.  
289  * 
290  * 112   10/23/97 4:12p Mike
291  * Support for AIM_STAY_NEAR.
292  * 
293  * 111   10/22/97 1:46p Allender
294  * get ship_name field for ai-stay-still
295  * 
296  * 110   10/22/97 1:44p Allender
297  * more work on stay-still and play-dead
298  * 
299  * 109   10/22/97 11:07a Allender
300  * Hooked in form on my wing into Mikes AI code
301  * 
302  * 108   10/22/97 1:03a Mike
303  * ai-stay-still
304  * form-on-my-wing
305  * fix mysterious code in find_enemy that used goal_objnum instead of
306  * target_objnum.
307  * 
308  * 107   10/12/97 11:23p Mike
309  * About ten fixes/changes in the docking system.
310  * Also, renamed SIF_REARM_REPAIR to SIF_SUPPORT.
311  * 
312  * 106   10/10/97 5:03p Allender
313  * started work on ai-stay-still
314  * 
315  * 105   9/24/97 4:51p Allender
316  * got rid pf default_player_ship_name variable.
317  * 
318  * 104   9/23/97 4:35p Allender
319  * added two function to add "internal" goals to ships and wings.  Used by
320  * AI when it needs to do something special
321  * 
322  * 103   9/09/97 2:42p Allender
323  * fix bug where guarding a wing that hasn't arrived caused goal code to
324  * crash
325  * 
326  * 102   9/09/97 11:28a Johnson
327  * Fixed bug: Code is not Fred-aware.
328  * 
329  * 101   9/08/97 1:04p Allender
330  * put code in ai_post_process_mission() to set orders for ships before
331  * mission starts.  Prevents ships from following orders N seconds after
332  * mission actually starts
333  * 
334  * 100   9/05/97 5:05p Lawrance
335  * memset string to 0 during restore for safety
336  * 
337  * 99    8/18/97 1:16p Allender
338  * added ignore goal into sexpressions
339  * 
340  * 98    8/16/97 3:09p Mike
341  * If telling a ship to attack a protected ship, unprotect that ship.
342  * 
343  * 97    8/16/97 2:40p Mike
344  * Move unmoving ship if it's attacking a large ship and it sees incoming
345  * fire.
346  * Also, don't set protected bit in a ship if someone has attacked a
347  * subsystem.  Only do if someone was told to disarm or disable.
348  * 
349  * 96    8/15/97 11:50a Duncan
350  * Fixed bug with ai functions assuming ai_goal_undock takes a ship
351  * target, which it doesn't.
352  * 
353  * 95    8/15/97 10:05a Hoffoss
354  * One more change to ai_query_goal_valid() to allow for AI_GOAL_NONE.
355  * 
356  * 94    8/15/97 9:59a Hoffoss
357  * Changed ai_query_goal_valid() again.  I can't think of any time you
358  * wouldn't find it easier just to pass in a ship instead of a ship_info
359  * flag structure filtered of all flags that aren't ship type bits.  Since
360  * I'll call this quite a few times, it will make my life easier.
361  * 
362  * 93    8/15/97 9:49a Hoffoss
363  * Changed ai_query_goal_valid() slightly for Fred.  Fred can be more
364  * detailed with error messages should these situations happen, and
365  * recover from them better.
366  * 
367  * 92    8/14/97 10:00p Allender
368  * made ai goals bitfields so that we can have set of orders that certain
369  * ship types are allowed to receive.  Added function for Hoffoss to check
370  * goal vs. ship type validity
371  * 
372  * 91    8/12/97 9:58a Hoffoss
373  * Fixed ai_update_goal_references() and query_referended_in_ai_goals().
374  * 
375  * 90    8/12/97 8:01a Allender
376  * when adding goals to wing structures, replace the oldest entry of the
377  * goals if there are no empty entries
378  * 
379  * 89    8/12/97 1:55a Hoffoss
380  * Made extensive changes to object reference checking and handling for
381  * object deletion call.
382  * 
383  * 88    8/10/97 4:24p Hoffoss
384  * Made initial orders revert back to none if it has invalid data
385  * anywhere.
386  * 
387  * 87    8/08/97 1:30p Allender
388  * added commands (and ai goals) for support ships.  stay near ship (which
389  * could be player or current target), and keep safe distance
390  * 
391  * 86    8/07/97 11:11a Allender
392  * fixed problem with wings completing waypoint goals
393  * 
394  * 85    8/06/97 9:41a Allender
395  * renamed from ai_goal function.  made a function to remove a goal from a
396  * wing (used strictly for waypoints now).  ai-chase-wing and
397  * ai-guard-wing now appear to user as ai-chase and ai-guard.  Tidied up
398  * function which deals with sexpression goals
399  * 
400  * 84    8/06/97 8:06a Lawrance
401  * add more stuff to save/restore
402  * 
403  * 83    8/01/97 11:51a Dave
404  * Changed calls to timer_get_fixed_seconds() to simply Missiontime.
405  * Solves a lot of demo related problems.
406  * 
407  * 82    7/30/97 11:01p Mike
408  * Set submode when setting mode from player order.
409  * Increase distance to circle when ignoring player's target.
410  * 
411  * 81    7/28/97 10:28a Mike
412  * Use forward_interpolate() to prevent weird banking behavior.
413  * 
414  * Suppress a couple annoying mprints and clarify another.
415  * 
416  * 80    7/25/97 12:10p Mike
417  * Better default behavior.
418  * 
419  * 79    7/24/97 4:55p Allender
420  * added ai-evade-ship to fred and to Freespace
421  * 
422  * 78    7/24/97 4:20p Mike
423  * Add goal and Fred hook for evade behavior.
424  * 
425  * 77    7/24/97 2:17p Jasen
426  * Fixed a bug with mission loading.
427  * 
428  * 76    7/24/97 12:10a Mike
429  * Suppress problems when loadign pain.fsm.
430  * 
431  * 75    7/23/97 6:49p Hoffoss
432  * Fixed bug in ai goal adding code.  It was using a pointer that was
433  * temporary.
434  * 
435  * 74    7/23/97 11:25a Allender
436  * more robust sexpression checking.  Fixed problem with some ai goals not
437  * being named properly
438  *
439  * $NoKeywords: $
440  */
441
442 #include "aigoals.h"
443 #include "freespace.h"
444 #include "ai.h"
445 #include "sexp.h"
446 #include "missionlog.h"
447 #include "missionparse.h"
448 #include "model.h"                              // for subsystem types
449 #include "linklist.h"
450 #include "timer.h"
451 #include "player.h"
452 #include "multimsgs.h"
453 #include "multi.h"
454
455 // all ai goals dealt with in this code are goals that are specified through
456 // sexpressions in the mission file.  They are either specified as part of a
457 // ships goals in the #Object section of the mission file, or are created (and
458 // removed) dynamically using the #Events section.  Default goal behaviour and
459 // dynamic goals are not handled here.
460
461 // defines for player issued goal priorities
462 #define PLAYER_PRIORITY_MIN                             90
463 #define PLAYER_PRIORITY_SHIP                            100
464 #define PLAYER_PRIORITY_WING                            95
465 #define PLAYER_PRIORITY_SUPPORT_LOW             10
466
467 // define for which goals cause other goals to get purged
468 #define PURGE_GOALS             (AI_GOAL_IGNORE | AI_GOAL_DISABLE_SHIP | AI_GOAL_DISARM_SHIP)
469
470 // goals given from the player to other ships in the game are also handled in this
471 // code
472
473
474 #define AI_GOAL_ACHIEVABLE                      1
475 #define AI_GOAL_NOT_ACHIEVABLE  2
476 #define AI_GOAL_NOT_KNOWN                       3
477 #define AI_GOAL_SATISFIED                       4
478
479 int     Ai_goal_signature;
480 int     Num_ai_dock_names = 0;
481 char    Ai_dock_names[MAX_AI_DOCK_NAMES][NAME_LENGTH];
482
483 // AL 11-17-97: A text description of the AI goals.  This is used for printing out on the
484 // HUD what a ship's current orders are.  If the AI goal doesn't correspond to something that
485 // ought to be printable, then NULL is used.
486 // JAS: Converted to a function in order to externalize the strings
487 char *Ai_goal_text(int goal)
488 {
489         switch(goal)    {
490         case 1:
491                 return XSTR( "attack ", 474);
492         case 2:
493                 return XSTR( "dock ", 475);
494         case 3:
495                 return XSTR( "waypoints", 476);
496         case 4:
497                 return XSTR( "waypoints", 476);
498         case 6:
499                 return XSTR( "destroy ", 477);
500         case 7:
501                 return XSTR( "form on ", 478);
502         case 8:
503                 return XSTR( "undock ", 479);
504         case 9:
505                 return XSTR( "attack ", 474);
506         case 10:
507                 return XSTR( "guard ", 480);
508         case 11:
509                 return XSTR( "disable ", 481);
510         case 12:
511                 return XSTR( "disarm ", 482);
512         case 15:
513                 return XSTR( "guard ", 480);
514         case 16:
515                 return XSTR( "evade ", 483);
516         case 19:
517                 return XSTR( "rearm ", 484);
518         }
519
520         // Avoid compiler warning
521         return NULL;
522 };
523
524 // function to maybe add the form on my wing goal for a player's starting wing.  Called from below and when a
525 // player wing arrives.
526 void ai_maybe_add_form_goal( wing *wingp )
527 {
528         int j;
529
530         // iterate through the ship_index list of this wing and check for orders.  We will do
531         // this for all ships in the wing instead of on a wing only basis in cases some ships
532         // in the wing actually have different orders than others
533         for ( j = 0; j < wingp->current_count; j++ ) {
534                 ai_info *aip;
535
536                 Assert( wingp->ship_index[j] != -1 );                                           // get Allender
537
538                 aip = &Ai_info[Ships[wingp->ship_index[j]].ai_index];
539                 // don't process Player_ship
540                 if ( aip == Player_ai )
541                         continue;
542                 
543                 // it is sufficient enough to check the first goal entry to see if it has a valid
544                 // goal
545                 if ( aip->goals[0].ai_mode == AI_GOAL_NONE ) {
546                         // need to add a form on my wing goal here.  Ships are always forming on the player's wing.
547                         ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Player_ship->ship_name, aip );
548                 }
549         }
550 }
551
552 void ai_post_process_mission()
553 {
554         object *objp;
555         int i;
556
557         // Check ships in player starting wings.  Those ships should follow these rules:
558         // (1) if they have no orders, they should get a form on my wing order
559         // (2) if they have an order, they are free to act on it.
560         //
561         // So basically, we are checking for (1)
562         if ( !Fred_running ) {
563                 for ( i = 0; i < 1; i++ ) {     //      MK, 5/9/98: Used to iterate through MAX_PLAYER_WINGS, but this was too many ships forming on player.
564                         if ( Starting_wings[i] != -1 ) {
565                                 wing *wingp;
566
567                                 wingp = &Wings[Starting_wings[i]];
568
569                                 ai_maybe_add_form_goal( wingp );
570
571                         }
572                 }
573         }
574
575         // for every valid ship object, call process_mission_orders to be sure that ships start the
576         // mission following the orders in the mission file right away instead of waiting N seconds
577         // before following them.  Do both the created list and the object list for safety
578         for ( objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
579                 if ( objp->type != OBJ_SHIP )
580                         continue;
581                 ai_process_mission_orders( OBJ_INDEX(objp), &Ai_info[Ships[objp->instance].ai_index] );
582         }
583         for ( objp = GET_FIRST(&obj_create_list); objp != END_OF_LIST(&obj_create_list); objp = GET_NEXT(objp) ) {
584                 if ( (objp->type != OBJ_SHIP) || Fred_running )
585                         continue;
586                 ai_process_mission_orders( OBJ_INDEX(objp), &Ai_info[Ships[objp->instance].ai_index] );
587         }
588
589         return;
590 }
591
592 // function which determines is a goal is valid for a particular type of ship
593 int ai_query_goal_valid( int ship, int ai_goal )
594 {
595         int accepted;
596
597         if (ai_goal == AI_GOAL_NONE)
598                 return 1;  // anything can have no orders.
599
600         accepted = 0;
601         switch (Ship_info[Ships[ship].ship_info_index].flags & SIF_ALL_SHIP_TYPES) {
602         case SIF_CARGO:
603                 if (!Fred_running)
604                         Int3();                 // get Hoffoss or Allender -- cargo containers shouldn't have a goal!!!
605                 break;
606
607         case SIF_FIGHTER:
608                 if ( ai_goal & AI_GOAL_ACCEPT_FIGHTER ){
609                         accepted = 1;
610                 }
611                 break;
612         case SIF_BOMBER:
613                 if ( ai_goal & AI_GOAL_ACCEPT_BOMBER ){
614                         accepted = 1;
615                 }
616                 break;
617         case SIF_CRUISER:
618                 if ( ai_goal & AI_GOAL_ACCEPT_CRUISER ){
619                         accepted = 1;
620                 }
621                 break;
622         case SIF_FREIGHTER:
623                 if ( ai_goal & AI_GOAL_ACCEPT_FREIGHTER ){
624                         accepted = 1;
625                 }
626                 break;
627         case SIF_CAPITAL:
628                 if ( ai_goal & AI_GOAL_ACCEPT_CAPITAL ){
629                         accepted = 1;
630                 }
631                 break;
632         case SIF_TRANSPORT:
633                 if ( ai_goal & AI_GOAL_ACCEPT_TRANSPORT ){
634                         accepted = 1;
635                 }
636                 break;
637         case SIF_SUPPORT:
638                 if ( ai_goal & AI_GOAL_ACCEPT_SUPPORT ){
639                         accepted = 1;
640                 }
641                 break;
642         case SIF_ESCAPEPOD:
643                 if ( ai_goal & AI_GOAL_ACCEPT_ESCAPEPOD ){
644                         accepted = 1;
645                 }
646                 break;
647         case SIF_SUPERCAP:
648                 if ( ai_goal & AI_GOAL_ACCEPT_SUPERCAP ){
649                         accepted = 1;
650                 }
651                 break;
652         case SIF_STEALTH:
653                 if ( ai_goal & AI_GOAL_ACCEPT_STEALTH ){
654                         accepted = 1;
655                 }
656                 break;          
657         case SIF_CORVETTE:
658                 if ( ai_goal & AI_GOAL_ACCEPT_CORVETTE ){
659                         accepted = 1;
660                 }
661                 break;
662         case SIF_GAS_MINER:
663                 if ( ai_goal & AI_GOAL_ACCEPT_GAS_MINER ){
664                         accepted = 1;
665                 }
666                 break;
667         case SIF_AWACS:
668                 if ( ai_goal & AI_GOAL_ACCEPT_AWACS ){
669                         accepted = 1;
670                 }
671                 break;
672         case SIF_NO_SHIP_TYPE:
673                 if (!Fred_running){
674                         Int3();                 // HUH?  doesn't make sense
675                 }
676                 break;
677         default:
678                 if (!Fred_running){
679                         Int3();                 // get allender or hoffos -- unknown ship type
680                 }
681                 break;
682         }
683
684         return accepted;
685 }
686
687 // remove an ai goal from it's list.  Uses the active_goal member as the goal to remove
688 void ai_remove_ship_goal( ai_info *aip, int index )
689 {
690         // only need to set the ai_mode for the particular goal to AI_GOAL_NONE
691         // reset ai mode to default behavior.  Might get changed next time through
692         // ai goal code look
693         Assert ( index >= 0 );                  // must have a valid goal
694
695         aip->goals[index].ai_mode = AI_GOAL_NONE;
696         aip->goals[index].signature = -1;
697         aip->goals[index].priority = -1;
698         aip->goals[index].flags = 0;                            // must reset the flags since not doing so will screw up goal sorting.
699         if ( index == aip->active_goal )
700                 aip->active_goal = AI_GOAL_NONE;
701
702         // mwa -- removed this line 8/5/97.  Just because we remove a goal doesn't mean to do the default
703         // behavior.  We will make the call commented out below in a more reasonable location
704         //ai_do_default_behavior( &Objects[Ships[aip->shipnum].objnum] );
705 }
706
707 void ai_clear_ship_goals( ai_info *aip )
708 {
709         int i;
710
711         for (i = 0; i < MAX_AI_GOALS; i++)
712                 ai_remove_ship_goal( aip, i );                  // resets active_goal and default behavior
713
714         aip->active_goal = AI_GOAL_NONE;                                        // for good measure
715
716         // next line moved here on 8/5/97 by MWA
717         // Dont reset player ai (and hence target)
718         if ( !((Player_ship != NULL) && (&Ships[aip->shipnum] == Player_ship)) ) {
719                 ai_do_default_behavior( &Objects[Ships[aip->shipnum].objnum] );
720         }
721 }
722
723 void ai_clear_wing_goals( int wingnum )
724 {
725         int i;
726         wing *wingp = &Wings[wingnum];
727         //p_object *objp;
728
729         // clear the goals for all ships in the wing
730         for (i = 0; i < wingp->current_count; i++) {
731                 int num = wingp->ship_index[i];
732
733                 if ( num > -1 )
734                         ai_clear_ship_goals( &Ai_info[Ships[num].ai_index] );
735
736         }
737
738                 // clear out the goals for the wing now
739         for (i = 0; i < MAX_AI_GOALS; i++) {
740                 wingp->ai_goals[i].ai_mode = AI_GOAL_NONE;
741                 wingp->ai_goals[i].signature = -1;
742                 wingp->ai_goals[i].priority = -1;
743         }
744
745
746 }
747
748 // routine which marks a wing goal as being complete.  We get the wingnum and a pointer to the goal
749 // structure of the goal to be removed.  This process is slightly tricky since some member of the wing
750 // might be pursuing a different goal.  We will have to compare based on mode, submode, priority,
751 // and name.. This routine is only currently called from waypoint code!!!
752 void ai_mission_wing_goal_complete( int wingnum, ai_goal *remove_goalp )
753 {
754         int mode, submode, priority, i;
755         char *name;
756         ai_goal *aigp;
757         wing *wingp;
758
759         wingp = &Wings[wingnum];
760
761         // set up locals for faster access.
762         mode = remove_goalp->ai_mode;
763         submode = remove_goalp->ai_submode;
764         priority = remove_goalp->priority;
765         name = remove_goalp->ship_name;
766
767         Assert ( name );                        // should not be NULL!!!!
768
769         // remove the goal from all the ships currently in the wing
770         for (i = 0; i < wingp->current_count; i++ ) {
771                 int num, j;
772                 ai_info *aip;
773
774                 num = wingp->ship_index[i];
775                 Assert ( num >= 0 );
776                 aip = &Ai_info[Ships[num].ai_index];
777                 for ( j = 0; j < MAX_AI_GOALS; j++ ) {
778                         aigp = &(aip->goals[j]);
779
780                         // don't need to worry about these types of goals since they can't possibly be a goal we are looking for.
781                         if ( (aigp->ai_mode == AI_GOAL_NONE) || !aigp->ship_name )
782                                 continue;
783
784                         if ( (aigp->ai_mode == mode) && (aigp->ai_submode == submode) && (aigp->priority == priority) && !stricmp(name, aigp->ship_name) ) {
785                                 ai_remove_ship_goal( aip, j );
786                                 ai_do_default_behavior( &Objects[Ships[aip->shipnum].objnum] );         // do the default behavior
787                                 break;                  // we are all done
788                         }
789                 }
790         }
791
792         // now remove the goal from the wing
793         for (i = 0; i < MAX_AI_GOALS; i++ ) {
794                 aigp = &(wingp->ai_goals[i]);
795                 if ( (aigp->ai_mode == AI_GOAL_NONE) || !aigp->ship_name )
796                         continue;
797
798                 if ( (aigp->ai_mode == mode) && (aigp->ai_submode == submode) && (aigp->priority == priority) && !stricmp(name, aigp->ship_name) ) {
799                         wingp->ai_goals[i].ai_mode = AI_GOAL_NONE;
800                         wingp->ai_goals[i].signature = -1;
801                         wingp->ai_goals[i].priority = -1;
802                         break;
803                 }
804         }
805                         
806 }
807
808 // routine which is called with an ai object complete it's goal.  Do some action
809 // based on the goal what was just completed
810
811 void ai_mission_goal_complete( ai_info *aip )
812 {
813         // if the active goal is dynamic or none, just return.  (AI_GOAL_NONE is probably an error, but
814         // I don't think that this is a problem)
815         if ( (aip->active_goal == AI_GOAL_NONE) || (aip->active_goal == AI_ACTIVE_GOAL_DYNAMIC) )
816                 return;
817
818         ai_remove_ship_goal( aip, aip->active_goal );
819         ai_do_default_behavior( &Objects[Ships[aip->shipnum].objnum] );         // do the default behavior
820
821 }
822
823 int ai_get_subsystem_type( char *subsystem )
824 {
825         if ( strstr(subsystem, "engine") ) {
826                 return SUBSYSTEM_ENGINE;
827         } else if ( strstr(subsystem, "radar") ) {
828                 return SUBSYSTEM_RADAR;
829         } else if ( strstr(subsystem, "turret") ) {
830                 return SUBSYSTEM_TURRET;
831         } else if ( strstr(subsystem, "navigation") ) {
832                 return SUBSYSTEM_NAVIGATION;
833         } else if ( !strnicmp(subsystem, NOX("communication"), 13) ) {
834                 return SUBSYSTEM_COMMUNICATION;
835         } else if ( !strnicmp(subsystem, NOX("weapons"), 7) )  {
836                 return SUBSYSTEM_WEAPONS;
837         } else if ( !strnicmp(subsystem, NOX("sensors"), 7) )  {
838                 return SUBSYSTEM_SENSORS;
839         } else if ( !strnicmp(subsystem, NOX("solar"), 5) )  {
840                 return SUBSYSTEM_SOLAR;
841         } else if ( !strnicmp(subsystem, NOX("gas"), 3) )  {
842                 return SUBSYSTEM_GAS_COLLECT;
843         } else if ( !strnicmp(subsystem, NOX("activator"), 9) )  {
844                 return SUBSYSTEM_ACTIVATION;
845         } else {                                                                        // If unrecognized type, set to engine so artist can continue working...
846                 if (!Fred_running) {
847 //                      Int3();                                                 // illegal subsystem type -- find allender
848                 }
849
850                 return SUBSYSTEM_UNKNOWN;
851         }
852 }
853
854 // function to prune out goals which are no longer valid, based on a goal pointer passed in.
855 // for instance, if we get passed a goal of "disable X", then any goals in the given goal array
856 // which are destroy, etc, should get removed.  goal list is the list of goals to purge.  It is
857 // always MAX_AI_GOALS in length.  This function will only get called when the goal which causes
858 // purging becomes valid.
859 void ai_goal_purge_invalid_goals( ai_goal *aigp, ai_goal *goal_list )
860 {
861         int i;
862         ai_goal *purge_goal;
863         char *name;
864         int mode, ship_index, wingnum;
865
866         // get locals for easer access
867         name = aigp->ship_name;
868         mode = aigp->ai_mode;
869
870         // these goals cannot be associated to wings, but can to a ship in a wing.  So, we should find out
871         // if the ship is in a wing so we can purge goals which might operate on that wing
872         ship_index = ship_name_lookup(name);
873         if ( ship_index == -1 ) {
874                 Int3();                                         // get allender -- this is sort of odd
875                 return;
876         }
877         wingnum = Ships[ship_index].wingnum;
878
879         purge_goal = goal_list;
880         for ( i = 0; i < MAX_AI_GOALS; purge_goal++, i++ ) {
881                 int purge_ai_mode, purge_wing;
882
883                 purge_ai_mode = purge_goal->ai_mode;
884
885                 // don't need to process AI_GOAL_NONE
886                 if ( purge_ai_mode == AI_GOAL_NONE )
887                         continue;
888
889                 // goals must operate on something to be purged.
890                 if ( purge_goal->ship_name == NULL )
891                         continue;
892
893                 // determine if the purge goal is acting either on the ship or the ship's wing.
894                 purge_wing = wing_name_lookup( purge_goal->ship_name, 1 );
895
896                 // if the target of the purge goal is a ship (purge_wing will be -1), then if the names
897                 // don't match, we can continue;  if the wing is valid, don't process if the wing numbers
898                 // are different.
899                 if ( purge_wing == -1 ) {
900                         if ( stricmp(purge_goal->ship_name, name ) )
901                                 continue;
902                 } else if ( purge_wing != wingnum )
903                         continue;
904
905                 switch( mode ) {
906                 // ignore goals should get rid of any kind of attack goal
907                 case AI_GOAL_IGNORE:
908                         if ( purge_ai_mode & (AI_GOAL_DISABLE_SHIP | AI_GOAL_DISARM_SHIP | AI_GOAL_CHASE | AI_GOAL_CHASE_WING | AI_GOAL_DESTROY_SUBSYSTEM) )
909                                 purge_goal->flags |= AIGF_PURGE;
910                         break;
911
912                 // disarm/disable goals should remove any general attack
913                 case AI_GOAL_DISABLE_SHIP:
914                 case AI_GOAL_DISARM_SHIP:
915                         if ( purge_ai_mode & (AI_GOAL_CHASE | AI_GOAL_CHASE_WING) )
916                                 purge_goal->flags |= AIGF_PURGE;
917                         break;
918                 }
919         }
920 }
921
922 // function to purge the goals of all ships in the game based on the incoming goal structure
923 void ai_goal_purge_all_invalid_goals( ai_goal *aigp )
924 {
925         int mode, i;
926         ship_obj *sop;
927
928         mode = aigp->ai_mode;
929
930         // only purge goals if a new goal is one of the types in next statement
931         if ( !(mode & PURGE_GOALS) )
932                 return;
933
934         for ( sop = GET_FIRST(&Ship_obj_list); sop != END_OF_LIST(&Ship_obj_list); sop = GET_NEXT(sop) ) {
935                 ship *shipp;
936
937                 shipp = &Ships[Objects[sop->objnum].instance];
938                 ai_goal_purge_invalid_goals( aigp, Ai_info[shipp->ai_index].goals );
939         }
940
941         // we must do the same for the wing goals
942         for (i = 0; i < num_wings; i++ )
943                 ai_goal_purge_invalid_goals( aigp, Wings[i].ai_goals );
944 }
945
946 // function to fix up dock point references for objects.
947 // passed are the pointer to goal we are working with.  aip if the ai_info pointer
948 // of the ship with the order.  aigp is a pointer to the goal (of aip) of which we are
949 // fixing up the docking points
950 void ai_goal_fixup_dockpoints(ai_info *aip, ai_goal *aigp)
951 {
952         int shipnum, dockee_index, docker_index;
953
954         Assert ( aip->shipnum != -1 );
955         shipnum = ship_name_lookup( aigp->ship_name );
956         docker_index = -1;
957         dockee_index = -1;
958
959         // look for docking points of the appriopriate type.  Use cargo docks for cargo ships.
960         // use 
961         if ( Ship_info[Ships[shipnum].ship_info_index].flags & SIF_CARGO ) {
962                 docker_index = model_find_dock_index(Ships[aip->shipnum].modelnum, DOCK_TYPE_CARGO );
963                 dockee_index = model_find_dock_index(Ships[shipnum].modelnum, DOCK_TYPE_CARGO );
964         } else if ( Ship_info[Ships[aip->shipnum].ship_info_index].flags & SIF_SUPPORT ) {
965                 docker_index = model_find_dock_index( Ships[aip->shipnum].modelnum, DOCK_TYPE_REARM );
966                 dockee_index = model_find_dock_index( Ships[shipnum].modelnum, DOCK_TYPE_REARM );
967         }
968
969         // if we didn't find dockpoints above, then we should just look for generic docking points
970         if ( docker_index == -1 )
971                 docker_index = model_find_dock_index(Ships[aip->shipnum].modelnum, DOCK_TYPE_GENERIC );
972         if ( dockee_index == -1 )
973                 dockee_index = model_find_dock_index(Ships[shipnum].modelnum, DOCK_TYPE_GENERIC );
974                 
975         aigp->docker.index = docker_index;
976         aigp->dockee.index = dockee_index;
977         aigp->flags &= ~(AIGF_DOCKER_NAME_VALID | AIGF_DOCKEE_NAME_VALID);
978 }
979
980 // these functions deal with adding goals sent from the player.  They are slightly different
981 // from the mission goals (i.e. those goals which come from events) in that we don't
982 // use sexpressions for goals from the player...so we enumerate all the parameters
983
984 void ai_add_goal_sub_player(int type, int mode, int submode, char *shipname, ai_goal *aigp )
985 {
986         Assert ( (type == AIG_TYPE_PLAYER_WING) || (type == AIG_TYPE_PLAYER_SHIP) );
987
988         aigp->time = Missiontime;
989         aigp->type = type;                                                                                      // from player for sure -- could be to ship or to wing
990         aigp->ai_mode = mode;                                                                           // major mode for this goal
991         aigp->ai_submode = submode;                                                             // could mean different things depending on mode
992
993         if ( mode == AI_GOAL_WARP )
994                 aigp->wp_index = submode;
995
996         if ( mode == AI_GOAL_CHASE_WEAPON ) {
997                 aigp->wp_index = submode;                                                               // submode contains the instance of the weapon
998                 aigp->weapon_signature = Objects[Weapons[submode].objnum].signature;
999         }
1000
1001         if ( shipname != NULL )
1002                 aigp->ship_name = ai_get_goal_ship_name( shipname, &aigp->ship_name_index );
1003         else
1004                 aigp->ship_name = NULL;
1005
1006         // special case certain orders from player so that ships continue to do the right thing
1007
1008         // make priority for these two support ship orders low so that they will prefer repairing
1009         // a ship over staying near a ship.
1010         if ( (mode == AI_GOAL_STAY_NEAR_SHIP) || (mode == AI_GOAL_KEEP_SAFE_DISTANCE) )
1011                 aigp->priority = PLAYER_PRIORITY_SUPPORT_LOW;
1012
1013         else if ( aigp->type == AIG_TYPE_PLAYER_WING )
1014                 aigp->priority = PLAYER_PRIORITY_WING;                  // player wing goals not as high as ship goals
1015         else
1016                 aigp->priority = PLAYER_PRIORITY_SHIP;
1017 }
1018
1019 int ai_goal_find_empty_slot( ai_goal *goals )
1020 {
1021         int gindex, empty_index, oldest_index;
1022
1023         empty_index = -1;
1024         oldest_index = 0;
1025         for ( gindex = 0; gindex < MAX_AI_GOALS; gindex++ ) {
1026                 if ( goals[gindex].time < goals[oldest_index].time )
1027                         oldest_index = gindex;
1028
1029                 if ( (empty_index == -1) && (goals[gindex].ai_mode == AI_GOAL_NONE) )                   // get the index for this goal
1030                         empty_index = gindex;
1031         }
1032
1033         // if we didn't find an empty slot, find the oldest goal and use it's slot
1034         if ( empty_index == -1 )
1035                 empty_index = oldest_index;
1036
1037         Assert ( empty_index < MAX_AI_GOALS );
1038
1039         return empty_index;
1040 }
1041
1042 // adds a goal from a player to the given ship's ai_info structure.  'type' tells us if goal
1043 // is issued to ship or wing (from player),  mode is AI_GOAL_*. submode is the submode the
1044 // ship should go into.  shipname is the object of the action.  aip is the ai_info pointer
1045 // of the ship receiving the order
1046 void ai_add_ship_goal_player( int type, int mode, int submode, char *shipname, ai_info *aip )
1047 {
1048         int empty_index;
1049         ai_goal *aigp;
1050
1051         empty_index = ai_goal_find_empty_slot( aip->goals );
1052
1053         // get a pointer to the goal structure
1054         aigp = &aip->goals[empty_index];
1055         ai_add_goal_sub_player( type, mode, submode, shipname, aigp );
1056
1057         // if the goal is to dock, then we must determine which dock points on the two ships to use.
1058         // If the target of the dock is a cargo type container, then we should use DOCK_TYPE_CARGO
1059         // on both ships.  Code is here instead of in ai_add_goal_sub_player() since a dock goal
1060         // should only occur to a specific ship.
1061
1062         if ( (mode == AI_GOAL_REARM_REPAIR) || ((mode == AI_GOAL_DOCK) && (submode == AIS_DOCK_0)) ) {
1063                 ai_goal_fixup_dockpoints( aip, aigp );
1064         }
1065
1066         aigp->signature = Ai_goal_signature++;
1067
1068 }
1069
1070 // adds a goal from the player to the given wing (which in turn will add it to the proper
1071 // ships in the wing
1072 void ai_add_wing_goal_player( int type, int mode, int submode, char *shipname, int wingnum )
1073 {
1074         int i, empty_index;
1075         wing *wingp = &Wings[wingnum];
1076
1077         // add the ai goal for any ship that is currently arrived in the game.
1078         if ( !Fred_running ) {                                                                          // only add goals to ships if fred isn't running
1079                 for (i = 0; i < wingp->current_count; i++) {
1080                         int num = wingp->ship_index[i];
1081                         if ( num == -1 )                        // ship must have been destroyed or departed
1082                                 continue;
1083                         ai_add_ship_goal_player( type, mode, submode, shipname, &Ai_info[Ships[num].ai_index] );
1084                 }
1085         }
1086
1087         // add the sexpression index into the wing's list of goal sexpressions if
1088         // there are more waves to come.  We use the same method here as when adding a goal to
1089         // a ship -- find the first empty entry.  If none exists, take the oldest entry and replace it.
1090         empty_index = ai_goal_find_empty_slot( wingp->ai_goals );
1091         ai_add_goal_sub_player( type, mode, submode, shipname, &wingp->ai_goals[empty_index] );
1092 }
1093
1094
1095 // common routine to add a sexpression mission goal to the appropriate goal structure.
1096 void ai_add_goal_sub_sexp( int sexp, int type, ai_goal *aigp )
1097 {
1098         int node, dummy, op;
1099         char *text;
1100
1101         Assert ( Sexp_nodes[sexp].first != -1 );
1102         node = Sexp_nodes[sexp].first;
1103         text = CTEXT(node);
1104
1105         aigp->signature = Ai_goal_signature++;
1106
1107         aigp->time = Missiontime;
1108         aigp->type = type;
1109         aigp->flags = 0;
1110
1111         op = find_operator( text );
1112
1113         switch (op) {
1114
1115         case OP_AI_WAYPOINTS_ONCE:
1116         case OP_AI_WAYPOINTS: {
1117                 int ref_type;
1118
1119                 ref_type = Sexp_nodes[CDR(node)].subtype;
1120                 if (ref_type == SEXP_ATOM_STRING) {  // referenced by name
1121                         // save the waypoint path name -- the index will get resolved when the goal is checked
1122                         // for acheivability.
1123                         aigp->ship_name = ai_get_goal_ship_name(CTEXT(CDR(node)), &aigp->ship_name_index);  // waypoint path name;
1124                         aigp->wp_index = -1;
1125
1126                 } else
1127                         Int3();
1128
1129                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1130                 aigp->ai_mode = AI_GOAL_WAYPOINTS;
1131                 if ( op == OP_AI_WAYPOINTS_ONCE )
1132                         aigp->ai_mode = AI_GOAL_WAYPOINTS_ONCE;
1133                 break;
1134         }
1135
1136         case OP_AI_DESTROY_SUBSYS:
1137                 aigp->ai_mode = AI_GOAL_DESTROY_SUBSYSTEM;
1138                 aigp->ship_name = ai_get_goal_ship_name( CTEXT(CDR(node)), &aigp->ship_name_index );
1139                 // store the name of the subsystem in the docker_name field for now -- this field must
1140                 // get fixed up when the goal is valid since we need to locate the subsystem on the ship's
1141                 // model.
1142                 aigp->docker.name = ai_get_goal_ship_name(CTEXT(CDR(CDR(node))), &dummy);
1143                 aigp->flags |= AIGF_SUBSYS_NAME_VALID;
1144                 aigp->priority = atoi( CTEXT(CDR(CDR(CDR(node)))) );
1145                 break;
1146
1147         case OP_AI_DISABLE_SHIP:
1148                 aigp->ai_mode = AI_GOAL_DISABLE_SHIP;
1149                 aigp->ship_name = ai_get_goal_ship_name( CTEXT(CDR(node)), &aigp->ship_name_index );
1150                 aigp->ai_submode = -SUBSYSTEM_ENGINE;
1151                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1152                 break;
1153
1154         case OP_AI_DISARM_SHIP:
1155                 aigp->ai_mode = AI_GOAL_DISARM_SHIP;
1156                 aigp->ship_name = ai_get_goal_ship_name( CTEXT(CDR(node)), &aigp->ship_name_index );
1157                 aigp->ai_submode = -SUBSYSTEM_TURRET;
1158                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1159                 break;
1160
1161         case OP_AI_WARP_OUT:
1162                 aigp->ai_mode = AI_GOAL_WARP;
1163                 aigp->priority = atoi( CTEXT(CDR(node)) );
1164                 break;
1165
1166                 // the following goal is obsolete, but here for compatibility
1167         case OP_AI_WARP:
1168                 aigp->ai_mode = AI_GOAL_WARP;
1169                 aigp->ship_name = ai_get_goal_ship_name(CTEXT(CDR(node)), &aigp->ship_name_index);  // waypoint path name;
1170                 //aigp->wp_index = atoi( CTEXT(CDR(node)) );            // this is the index into the warp points
1171                 aigp->wp_index = -1;
1172                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1173                 break;
1174
1175         case OP_AI_UNDOCK:
1176                 aigp->ship_name = NULL;
1177                 aigp->priority = atoi( CTEXT(CDR(node)) );
1178                 aigp->ai_mode = AI_GOAL_UNDOCK;
1179                 aigp->ai_submode = AIS_UNDOCK_0;
1180                 break;
1181
1182         case OP_AI_STAY_STILL:
1183                 aigp->ai_mode = AI_GOAL_STAY_STILL;
1184                 aigp->ship_name = ai_get_goal_ship_name(CTEXT(CDR(node)), &aigp->ship_name_index);  // waypoint path name;
1185                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1186                 break;
1187
1188         case OP_AI_DOCK:
1189                 aigp->ship_name = ai_get_goal_ship_name( CTEXT(CDR(node)), &aigp->ship_name_index );
1190                 aigp->docker.name = ai_add_dock_name(CTEXT(CDR(CDR(node))));
1191                 aigp->dockee.name = ai_add_dock_name(CTEXT(CDR(CDR(CDR(node)))));
1192                 aigp->flags |= ( AIGF_DOCKER_NAME_VALID | AIGF_DOCKEE_NAME_VALID );
1193                 aigp->priority = atoi( CTEXT(CDR(CDR(CDR(CDR(node))))) );
1194
1195                 aigp->ai_mode = AI_GOAL_DOCK;
1196                 aigp->ai_submode = AIS_DOCK_0;          // be sure to set the submode
1197                 break;
1198
1199         case OP_AI_CHASE_ANY:
1200                 aigp->priority = atoi( CTEXT(CDR(node)) );
1201                 aigp->ai_mode = AI_GOAL_CHASE_ANY;
1202                 break;
1203
1204         case OP_AI_PLAY_DEAD:
1205                 aigp->priority = atoi( CTEXT(CDR(node)) );
1206                 aigp->ai_mode = AI_GOAL_PLAY_DEAD;
1207                 break;
1208
1209         case OP_AI_KEEP_SAFE_DISTANCE:
1210                 aigp->priority = atoi( CTEXT(CDR(node)) );
1211                 aigp->ai_mode = AI_GOAL_KEEP_SAFE_DISTANCE;
1212                 break;
1213
1214         case OP_AI_CHASE:
1215         case OP_AI_GUARD:
1216         case OP_AI_GUARD_WING:
1217         case OP_AI_CHASE_WING:
1218         case OP_AI_EVADE_SHIP:
1219         case OP_AI_STAY_NEAR_SHIP:
1220         case OP_AI_IGNORE:
1221                 aigp->ship_name = ai_get_goal_ship_name( CTEXT(CDR(node)), &aigp->ship_name_index );
1222                 aigp->priority = atoi( CTEXT(CDR(CDR(node))) );
1223
1224                 if ( op == OP_AI_CHASE ) {
1225                         aigp->ai_mode = AI_GOAL_CHASE;
1226
1227                         // in the case of ai_chase (and ai_guard) we must do a wing_name_lookup on the name
1228                         // passed here to see if we could be chasing a wing.  Hoffoss and I have consolidated
1229                         // sexpression operators which makes this step necessary
1230                         if ( wing_name_lookup(aigp->ship_name, 1) != -1 )
1231                                 aigp->ai_mode = AI_GOAL_CHASE_WING;
1232
1233                 } else if ( op == OP_AI_GUARD ) {
1234                         aigp->ai_mode = AI_GOAL_GUARD;
1235                         if ( wing_name_lookup(aigp->ship_name, 1) != -1 )
1236                                 aigp->ai_mode = AI_GOAL_GUARD_WING;
1237
1238                 } else if ( op == OP_AI_EVADE_SHIP ) {
1239                         aigp->ai_mode = AI_GOAL_EVADE_SHIP;
1240
1241                 } else if ( op == OP_AI_GUARD_WING ) {
1242                         aigp->ai_mode = AI_GOAL_GUARD_WING;
1243                 } else if ( op == OP_AI_CHASE_WING ) {
1244                         aigp->ai_mode = AI_GOAL_CHASE_WING;
1245                 } else if ( op == OP_AI_STAY_NEAR_SHIP ) {
1246                         aigp->ai_mode = AI_GOAL_STAY_NEAR_SHIP;
1247                 } else if ( op == OP_AI_IGNORE ) {
1248                         aigp->ai_mode = AI_GOAL_IGNORE;
1249                 } else
1250                         Int3();         // this is impossible
1251
1252                 break;
1253
1254         default:
1255                 Int3();                 // get ALLENDER -- invalid ai-goal specified for ai object!!!!
1256         }
1257
1258         if ( aigp->priority >= PLAYER_PRIORITY_MIN ) {
1259                 nprintf (("AI", "bashing sexpression priority of goal %s from %d to %d.\n", text, aigp->priority, PLAYER_PRIORITY_MIN-1));
1260                 aigp->priority = PLAYER_PRIORITY_MIN-1;
1261         }
1262 }
1263
1264 // adds an ai goal for an individual ship
1265 // type determines who has issues this ship a goal (i.e. the player/mission event/etc)
1266 void ai_add_ship_goal_sexp( int sexp, int type, ai_info *aip )
1267 {
1268         int gindex;
1269
1270         gindex = ai_goal_find_empty_slot( aip->goals );
1271         ai_add_goal_sub_sexp( sexp, type, &aip->goals[gindex] );
1272 }
1273
1274 // code to add ai goals to wings.
1275 void ai_add_wing_goal_sexp(int sexp, int type, int wingnum)
1276 {
1277         int i;
1278         wing *wingp = &Wings[wingnum];
1279
1280         // add the ai goal for any ship that is currently arrived in the game (only if fred isn't running
1281         if ( !Fred_running ) {
1282                 for (i = 0; i < wingp->current_count; i++) {
1283                         int num = wingp->ship_index[i];
1284                         if ( num == -1 )                        // ship must have been destroyed or departed
1285                                 continue;
1286                         ai_add_ship_goal_sexp( sexp, type, &Ai_info[Ships[num].ai_index] );
1287                 }
1288         }
1289
1290         // add the sexpression index into the wing's list of goal sexpressions if
1291         // there are more waves to come
1292         if ((wingp->num_waves - wingp->current_wave > 0) || Fred_running) {
1293                 int gindex;
1294
1295                 gindex = ai_goal_find_empty_slot( wingp->ai_goals );
1296                 ai_add_goal_sub_sexp( sexp, type, &wingp->ai_goals[gindex] );
1297         }
1298 }
1299
1300 // function for internal code to add a goal to a ship.  Needed when the AI finds itself in a situation
1301 // that it must get out of by issuing itself an order.
1302 //
1303 // objp is the object getting the goal
1304 // goal_type is one of AI_GOAL_*
1305 // other_name is a character string objp might act on (for docking, this is a shipname, for guarding
1306 // this name can be a shipname or a wingname)
1307 // docker_point and dockee_point are used for the AI_GOAL_DOCK command to tell two ships where to dock
1308 // immediate means to process this order right away
1309 void ai_add_goal_ship_internal( ai_info *aip, int goal_type, char *name, int docker_point, int dockee_point, int immediate )
1310 {
1311         int gindex;
1312         ai_goal *aigp;
1313
1314         // find an empty slot to put this goal in.
1315         gindex = ai_goal_find_empty_slot( aip->goals );
1316
1317         aigp = &(aip->goals[gindex]);
1318
1319         aigp->signature = Ai_goal_signature++;
1320
1321         aigp->time = Missiontime;
1322         aigp->type = AIG_TYPE_DYNAMIC;
1323         aigp->flags = AIGF_GOAL_OVERRIDE;
1324
1325         switch ( goal_type ) {
1326         case AI_GOAL_DOCK:
1327                 aigp->ship_name = name;
1328                 aigp->docker.index = docker_point;
1329                 aigp->dockee.index = dockee_point;
1330                 aigp->priority = 100;
1331
1332                 aigp->ai_mode = AI_GOAL_DOCK;
1333                 aigp->ai_submode = AIS_DOCK_0;          // be sure to set the submode
1334                 break;
1335
1336         case AI_GOAL_UNDOCK:
1337                 aigp->ship_name = NULL;
1338                 aigp->priority = 100;
1339                 aigp->ai_mode = AI_GOAL_UNDOCK;
1340                 aigp->ai_submode = AIS_UNDOCK_0;
1341                 break;
1342
1343         case AI_GOAL_GUARD:
1344                 aigp->ai_mode = AI_GOAL_GUARD;
1345                 if ( wing_name_lookup(name, 1) != -1 )
1346                         aigp->ai_mode = AI_GOAL_GUARD_WING;
1347                 break;
1348
1349         case AI_GOAL_REARM_REPAIR:
1350                 aigp->ai_mode = AI_GOAL_REARM_REPAIR;
1351                 aigp->ai_submode = 0;
1352                 aigp->ship_name = name;
1353                 aigp->priority = PLAYER_PRIORITY_MIN-1;         // make the priority always less than what the player's is
1354                 aigp->flags &= ~AIGF_GOAL_OVERRIDE;                             // don't override this goal.  rearm repair requests should happen in order
1355                 ai_goal_fixup_dockpoints( aip, aigp );
1356                 break;
1357
1358         default:
1359                 Int3();         // unsupported internal goal -- see Mike K or Mark A.
1360                 break;
1361         }
1362
1363
1364         // process the orders immediately so that these goals take effect right away
1365         if ( immediate )
1366                 ai_process_mission_orders( Ships[aip->shipnum].objnum, aip );
1367 }
1368
1369 // function to add an internal goal to a wing.  Mike K says that the goal doesn't need to persist
1370 // across waves of the wing so we merely need to add the goal to each ship in the wing.  Certain
1371 // goal are simply not valid for wings (like dock, undock).  Immediate parameter gets passed to add_ship_goal
1372 // to say whether or not we should process this goal right away
1373 void ai_add_goal_wing_internal( wing *wingp, int goal_type, char *name, int immediate )
1374 {
1375         int i;
1376
1377         // be sure we are not trying to issue dock or undock goals to wings
1378         Assert ( (goal_type != AI_GOAL_DOCK) || (goal_type != AI_GOAL_UNDOCK) );
1379
1380         for (i = 0; i < wingp->current_count; i++) {
1381                 int num = wingp->ship_index[i];
1382                 if ( num == -1 )                        // ship must have been destroyed or departed
1383                         continue;
1384                 ai_add_goal_ship_internal(  &Ai_info[Ships[num].ai_index], goal_type, name, -1, -1, immediate);
1385         }
1386 }
1387
1388 // this function copies goals from a wing to an ai_info * from a ship.
1389 void ai_copy_mission_wing_goal( ai_goal *aigp, ai_info *aip )
1390 {
1391         int j;
1392
1393         for ( j = 0; j < MAX_AI_GOALS; j++ ) {
1394                 if ( aip->goals[j].ai_mode == AI_GOAL_NONE )
1395                         break;
1396         }
1397         Assert ( j < MAX_AI_GOALS );
1398         aip->goals[j] = *aigp;
1399 }
1400
1401
1402 #define SHIP_STATUS_GONE                                1
1403 #define SHIP_STATUS_NOT_ARRIVED         2
1404 #define SHIP_STATUS_ARRIVED                     3
1405 #define SHIP_STATUS_UNKNOWN                     4
1406
1407 // function to determine if an ai goal is achieveable or not.  Will return
1408 // one of the AI_GOAL_* values.  Also determines is a goal was successful.
1409
1410 int ai_mission_goal_achievable( int objnum, ai_goal *aigp )
1411 {
1412         int status;
1413         char *ai_shipname;
1414         int return_val;
1415         object *objp;
1416         ai_info *aip;
1417
1418         //  these orders are always achievable.
1419         if ( (aigp->ai_mode == AI_GOAL_KEEP_SAFE_DISTANCE) || (aigp->ai_mode == AI_GOAL_WARP)
1420                 || (aigp->ai_mode == AI_GOAL_CHASE_ANY) || (aigp->ai_mode == AI_GOAL_STAY_STILL)
1421                 || (aigp->ai_mode == AI_GOAL_PLAY_DEAD) )
1422                 return AI_GOAL_ACHIEVABLE;
1423
1424         // form on my wing is always achievable, but need to set the override bit so that it
1425         // always gets executed next
1426         if ( aigp->ai_mode == AI_GOAL_FORM_ON_WING ) {
1427                 aigp->flags |= AIGF_GOAL_OVERRIDE;
1428                 return AI_GOAL_ACHIEVABLE;
1429         }
1430
1431         // check to see if we have a valid index.  If not, then try to set one up.  If that
1432         // fails, then we must pitch this order
1433         if ( (aigp->ai_mode == AI_GOAL_WAYPOINTS_ONCE) || (aigp->ai_mode == AI_GOAL_WAYPOINTS) ) {
1434                 if ( aigp->wp_index == -1 ) {
1435                         int i;
1436
1437                         for (i = 0; i < Num_waypoint_lists; i++) {
1438                                 if (!stricmp(aigp->ship_name, Waypoint_lists[i].name)) {
1439                                         aigp->wp_index = i;
1440                                         break;
1441                                 }
1442                         }
1443                         if ( i == Num_waypoint_lists ) {
1444                                 Warning(LOCATION, "Unknown waypoint %s.  not found in mission file.  Killing ai goal", aigp->ship_name );
1445                                 return AI_GOAL_NOT_ACHIEVABLE;
1446                         }
1447                 }
1448                 return AI_GOAL_ACHIEVABLE;
1449         }
1450
1451         objp = &Objects[objnum];
1452         Assert( objp->instance != -1 );
1453         ai_shipname = Ships[objp->instance].ship_name;
1454         aip = &Ai_info[Ships[objp->instance].ai_index];
1455
1456         return_val = AI_GOAL_SATISFIED;
1457
1458         // next, determine if the goal has been completed successfully
1459         switch ( aigp->ai_mode ) {
1460
1461         case AI_GOAL_DOCK:
1462         case AI_GOAL_CHASE_WING:
1463         case AI_GOAL_UNDOCK:
1464                 //status = mission_log_get_time( LOG_SHIP_DOCK, ai_shipname, aigp->ship_name, NULL);
1465                 //status = mission_log_get_time( LOG_SHIP_UNDOCK, ai_shipname, aigp->ship_name, NULL );
1466                 //MWA 3/20/97 -- cannot short circuit a dock or undock goal already succeeded -- we must
1467                 // rely on the goal removal code to just remove this goal.  This is because docking/undock
1468                 // can happen > 1 time per mission per pair of ships.  The above checks will find only
1469                 // if the ships docked or undocked at all, which is not what we want.
1470                 status = 0;
1471                 break;
1472         case AI_GOAL_DESTROY_SUBSYSTEM: {
1473                 int shipnum;
1474                 ship_subsys *ssp;
1475
1476                 // shipnum could be -1 depending on if the ship hasn't arrived or died.  only look for subsystem
1477                 // destroyed when shipnum is valid
1478                 shipnum = ship_name_lookup( aigp->ship_name );
1479
1480                 // can't determine the status of this goal if ship not valid or the subsystem
1481                 // name *is* still valid (meaning we haven't found a valid index yet).
1482                 if ( (shipnum == -1) || (aigp->flags & AIGF_SUBSYS_NAME_VALID) ) {
1483                         status = 0;
1484                         break;
1485                 }
1486
1487                 // if the ship is not in the mission or the subsystem name is still being stored, mark the status
1488                 // as 0 so we can continue.  (The subsystem name must be turned into an index into the ship's subsystems
1489                 // for this goal to be valid).
1490                 Assert ( aigp->ai_submode >= 0 );
1491                 ssp = ship_get_indexed_subsys( &Ships[shipnum], aigp->ai_submode );
1492                 status = mission_log_get_time( LOG_SHIP_SUBSYS_DESTROYED, aigp->ship_name, ssp->system_info->name, NULL );
1493
1494                 break;
1495         }
1496         case AI_GOAL_DISABLE_SHIP:
1497                 status = mission_log_get_time( LOG_SHIP_DISABLED, aigp->ship_name, NULL, NULL );
1498                 break;
1499         case AI_GOAL_DISARM_SHIP:
1500                 status = mission_log_get_time( LOG_SHIP_DISARMED, aigp->ship_name, NULL, NULL );
1501                 break;
1502
1503                 // to guard or ignore a ship, the goal cannot continue if the ship being guarded is either destroyed
1504                 // or has departed.
1505         case AI_GOAL_GUARD:
1506         case AI_GOAL_IGNORE:
1507         case AI_GOAL_EVADE_SHIP:
1508         case AI_GOAL_CHASE:
1509         case AI_GOAL_STAY_NEAR_SHIP:
1510         case AI_GOAL_REARM_REPAIR: {
1511                 int shipnum;
1512
1513                 // MWA -- 4/22/98.  Check for the ship actually being in the mission before
1514                 // checking departure and destroyed.  In multiplayer, since ships can respawn,
1515                 // they get log entries for being destroyed even though they have respawned.
1516                 shipnum = ship_name_lookup( aigp->ship_name );
1517                 if ( shipnum == -1 ) {
1518                         status = mission_log_get_time( LOG_SHIP_DEPART, aigp->ship_name, NULL, NULL);
1519                         if ( !status ) {
1520                                 status = mission_log_get_time( LOG_SHIP_DESTROYED, aigp->ship_name, NULL, NULL);
1521                                 if ( status )
1522                                         return_val = AI_GOAL_NOT_ACHIEVABLE;
1523                         }
1524                 } else {
1525                         status = 0;
1526                 }
1527                 break;
1528         }
1529
1530         case AI_GOAL_GUARD_WING:
1531                 status = mission_log_get_time( LOG_WING_DEPART, aigp->ship_name, NULL, NULL );
1532                 if ( !status ) {
1533                         status = mission_log_get_time( LOG_WING_DESTROYED, aigp->ship_name, NULL, NULL);
1534                         if ( status )
1535                                 return_val = AI_GOAL_NOT_ACHIEVABLE;
1536                 }
1537                 break;
1538
1539                 // the following case statement returns control to caller on all paths!!!!
1540         case AI_GOAL_CHASE_WEAPON:
1541                 // for chase weapon, we simply need to look at the weapon instance that we are trying to
1542                 // attack and see if the object still exists, and has the same signature that we expect.
1543                 if ( Weapons[aigp->wp_index].objnum == -1 )
1544                         return AI_GOAL_NOT_ACHIEVABLE;
1545
1546                 // if the signatures don't match, then goal isn't achievable.
1547                 if ( Objects[Weapons[aigp->wp_index].objnum].signature != aigp->weapon_signature )
1548                         return AI_GOAL_NOT_ACHIEVABLE;
1549
1550                 // otherwise, we should be good to go
1551                 return AI_GOAL_ACHIEVABLE;
1552
1553                 break;
1554
1555         default:
1556                 Int3();
1557                 status = 0;
1558                 break;
1559         }
1560
1561         // if status is true, then the mission log event was found and the goal was satisfied.  return
1562         // AI_GOAL_SATISFIED which should allow this ai object to move onto the next order
1563         if ( status )
1564                 return return_val;
1565
1566         // determine the status of the shipname that this object is acting on.  There are a couple of
1567         // special cases to deal with.  Both the chase wing and undock commands will return from within
1568         // the if statement.
1569         if ( (aigp->ai_mode == AI_GOAL_CHASE_WING) || (aigp->ai_mode == AI_GOAL_GUARD_WING) ) {
1570                 int num = wing_name_lookup( aigp->ship_name );
1571                 wing *wingp = &Wings[num];
1572
1573                 if ( wingp->flags & WF_WING_GONE )
1574                         return AI_GOAL_NOT_ACHIEVABLE;
1575                 else if ( wingp->total_arrived_count == 0 )
1576                         return AI_GOAL_NOT_KNOWN;
1577                 else
1578                         return AI_GOAL_ACHIEVABLE;
1579         } else if ( aigp->ai_mode == AI_GOAL_UNDOCK ) {
1580                         return AI_GOAL_ACHIEVABLE;
1581         } else {
1582                 if ( ship_name_lookup( aigp->ship_name ) != -1 )
1583                         status = SHIP_STATUS_ARRIVED;
1584                 else if ( !mission_parse_ship_arrived(aigp->ship_name) )
1585                         status = SHIP_STATUS_NOT_ARRIVED;
1586                 else if ( ship_find_exited_ship_by_name(aigp->ship_name) != -1 )
1587                         status = SHIP_STATUS_GONE;
1588                 else {
1589                         Int3();         // get ALLENDER
1590                         status = SHIP_STATUS_UNKNOWN;
1591                 }
1592         }
1593
1594         // if the goal is an ignore/disable/disarm goal, then 
1595         if ( (status == SHIP_STATUS_ARRIVED) && (aigp->ai_mode & PURGE_GOALS) && !(aigp->flags & AIGF_GOALS_PURGED) ) {
1596                 ai_goal_purge_all_invalid_goals( aigp );
1597                 aigp->flags |= AIGF_GOALS_PURGED;
1598         }
1599                 
1600
1601         // if we are docking, validate the docking indices on both ships.  We might have to change names to indices.
1602         // only enter this calculation if the ship we are docking with has arrived.  If the ship is gone, then
1603         // this goal will get removed.
1604         if ( (aigp->ai_mode == AI_GOAL_DOCK) && (status == SHIP_STATUS_ARRIVED) ) {
1605                 int index, modelnum, shipnum;
1606                 char docker_name[NAME_LENGTH], dockee_name[NAME_LENGTH];
1607
1608                 // debug code to save off the name of the dockpoints (if they exist).
1609                 docker_name[0] = dockee_name[0] = '\0';
1610                 if ( aigp->flags & AIGF_DOCKER_NAME_VALID ) {
1611                         strcpy(docker_name, aigp->docker.name);
1612                         modelnum = Ships[objp->instance].modelnum;
1613                         index = model_find_dock_name_index(modelnum, aigp->docker.name);
1614                         aigp->docker.index = index;
1615                         aigp->flags &= ~AIGF_DOCKER_NAME_VALID;
1616                 }
1617                 if ( aigp->flags & AIGF_DOCKEE_NAME_VALID ) {
1618                         shipnum = ship_name_lookup(aigp->ship_name);
1619                         if ( shipnum != -1 ) {
1620                                 strcpy(dockee_name, aigp->dockee.name);
1621                                 modelnum = Ships[shipnum].modelnum;
1622                                 index = model_find_dock_name_index(modelnum, aigp->dockee.name);
1623                                 aigp->dockee.index = index;
1624                                 aigp->flags &= ~AIGF_DOCKEE_NAME_VALID;
1625                         } else
1626                                 aigp->dockee.index = -1;                // this will force code into if statement below making goal not achievable.
1627                 }
1628                 if ( (aigp->dockee.index == -1) || (aigp->docker.index == -1) ) {
1629                         Int3();                 // for now, allender wants to know about these things!!!!
1630                         return AI_GOAL_NOT_ACHIEVABLE;
1631                 }
1632
1633                 // we must also determine if this ship which is supposed to dock with something is currently
1634                 // docked with something else.  If so, then return the ON_HOLD until it is not docked anymore
1635                 shipnum = ship_name_lookup(aigp->ship_name);
1636                 Assert( shipnum != -1 );
1637
1638                 // if ship is disabled, dont' know if it can dock or not
1639                 if ( Ships[objp->instance].flags & SF_DISABLED )
1640                         return AI_GOAL_NOT_KNOWN;
1641
1642                 // if the ship that I am supposed to dock with is docked with something else, then I need to put my
1643                 // goal on hold
1644                 //      [MK, 4/23/98: With Mark, we believe this fixes the problem of Comet refusing to warp out after docking with Omega.
1645                 //      This bug occurred only when mission goals were validated in the frame in which Comet docked, which happened about
1646                 // once in 10-20 tries.]
1647                 if ( Ai_info[Ships[shipnum].ai_index].ai_flags & AIF_DOCKED )
1648                         if (aip->dock_objnum != Ships[shipnum].objnum)
1649                                 return AI_GOAL_NOT_KNOWN;
1650
1651                 // if this ship is docked and needs to get docked with something else, then undock this
1652                 // ship
1653                 if ( aip->ai_flags & AIF_DOCKED ) {
1654
1655                         // if we are trying to dock with a different ship, then force this ship to undock
1656                         if ( aip->dock_objnum != Ships[shipnum].objnum ) {
1657                                 // if this goal isn't on hold yet, then issue the undock goal and return NOT_KNOWN
1658                                 // which will then place the goal on hold until the undocking is complete.
1659                                 if ( !(aigp->flags & AIGF_GOAL_ON_HOLD) )
1660                                         ai_add_goal_ship_internal( aip, AI_GOAL_UNDOCK, NULL, -1, -1, 0 );
1661
1662                                 return AI_GOAL_NOT_KNOWN;
1663                         } else {
1664                                 // if this ship is already docked with the guy this order tells him to dock with,
1665                                 // then mark the goal as satisfied.
1666                                 // MWA 2/23/98 -- don't return anything.  Since this item is a goal, the ai_dock code
1667                                 // should remove the goal!!!!
1668                                 //return AI_GOAL_SATISFIED;
1669                         }
1670                 }
1671
1672         } else if ( (aigp->ai_mode == AI_GOAL_DESTROY_SUBSYSTEM) && (status == SHIP_STATUS_ARRIVED) ) {
1673                 // if the ship has arrived, and the goal is destroy subsystem, then check to see that we
1674                 // have fixed up the subsystem name (of the subsystem to destroy) into an index into
1675                 // the ship's subsystem list
1676                 if ( aigp->flags & AIGF_SUBSYS_NAME_VALID ) {
1677                         int shipnum;                    
1678
1679                         shipnum = ship_name_lookup( aigp->ship_name );
1680                         if ( shipnum != -1 ) {
1681                                 aigp->ai_submode = ship_get_subsys_index( &Ships[shipnum], aigp->docker.name );
1682                                 aigp->flags &= ~AIGF_SUBSYS_NAME_VALID;
1683                         } else {
1684                                 Int3();
1685                                 return AI_GOAL_NOT_ACHIEVABLE;                  // force this goal to be invalid
1686                         }
1687                 }
1688         } else if ( (aigp->ai_mode == AI_GOAL_IGNORE) && (status == SHIP_STATUS_ARRIVED) ) {
1689                 int shipnum;
1690                 object *ignored;
1691
1692                 // for ignoring a ship, call the ai_ignore object function, then declare the goal satisfied
1693                 shipnum = ship_name_lookup( aigp->ship_name );
1694                 Assert( shipnum != -1 );                // should be true because of above status
1695                 ignored = &Objects[Ships[shipnum].objnum];
1696                 ai_ignore_object(objp, ignored, 100);
1697                 return AI_GOAL_SATISFIED;
1698         }
1699
1700         switch ( aigp->ai_mode ) {
1701
1702         case AI_GOAL_CHASE:
1703         case AI_GOAL_DOCK:
1704         case AI_GOAL_DESTROY_SUBSYSTEM:
1705         case AI_GOAL_UNDOCK:
1706         case AI_GOAL_GUARD:
1707         case AI_GOAL_GUARD_WING:
1708         case AI_GOAL_DISABLE_SHIP:
1709         case AI_GOAL_DISARM_SHIP:
1710         case AI_GOAL_IGNORE:
1711         case AI_GOAL_EVADE_SHIP:
1712         case AI_GOAL_STAY_NEAR_SHIP:
1713                 if ( status == SHIP_STATUS_ARRIVED )
1714                         return AI_GOAL_ACHIEVABLE;
1715                 else if ( status == SHIP_STATUS_NOT_ARRIVED )
1716                         return AI_GOAL_NOT_KNOWN;
1717                 else if ( status == SHIP_STATUS_GONE )
1718                         return AI_GOAL_NOT_ACHIEVABLE;
1719                 else if ( status == SHIP_STATUS_UNKNOWN )
1720                         return AI_GOAL_NOT_KNOWN;
1721                         Int3();         // get allender -- bad logic
1722                 break;
1723
1724         // for rearm repair ships, a goal is only achievable if the support ship isn't repairing anything
1725         // else at the time, or is set to repair the ship for this goal.  All other goals should be placed
1726         // on hold by returning GOAL_NOT_KNOWN.
1727         case AI_GOAL_REARM_REPAIR: {
1728                 int shipnum;
1729
1730                 // short circuit a couple of cases.  Ship not arrived shouldn't happen.  Ship gone means
1731                 // we mark the goal as not achievable.
1732                 if ( status == SHIP_STATUS_NOT_ARRIVED ) {
1733                         Int3();                                                                         // get Allender.  this shouldn't happen!!!
1734                         return AI_GOAL_NOT_ACHIEVABLE;
1735                 }
1736
1737                 if ( status == SHIP_STATUS_GONE )
1738                         return AI_GOAL_NOT_ACHIEVABLE;
1739
1740                 Assert( aigp->ship_name );
1741                 shipnum = ship_name_lookup( aigp->ship_name );
1742
1743                 // if desitnation currently being repaired, then goal is stil active
1744                 if ( Ai_info[Ships[shipnum].ai_index].ai_flags & AIF_BEING_REPAIRED )
1745                         return AI_GOAL_ACHIEVABLE;
1746
1747                 // if the destination ship is dying or departing (but not completed yet), the mark goal as
1748                 // not achievable.
1749                 if ( Ships[shipnum].flags & (SF_DYING | SF_DEPARTING) )
1750                         return AI_GOAL_NOT_ACHIEVABLE;
1751
1752                 // if the destination object is no longer awaiting repair, then remove the item
1753                 if ( !(Ai_info[Ships[shipnum].ai_index].ai_flags & AIF_AWAITING_REPAIR) )
1754                         return AI_GOAL_NOT_ACHIEVABLE;
1755
1756                 // not repairing anything means that he can do this goal!!!
1757                 if ( !(aip->ai_flags  & AIF_REPAIRING) )
1758                         return AI_GOAL_ACHIEVABLE;
1759
1760                 // test code!!!
1761                 if ( aip->goal_objnum == -1 ) {
1762                         // -- MK, 11/9/97 -- I was always hitting this: Int3();
1763                         return AI_GOAL_ACHIEVABLE;
1764                 }
1765
1766                 // if he is repairing something, he can satisfy his repair goal (his goal_objnum)
1767                 // return GOAL_NOT_KNOWN which is kind of a hack which puts the goal on hold until it can be
1768                 // satisfied.  
1769                 if ( aip->goal_objnum != Ships[shipnum].objnum )
1770                         return AI_GOAL_NOT_KNOWN;
1771
1772                 return AI_GOAL_ACHIEVABLE;
1773         }
1774
1775         default:
1776                 Int3();                 // invalid case in switch:
1777         }
1778
1779         return AI_GOAL_NOT_KNOWN;
1780 }
1781
1782 //      Compare function for system qsort() for sorting ai_goals based on priority.
1783 //      Return values set to sort array in _decreasing_ order.
1784 int ai_goal_priority_compare(const void *a, const void *b)
1785 {
1786         ai_goal *ga, *gb;
1787
1788         ga = (ai_goal *) a;
1789         gb = (ai_goal *) b;
1790
1791         // first, sort based on whether or not the ON_HOLD flag is set for the goal.
1792         // If the flag is set, don't push the goal higher in the list even if priority
1793         // is higher since goal cannot currently be achieved.
1794
1795         if ( (ga->flags & AIGF_GOAL_ON_HOLD) && !(gb->flags & AIGF_GOAL_ON_HOLD) )
1796                 return 1;
1797         else if ( !(ga->flags & AIGF_GOAL_ON_HOLD) && (gb->flags & AIGF_GOAL_ON_HOLD) )
1798                 return -1;
1799
1800         // check whether or not the goal override flag is set.  If it is set, then push this goal higher
1801         // in the list
1802
1803         else if ( (ga->flags & AIGF_GOAL_OVERRIDE) && !(gb->flags & AIGF_GOAL_OVERRIDE) )
1804                 return -1;
1805         else if ( !(ga->flags & AIGF_GOAL_OVERRIDE) && (gb->flags & AIGF_GOAL_OVERRIDE) )
1806                 return 1;
1807
1808         // now normal priority processing
1809
1810         if (ga->priority > gb->priority)
1811                 return -1;
1812         else if ( ga->priority < gb->priority )
1813                 return 1;
1814         else {
1815                 if ( ga->time > gb->time )
1816                         return -1;
1817                 else // if ( ga->time < gb->time )                      // this way prevents element swapping if times happen to be equal (which they should not)
1818                         return 1;
1819         }
1820 }
1821
1822 //      Prioritize goal list.
1823 //      First sort on priority.
1824 //      Then sort on time for goals of equivalent priority.
1825 //      objnum  The object number to act upon.  Redundant with *aip.
1826 //      *aip            The AI info to act upon.  Goals are stored at aip->goals
1827 void prioritize_goals(int objnum, ai_info *aip)
1828 {
1829
1830         //      First sort based on priority field.
1831         qsort(aip->goals, MAX_AI_GOALS, sizeof(ai_goal), ai_goal_priority_compare);
1832
1833 }
1834
1835 //      Scan the list of goals at aip->goals.
1836 //      Remove obsolete goals.
1837 //      objnum  Object of interest.  Redundant with *aip.
1838 //      *aip            contains goals at aip->goals.
1839 void validate_mission_goals(int objnum, ai_info *aip)
1840 {
1841         int     i;
1842         
1843         // loop through all of the goals to determine which goal should be followed.
1844         // This determination will be based on priority, and the time at which it was issued.
1845         for ( i = 0; i < MAX_AI_GOALS; i++ ) {
1846                 int             state;
1847                 ai_goal *aigp;
1848
1849                 aigp = &aip->goals[i];
1850
1851                 // quick check to see if this goal is valid or not, or if we are trying to process the
1852                 // current goal
1853                 if (aigp->ai_mode == AI_GOAL_NONE)
1854                         continue;
1855
1856                 // purge any goals which should get purged
1857                 if ( aigp->flags & AIGF_PURGE ) {
1858                         ai_remove_ship_goal( aip, i );
1859                         continue;
1860                 }
1861
1862                 state = ai_mission_goal_achievable( objnum, aigp );
1863
1864                 // if this order is no longer a valid one, remove it
1865                 if ( (state == AI_GOAL_NOT_ACHIEVABLE) || (state == AI_GOAL_SATISFIED) ) {
1866                         ai_remove_ship_goal( aip, i );
1867                         continue;
1868                 }
1869
1870                 // if the status is achievable, and the on_hold flag is set, clear the flagb
1871                 if ( (state == AI_GOAL_ACHIEVABLE) && (aigp->flags & AIGF_GOAL_ON_HOLD) )
1872                         aigp->flags &= ~AIGF_GOAL_ON_HOLD;
1873
1874                 // if the goal is not known, then set the ON_HOLD flag so that it doesn't get counted as
1875                 // a goal to be pursued
1876                 if ( state == AI_GOAL_NOT_KNOWN )
1877                         aigp->flags |= AIGF_GOAL_ON_HOLD;               // put this goal on hold until it becomes true
1878         }
1879
1880         // if we had an active goal, and that goal is now in hold, make the mode AIM_NONE.  If a new valid
1881         // goal is produced after prioritizing, then the mode will get reset immediately.  Otherwise, setting
1882         // the mode to none will force ship to do default behavior.
1883         if ( (aip->goals[0].ai_mode != AI_GOAL_NONE) && (aip->goals[0].flags & AIGF_GOAL_ON_HOLD) )
1884                 aip->mode = AIM_NONE;
1885
1886         // if the active goal is a rearm/repair goal, the put all other valid goals (which are not repair goals)
1887         // on hold
1888         if ( (aip->goals[0].ai_mode == AI_GOAL_REARM_REPAIR) && (aip->ai_flags & AIF_DOCKED) ) {
1889                 for ( i = 1; i < MAX_AI_GOALS; i++ ) {
1890                         if ( (aip->goals[i].ai_mode == AI_GOAL_NONE) || (aip->goals[i].ai_mode == AI_GOAL_REARM_REPAIR) )
1891                                 continue;
1892                         aip->goals[i].flags |= AIGF_GOAL_ON_HOLD;
1893                 }
1894         }
1895 }
1896
1897 //XSTR:OFF
1898 char *Goal_text[5] = {
1899 "EVENT_SHIP",
1900 "EVENT_WING",
1901 "PLAYER_SHIP",
1902 "PLAYER_WING",
1903 "DYNAMIC",
1904 };
1905 //XSTR:ON
1906
1907 extern char *Mode_text[MAX_AI_BEHAVIORS];
1908
1909 // code to process ai "orders".  Orders include those determined from the mission file and those
1910 // given by the player to a ship that is under his control.  This function gets called for every
1911 // AI object every N seconds through the ai loop.
1912 void ai_process_mission_orders( int objnum, ai_info *aip )
1913 {
1914         object  *objp = &Objects[objnum];
1915         object  *other_obj;
1916         ai_goal *current_goal;
1917         int             wingnum, shipnum;
1918         int             original_signature;
1919
1920 /*      if (!stricmp(Ships[objp->instance].ship_name, "gtt comet")) {
1921                 for (int i=0; i<MAX_AI_GOALS; i++) {
1922                         if (aip->goals[i].signature != -1) {
1923                                 nprintf(("AI", "%6.1f: mode=%s, type=%s, ship=%s\n", f2fl(Missiontime), Mode_text[aip->goals[i].ai_mode], Goal_text[aip->goals[i].type], aip->goals[i].ship_name));
1924                         }
1925                 }
1926                 nprintf(("AI", "\n"));
1927         }
1928 */
1929
1930         // AL 12-12-97: If a ship is entering/leaving a docking bay, wait until path
1931         //                                       following is finished before pursuing goals.
1932         if ( aip->mode == AIM_BAY_EMERGE || aip->mode == AIM_BAY_DEPART ) {
1933                 return;
1934         }
1935
1936         //      Goal #0 is always the active goal, as we maintain a sorted list.
1937         //      Get the signature to see if sorting it again changes it.
1938         original_signature = aip->goals[0].signature;
1939
1940         validate_mission_goals(objnum, aip);
1941
1942         //      Sort the goal array by priority and other factors.
1943         prioritize_goals(objnum, aip);
1944
1945         //      Make sure there's a goal to pursue, else return.
1946         if (aip->goals[0].signature == -1) {
1947                 if (aip->mode == AIM_NONE)
1948                         ai_do_default_behavior(objp);
1949                 return;
1950         }
1951
1952         //      If goal didn't change, return.
1953         if ((aip->active_goal != -1) && (aip->goals[0].signature == original_signature))
1954                 return;
1955
1956         // if the first goal in the list has the ON_HOLD flag, set, there is no current valid goal
1957         // to pursue.
1958         if ( aip->goals[0].flags & AIGF_GOAL_ON_HOLD )
1959                 return;
1960
1961         //      Kind of a hack for now.  active_goal means the goal currently being pursued.
1962         //      It will always be #0 since the list is prioritized.
1963         aip->active_goal = 0;
1964
1965         //nprintf(("AI", "New goal for %s = %i\n", Ships[objp->instance].ship_name, aip->goals[0].ai_mode));
1966
1967         current_goal = &aip->goals[0];
1968
1969         if ( MULTIPLAYER_MASTER ){
1970                 send_ai_info_update_packet( objp, AI_UPDATE_ORDERS );
1971         }
1972
1973         // if this object was flying in formation off of another object, remove the flag that tells him
1974         // to do this.  The form-on-my-wing command is removed from the goal list as soon as it is called, so
1975         // we are safe removing this bit here.
1976         aip->ai_flags &= ~AIF_FORMATION_OBJECT;
1977
1978         // AL 3-7-98: If this is a player ship, and the goal is not a formation goal, then do a quick out
1979         if ( (objp->flags & OF_PLAYER_SHIP) && (current_goal->ai_mode != AI_GOAL_FORM_ON_WING) ) {
1980                 return;
1981         }       
1982
1983         switch ( current_goal->ai_mode ) {
1984
1985         case AI_GOAL_CHASE:
1986                 if ( current_goal->ship_name ) {
1987                         shipnum = ship_name_lookup( current_goal->ship_name );
1988                         Assert (shipnum != -1 );                        // shouldn't get here if this is false!!!!
1989                         other_obj = &Objects[Ships[shipnum].objnum];
1990                 } else
1991                         other_obj = NULL;                                               // we get this case when we tell ship to engage enemy!
1992
1993                 //      Mike -- debug code!
1994                 //      If a ship has a subobject on it, attack that instead of the main ship!
1995                 ai_attack_object( objp, other_obj, current_goal->priority, NULL);
1996                 break;
1997
1998         case AI_GOAL_CHASE_WEAPON:
1999                 Assert( Weapons[current_goal->wp_index].objnum != -1 );
2000                 other_obj = &Objects[Weapons[current_goal->wp_index].objnum];
2001                 ai_attack_object( objp, other_obj, current_goal->priority, NULL );
2002                 break;
2003
2004         case AI_GOAL_GUARD:
2005                 shipnum = ship_name_lookup( current_goal->ship_name );
2006                 Assert (shipnum != -1 );                        // shouldn't get here if this is false!!!!
2007                 other_obj = &Objects[Ships[shipnum].objnum];
2008                 // shipnum and other_obj are the shipnumber and object pointer of the object that you should
2009                 // guard.
2010                 if (objp != other_obj) {
2011                         ai_set_guard_object(objp, other_obj);
2012                         aip->submode_start_time = Missiontime;
2013                 } else {
2014                         mprintf(("Warning: Ship %s told to guard itself.  Goal ignored.\n", Ships[objp->instance].ship_name));
2015                 }
2016                 // -- What the hell is this doing here?? -- MK, 7/30/97 -- ai_do_default_behavior( objp );
2017                 break;
2018
2019         case AI_GOAL_GUARD_WING:
2020                 wingnum = wing_name_lookup( current_goal->ship_name );
2021                 Assert (wingnum != -1 );                        // shouldn't get here if this is false!!!!
2022                 ai_set_guard_wing(objp, wingnum);
2023                 aip->submode_start_time = Missiontime;
2024                 break;
2025
2026         case AI_GOAL_WAYPOINTS:                         // do nothing for waypoints
2027         case AI_GOAL_WAYPOINTS_ONCE: {
2028                 int flags = 0;
2029
2030                 if ( current_goal->ai_mode == AI_GOAL_WAYPOINTS)
2031                         flags |= WPF_REPEAT;
2032                 ai_start_waypoints(objp, current_goal->wp_index, flags);
2033                 break;
2034         }
2035
2036         case AI_GOAL_DOCK: {
2037                 shipnum = ship_name_lookup( current_goal->ship_name );
2038                 Assert (shipnum != -1 );                        // shouldn't get here if this is false!!!!
2039                 other_obj = &Objects[Ships[shipnum].objnum];
2040
2041                 // be sure that we have indices for docking points here!  If we ever had names, they should
2042                 // get fixed up in goal_achievable so that the points can be checked there for validity
2043                 Assert ( !(current_goal->flags & AIGF_DOCKER_NAME_VALID) );
2044                 Assert ( !(current_goal->flags & AIGF_DOCKEE_NAME_VALID) );
2045                 ai_dock_with_object( objp, other_obj, current_goal->priority, AIDO_DOCK, current_goal->docker.index, current_goal->dockee.index );
2046                 aip->submode_start_time = Missiontime;
2047                 break;
2048         }
2049
2050         case AI_GOAL_UNDOCK:
2051                 // try to find the object which which this object is docked with.  Use that object as the
2052                 // "other object" for the undocking proceedure.  If "other object" isn't found, then the undock
2053                 // goal cannot continue.  Spit out a warning and remove the goal.
2054                 other_obj = ai_find_docked_object( objp );
2055                 if ( other_obj == NULL ) {
2056                         //Int3();
2057                         // assume that the guy he was docked with doesn't exist anymore.  (i.e. a cargo containuer
2058                         // can get destroyed while docked with a freighter.)  We should just remove this goal and
2059                         // let this ship pick up it's next goal.
2060                         ai_mission_goal_complete( aip );                // mark as complete, so we can remove it and move on!!!
2061                         break;
2062                 }
2063                 ai_dock_with_object( objp, other_obj, current_goal->priority, AIDO_UNDOCK, 0, 0 );
2064                 aip->submode_start_time = Missiontime;
2065                 break;
2066
2067
2068                 // when destroying a subsystem, we can destroy a specific instance of a subsystem
2069                 // or all instances of a type of subsystem (i.e. a specific engine or all engines).
2070                 // the ai_submode value is > 0 for a specific instance of subsystem and < 0 for all
2071                 // instances of a specific type
2072         case AI_GOAL_DESTROY_SUBSYSTEM:
2073         case AI_GOAL_DISABLE_SHIP:
2074         case AI_GOAL_DISARM_SHIP: {
2075                 shipnum = ship_name_lookup( current_goal->ship_name );
2076                 other_obj = &Objects[Ships[shipnum].objnum];
2077                 ai_attack_object( objp, other_obj, current_goal->priority, NULL);
2078                 ai_set_attack_subsystem( objp, current_goal->ai_submode );              // submode stored the subsystem type
2079                 if (current_goal->ai_mode != AI_GOAL_DESTROY_SUBSYSTEM) {
2080                         if (aip->target_objnum != -1) {
2081                                 //      Only protect if _not_ a capital ship.  We don't want the Lucifer accidentally getting protected.
2082                                 if (!(Ship_info[Ships[shipnum].ship_info_index].flags & SIF_HUGE_SHIP))
2083                                         Objects[aip->target_objnum].flags |= OF_PROTECTED;
2084                         }
2085                 } else  //      Just in case this ship had been protected, unprotect it.
2086                         if (aip->target_objnum != -1)
2087                                 Objects[aip->target_objnum].flags &= ~OF_PROTECTED;
2088
2089                 break;
2090                                                                           }
2091
2092         case AI_GOAL_CHASE_WING:
2093                 wingnum = wing_name_lookup( current_goal->ship_name );
2094                 ai_attack_wing(objp, wingnum, current_goal->priority);
2095                 break;
2096
2097         case AI_GOAL_CHASE_ANY:
2098                 ai_attack_object( objp, NULL, current_goal->priority, NULL );
2099                 break;
2100
2101         case AI_GOAL_WARP: {
2102                 int index;
2103
2104                 index = current_goal->wp_index;
2105                 ai_set_mode_warp_out( objp, aip );
2106                 break;
2107         }
2108
2109         case AI_GOAL_EVADE_SHIP:
2110                 shipnum = ship_name_lookup( current_goal->ship_name );
2111                 other_obj = &Objects[Ships[shipnum].objnum];
2112                 ai_evade_object( objp, other_obj, current_goal->priority );
2113                 break;
2114
2115         case AI_GOAL_STAY_STILL:
2116                 // for now, ignore any other parameters!!!!
2117                 // clear out the object's goals.  Seems to me that if a ship is staying still for a purpose
2118                 // then we need to clear everything out since there is not a real way to get rid of this goal
2119                 // clearing out goals is okay here since we are now what mode to set this AI object to.
2120                 ai_clear_ship_goals( aip );
2121                 ai_stay_still( objp, NULL );
2122                 break;
2123
2124         case AI_GOAL_PLAY_DEAD:
2125                 // if a ship is playing dead, MWA says that it shouldn't try to do anything else.
2126                 // clearing out goals is okay here since we are now what mode to set this AI object to.
2127                 ai_clear_ship_goals( aip );
2128                 aip->mode = AIM_PLAY_DEAD;
2129                 aip->submode = -1;
2130                 break;
2131
2132         case AI_GOAL_FORM_ON_WING:
2133                 // for form on wing, we need to clear out all goals for this ship, and then call the form
2134                 // on wing AI code
2135                 // clearing out goals is okay here since we are now what mode to set this AI object to.
2136                 ai_clear_ship_goals( aip );
2137                 shipnum = ship_name_lookup( current_goal->ship_name );
2138                 other_obj = &Objects[Ships[shipnum].objnum];
2139                 ai_form_on_wing( objp, other_obj );
2140                 break;
2141
2142 // labels for support ship commands
2143
2144         case AI_GOAL_STAY_NEAR_SHIP: {
2145                 shipnum = ship_name_lookup( current_goal->ship_name );
2146                 other_obj = &Objects[Ships[shipnum].objnum];
2147                 // todo MK:  hook to keep support ship near other_obj -- other_obj could be the player object!!!
2148                 float   dist = 300.0f;          //      How far away to stay from ship.  Should be set in SEXP?
2149                 ai_do_stay_near(objp, other_obj, dist);
2150                 break;
2151                                                                                   }
2152
2153         case AI_GOAL_KEEP_SAFE_DISTANCE:
2154                 // todo MK: hook to keep support ship at a safe distance
2155                 break;
2156
2157         case AI_GOAL_REARM_REPAIR:
2158                 shipnum = ship_name_lookup( current_goal->ship_name );
2159                 other_obj = &Objects[Ships[shipnum].objnum];
2160                 ai_rearm_repair( objp, other_obj, current_goal->priority, current_goal->docker.index, current_goal->dockee.index );
2161                 break;
2162
2163         default:
2164                 Int3();
2165                 break;
2166         }
2167
2168 }
2169
2170 void ai_update_goal_references(ai_goal *goals, int type, char *old_name, char *new_name)
2171 {
2172         int i, mode, flag, dummy;
2173
2174         for (i=0; i<MAX_AI_GOALS; i++)  // loop through all the goals in the Ai_info entry
2175         {
2176                 mode = goals[i].ai_mode;
2177                 flag = 0;
2178                 switch (type)
2179                 {
2180                         case REF_TYPE_SHIP:
2181                         case REF_TYPE_PLAYER:
2182                                 switch (mode)
2183                                 {
2184                                         case AI_GOAL_CHASE:
2185                                         case AI_GOAL_DOCK:
2186                                         case AI_GOAL_DESTROY_SUBSYSTEM:
2187                                         case AI_GOAL_GUARD:
2188                                         case AI_GOAL_DISABLE_SHIP:
2189                                         case AI_GOAL_DISARM_SHIP:
2190                                         case AI_GOAL_IGNORE:
2191                                         case AI_GOAL_EVADE_SHIP:
2192                                         case AI_GOAL_STAY_NEAR_SHIP:
2193                                                 flag = 1;
2194                                 }
2195                                 break;
2196
2197                         case REF_TYPE_WING:
2198                                 switch (mode)
2199                                 {
2200                                         case AI_GOAL_CHASE_WING:
2201                                         case AI_GOAL_GUARD_WING:
2202                                                 flag = 1;
2203                                 }
2204                                 break;
2205
2206                         case REF_TYPE_WAYPOINT:
2207                                 switch (mode)
2208                                 {
2209                                         case AI_GOAL_WAYPOINTS:
2210                                         case AI_GOAL_WAYPOINTS_ONCE:
2211                                                 flag = 1;
2212                                 }
2213                                 break;
2214
2215                         case REF_TYPE_PATH:
2216                                 switch (mode)
2217                                 {
2218                                         case AI_GOAL_WAYPOINTS:
2219                                         case AI_GOAL_WAYPOINTS_ONCE:
2220                                         
2221                                                 flag = 1;
2222                                 }
2223                                 break;
2224                 }
2225
2226                 if (flag)  // is this a valid goal to parse for this conversion?
2227                         if (!stricmp(goals[i].ship_name, old_name)) {
2228                                 if (*new_name == '<')  // target was just deleted..
2229                                         goals[i].ai_mode = AI_GOAL_NONE;
2230                                 else
2231                                         goals[i].ship_name = ai_get_goal_ship_name(new_name, &dummy);
2232                         }
2233         }
2234 }
2235
2236 int query_referenced_in_ai_goals(ai_goal *goals, int type, char *name)
2237 {
2238         int i, mode, flag;
2239
2240         for (i=0; i<MAX_AI_GOALS; i++)  // loop through all the goals in the Ai_info entry
2241         {
2242                 mode = goals[i].ai_mode;
2243                 flag = 0;
2244                 switch (type)
2245                 {
2246                         case REF_TYPE_SHIP:
2247                                 switch (mode)
2248                                 {
2249                                         case AI_GOAL_CHASE:
2250                                         case AI_GOAL_DOCK:
2251                                         case AI_GOAL_DESTROY_SUBSYSTEM:
2252                                         case AI_GOAL_GUARD:
2253                                         case AI_GOAL_DISABLE_SHIP:
2254                                         case AI_GOAL_DISARM_SHIP:
2255                                         case AI_GOAL_IGNORE:
2256                                         case AI_GOAL_EVADE_SHIP:
2257                                         case AI_GOAL_STAY_NEAR_SHIP:
2258                                                 flag = 1;
2259                                 }
2260                                 break;
2261
2262                         case REF_TYPE_WING:
2263                                 switch (mode)
2264                                 {
2265                                         case AI_GOAL_CHASE_WING:
2266                                         case AI_GOAL_GUARD_WING:
2267                                                 flag = 1;
2268                                 }
2269                                 break;
2270
2271                         case REF_TYPE_WAYPOINT:
2272                                 switch (mode)
2273                                 {
2274                                         case AI_GOAL_WAYPOINTS:
2275                                         case AI_GOAL_WAYPOINTS_ONCE:
2276                                                 flag = 1;
2277                                 }
2278                                 break;
2279
2280                         case REF_TYPE_PATH:
2281                                 switch (mode)
2282                                 {
2283                                         case AI_GOAL_WAYPOINTS:
2284                                         case AI_GOAL_WAYPOINTS_ONCE:
2285                                                 flag = 1;
2286                                 }
2287                                 break;
2288                 }
2289
2290                 if (flag)  // is this a valid goal to parse for this conversion?
2291                 {
2292                         if (!stricmp(goals[i].ship_name, name))
2293                                 return 1;
2294                 }
2295         }
2296
2297         return 0;
2298 }
2299
2300 char *ai_add_dock_name(char *str)
2301 {
2302         char *ptr;
2303         int i;
2304
2305         Assert(strlen(str) < NAME_LENGTH - 1);
2306         for (i=0; i<Num_ai_dock_names; i++)
2307                 if (!stricmp(Ai_dock_names[i], str))
2308                         return Ai_dock_names[i];
2309
2310         Assert(Num_ai_dock_names < MAX_AI_DOCK_NAMES);
2311         ptr = Ai_dock_names[Num_ai_dock_names++];
2312         strcpy(ptr, str);
2313         return ptr;
2314 }