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;
}
static resource_limit *get_resourcelimit(const resource_type *rtype) {
return rtype->limit;
}
static void allocate_resource(unit * u, const resource_type * rtype, int want)
{
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;
allocation_list *alist;
allocation *al;
resource_limit *rdata = get_resourcelimit(rtype);
resource_limit *rdata = rtype->limit;
const resource_type *rring;
int amount, skill, skill_mod = 0;
variant save_mod;
@ -936,8 +932,8 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
|| itype->construction->materials == NULL));
assert(rdata != NULL);
if (rdata->limit != NULL) {
int avail = rdata->limit(r, rtype);
if (!rtype->raw) {
int avail = limit_resource(r, rtype);
if (avail <= 0) {
cmistake(u, u->thisorder, 121, MSG_PRODUCE);
return;
@ -1122,17 +1118,17 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist)
{
allocation *al;
int nreq = 0;
resource_limit *rdata = get_resourcelimit(rtype);
int avail = 0;
for (al = alist; al; al = al->next) {
nreq += required(al->want, al->save);
}
if (rdata->limit) {
avail = rdata->limit(r, rtype);
if (avail < 0)
if (!rtype->raw) {
avail = limit_resource(r, rtype);
if (avail < 0) {
avail = 0;
}
}
avail = MIN(avail, nreq);
@ -1147,10 +1143,11 @@ attrib_allocation(const resource_type * rtype, region * r, allocation * alist)
nreq -= want;
al->get = x * al->save.sa[0] / al->save.sa[1];
al->get = MIN(al->want, al->get);
if (rdata->produce) {
if (!rtype->raw) {
int use = required(al->get, al->save);
if (use)
rdata->produce(r, rtype, use);
if (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)
{
resource_limit *rdata = get_resourcelimit(rtype);
if (rdata) {
if (rdata->limit != NULL) {
return attrib_allocation;
}
if (rtype->raw) {
return leveled_allocation;
}
return NULL;
return attrib_allocation;
}
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/spell.h>
#include <kernel/race.h>
#include <kernel/resources.h>
#include <kernel/unit.h>
#include <kernel/building.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;
}
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];
int result = -1;
@ -110,7 +111,7 @@ static int limit_resource(const region * r, const resource_type * rtype)
}
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;
char fname[64];
@ -564,9 +565,7 @@ void register_tolua_helpers(void)
register_function((pf_generic)lua_maintenance,
TOLUA_CAST "lua_maintenance");
register_function((pf_generic)produce_resource,
TOLUA_CAST "lua_produceresource");
register_function((pf_generic)limit_resource,
TOLUA_CAST "lua_limitresource");
res_produce_fun = produce_resource_lua;
res_limit_fun = limit_resource_lua;
register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem");
}

View File

@ -69,6 +69,7 @@ const resource_type * rtype)
rm->divisor = divisor;
rm->flags = 0;
rm->type = rmt_get(rtype);
assert(rm->type);
update_resource(rm, 1.0);
rm->type->terraform(rm, r);
}
@ -211,3 +212,23 @@ struct rawmaterial_type *rmt_create(struct resource_type *rtype)
rmtype->visible = visible_default;
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;
} 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;
@ -56,8 +51,6 @@ extern "C" {
} resource_mod;
typedef struct resource_limit {
rlimit_limit limit;
rlimit_produce produce;
resource_mod *modifiers;
} resource_limit;
@ -83,6 +76,12 @@ extern "C" {
const 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
}
#endif

View File

@ -933,8 +933,6 @@ static int parse_resources(xmlDocPtr doc)
if (xml_bvalue(node, "pooled", true))
flags |= RTF_POOLED;
if (xml_bvalue(node, "limited", false))
flags |= RTF_LIMITED;
name = xmlGetProp(node, BAD_CAST "name");
if (!name) {
@ -1044,39 +1042,6 @@ static int parse_resources(xmlDocPtr doc)
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);
/* reading eressea/resources/resource/resourcelimit/function */