From d4b973fea4eb2791693f052cf1aca07a4511b23a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 26 Feb 2017 13:55:19 +0100 Subject: [PATCH] remove resource_limit struct indirection. --- src/economy.c | 5 ++--- src/economy.test.c | 18 ++++++++---------- src/kernel/item.c | 9 +++------ src/kernel/item.h | 4 ++-- src/kernel/resources.h | 4 ---- src/kernel/xmlreader.c | 19 +++++++++---------- 6 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/economy.c b/src/economy.c index f7f23eb16..ab6862aa4 100644 --- a/src/economy.c +++ b/src/economy.c @@ -921,7 +921,6 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want) int dm = 0; allocation_list *alist; allocation *al; - resource_limit *rdata = rtype->limit; const resource_type *rring; int amount, skill, skill_mod = 0; variant save_mod; @@ -944,8 +943,8 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want) return; } - if (rdata && rdata->modifiers) { - message *msg = get_modifiers(u, rdata->modifiers, &save_mod, &skill_mod); + if (rtype->modifiers) { + message *msg = get_modifiers(u, rtype->modifiers, &save_mod, &skill_mod); if (msg) { ADDMSG(&u->faction->msgs, msg); return; diff --git a/src/economy.test.c b/src/economy.test.c index 6d9c84542..8b1b0b0fa 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -348,7 +348,6 @@ static void test_make_item(CuTest *tc) { struct item_type *itype; const struct resource_type *rt_silver; resource_type *rtype; - resource_limit *rdata; double d = 0.6; test_setup(); @@ -382,7 +381,6 @@ static void test_make_item(CuTest *tc) { itype->construction->materials = 0; rtype->flags |= RTF_LIMITED; rmt_create(rtype); - rdata = rtype->limit = calloc(1, sizeof(resource_limit)); add_resource(u->region, 1, 300, 150, rtype); u->region->resources->amount = 300; /* there are 300 stones at level 1 */ set_level(u, SK_ALCHEMY, 10); @@ -392,11 +390,11 @@ static void test_make_item(CuTest *tc) { CuAssertIntEquals(tc, 11, get_item(u, itype)); CuAssertIntEquals(tc, 290, u->region->resources->amount); /* used 10 stones to make 10 stones */ - rdata->modifiers = calloc(2, sizeof(resource_mod)); - rdata->modifiers[0].flags = RMF_SAVEMATERIAL; - rdata->modifiers[0].race = u->_race; - rdata->modifiers[0].value.sa[0] = (short)(0.5+100*d); - rdata->modifiers[0].value.sa[1] = 100; + rtype->modifiers = calloc(2, sizeof(resource_mod)); + rtype->modifiers[0].flags = RMF_SAVEMATERIAL; + rtype->modifiers[0].race = u->_race; + rtype->modifiers[0].value.sa[0] = (short)(0.5+100*d); + rtype->modifiers[0].value.sa[1] = 100; make_item(u, itype, 10); split_allocations(u->region); CuAssertIntEquals(tc, 21, get_item(u, itype)); @@ -407,9 +405,9 @@ static void test_make_item(CuTest *tc) { CuAssertIntEquals(tc, 22, get_item(u, itype)); CuAssertIntEquals(tc, 283, u->region->resources->amount); /* no free lunches */ - rdata->modifiers[0].flags = RMF_REQUIREDBUILDING; - rdata->modifiers[0].race = NULL; - rdata->modifiers[0].btype = bt_get_or_create("mine"); + rtype->modifiers[0].flags = RMF_REQUIREDBUILDING; + rtype->modifiers[0].race = NULL; + rtype->modifiers[0].btype = bt_get_or_create("mine"); make_item(u, itype, 10); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error104")); diff --git a/src/kernel/item.c b/src/kernel/item.c index 6e64c147f..38d2e8711 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1236,15 +1236,12 @@ void free_rtype(resource_type *rtype) { if (rtype->wtype) { free_wtype(rtype->wtype); } - if (rtype->atype) { - free(rtype->atype); - } if (rtype->itype) { free_itype(rtype->itype); } - if (rtype->raw) { - free(rtype->raw); - } + free(rtype->atype); + free(rtype->modifiers); + free(rtype->raw); free(rtype->_name); free(rtype); } diff --git a/src/kernel/item.h b/src/kernel/item.h index c1ac38517..4351d611d 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -39,7 +39,7 @@ extern "C" { struct storage; struct gamedata; struct rawmaterial_type; - struct resource_limit; + struct resource_mod; typedef struct item { struct item *next; @@ -80,7 +80,7 @@ extern "C" { rtype_uget uget; rtype_name name; struct rawmaterial_type *raw; - struct resource_limit *limit; + struct resource_mod *modifiers; /* --- pointers --- */ struct attrib *attribs; struct item_type *itype; diff --git a/src/kernel/resources.h b/src/kernel/resources.h index 6ee021fc1..b448425f4 100644 --- a/src/kernel/resources.h +++ b/src/kernel/resources.h @@ -50,10 +50,6 @@ extern "C" { unsigned int flags; } resource_mod; - typedef struct resource_limit { - resource_mod *modifiers; - } resource_limit; - typedef struct rawmaterial_type { const struct resource_type *rtype; diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index d4c31001e..3cf8b8601 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -988,7 +988,6 @@ static int parse_resources(xmlDocPtr doc) result = xmlXPathEvalExpression(BAD_CAST "resourcelimit", xpath); assert(result->nodesetval->nodeNr <= 1); if (result->nodesetval->nodeNr != 0) { - resource_limit *rdata = rtype->limit = calloc(1, sizeof(resource_limit)); xmlNodePtr limit = result->nodesetval->nodeTab[0]; xpath->node = limit; @@ -996,7 +995,7 @@ static int parse_resources(xmlDocPtr doc) result = xmlXPathEvalExpression(BAD_CAST "modifier", xpath); if (result->nodesetval != NULL) { - rdata->modifiers = + rtype->modifiers = calloc(result->nodesetval->nodeNr + 1, sizeof(resource_mod)); for (k = 0; k != result->nodesetval->nodeNr; ++k) { xmlNodePtr node = result->nodesetval->nodeTab[k]; @@ -1010,31 +1009,31 @@ static int parse_resources(xmlDocPtr doc) rc = rc_get_or_create((const char *)propValue); xmlFree(propValue); } - rdata->modifiers[k].race = rc; + rtype->modifiers[k].race = rc; propValue = xmlGetProp(node, BAD_CAST "building"); if (propValue != NULL) { btype = bt_get_or_create((const char *)propValue); xmlFree(propValue); } - rdata->modifiers[k].btype = btype; + rtype->modifiers[k].btype = btype; propValue = xmlGetProp(node, BAD_CAST "type"); assert(propValue != NULL); if (strcmp((const char *)propValue, "skill") == 0) { - rdata->modifiers[k].value.i = xml_ivalue(node, "value", 0); - rdata->modifiers[k].flags = RMF_SKILL; + rtype->modifiers[k].value.i = xml_ivalue(node, "value", 0); + rtype->modifiers[k].flags = RMF_SKILL; } else if (strcmp((const char *)propValue, "material") == 0) { - rdata->modifiers[k].value = xml_fraction(node, "value"); - rdata->modifiers[k].flags = RMF_SAVEMATERIAL; + rtype->modifiers[k].value = xml_fraction(node, "value"); + rtype->modifiers[k].flags = RMF_SAVEMATERIAL; } else if (strcmp((const char *)propValue, "require") == 0) { xmlChar *propBldg = xmlGetProp(node, BAD_CAST "building"); if (propBldg != NULL) { btype = bt_get_or_create((const char *)propBldg); - rdata->modifiers[k].btype = btype; - rdata->modifiers[k].flags = RMF_REQUIREDBUILDING; + rtype->modifiers[k].btype = btype; + rtype->modifiers[k].flags = RMF_REQUIREDBUILDING; xmlFree(propBldg); } }