building type construction is in bt_get_or_create, and only there.

+ not in jsonconf
+ not in xmlreader (no test for this)
This commit is contained in:
Enno Rehling 2015-02-02 16:55:18 +01:00
parent 0955dd7f05
commit a4e6f4874c
4 changed files with 62 additions and 10 deletions

View File

@ -111,6 +111,10 @@ building_type *bt_get_or_create(const char *name)
if (btype == NULL) { if (btype == NULL) {
btype = calloc(sizeof(building_type), 1); btype = calloc(sizeof(building_type), 1);
btype->_name = _strdup(name); btype->_name = _strdup(name);
btype->auraregen = 1.0;
btype->maxsize = -1;
btype->capacity = -1;
btype->maxcapacity = -1;
bt_register(btype); bt_register(btype);
} }
return btype; return btype;

View File

@ -357,10 +357,36 @@ void test_buildingowner_goes_to_empty_unit_after_leave(CuTest * tc)
test_cleanup(); 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 *get_building_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_register_building); 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_building_set_owner);
SUITE_ADD_TEST(suite, test_buildingowner_resets_when_empty); SUITE_ADD_TEST(suite, test_buildingowner_resets_when_empty);
SUITE_ADD_TEST(suite, test_buildingowner_goes_to_next_when_empty); SUITE_ADD_TEST(suite, test_buildingowner_goes_to_next_when_empty);

View File

@ -296,6 +296,30 @@ static void test_buildings(CuTest * tc)
test_cleanup(); 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) static void test_configs(CuTest * tc)
{ {
const char * data = "{\"include\": [ \"test.json\" ] }"; 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_items);
SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_ships);
SUITE_ADD_TEST(suite, test_buildings); SUITE_ADD_TEST(suite, test_buildings);
SUITE_ADD_TEST(suite, test_buildings_default);
SUITE_ADD_TEST(suite, test_configs); SUITE_ADD_TEST(suite, test_configs);
SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_castles);
SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_terrains);

View File

@ -244,14 +244,14 @@ static int parse_buildings(xmlDocPtr doc)
btype = bt_get_or_create((const char *)propValue); btype = bt_get_or_create((const char *)propValue);
xmlFree(propValue); xmlFree(propValue);
btype->capacity = xml_ivalue(node, "capacity", -1); btype->capacity = xml_ivalue(node, "capacity", btype->capacity);
btype->maxcapacity = xml_ivalue(node, "maxcapacity", -1); btype->maxcapacity = xml_ivalue(node, "maxcapacity", btype->maxcapacity);
btype->maxsize = xml_ivalue(node, "maxsize", -1); btype->maxsize = xml_ivalue(node, "maxsize", btype->maxsize);
btype->magres = xml_ivalue(node, "magres", 0); btype->magres = xml_ivalue(node, "magres", btype->magres);
btype->magresbonus = xml_ivalue(node, "magresbonus", 0); btype->magresbonus = xml_ivalue(node, "magresbonus", btype->magresbonus);
btype->fumblebonus = xml_ivalue(node, "fumblebonus", 0); btype->fumblebonus = xml_ivalue(node, "fumblebonus", btype->fumblebonus);
btype->auraregen = xml_fvalue(node, "auraregen", 1.0); btype->auraregen = xml_fvalue(node, "auraregen", btype->auraregen);
if (xml_bvalue(node, "nodestroy", false)) if (xml_bvalue(node, "nodestroy", false))
btype->flags |= BTF_INDESTRUCTIBLE; btype->flags |= BTF_INDESTRUCTIBLE;
@ -305,9 +305,6 @@ static int parse_buildings(xmlDocPtr doc)
else if (strcmp((const char *)propValue, "taxes") == 0) { else if (strcmp((const char *)propValue, "taxes") == 0) {
btype->taxes = (double(*)(const struct building *, int))fun; btype->taxes = (double(*)(const struct building *, int))fun;
} }
else if (strcmp((const char *)propValue, "age") == 0) {
btype->age = (void(*)(struct building *))fun;
}
else { else {
log_error("unknown function type '%s' for building %s\n", (const char *)propValue, btype->_name); log_error("unknown function type '%s' for building %s\n", (const char *)propValue, btype->_name);
} }