From a4e6f4874cd73e10e9b7a59ab1b1c9d91fb8b444 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 2 Feb 2015 16:55:18 +0100 Subject: [PATCH] building type construction is in bt_get_or_create, and only there. + not in jsonconf + not in xmlreader (no test for this) --- src/kernel/building.c | 4 ++++ src/kernel/building.test.c | 26 ++++++++++++++++++++++++++ src/kernel/jsonconf.test.c | 25 +++++++++++++++++++++++++ src/kernel/xmlreader.c | 17 +++++++---------- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/kernel/building.c b/src/kernel/building.c index 2877f50fe..48241c398 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -111,6 +111,10 @@ building_type *bt_get_or_create(const char *name) if (btype == NULL) { btype = calloc(sizeof(building_type), 1); btype->_name = _strdup(name); + btype->auraregen = 1.0; + btype->maxsize = -1; + btype->capacity = -1; + btype->maxcapacity = -1; bt_register(btype); } return btype; diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 6a7d0c17a..72439bad6 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -357,10 +357,36 @@ void test_buildingowner_goes_to_empty_unit_after_leave(CuTest * tc) test_cleanup(); } +static void test_btype_defaults(CuTest *tc) { + building_type * btype; + test_cleanup(); + + btype = bt_get_or_create("hodor"); + CuAssertPtrNotNull(tc, btype); + CuAssertPtrEquals(tc, 0, btype->maintenance); + CuAssertPtrEquals(tc, 0, btype->construction); + CuAssertTrue(tc, !btype->name); + CuAssertTrue(tc, !btype->init); + CuAssertTrue(tc, !btype->age); + CuAssertTrue(tc, !btype->protection); + CuAssertTrue(tc, !btype->taxes); + CuAssertStrEquals(tc, "hodor", btype->_name); + CuAssertDblEquals(tc, 1.0, btype->auraregen, 0.0); + CuAssertIntEquals(tc, -1, btype->maxsize); + CuAssertIntEquals(tc, -1, btype->capacity); + CuAssertIntEquals(tc, -1, btype->maxcapacity); + CuAssertIntEquals(tc, 0, btype->magres); + CuAssertIntEquals(tc, 0, btype->magresbonus); + CuAssertIntEquals(tc, 0, btype->fumblebonus); + CuAssertIntEquals(tc, 0, btype->flags); + test_cleanup(); +} + CuSuite *get_building_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_register_building); + SUITE_ADD_TEST(suite, test_btype_defaults); SUITE_ADD_TEST(suite, test_building_set_owner); SUITE_ADD_TEST(suite, test_buildingowner_resets_when_empty); SUITE_ADD_TEST(suite, test_buildingowner_goes_to_next_when_empty); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 3bba66a88..177340fe0 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -296,6 +296,30 @@ static void test_buildings(CuTest * tc) test_cleanup(); } +static const char * building_defaults_data = "{\"buildings\": { " +"\"house\" : { }" +"}}"; + +static void test_buildings_default(CuTest * tc) +{ + cJSON *json = cJSON_Parse(building_defaults_data); + const building_type *bt; + building_type clone; + + test_cleanup(); + + bt = bt_get_or_create("house"); + clone = *bt; + + CuAssertIntEquals(tc, 0, memcmp(bt, &clone, sizeof(building_type))); + CuAssertPtrNotNull(tc, json); + json_config(json); + + CuAssertPtrEquals(tc, (void *)bt, (void *)bt_find("house")); + CuAssertIntEquals(tc, 0, memcmp(bt, &clone, sizeof(building_type))); + test_cleanup(); +} + static void test_configs(CuTest * tc) { const char * data = "{\"include\": [ \"test.json\" ] }"; @@ -448,6 +472,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_items); SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_buildings); + SUITE_ADD_TEST(suite, test_buildings_default); SUITE_ADD_TEST(suite, test_configs); SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_terrains); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 055615d02..5d4c8dc99 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -244,14 +244,14 @@ static int parse_buildings(xmlDocPtr doc) btype = bt_get_or_create((const char *)propValue); xmlFree(propValue); - btype->capacity = xml_ivalue(node, "capacity", -1); - btype->maxcapacity = xml_ivalue(node, "maxcapacity", -1); - btype->maxsize = xml_ivalue(node, "maxsize", -1); + btype->capacity = xml_ivalue(node, "capacity", btype->capacity); + btype->maxcapacity = xml_ivalue(node, "maxcapacity", btype->maxcapacity); + btype->maxsize = xml_ivalue(node, "maxsize", btype->maxsize); - btype->magres = xml_ivalue(node, "magres", 0); - btype->magresbonus = xml_ivalue(node, "magresbonus", 0); - btype->fumblebonus = xml_ivalue(node, "fumblebonus", 0); - btype->auraregen = xml_fvalue(node, "auraregen", 1.0); + btype->magres = xml_ivalue(node, "magres", btype->magres); + btype->magresbonus = xml_ivalue(node, "magresbonus", btype->magresbonus); + btype->fumblebonus = xml_ivalue(node, "fumblebonus", btype->fumblebonus); + btype->auraregen = xml_fvalue(node, "auraregen", btype->auraregen); if (xml_bvalue(node, "nodestroy", false)) btype->flags |= BTF_INDESTRUCTIBLE; @@ -305,9 +305,6 @@ static int parse_buildings(xmlDocPtr doc) else if (strcmp((const char *)propValue, "taxes") == 0) { btype->taxes = (double(*)(const struct building *, int))fun; } - else if (strcmp((const char *)propValue, "age") == 0) { - btype->age = (void(*)(struct building *))fun; - } else { log_error("unknown function type '%s' for building %s\n", (const char *)propValue, btype->_name); }