read weight, capacity and flags of items

This commit is contained in:
Enno Rehling 2014-06-25 08:00:09 -07:00
parent af77a1d8e5
commit 12e15978b8
4 changed files with 42 additions and 10 deletions

View File

@ -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

View File

@ -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);

View File

@ -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));
}

View File

@ -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);
}
}