no more funpointers in resource_limit.

change how resource limits in lua are called.
This commit is contained in:
Enno Rehling 2017-02-26 13:19:47 +01:00
parent 84c6a4b7b5
commit 0738090f28
5 changed files with 45 additions and 69 deletions

View File

@ -914,10 +914,6 @@ struct message * get_modifiers(unit *u, const resource_mod *mod, variant *savep,
return NULL; return NULL;
} }
static resource_limit *get_resourcelimit(const resource_type *rtype) {
return rtype->limit;
}
static void allocate_resource(unit * u, const resource_type * rtype, int want) static void allocate_resource(unit * u, const resource_type * rtype, int want)
{ {
const item_type *itype = resource2item(rtype); const item_type *itype = resource2item(rtype);
@ -925,7 +921,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
int dm = 0; int dm = 0;
allocation_list *alist; allocation_list *alist;
allocation *al; allocation *al;
resource_limit *rdata = get_resourcelimit(rtype); resource_limit *rdata = rtype->limit;
const resource_type *rring; const resource_type *rring;
int amount, skill, skill_mod = 0; int amount, skill, skill_mod = 0;
variant save_mod; variant save_mod;
@ -936,8 +932,8 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
|| itype->construction->materials == NULL)); || itype->construction->materials == NULL));
assert(rdata != NULL); assert(rdata != NULL);
if (rdata->limit != NULL) { if (!rtype->raw) {
int avail = rdata->limit(r, rtype); int avail = limit_resource(r, rtype);
if (avail <= 0) { if (avail <= 0) {
cmistake(u, u->thisorder, 121, MSG_PRODUCE); cmistake(u, u->thisorder, 121, MSG_PRODUCE);
return; return;
@ -1122,17 +1118,17 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist)
{ {
allocation *al; allocation *al;
int nreq = 0; int nreq = 0;
resource_limit *rdata = get_resourcelimit(rtype);
int avail = 0; int avail = 0;
for (al = alist; al; al = al->next) { for (al = alist; al; al = al->next) {
nreq += required(al->want, al->save); nreq += required(al->want, al->save);
} }
if (rdata->limit) { if (!rtype->raw) {
avail = rdata->limit(r, rtype); avail = limit_resource(r, rtype);
if (avail < 0) if (avail < 0) {
avail = 0; avail = 0;
}
} }
avail = MIN(avail, nreq); avail = MIN(avail, nreq);
@ -1147,10 +1143,11 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist)
nreq -= want; nreq -= want;
al->get = x * al->save.sa[0] / al->save.sa[1]; al->get = x * al->save.sa[0] / al->save.sa[1];
al->get = MIN(al->want, al->get); al->get = MIN(al->want, al->get);
if (rdata->produce) { if (!rtype->raw) {
int use = required(al->get, al->save); int use = required(al->get, al->save);
if (use) if (use) {
rdata->produce(r, rtype, use); produce_resource(r, rtype, use);
}
} }
} }
} }
@ -1162,15 +1159,10 @@ typedef void(*allocate_function) (const resource_type *, struct region *,
static allocate_function get_allocator(const struct resource_type *rtype) static allocate_function get_allocator(const struct resource_type *rtype)
{ {
resource_limit *rdata = get_resourcelimit(rtype); if (rtype->raw) {
if (rdata) {
if (rdata->limit != NULL) {
return attrib_allocation;
}
return leveled_allocation; return leveled_allocation;
} }
return NULL; return attrib_allocation;
} }
void split_allocations(region * r) void split_allocations(region * r)

View File

@ -27,6 +27,7 @@ without prior permission by the authors of Eressea.
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/resources.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/item.h> #include <kernel/item.h>
@ -78,7 +79,7 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *o
return result; return result;
} }
static int limit_resource(const region * r, const resource_type * rtype) static int limit_resource_lua(const region * r, const resource_type * rtype)
{ {
char fname[64]; char fname[64];
int result = -1; int result = -1;
@ -110,7 +111,7 @@ static int limit_resource(const region * r, const resource_type * rtype)
} }
static void static void
produce_resource(region * r, const resource_type * rtype, int norders) produce_resource_lua(region * r, const resource_type * rtype, int norders)
{ {
lua_State *L = (lua_State *)global.vm_state; lua_State *L = (lua_State *)global.vm_state;
char fname[64]; char fname[64];
@ -564,9 +565,7 @@ void register_tolua_helpers(void)
register_function((pf_generic)lua_maintenance, register_function((pf_generic)lua_maintenance,
TOLUA_CAST "lua_maintenance"); TOLUA_CAST "lua_maintenance");
register_function((pf_generic)produce_resource, res_produce_fun = produce_resource_lua;
TOLUA_CAST "lua_produceresource"); res_limit_fun = limit_resource_lua;
register_function((pf_generic)limit_resource,
TOLUA_CAST "lua_limitresource");
register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem"); register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem");
} }

View File

@ -69,6 +69,7 @@ const resource_type * rtype)
rm->divisor = divisor; rm->divisor = divisor;
rm->flags = 0; rm->flags = 0;
rm->type = rmt_get(rtype); rm->type = rmt_get(rtype);
assert(rm->type);
update_resource(rm, 1.0); update_resource(rm, 1.0);
rm->type->terraform(rm, r); rm->type->terraform(rm, r);
} }
@ -211,3 +212,23 @@ struct rawmaterial_type *rmt_create(struct resource_type *rtype)
rmtype->visible = visible_default; rmtype->visible = visible_default;
return rmtype; return rmtype;
} }
int(*res_limit_fun)(const struct region *, const struct resource_type *);
void(*res_produce_fun)(struct region *, const struct resource_type *, int);
int limit_resource(const struct region *r, const resource_type *rtype)
{
assert(!rtype->raw);
if (res_limit_fun) {
return res_limit_fun(r, rtype);
}
return -1;
}
void produce_resource(struct region *r, const struct resource_type *rtype, int amount)
{
assert(!rtype->raw);
if (res_produce_fun) {
res_produce_fun(r, rtype, amount);
}
}

View File

@ -43,11 +43,6 @@ 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 { typedef struct resource_mod {
variant value; variant value;
const struct building_type *btype; const struct building_type *btype;
@ -56,8 +51,6 @@ extern "C" {
} resource_mod; } resource_mod;
typedef struct resource_limit { typedef struct resource_limit {
rlimit_limit limit;
rlimit_produce produce;
resource_mod *modifiers; resource_mod *modifiers;
} resource_limit; } resource_limit;
@ -83,6 +76,12 @@ extern "C" {
const struct resource_type *rtype); const struct resource_type *rtype);
struct rawmaterial_type *rmt_create(struct resource_type *rtype); struct rawmaterial_type *rmt_create(struct resource_type *rtype);
extern int(*res_limit_fun)(const struct region *, const struct resource_type *);
extern void(*res_produce_fun)(struct region *, const struct resource_type *, int);
int limit_resource(const struct region *r, const struct resource_type *rtype);
void produce_resource(struct region *r, const struct resource_type *rtype, int amount);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -933,8 +933,6 @@ static int parse_resources(xmlDocPtr doc)
if (xml_bvalue(node, "pooled", true)) if (xml_bvalue(node, "pooled", true))
flags |= RTF_POOLED; flags |= RTF_POOLED;
if (xml_bvalue(node, "limited", false))
flags |= RTF_LIMITED;
name = xmlGetProp(node, BAD_CAST "name"); name = xmlGetProp(node, BAD_CAST "name");
if (!name) { if (!name) {
@ -1044,39 +1042,6 @@ static int parse_resources(xmlDocPtr doc)
xmlFree(propValue); xmlFree(propValue);
} }
} }
xmlXPathFreeObject(result);
/* reading eressea/resources/resource/resourcelimit/function */
result = xmlXPathEvalExpression(BAD_CAST "function", xpath);
if (result->nodesetval != NULL) {
for (k = 0; k != result->nodesetval->nodeNr; ++k) {
xmlNodePtr node = result->nodesetval->nodeTab[k];
pf_generic fun;
propValue = xmlGetProp(node, BAD_CAST "value");
assert(propValue != NULL);
fun = get_function((const char *)propValue);
if (fun == NULL) {
log_error("unknown limit '%s' for resource %s\n", (const char *)propValue, rtype->_name);
xmlFree(propValue);
continue;
}
xmlFree(propValue);
propValue = xmlGetProp(node, BAD_CAST "name");
assert(propValue != NULL);
if (strcmp((const char *)propValue, "produce") == 0) {
rdata->produce = (rlimit_produce)fun;
}
else if (strcmp((const char *)propValue, "limit") == 0) {
rdata->limit = (rlimit_limit)fun;
}
else {
log_error("unknown limit '%s' for resource %s\n", (const char *)propValue, rtype->_name);
}
xmlFree(propValue);
}
}
} }
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
/* reading eressea/resources/resource/resourcelimit/function */ /* reading eressea/resources/resource/resourcelimit/function */