Skip to content

Commit

Permalink
Merge pull request scp-fs2open#6104 from Goober5000/locations_to_enums
Browse files Browse the repository at this point in the history
convert arrival/departure locations to enum classes
  • Loading branch information
Goober5000 authored Apr 18, 2024
2 parents a18b122 + 3ad0d37 commit 88bf67a
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 218 deletions.
2 changes: 1 addition & 1 deletion code/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,7 +3063,7 @@ void HudGaugeSupport::render(float /*frametime*/)
renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, outstr);
} else {
if ( Hud_support_objnum == -1 ) {
if (The_mission.support_ships.arrival_location == ARRIVE_FROM_DOCK_BAY)
if (The_mission.support_ships.arrival_location == ArrivalLocation::FROM_DOCK_BAY)
{
strcpy_s(outstr, XSTR( "exiting hangar", 1622));
}
Expand Down
96 changes: 52 additions & 44 deletions code/mission/missionparse.cpp

Large diffs are not rendered by default.

46 changes: 26 additions & 20 deletions code/mission/missionparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

struct wing;
struct p_dock_instance;
enum class ArrivalLocation;
enum class DepartureLocation;

#define NUM_NEBULAS 3 // how many background nebulas we have altogether
#define NUM_NEBULA_COLORS 9
Expand Down Expand Up @@ -86,9 +88,9 @@ extern bool check_for_24_1_data();

// Goober5000
typedef struct support_ship_info {
int arrival_location; // arrival location
ArrivalLocation arrival_location; // arrival location
int arrival_anchor; // arrival anchor
int departure_location; // departure location
DepartureLocation departure_location; // departure location
int departure_anchor; // departure anchor
float max_hull_repair_val; // % of a ship's hull that can be repaired -C
float max_subsys_repair_val; // same thing, except for subsystems -C
Expand Down Expand Up @@ -218,24 +220,28 @@ typedef struct mission {
extern mission The_mission;
extern char Mission_filename[80]; // filename of mission in The_mission (Fred only)

// defines for arrival locations. These defines should match their counterparts in the arrival location
// array
// enums for arrival locations. The integer values of these enums should match their counterparts in the arrival location array
#define MAX_ARRIVAL_NAMES 9
#define ARRIVE_AT_LOCATION 0
#define ARRIVE_NEAR_SHIP 1
#define ARRIVE_IN_FRONT_OF_SHIP 2
#define ARRIVE_IN_BACK_OF_SHIP 3
#define ARRIVE_ABOVE_SHIP 4
#define ARRIVE_BELOW_SHIP 5
#define ARRIVE_TO_LEFT_OF_SHIP 6
#define ARRIVE_TO_RIGHT_OF_SHIP 7
#define ARRIVE_FROM_DOCK_BAY 8

// defines for departure locations. These defines should match their counterparts in the departure location
// array
enum class ArrivalLocation : int
{
AT_LOCATION = 0,
NEAR_SHIP = 1,
IN_FRONT_OF_SHIP = 2,
IN_BACK_OF_SHIP = 3,
ABOVE_SHIP = 4,
BELOW_SHIP = 5,
TO_LEFT_OF_SHIP = 6,
TO_RIGHT_OF_SHIP = 7,
FROM_DOCK_BAY = 8
};

// enums for departure locations. The integer values of these enums should match their counterparts in the departure location array
#define MAX_DEPARTURE_NAMES 2
#define DEPART_AT_LOCATION 0
#define DEPART_AT_DOCK_BAY 1
enum class DepartureLocation : int
{
AT_LOCATION = 0,
TO_DOCK_BAY = 1
};

#define MAX_GOAL_TYPE_NAMES 3

Expand Down Expand Up @@ -356,14 +362,14 @@ class p_object
int initial_hull = 100;
int initial_shields = 100;

int arrival_location = ARRIVE_AT_LOCATION;
ArrivalLocation arrival_location = ArrivalLocation::AT_LOCATION;
int arrival_distance = 0; // used when arrival location is near or in front of some ship
int arrival_anchor = -1; // ship used for anchoring an arrival point
int arrival_path_mask = 0; // Goober5000
int arrival_cue = -1; // Index in Sexp_nodes of this sexp.
int arrival_delay = 0;

int departure_location = DEPART_AT_LOCATION;
DepartureLocation departure_location = DepartureLocation::AT_LOCATION;
int departure_anchor = -1;
int departure_path_mask = 0; // Goober5000
int departure_cue = -1; // Index in Sexp_nodes of this sexp.
Expand Down
70 changes: 31 additions & 39 deletions code/parse/sexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4209,7 +4209,7 @@ int check_sexp_potential_issues(int node, int *bad_node, SCP_string &issue_msg)
if (wingnum >= 0)
{
auto wingp = &Wings[wingnum];
if (wingp->num_waves > 1 && wingp->arrival_location == ARRIVE_FROM_DOCK_BAY)
if (wingp->num_waves > 1 && wingp->arrival_location == ArrivalLocation::FROM_DOCK_BAY)
{
issue_msg = "Wing ";
issue_msg += wingp->name;
Expand Down Expand Up @@ -18862,7 +18862,7 @@ void sexp_ship_create(int n)
mission_log_add_entry(LOG_SHIP_ARRIVED, shipp->ship_name, nullptr, -1, show_in_mission_log ? 0 : MLF_HIDDEN);

if (scripting::hooks::OnShipArrive->isActive()) {
scripting::hooks::OnShipArrive->run(scripting::hooks::ShipArriveConditions{ shipp, ARRIVE_AT_LOCATION, nullptr },
scripting::hooks::OnShipArrive->run(scripting::hooks::ShipArriveConditions{ shipp, ArrivalLocation::AT_LOCATION, nullptr },
scripting::hook_param_list(
scripting::hook_param("Ship", 'o', &Objects[objnum])
));
Expand Down Expand Up @@ -22756,29 +22756,27 @@ void sexp_damage_escort_list(int node)
// Goober5000 - set stuff for mission support ship
void sexp_set_support_ship(int n)
{
int i, temp_val;
int temp_val;
bool is_nan, is_nan_forever;

// get arrival location
temp_val = -1;
for (i = 0; i < MAX_ARRIVAL_NAMES; i++)
{
if (!stricmp(CTEXT(n), Arrival_location_names[i]))
temp_val = i;
}
if (temp_val < 0)
auto arrival_location = ArrivalLocation::AT_LOCATION;
int index = string_lookup(CTEXT(n), Arrival_location_names, MAX_ARRIVAL_NAMES);
if (index >= 0)
arrival_location = static_cast<ArrivalLocation>(index);
else
{
Warning(LOCATION, "Support ship arrival location '%s' not found.\n", CTEXT(n));
return;
}
The_mission.support_ships.arrival_location = temp_val;
The_mission.support_ships.arrival_location = arrival_location;

// get arrival anchor
n = CDR(n);
if (!stricmp(CTEXT(n), "<no anchor>"))
{
// if no anchor, set arrival location to hyperspace
The_mission.support_ships.arrival_location = 0;
The_mission.support_ships.arrival_location = ArrivalLocation::AT_LOCATION;
}
else
{
Expand All @@ -22788,25 +22786,23 @@ void sexp_set_support_ship(int n)

// get departure location
n = CDR(n);
temp_val = -1;
for (i = 0; i < MAX_DEPARTURE_NAMES; i++)
{
if (!stricmp(CTEXT(n), Departure_location_names[i]))
temp_val = i;
}
if (temp_val < 0)
auto departure_location = DepartureLocation::AT_LOCATION;
index = string_lookup(CTEXT(n), Departure_location_names, MAX_DEPARTURE_NAMES);
if (index >= 0)
departure_location = static_cast<DepartureLocation>(index);
else
{
Warning(LOCATION, "Support ship departure location '%s' not found.\n", CTEXT(n));
return;
}
The_mission.support_ships.departure_location = temp_val;
The_mission.support_ships.departure_location = departure_location;

// get departure anchor
n = CDR(n);
if (!stricmp(CTEXT(n), "<no anchor>"))
{
// if no anchor, set departure location to hyperspace
The_mission.support_ships.departure_location = 0;
The_mission.support_ships.departure_location = DepartureLocation::AT_LOCATION;
}
else
{
Expand Down Expand Up @@ -22852,7 +22848,7 @@ void sexp_set_support_ship(int n)
// Goober5000 - set stuff for arriving ships or wings
void sexp_set_arrival_info(int node)
{
int i, arrival_location, arrival_anchor, arrival_mask, arrival_distance, arrival_delay, n = node;
int arrival_anchor, arrival_mask, arrival_distance, arrival_delay, n = node;
bool show_warp, adjust_warp_when_docked, is_nan, is_nan_forever;
object_ship_wing_point_team oswpt;

Expand All @@ -22861,13 +22857,11 @@ void sexp_set_arrival_info(int node)
n = CDR(n);

// get arrival location
arrival_location = -1;
for (i=0; i<MAX_ARRIVAL_NAMES; i++)
{
if (!stricmp(CTEXT(n), Arrival_location_names[i]))
arrival_location = i;
}
if (arrival_location < 0)
auto arrival_location = ArrivalLocation::AT_LOCATION;
int index = string_lookup(CTEXT(n), Arrival_location_names, MAX_ARRIVAL_NAMES);
if (index >= 0)
arrival_location = static_cast<ArrivalLocation>(index);
else
{
Warning(LOCATION, "Arrival location '%s' not found.\n", CTEXT(n));
return;
Expand All @@ -22879,7 +22873,7 @@ void sexp_set_arrival_info(int node)
if ((n < 0) || !stricmp(CTEXT(n), "<no anchor>"))
{
// if no anchor, set arrival location to hyperspace
arrival_location = 0;
arrival_location = ArrivalLocation::AT_LOCATION;
}
else
{
Expand Down Expand Up @@ -22954,7 +22948,7 @@ void sexp_set_arrival_info(int node)
// Goober5000 - set stuff for departing ships or wings
void sexp_set_departure_info(int node)
{
int i, departure_location, departure_anchor, departure_mask, departure_delay, n = node;
int departure_anchor, departure_mask, departure_delay, n = node;
bool show_warp, adjust_warp_when_docked, is_nan, is_nan_forever;
object_ship_wing_point_team oswpt;

Expand All @@ -22963,13 +22957,11 @@ void sexp_set_departure_info(int node)
n = CDR(n);

// get departure location
departure_location = -1;
for (i=0; i<MAX_DEPARTURE_NAMES; i++)
{
if (!stricmp(CTEXT(n), Departure_location_names[i]))
departure_location = i;
}
if (departure_location < 0)
auto departure_location = DepartureLocation::AT_LOCATION;
int index = string_lookup(CTEXT(n), Departure_location_names, MAX_DEPARTURE_NAMES);
if (index >= 0)
departure_location = static_cast<DepartureLocation>(index);
else
{
Warning(LOCATION, "Departure location '%s' not found.\n", CTEXT(n));
return;
Expand All @@ -22981,7 +22973,7 @@ void sexp_set_departure_info(int node)
if ((n < 0) || !stricmp(CTEXT(n), "<no anchor>"))
{
// if no anchor, set departure location to hyperspace
departure_location = 0;
departure_location = DepartureLocation::AT_LOCATION;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion code/scripting/api/libs/mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ ADE_FUNC(createShip,
mission_log_add_entry(LOG_SHIP_ARRIVED, shipp->ship_name, nullptr, -1, show_in_log ? 0 : MLF_HIDDEN);

if (scripting::hooks::OnShipArrive->isActive()) {
scripting::hooks::OnShipArrive->run(scripting::hooks::ShipArriveConditions{ shipp, ARRIVE_AT_LOCATION, nullptr },
scripting::hooks::OnShipArrive->run(scripting::hooks::ShipArriveConditions{ shipp, ArrivalLocation::AT_LOCATION, nullptr },
scripting::hook_param_list(
scripting::hook_param("Ship", 'o', &Objects[obj_idx])
));
Expand Down
7 changes: 4 additions & 3 deletions code/scripting/api/objs/parse_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ ADE_VIRTVAR(Subsystems, l_ParseObject, nullptr, "Get the list of subsystems of t
return ade_set_args(L, "t", tbl);
}

static int parse_object_getset_location_helper(lua_State* L, int p_object::* field, const char* location_type, const char** location_names, size_t location_names_size)
template <typename LOC>
static int parse_object_getset_location_helper(lua_State* L, LOC p_object::* field, const char* location_type, const char** location_names, size_t location_names_size)
{
parse_object_h* poh;
const char* s = nullptr;
Expand All @@ -440,10 +441,10 @@ static int parse_object_getset_location_helper(lua_State* L, int p_object::* fie
Warning(LOCATION, "%s location '%s' not found.", location_type, s);
return ADE_RETURN_NIL;
}
poh->getObject()->*field = location;
poh->getObject()->*field = static_cast<LOC>(location);
}

return ade_set_args(L, "s", location_names[poh->getObject()->*field]);
return ade_set_args(L, "s", location_names[static_cast<int>(poh->getObject()->*field)]);
}

ADE_VIRTVAR(ArrivalLocation, l_ParseObject, "string", "The ship's arrival location", "string", "Arrival location, or nil if handle is invalid")
Expand Down
7 changes: 4 additions & 3 deletions code/scripting/api/objs/ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,8 @@ ADE_VIRTVAR(WaypointSpeedCap, l_Ship, "number", "Waypoint speed cap", "number",
return ade_set_args(L, "i", aip->waypoint_speed_cap);
}

static int ship_getset_location_helper(lua_State* L, int ship::* field, const char* location_type, const char** location_names, size_t location_names_size)
template <typename LOC>
static int ship_getset_location_helper(lua_State* L, LOC ship::* field, const char* location_type, const char** location_names, size_t location_names_size)
{
object_h* objh;
const char* s = nullptr;
Expand All @@ -1189,10 +1190,10 @@ static int ship_getset_location_helper(lua_State* L, int ship::* field, const ch
Warning(LOCATION, "%s location '%s' not found.", location_type, s);
return ADE_RETURN_NIL;
}
shipp->*field = location;
shipp->*field = static_cast<LOC>(location);
}

return ade_set_args(L, "s", location_names[shipp->*field]);
return ade_set_args(L, "s", location_names[static_cast<int>(shipp->*field)]);
}

ADE_VIRTVAR(ArrivalLocation, l_Ship, "string", "The ship's arrival location", "string", "Arrival location, or nil if handle is invalid")
Expand Down
7 changes: 4 additions & 3 deletions code/scripting/api/objs/wing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ ADE_VIRTVAR(TotalVanished, l_Wing, nullptr, "Gets the number of ships that have
return wing_getset_helper(L, &wing::total_vanished);
}

static int wing_getset_location_helper(lua_State* L, int wing::* field, const char* location_type, const char** location_names, size_t location_names_size)
template <typename LOC>
static int wing_getset_location_helper(lua_State* L, LOC wing::* field, const char* location_type, const char** location_names, size_t location_names_size)
{
int wingnum;
const char* s = nullptr;
Expand All @@ -281,10 +282,10 @@ static int wing_getset_location_helper(lua_State* L, int wing::* field, const ch
Warning(LOCATION, "%s location '%s' not found.", location_type, s);
return ADE_RETURN_NIL;
}
Wings[wingnum].*field = location;
Wings[wingnum].*field = static_cast<LOC>(location);
}

return ade_set_args(L, "s", location_names[Wings[wingnum].*field]);
return ade_set_args(L, "s", location_names[static_cast<int>(Wings[wingnum].*field)]);
}

ADE_VIRTVAR(ArrivalLocation, l_Wing, "string", "The wing's arrival location", "string", "Arrival location, or nil if handle is invalid")
Expand Down
3 changes: 2 additions & 1 deletion code/scripting/hook_conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class object;
class ship;
struct weapon;
class ship_subsys;
enum class ArrivalLocation;

#define HOOK_DEFINE_CONDITIONS static const SCP_unordered_map<SCP_string, const std::unique_ptr<const ParseableCondition>> conditions

Expand Down Expand Up @@ -85,7 +86,7 @@ struct ObjectDeathConditions {
struct ShipArriveConditions {
HOOK_DEFINE_CONDITIONS;
const ship* spawned_shipp;
int arrival_location; // As of yet unused
ArrivalLocation arrival_location; // As of yet unused
const object* spawn_anchor_objp; // As of yet unused
};

Expand Down
Loading

0 comments on commit 88bf67a

Please sign in to comment.