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