From a3ff8781c45dec1fd215a33ae8a8df9388b24132 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Aug 2016 19:04:48 +0200 Subject: [PATCH] setting herbs will warn if they are the wrong herbs. planes can be erased. add a script that erases unused planes from E2. --- scripts/kill-planes.lua | 23 +++++++++++++++++++++++ scripts/run-turn.lua | 1 - src/bind_region.c | 8 ++++++++ src/kernel/config.c | 3 +-- src/kernel/plane.c | 30 ++++++++++++++++++++++++++++++ src/kernel/plane.h | 13 ++++++++----- src/kernel/region.c | 27 +++++++++++++++++++++++---- src/kernel/region.h | 6 ++++-- src/kernel/save.c | 2 +- 9 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 scripts/kill-planes.lua diff --git a/scripts/kill-planes.lua b/scripts/kill-planes.lua new file mode 100644 index 000000000..182415e2b --- /dev/null +++ b/scripts/kill-planes.lua @@ -0,0 +1,23 @@ +path = 'scripts' +if config.install then + path = config.install .. '/' .. path + package.path = package.path .. ';' .. config.install .. '/lunit/?.lua' + --needed to find lunit if not run from eressea root. Needs right [lua] install setting in eressea.ini (point to eressea root from the start folder) +end +package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua' + +config.rules = 'e2' + +require 'eressea' +require 'eressea.xmlconf' +require 'eressea.path' + +eressea.read_game(get_turn() .. '.dat') +ids = {2081501646, 1967748303, 1137, 2000, 1456894557, 1580742069, 1143084084, 285224813, 604912520, 296884068, 50} + +for k,v in ipairs(ids) do + p = plane.get(v) + print(v, p) + p:erase() +end +eressea.write_game(get_turn() .. '.new') diff --git a/scripts/run-turn.lua b/scripts/run-turn.lua index fc5eebbbf..7ee9165ca 100644 --- a/scripts/run-turn.lua +++ b/scripts/run-turn.lua @@ -3,7 +3,6 @@ function nmr_check(maxnmrs) if nmrs >= maxnmrs then eressea.log.error("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")") write_summary() - eressea.write_game("aborted.dat") return -1 end print (nmrs .. " Factions with 1 NMR") diff --git a/src/bind_region.c b/src/bind_region.c index a28b94c07..24c2fb08d 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -582,6 +582,13 @@ static int tolua_plane_get(lua_State * L) return 1; } +static int tolua_plane_erase(lua_State *L) +{ + plane *self = (plane *)tolua_tousertype(L, 1, 0); + remove_plane(self); + return 0; +} + static int tolua_plane_create(lua_State * L) { int id = (int)tolua_tonumber(L, 1, 0); @@ -739,6 +746,7 @@ void tolua_region_open(lua_State * L) tolua_beginmodule(L, TOLUA_CAST "plane"); { tolua_function(L, TOLUA_CAST "create", tolua_plane_create); + tolua_function(L, TOLUA_CAST "erase", tolua_plane_erase); tolua_function(L, TOLUA_CAST "get", tolua_plane_get); tolua_function(L, TOLUA_CAST "__tostring", tolua_plane_tostring); diff --git a/src/kernel/config.c b/src/kernel/config.c index 56d995bdc..cda449a23 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1104,8 +1104,7 @@ void free_gamedata(void) while (planes) { plane *pl = planes; planes = planes->next; - free(pl->name); - free(pl); + free_plane(pl); } while (global.attribs) { diff --git a/src/kernel/plane.c b/src/kernel/plane.c index 2542a053d..7e9650d13 100644 --- a/src/kernel/plane.c +++ b/src/kernel/plane.c @@ -290,3 +290,33 @@ int read_plane_reference(plane ** pp, struct storage *store) ur_add(id, pp, resolve_plane); return AT_READ_OK; } + +void free_plane(plane *pl) { + free(pl->name); + free(pl); +} + +void remove_plane(plane *pl) { + region **rp = ®ions; + plane **pp = &planes; + assert(pl); + while (*rp) { + region *r = *rp; + if (r->_plane == pl) { + remove_region(rp, r); + } + else { + rp = &r->next; + } + } + while (*pp) { + if (pl==*pp) { + *pp = pl->next; + free_plane(pl); + break; + } + else { + pp = &(*pp)->next; + } + } +} diff --git a/src/kernel/plane.h b/src/kernel/plane.h index 051c05b69..85c38cf9f 100644 --- a/src/kernel/plane.h +++ b/src/kernel/plane.h @@ -68,13 +68,16 @@ extern "C" { int miny, int maxy, int flags); struct plane *getplanebyname(const char *); struct plane *get_homeplane(void); - extern int rel_to_abs(const struct plane *pl, const struct faction *f, + int rel_to_abs(const struct plane *pl, const struct faction *f, int rel, unsigned char index); - extern void write_plane_reference(const plane * p, struct storage *store); - extern int read_plane_reference(plane ** pp, struct storage *store); - extern int plane_width(const plane * pl); - extern int plane_height(const plane * pl); + void write_plane_reference(const plane * p, struct storage *store); + int read_plane_reference(plane ** pp, struct storage *store); + int plane_width(const plane * pl); + int plane_height(const plane * pl); void adjust_coordinates(const struct faction *f, int *x, int *y, const struct plane *pl); + + void free_plane(struct plane *pl); + void remove_plane(struct plane *pl); #ifdef __cplusplus } #endif diff --git a/src/kernel/region.c b/src/kernel/region.c index c4e2042e4..54e78ec06 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -645,20 +645,39 @@ void rsetmoney(region * r, int value) } } -int rherbs(const struct region *r) +int rherbs(const region *r) { return r->land?r->land->herbs:0; } -void rsetherbs(const struct region *r, int value) +void rsetherbs(region *r, int value) { assert(r->land || value==0); - assert(value >= 0); + assert(value >= 0 && value<=SHRT_MAX); if (r->land) { - r->land->herbs = (short)(value); + r->land->herbs = (short)value; } } +void rsetherbtype(region *r, const struct item_type *itype) { + assert(r->land && r->terrain); + if (itype == NULL) { + r->land->herbtype = NULL; + } + else { + if (r->terrain->herbs) { + int i; + for (i = 0; r->terrain->herbs[i]; ++i) { + if (r->terrain->herbs[i] == itype) { + r->land->herbtype = itype; + return; + } + } + } + log_debug("attempt to set herbtype=%s for terrain=%s in %s", itype->rtype->_name, r->terrain->_name, regionname(r, 0)); + r->land->herbtype = itype; + } +} void r_setdemand(region * r, const luxury_type * ltype, int value) { diff --git a/src/kernel/region.h b/src/kernel/region.h index 70d998a0e..bbc5282b0 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -202,12 +202,12 @@ extern "C" { void rsethorses(const struct region *r, int value); int rherbs(const struct region *r); - void rsetherbs(const struct region *r, int value); + void rsetherbs(struct region *r, int value); + void rsetherbtype(struct region *r, const struct item_type *itype); #define rbuildings(r) ((r)->buildings) #define rherbtype(r) ((r)->land?(r)->land->herbtype:0) -#define rsetherbtype(r, value) if ((r)->land) (r)->land->herbtype=(value) bool r_isforest(const struct region *r); @@ -276,6 +276,8 @@ extern "C" { struct faction *update_owners(struct region *r); + void region_erase(struct region *r); + #ifdef __cplusplus } #endif diff --git a/src/kernel/save.c b/src/kernel/save.c index e5ea1f400..bf0969545 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1066,7 +1066,7 @@ static region *readregion(struct gamedata *data, int x, int y) rsetherbtype(r, NULL); } READ_INT(data->store, &n); - rsetherbs(r, (short)n); + rsetherbs(r, n); READ_INT(data->store, &n); if (n < 0) { /* bug 2182 */