From 5c78a3883e4999f8c4d4f45b1884b50fd2b9ec42 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 16 Jun 2014 22:43:40 -0700 Subject: [PATCH] cleaning up terrains, reading race flags from a JSON array --- res/e3a/terrains.xml | 6 ++-- res/terrains.xml | 6 ++-- src/kernel/jsonconf.c | 59 +++++++++++++++++++------------------- src/kernel/jsonconf.test.c | 6 ++-- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/res/e3a/terrains.xml b/res/e3a/terrains.xml index f473fd500..c4c5ff70a 100644 --- a/res/e3a/terrains.xml +++ b/res/e3a/terrains.xml @@ -1,8 +1,8 @@ - - - + + + diff --git a/res/terrains.xml b/res/terrains.xml index 5fb289d3b..3ccfa4606 100644 --- a/res/terrains.xml +++ b/res/terrains.xml @@ -1,8 +1,8 @@ - - - + + + diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 6bb0c6360..558c40b08 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -52,6 +52,23 @@ without prior permission by the authors of Eressea. #include #include +int json_flags(cJSON *json, const char *flags[]) { + cJSON *entry; + int result = 0; + assert(json->type==cJSON_Array); + for (entry=json->child;entry;entry=entry->next) { + if (entry->type == cJSON_String) { + int i; + for (i = 0; flags[i]; ++i) { + if (strcmp(flags[i], entry->valuestring)==0) { + result |= (1<type!=cJSON_Object) { @@ -89,20 +106,10 @@ void json_terrain(cJSON *json, terrain_type *ter) { switch(child->type) { case cJSON_Array: if (strcmp(child->string, "flags")==0) { - cJSON *entry; const char * flags[] = { "land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0 }; - for (entry=child->child;entry;entry=entry->next) { - if (entry->type == cJSON_String) { - int i; - for (i = 0; flags[i]; ++i) { - if (strcmp(flags[i], entry->valuestring)==0) { - ter->flags |= (1<flags = json_flags(child, flags); } else { log_error_n("terrain %s contains unknown attribute %s", json->string, child->string); } @@ -162,6 +169,15 @@ void json_ship(cJSON *json, ship_type *st) { void json_race(cJSON *json, race *rc) { cJSON *child; + const char *flags[] = { + "playerrace", "killpeasants", "scarepeasants", + "cansteal", "moverandom", "cannotmove", + "learn", "fly", "swim", "walk", "nolearn", + "noteach", "horse", "desert", + "illusionary", "absorbpeasants", "noheal", + "noweapons", "shapeshift", "", "undead", "dragon", + "coastal", "", "cansail", 0 + }; if (json->type!=cJSON_Object) { log_error_n("race %s is not a json object: %d", json->string, json->type); return; @@ -206,27 +222,12 @@ void json_race(cJSON *json, race *rc) { } // TODO: studyspeed (orcs only) break; - case cJSON_True: { - const char *flags[] = { - "playerrace", "killpeasants", "scarepeasants", - "cansteal", "moverandom", "cannotmove", - "learn", "fly", "swim", "walk", "nolearn", - "noteach", "horse", "desert", - "illusionary", "absorbpeasants", "noheal", - "noweapons", "shapeshift", "", "undead", "dragon", - "coastal", "", "cansail", 0 - }; - int i; - for(i=0;flags[i];++i) { - const char * flag = flags[i]; - if (*flag && strcmp(child->string, flag)==0) { - rc->flags |= (1<string, "flags")==0) { + rc->flags = json_flags(child, flags); } break; } - } } } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 5ee6f8af2..d701802e9 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -18,7 +18,7 @@ static void check_flag(CuTest *tc, const char *name, int flag) { char data[1024]; const struct race *rc; cJSON *json; - sprintf(data, "{\"races\" : { \"orc\": { \"%s\" : true }}}", name); + sprintf(data, "{\"races\" : { \"orc\": { \"flags\" : [ \"%s\"] }}}", name); json = cJSON_Parse(data); free_races(); @@ -52,7 +52,8 @@ static void test_races(CuTest * tc) "\"weight\" : 3," "\"capacity\" : 4," "\"hp\" : 5," - "\"ac\" : 6" + "\"ac\" : 6," + "\"flags\" : [ \"playerrace\", \"walk\", \"undead\" ]" "}}}"; cJSON *json = cJSON_Parse(data); const struct race *rc; @@ -66,6 +67,7 @@ static void test_races(CuTest * tc) CuAssertPtrNotNull(tc, races); rc = rc_find("orc"); CuAssertPtrNotNull(tc, rc); + CuAssertIntEquals(tc, RCF_PLAYERRACE|RCF_WALK|RCF_UNDEAD, rc->flags); CuAssertStrEquals(tc, "1d4", rc->def_damage); CuAssertDblEquals(tc, 1.0, rc->magres, 0.0); CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0);