Skip to content

Commit cc9b7ad

Browse files
authored
Merge pull request scp-fs2open#7474 from Goober5000/fix/length_limit
fix wing name length checks
2 parents 15c4141 + 53db82a commit cc9b7ad

3 files changed

Lines changed: 37 additions & 23 deletions

File tree

code/network/multiteamselect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,10 +1723,10 @@ void multi_ts_get_shipname( char *ship_name, int team, int slot_index )
17231723
{
17241724
if ( Netgame.type_flags & NG_TYPE_TEAM ) {
17251725
Assert( (team >= 0) && (team < MULTI_TS_MAX_TVT_TEAMS) );
1726-
wing_bash_ship_name(ship_name, TVT_wing_names[team], slot_index);
1726+
wing_bash_ship_name(ship_name, TVT_wing_names[team], slot_index + 1);
17271727
} else {
17281728
Assert( team == 0 );
1729-
wing_bash_ship_name(ship_name, Starting_wing_names[slot_index / MULTI_TS_NUM_SHIP_SLOTS_TEAM], slot_index % MULTI_TS_NUM_SHIP_SLOTS_TEAM);
1729+
wing_bash_ship_name(ship_name, Starting_wing_names[slot_index / MULTI_TS_NUM_SHIP_SLOTS_TEAM], (slot_index % MULTI_TS_NUM_SHIP_SLOTS_TEAM) + 1);
17301730
}
17311731
}
17321732

code/ship/ship.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15218,42 +15218,46 @@ int get_available_secondary_weapons(object *objp, int *outlist, int *outbanklist
1521815218
return count;
1521915219
}
1522015220

15221-
void wing_bash_ship_name(SCP_string &ship_name, const char *wing_name, int index)
15221+
void wing_bash_ship_name(SCP_string &ship_name, const char *wing_name, int ordinal)
1522215222
{
1522315223
// always create the name this way; display names are handled in the p_object* and ship* functions
15224-
sprintf(ship_name, NOX("%s %d"), wing_name, index);
15224+
sprintf(ship_name, NOX("%s %d"), wing_name, ordinal);
1522515225
}
1522615226

15227-
void wing_bash_ship_name(char *ship_name, const char *wing_name, int index)
15227+
void wing_bash_ship_name(char *ship_name, const char *wing_name, int ordinal)
1522815228
{
15229-
static_assert(MAX_SHIPS_PER_WING < 10, "If two-digit wingman indexes are possible, this function will need to be modified.");
15230-
constexpr size_t max_name_len = NAME_LENGTH - 3;
15229+
char ordinal_str[NAME_LENGTH];
15230+
sprintf(ordinal_str, "%d", ordinal);
15231+
15232+
size_t max_name_len = NAME_LENGTH - 2 - strlen(ordinal_str);
1523115233

1523215234
// truncate name if too long
1523315235
if (strlen(wing_name) > max_name_len)
1523415236
{
1523515237
Warning(LOCATION, "Wing name %s is too long; truncating name for ship", wing_name);
1523615238
strncpy(ship_name, wing_name, max_name_len);
15237-
sprintf(ship_name + max_name_len, NOX("%d"), index);
15239+
ship_name[max_name_len] = '\0';
1523815240
}
1523915241
else
15240-
{
15241-
// always create the name this way; display names are handled in the p_object* and ship* functions
15242-
sprintf(ship_name, NOX("%s %d"), wing_name, index);
15243-
}
15242+
strcpy(ship_name, wing_name);
15243+
15244+
// add the rest
15245+
// always create the name this way; display names are handled in the p_object* and ship* functions
15246+
strcat(ship_name, " ");
15247+
strcat(ship_name, ordinal_str);
1524415248
}
1524515249

15246-
void wing_bash_ship_name(p_object *p_objp, const wing *wingp, int index, bool reset_display_name_if_normal)
15250+
void wing_bash_ship_name(p_object *p_objp, const wing *wingp, int ordinal, bool reset_display_name_if_normal)
1524715251
{
1524815252
// always update the real name
15249-
wing_bash_ship_name(p_objp->name, wingp->name, index);
15253+
wing_bash_ship_name(p_objp->name, wingp->name, ordinal);
1525015254

1525115255
// also set up the display name if we have one
1525215256
// (In the unlikely edge case where the ship already has a display name for some reason, it will be overwritten.
1525315257
// This is unavoidable, because if we didn't overwrite display names, all waves would have the display name from the first wave.)
1525415258
if (wingp->has_display_name())
1525515259
{
15256-
wing_bash_ship_name(p_objp->display_name, wingp->get_display_name(), index);
15260+
wing_bash_ship_name(p_objp->display_name, wingp->get_display_name(), ordinal);
1525715261
p_objp->flags.set(Mission::Parse_Object_Flags::SF_Has_display_name);
1525815262
}
1525915263
else if (reset_display_name_if_normal)
@@ -15263,18 +15267,28 @@ void wing_bash_ship_name(p_object *p_objp, const wing *wingp, int index, bool re
1526315267
}
1526415268
}
1526515269

15266-
void wing_bash_ship_name(ship *shipp, const wing *wingp, int index, bool reset_display_name_if_normal)
15270+
void wing_bash_ship_name(ship *shipp, const wing *wingp, int ordinal, bool reset_display_name_if_normal)
1526715271
{
1526815272
// always update the real name
15269-
wing_bash_ship_name(shipp->ship_name, wingp->name, index);
15273+
wing_bash_ship_name(shipp->ship_name, wingp->name, ordinal);
1527015274

1527115275
// also set up the display name if we have one
1527215276
// (In the unlikely edge case where the ship already has a display name for some reason, it will be overwritten.
1527315277
// This is unavoidable, because if we didn't overwrite display names, all waves would have the display name from the first wave.)
1527415278
if (wingp->has_display_name())
1527515279
{
15276-
wing_bash_ship_name(shipp->display_name, wingp->get_display_name(), index);
15277-
shipp->flags.set(Ship::Ship_Flags::Has_display_name);
15280+
// in FRED, since the wing display name takes precedence, clear the ship display name
15281+
if (Fred_running)
15282+
{
15283+
shipp->display_name = "";
15284+
shipp->flags.remove(Ship::Ship_Flags::Has_display_name);
15285+
}
15286+
// in FS, set up the ship display name based on the wing display name
15287+
else
15288+
{
15289+
wing_bash_ship_name(shipp->display_name, wingp->get_display_name(), ordinal);
15290+
shipp->flags.set(Ship::Ship_Flags::Has_display_name);
15291+
}
1527815292
}
1527915293
else if (reset_display_name_if_normal)
1528015294
{

code/ship/ship.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,10 +1776,10 @@ extern int wing_name_lookup(const char *name, int ignore_count = 0);
17761776
extern bool wing_has_yet_to_arrive(const wing *wingp);
17771777

17781778
// for generating a ship name for arbitrary waves/indexes of that wing
1779-
extern void wing_bash_ship_name(SCP_string &ship_name, const char *wing_name, int index);
1780-
extern void wing_bash_ship_name(char *ship_name, const char *wing_name, int index);
1781-
extern void wing_bash_ship_name(p_object *p_objp, const wing *wingp, int index, bool reset_display_name_if_normal = false);
1782-
extern void wing_bash_ship_name(ship *shipp, const wing *wingp, int index, bool reset_display_name_if_normal = false);
1779+
extern void wing_bash_ship_name(SCP_string &ship_name, const char *wing_name, int ordinal);
1780+
extern void wing_bash_ship_name(char *ship_name, const char *wing_name, int ordinal);
1781+
extern void wing_bash_ship_name(p_object *p_objp, const wing *wingp, int ordinal, bool reset_display_name_if_normal = false);
1782+
extern void wing_bash_ship_name(ship *shipp, const wing *wingp, int ordinal, bool reset_display_name_if_normal = false);
17831783
extern int Player_ship_class;
17841784

17851785
// Do the special effect for energy dissipating into the shield for a hit.

0 commit comments

Comments
 (0)