directions can now be read from json configuration.

rename a couple of functions for more consistency across modules.
This commit is contained in:
Enno Rehling 2014-06-15 22:17:08 -07:00
parent a144417daa
commit 64e0c221ec
29 changed files with 193 additions and 98 deletions

View File

@ -319,7 +319,7 @@ static int tolua_faction_create(lua_State * L)
const char *email = tolua_tostring(L, 1, 0);
const char *racename = tolua_tostring(L, 2, 0);
const char *lang = tolua_tostring(L, 3, 0);
struct locale *loc = find_locale(lang);
struct locale *loc = get_locale(lang);
faction *f = NULL;
const struct race *frace = rc_find(racename);
if (frace != NULL) {
@ -371,7 +371,7 @@ static int tolua_faction_set_locale(lua_State * L)
{
faction *self = (faction *) tolua_tousertype(L, 1, 0);
const char *name = tolua_tostring(L, 2, 0);
const struct locale *loc = find_locale(name);
const struct locale *loc = get_locale(name);
if (loc) {
self->locale = loc;
}

View File

@ -2,18 +2,18 @@
#include "util/language.h"
void locale_create(const char *lang) {
make_locale(lang);
get_or_create_locale(lang);
}
void locale_set(const char *lang, const char *key, const char *str) {
struct locale *loc = find_locale(lang);
struct locale *loc = get_locale(lang);
if (loc) {
locale_setstring(loc, key, str);
}
}
const char * locale_get(const char *lang, const char *key) {
struct locale *loc = find_locale(lang);
struct locale *loc = get_locale(lang);
if (loc) {
return locale_getstring(loc, key);
}

View File

@ -190,7 +190,7 @@ static int tolua_translate(lua_State * L)
{
const char *str = tolua_tostring(L, 1, 0);
const char *lang = tolua_tostring(L, 2, 0);
struct locale *loc = lang ? find_locale(lang) : default_locale;
struct locale *loc = lang ? get_locale(lang) : default_locale;
if (loc) {
str = locale_string(loc, str);
tolua_pushstring(L, str);

View File

@ -91,7 +91,7 @@ static const char *crtag(const char *key)
{
static const struct locale *lang = NULL;
if (!lang)
lang = find_locale(TAG_LOCALE);
lang = get_locale(TAG_LOCALE);
return locale_string(lang, key);
}
#else

View File

@ -5,6 +5,8 @@
#include "util/language.h"
#include "util/umlaut.h"
#include <string.h>
void init_direction(const struct locale *lang, direction_t dir, const char *str) {
void **tokens = get_translations(lang, UT_DIRECTIONS);
variant token;
@ -45,7 +47,7 @@ void init_directions(const struct locale *lang) {
}
}
direction_t finddirection(const char *s, const struct locale *lang)
direction_t get_direction(const char *s, const struct locale *lang)
{
void **tokens = get_translations(lang, UT_DIRECTIONS);
variant token;
@ -56,3 +58,17 @@ direction_t finddirection(const char *s, const struct locale *lang)
return NODIRECTION;
}
direction_t finddirection(const char *str) {
int i;
for (i=0;i!=MAXDIRECTIONS+2;++i) {
if (directions[i] && strcmp(str, directions[i])==0) {
return (direction_t)i;
}
}
return NODIRECTION;
}
const char * directions[MAXDIRECTIONS+2] = {
"northwest", "northeast", "east", "southeast", "southwest", "west", 0, "pause"
};

View File

@ -21,10 +21,14 @@ typedef enum {
NODIRECTION = -1
} direction_t;
direction_t finddirection(const char *s, const struct locale *);
direction_t get_direction(const char *s, const struct locale *);
void init_directions(const struct locale *lang);
void init_direction(const struct locale *lang, direction_t dir, const char *str);
direction_t finddirection(const char *str);
extern const char * directions[];
#ifdef __cplusplus
#endif
#endif

View File

@ -10,10 +10,10 @@ void test_init_directions(CuTest *tc) {
struct locale *lang;
test_cleanup();
lang = make_locale("en");
lang = get_or_create_locale("en");
locale_setstring(lang, "dir_nw", "NW");
init_directions(lang);
CuAssertIntEquals(tc, D_NORTHWEST, finddirection("nw", lang));
CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nw", lang));
test_cleanup();
}
@ -21,26 +21,39 @@ void test_init_direction(CuTest *tc) {
struct locale *lang;
test_cleanup();
lang = make_locale("de");
lang = get_or_create_locale("de");
init_direction(lang, D_NORTHWEST, "NW");
init_direction(lang, D_EAST, "OST");
CuAssertIntEquals(tc, D_NORTHWEST, finddirection("nw", lang));
CuAssertIntEquals(tc, D_EAST, finddirection("ost", lang));
CuAssertIntEquals(tc, NODIRECTION, finddirection("east", lang));
CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nw", lang));
CuAssertIntEquals(tc, D_EAST, get_direction("ost", lang));
CuAssertIntEquals(tc, NODIRECTION, get_direction("east", lang));
test_cleanup();
}
void test_finddirection_default(CuTest *tc) {
void test_finddirection(CuTest *tc) {
test_cleanup();
CuAssertIntEquals(tc, D_SOUTHWEST, finddirection("southwest"));
CuAssertIntEquals(tc, D_SOUTHEAST, finddirection("southeast"));
CuAssertIntEquals(tc, D_NORTHWEST, finddirection("northwest"));
CuAssertIntEquals(tc, D_NORTHEAST, finddirection("northeast"));
CuAssertIntEquals(tc, D_WEST, finddirection("west"));
CuAssertIntEquals(tc, D_EAST, finddirection("east"));
CuAssertIntEquals(tc, D_PAUSE, finddirection("pause"));
CuAssertIntEquals(tc, NODIRECTION, finddirection(""));
CuAssertIntEquals(tc, NODIRECTION, finddirection("potato"));
}
void test_get_direction_default(CuTest *tc) {
struct locale *lang;
test_cleanup();
lang = make_locale("en");
CuAssertIntEquals(tc, NODIRECTION, finddirection("potato", lang));
CuAssertIntEquals(tc, D_SOUTHWEST, finddirection("southwest", lang));
CuAssertIntEquals(tc, D_SOUTHEAST, finddirection("southeast", lang));
CuAssertIntEquals(tc, D_NORTHWEST, finddirection("northwest", lang));
CuAssertIntEquals(tc, D_NORTHEAST, finddirection("northeast", lang));
CuAssertIntEquals(tc, D_WEST, finddirection("west", lang));
CuAssertIntEquals(tc, D_EAST, finddirection("east", lang));
lang = get_or_create_locale("en");
CuAssertIntEquals(tc, NODIRECTION, get_direction("potato", lang));
CuAssertIntEquals(tc, D_SOUTHWEST, get_direction("southwest", lang));
CuAssertIntEquals(tc, D_SOUTHEAST, get_direction("southeast", lang));
CuAssertIntEquals(tc, D_NORTHWEST, get_direction("northwest", lang));
CuAssertIntEquals(tc, D_NORTHEAST, get_direction("northeast", lang));
CuAssertIntEquals(tc, D_WEST, get_direction("west", lang));
CuAssertIntEquals(tc, D_EAST, get_direction("east", lang));
}
CuSuite *get_direction_suite(void)
@ -48,7 +61,8 @@ CuSuite *get_direction_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_init_direction);
SUITE_ADD_TEST(suite, test_init_directions);
SUITE_ADD_TEST(suite, test_finddirection_default);
SUITE_ADD_TEST(suite, test_finddirection);
SUITE_ADD_TEST(suite, test_get_direction_default);
return suite;
}

View File

@ -1718,7 +1718,7 @@ int make_cmd(unit * u, struct order *ord)
if (pl && fval(pl, PFL_NOBUILD)) {
cmistake(u, ord, 275, MSG_PRODUCE);
} else {
direction_t d = finddirection(getstrtoken(), u->faction->locale);
direction_t d = get_direction(getstrtoken(), u->faction->locale);
if (d != NODIRECTION) {
build_road(r, u, m, d);
} else {

View File

@ -94,7 +94,7 @@ ship *getship(const struct region * r)
static void destroy_road(unit * u, int nmax, struct order *ord)
{
direction_t d = finddirection(getstrtoken(), u->faction->locale);
direction_t d = get_direction(getstrtoken(), u->faction->locale);
unit *u2;
region *r = u->region;
short n = (short)nmax;

View File

@ -307,17 +307,6 @@ helpmode helpmodes[] = {
{NULL, 0}
};
const char *directions[MAXDIRECTIONS + 2] = {
"northwest",
"northeast",
"east",
"southeast",
"southwest",
"west",
"",
"pause"
};
/** Returns the English name of the race, which is what the database uses.
*/
const char *dbrace(const struct race *rc)
@ -326,7 +315,7 @@ const char *dbrace(const struct race *rc)
char *zPtr = zText;
/* the english names are all in ASCII, so we don't need to worry about UTF8 */
strcpy(zText, (const char *)LOC(find_locale("en"), rc_name(rc, 0)));
strcpy(zText, (const char *)LOC(get_locale("en"), rc_name(rc, 0)));
while (*zPtr) {
*zPtr = (char)(toupper(*zPtr));
++zPtr;
@ -2035,9 +2024,9 @@ void init_locales(void)
{
int l;
for (l = 0; localenames[l]; ++l) {
const struct locale *lang = find_locale(localenames[l]);
const struct locale *lang = get_locale(localenames[l]);
if (!lang) {
lang = make_locale(localenames[l]);
lang = get_or_create_locale(localenames[l]);
}
init_locale(lang);
}
@ -2704,7 +2693,7 @@ message *movement_error(unit * u, const char *token, order * ord,
direction_t d;
switch (error_code) {
case E_MOVE_BLOCKED:
d = finddirection(token, u->faction->locale);
d = get_direction(token, u->faction->locale);
return msg_message("moveblocked", "unit direction", u, d);
case E_MOVE_NOREGION:
return msg_feedback(u, ord, "unknowndirection", "dirname", token);
@ -2722,7 +2711,7 @@ int movewhere(const unit * u, const char *token, region * r, region ** resultp)
return E_MOVE_OK;
}
d = finddirection(token, u->faction->locale);
d = get_direction(token, u->faction->locale);
switch (d) {
case D_PAUSE:
*resultp = r;

View File

@ -141,9 +141,6 @@ extern "C" {
int skill_limit(struct faction *f, skill_t sk);
int count_skill(struct faction *f, skill_t sk);
/* direction, geography */
extern const char *directions[];
int findoption(const char *s, const struct locale *lang);
/* special units */

View File

@ -78,7 +78,7 @@ void test_finditemtype(CuTest * tc)
test_cleanup();
test_create_world();
lang = find_locale("de");
lang = get_locale("de");
locale_setstring(lang, "horse", "Pferd");
itype = it_find("horse");
iresult = finditemtype("Pferd", lang);
@ -94,7 +94,7 @@ void test_findresourcetype(CuTest * tc)
test_cleanup();
test_create_world();
lang = find_locale("de");
lang = get_locale("de");
locale_setstring(lang, "horse", "Pferd");
locale_setstring(lang, "peasant", "Bauer");

View File

@ -229,6 +229,42 @@ void json_ships(cJSON *json) {
}
}
static void json_direction(cJSON *json, struct locale *lang) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error("directions for locale `%s` not a json object: %d\n", locale_name(lang), json->type);
return;
}
for (child=json->child;child;child=child->next) {
direction_t dir = finddirection(child->string);
if (dir!=NODIRECTION) {
if (child->type==cJSON_String) {
init_direction(lang, dir, child->valuestring);
}
else if (child->type==cJSON_Array) {
cJSON *entry;
for (entry=child->child;entry;entry=entry->next) {
init_direction(lang, dir, entry->valuestring);
}
} else {
log_error("invalid type %d for direction `%s`\n", child->type, child->string);
}
}
}
}
void json_directions(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error("directions is not a json object: %d\n", json->type);
return;
}
for (child=json->child;child;child=child->next) {
struct locale * lang = get_or_create_locale(child->string);
json_direction(child, lang);
}
}
void json_races(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
@ -253,6 +289,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "ships")==0) {
json_ships(child);
}
else if (strcmp(child->string, "directions")==0) {
json_directions(child);
}
else if (strcmp(child->string, "buildings")==0) {
json_buildings(child);
}

View File

@ -1,10 +1,13 @@
#include <platform.h>
#include "types.h"
#include "jsonconf.h"
#include "building.h"
#include "direction.h"
#include "race.h"
#include "terrain.h"
#include "ship.h"
#include "terrain.h"
#include "util/language.h"
#include <CuTest.h>
#include <cJSON.h>
#include <tests.h>
@ -142,9 +145,31 @@ static void test_terrains(CuTest * tc)
test_cleanup();
}
static void test_directions(CuTest * tc)
{
const char * data = "{\"directions\": { \"de\" : { \"east\" : \"osten\", \"northwest\" : [ \"nw\", \"nordwest\" ], \"pause\" : \"pause\" }}}";
const struct locale * lang;
cJSON *json = cJSON_Parse(data);
test_cleanup();
lang = get_or_create_locale("de");
CuAssertPtrNotNull(tc, json);
CuAssertIntEquals(tc, NODIRECTION, get_direction("ost", lang));
json_config(json);
CuAssertIntEquals(tc, D_EAST, get_direction("ost", lang));
CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nw", lang));
CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nordwest", lang));
CuAssertIntEquals(tc, D_PAUSE, get_direction("pause", lang));
test_cleanup();
}
CuSuite *get_jsonconf_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_directions);
SUITE_ADD_TEST(suite, test_ships);
SUITE_ADD_TEST(suite, test_buildings);
SUITE_ADD_TEST(suite, test_terrains);

View File

@ -178,7 +178,7 @@ void test_getspell_unit(CuTest * tc)
set_level(u, SK_MAGIC, 1);
lang = find_locale("de");
lang = get_locale("de");
sp = create_spell("testspell", 0);
locale_setstring(lang, mkname("spell", sp->sname), "Herp-a-derp");
@ -207,7 +207,7 @@ void test_getspell_faction(CuTest * tc)
set_level(u, SK_MAGIC, 1);
lang = find_locale("de");
lang = get_locale("de");
sp = create_spell("testspell", 0);
locale_setstring(lang, mkname("spell", sp->sname), "Herp-a-derp");
@ -237,7 +237,7 @@ void test_getspell_school(CuTest * tc)
skill_enabled[SK_MAGIC] = 1;
set_level(u, SK_MAGIC, 1);
lang = find_locale("de");
lang = get_locale("de");
sp = create_spell("testspell", 0);
locale_setstring(lang, mkname("spell", sp->sname), "Herp-a-derp");

View File

@ -1022,7 +1022,7 @@ static void cycle_route(order * ord, unit * u, int gereist)
pause = false;
token = getstrtoken();
if (token && *token) {
d = finddirection(token, lang);
d = get_direction(token, lang);
if (d == D_PAUSE) {
pause = true;
} else if (d == NODIRECTION) {

View File

@ -277,8 +277,8 @@ int readorders(const char *filename)
case P_LOCALE:
{
const char *s = getstrtoken();
if (f && find_locale(s)) {
f->locale = find_locale(s);
if (f && get_locale(s)) {
f->locale = get_locale(s);
}
}
b = getbuf(F, enc_gamedata);
@ -1261,7 +1261,7 @@ faction *readfaction(struct gamedata * data)
}
READ_STR(data->store, name, sizeof(name));
f->locale = find_locale(name);
f->locale = get_locale(name);
READ_INT(data->store, &f->lastorders);
READ_INT(data->store, &f->age);
READ_STR(data->store, name, sizeof(name));

View File

@ -58,10 +58,10 @@ static void xml_readtext(xmlNodePtr node, struct locale **lang, xmlChar ** text)
{
xmlChar *propValue = xmlGetProp(node, BAD_CAST "locale");
assert(propValue != NULL);
*lang = find_locale((const char *)propValue);
*lang = get_locale((const char *)propValue);
#ifdef MAKE_LOCALES
if (*lang == NULL)
*lang = make_locale((const char *)propValue);
*lang = get_or_create_locale((const char *)propValue);
#endif
xmlFree(propValue);

View File

@ -1639,7 +1639,7 @@ static void init_prefixnames(void)
{
int i;
for (i = 0; localenames[i]; ++i) {
const struct locale *lang = find_locale(localenames[i]);
const struct locale *lang = get_locale(localenames[i]);
bool exist = false;
struct local_names *in = pnames;

View File

@ -344,7 +344,7 @@ static void guardian_faction(plane * pl, int id)
f->name = _strdup("Igjarjuks Kundschafter");
f->race = new_race[RC_ILLUSION];
f->age = turn;
f->locale = find_locale("de");
f->locale = get_locale("de");
f->options =
want(O_COMPRESS) | want(O_REPORT) | want(O_COMPUTER) | want(O_ADRESSEN) |
want(O_DEBUG);

View File

@ -298,7 +298,7 @@ newfaction *read_newfactions(const char *filename)
}
}
}
nf->lang = find_locale(lang);
nf->lang = get_locale(lang);
nf->bonus = bonus;
assert(nf->race && nf->email && nf->lang);
nfi = &newfactions;

View File

@ -2608,7 +2608,7 @@ static int sp_firewall(castorder * co)
direction_t dir;
region *r2;
dir = finddirection(pa->param[0]->data.xs, mage->faction->locale);
dir = get_direction(pa->param[0]->data.xs, mage->faction->locale);
if (dir < MAXDIRECTIONS && dir != NODIRECTION) {
r2 = rconnect(r, dir);
} else {
@ -5862,7 +5862,7 @@ int sp_movecastle(castorder * co)
return 0;
b = pa->param[0]->data.b;
dir = finddirection(pa->param[1]->data.xs, mage->faction->locale);
dir = get_direction(pa->param[1]->data.xs, mage->faction->locale);
if (dir == NODIRECTION) {
/* Die Richtung wurde nicht erkannt */

View File

@ -134,7 +134,7 @@ void test_create_world(void)
int i;
const char * names[] = { "horse", "horse_p", "boat", "boat_p", "iron", "iron_p", "stone", "stone_p" };
make_locale("de");
get_or_create_locale("de");
init_resources();
assert(!olditemtype[I_HORSE]);

View File

@ -12,12 +12,12 @@
static void test_recreate_world(CuTest * tc)
{
test_cleanup();
CuAssertPtrEquals(tc, 0, find_locale("de"));
CuAssertPtrEquals(tc, 0, get_locale("de"));
CuAssertPtrEquals(tc, 0, it_find("money"));
CuAssertPtrEquals(tc, 0, it_find("horse"));
test_create_world();
CuAssertPtrEquals(tc, default_locale, find_locale("de"));
CuAssertPtrEquals(tc, default_locale, get_locale("de"));
CuAssertPtrNotNull(tc, default_locale);
CuAssertPtrNotNull(tc, findregion(0, 0));
CuAssertPtrNotNull(tc, it_find("money"));
@ -31,7 +31,7 @@ static void test_recreate_world(CuTest * tc)
CuAssertPtrNotNull(tc, rt_find("unit"));
test_cleanup();
CuAssertPtrEquals(tc, 0, find_locale("de"));
CuAssertPtrEquals(tc, 0, get_locale("de"));
CuAssertPtrEquals(tc, 0, it_find("money"));
CuAssertPtrEquals(tc, 0, it_find("horse"));
CuAssertPtrEquals(tc, 0, rt_find("horse"));

View File

@ -40,7 +40,7 @@ unsigned int locale_index(const locale * lang)
return lang->index;
}
locale *find_locale(const char *name)
locale *get_locale(const char *name)
{
unsigned int hkey = hashstring(name);
locale *l = locales;
@ -51,31 +51,27 @@ locale *find_locale(const char *name)
static unsigned int nextlocaleindex = 0;
locale *make_locale(const char *name)
locale *get_or_create_locale(const char *name)
{
unsigned int hkey = hashstring(name);
locale *l = (locale *) calloc(sizeof(locale), 1);
locale **lp = &locales;
locale *l;
unsigned int hkey = hashstring(name);
locale **lp = &locales;
if (!locales) {
nextlocaleindex = 0;
}
while (*lp && (*lp)->hashkey != hkey)
lp = &(*lp)->next;
if (*lp) {
return *lp;
}
l->hashkey = hkey;
l->name = _strdup(name);
l->next = NULL;
l->index = nextlocaleindex++;
assert(nextlocaleindex <= MAXLOCALES);
*lp = l;
if (default_locale == NULL)
default_locale = l;
return l;
if (!locales) {
nextlocaleindex = 0;
} else {
while (*lp && (*lp)->hashkey != hkey) lp = &(*lp)->next;
if (*lp) {
return *lp;
}
}
*lp = l = (locale *)calloc(sizeof(locale), 1);
l->hashkey = hkey;
l->name = _strdup(name);
l->index = nextlocaleindex++;
assert(nextlocaleindex <= MAXLOCALES);
if (default_locale == NULL) default_locale = l;
return l;
}
/** creates a list of locales
@ -92,7 +88,7 @@ void make_locales(const char *str)
++tok;
strncpy(zText, str, tok - str);
zText[tok - str] = 0;
make_locale(zText);
get_or_create_locale(zText);
if (*tok) {
str = ++tok;
}

View File

@ -27,8 +27,8 @@ extern "C" {
struct locale;
/** managing multiple locales: **/
extern struct locale *find_locale(const char *name);
extern struct locale *make_locale(const char *key);
extern struct locale *get_locale(const char *name);
extern struct locale *get_or_create_locale(const char *key);
extern void free_locales(void);
/** operations on locales: **/

View File

@ -1,5 +1,8 @@
-- new tests 2014-06-11
-- require "tests.ships"
require "tests.settings"
require "tests.config"
require "tests.locale"
require "tests.regions"
--require "tests.ships"

View File

@ -18,5 +18,3 @@ function test_create()
r = region.create(0, 0, "ocean")
assert_not_nil(r)
end

View File

@ -26,9 +26,23 @@ function setup()
}]]
eressea.config.parse(conf)
eressea.locale.create("en")
end
function test_landing1()
function test_sail()
local r1 = region.create(0, 0, "ocean")
local r2 = region.create(1, 0, "ocean")
local f = faction.create("test@example.com", "human", "de")
local u = unit.create(f, r1, 1)
u.ship = ship.create(r1, "boat")
u:set_skill("sailing", 10)
u:add_order("NACH O")
process_orders()
-- eressea.process.movement()
assert_equal(r2, u.region)
end
function notest_landing1()
local ocean = region.create(1, 0, "ocean")
local r = region.create(0, 0, "plain")
local f = faction.create("noreply@eressea.de", "insect", "de")