diff --git a/src/kernel/save.c b/src/kernel/save.c index c1140e9ca..153829ace 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -642,9 +642,6 @@ unit *read_unit(struct gamedata *data) READ_INT(data->store, &number); set_number(u, number); - - - READ_INT(data->store, &n); u->age = (short)n; @@ -1711,6 +1708,13 @@ int readgame(const char *filename, int backup) unit *u = read_unit(&gdata); sc_mage *mage; + if (gdata.version < AUTO_RACENAME_VERSION) { + if (u->name && fval(u->faction, FFL_NPC)) { + if (unit_name_equals_race(u)) { + unit_setname(u, NULL); + } + } + } assert(u->region == NULL); u->region = r; *up = u; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index ddc1e0dc7..2170b888e 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1853,8 +1853,9 @@ char *write_unitname(const unit * u, char *buffer, size_t size) if (u->name) { slprintf(buffer, size, "%s (%s)", u->name, itoa36(u->no)); } else { + const struct locale * lang = u->faction ? u->faction->locale : default_locale; const char * name = rc_name_s(u->_race, u->number == 1 ? NAME_SINGULAR : NAME_PLURAL); - slprintf(buffer, size, "%s (%s)", locale_string(u->faction->locale, name), itoa36(u->no)); + slprintf(buffer, size, "%s (%s)", locale_string(lang, name), itoa36(u->no)); } buffer[size - 1] = 0; return buffer; @@ -1866,15 +1867,17 @@ const char *unitname(const unit * u) return write_unitname(u, ubuf, sizeof(name)); } -void update_monster_name(unit *u) { - char sing[32], plur[32]; - const struct locale *lang = u->faction->locale; - if (!u->name) return; - rc_name(u->_race, NAME_SINGULAR, sing, sizeof(sing)); - rc_name(u->_race, NAME_PLURAL, plur, sizeof(plur)); - if (strcmp(u->name, sing) == 0 || strcmp(u->name, plur) == 0 || - strcmp(u->name, locale_string(lang, sing)) == 0 || - strcmp(u->name, locale_string(lang, plur)) == 0) { - unit_setname(u, 0); +bool unit_name_equals_race(const unit *u) { + if (u->name) { + char sing[32], plur[32]; + const struct locale *lang = u->faction->locale; + rc_name(u->_race, NAME_SINGULAR, sing, sizeof(sing)); + rc_name(u->_race, NAME_PLURAL, plur, sizeof(plur)); + if (strcmp(u->name, sing) == 0 || strcmp(u->name, plur) == 0 || + strcmp(u->name, locale_string(lang, sing)) == 0 || + strcmp(u->name, locale_string(lang, plur)) == 0) { + return true; + } } + return false; } diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 80fafae91..bd5ffd493 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -249,7 +249,7 @@ extern "C" { const char *unitname(const struct unit *u); char *write_unitname(const struct unit *u, char *buffer, size_t size); - void update_monster_name(struct unit *u); + bool unit_name_equals_race(const struct unit *u); #ifdef __cplusplus } diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 8af5504f8..76d708721 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -175,16 +175,22 @@ static void test_update_monster_name(CuTest *tc) { locale_setstring(lang, rc_name_s(u->_race, NAME_PLURAL), "Menschen"); unit_setname(u, "Hodor"); - update_monster_name(u); - CuAssertStrEquals(tc, "Hodor", u->name); + CuAssertTrue(tc, !unit_name_equals_race(u)); + + unit_setname(u, "Menschling"); + CuAssertTrue(tc, !unit_name_equals_race(u)); + + unit_setname(u, rc_name_s(u->_race, NAME_SINGULAR)); + CuAssertTrue(tc, unit_name_equals_race(u)); + + unit_setname(u, rc_name_s(u->_race, NAME_PLURAL)); + CuAssertTrue(tc, unit_name_equals_race(u)); unit_setname(u, "Mensch"); - update_monster_name(u); - CuAssertPtrEquals(tc, 0, u->name); + CuAssertTrue(tc, unit_name_equals_race(u)); unit_setname(u, "Menschen"); - update_monster_name(u); - CuAssertPtrEquals(tc, 0, u->name); + CuAssertTrue(tc, unit_name_equals_race(u)); test_cleanup(); } diff --git a/src/kernel/version.h b/src/kernel/version.h index 42f653070..e21c2c74b 100644 --- a/src/kernel/version.h +++ b/src/kernel/version.h @@ -73,8 +73,8 @@ #define INTFLAGS_VERSION 342 /* turn 876, FFL_NPC is now bit 25, flags is an int */ #define SAVEGAMEID_VERSION 343 /* instead of XMLNAME, save the game.id parameter from the config */ #define BUILDNO_VERSION 344 /* storing the build number in the save */ - +#define AUTO_RACENAME_VERSION 345 /* NPC units with name==NULL will automatically get their race for a name */ #define MIN_VERSION CURSETYPE_VERSION /* minimal datafile we support */ -#define RELEASE_VERSION BUILDNO_VERSION /* current datafile */ +#define RELEASE_VERSION AUTO_RACENAME_VERSION /* current datafile */ #define STREAM_VERSION 2 /* internal encoding of binary files */