From 12e15978b813bb80fe5d334cb912cc7c2b059991 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 25 Jun 2014 08:00:09 -0700 Subject: [PATCH] read weight, capacity and flags of items --- src/kernel/item.h | 10 +++++----- src/kernel/jsonconf.c | 22 +++++++++++++++++++++- src/kernel/jsonconf.test.c | 17 ++++++++++++++--- src/util/log.c | 3 ++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/kernel/item.h b/src/kernel/item.h index 8a1b19e45..fd4073300 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -113,11 +113,11 @@ extern "C" { /* bitfield values for item_type::flags */ #define ITF_NONE 0x0000 #define ITF_HERB 0x0001 /* this item is a herb */ -#define ITF_CURSED 0x0010 /* cursed object, cannot be given away */ -#define ITF_NOTLOST 0x0020 /* special object (quests), cannot be lost through death etc. */ -#define ITF_BIG 0x0040 /* big item, e.g. does not fit in a bag of holding */ -#define ITF_ANIMAL 0x0080 /* an animal */ -#define ITF_VEHICLE 0x0100 /* a vehicle, drawn by two animals */ +#define ITF_CURSED 0x0002 /* cursed object, cannot be given away */ +#define ITF_NOTLOST 0x0004 /* special object (quests), cannot be lost through death etc. */ +#define ITF_BIG 0x0008 /* big item, e.g. does not fit in a bag of holding */ +#define ITF_ANIMAL 0x0010 /* an animal */ +#define ITF_VEHICLE 0x0020 /* a vehicle, drawn by two animals */ /* error codes for item_type::use */ #define ECUSTOM -1 diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index dbc999d85..415684e1b 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -139,14 +139,34 @@ void json_building(cJSON *json, building_type *bt) { } } -void json_item(cJSON *json, item_type *st) { +void json_item(cJSON *json, item_type *itype) { cJSON *child; + const char *flags[] = { + "herb", "cursed", "nodrop", "big", "animal", "vehicle", 0 + }; if (json->type!=cJSON_Object) { log_error_n("ship %s is not a json object: %d", json->string, json->type); return; } for (child=json->child;child;child=child->next) { switch(child->type) { + case cJSON_Number: + if (strcmp(child->string, "weight")==0) { + itype->weight = child->valueint; + break; + } + if (strcmp(child->string, "capacity")==0) { + itype->capacity = child->valueint; + break; + } + log_error_n("item %s contains unknown attribute %s", json->string, child->string); + break; + case cJSON_Array: + if (strcmp(child->string, "flags")==0) { + itype->flags = json_flags(child, flags); + break; + } + log_error_n("item %s contains unknown attribute %s", json->string, child->string); case cJSON_Object: default: log_error_n("item %s contains unknown attribute %s", json->string, child->string); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 9b9d3ed61..2e104448e 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -86,10 +86,11 @@ static void test_races(CuTest * tc) static void test_items(CuTest * tc) { const char * data = "{\"items\": { " - "\"axe\" : {}," - "\"horse\" : {}" + "\"axe\" : { \"weight\" : 2}," + "\"horse\" : { \"flags\" : [ \"animal\", \"big\" ], \"capacity\" : 20 }" "}}"; cJSON *json = cJSON_Parse(data); + const item_type * itype; test_cleanup(); @@ -99,7 +100,17 @@ static void test_items(CuTest * tc) CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); json_config(json); - CuAssertPtrNotNull(tc, it_find("axe")); + + itype = it_find("axe"); + CuAssertPtrNotNull(tc, itype); + CuAssertIntEquals(tc, 2, itype->weight); + CuAssertIntEquals(tc, 0, itype->flags); + + itype = it_find("horse"); + CuAssertPtrNotNull(tc, itype); + CuAssertIntEquals(tc, 20, itype->capacity); + CuAssertIntEquals(tc, ITF_ANIMAL|ITF_BIG, itype->flags); + CuAssertPtrNotNull(tc, rt_find("axe")); CuAssertPtrNotNull(tc, (void *)get_resourcetype(R_HORSE)); } diff --git a/src/util/log.c b/src/util/log.c index 3cab62b42..6b371332b 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -235,6 +235,7 @@ void log_error_n(const char *format, ...) va_list args; va_start(args, format); _log_write(logfile, 0, prefix, format, args); + fputc('\n', logfile); va_end(args); } @@ -245,7 +246,7 @@ void log_error_n(const char *format, ...) va_list args; va_start(args, format); _log_write(stderr, stdio_codepage, prefix, format, args); - fputc('\n', logfile); + fputc('\n', stderr); va_end(args); } }