@@ -40,28 +40,43 @@ 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+ // NOTE: this function is only evaluated in ai_add_goal_sub_player() and ai_add_goal_sub_scripting(); for
72+ // ai_add_goal_sub_sexp(), the goal clearing is integrated into the logic of the relevant sexp operators
73+ [[nodiscard]] bool causes_goal_clearing (ai_goal_mode ai_mode)
74+ {
75+ return ((ai_mode == AI_GOAL_STAY_STILL && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_running_stay_still])
76+ || (ai_mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_running_form_on_wing])
77+ || (ai_mode == AI_GOAL_PLAY_DEAD ));
78+ }
79+
6580// goals given from the player to other ships in the game are also handled in this
6681// code
6782
@@ -212,7 +227,8 @@ void ai_maybe_add_form_goal(wing* wingp)
212227 }
213228
214229 // 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
230+ // it is sufficient enough to check the first goal entry to see if it has a valid goal.
231+ // Note: Although the player didn't explicitly order this, it is treated as a player-issued order
216232 if (aip->goals [0 ].ai_mode == AI_GOAL_NONE ) {
217233 // Need to have a more specific target in multi, or they may end up trying to target standalone placeholder.
218234 // So form on their team leader. In dogfight, all player-slot ai die, so just exclude.
@@ -600,8 +616,8 @@ void ai_goal_purge_all_invalid_goals(ai_goal *aigp)
600616 int i;
601617 ship_obj *sop;
602618
603- // only purge goals if a new goal is one of the types in next statement
604- if (!purge_goals_all_ships (aigp->ai_mode ))
619+ // only purge goals if a new goal is one of the types that require purging
620+ if (!causes_invalid_goal_purge_all_ships (aigp->ai_mode ))
605621 return ;
606622
607623 for (sop = GET_FIRST (&Ship_obj_list); sop != END_OF_LIST (&Ship_obj_list); sop = GET_NEXT (sop))
@@ -770,10 +786,8 @@ void ai_add_goal_sub_player(ai_goal_type type, ai_goal_mode mode, int submode, c
770786 aigp->target_name = ai_get_goal_target_name ( target_name, &aigp->target_name_index );
771787
772788 // 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_running_stay_still])
774- || (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_running_form_on_wing])
775- || (mode == AI_GOAL_PLAY_DEAD ))
776- aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
789+ if (causes_goal_clearing (mode))
790+ aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
777791
778792 // also set up the override, since it's no longer done automatically in ai_mission_goal_achievable
779793 if (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_set_override_when_assigning_form_on_wing])
@@ -808,22 +822,22 @@ void ai_add_goal_sub_player(ai_goal_type type, ai_goal_mode mode, int submode, c
808822// my new docking code. :)
809823int ai_goal_find_empty_slot ( ai_goal *goals, int active_goal )
810824{
811- int oldest_index = -1 , empty_index = -1 ;
825+ int oldest_index = -1 , first_empty_index = -1 ;
812826
813827 for ( int gindex = 0 ; gindex < MAX_AI_GOALS ; gindex++ )
814828 {
815829 // get the index for the first unused goal
816830 if (goals[gindex].ai_mode == AI_GOAL_NONE )
817831 {
818- if (empty_index < 0 )
819- empty_index = gindex;
832+ if (first_empty_index < 0 )
833+ first_empty_index = gindex;
820834 }
821835 // if any goal needs to be purged when we add a goal, set the flag
822836 else if (goals[gindex].flags [AI ::Goal_Flags::Purge_when_new_goal_added])
823837 goals[gindex].flags .set (AI ::Goal_Flags::Purge);
824838
825- // if this is the active goal, don't consider it for pre-emption!!
826- if (gindex == active_goal)
839+ // if this is the active goal, and a real goal, don't consider it for pre-emption!!
840+ if (gindex == active_goal && goals[gindex]. ai_mode != AI_GOAL_NONE )
827841 continue ;
828842
829843 // store the index of the oldest goal
@@ -834,8 +848,8 @@ int ai_goal_find_empty_slot( ai_goal *goals, int active_goal )
834848 }
835849
836850 // try to use the first empty slot
837- if (empty_index >= 0 )
838- return empty_index ;
851+ if (first_empty_index >= 0 )
852+ return first_empty_index ;
839853
840854 // if we didn't find an empty slot, use the oldest goal's slot
841855 return oldest_index;
@@ -870,10 +884,8 @@ void ai_add_goal_sub_scripting(ai_goal_type type, ai_goal_mode mode, int submode
870884 aigp->target_name = ai_get_goal_target_name ( target_name, &aigp->target_name_index );
871885
872886 // set up the clear-goals flag for certain goals
873- if ((mode == AI_GOAL_STAY_STILL && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_running_stay_still])
874- || (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_clear_goals_when_running_form_on_wing])
875- || (mode == AI_GOAL_PLAY_DEAD ))
876- aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
887+ if (causes_goal_clearing (mode))
888+ aigp->flags .set (AI ::Goal_Flags::Clear_all_goals_first);
877889
878890 // also set up the override, since it's no longer done automatically in ai_mission_goal_achievable
879891 if (mode == AI_GOAL_FORM_ON_WING && !The_mission.ai_profile ->flags [AI ::Profile_Flags::Do_not_set_override_when_assigning_form_on_wing])
@@ -882,6 +894,15 @@ void ai_add_goal_sub_scripting(ai_goal_type type, ai_goal_mode mode, int submode
882894 aigp->priority = priority;
883895 aigp->int_data = int_data;
884896 aigp->float_data = float_data;
897+
898+ // range check
899+ if ( aigp->priority > MAX_GOAL_PRIORITY ) {
900+ nprintf ((" AI" , " bashing scripting priority of goal %d from %d to %d.\n " , mode, aigp->priority , MAX_GOAL_PRIORITY ));
901+ aigp->priority = MAX_GOAL_PRIORITY ;
902+ } else if ( aigp->priority < MIN_GOAL_PRIORITY ) {
903+ nprintf ((" AI" , " bashing scripting priority of goal %d from %d to %d.\n " , mode, aigp->priority , MIN_GOAL_PRIORITY ));
904+ aigp->priority = MIN_GOAL_PRIORITY ;
905+ }
885906}
886907
887908void 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)
@@ -1262,6 +1283,9 @@ void ai_add_goal_sub_sexp( int sexp, ai_goal_type type, ai_info *aip, ai_goal *a
12621283 } else if ( aigp->priority > MAX_GOAL_PRIORITY ) {
12631284 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 ));
12641285 aigp->priority = MAX_GOAL_PRIORITY ;
1286+ } else if ( aigp->priority < MIN_GOAL_PRIORITY ) {
1287+ 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 ));
1288+ aigp->priority = MIN_GOAL_PRIORITY ;
12651289 }
12661290
12671291 // Goober5000 - we now have an extra optional chase argument to allow chasing our own team
@@ -1374,6 +1398,11 @@ int ai_remove_goal_sexp_sub( int sexp, ai_goal* aigp, bool &remove_more )
13741398 nprintf ((" AI" , " bashing remove-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , _priority, MAX_GOAL_PRIORITY ));
13751399 _priority = MAX_GOAL_PRIORITY ;
13761400 }
1401+ else if (_priority < MIN_GOAL_PRIORITY )
1402+ {
1403+ nprintf ((" AI" , " bashing remove-goal sexpression priority of goal %s from %d to %d.\n " , Sexp_nodes[CAR (sexp)].text , _priority, MIN_GOAL_PRIORITY ));
1404+ _priority = MIN_GOAL_PRIORITY ;
1405+ }
13771406
13781407 if (n >= 0 )
13791408 {
@@ -1492,7 +1521,7 @@ int ai_remove_goal_sexp_sub( int sexp, ai_goal* aigp, bool &remove_more )
14921521 goalmode = (op == OP_AI_IGNORE ) ? AI_GOAL_IGNORE : AI_GOAL_IGNORE_NEW ;
14931522 break ;
14941523 case OP_AI_FORM_ON_WING :
1495- priority = eval_priority_et_seq (- 1 , 99 );
1524+ priority = eval_priority_et_seq (CDDR (node), The_mission. ai_profile -> default_form_on_wing_priority );
14961525 goalmode = AI_GOAL_FORM_ON_WING ;
14971526 break ;
14981527 case OP_AI_FLY_TO_SHIP :
@@ -2030,11 +2059,11 @@ ai_achievability ai_mission_goal_achievable( int objnum, ai_goal *aigp )
20302059 // Goober5000 - see note at PURGE_GOALS_ALL_SHIPS... this is bizarre
20312060 if ((status == SHIP_STATUS_ARRIVED ) && !(aigp->flags [AI ::Goal_Flags::Goals_purged]))
20322061 {
2033- if (purge_goals_all_ships (aigp->ai_mode )) {
2062+ if (causes_invalid_goal_purge_all_ships (aigp->ai_mode )) {
20342063 ai_goal_purge_all_invalid_goals (aigp);
20352064 aigp->flags .set (AI ::Goal_Flags::Goals_purged);
20362065 }
2037- else if (purge_goals_one_ship (aigp->ai_mode )) {
2066+ else if (causes_invalid_goal_purge_one_ship (aigp->ai_mode )) {
20382067 ai_goal_purge_invalid_goals (aigp, aip->goals , aip, -1 );
20392068 aigp->flags .set (AI ::Goal_Flags::Goals_purged);
20402069 }
@@ -2391,7 +2420,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
23912420 object *objp = &Objects[objnum];
23922421 object *other_obj;
23932422 ai_goal *current_goal;
2394- int wingnum, shipnum ;
2423+ int wingnum;
23952424 int original_signature;
23962425
23972426/* if (!stricmp(Ships[objp->instance].ship_name, "gtt comet")) {
@@ -2466,12 +2495,14 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
24662495 }
24672496
24682497
2469- // save the current goal (if any) first, in case it's wiped out by the next action
2470- int current_goal_ai_mode = current_goal->ai_mode ;
2471- auto current_goal_target_name = current_goal->target_name ;
2472- auto current_goal_target_ship = current_goal_target_name ? ship_registry_get (current_goal_target_name) : nullptr ;
2498+ std::unique_ptr<ai_goal> current_goal_backup;
2499+ auto current_goal_target_ship = current_goal->target_name ? ship_registry_get (current_goal->target_name ) : nullptr ;
24732500
24742501 if (current_goal->flags [AI ::Goal_Flags::Clear_all_goals_first]) {
2502+ // save the current goal before we wipe it out by clearing everything
2503+ current_goal_backup.reset (new ai_goal (*current_goal));
2504+ current_goal = current_goal_backup.get ();
2505+
24752506 // stay-still, form-on-wing, and play-dead all clear their goals here...
24762507 //
24772508 // clear out the object's goals. Seems to me that if a ship is staying still for a purpose
@@ -2486,10 +2517,10 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
24862517 ai_clear_ship_goals (aip);
24872518 }
24882519
2489- switch ( current_goal_ai_mode ) {
2520+ switch ( current_goal-> ai_mode ) {
24902521
24912522 case AI_GOAL_CHASE :
2492- if (current_goal_target_name ) {
2523+ if (current_goal-> target_name ) {
24932524 Assert (current_goal_target_ship && current_goal_target_ship->has_objp ()); // shouldn't get here if this is false!!!!
24942525 other_obj = current_goal_target_ship->objp ();
24952526 } else
@@ -2522,7 +2553,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25222553 break ;
25232554
25242555 case AI_GOAL_GUARD_WING :
2525- wingnum = wing_name_lookup ( current_goal_target_name );
2556+ wingnum = wing_name_lookup ( current_goal-> target_name );
25262557 Assert (wingnum != -1 ); // shouldn't get here if this is false!!!!
25272558 ai_set_guard_wing (objp, wingnum);
25282559 aip->submode_start_time = Missiontime;
@@ -2531,7 +2562,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25312562 case AI_GOAL_WAYPOINTS : // do nothing for waypoints
25322563 case AI_GOAL_WAYPOINTS_ONCE : {
25332564 int flags = 0 ;
2534- if (current_goal_ai_mode == AI_GOAL_WAYPOINTS )
2565+ if (current_goal-> ai_mode == AI_GOAL_WAYPOINTS )
25352566 flags |= WPF_REPEAT ;
25362567 if (current_goal->flags [AI ::Goal_Flags::Waypoints_in_reverse])
25372568 flags |= WPF_BACKTRACK ;
@@ -2556,7 +2587,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
25562587 // goal cannot continue. Spit out a warning and remove the goal.
25572588
25582589 // Goober5000 - do we have a specific ship to undock from?
2559- if (current_goal_target_name )
2590+ if (current_goal-> target_name )
25602591 {
25612592 // hmm, perhaps he was destroyed
25622593 if (!current_goal_target_ship || !current_goal_target_ship->has_objp ())
@@ -2625,7 +2656,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26252656 ai_set_attack_subsystem ( objp, current_goal->ai_submode ); // submode stored the subsystem type
26262657
26272658 // don't protect-ship for tactical goals
2628- 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 ) {
2659+ 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 ) {
26292660 if (aip->target_objnum != -1 ) {
26302661 int class_type = Ship_info[current_goal_target_ship->shipp ()->ship_info_index ].class_type ;
26312662 // Only protect if _not_ a capital ship. We don't want the Lucifer accidentally getting protected.
@@ -2640,7 +2671,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26402671 }
26412672
26422673 case AI_GOAL_CHASE_WING :
2643- wingnum = wing_name_lookup ( current_goal_target_name );
2674+ wingnum = wing_name_lookup ( current_goal-> target_name );
26442675 Assertion ( wingnum >= 0 , " The target of AI_GOAL_CHASE_WING must refer to a valid wing!" );
26452676 ai_attack_wing (objp, wingnum);
26462677 break ;
@@ -2652,7 +2683,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
26522683 // chase-ship-class is chase-any but restricted to a subset of ships
26532684 case AI_GOAL_CHASE_SHIP_CLASS :
26542685 {
2655- int ship_info_index = ship_info_lookup (current_goal_target_name );
2686+ int ship_info_index = ship_info_lookup (current_goal-> target_name );
26562687 Assertion (ship_info_index >= 0 , " The target of AI_GOAL_CHASE_SHIP_CLASS must refer to a valid ship class!" );
26572688 ai_attack_object (objp, nullptr , ship_info_index);
26582689 break ;
@@ -2729,7 +2760,7 @@ void ai_process_mission_orders( int objnum, ai_info *aip )
27292760 break ;
27302761
27312762 default :
2732- UNREACHABLE (" unsupported goal of %d found in ai_process_mission_orders. Please report to the SCP" , current_goal_ai_mode );
2763+ UNREACHABLE (" unsupported goal of %d found in ai_process_mission_orders. Please report to the SCP" , current_goal-> ai_mode );
27332764 break ;
27342765 }
27352766
0 commit comments