Skip to content

Commit 7277fc1

Browse files
authored
Merge pull request #7186 from Goober5000/ship_class_display_name
make ship and weapon class display names SCP_string
2 parents 2e6b38e + b4d9c3e commit 7277fc1

13 files changed

Lines changed: 83 additions & 49 deletions

File tree

code/localization/localize.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,16 +1361,16 @@ void lcl_get_language_name(char *lang_name)
13611361
// For displaying weapon names in german version
13621362
// since we can't actually just change them outright.
13631363
//
1364-
void lcl_translate_wep_name_gr(char *name)
1364+
void lcl_translate_wep_name_gr(SCP_string &name)
13651365
{
1366-
if (!strcmp(name, "Morning Star")) {
1367-
strcpy(name, "Morgenstern");
1368-
} else if (!strcmp(name, "MorningStar")) {
1369-
strcpy(name, "Morgenstern D");
1370-
} else if (!strcmp(name, "UD-8 Kayser")) {
1371-
strcpy(name, "Kayserstrahl");
1372-
} else if (!strcmp(name, "UD-D Kayser")) {
1373-
strcpy(name, "Kayserstrahl");
1366+
if (name == "Morning Star") {
1367+
name = "Morgenstern";
1368+
} else if (name == "MorningStar") {
1369+
name = "Morgenstern D";
1370+
} else if (name == "UD-8 Kayser") {
1371+
name = "Kayserstrahl";
1372+
} else if (name == "UD-D Kayser") {
1373+
name = "Kayserstrahl";
13741374
}
13751375
}
13761376

@@ -1615,10 +1615,10 @@ char buf[128];
16151615
// For displaying ship names in german version in the briefing
16161616
// since we can't actually just change them outright.
16171617
//
1618-
void lcl_translate_ship_name_gr(char *name)
1618+
void lcl_translate_ship_name_gr(SCP_string &name)
16191619
{
1620-
if (!strcmp(name, "GTDR Amazon Advanced")) {
1621-
strcpy(name, "GTDR Amazon VII");
1620+
if (name == "GTDR Amazon Advanced") {
1621+
name = "GTDR Amazon VII";
16221622
}
16231623
}
16241624

code/localization/localize.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ void lcl_ext_localize(const SCP_string &in, SCP_string &out, int *id = nullptr);
130130
// translate the specified string based upon the current language
131131
int lcl_get_xstr_offset(int index, int res);
132132

133-
void lcl_translate_wep_name_gr(char *name);
134-
void lcl_translate_ship_name_gr(char *name);
133+
void lcl_translate_wep_name_gr(SCP_string &name);
134+
void lcl_translate_ship_name_gr(SCP_string &name);
135135
void lcl_translate_brief_icon_name_gr(char *name);
136136
void lcl_translate_brief_icon_name_pl(char *name);
137137
void lcl_translate_targetbox_name_gr(char *name);

code/menuui/techmenu.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,6 @@ void tech_common_render()
497497
memset( buf, 0, sizeof(buf) );
498498
strncpy(buf, Current_list->at(z).name, sizeof(buf) - 1);
499499

500-
if (Lcl_gr && !Disable_built_in_translations)
501-
lcl_translate_ship_name_gr(buf);
502-
503500
font::force_fit_string(buf, 255, Tech_list_coords[gr_screen.res][SHIP_W_COORD]);
504501
gr_string(Tech_list_coords[gr_screen.res][SHIP_X_COORD], Tech_list_coords[gr_screen.res][SHIP_Y_COORD] + y, buf, GR_RESIZE_MENU);
505502

code/parse/parselo.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4475,7 +4475,7 @@ void consolidate_double_characters(char *src, char ch)
44754475
while (*src)
44764476
{
44774477
if (*src == ch && *(src + 1) == ch)
4478-
dest--;
4478+
--dest;
44794479

44804480
++src;
44814481
++dest;
@@ -4485,6 +4485,28 @@ void consolidate_double_characters(char *src, char ch)
44854485
}
44864486
}
44874487

4488+
// Goober5000
4489+
// Used for escape sequences: ## to #, !! to !, etc.
4490+
void consolidate_double_characters(SCP_string &str, char ch)
4491+
{
4492+
auto src = str.begin();
4493+
auto dest = src;
4494+
while (src != str.end())
4495+
{
4496+
if (*src == ch && *(src + 1) == ch)
4497+
--dest;
4498+
4499+
++src;
4500+
++dest;
4501+
4502+
if (src != dest && src != str.end())
4503+
*dest = *src;
4504+
}
4505+
4506+
if (src != dest)
4507+
str.resize(dest - str.begin());
4508+
}
4509+
44884510
char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size)
44894511
{
44904512
Assertion(buffer && source, "Arguments must not be null!");

code/parse/parselo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extern const char *get_pointer_to_first_hash_symbol(const char *src, bool ignore
8484
extern int get_index_of_first_hash_symbol(const SCP_string &src, bool ignore_doubled_hash = false);
8585

8686
extern void consolidate_double_characters(char *str, char ch);
87+
extern void consolidate_double_characters(SCP_string &str, char ch);
8788

8889
// for limiting strings that may be very long; useful for dialog boxes
8990
char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size);

code/scripting/api/objs/shipclass.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,17 +697,17 @@ ADE_VIRTVAR(AltName, l_Shipclass, "string", "Alternate name for ship class", "st
697697
if(idx < 0 || idx >= ship_info_size())
698698
return ade_set_error(L, "s", "");
699699

700-
if(ADE_SETTING_VAR && newName != NULL) {
701-
if (strlen(newName) >= NAME_LENGTH)
702-
{
703-
LuaError(L, "Cannot set alternate name value to '%s' because it is too long, maximum length is %d!", newName, NAME_LENGTH - 1);
704-
return ade_set_error(L, "s", "");
700+
if(ADE_SETTING_VAR && newName != nullptr) {
701+
if (newName == Ship_info[idx].name) {
702+
Ship_info[idx].display_name = "";
703+
Ship_info[idx].flags.remove(Ship::Info_Flags::Has_display_name);
704+
} else {
705+
Ship_info[idx].display_name = newName;
706+
Ship_info[idx].flags.set(Ship::Info_Flags::Has_display_name);
705707
}
706-
707-
strcpy_s(Ship_info[idx].display_name, newName);
708708
}
709709

710-
return ade_set_args(L, "s", Ship_info[idx].display_name);
710+
return ade_set_args(L, "s", Ship_info[idx].display_name.c_str());
711711
}
712712

713713
ADE_VIRTVAR(VelocityMax, l_Shipclass, "vector", "Ship's lateral and forward speeds", "vector", "Maximum velocity, or null vector if handle is invalid")

code/scripting/api/objs/weaponclass.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ ADE_VIRTVAR(AltName, l_Weaponclass, "string", "The alternate weapon class name."
7676
return ade_set_error(L, "s", "");
7777

7878
if(ADE_SETTING_VAR && s != nullptr) {
79-
auto len = sizeof(Weapon_info[idx].display_name);
80-
strncpy(Weapon_info[idx].display_name, s, len);
81-
Weapon_info[idx].display_name[len - 1] = 0;
79+
if (s == Weapon_info[idx].name) {
80+
Weapon_info[idx].display_name = "";
81+
Weapon_info[idx].wi_flags.remove(Weapon::Info_Flags::Has_display_name);
82+
} else {
83+
Weapon_info[idx].display_name = s;
84+
Weapon_info[idx].wi_flags.set(Weapon::Info_Flags::Has_display_name);
85+
}
8286
}
8387

84-
return ade_set_args(L, "s", Weapon_info[idx].display_name);
88+
return ade_set_args(L, "s", Weapon_info[idx].display_name.c_str());
8589
}
8690

8791
ADE_VIRTVAR(TurretName, l_Weaponclass, "string", "The name displayed for a turret if the turret's first weapon is this weapon class.", "string", "Turret name (aka alternate subsystem name), or empty string if handle is invalid")

code/ship/ship.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ const float AWACS_HELP_HULL_LOW = 0.25f; // percent hull at which ship will a
988988
void ship_info::clone(const ship_info& other)
989989
{
990990
strcpy_s(name, other.name);
991-
strcpy_s(display_name, other.display_name);
991+
display_name = other.display_name;
992992
strcpy_s(short_name, other.short_name);
993993
species = other.species;
994994
class_type = other.class_type;
@@ -1725,7 +1725,7 @@ ship_info::ship_info(ship_info&& other) noexcept
17251725
ship_info::ship_info()
17261726
{
17271727
name[0] = '\0';
1728-
display_name[0] = '\0';
1728+
display_name = "";
17291729
sprintf(short_name, "ShipClass%d", ship_info_size());
17301730
species = 0;
17311731
class_type = -1;
@@ -2112,7 +2112,7 @@ ship_info::~ship_info()
21122112
const char* ship_info::get_display_name() const
21132113
{
21142114
if (has_display_name())
2115-
return display_name;
2115+
return display_name.c_str();
21162116
else
21172117
return name;
21182118
}
@@ -2332,15 +2332,24 @@ static void parse_ship(const char *filename, bool replace)
23322332
}
23332333

23342334
if (new_name) {
2335-
if (!sip->flags[Ship::Info_Flags::Has_display_name]) {
2335+
if (!sip->has_display_name()) {
23362336
// if this name has a hash, create a default display name
23372337
if (get_pointer_to_first_hash_symbol(sip->name)) {
2338-
strcpy_s(sip->display_name, sip->name);
2338+
sip->display_name = sip->name;
23392339
end_string_at_first_hash_symbol(sip->display_name);
23402340
sip->flags.set(Ship::Info_Flags::Has_display_name);
23412341
}
23422342
}
23432343

2344+
// do German translation
2345+
if (Lcl_gr && !Disable_built_in_translations) {
2346+
if (!sip->has_display_name()) {
2347+
sip->display_name = sip->name;
2348+
lcl_translate_ship_name_gr(sip->display_name);
2349+
sip->flags.set(Ship::Info_Flags::Has_display_name);
2350+
}
2351+
}
2352+
23442353
sip->animations.changeShipName(sip->name);
23452354
sip->cockpit_animations.changeShipName(sip->name);
23462355
}
@@ -2974,7 +2983,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool
29742983

29752984
if (optional_string("$Alt name:") || optional_string("$Display Name:"))
29762985
{
2977-
stuff_string(sip->display_name, F_NAME, NAME_LENGTH);
2986+
stuff_string(sip->display_name, F_NAME);
29782987
end_string_at_first_hash_symbol(sip->display_name, true);
29792988
consolidate_double_characters(sip->display_name, '#');
29802989
sip->flags.set(Ship::Info_Flags::Has_display_name);

code/ship/ship.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ class ship_info
11441144
{
11451145
public:
11461146
char name[NAME_LENGTH]; // name for the ship
1147-
char display_name[NAME_LENGTH]; // display another name for the ship
1147+
SCP_string display_name; // display another name for the ship
11481148
char short_name[NAME_LENGTH]; // short name, for use in the editor?
11491149
int species; // which species this craft belongs to
11501150
int class_type; //For type table

code/weapon/weapon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ extern SCP_vector<weapon_info> Weapon_info;
363363
struct weapon_info
364364
{
365365
char name[NAME_LENGTH]; // name of this weapon
366-
char display_name[NAME_LENGTH]; // display name of this weapon
366+
SCP_string display_name; // display name of this weapon
367367
char title[WEAPON_TITLE_LEN]; // official title of weapon (used by tooltips)
368368
std::unique_ptr<char[]> desc; // weapon's description (used by tooltips)
369369
char altSubsysName[NAME_LENGTH]; // rename turret to this if this is the turrets first weapon

0 commit comments

Comments
 (0)