fix bug in generic_name, minor improvements

Amusingly, names::generic_name used u->no instead of u->number to determine
singular or plural.
This commit is contained in:
Steffen Mecke 2015-02-11 22:41:10 +01:00
parent 6f2b122f18
commit a31898ceb5
6 changed files with 28 additions and 7 deletions

View File

@ -70,9 +70,12 @@ static void test_remove_dead_factions(CuTest *tc) {
static void test_addfaction(CuTest *tc) { static void test_addfaction(CuTest *tc) {
faction *f = 0; faction *f = 0;
const struct race *rc = rc_get_or_create("human"); const struct race *rc;
const struct locale *lang = get_or_create_locale("en"); const struct locale *lang;
test_cleanup();
rc = rc_get_or_create("human");
lang = get_or_create_locale("en");
f = addfaction("test@eressea.de", "hurrdurr", rc, lang, 1234); f = addfaction("test@eressea.de", "hurrdurr", rc, lang, 1234);
CuAssertPtrNotNull(tc, f); CuAssertPtrNotNull(tc, f);
CuAssertPtrNotNull(tc, f->name); CuAssertPtrNotNull(tc, f->name);

View File

@ -1631,6 +1631,7 @@ int countheroes(const struct faction *f)
return n; return n;
} }
/** Returns the raw unit name, like "Frodo", or "Seeschlange" */
const char *unit_getname(const unit * u) const char *unit_getname(const unit * u)
{ {
if (!u->_name) { if (!u->_name) {
@ -1883,6 +1884,7 @@ typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8]; static name idbuf[8];
static int nextbuf = 0; static int nextbuf = 0;
/** Puts human-readable unit name, with number, like "Frodo (hobb)" into buffer */
char *write_unitname(const unit * u, char *buffer, size_t size) char *write_unitname(const unit * u, char *buffer, size_t size)
{ {
const char * name = unit_getname(u); const char * name = unit_getname(u);
@ -1891,6 +1893,7 @@ char *write_unitname(const unit * u, char *buffer, size_t size)
return buffer; return buffer;
} }
/** Returns human-readable unit name, with number, like "Frodo (hobb)" */
const char *unitname(const unit * u) const char *unitname(const unit * u)
{ {
char *ubuf = idbuf[(++nextbuf) % 8]; char *ubuf = idbuf[(++nextbuf) % 8];

View File

@ -82,14 +82,14 @@ extern "C" {
struct unit *nextF; /* nächste Einheit der Partei */ struct unit *nextF; /* nächste Einheit der Partei */
struct unit *prevF; /* vorherige Einheit der Partei */ struct unit *prevF; /* vorherige Einheit der Partei */
struct region *region; struct region *region;
int no; int no; /* id */
int hp; int hp;
char *_name; char *_name;
char *display; char *display;
struct faction *faction; struct faction *faction;
struct building *building; struct building *building;
struct ship *ship; struct ship *ship;
unsigned short number; unsigned short number; /* persons */
short age; short age;
/* skill data */ /* skill data */
@ -110,7 +110,7 @@ extern "C" {
int flags; int flags;
struct attrib *attribs; struct attrib *attribs;
status_t status; status_t status;
int n; /* enno: attribut? */ int n; /* helper temporariy variable, used in econmy, enno: attribut? */
int wants; /* enno: attribut? */ int wants; /* enno: attribut? */
} unit; } unit;

View File

@ -197,6 +197,20 @@ static void test_update_monster_name(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_names(CuTest *tc) {
unit *u;
test_cleanup();
test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
unit_setname(u, "Hodor");
unit_setid(u, 5);
CuAssertStrEquals(tc, "Hodor", unit_getname(u));
CuAssertStrEquals(tc, "Hodor (5)", unitname(u));
test_cleanup();
}
CuSuite *get_unit_suite(void) CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -209,5 +223,6 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_remove_units_without_faction); SUITE_ADD_TEST(suite, test_remove_units_without_faction);
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);
return suite; return suite;
} }

View File

@ -215,7 +215,7 @@ const char *silbe3[SIL3] = {
static const char *generic_name(const unit * u) static const char *generic_name(const unit * u)
{ {
const char * name = rc_name_s(u_race(u), (u->no == 1) ? NAME_SINGULAR : NAME_PLURAL); const char * name = rc_name_s(u_race(u), (u->number == 1) ? NAME_SINGULAR : NAME_PLURAL);
return LOC(u->faction->locale, name); return LOC(u->faction->locale, name);
} }

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, unit_getname(du),
du); du);
/* new units gets some stats from old unit */ /* new units gets some stats from old unit */