server/src/kernel/names.c

480 lines
10 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include <kernel/config.h>
#include "names.h"
/* kernel includes */
#include "unit.h"
#include "region.h"
#include "faction.h"
#include "magic.h"
#include "race.h"
#include "terrain.h"
#include "terrainid.h"
/* util includes */
#include <util/base36.h>
#include <util/bsdstring.h>
#include <util/functions.h>
#include <util/language.h>
#include <util/rng.h>
#include <util/unicode.h>
/* libc includes */
#include <ctype.h>
#include <wctype.h>
#include <stdlib.h>
#include <string.h>
2011-03-07 08:02:35 +01:00
const char *describe_braineater(unit * u, const struct locale *lang)
2010-08-08 10:06:34 +02:00
{
return LOC(lang, "describe_braineater");
}
2011-03-07 08:02:35 +01:00
static const char *make_names(const char *monster, int *num_postfix,
int pprefix, int *num_name, int *num_prefix, int ppostfix)
2010-08-08 10:06:34 +02:00
{
int uv, uu, un;
static char name[NAMESIZE + 1];
char zText[32];
2011-03-07 08:02:35 +01:00
const char *str;
if (*num_prefix == 0) {
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
for (*num_prefix = 0;; ++*num_prefix) {
2010-08-08 10:06:34 +02:00
sprintf(zText, "%s_prefix_%d", monster, *num_prefix);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
if (str == NULL)
break;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
for (*num_name = 0;; ++*num_name) {
2010-08-08 10:06:34 +02:00
sprintf(zText, "%s_name_%d", monster, *num_name);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
if (str == NULL)
break;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
for (*num_postfix = 0;; ++*num_postfix) {
2010-08-08 10:06:34 +02:00
sprintf(zText, "%s_postfix_%d", monster, *num_postfix);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
if (str == NULL)
break;
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
if (*num_name == 0) {
2010-08-08 10:06:34 +02:00
return NULL;
}
/* nur 50% aller Namen haben "Vor-Teil" */
uv = rng_int() % (*num_prefix * pprefix);
uu = rng_int() % *num_name;
/* nur 50% aller Namen haben "Nach-Teil", wenn kein Vor-Teil */
2011-03-07 08:02:35 +01:00
if (uv >= *num_prefix) {
2010-08-08 10:06:34 +02:00
un = rng_int() % *num_postfix;
} else {
un = rng_int() % (*num_postfix * ppostfix);
}
name[0] = 0;
if (uv < *num_prefix) {
sprintf(zText, "%s_prefix_%d", monster, uv);
str = locale_getstring(default_locale, zText);
if (str) {
strcat(name, (const char *)str);
strcat(name, " ");
}
}
sprintf(zText, "%s_name_%d", monster, uu);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
if (str)
strcat(name, (const char *)str);
2010-08-08 10:06:34 +02:00
if (un < *num_postfix) {
sprintf(zText, "%s_postfix_%d", monster, un);
str = locale_getstring(default_locale, zText);
if (str) {
strcat(name, " ");
strcat(name, (const char *)str);
}
}
return name;
}
2011-03-07 08:02:35 +01:00
const char *undead_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
static int num_postfix, num_name, num_prefix;
return make_names("undead", &num_postfix, 2, &num_name, &num_prefix, 2);
}
2011-03-07 08:02:35 +01:00
const char *skeleton_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
static int num_postfix, num_name, num_prefix;
return make_names("skeleton", &num_postfix, 5, &num_name, &num_prefix, 2);
}
2011-03-07 08:02:35 +01:00
const char *zombie_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
static int num_postfix, num_name, num_prefix;
return make_names("zombie", &num_postfix, 5, &num_name, &num_prefix, 2);
}
2011-03-07 08:02:35 +01:00
const char *ghoul_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
static int num_postfix, num_name, num_prefix;
return make_names("ghoul", &num_postfix, 5, &num_name, &num_prefix, 4);
}
/* Drachen */
#define SIL1 15
const char *silbe1[SIL1] = {
2011-03-07 08:02:35 +01:00
"Tar",
"Ter",
"Tor",
"Pan",
"Par",
"Per",
"Nim",
"Nan",
"Nun",
"Gor",
"For",
"Fer",
"Kar",
"Kur",
"Pen",
2010-08-08 10:06:34 +02:00
};
#define SIL2 19
const char *silbe2[SIL2] = {
2011-03-07 08:02:35 +01:00
"da",
"do",
"dil",
"di",
"dor",
"dar",
"ra",
"ran",
"ras",
"ro",
"rum",
"rin",
"ten",
"tan",
"ta",
"tor",
"gur",
"ga",
"gas",
2010-08-08 10:06:34 +02:00
};
#define SIL3 14
const char *silbe3[SIL3] = {
2011-03-07 08:02:35 +01:00
"gul",
"gol",
"dol",
"tan",
"tar",
"tur",
"sur",
"sin",
"kur",
"kor",
"kar",
"dul",
"dol",
"bus",
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
const char *generic_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
if (u->no == 1) {
return LOC(u->faction->locale, mkname("race", u->race->_name[0]));
}
return LOC(u->faction->locale, mkname("race", u->race->_name[1]));
}
2011-03-07 08:02:35 +01:00
const char *dragon_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
static char name[NAMESIZE + 1];
int rnd, ter = 0;
int anzahl = 1;
static int num_postfix;
char zText[32];
2011-03-07 08:02:35 +01:00
const char *str;
if (num_postfix == 0) {
for (num_postfix = 0;; ++num_postfix) {
2010-08-08 10:06:34 +02:00
sprintf(zText, "dragon_postfix_%d", num_postfix);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
if (str == NULL)
break;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (num_postfix == 0)
num_postfix = -1;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (num_postfix <= 0) {
2010-08-08 10:06:34 +02:00
return NULL;
}
if (u) {
region *r = u->region;
anzahl = u->number;
switch (rterrain(r)) {
2011-03-07 08:02:35 +01:00
case T_PLAIN:
ter = 1;
break;
case T_MOUNTAIN:
ter = 2;
break;
case T_DESERT:
ter = 3;
break;
case T_SWAMP:
ter = 4;
break;
case T_GLACIER:
ter = 5;
break;
2010-08-08 10:06:34 +02:00
}
}
rnd = num_postfix / 6;
rnd = (rng_int() % rnd) + ter * rnd;
sprintf(zText, "dragon_postfix_%d", rnd);
str = locale_getstring(default_locale, zText);
2011-03-07 08:02:35 +01:00
assert(str != NULL);
2010-08-08 10:06:34 +02:00
if (anzahl > 1) {
2011-03-07 08:02:35 +01:00
const char *no_article = strchr((const char *)str, ' ');
2010-08-08 10:06:34 +02:00
assert(no_article);
/* TODO: GERMAN */
sprintf(name, "Die %sn von %s", no_article+1, rname(u->region,
2011-03-07 08:02:35 +01:00
default_locale));
2010-08-08 10:06:34 +02:00
} else {
char n[32];
strcpy(n, silbe1[rng_int() % SIL1]);
strcat(n, silbe2[rng_int() % SIL2]);
strcat(n, silbe3[rng_int() % SIL3]);
if (rng_int() % 5 > 2) {
2011-03-07 08:02:35 +01:00
sprintf(name, "%s, %s", n, str); /* "Name, der Titel" */
2010-08-08 10:06:34 +02:00
} else {
2011-03-07 08:02:35 +01:00
strcpy(name, (const char *)str); /* "Der Titel Name" */
2010-08-08 10:06:34 +02:00
name[0] = (char)toupper(name[0]); /* TODO: UNICODE - should use towupper() */
strcat(name, " ");
strcat(name, n);
}
if (u && (rng_int() % 3 == 0)) {
strcat(name, " von ");
strcat(name, (const char *)rname(u->region, default_locale));
}
}
return name;
}
/* Dracoide */
#define DRAC_PRE 13
static const char *drac_pre[DRAC_PRE] = {
2011-03-07 08:02:35 +01:00
"Siss",
"Xxaa",
"Shht",
"X'xixi",
"Xar",
"X'zish",
"X",
"Sh",
"R",
"Z",
"Y",
"L",
"Ck",
2010-08-08 10:06:34 +02:00
};
#define DRAC_MID 12
static const char *drac_mid[DRAC_MID] = {
2011-03-07 08:02:35 +01:00
"siss",
"xxaa",
"shht",
"xxi",
"xar",
"x'zish",
"x",
"sh",
"r",
"z'ck",
"y",
"rl"
2010-08-08 10:06:34 +02:00
};
#define DRAC_SUF 10
static const char *drac_suf[DRAC_SUF] = {
2011-03-07 08:02:35 +01:00
"xil",
"shh",
"s",
"x",
"arr",
"lll",
"lll",
"shack",
"ck",
"k"
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
const char *dracoid_name(const unit * u)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
static char name[NAMESIZE + 1];
int mid_syllabels;
u = u;
/* Wieviele Mittelteile? */
mid_syllabels = rng_int() % 4;
strcpy(name, drac_pre[rng_int() % DRAC_PRE]);
while (mid_syllabels > 0) {
mid_syllabels--;
if (rng_int() % 10 < 4)
strcat(name, "'");
strcat(name, drac_mid[rng_int() % DRAC_MID]);
}
strcat(name, drac_suf[rng_int() % DRAC_SUF]);
return name;
2010-08-08 10:06:34 +02:00
}
/** returns an abbreviation of a string.
* TODO: buflen is being ignored */
2011-03-07 08:02:35 +01:00
const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
const char *p = s;
char *bufp;
unsigned int c = 0;
size_t bpt, i;
2010-08-08 10:06:34 +02:00
ucs4_t ucs;
size_t size;
int result;
2011-03-07 08:02:35 +01:00
/* Pr<50>fen, ob Kurz genug */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
if (strlen(s) <= maxchars) {
return s;
}
/* Anzahl der W<>rter feststellen */
while (*p != 0) {
2010-08-08 10:06:34 +02:00
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
/* Leerzeichen <20>berspringen */
2011-03-07 08:02:35 +01:00
while (*p != 0 && !iswalnum((wint_t) ucs)) {
p += size;
2010-08-08 10:06:34 +02:00
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
}
/* Counter erh<72>hen */
2011-03-07 08:02:35 +01:00
if (*p != 0)
++c;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
/* alnums <20>berspringen */
while (*p != 0 && iswalnum((wint_t) ucs)) {
p += size;
2010-08-08 10:06:34 +02:00
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
/* Buchstaben pro Teilk<6C>rzel = MAX(1,max/AnzWort) */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
bpt = MAX(1, maxchars / c);
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
/* Einzelne W<>rter anspringen und jeweils die ersten BpT kopieren */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
p = s;
c = 0;
2010-08-08 10:06:34 +02:00
bufp = buf;
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (*p != 0 && c < maxchars) {
/* Leerzeichen <20>berspringen */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (*p != 0 && !iswalnum((wint_t) ucs)) {
p += size;
2010-08-08 10:06:34 +02:00
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
/* alnums <20>bertragen */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
for (i = 0; i < bpt && *p != 0 && iswalnum((wint_t) ucs); ++i) {
memcpy(bufp, p, size);
p += size;
2010-08-08 10:06:34 +02:00
bufp += size;
++c;
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
}
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
/* Bis zum n<>chsten Leerzeichen */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (c < maxchars && *p != 0 && iswalnum((wint_t) ucs)) {
p += size;
2010-08-08 10:06:34 +02:00
result = unicode_utf8_to_ucs4(&ucs, p, &size);
2011-03-07 08:02:35 +01:00
assert(result == 0 || "damnit, we're not handling invalid input here!");
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
*bufp = 0;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
return buf;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void register_names(void)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
register_function((pf_generic) describe_braineater, "describe_braineater");
2010-08-08 10:06:34 +02:00
/* function name
* generate a name for a nonplayerunit
* race->generate_name() */
2011-03-07 08:02:35 +01:00
register_function((pf_generic) undead_name, "nameundead");
register_function((pf_generic) skeleton_name, "nameskeleton");
register_function((pf_generic) zombie_name, "namezombie");
register_function((pf_generic) ghoul_name, "nameghoul");
register_function((pf_generic) dragon_name, "namedragon");
register_function((pf_generic) dracoid_name, "namedracoid");
register_function((pf_generic) generic_name, "namegeneric");
2010-08-08 10:06:34 +02:00
}