From bfb5a9b69231c1b53ae9d3ad700582d332beb125 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 20 Feb 2017 22:07:36 +0100 Subject: [PATCH] set age_unit and name_unit funpointers in rc_create. no more in XML races. rename juju-zombie => juju. --- res/core/de/strings.xml | 8 ++++---- res/core/fr/strings.xml | 4 ++-- res/e3a/races.xml | 19 +------------------ res/eressea/races.xml | 23 +---------------------- src/kernel/race.c | 12 ++++++++---- src/kernel/race.h | 8 ++++---- src/kernel/unit.c | 9 ++++++--- src/kernel/unit.test.c | 2 +- src/kernel/xmlreader.c | 24 ------------------------ src/laws.c | 5 +++-- src/names.c | 23 ++++++++++------------- src/names.test.c | 35 ++++++++++++++++++++--------------- 12 files changed, 60 insertions(+), 112 deletions(-) diff --git a/res/core/de/strings.xml b/res/core/de/strings.xml index 3aa71393d..97ded6e8d 100644 --- a/res/core/de/strings.xml +++ b/res/core/de/strings.xml @@ -2379,19 +2379,19 @@ ghoul - + Juju-Zombie juju-zombie - + Juju-Zombies juju-zombies - + Juju-Zombies juju-zombies - + Juju-Zombie juju-zombie diff --git a/res/core/fr/strings.xml b/res/core/fr/strings.xml index d1801b338..7eb091570 100644 --- a/res/core/fr/strings.xml +++ b/res/core/fr/strings.xml @@ -1912,10 +1912,10 @@ zombie - + zombies juju - + zombie juju diff --git a/res/e3a/races.xml b/res/e3a/races.xml index b7111b4f1..2e5a81aee 100644 --- a/res/e3a/races.xml +++ b/res/e3a/races.xml @@ -415,7 +415,6 @@ - @@ -588,7 +587,6 @@ - @@ -643,19 +641,16 @@ - - - @@ -667,7 +662,6 @@ - @@ -683,7 +677,6 @@ - @@ -691,14 +684,12 @@ - - @@ -731,7 +722,6 @@ - @@ -750,7 +740,6 @@ - @@ -766,9 +755,8 @@ - + - @@ -785,7 +773,6 @@ - @@ -800,7 +787,6 @@ - @@ -816,7 +802,6 @@ - @@ -831,13 +816,11 @@ - - diff --git a/res/eressea/races.xml b/res/eressea/races.xml index b9077d98b..6deefacc9 100644 --- a/res/eressea/races.xml +++ b/res/eressea/races.xml @@ -330,7 +330,6 @@ - @@ -581,7 +580,6 @@ - @@ -652,19 +650,16 @@ - - - @@ -676,7 +671,6 @@ - @@ -691,7 +685,6 @@ - @@ -705,13 +698,11 @@ - - @@ -722,7 +713,6 @@ - @@ -733,7 +723,6 @@ - @@ -745,7 +734,6 @@ - @@ -997,7 +985,6 @@ - @@ -1015,7 +1002,6 @@ - @@ -1030,9 +1016,8 @@ - + - @@ -1048,7 +1033,6 @@ - @@ -1062,7 +1046,6 @@ - @@ -1077,7 +1060,6 @@ - @@ -1091,12 +1073,10 @@ - - @@ -1224,7 +1204,6 @@ - diff --git a/src/kernel/race.c b/src/kernel/race.c index 4e8e0817c..4a3a04916 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -74,7 +74,7 @@ static const char *racenames[MAXRACES] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "seaserpent", "shadowknight", NULL, "skeleton", "skeletonlord", "zombie", - "juju-zombie", "ghoul", "ghast", NULL, NULL, "template", + "juju", "ghoul", "ghast", NULL, NULL, "template", "clone" }; @@ -297,7 +297,7 @@ static race *rc_find_i(const char *name) rc = rc->next; } if (!rc) { - const char *rc_depr[] = { "uruk", "orc", "illusion", "template", NULL }; + const char *rc_depr[] = { "uruk", "orc", "illusion", "template", "juju-zombie", "juju", NULL }; int i; for (i = 0; rc_depr[i]; i += 2) { if (strcmp(name, rc_depr[i]) == 0) { @@ -353,7 +353,11 @@ race *rc_create(const char *zName) rc->next = races; snprintf(zText, sizeof(zText), "age_%s", zName); - rc->age = (void(*) (struct unit *))get_function(zText); + rc->age_unit = (race_func)get_function(zText); + + snprintf(zText, sizeof(zText), "name_%s", zName); + rc->name_unit = (race_func)get_function(zText); + return races = rc; } @@ -553,6 +557,6 @@ variant read_race_reference(struct storage *store) return result; } -void register_race_name_function(race_name_func func, const char *name) { +void register_race_function(race_func func, const char *name) { register_function((pf_generic)func, name); } diff --git a/src/kernel/race.h b/src/kernel/race.h index 1039402b0..e464c9447 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -112,7 +112,7 @@ extern "C" { int level; } att; - typedef void (*race_name_func)(struct unit *); + typedef void (*race_func) (struct unit *); typedef struct race { char *_name; @@ -143,8 +143,8 @@ extern "C" { struct att attack[RACE_ATTACKS]; signed char bonus[MAXSKILLS]; - race_name_func generate_name; - void(*age) (struct unit * u); + race_func name_unit; + race_func age_unit; struct rcoption *options; /* rarely used properties */ @@ -262,7 +262,7 @@ extern "C" { variant read_race_reference(struct storage *store); const char *raceprefix(const struct unit *u); - void register_race_name_function(race_name_func, const char *); + void register_race_function(race_func, const char *); #ifdef __cplusplus } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index ab1669ed9..4592d0bc7 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1460,8 +1460,11 @@ void default_name(const unit *u, char name[], int len) { void name_unit(unit * u) { const race *rc = u_race(u); - if (rc->generate_name) { - rc->generate_name(u); + if (rc->name_unit) { + rc->name_unit(u); + } + else if (u->faction->flags & FFL_NPC) { + unit_setname(u, NULL); } else { char name[32]; @@ -1514,7 +1517,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace, if (dname) { u->_name = strdup(dname); } - else if (urace->generate_name || playerrace(urace)) { + else if (urace->name_unit || playerrace(urace)) { name_unit(u); } diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index c187f7bab..7c7bb087b 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -487,7 +487,7 @@ static void test_name_unit(CuTest *tc) { test_setup(); rc = test_create_race("skeleton"); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); - rc->generate_name = gen_name; + rc->name_unit = gen_name; name_unit(u); CuAssertStrEquals(tc, "Hodor", unit_getname(u)); test_cleanup(); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 5c294d13a..695f91d69 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1778,30 +1778,6 @@ static int parse_races(xmlDocPtr doc) } xmlXPathFreeObject(result); - /* reading eressea/races/race/function */ - xpath->node = node; - result = xmlXPathEvalExpression(BAD_CAST "function", xpath); - for (k = 0; k != result->nodesetval->nodeNr; ++k) { - xmlNodePtr node = result->nodesetval->nodeTab[k]; - pf_generic fun; - - parse_function(node, &fun, &propValue); - if (fun == NULL) { - log_error("unknown function name '%s' for race %s\n", (const char *)propValue, rc->_name); - xmlFree(propValue); - continue; - } - assert(propValue != NULL); - if (strcmp((const char *)propValue, "name") == 0) { - rc->generate_name = (race_name_func)fun; - } - else { - log_error("unknown function type '%s' for race %s\n", (const char *)propValue, rc->_name); - } - xmlFree(propValue); - } - xmlXPathFreeObject(result); - /* reading eressea/races/race/familiar */ xpath->node = node; result = xmlXPathEvalExpression(BAD_CAST "familiar", xpath); diff --git a/src/laws.c b/src/laws.c index e314e2d85..253b45b89 100644 --- a/src/laws.c +++ b/src/laws.c @@ -164,9 +164,10 @@ static void age_unit(region * r, unit * u) } } else { + const race *rc = u_race(u); ++u->age; - if (u->number > 0 && u_race(u)->age) { - u_race(u)->age(u); + if (u->number > 0 && rc->age_unit) { + rc->age_unit(u); } } if (u->region && is_astral(u->region)) { diff --git a/src/names.c b/src/names.c index 780386deb..bc740a704 100644 --- a/src/names.c +++ b/src/names.c @@ -212,11 +212,6 @@ const char *silbe3[SIL3] = { "bus", }; -static void generic_name(unit * u) -{ - unit_setname(u, NULL); -} - static void dragon_name(unit * u) { char name[NAMESIZE + 1]; @@ -469,12 +464,14 @@ void register_names(void) { /* function name * generate a name for a nonplayerunit - * race->generate_name() */ - register_race_name_function(undead_name, "nameundead"); - register_race_name_function(skeleton_name, "nameskeleton"); - register_race_name_function(zombie_name, "namezombie"); - register_race_name_function(ghoul_name, "nameghoul"); - register_race_name_function(dracoid_name, "namedracoid"); - register_race_name_function(dragon_name, "namedragon"); - register_race_name_function(generic_name, "namegeneric"); + * race->name_unit() */ + + register_race_function(undead_name, "name_undead"); + register_race_function(skeleton_name, "name_skeleton"); + register_race_function(zombie_name, "name_zombie"); + register_race_function(ghoul_name, "name_ghoul"); + register_race_function(dracoid_name, "name_dracoid"); + register_race_function(dragon_name, "name_dragon"); + register_race_function(dragon_name, "name_youngdragon"); + register_race_function(dragon_name, "name_wyrm"); } diff --git a/src/names.test.c b/src/names.test.c index 01fe0ff41..7d62d4752 100644 --- a/src/names.test.c +++ b/src/names.test.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -12,31 +13,33 @@ static void test_names(CuTest * tc) { - race_name_func foo; unit *u; race *rc; test_cleanup(); register_names(); + CuAssertPtrNotNull(tc, get_function("name_undead")); + CuAssertPtrNotNull(tc, get_function("name_skeleton")); + CuAssertPtrNotNull(tc, get_function("name_zombie")); + CuAssertPtrNotNull(tc, get_function("name_ghoul")); + CuAssertPtrNotNull(tc, get_function("name_dragon")); + CuAssertPtrNotNull(tc, get_function("name_youngdragon")); + CuAssertPtrNotNull(tc, get_function("name_wyrm")); + CuAssertPtrNotNull(tc, get_function("name_dracoid")); default_locale = test_create_locale(); - u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); rc = test_create_race("undead"); + u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); locale_setstring(default_locale, "undead_name_0", "Graue"); locale_setstring(default_locale, "undead_postfix_0", "Kobolde"); - CuAssertPtrNotNull(tc, foo = (race_name_func)get_function("nameundead")); - rc->generate_name = foo; - rc->generate_name(u); + CuAssertPtrNotNull(tc, rc->name_unit); + CuAssertTrue(tc, rc->name_unit == (race_func)get_function("name_undead")); + name_unit(u); CuAssertStrEquals(tc, "Graue Kobolde", u->_name); - CuAssertPtrNotNull(tc, get_function("nameskeleton")); - CuAssertPtrNotNull(tc, get_function("namezombie")); - CuAssertPtrNotNull(tc, get_function("nameghoul")); - CuAssertPtrNotNull(tc, get_function("namedragon")); - CuAssertPtrNotNull(tc, get_function("namedracoid")); - CuAssertPtrNotNull(tc, get_function("namegeneric")); test_cleanup(); } static void test_monster_names(CuTest *tc) { unit *u; + faction *f; race *rc; test_setup(); @@ -45,11 +48,13 @@ static void test_monster_names(CuTest *tc) { locale_setstring(default_locale, "race::irongolem", "Eisengolem"); locale_setstring(default_locale, "race::irongolem_p", "Eisengolems"); rc = test_create_race("irongolem"); - u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); + f = test_create_faction(rc); + f->flags |= FFL_NPC; + u = test_create_unit(f, test_create_region(0, 0, 0)); + unit_setname(u, "Hodor"); CuAssertPtrNotNull(tc, u->_name); - rc->generate_name = (race_name_func)get_function("namegeneric"); - rc->generate_name(u); - CuAssertPtrEquals(tc, 0, u->_name); + name_unit(u); + CuAssertPtrEquals(tc, NULL, u->_name); CuAssertStrEquals(tc, "Eisengolem", unit_getname(u)); u->number = 2; CuAssertStrEquals(tc, "Eisengolems", unit_getname(u));