From 91a776c0acfdec4cb248890cba6bd2dfd71dde7d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 2 Jul 2014 21:15:22 -0700 Subject: [PATCH 1/4] begin configuring spells from JSON --- src/kernel/jsonconf.c | 36 ++++++++++++++++++++++++++++++++++-- src/kernel/jsonconf.test.c | 22 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 0e36ad8f7..f888ab88e 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -345,6 +345,35 @@ static void json_buildings(cJSON *json) { } } +static void json_spells(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Object) { + log_error_n("spells is not a json object: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + if (child->type == cJSON_Object) { + spell *sp; + cJSON * item = cJSON_GetObjectItem(child, "index"); + sp = create_spell(child->string, item ? item->valueint : 0); + for (item = child->child; item; item = item->next) { + if (strcmp(item->string, "index") == 0) { + continue; + } + else if (strcmp(item->string, "cast") == 0) { + sp->cast = (spell_f)get_function(item->valuestring); + } + else if (strcmp(item->string, "fumble") == 0) { + sp->fumble = (fumble_f)get_function(item->valuestring); + } + else if (strcmp(item->string, "syntax") == 0) { + sp->syntax = _strdup(item->valuestring); + } + } + } + } +} + static void json_items(cJSON *json) { cJSON *child; if (json->type!=cJSON_Object) { @@ -560,10 +589,13 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "skills")==0) { json_skills(child); } - else if (strcmp(child->string, "buildings")==0) { + else if (strcmp(child->string, "buildings") == 0) { json_buildings(child); } - else if (strcmp(child->string, "terrains")==0) { + else if (strcmp(child->string, "spells") == 0) { + json_spells(child); + } + else if (strcmp(child->string, "terrains") == 0) { json_terrains(child); } else { log_error_n("config contains unknown attribute %s", child->string); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index e422d4e63..f128acf4a 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -8,6 +8,7 @@ #include "keyword.h" #include "race.h" #include "ship.h" +#include "spell.h" #include "terrain.h" #include "util/language.h" #include @@ -177,6 +178,26 @@ static void test_castles(CuTest *tc) { CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement); } +static void test_spells(CuTest * tc) +{ + const char * data = "{\"spells\": { \"fireball\" : { \"syntax\" : \"u+\" } } }"; + + cJSON *json = cJSON_Parse(data); + const spell *sp; + + test_cleanup(); + CuAssertPtrNotNull(tc, json); + CuAssertPtrEquals(tc, 0, find_spell("fireball")); + + json_config(json); + sp = find_spell("fireball"); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, "u+", sp->syntax); + + test_cleanup(); + CuAssertPtrEquals(tc, 0, find_spell("fireball")); +} + static void test_buildings(CuTest * tc) { const char * data = "{\"buildings\": { \"house\" : { " @@ -331,6 +352,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_races); SUITE_ADD_TEST(suite, test_strings); + SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_flags); return suite; } From b5439a1279f147992ad44b1681a7ab25eb672cdb Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 2 Jul 2014 21:17:31 -0700 Subject: [PATCH 2/4] fix line/column calculation for JSON Errors. + start building a test for http://bugs.eressea.de/view.php?id=1692 --- src/bind_config.c | 3 ++- src/kernel/config.c | 31 ++++++++++++++++-------------- tests/init.lua | 1 + tests/spells.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 tests/spells.lua diff --git a/src/bind_config.c b/src/bind_config.c index 19e99ec77..e0930481f 100644 --- a/src/bind_config.c +++ b/src/bind_config.c @@ -36,8 +36,9 @@ int config_parse(const char *json) int line; char buffer[10]; const char *xp = json, *lp, *ep = cJSON_GetErrorPtr(); - for (line=0,lp=xp;xp && xp=ep) break; } xp = (ep > json + 10) ? ep - 10 : json; strncpy(buffer, xp, sizeof(buffer)); diff --git a/src/kernel/config.c b/src/kernel/config.c index 5aaf90801..4d0fcacfa 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1161,21 +1161,24 @@ int findoption(const char *s, const struct locale *lang) param_t findparam(const char *s, const struct locale * lang) { - param_t result = NOPARAM; - char buffer[64]; - char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), s); - - if (str && *str) { - int i; - const void * match; - void **tokens = get_translations(lang, UT_PARAMS); - critbit_tree *cb = (critbit_tree *)*tokens; - if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { - cb_get_kv(match, &i, sizeof(int)); - result = (param_t)i; + param_t result = NOPARAM; + char buffer[64]; + char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), s); + + if (str && *str) { + int i; + const void * match; + void **tokens = get_translations(lang, UT_PARAMS); + critbit_tree *cb = (critbit_tree *)*tokens; + if (!cb) { + log_error_n("no parameters defined in locale %s", locale_name(lang)); + } + else if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { + cb_get_kv(match, &i, sizeof(int)); + result = (param_t)i; + } } - } - return result; + return result; } param_t findparam_ex(const char *s, const struct locale * lang) diff --git a/tests/init.lua b/tests/init.lua index 032bf94ec..e54c2229f 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -7,3 +7,4 @@ require "tests.ships" require "tests.study" require "tests.movement" require "tests.castles" +require "tests.spells" diff --git a/tests/spells.lua b/tests/spells.lua new file mode 100644 index 000000000..0c6333569 --- /dev/null +++ b/tests/spells.lua @@ -0,0 +1,47 @@ +require "lunit" + +module("tests.spells", package.seeall, lunit.testcase) + +function setup() + eressea.game.reset() + eressea.settings.set("nmr.removenewbie", "0") + eressea.settings.set("nmr.timeout", "0") + conf = [[{ + "races": { + "human" : {} + }, + "terrains" : { + "plain": { "flags" : [ "land", "walk", "sail" ] } + }, + "spells" : { + "resist_magic" : { + "index" : 97, + "parameters" : "u+" + } + }, + "keywords" : { + "de" : { + "cast" : "ZAUBERE" + } + }, + "strings" : { + "de" : { + "harbour" : "Hafen" + } + } + }]] + + eressea.config.reset() + assert(eressea.config.parse(conf)==0) +end + +function test_antimagic_visibility() + local r = region.create(0, 0, "plain") + local f1 = faction.create("test@example.com", "human", "de") + local mage = unit.create(f1, r, 1) + local target = unit.create(f1, r, 1) + mage:set_skill("magic", 10) + mage:add_spell("resist_magic") + mage:add_order("ZAUBERE Antimagie " .. target.id) + process_orders() +end From 2aaabc0f1222cba487f633857b41d5f7bb1d8d72 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 2 Jul 2014 21:22:41 -0700 Subject: [PATCH 3/4] let s/cmake-init take care of mkdir, not configure --- configure | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/configure b/configure index 930b90d45..220805263 100755 --- a/configure +++ b/configure @@ -1,12 +1,6 @@ #!/bin/sh git submodule update --init -MACHINE=`uname -m` -[ -z "$CC" ] && [ ! -z `which gcc` ] && CC="gcc" -[ -z "$CC" ] && [ ! -z `which tcc` ] && CC="tcc" -[ -z "$CC" ] && [ ! -z `which cc` ] && CC="cc" -[ -z "$JOBS" ] && JOBS=$(nproc) -BIN_DIR="build-$MACHINE-$CC-Debug" DISTCC=`which distcc` if [ ! -z "$DISTCC" ] ; then JOBS=`distcc -j` @@ -19,8 +13,6 @@ fi fi echo "Building with $CC and $JOBS jobs" -mkdir -p $BIN_DIR -cd $BIN_DIR -CC="$CC" ../s/cmake-init +CC="$CC" s/cmake-init make -j$JOBS make test From 90120ee5c3952788991c3eb0e9357c987f34cd97 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 2 Jul 2014 21:35:16 -0700 Subject: [PATCH 4/4] stop side-effect of cd - at the end of these scripts --- s/build | 2 +- s/cmake-init | 2 +- s/install | 2 +- s/runtests | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/s/build b/s/build index ab29c20ad..40ab81283 100755 --- a/s/build +++ b/s/build @@ -31,4 +31,4 @@ fi cd $ROOT/$BIN_DIR make $MAKEOPTS && make test -cd - +cd $OLDPWD diff --git a/s/cmake-init b/s/cmake-init index 361825423..eb3a1e067 100755 --- a/s/cmake-init +++ b/s/cmake-init @@ -44,4 +44,4 @@ cmake .. \ -DCMAKE_INCLUDE_PATH=$INCLUDE_PATH \ -DCMAKE_PREFIX_PATH=$PREFIX_PATH \ -DCMAKE_INSTALL_PREFIX=$HOME/eressea $* -cd - +cd $OLDPWD diff --git a/s/install b/s/install index 359254e6a..2391ad00c 100755 --- a/s/install +++ b/s/install @@ -16,4 +16,4 @@ BIN_DIR="build-$MACHINE-$CC-Debug" cd $ROOT/$BIN_DIR make install -cd - +cd $OLDPWD diff --git a/s/runtests b/s/runtests index 68e08b607..35934997b 100755 --- a/s/runtests +++ b/s/runtests @@ -18,4 +18,4 @@ fi $ROOT/$BIN_DIR/eressea/test_eressea cd $ROOT $ROOT/$BIN_DIR/eressea/eressea -v0 scripts/runtests.lua -cd - +cd $OLDWPD