From 64e0c221eca03b3ea4129d42132a9cd9a0fac8bc Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 15 Jun 2014 22:17:08 -0700 Subject: [PATCH] directions can now be read from json configuration. rename a couple of functions for more consistency across modules. --- src/bind_faction.c | 4 ++-- src/bind_locale.c | 6 ++--- src/bindings.c | 2 +- src/creport.c | 2 +- src/direction.c | 18 ++++++++++++++- src/direction.h | 6 ++++- src/direction_test.c | 46 +++++++++++++++++++++++++------------- src/economy.c | 2 +- src/kernel/build.c | 2 +- src/kernel/config.c | 21 +++++------------ src/kernel/config.h | 3 --- src/kernel/item_test.c | 4 ++-- src/kernel/jsonconf.c | 39 ++++++++++++++++++++++++++++++++ src/kernel/jsonconf_test.c | 27 +++++++++++++++++++++- src/kernel/magic_test.c | 6 ++--- src/kernel/move.c | 2 +- src/kernel/save.c | 6 ++--- src/kernel/xmlreader.c | 4 ++-- src/laws.c | 2 +- src/modules/arena.c | 2 +- src/modules/autoseed.c | 2 +- src/spells/spells.c | 4 ++-- src/tests.c | 2 +- src/tests_test.c | 6 ++--- src/util/language.c | 46 +++++++++++++++++--------------------- src/util/language.h | 4 ++-- tests/init.lua | 5 ++++- tests/regions.lua | 2 -- tests/ships.lua | 16 ++++++++++++- 29 files changed, 193 insertions(+), 98 deletions(-) diff --git a/src/bind_faction.c b/src/bind_faction.c index e733f8525..a13c5f1f4 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -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; } diff --git a/src/bind_locale.c b/src/bind_locale.c index dcfbd9206..a4c2ac0f0 100644 --- a/src/bind_locale.c +++ b/src/bind_locale.c @@ -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); } diff --git a/src/bindings.c b/src/bindings.c index aa863c34d..45e23207c 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -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); diff --git a/src/creport.c b/src/creport.c index 875c5e6e3..640427501 100644 --- a/src/creport.c +++ b/src/creport.c @@ -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 diff --git a/src/direction.c b/src/direction.c index 95ad70926..ff437e937 100644 --- a/src/direction.c +++ b/src/direction.c @@ -5,6 +5,8 @@ #include "util/language.h" #include "util/umlaut.h" +#include + 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" +}; + diff --git a/src/direction.h b/src/direction.h index 6919ecd44..e02b5f9f7 100644 --- a/src/direction.h +++ b/src/direction.h @@ -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 diff --git a/src/direction_test.c b/src/direction_test.c index 409568fee..8552fa643 100644 --- a/src/direction_test.c +++ b/src/direction_test.c @@ -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; } diff --git a/src/economy.c b/src/economy.c index b83d64f79..fb66782ac 100644 --- a/src/economy.c +++ b/src/economy.c @@ -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 { diff --git a/src/kernel/build.c b/src/kernel/build.c index eb19505be..c1afdc02c 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -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; diff --git a/src/kernel/config.c b/src/kernel/config.c index 465f20ef9..e9160fd37 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -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; diff --git a/src/kernel/config.h b/src/kernel/config.h index 7f03776ae..ff52dc104 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -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 */ diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c index c9a144ede..e78799a79 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -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"); diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index a487b7c21..c0f82e776 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -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); } diff --git a/src/kernel/jsonconf_test.c b/src/kernel/jsonconf_test.c index 00443b9b3..779b965d8 100644 --- a/src/kernel/jsonconf_test.c +++ b/src/kernel/jsonconf_test.c @@ -1,10 +1,13 @@ #include #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 #include #include @@ -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); diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index 044375d1d..2a89de23c 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -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"); diff --git a/src/kernel/move.c b/src/kernel/move.c index 30f37b64a..f7facc4c8 100644 --- a/src/kernel/move.c +++ b/src/kernel/move.c @@ -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) { diff --git a/src/kernel/save.c b/src/kernel/save.c index 5599d2fc8..269833d0e 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -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)); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 31226c830..bc2739b95 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -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); diff --git a/src/laws.c b/src/laws.c index 487397b89..6e06ebfb6 100755 --- a/src/laws.c +++ b/src/laws.c @@ -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; diff --git a/src/modules/arena.c b/src/modules/arena.c index 3ebdeff81..e9edd31e8 100644 --- a/src/modules/arena.c +++ b/src/modules/arena.c @@ -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); diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 19ee1e662..a28944b1b 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -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; diff --git a/src/spells/spells.c b/src/spells/spells.c index 08f0dce46..d67ddf2d4 100644 --- a/src/spells/spells.c +++ b/src/spells/spells.c @@ -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 */ diff --git a/src/tests.c b/src/tests.c index 8c27d845b..c95d30fbe 100644 --- a/src/tests.c +++ b/src/tests.c @@ -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]); diff --git a/src/tests_test.c b/src/tests_test.c index 250ea5ec6..efb0fca46 100644 --- a/src/tests_test.c +++ b/src/tests_test.c @@ -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")); diff --git a/src/util/language.c b/src/util/language.c index 67c230c89..57e4fa103 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -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; } diff --git a/src/util/language.h b/src/util/language.h index eb7c81986..aeb79ab64 100644 --- a/src/util/language.h +++ b/src/util/language.h @@ -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: **/ diff --git a/tests/init.lua b/tests/init.lua index f6e5a307c..31ddd376f 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -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" + diff --git a/tests/regions.lua b/tests/regions.lua index ca9e9af45..8b334cef2 100644 --- a/tests/regions.lua +++ b/tests/regions.lua @@ -18,5 +18,3 @@ function test_create() r = region.create(0, 0, "ocean") assert_not_nil(r) end - - diff --git a/tests/ships.lua b/tests/ships.lua index 35ef87fc7..f47131ef7 100644 --- a/tests/ships.lua +++ b/tests/ships.lua @@ -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")