From 90bcf30628984a8abc8b278bad61a6beeca76607 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 13 Sep 2015 09:46:40 +0200 Subject: [PATCH] fix bad terrain definitions in E2 (string, not number: these values are used for dice throws). clean up the jsonconf.c code for reading these a little bit, report more specific errors. --- conf/e2/terrains.json | 36 ++++++++++++++++++------------------ src/kernel/jsonconf.c | 36 +++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/conf/e2/terrains.json b/conf/e2/terrains.json index 709f1b1a4..470bc8b7c 100644 --- a/conf/e2/terrains.json +++ b/conf/e2/terrains.json @@ -229,21 +229,21 @@ "production": { "iron": { "chance": 0.5, - "level": 1, - "base": 50, - "div": 50 + "level": "1", + "base": "50", + "div": "50" }, "stone": { "chance": 0.5, - "level": 1, - "base": 100, - "div": 100 + "level": "1", + "base": "100", + "div": "100" }, "laen": { "chance": 0.075, - "level": 1, - "base": 4, - "div": 100 + "level": "1", + "base": "4", + "div": "100" } } }, @@ -254,21 +254,21 @@ "production": { "iron": { "chance": 0.5, - "level": 1, - "base": 50, - "div": 50 + "level": "1", + "base": "50", + "div": "50" }, "stone": { "chance": 0.5, - "level": 1, - "base": 100, - "div": 100 + "level": "1", + "base": "100", + "div": "100" }, "laen": { "chance": 0.075, - "level": 1, - "base": 4, - "div": 100 + "level": "1", + "base": "4", + "div": "100" } } }, diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 64ae06477..c8126715a 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -192,32 +192,38 @@ static void json_terrain_production(cJSON *json, terrain_production *prod) { assert(json->type == cJSON_Object); cJSON *child; for (child = json->child; child; child = child->next) { + char **dst = 0; switch (child->type) { - case cJSON_String: - if (strcmp(child->string, "base") == 0) { - prod->base = _strdup(child->valuestring); - } - else if (strcmp(child->string, "level") == 0) { - prod->startlevel = _strdup(child->valuestring); - } - else if (strcmp(child->string, "div") == 0) { - prod->divisor = _strdup(child->valuestring); - } - else { - log_error("terrain_production %s contains unknown attribute %s", json->string, child->string); - } - break; case cJSON_Number: if (strcmp(child->string, "chance") == 0) { prod->chance = (float)child->valuedouble; } else { - log_error("terrain_production %s contains unknown attribute %s", json->string, child->string); + log_error("terrain_production %s contains unknown number %s", json->string, child->string); + } + break; + case cJSON_String: + if (strcmp(child->string, "base") == 0) { + dst = &prod->base; + } + else if (strcmp(child->string, "level") == 0) { + dst = &prod->startlevel; + } + else if (strcmp(child->string, "div") == 0) { + dst = &prod->divisor; + } + else { + log_error("terrain_production %s contains unknown string %s", json->string, child->string); } break; default: log_error("terrain_production %s contains unknown attribute %s", json->string, child->string); } + if (dst) { + free(*dst); + assert(child->type == cJSON_String); + *dst = _strdup(child->valuestring); + } } }