diff --git a/src/bind_unit.c b/src/bind_unit.c index e18a81d8a..6c61b2beb 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -167,7 +167,7 @@ static int tolua_unit_set_group(lua_State * L) static int tolua_unit_get_name(lua_State * L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); - tolua_pushstring(L, self->name); + tolua_pushstring(L, unit_getname(self)); return 1; } diff --git a/src/creport.c b/src/creport.c index fc823284f..845adf864 100644 --- a/src/creport.c +++ b/src/creport.c @@ -758,7 +758,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f, assert(u && u->number); fprintf(F, "EINHEIT %d\n", u->no); - fprintf(F, "\"%s\";Name\n", u->name); + fprintf(F, "\"%s\";Name\n", unit_getname(u)); str = u_description(u, f->locale); if (str) { fprintf(F, "\"%s\";Beschr\n", str); diff --git a/src/gmtool.c b/src/gmtool.c index e92299f2b..91134e8b5 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -423,7 +423,7 @@ static void paint_info_region(window * wnd, const state * st) wattroff(win, A_BOLD | COLOR_PAIR(COLOR_YELLOW)); for (u = r->units; u && line < maxline; u = u->next) { mvwprintw(win, line, 1, "%.4s ", itoa36(u->no)); - mvwaddnstr(win, line++, 6, (char *)u->name, size - 5); + mvwaddnstr(win, line++, 6, unit_getname(u), size - 5); } } } diff --git a/src/kernel/save.c b/src/kernel/save.c index db9999148..dc7444667 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -618,7 +618,7 @@ unit *read_unit(struct gamedata *data) } READ_STR(data->store, obuf, sizeof(obuf)); - u->name = _strdup(obuf); + u->_name = _strdup(obuf); if (lomem) { READ_STR(data->store, NULL, 0); } @@ -754,8 +754,8 @@ void write_unit(struct gamedata *data, const unit * u) write_unit_reference(u, data->store); write_faction_reference(u->faction, data->store); - WRITE_STR(data->store, (const char *)u->name); - WRITE_STR(data->store, u->display ? (const char *)u->display : ""); + WRITE_STR(data->store, u->_name); + WRITE_STR(data->store, u->display ? u->display : ""); WRITE_INT(data->store, u->number); WRITE_INT(data->store, u->age); WRITE_TOK(data->store, u_race(u)->_name); @@ -1600,7 +1600,7 @@ int readgame(const char *filename, int backup) sc_mage *mage; if (gdata.version < AUTO_RACENAME_VERSION) { - if (u->name && fval(u->faction, FFL_NPC)) { + if (u->_name && fval(u->faction, FFL_NPC)) { if (unit_name_equals_race(u)) { unit_setname(u, NULL); } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 2b987c572..07731e636 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1430,7 +1430,7 @@ int invisible(const unit * target, const unit * viewer) */ void free_unit(unit * u) { - free(u->name); + free(u->_name); free(u->display); free_order(u->thisorder); free_orders(&u->orders); @@ -1542,7 +1542,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace, name_unit(u); } else { - u->name = _strdup(dname); + u->_name = _strdup(dname); } if (creator) { @@ -1633,16 +1633,21 @@ int countheroes(const struct faction *f) const char *unit_getname(const unit * u) { - return (const char *)u->name; + if (!u->_name) { + const struct locale * lang = u->faction ? u->faction->locale : default_locale; + const char *rcname = rc_name_s(u->_race, u->number == 1 ? NAME_SINGULAR : NAME_PLURAL); + return LOC(lang, rcname); + } + return u->_name; } void unit_setname(unit * u, const char *name) { - free(u->name); + free(u->_name); if (name) - u->name = _strdup(name); + u->_name = _strdup(name); else - u->name = NULL; + u->_name = NULL; } const char *unit_getinfo(const unit * u) @@ -1880,14 +1885,8 @@ static int nextbuf = 0; 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)", LOC(lang, name), itoa36(u->no)); - } + const char * name = unit_getname(u); + slprintf(buffer, size, "%s (%s)", name, itoa36(u->no)); buffer[size - 1] = 0; return buffer; } @@ -1899,14 +1898,14 @@ const char *unitname(const unit * u) } bool unit_name_equals_race(const unit *u) { - if (u->name) { + 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, LOC(lang, sing)) == 0 || - strcmp(u->name, LOC(lang, plur)) == 0) { + if (strcmp(u->_name, sing) == 0 || strcmp(u->_name, plur) == 0 || + strcmp(u->_name, LOC(lang, sing)) == 0 || + strcmp(u->_name, LOC(lang, plur)) == 0) { return true; } } diff --git a/src/kernel/unit.h b/src/kernel/unit.h index 5fd40c57e..95e789c3c 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -84,7 +84,7 @@ extern "C" { struct region *region; int no; int hp; - char *name; + char *_name; char *display; struct faction *faction; struct building *building; diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 76d708721..659871745 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -155,10 +155,12 @@ static void test_unit_name_from_race(CuTest *tc) { _snprintf(name, sizeof(name), "Mensch (%s)", itoa36(u->no)); CuAssertStrEquals(tc, name, unitname(u)); + CuAssertStrEquals(tc, "Mensch", unit_getname(u)); u->number = 2; _snprintf(name, sizeof(name), "Menschen (%s)", itoa36(u->no)); CuAssertStrEquals(tc, name, unitname(u)); + CuAssertStrEquals(tc, "Menschen", unit_getname(u)); test_cleanup(); } diff --git a/src/laws.c b/src/laws.c index 7fc04876b..2904aaf7b 100755 --- a/src/laws.c +++ b/src/laws.c @@ -1835,9 +1835,10 @@ int name_cmd(struct unit *u, struct order *ord) } else { const char *udefault = LOC(u2->faction->locale, "unitdefault"); + const char *uname = unit_getname(u2); size_t udlen = strlen(udefault); - size_t unlen = strlen(u2->name); - if (unlen >= udlen && strncmp(u2->name, udefault, udlen) != 0) { + size_t unlen = strlen(uname); + if (unlen >= udlen && strncmp(uname, udefault, udlen) != 0) { cmistake(u2, ord, 244, MSG_EVENT); break; } @@ -1850,10 +1851,10 @@ int name_cmd(struct unit *u, struct order *ord) ADDMSG(&u2->faction->msgs, msg_message("renamed_notseen", "renamed region", u2, r)); } - s = &u2->name; + s = &u2->_name; } else { - s = &u->name; + s = &u->_name; } break; diff --git a/src/report.c b/src/report.c index 7b794e709..649eae934 100644 --- a/src/report.c +++ b/src/report.c @@ -1589,7 +1589,7 @@ report_template(const char *filename, report_context * ctx, const char *charset) size = sizeof(buf) - 1; bytes = _snprintf(bufp, size, "%s %s; %s [%d,%d$", LOC(u->faction->locale, parameters[P_UNIT]), - unitid(u), u->name, u->number, get_money(u)); + unitid(u), unit_getname(u), u->number, get_money(u)); if (wrptr(&bufp, &size, bytes) != 0) WARN_STATIC_BUFFER(); if (u->building && building_owner(u->building) == u) { diff --git a/src/reports.c b/src/reports.c index 1d305230f..3ad08371b 100644 --- a/src/reports.c +++ b/src/reports.c @@ -2074,7 +2074,7 @@ static void eval_unitname(struct opstack **stack, const void *userdata) { /* unit -> string */ const struct faction *f = (const struct faction *)userdata; const struct unit *u = (const struct unit *)opop(stack).v; - const char *c = u ? u->name : LOC(f->locale, "an_unknown_unit"); + const char *c = u ? unit_getname(u) : LOC(f->locale, "an_unknown_unit"); size_t len = strlen(c); variant var; @@ -2086,7 +2086,7 @@ static void eval_unitid(struct opstack **stack, const void *userdata) { /* unit -> int */ const struct faction *f = (const struct faction *)userdata; const struct unit *u = (const struct unit *)opop(stack).v; - const char *c = u ? u->name : LOC(f->locale, "an_unknown_unit"); + const char *c = u ? unit_getname(u) : LOC(f->locale, "an_unknown_unit"); size_t len = strlen(c); variant var; diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 21410465d..1cb4c8444 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -1748,7 +1748,7 @@ int sp_undeadhero(struct castorder * co) if (j > 0) { item **ilist; unit *u = - create_unit(r, mage->faction, 0, get_race(RC_UNDEAD), 0, du->name, + create_unit(r, mage->faction, 0, get_race(RC_UNDEAD), 0, du->_name, du); /* new units gets some stats from old unit */ diff --git a/src/tests.c b/src/tests.c index f10befba6..31c8b0bb0 100644 --- a/src/tests.c +++ b/src/tests.c @@ -240,7 +240,7 @@ const message_type *register_msg(const char *type, int n_param, ...) { void assert_messages(struct CuTest * tc, struct mlist *msglist, const message_type **types, int num_msgs, bool exact_match, ...) { va_list args; - int found, argc = -1; + int found = 0, argc = -1; struct message *msg; bool match = true;