refactor rawmaterial_type.

still have to decide if we need it at all.
This commit is contained in:
Enno Rehling 2017-02-22 21:10:22 +01:00
parent ce2ea95880
commit 1498f2e1d7
8 changed files with 49 additions and 68 deletions

View File

@ -915,8 +915,7 @@ struct message * get_modifiers(unit *u, const resource_mod *mod, variant *savep,
} }
static resource_limit *get_resourcelimit(const resource_type *rtype) { static resource_limit *get_resourcelimit(const resource_type *rtype) {
attrib *a = a_find(rtype->attribs, &at_resourcelimit); return rtype->limit;
return a ? (resource_limit *)a->data.v : NULL;
} }
static void allocate_resource(unit * u, const resource_type * rtype, int want) static void allocate_resource(unit * u, const resource_type * rtype, int want)

View File

@ -348,7 +348,7 @@ static void test_make_item(CuTest *tc) {
struct item_type *itype; struct item_type *itype;
const struct resource_type *rt_silver; const struct resource_type *rt_silver;
resource_type *rtype; resource_type *rtype;
attrib *a; rawmaterial_type *rmt;
resource_limit *rdata; resource_limit *rdata;
double d = 0.6; double d = 0.6;
@ -382,10 +382,9 @@ static void test_make_item(CuTest *tc) {
free(itype->construction->materials); free(itype->construction->materials);
itype->construction->materials = 0; itype->construction->materials = 0;
rtype->flags |= RTF_LIMITED; rtype->flags |= RTF_LIMITED;
a = a_add(&rtype->attribs, a_new(&at_resourcelimit)); rmt = rmt_create(rtype, "rm_stone");
rdata = (resource_limit *)a->data.v; rdata = rtype->limit = calloc(1, sizeof(resource_limit));
rdata->value = 0; rdata->value = 0;
rmt_create(rtype, "stone");
add_resource(u->region, 1, 300, 150, rtype); add_resource(u->region, 1, 300, 150, rtype);
u->region->resources->amount = 300; /* there are 300 stones at level 1 */ u->region->resources->amount = 300; /* there are 300 stones at level 1 */
set_level(u, SK_ALCHEMY, 10); set_level(u, SK_ALCHEMY, 10);

View File

@ -24,6 +24,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/build.h> #include <kernel/build.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/resources.h>
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
@ -31,6 +32,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
#include <stdlib.h>
static void produce_seeds(region * r, const resource_type * rtype, int norders) static void produce_seeds(region * r, const resource_type * rtype, int norders)
{ {
@ -48,14 +50,11 @@ static int limit_seeds(const region * r, const resource_type * rtype)
void init_seed(void) void init_seed(void)
{ {
attrib *a;
resource_limit *rdata;
resource_type *rtype; resource_type *rtype;
rtype = rt_find("seed"); rtype = rt_find("seed");
if (rtype != NULL) { if (rtype != NULL) {
a = a_add(&rtype->attribs, a_new(&at_resourcelimit)); resource_limit *rdata = rtype->limit = calloc(1, sizeof(resource_limit));
rdata = (resource_limit *)a->data.v;
rdata->limit = limit_seeds; rdata->limit = limit_seeds;
rdata->produce = produce_seeds; rdata->produce = produce_seeds;
} }
@ -80,17 +79,14 @@ static int limit_mallornseeds(const region * r, const resource_type * rtype)
void init_mallornseed(void) void init_mallornseed(void)
{ {
attrib *a;
resource_limit *rdata;
resource_type *rtype; resource_type *rtype;
rtype = rt_find("mallornseed"); rtype = rt_find("mallornseed");
if (rtype != NULL) { if (rtype != NULL) {
resource_limit *rdata = rtype->limit = calloc(1, sizeof(resource_limit));
rtype->flags |= RTF_LIMITED; rtype->flags |= RTF_LIMITED;
rtype->flags |= RTF_POOLED; rtype->flags |= RTF_POOLED;
a = a_add(&rtype->attribs, a_new(&at_resourcelimit));
rdata = (resource_limit *)a->data.v;
rdata->limit = limit_mallornseeds; rdata->limit = limit_mallornseeds;
rdata->produce = produce_mallornseeds; rdata->produce = produce_mallornseeds;
} }

View File

@ -1159,22 +1159,6 @@ const item_type *finditemtype(const char *name, const struct locale *lang)
return 0; return 0;
} }
static void init_resourcelimit(attrib * a)
{
a->data.v = calloc(sizeof(resource_limit), 1);
}
static void finalize_resourcelimit(attrib * a)
{
free(a->data.v);
}
attrib_type at_resourcelimit = {
"resourcelimit",
init_resourcelimit,
finalize_resourcelimit,
};
item *item_spoil(const struct race *rc, int size) item *item_spoil(const struct race *rc, int size)
{ {
item *itm = NULL; item *itm = NULL;

View File

@ -29,6 +29,7 @@ extern "C" {
struct unit; struct unit;
struct attrib; struct attrib;
struct attrib_type; struct attrib_type;
struct race;
struct region; struct region;
struct resource_type; struct resource_type;
struct locale; struct locale;
@ -37,6 +38,8 @@ extern "C" {
struct order; struct order;
struct storage; struct storage;
struct gamedata; struct gamedata;
struct rawmaterial_type;
struct resource_limit;
typedef struct item { typedef struct item {
struct item *next; struct item *next;
@ -76,6 +79,8 @@ extern "C" {
rtype_uchange uchange; rtype_uchange uchange;
rtype_uget uget; rtype_uget uget;
rtype_name name; rtype_name name;
struct rawmaterial_type *raw;
struct resource_limit *limit;
/* --- pointers --- */ /* --- pointers --- */
struct attrib *attribs; struct attrib *attribs;
struct item_type *itype; struct item_type *itype;
@ -94,25 +99,6 @@ extern "C" {
#define RMF_SAVEMATERIAL 0x02 /* fraction (sa[0]/sa[1]), multiplier on resource usage */ #define RMF_SAVEMATERIAL 0x02 /* fraction (sa[0]/sa[1]), multiplier on resource usage */
#define RMF_REQUIREDBUILDING 0x04 /* building, required to build */ #define RMF_REQUIREDBUILDING 0x04 /* building, required to build */
typedef struct resource_mod {
variant value;
const struct building_type *btype;
const struct race *race;
unsigned int flags;
} resource_mod;
extern struct attrib_type at_resourcelimit;
typedef int(*rlimit_limit) (const struct region * r,
const struct resource_type * rtype);
typedef void(*rlimit_produce) (struct region * r,
const struct resource_type * rtype, int n);
typedef struct resource_limit {
rlimit_limit limit;
rlimit_produce produce;
int value;
resource_mod *modifiers;
} resource_limit;
/* bitfield values for item_type::flags */ /* bitfield values for item_type::flags */
#define ITF_NONE 0x0000 #define ITF_NONE 0x0000
#define ITF_HERB 0x0001 /* this item is a herb */ #define ITF_HERB 0x0001 /* this item is a herb */

View File

@ -195,16 +195,16 @@ struct rawmaterial_type *rmt_find(const char *str)
struct rawmaterial_type *rmt_get(const struct resource_type *rtype) struct rawmaterial_type *rmt_get(const struct resource_type *rtype)
{ {
rawmaterial_type *rmt = rawmaterialtypes; return rtype->raw;
while (rmt && rmt->rtype != rtype)
rmt = rmt->next;
return rmt;
} }
struct rawmaterial_type *rmt_create(const struct resource_type *rtype, struct rawmaterial_type *rmt_create(struct resource_type *rtype,
const char *name) const char *name)
{ {
rawmaterial_type *rmtype = malloc(sizeof(rawmaterial_type)); rawmaterial_type *rmtype;
assert(!rtype->raw);
rmtype = rtype->raw = malloc(sizeof(rawmaterial_type));
rmtype->name = strdup(name); rmtype->name = strdup(name);
rmtype->rtype = rtype; rmtype->rtype = rtype;
rmtype->terraform = terraform_default; rmtype->terraform = terraform_default;

View File

@ -15,6 +15,9 @@
extern "C" { extern "C" {
#endif #endif
struct building_type;
struct race;
enum { enum {
RM_USED = 1 << 0, /* resource has been used */ RM_USED = 1 << 0, /* resource has been used */
RM_MALLORN = 1 << 1 /* this is not wood. it's mallorn */ RM_MALLORN = 1 << 1 /* this is not wood. it's mallorn */
@ -40,6 +43,25 @@ extern "C" {
struct rawmaterial *next; struct rawmaterial *next;
} rawmaterial; } rawmaterial;
typedef int(*rlimit_limit) (const struct region * r,
const struct resource_type * rtype);
typedef void(*rlimit_produce) (struct region * r,
const struct resource_type * rtype, int n);
typedef struct resource_mod {
variant value;
const struct building_type *btype;
const struct race *race;
unsigned int flags;
} resource_mod;
typedef struct resource_limit {
rlimit_limit limit;
rlimit_produce produce;
int value;
resource_mod *modifiers;
} resource_limit;
typedef struct rawmaterial_type { typedef struct rawmaterial_type {
char *name; char *name;
const struct resource_type *rtype; const struct resource_type *rtype;
@ -64,7 +86,7 @@ extern "C" {
void add_resource(struct region *r, int level, int base, int divisor, void add_resource(struct region *r, int level, int base, int divisor,
const struct resource_type *rtype); const struct resource_type *rtype);
struct rawmaterial_type *rmt_create(const struct resource_type *rtype, struct rawmaterial_type *rmt_create(struct resource_type *rtype,
const char *name); const char *name);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -949,12 +949,6 @@ static int parse_resources(xmlDocPtr doc)
rtype->flags |= flags; rtype->flags |= flags;
xmlFree(name); xmlFree(name);
name = xmlGetProp(node, BAD_CAST "material");
if (name) {
rmt_create(rtype, (const char *)name);
xmlFree(name);
}
/* reading eressea/resources/resource/function */ /* reading eressea/resources/resource/function */
xpath->node = node; xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "function", xpath); result = xmlXPathEvalExpression(BAD_CAST "function", xpath);
@ -987,18 +981,20 @@ static int parse_resources(xmlDocPtr doc)
} }
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
name = xmlGetProp(node, BAD_CAST "material");
if (name) {
rmt_create(rtype, (const char *)name);
xmlFree(name);
}
/* reading eressea/resources/resource/resourcelimit */ /* reading eressea/resources/resource/resourcelimit */
xpath->node = node; xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "resourcelimit", xpath); result = xmlXPathEvalExpression(BAD_CAST "resourcelimit", xpath);
assert(result->nodesetval->nodeNr <= 1); assert(result->nodesetval->nodeNr <= 1);
if (result->nodesetval->nodeNr != 0) { if (result->nodesetval->nodeNr != 0) {
resource_limit *rdata; resource_limit *rdata = rtype->limit = calloc(1, sizeof(resource_limit));
attrib *a = a_find(rtype->attribs, &at_resourcelimit);
xmlNodePtr limit = result->nodesetval->nodeTab[0]; xmlNodePtr limit = result->nodesetval->nodeTab[0];
if (a == NULL)
a = a_add(&rtype->attribs, a_new(&at_resourcelimit));
rdata = (resource_limit *)a->data.v;
rtype->flags |= RTF_LIMITED; rtype->flags |= RTF_LIMITED;
xpath->node = limit; xpath->node = limit;
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
@ -1095,7 +1091,6 @@ static int parse_resources(xmlDocPtr doc)
} }
} }
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
/* reading eressea/resources/resource/resourcelimit/function */ /* reading eressea/resources/resource/resourcelimit/function */
xpath->node = node; xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "resourcelimit/function", xpath); result = xmlXPathEvalExpression(BAD_CAST "resourcelimit/function", xpath);