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