Merge pull request #142 from badgerman/bug-1802-unitname

Bug 1802: unit names do not show up in CR
This commit is contained in:
Enno Rehling 2015-02-11 18:32:55 +01:00
commit 6f2b122f18
11 changed files with 36 additions and 34 deletions

View File

@ -167,7 +167,7 @@ static int tolua_unit_set_group(lua_State * L)
static int tolua_unit_get_name(lua_State * L) static int tolua_unit_get_name(lua_State * L)
{ {
unit *self = (unit *)tolua_tousertype(L, 1, 0); unit *self = (unit *)tolua_tousertype(L, 1, 0);
tolua_pushstring(L, self->name); tolua_pushstring(L, unit_getname(self));
return 1; return 1;
} }

View File

@ -758,7 +758,7 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
assert(u && u->number); assert(u && u->number);
fprintf(F, "EINHEIT %d\n", u->no); 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); str = u_description(u, f->locale);
if (str) { if (str) {
fprintf(F, "\"%s\";Beschr\n", str); fprintf(F, "\"%s\";Beschr\n", str);

View File

@ -423,7 +423,7 @@ static void paint_info_region(window * wnd, const state * st)
wattroff(win, A_BOLD | COLOR_PAIR(COLOR_YELLOW)); wattroff(win, A_BOLD | COLOR_PAIR(COLOR_YELLOW));
for (u = r->units; u && line < maxline; u = u->next) { for (u = r->units; u && line < maxline; u = u->next) {
mvwprintw(win, line, 1, "%.4s ", itoa36(u->no)); 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);
} }
} }
} }

View File

@ -618,7 +618,7 @@ unit *read_unit(struct gamedata *data)
} }
READ_STR(data->store, obuf, sizeof(obuf)); READ_STR(data->store, obuf, sizeof(obuf));
u->name = _strdup(obuf); u->_name = _strdup(obuf);
if (lomem) { if (lomem) {
READ_STR(data->store, NULL, 0); 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_unit_reference(u, data->store);
write_faction_reference(u->faction, data->store); write_faction_reference(u->faction, data->store);
WRITE_STR(data->store, (const char *)u->name); WRITE_STR(data->store, u->_name);
WRITE_STR(data->store, u->display ? (const char *)u->display : ""); WRITE_STR(data->store, u->display ? u->display : "");
WRITE_INT(data->store, u->number); WRITE_INT(data->store, u->number);
WRITE_INT(data->store, u->age); WRITE_INT(data->store, u->age);
WRITE_TOK(data->store, u_race(u)->_name); WRITE_TOK(data->store, u_race(u)->_name);
@ -1600,7 +1600,7 @@ int readgame(const char *filename, int backup)
sc_mage *mage; sc_mage *mage;
if (gdata.version < AUTO_RACENAME_VERSION) { 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)) { if (unit_name_equals_race(u)) {
unit_setname(u, NULL); unit_setname(u, NULL);
} }

View File

@ -1430,7 +1430,7 @@ int invisible(const unit * target, const unit * viewer)
*/ */
void free_unit(unit * u) void free_unit(unit * u)
{ {
free(u->name); free(u->_name);
free(u->display); free(u->display);
free_order(u->thisorder); free_order(u->thisorder);
free_orders(&u->orders); free_orders(&u->orders);
@ -1542,7 +1542,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
name_unit(u); name_unit(u);
} }
else { else {
u->name = _strdup(dname); u->_name = _strdup(dname);
} }
if (creator) { if (creator) {
@ -1633,16 +1633,21 @@ int countheroes(const struct faction *f)
const char *unit_getname(const unit * u) 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) void unit_setname(unit * u, const char *name)
{ {
free(u->name); free(u->_name);
if (name) if (name)
u->name = _strdup(name); u->_name = _strdup(name);
else else
u->name = NULL; u->_name = NULL;
} }
const char *unit_getinfo(const unit * u) 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) char *write_unitname(const unit * u, char *buffer, size_t size)
{ {
if (u->name) { const char * name = unit_getname(u);
slprintf(buffer, size, "%s (%s)", u->name, itoa36(u->no)); slprintf(buffer, size, "%s (%s)", 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));
}
buffer[size - 1] = 0; buffer[size - 1] = 0;
return buffer; return buffer;
} }
@ -1899,14 +1898,14 @@ const char *unitname(const unit * u)
} }
bool unit_name_equals_race(const unit *u) { bool unit_name_equals_race(const unit *u) {
if (u->name) { if (u->_name) {
char sing[32], plur[32]; char sing[32], plur[32];
const struct locale *lang = u->faction->locale; const struct locale *lang = u->faction->locale;
rc_name(u->_race, NAME_SINGULAR, sing, sizeof(sing)); rc_name(u->_race, NAME_SINGULAR, sing, sizeof(sing));
rc_name(u->_race, NAME_PLURAL, plur, sizeof(plur)); rc_name(u->_race, NAME_PLURAL, plur, sizeof(plur));
if (strcmp(u->name, sing) == 0 || strcmp(u->name, 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, sing)) == 0 ||
strcmp(u->name, LOC(lang, plur)) == 0) { strcmp(u->_name, LOC(lang, plur)) == 0) {
return true; return true;
} }
} }

View File

@ -84,7 +84,7 @@ extern "C" {
struct region *region; struct region *region;
int no; int no;
int hp; int hp;
char *name; char *_name;
char *display; char *display;
struct faction *faction; struct faction *faction;
struct building *building; struct building *building;

View File

@ -155,10 +155,12 @@ static void test_unit_name_from_race(CuTest *tc) {
_snprintf(name, sizeof(name), "Mensch (%s)", itoa36(u->no)); _snprintf(name, sizeof(name), "Mensch (%s)", itoa36(u->no));
CuAssertStrEquals(tc, name, unitname(u)); CuAssertStrEquals(tc, name, unitname(u));
CuAssertStrEquals(tc, "Mensch", unit_getname(u));
u->number = 2; u->number = 2;
_snprintf(name, sizeof(name), "Menschen (%s)", itoa36(u->no)); _snprintf(name, sizeof(name), "Menschen (%s)", itoa36(u->no));
CuAssertStrEquals(tc, name, unitname(u)); CuAssertStrEquals(tc, name, unitname(u));
CuAssertStrEquals(tc, "Menschen", unit_getname(u));
test_cleanup(); test_cleanup();
} }

View File

@ -1835,9 +1835,10 @@ int name_cmd(struct unit *u, struct order *ord)
} }
else { else {
const char *udefault = LOC(u2->faction->locale, "unitdefault"); const char *udefault = LOC(u2->faction->locale, "unitdefault");
const char *uname = unit_getname(u2);
size_t udlen = strlen(udefault); size_t udlen = strlen(udefault);
size_t unlen = strlen(u2->name); size_t unlen = strlen(uname);
if (unlen >= udlen && strncmp(u2->name, udefault, udlen) != 0) { if (unlen >= udlen && strncmp(uname, udefault, udlen) != 0) {
cmistake(u2, ord, 244, MSG_EVENT); cmistake(u2, ord, 244, MSG_EVENT);
break; break;
} }
@ -1850,10 +1851,10 @@ int name_cmd(struct unit *u, struct order *ord)
ADDMSG(&u2->faction->msgs, msg_message("renamed_notseen", ADDMSG(&u2->faction->msgs, msg_message("renamed_notseen",
"renamed region", u2, r)); "renamed region", u2, r));
} }
s = &u2->name; s = &u2->_name;
} }
else { else {
s = &u->name; s = &u->_name;
} }
break; break;

View File

@ -1589,7 +1589,7 @@ report_template(const char *filename, report_context * ctx, const char *charset)
size = sizeof(buf) - 1; size = sizeof(buf) - 1;
bytes = _snprintf(bufp, size, "%s %s; %s [%d,%d$", bytes = _snprintf(bufp, size, "%s %s; %s [%d,%d$",
LOC(u->faction->locale, parameters[P_UNIT]), 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) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
if (u->building && building_owner(u->building) == u) { if (u->building && building_owner(u->building) == u) {

View File

@ -2074,7 +2074,7 @@ static void eval_unitname(struct opstack **stack, const void *userdata)
{ /* unit -> string */ { /* unit -> string */
const struct faction *f = (const struct faction *)userdata; const struct faction *f = (const struct faction *)userdata;
const struct unit *u = (const struct unit *)opop(stack).v; 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); size_t len = strlen(c);
variant var; variant var;
@ -2086,7 +2086,7 @@ static void eval_unitid(struct opstack **stack, const void *userdata)
{ /* unit -> int */ { /* unit -> int */
const struct faction *f = (const struct faction *)userdata; const struct faction *f = (const struct faction *)userdata;
const struct unit *u = (const struct unit *)opop(stack).v; 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); size_t len = strlen(c);
variant var; variant var;

View File

@ -1748,7 +1748,7 @@ int sp_undeadhero(struct castorder * co)
if (j > 0) { if (j > 0) {
item **ilist; item **ilist;
unit *u = 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); du);
/* new units gets some stats from old unit */ /* new units gets some stats from old unit */