@@ -40,28 +40,41 @@ struct ship_registry_entry;
4040// dynamic goals are not handled here.
4141
4242// defines for player issued goal priorities
43- # define PLAYER_PRIORITY_MIN 90
44- # define PLAYER_PRIORITY_SHIP 100
45- # define PLAYER_PRIORITY_WING 95
46- # define PLAYER_PRIORITY_SUPPORT_LOW 10
43+ constexpr int PLAYER_PRIORITY_MIN = 90 ;
44+ constexpr int PLAYER_PRIORITY_SHIP = 100 ;
45+ constexpr int PLAYER_PRIORITY_WING = 95 ;
46+ constexpr int PLAYER_PRIORITY_SUPPORT_LOW = 10 ;
4747
48- #define MAX_GOAL_PRIORITY 200
48+ constexpr int MAX_GOAL_PRIORITY = 200 ;
49+ constexpr int MIN_GOAL_PRIORITY = 1 ;
50+
51+
52+ // note: "purging" is not the same as "clearing" here
53+ // purging: any goal that no longer makes sense (i.e. is "invalid") in light of a new goal is removed
54+ // clearing: every goal is removed, including, ironically, the goal that caused the clearing
4955
50- // define for which goals cause other goals to get purged
5156// Goober5000 - okay, this seems really stupid. If any ship in the mission is assigned a goal
5257// in PURGE_GOALS_ALL_SHIPS, *every* other ship will have certain goals purged. So I added
5358// PURGE_GOALS_ONE_SHIP for goals which should only purge other goals in the one ship.
5459// Goober5000 - note that the new disable and disarm goals (AI_GOAL_DISABLE_SHIP_TACTICAL and
5560// AI_GOAL_DISARM_SHIP_TACTICAL) do not purge ANY goals, not even the ones in the one ship
56- [[nodiscard]] bool purge_goals_all_ships (ai_goal_mode ai_mode)
61+ [[nodiscard]] bool causes_invalid_goal_purge_all_ships (ai_goal_mode ai_mode)
5762{
5863 return ai_mode == AI_GOAL_IGNORE || ai_mode == AI_GOAL_DISABLE_SHIP || ai_mode == AI_GOAL_DISARM_SHIP ;
5964}
60- [[nodiscard]] bool purge_goals_one_ship (ai_goal_mode ai_mode)
65+ [[nodiscard]] bool causes_invalid_goal_purge_one_ship (ai_goal_mode ai_mode)
6166{
6267 return ai_mode == AI_GOAL_IGNORE_NEW ;
6368}
6469
70+ // function for which goals cause other goals to be cleared -- see comments above on purging vs clearing
71+ [[nodiscard]] bool causes_goal_clearing (ai_goal_mode ai_mode)
72+ {
73+ return ((ai_mode == AI_GOAL_STAY_STILL && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_stay_still])
74+ || (ai_mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_form_on_wing])
75+ || (ai_mode == AI_GOAL_PLAY_DEAD ));
76+ }
77+
6578// goals given from the player to other ships in the game are also handled in this
6679// code
6780
@@ -212,7 +225,8 @@ void ai_maybe_add_form_goal(wing* wingp)
212225 }
213226
214227 // need to add a form on my wing goal here. Ships are always forming on the player's wing.
215- // it is sufficient enough to check the first goal entry to see if it has a valid goal
228+ // it is sufficient enough to check the first goal entry to see if it has a valid goal.
229+ // Note: Although the player didn't explicitly order this, it is treated as a player-issued order
216230 if (aip->goals [0 ].ai_mode == AI_GOAL_NONE ) {
217231 // Need to have a more specific target in multi, or they may end up trying to target standalone placeholder.
218232 // So form on their team leader. In dogfight, all player-slot ai die, so just exclude.
@@ -601,7 +615,7 @@ void ai_goal_purge_all_invalid_goals(ai_goal *aigp)
601615 ship_obj *sop;
602616
603617 // only purge goals if a new goal is one of the types in next statement
604- if (!purge_goals_all_ships (aigp->ai_mode ))
618+ if (!causes_invalid_goal_purge_all_ships (aigp->ai_mode ))
605619 return ;
606620
607621 for (sop = GET_FIRST (&Ship_obj_list); sop != END_OF_LIST (&Ship_obj_list); sop = GET_NEXT (sop))
@@ -770,10 +784,8 @@ void ai_add_goal_sub_player(ai_goal_type type, ai_goal_mode mode, int submode, c
770784 aigp->target_name = ai_get_goal_target_name ( target_name, &aigp->target_name_index );
771785
772786 // set up the clear-goals flag for certain goals
773- if ((mode == AI_GOAL_STAY_STILL && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_stay_still])
774- || (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_form_on_wing])
775- || (mode == AI_GOAL_PLAY_DEAD ))
776- aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
787+ if (causes_goal_clearing (mode))
788+ aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
777789
778790 // also set up the override, since it's no longer done automatically in ai_mission_goal_achievable
779791 if (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_set_override_when_assigning_form_on_wing])
@@ -812,22 +824,22 @@ void ai_add_goal_sub_player(ai_goal_type type, ai_goal_mode mode, int submode, c
812824// my new docking code. :)
813825int ai_goal_find_empty_slot ( ai_goal *goals, int active_goal )
814826{
815- int oldest_index = -1 , empty_index = -1 ;
827+ int oldest_index = -1 , first_empty_index = -1 ;
816828
817829 for ( int gindex = 0 ; gindex < MAX_AI_GOALS ; gindex++ )
818830 {
819831 // get the index for the first unused goal
820832 if (goals[gindex].ai_mode == AI_GOAL_NONE )
821833 {
822- if (empty_index < 0 )
823- empty_index = gindex;
834+ if (first_empty_index < 0 )
835+ first_empty_index = gindex;
824836 }
825837 // if any goal needs to be purged when we add a goal, set the flag
826838 else if (goals[gindex].flags [AI ::Goal_Flags::Purge_when_new_goal_added])
827839 goals[gindex].flags .set (AI ::Goal_Flags::Purge);
828840
829- // if this is the active goal, don't consider it for pre-emption!!
830- if (gindex == active_goal)
841+ // if this is the active goal, and a real goal, don't consider it for pre-emption!!
842+ if (gindex == active_goal && goals[gindex]. ai_mode != AI_GOAL_NONE )
831843 continue ;
832844
833845 // store the index of the oldest goal
@@ -838,8 +850,8 @@ int ai_goal_find_empty_slot( ai_goal *goals, int active_goal )
838850 }
839851
840852 // try to use the first empty slot
841- if (empty_index >= 0 )
842- return empty_index ;
853+ if (first_empty_index >= 0 )
854+ return first_empty_index ;
843855
844856 // if we didn't find an empty slot, use the oldest goal's slot
845857 return oldest_index;
@@ -874,10 +886,8 @@ void ai_add_goal_sub_scripting(ai_goal_type type, ai_goal_mode mode, int submode
874886 aigp->target_name = ai_get_goal_target_name ( target_name, &aigp->target_name_index );
875887
876888 // set up the clear-goals flag for certain goals
877- if ((mode == AI_GOAL_STAY_STILL && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_stay_still])
878- || (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_assigning_form_on_wing])
879- || (mode == AI_GOAL_PLAY_DEAD ))
880- aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
889+ if (causes_goal_clearing (mode))
890+ aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
881891
882892 // also set up the override, since it's no longer done automatically in ai_mission_goal_achievable
883893 if (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_set_override_when_assigning_form_on_wing])
@@ -886,6 +896,15 @@ void ai_add_goal_sub_scripting(ai_goal_type type, ai_goal_mode mode, int submode
886896 aigp->priority = priority;
887897 aigp->int_data = int_data;
888898 aigp->float_data = float_data;
899+
900+ // range check
901+ if ( aigp->priority > MAX_GOAL_PRIORITY ) {
902+ nprintf ((" AI" , " bashing scripting priority of goal %d from %d to %d.\n " , mode, aigp->priority , MAX_GOAL_PRIORITY ));
903+ aigp->priority = MAX_GOAL_PRIORITY ;
904+ } else if ( aigp->priority < MIN_GOAL_PRIORITY ) {
905+ nprintf ((" AI" , " bashing scripting priority of goal %d from %d to %d.\n " , mode, aigp->priority , MIN_GOAL_PRIORITY ));
906+ aigp->priority = MIN_GOAL_PRIORITY ;
907+ }
889908}
890909
891910void ai_add_ship_goal_scripting (ai_goal_mode mode, int submode, int priority, const char *shipname, ai_info *aip, int int_data, float float_data)
@@ -1266,6 +1285,9 @@ void ai_add_goal_sub_sexp( int sexp, ai_goal_type type, ai_info *aip, ai_goal *a
12661285 } else if ( aigp->priority > MAX_GOAL_PRIORITY ) {
12671286 nprintf ((" AI" , " bashing add-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , aigp->priority , MAX_GOAL_PRIORITY ));
12681287 aigp->priority = MAX_GOAL_PRIORITY ;
1288+ } else if ( aigp->priority < MIN_GOAL_PRIORITY ) {
1289+ nprintf ((" AI" , " bashing add-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , aigp->priority , MIN_GOAL_PRIORITY ));
1290+ aigp->priority = MIN_GOAL_PRIORITY ;
12691291 }
12701292
12711293 // Goober5000 - we now have an extra optional chase argument to allow chasing our own team
@@ -1378,6 +1400,11 @@ int ai_remove_goal_sexp_sub( int sexp, ai_goal* aigp, bool &remove_more )
13781400 nprintf ((" AI" , " bashing remove-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , _priority, MAX_GOAL_PRIORITY ));
13791401 _priority = MAX_GOAL_PRIORITY ;
13801402 }
1403+ else if (_priority < MIN_GOAL_PRIORITY )
1404+ {
1405+ nprintf ((" AI" , " bashing remove-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , _priority, MIN_GOAL_PRIORITY ));
1406+ _priority = MIN_GOAL_PRIORITY ;
1407+ }
13811408
13821409 if (n >= 0 )
13831410 {
@@ -1496,7 +1523,7 @@ int ai_remove_goal_sexp_sub( int sexp, ai_goal* aigp, bool &remove_more )
14961523 goalmode = (op == OP_AI_IGNORE ) ? AI_GOAL_IGNORE : AI_GOAL_IGNORE_NEW ;
14971524 break ;
14981525 case OP_AI_FORM_ON_WING :
1499- priority = eval_priority_et_seq (- 1 , 99 );
1526+ priority = eval_priority_et_seq (CDDR (node), The_mission. ai_profile -> default_form_on_wing_priority );
15001527 goalmode = AI_GOAL_FORM_ON_WING ;
15011528 break ;
15021529 case OP_AI_FLY_TO_SHIP :
@@ -2034,11 +2061,11 @@ ai_achievability ai_mission_goal_achievable( int objnum, ai_goal *aigp )
20342061 // Goober5000 - see note at PURGE_GOALS_ALL_SHIPS... this is bizarre
20352062 if ((status == SHIP_STATUS_ARRIVED ) && !(aigp->flags [AI ::Goal_Flags::Goals_purged]))
20362063 {
2037- if (purge_goals_all_ships (aigp->ai_mode )) {
2064+ if (causes_invalid_goal_purge_all_ships (aigp->ai_mode )) {
20382065 ai_goal_purge_all_invalid_goals (aigp);
20392066 aigp->flags .set (AI ::Goal_Flags::Goals_purged);
20402067 }
2041- else if (purge_goals_one_ship (aigp->ai_mode )) {
2068+ else if (causes_invalid_goal_purge_one_ship (aigp->ai_mode )) {
20422069 ai_goal_purge_invalid_goals (aigp, aip->goals , aip, -1 );
20432070 aigp->flags .set (AI ::Goal_Flags::Goals_purged);
20442071 }
@@ -2395,7 +2422,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
23952422 object *objp = &Objects[objnum];
23962423 object *other_obj;
23972424 ai_goal *current_goal;
2398- int wingnum, shipnum ;
2425+ int wingnum;
23992426 int original_signature;
24002427
24012428/* if (!stricmp(Ships[objp->instance].ship_name, "gtt comet")) {
@@ -2470,12 +2497,14 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
24702497 }
24712498
24722499
2473- // save the current goal (if any) first, in case it's wiped out by the next action
2474- int current_goal_ai_mode = current_goal->ai_mode ;
2475- auto current_goal_target_name = current_goal->target_name ;
2476- auto current_goal_target_ship = current_goal_target_name ? ship_registry_get (current_goal_target_name) : nullptr ;
2500+ std::unique_ptr<ai_goal> current_goal_backup;
2501+ auto current_goal_target_ship = current_goal->target_name ? ship_registry_get (current_goal->target_name ) : nullptr ;
24772502
24782503 if (current_goal->flags [AI ::Goal_Flags::Clear_all_goals_first]) {
2504+ // save the current goal before we wipe it out by clearing everything
2505+ current_goal_backup.reset (new ai_goal (*current_goal));
2506+ current_goal = current_goal_backup.get ();
2507+
24792508 // stay-still, form-on-wing, and play-dead all clear their goals here...
24802509 //
24812510 // clear out the object's goals. Seems to me that if a ship is staying still for a purpose
@@ -2490,10 +2519,10 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
24902519 ai_clear_ship_goals (aip);
24912520 }
24922521
2493- switch ( current_goal_ai_mode ) {
2522+ switch ( current_goal-> ai_mode ) {
24942523
24952524 case AI_GOAL_CHASE :
2496- if (current_goal_target_name ) {
2525+ if (current_goal-> target_name ) {
24972526 Assert (current_goal_target_ship && current_goal_target_ship->has_objp ()); // shouldn't get here if this is false!!!!
24982527 other_obj = current_goal_target_ship->objp ();
24992528 } else
@@ -2526,7 +2555,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25262555 break ;
25272556
25282557 case AI_GOAL_GUARD_WING :
2529- wingnum = wing_name_lookup ( current_goal_target_name );
2558+ wingnum = wing_name_lookup ( current_goal-> target_name );
25302559 Assert (wingnum != -1 ); // shouldn't get here if this is false!!!!
25312560 ai_set_guard_wing (objp, wingnum);
25322561 aip->submode_start_time = Missiontime;
@@ -2535,7 +2564,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25352564 case AI_GOAL_WAYPOINTS : // do nothing for waypoints
25362565 case AI_GOAL_WAYPOINTS_ONCE : {
25372566 int flags = 0 ;
2538- if (current_goal_ai_mode == AI_GOAL_WAYPOINTS )
2567+ if (current_goal-> ai_mode == AI_GOAL_WAYPOINTS )
25392568 flags |= WPF_REPEAT ;
25402569 if (current_goal->flags [AI ::Goal_Flags::Waypoints_in_reverse])
25412570 flags |= WPF_BACKTRACK ;
@@ -2560,7 +2589,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25602589 // goal cannot continue. Spit out a warning and remove the goal.
25612590
25622591 // Goober5000 - do we have a specific ship to undock from?
2563- if (current_goal_target_name )
2592+ if (current_goal-> target_name )
25642593 {
25652594 // hmm, perhaps he was destroyed
25662595 if (!current_goal_target_ship || !current_goal_target_ship->has_objp ())
@@ -2629,7 +2658,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26292658 ai_set_attack_subsystem ( objp, current_goal->ai_submode ); // submode stored the subsystem type
26302659
26312660 // don't protect-ship for tactical goals
2632- if (current_goal_ai_mode != AI_GOAL_DESTROY_SUBSYSTEM && current_goal_ai_mode != AI_GOAL_DISABLE_SHIP_TACTICAL && current_goal_ai_mode != AI_GOAL_DISARM_SHIP_TACTICAL ) {
2661+ if (current_goal-> ai_mode != AI_GOAL_DESTROY_SUBSYSTEM && current_goal-> ai_mode != AI_GOAL_DISABLE_SHIP_TACTICAL && current_goal-> ai_mode != AI_GOAL_DISARM_SHIP_TACTICAL ) {
26332662 if (aip->target_objnum != -1 ) {
26342663 int class_type = Ship_info[current_goal_target_ship->shipp ()->ship_info_index ].class_type ;
26352664 // Only protect if _not_ a capital ship. We don't want the Lucifer accidentally getting protected.
@@ -2644,7 +2673,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26442673 }
26452674
26462675 case AI_GOAL_CHASE_WING :
2647- wingnum = wing_name_lookup ( current_goal_target_name );
2676+ wingnum = wing_name_lookup ( current_goal-> target_name );
26482677 Assertion ( wingnum >= 0 , " The target of AI_GOAL_CHASE_WING must refer to a valid wing!" );
26492678 ai_attack_wing (objp, wingnum);
26502679 break ;
@@ -2656,7 +2685,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26562685 // chase-ship-class is chase-any but restricted to a subset of ships
26572686 case AI_GOAL_CHASE_SHIP_CLASS :
26582687 {
2659- int ship_info_index = ship_info_lookup (current_goal_target_name );
2688+ int ship_info_index = ship_info_lookup (current_goal-> target_name );
26602689 Assertion (ship_info_index >= 0 , " The target of AI_GOAL_CHASE_SHIP_CLASS must refer to a valid ship class!" );
26612690 ai_attack_object (objp, nullptr , ship_info_index);
26622691 break ;
@@ -2733,7 +2762,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
27332762 break ;
27342763
27352764 default :
2736- UNREACHABLE (" unsupported goal of %d found in ai_process_mission_orders. Please report to the SCP" , current_goal_ai_mode );
2765+ UNREACHABLE (" unsupported goal of %d found in ai_process_mission_orders. Please report to the SCP" , current_goal-> ai_mode );
27372766 break ;
27382767 }
27392768
0 commit comments