race naming functions do more work than before.

This commit is contained in:
Enno Rehling 2016-08-28 20:27:40 +01:00
parent 514565d66b
commit e3b0dfedc9
4 changed files with 40 additions and 31 deletions

View File

@ -323,12 +323,9 @@ void register_race_name_function(race_name_func func, const char *name) {
register_function((pf_generic)func, name); register_function((pf_generic)func, name);
} }
char * race_namegen(const struct race *rc, const struct unit *u) { char * race_namegen(const struct race *rc, struct unit *u) {
if (rc->generate_name) { if (rc->generate_name) {
const char * str = rc->generate_name(u); rc->generate_name(u);
if (str) {
return _strdup(str);
}
} }
return NULL; return NULL;
} }

View File

@ -119,7 +119,7 @@ extern "C" {
extern int num_races; extern int num_races;
typedef const char *(*race_name_func)(const struct unit *); typedef void (*race_name_func)(struct unit *);
typedef struct race { typedef struct race {
char *_name; char *_name;
@ -262,7 +262,7 @@ extern "C" {
struct unit *u); struct unit *u);
const char *dbrace(const struct race *rc); const char *dbrace(const struct race *rc);
void register_race_name_function(race_name_func, const char *); void register_race_name_function(race_name_func, const char *);
char * race_namegen(const struct race *rc, const struct unit *u); char * race_namegen(const struct race *rc, struct unit *u);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -86,15 +86,14 @@ static const char *make_names(const char *monster, int *num_postfix,
if (*num_name > 0) { if (*num_name > 0) {
static char name[NAMESIZE + 1]; // FIXME: static return value static char name[NAMESIZE + 1]; // FIXME: static return value
char zText[32]; char zText[32];
int uv, uu, un; int uv = 0, uu = 0, un = 0;
const char *str; const char *str;
/* nur 50% aller Namen haben "Vor-Teil" */ if (*num_prefix > 0) {
uv = rng_int() % (*num_prefix * pprefix); uv = rng_int() % (*num_prefix * pprefix);
}
uu = rng_int() % *num_name; uu = rng_int() % *num_name;
/* nur 50% aller Namen haben "Nach-Teil", wenn kein Vor-Teil */
if (*num_postfix > 0 && uv >= *num_prefix) { if (*num_postfix > 0 && uv >= *num_prefix) {
un = rng_int() % *num_postfix; un = rng_int() % *num_postfix;
} }
@ -130,28 +129,32 @@ static const char *make_names(const char *monster, int *num_postfix,
return NULL; return NULL;
} }
static const char *undead_name(const unit * u) static void undead_name(unit * u)
{ {
static int num_postfix, num_name, num_prefix; static int num_postfix, num_name, num_prefix;
return make_names("undead", &num_postfix, 2, &num_name, &num_prefix, 2); const char *str = make_names("undead", &num_postfix, 2, &num_name, &num_prefix, 2);
unit_setname(u, str);
} }
static const char *skeleton_name(const unit * u) static void skeleton_name(unit * u)
{ {
static int num_postfix, num_name, num_prefix; static int num_postfix, num_name, num_prefix;
return make_names("skeleton", &num_postfix, 5, &num_name, &num_prefix, 2); const char *str = make_names("skeleton", &num_postfix, 5, &num_name, &num_prefix, 2);
unit_setname(u, str);
} }
static const char *zombie_name(const unit * u) static void zombie_name(unit * u)
{ {
static int num_postfix, num_name, num_prefix; static int num_postfix, num_name, num_prefix;
return make_names("zombie", &num_postfix, 5, &num_name, &num_prefix, 2); const char *str = make_names("zombie", &num_postfix, 5, &num_name, &num_prefix, 2);
unit_setname(u, str);
} }
static const char *ghoul_name(const unit * u) static void ghoul_name(unit * u)
{ {
static int num_postfix, num_name, num_prefix; static int num_postfix, num_name, num_prefix;
return make_names("ghoul", &num_postfix, 5, &num_name, &num_prefix, 4); const char *str = make_names("ghoul", &num_postfix, 5, &num_name, &num_prefix, 4);
unit_setname(u, str);
} }
/* Drachen */ /* Drachen */
@ -219,15 +222,18 @@ const char *silbe3[SIL3] = {
"bus", "bus",
}; };
static const char *generic_name(const unit * u) static void generic_name(unit * u)
{ {
const char * name = rc_name_s(u_race(u), (u->number == 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); name = LOC(u->faction->locale, name);
if (name) {
unit_setname(u, name);
}
} }
static const char *dragon_name(const unit * u) static void dragon_name(unit * u)
{ {
static char name[NAMESIZE + 1]; // FIXME: static return value char name[NAMESIZE + 1];
int rnd, ter = 0; int rnd, ter = 0;
int anzahl = 1; int anzahl = 1;
static int num_postfix; static int num_postfix;
@ -268,7 +274,7 @@ static const char *dragon_name(const unit * u)
} }
if (num_postfix <=0) { if (num_postfix <=0) {
return NULL; return;
} }
else if (num_postfix < 6) { else if (num_postfix < 6) {
rnd = rng_int() % num_postfix; rnd = rng_int() % num_postfix;
@ -311,7 +317,7 @@ static const char *dragon_name(const unit * u)
} }
} }
return name; unit_setname(u, name);
} }
/* Dracoide */ /* Dracoide */
@ -363,7 +369,7 @@ static const char *drac_suf[DRAC_SUF] = {
"k" "k"
}; };
static const char *dracoid_name(const unit * u) static void dracoid_name(unit * u)
{ {
static char name[NAMESIZE + 1]; // FIXME: static return value static char name[NAMESIZE + 1]; // FIXME: static return value
int mid_syllabels; int mid_syllabels;
@ -383,7 +389,7 @@ static const char *dracoid_name(const unit * u)
sz += strlcat(name, drac_mid[rng_int() % DRAC_MID], sizeof(name)); sz += strlcat(name, drac_mid[rng_int() % DRAC_MID], sizeof(name));
} }
sz += strlcat(name, drac_suf[rng_int() % DRAC_SUF], sizeof(name)); sz += strlcat(name, drac_suf[rng_int() % DRAC_SUF], sizeof(name));
return name; unit_setname(u, name);
} }
/** returns an abbreviation of a string. /** returns an abbreviation of a string.
@ -487,7 +493,7 @@ void register_names(void)
register_race_name_function(skeleton_name, "nameskeleton"); register_race_name_function(skeleton_name, "nameskeleton");
register_race_name_function(zombie_name, "namezombie"); register_race_name_function(zombie_name, "namezombie");
register_race_name_function(ghoul_name, "nameghoul"); register_race_name_function(ghoul_name, "nameghoul");
register_race_name_function(dragon_name, "namedragon");
register_race_name_function(dracoid_name, "namedracoid"); register_race_name_function(dracoid_name, "namedracoid");
register_race_name_function(dragon_name, "namedragon");
register_race_name_function(generic_name, "namegeneric"); register_race_name_function(generic_name, "namegeneric");
} }

View File

@ -3,6 +3,7 @@
#include "names.h" #include "names.h"
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/unit.h>
#include <util/language.h> #include <util/language.h>
#include <util/functions.h> #include <util/functions.h>
@ -12,14 +13,19 @@
static void test_names(CuTest * tc) static void test_names(CuTest * tc)
{ {
race_name_func foo; race_name_func foo;
unit *u;
race *rc;
test_cleanup(); test_cleanup();
register_names(); register_names();
default_locale = test_create_locale(); default_locale = test_create_locale();
locale_setstring(default_locale, "undead_prefix_0", "Kleine"); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
rc = test_create_race("undead");
locale_setstring(default_locale, "undead_name_0", "Graue"); locale_setstring(default_locale, "undead_name_0", "Graue");
locale_setstring(default_locale, "undead_postfix_0", "Kobolde"); locale_setstring(default_locale, "undead_postfix_0", "Kobolde");
CuAssertPtrNotNull(tc, foo = (race_name_func)get_function("nameundead")); CuAssertPtrNotNull(tc, foo = (race_name_func)get_function("nameundead"));
CuAssertStrEquals(tc, "Kleine Graue Kobolde", foo(NULL)); rc->generate_name = foo;
race_namegen(rc, u);
CuAssertStrEquals(tc, "Graue Kobolde", u->_name);
CuAssertPtrNotNull(tc, get_function("nameskeleton")); CuAssertPtrNotNull(tc, get_function("nameskeleton"));
CuAssertPtrNotNull(tc, get_function("namezombie")); CuAssertPtrNotNull(tc, get_function("namezombie"));
CuAssertPtrNotNull(tc, get_function("nameghoul")); CuAssertPtrNotNull(tc, get_function("nameghoul"));