fix NAME FOREIGN UNIT

RENAME FOREIGN UNIT used a somewhat naive approach to check if a
unit still had its generic name. Fixed that.
This commit is contained in:
Steffen Mecke 2015-02-11 16:47:26 +01:00
parent a31898ceb5
commit d416a8eef1
4 changed files with 51 additions and 26 deletions

View File

@ -1461,6 +1461,28 @@ static void createunitid(unit * u, int id)
uhash(u); uhash(u);
} }
void default_name(const unit *u, char name[], int len) {
const char * result;
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
if (lang) {
static const char * prefix[MAXLOCALES];
int i = locale_index(lang);
/*if (!prefix[i]) {*/
prefix[i] = LOC(lang, "unitdefault");
if (!prefix[i]) {
prefix[i] = parameters[P_UNIT];
}
/*}*/
result = prefix[i];
}
else {
result = parameters[P_UNIT];
}
strlcpy(name, result, len);
strlcat(name, " ", len);
strlcat(name, itoa36(u->no), len);
}
void name_unit(unit * u) void name_unit(unit * u)
{ {
if (u_race(u)->generate_name) { if (u_race(u)->generate_name) {
@ -1474,25 +1496,7 @@ void name_unit(unit * u)
} }
else { else {
char name[32]; char name[32];
const char * result; default_name(u, name, sizeof(name));
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
if (lang) {
static const char * prefix[MAXLOCALES];
int i = locale_index(lang);
if (!prefix[i]) {
prefix[i] = LOC(lang, "unitdefault");
if (!prefix[i]) {
prefix[i] = parameters[P_UNIT];
}
}
result = prefix[i];
}
else {
result = parameters[P_UNIT];
}
strlcpy(name, result, sizeof(name));
strlcat(name, " ", sizeof(name));
strlcat(name, itoa36(u->no), sizeof(name));
unit_setname(u, name); unit_setname(u, name);
} }
} }

View File

@ -248,6 +248,7 @@ extern "C" {
struct unit *findunitr(const struct region *r, int n); struct unit *findunitr(const struct region *r, int n);
void default_name(const unit *u, char name[], int len);
const char *unitname(const struct unit *u); const char *unitname(const struct unit *u);
char *write_unitname(const struct unit *u, char *buffer, size_t size); char *write_unitname(const struct unit *u, char *buffer, size_t size);
bool unit_name_equals_race(const struct unit *u); bool unit_name_equals_race(const struct unit *u);

View File

@ -211,6 +211,27 @@ static void test_names(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_default_name(CuTest *tc) {
unit *u;
struct locale* lang;
char buf[32], compare[32];
test_cleanup();
test_create_world();
lang = get_or_create_locale("de");
/* FIXME this has no real effect: default_name uses a static buffer that is initialized in some other test. This sucks. */
locale_setstring(lang, "unitdefault", "Einheit");
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
default_name(u, buf, sizeof(buf));
sprintf(compare, "Einheit %s", itoa36(u->no));
CuAssertStrEquals(tc, compare, buf);
test_cleanup();
}
CuSuite *get_unit_suite(void) CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -224,5 +245,6 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction); SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction);
SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); SUITE_ADD_TEST(suite, test_remove_empty_units_in_region);
SUITE_ADD_TEST(suite, test_names); SUITE_ADD_TEST(suite, test_names);
SUITE_ADD_TEST(suite, test_default_name);
return suite; return suite;
} }

View File

@ -1834,12 +1834,10 @@ int name_cmd(struct unit *u, struct order *ord)
break; break;
} }
else { else {
const char *udefault = LOC(u2->faction->locale, "unitdefault"); char udefault[32];
const char *uname = unit_getname(u2); default_name(u2, udefault, sizeof(udefault));
size_t udlen = strlen(udefault); if (strcmp(unit_getname(u2), udefault) != 0) {
size_t unlen = strlen(uname); cmistake(u, ord, 244, MSG_EVENT);
if (unlen >= udlen && strncmp(uname, udefault, udlen) != 0) {
cmistake(u2, ord, 244, MSG_EVENT);
break; break;
} }
} }