Skip to content

Commit

Permalink
refactor AIG_TYPE_* into enum classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Goober5000 committed Dec 17, 2024
1 parent 41b7176 commit 487f66d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion code/ai/ai_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace AI {
Dockee_index_valid, // when set, index field for dockee is valid
Goal_on_hold, // when set, this goal cannot currently be satisfied, although it could be in the future
Subsys_needs_fixup, // when set, the subsystem index (for a destroy subsystem goal) is invalid and must be gotten from the subsys name stored in docker.name field!!
Goal_override, // paired with AIG_TYPE_DYNAMIC to mean this goal overrides any other goal
Goal_override, // paired with ai_goal_type::DYNAMIC to mean this goal overrides any other goal
Purge, // purge this goal next time we process
Goals_purged, // this goal has already caused other goals to get purged
Depart_sound_played,// Goober5000 - replacement for AL's hack ;)
Expand Down
8 changes: 3 additions & 5 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4871,12 +4871,10 @@ void ai_fly_to_target_position(const vec3d* target_pos, bool* pl_done_p=NULL, bo
// for itself and in a wing, treat the completion as we would a ship
treat_as_ship = 1;
if ( shipp->wingnum != -1 ) {
int type;

// protect array access from invalid indexes
if ((aip->active_goal >= 0) && (aip->active_goal < MAX_AI_GOALS)) {
type = aip->goals[aip->active_goal].type;
if ( (type == AIG_TYPE_EVENT_WING) || (type == AIG_TYPE_PLAYER_WING) ) {
auto type = aip->goals[aip->active_goal].type;
if ( (type == ai_goal_type::EVENT_WING) || (type == ai_goal_type::PLAYER_WING) ) {
treat_as_ship = 0;
} else {
treat_as_ship = 1;
Expand Down Expand Up @@ -16610,7 +16608,7 @@ void ai_add_rearm_goal( object *requester_objp, object *support_objp )
// ensures that the player get a higher priority!
requester_aip->ai_flags.set(AI::AI_Flags::Awaiting_repair); // Tell that I'm awaiting repair.
if ( requester_objp->flags[Object::Object_Flags::Player_ship] )
ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_REARM_REPAIR, -1, requester_shipp->ship_name, support_aip );
ai_add_ship_goal_player( ai_goal_type::PLAYER_SHIP, AI_GOAL_REARM_REPAIR, -1, requester_shipp->ship_name, support_aip );
else
ai_add_goal_ship_internal( support_aip, AI_GOAL_REARM_REPAIR, requester_shipp->ship_name, -1, -1 );

Expand Down
32 changes: 16 additions & 16 deletions code/ai/aigoals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const char *Ai_goal_text(ai_goal_mode goal, int submode)
* Reset all fields to their uninitialized defaults. But if this is being called before adding a new goal, the function will set the correct signature and time.
* Similarly the added mode, submode, and type can be assigned.
*/
void ai_goal_reset(ai_goal *aigp, bool adding_goal, ai_goal_mode ai_mode, int ai_submode, int type)
void ai_goal_reset(ai_goal *aigp, bool adding_goal, ai_goal_mode ai_mode, int ai_submode, ai_goal_type type)
{
if (ai_mode != AI_GOAL_NONE)
Assertion(adding_goal, "If a goal mode is being assigned, the adding_goal parameter must be true so that the signature and mission time can be set");
Expand Down Expand Up @@ -221,13 +221,13 @@ void ai_maybe_add_form_goal(wing* wingp)
if (Netgame.type_flags & NG_TYPE_TEAM) {
const ship_registry_entry* ship_regp = ship_registry_get(Ships[wingp->ship_index[j]].ship_name);
wingnum = TVT_wings[ship_regp->p_objp()->team];
ai_add_ship_goal_player(AIG_TYPE_PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Ships[Wings[wingnum].ship_index[Wings[wingnum].special_ship]].ship_name, aip);
ai_add_ship_goal_player(ai_goal_type::PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Ships[Wings[wingnum].ship_index[Wings[wingnum].special_ship]].ship_name, aip);
} else {
wingnum = Starting_wings[0];
ai_add_ship_goal_player(AIG_TYPE_PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Ships[Wings[wingnum].ship_index[Wings[wingnum].special_ship]].ship_name, aip);
ai_add_ship_goal_player(ai_goal_type::PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Ships[Wings[wingnum].ship_index[Wings[wingnum].special_ship]].ship_name, aip);
}
} else if (!(Game_mode & GM_MULTIPLAYER)) {
ai_add_ship_goal_player(AIG_TYPE_PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Player_ship->ship_name, aip);
ai_add_ship_goal_player(ai_goal_type::PLAYER_SHIP, AI_GOAL_FORM_ON_WING, -1, Player_ship->ship_name, aip);
}
}
}
Expand Down Expand Up @@ -732,9 +732,9 @@ void ai_goal_fixup_dockpoints(ai_info *aip, ai_goal *aigp)
// from the mission goals (i.e. those goals which come from events) in that we don't
// use sexpressions for goals from the player...so we enumerate all the parameters

void ai_add_goal_sub_player(int type, ai_goal_mode mode, int submode, const char *target_name, ai_goal *aigp, const ai_lua_parameters& lua_target )
void ai_add_goal_sub_player(ai_goal_type type, ai_goal_mode mode, int submode, const char *target_name, ai_goal *aigp, const ai_lua_parameters& lua_target )
{
Assert ( (type == AIG_TYPE_PLAYER_WING) || (type == AIG_TYPE_PLAYER_SHIP) );
Assert ( (type == ai_goal_type::PLAYER_WING) || (type == ai_goal_type::PLAYER_SHIP) );

ai_goal_reset(aigp, true, mode, submode, type);

Expand Down Expand Up @@ -770,7 +770,7 @@ void ai_add_goal_sub_player(int type, ai_goal_mode mode, int submode, const char
else if ( mode == AI_GOAL_FORM_ON_WING )
aigp->priority = PLAYER_PRIORITY_SUPPORT_LOW;

else if ( aigp->type == AIG_TYPE_PLAYER_WING )
else if ( aigp->type == ai_goal_type::PLAYER_WING )

Check failure on line 773 in code/ai/aigoals.cpp

View workflow job for this annotation

GitHub Actions / Linux (Debug, clang-16)

clang-tidy: statement should be inside braces (readability-braces-around-statements)
aigp->priority = PLAYER_PRIORITY_WING; // player wing goals not as high as ship goals
else
aigp->priority = PLAYER_PRIORITY_SHIP;
Expand Down Expand Up @@ -822,9 +822,9 @@ int ai_goal_num(ai_goal *goals)
}


void ai_add_goal_sub_scripting(int type, ai_goal_mode mode, int submode, int priority, const char *target_name, ai_goal *aigp )
void ai_add_goal_sub_scripting(ai_goal_type type, ai_goal_mode mode, int submode, int priority, const char *target_name, ai_goal *aigp )
{
Assert ( (type == AIG_TYPE_PLAYER_WING) || (type == AIG_TYPE_PLAYER_SHIP) );
Assert ( (type == ai_goal_type::PLAYER_WING) || (type == ai_goal_type::PLAYER_SHIP) );

ai_goal_reset(aigp, true, mode, submode, type);

Expand All @@ -846,7 +846,7 @@ void ai_add_ship_goal_scripting(ai_goal_mode mode, int submode, int priority, co

empty_index = ai_goal_find_empty_slot(aip->goals, aip->active_goal);
aigp = &aip->goals[empty_index];
ai_add_goal_sub_scripting(AIG_TYPE_PLAYER_SHIP, mode, submode, priority, shipname, aigp);
ai_add_goal_sub_scripting(ai_goal_type::PLAYER_SHIP, mode, submode, priority, shipname, aigp);

//WMC - hack to get docking setup correctly
if ( mode == AI_GOAL_DOCK ) {
Expand All @@ -863,7 +863,7 @@ void ai_add_ship_goal_scripting(ai_goal_mode mode, int submode, int priority, co
// is issued to ship or wing (from player), mode is AI_GOAL_*. submode is the submode the
// ship should go into. shipname is the object of the action. aip is the ai_info pointer
// of the ship receiving the order
void ai_add_ship_goal_player( int type, ai_goal_mode mode, int submode, const char *shipname, ai_info *aip, const ai_lua_parameters& lua_target)
void ai_add_ship_goal_player(ai_goal_type type, ai_goal_mode mode, int submode, const char *shipname, ai_info *aip, const ai_lua_parameters& lua_target)
{
int empty_index;
ai_goal *aigp;
Expand All @@ -884,7 +884,7 @@ void ai_add_ship_goal_player( int type, ai_goal_mode mode, int submode, const ch

// adds a goal from the player to the given wing (which in turn will add it to the proper
// ships in the wing
void ai_add_wing_goal_player( int type, ai_goal_mode mode, int submode, const char *shipname, int wingnum, const ai_lua_parameters& lua_target)
void ai_add_wing_goal_player(ai_goal_type type, ai_goal_mode mode, int submode, const char *shipname, int wingnum, const ai_lua_parameters& lua_target)
{
int i, empty_index;
wing *wingp = &Wings[wingnum];
Expand All @@ -908,7 +908,7 @@ void ai_add_wing_goal_player( int type, ai_goal_mode mode, int submode, const ch


// common routine to add a sexpression mission goal to the appropriate goal structure.
void ai_add_goal_sub_sexp( int sexp, int type, ai_info *aip, ai_goal *aigp, const char *actor_name )
void ai_add_goal_sub_sexp( int sexp, ai_goal_type type, ai_info *aip, ai_goal *aigp, const char *actor_name )
{
int node, dummy, op;

Expand Down Expand Up @@ -1422,7 +1422,7 @@ void ai_remove_wing_goal_sexp(int sexp, wing *wingp)

// adds an ai goal for an individual ship
// type determines who has issues this ship a goal (i.e. the player/mission event/etc)
void ai_add_ship_goal_sexp( int sexp, int type, ai_info *aip )
void ai_add_ship_goal_sexp( int sexp, ai_goal_type type, ai_info *aip )
{
int gindex;

Expand All @@ -1431,7 +1431,7 @@ void ai_add_ship_goal_sexp( int sexp, int type, ai_info *aip )
}

// code to add ai goals to wings.
void ai_add_wing_goal_sexp(int sexp, int type, wing *wingp)
void ai_add_wing_goal_sexp(int sexp, ai_goal_type type, wing *wingp)
{
int i;

Expand Down Expand Up @@ -1477,7 +1477,7 @@ void ai_add_goal_ship_internal( ai_info *aip, int goal_type, char *name, int /*
aigp = &(aip->goals[gindex]);
ai_goal_reset(aigp, true);

aigp->type = AIG_TYPE_DYNAMIC;
aigp->type = ai_goal_type::DYNAMIC;
aigp->flags.set(AI::Goal_Flags::Goal_override);

switch ( goal_type ) {
Expand Down
30 changes: 17 additions & 13 deletions code/ai/aigoals.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ struct ai_info;

// macros for goals which get set via sexpressions in the mission code

// types of ai goals -- tyese types will help us to determination on which goals should
// types of ai goals -- these will help us to determine which goals should
// have priority over others (i.e. when a player issues a goal to a wing, then a seperate
// goal to a ship in that wing). We would probably use this type in conjunction with
// goal priority to establish which goal to follow
#define AIG_TYPE_EVENT_SHIP 1 // from mission event direct to ship
#define AIG_TYPE_EVENT_WING 2 // from mission event direct to wing
#define AIG_TYPE_PLAYER_SHIP 3 // from player direct to ship
#define AIG_TYPE_PLAYER_WING 4 // from player direct to wing
#define AIG_TYPE_DYNAMIC 5 // created on the fly
enum class ai_goal_type
{
INVALID = 0,
EVENT_SHIP, // from mission event direct to ship
EVENT_WING, // from mission event direct to wing
PLAYER_SHIP, // from player direct to ship
PLAYER_WING, // from player direct to wing
DYNAMIC // created on the fly
};

// IMPORTANT! If you add a new AI_GOAL_x enum, be sure to update the functions
// ai_update_goal_references() and query_referenced_in_ai_goals() or else risk breaking
Expand Down Expand Up @@ -114,7 +118,7 @@ typedef struct ai_goal {
int signature; // Unique identifier. All goals ever created (per mission) have a unique signature.
ai_goal_mode ai_mode; // one of the AI_GOAL_* modes for this goal
int ai_submode; // maybe need a submode
int type; // one of the AIG_TYPE_* values above
ai_goal_type type; // one of the ai_goal_type (originally AIG_TYPE_*) values above
flagset<AI::Goal_Flags> flags; // one of the AIGF_* values above
fix time; // time at which this goal was issued.
int priority; // how important is this goal -- number 0 - 100
Expand Down Expand Up @@ -146,7 +150,7 @@ typedef struct ai_goal {

} ai_goal;

extern void ai_goal_reset(ai_goal *aigp, bool adding_goal = false, ai_goal_mode ai_mode = AI_GOAL_NONE, int ai_submode = -1, int type = -1);
extern void ai_goal_reset(ai_goal *aigp, bool adding_goal = false, ai_goal_mode ai_mode = AI_GOAL_NONE, int ai_submode = -1, ai_goal_type type = ai_goal_type::INVALID);


typedef flag_def_list_templated<ai_goal_mode> ai_goal_list;
Expand All @@ -173,16 +177,16 @@ extern int ai_goal_num(ai_goal *goals);

// adds goals to ships/wing through sexpressions
extern void ai_add_ship_goal_scripting(ai_goal_mode mode, int submode, int priority, const char *shipname, ai_info *aip);
extern void ai_add_ship_goal_sexp( int sexp, int type, ai_info *aip );
extern void ai_add_wing_goal_sexp( int sexp, int type, wing *wingp );
extern void ai_add_goal_sub_sexp( int sexp, int type, ai_info *aip, ai_goal *aigp, const char *actor_name);
extern void ai_add_ship_goal_sexp(int sexp, ai_goal_type type, ai_info *aip);
extern void ai_add_wing_goal_sexp(int sexp, ai_goal_type type, wing *wingp);
extern void ai_add_goal_sub_sexp(int sexp, ai_goal_type type, ai_info *aip, ai_goal *aigp, const char *actor_name);

extern int ai_remove_goal_sexp_sub( int sexp, ai_goal* aigp );
extern void ai_remove_wing_goal_sexp( int sexp, wing *wingp );

// adds goals to ships/sings through player orders
extern void ai_add_ship_goal_player(int type, ai_goal_mode mode, int submode, const char* shipname, ai_info* aip, const ai_lua_parameters& lua_target = { object_ship_wing_point_team(), luacpp::LuaValueList{} });
extern void ai_add_wing_goal_player(int type, ai_goal_mode mode, int submode, const char* shipname, int wingnum, const ai_lua_parameters& lua_target = { object_ship_wing_point_team(), luacpp::LuaValueList{} });
extern void ai_add_ship_goal_player(ai_goal_type type, ai_goal_mode mode, int submode, const char* shipname, ai_info* aip, const ai_lua_parameters& lua_target = { object_ship_wing_point_team(), luacpp::LuaValueList{} });
extern void ai_add_wing_goal_player(ai_goal_type type, ai_goal_mode mode, int submode, const char* shipname, int wingnum, const ai_lua_parameters& lua_target = { object_ship_wing_point_team(), luacpp::LuaValueList{} });

extern void ai_remove_ship_goal( ai_info *aip, int index );
extern void ai_clear_ship_goals( ai_info *aip );
Expand Down
14 changes: 7 additions & 7 deletions code/autopilot/autopilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bool StartAutopilot()
// is support ship trying to rearm-repair
if ( ai_find_goal_index( support_ship_aip->goals, AI_GOAL_REARM_REPAIR ) == -1 ) {
// no, so tell it to depart
ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_WARP, -1, NULL, support_ship_aip );
ai_add_ship_goal_player( ai_goal_type::PLAYER_SHIP, AI_GOAL_WARP, -1, nullptr, support_ship_aip );
} else {
// yes
send_autopilot_msgID(NP_MSG_FAIL_SUPPORT_WORKING);
Expand Down Expand Up @@ -464,12 +464,12 @@ bool StartAutopilot()
{
if (Navs[CurrentNav].flags & NP_WAYPOINT)
{
ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_WAYPOINTS_ONCE, 0, Waypoint_lists[Navs[CurrentNav].target_index].get_name(), &Ai_info[Ships[i].ai_index] );
ai_add_ship_goal_player( ai_goal_type::PLAYER_SHIP, AI_GOAL_WAYPOINTS_ONCE, 0, Waypoint_lists[Navs[CurrentNav].target_index].get_name(), &Ai_info[Ships[i].ai_index] );
//fixup has to wait until after wing goals
}
else
{
ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_FLY_TO_SHIP, 0, Ships[Objects[Navs[CurrentNav].target_index].instance].ship_name, &Ai_info[Ships[i].ai_index] );
ai_add_ship_goal_player( ai_goal_type::PLAYER_SHIP, AI_GOAL_FLY_TO_SHIP, 0, Ships[Objects[Navs[CurrentNav].target_index].instance].ship_name, &Ai_info[Ships[i].ai_index] );
}

}
Expand All @@ -485,15 +485,15 @@ bool StartAutopilot()
{
//ai_add_ship_goal_player( int type, int mode, int submode, char *shipname, ai_info *aip );

//ai_add_wing_goal_player( AIG_TYPE_PLAYER_WING, AI_GOAL_STAY_NEAR_SHIP, 0, target_shipname, wingnum );
//ai_add_wing_goal_player( AIG_TYPE_PLAYER_WING, AI_GOAL_WAYPOINTS_ONCE, 0, target_shipname, wingnum );
//ai_add_wing_goal_player( ai_goal_type::PLAYER_WING, AI_GOAL_STAY_NEAR_SHIP, 0, target_shipname, wingnum );
//ai_add_wing_goal_player( ai_goal_type::PLAYER_WING, AI_GOAL_WAYPOINTS_ONCE, 0, target_shipname, wingnum );
//ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );

ai_clear_wing_goals( &Wings[i] );
if (Navs[CurrentNav].flags & NP_WAYPOINT)
{

ai_add_wing_goal_player( AIG_TYPE_PLAYER_WING, AI_GOAL_WAYPOINTS_ONCE, 0, Waypoint_lists[Navs[CurrentNav].target_index].get_name(), i );
ai_add_wing_goal_player( ai_goal_type::PLAYER_WING, AI_GOAL_WAYPOINTS_ONCE, 0, Waypoint_lists[Navs[CurrentNav].target_index].get_name(), i );

// "fix up" the goal
for (j = 0; j < MAX_AI_GOALS; j++)
Expand All @@ -507,7 +507,7 @@ bool StartAutopilot()
}
else
{
ai_add_wing_goal_player( AIG_TYPE_PLAYER_WING, AI_GOAL_FLY_TO_SHIP, 0, Ships[Objects[Navs[CurrentNav].target_index].instance].ship_name, i );
ai_add_wing_goal_player( ai_goal_type::PLAYER_WING, AI_GOAL_FLY_TO_SHIP, 0, Ships[Objects[Navs[CurrentNav].target_index].instance].ship_name, i );

}
}
Expand Down
4 changes: 2 additions & 2 deletions code/hud/hudsquadmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ int hud_squadmsg_send_ship_command( int shipnum, int command, int send_message,
// handle case of messaging one ship. Deal with messaging all fighters next.
if (ai_mode != AI_GOAL_NONE) {
Assert(ai_submode != -1234567);
ai_add_ship_goal_player(AIG_TYPE_PLAYER_SHIP, ai_mode, ai_submode, target_shipname, &Ai_info[Ships[shipnum].ai_index], lua_target);
ai_add_ship_goal_player(ai_goal_type::PLAYER_SHIP, ai_mode, ai_submode, target_shipname, &Ai_info[Ships[shipnum].ai_index], lua_target);
if (update_history == SQUADMSG_HISTORY_ADD_ENTRY) {
hud_add_issued_order(Ships[shipnum].ship_name, command);
hud_update_last_order(target_shipname, player_num, special_index);
Expand Down Expand Up @@ -1478,7 +1478,7 @@ int hud_squadmsg_send_wing_command( int wingnum, int command, int send_message,

if (ai_mode != AI_GOAL_NONE) {
Assert(ai_submode != -1234567);
ai_add_wing_goal_player(AIG_TYPE_PLAYER_WING, ai_mode, ai_submode, target_shipname, wingnum, lua_target);
ai_add_wing_goal_player(ai_goal_type::PLAYER_WING, ai_mode, ai_submode, target_shipname, wingnum, lua_target);

if (update_history == SQUADMSG_HISTORY_ADD_ENTRY) {
hud_add_issued_order(Wings[wingnum].name, command);
Expand Down
4 changes: 2 additions & 2 deletions code/mission/missionparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,7 @@ int parse_create_object_sub(p_object *p_objp, bool standalone_ship)

// set up the ai goals for this object.
for (sexp = CDR(p_objp->ai_goals); sexp != -1; sexp = CDR(sexp))
ai_add_ship_goal_sexp(sexp, AIG_TYPE_EVENT_SHIP, aip);
ai_add_ship_goal_sexp(sexp, ai_goal_type::EVENT_SHIP, aip);

// free the sexpression nodes only for non-wing ships. wing code will handle its own case
if (p_objp->wingnum < 0)
Expand Down Expand Up @@ -4842,7 +4842,7 @@ void parse_wing(mission *pm)
// this will assign the goals to the wings as well as to any ships in the wing that have been
// already created.
for ( sexp = CDR(wing_goals); sexp != -1; sexp = CDR(sexp) )
ai_add_wing_goal_sexp(sexp, AIG_TYPE_EVENT_WING, wingp); // used by Fred
ai_add_wing_goal_sexp(sexp, ai_goal_type::EVENT_WING, wingp); // used by Fred

free_sexp2(wing_goals); // free up sexp nodes for reuse, since they aren't needed anymore.
}
Expand Down
4 changes: 2 additions & 2 deletions code/parse/sexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13112,7 +13112,7 @@ void sexp_add_goal(int n)
if (!ship_entry->has_shipp())
return; // ship not around anymore???? then forget it!

ai_add_ship_goal_sexp(goal_node, AIG_TYPE_EVENT_SHIP, &(Ai_info[ship_entry->shipp()->ai_index]));
ai_add_ship_goal_sexp(goal_node, ai_goal_type::EVENT_SHIP, &(Ai_info[ship_entry->shipp()->ai_index]));
return;
}

Expand All @@ -13122,7 +13122,7 @@ void sexp_add_goal(int n)
if (wingp->flags[Ship::Wing_Flags::Gone])
return; // wing not around anymore???? then forget it!

ai_add_wing_goal_sexp(goal_node, AIG_TYPE_EVENT_WING, wingp);
ai_add_wing_goal_sexp(goal_node, ai_goal_type::EVENT_WING, wingp);
}
}

Expand Down

0 comments on commit 487f66d

Please sign in to comment.