From 8bcdb5c38136ff36b970ae83dd67c8008de804d0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 3 Nov 2018 15:48:35 +0100 Subject: [PATCH] Bug 2509 contd: new_region still creating duplicates --- src/bind_region.c | 2 +- src/bindings.c | 2 +- src/kernel/region.c | 30 ++++++++++++++++-------------- src/kernel/region.h | 3 ++- src/kernel/save.c | 23 ++++++++--------------- 5 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/bind_region.c b/src/bind_region.c index 9d7897829..86c072a84 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -448,7 +448,7 @@ static int tolua_region_create(lua_State * L) return 0; } - assert(!pnormalize(&x, &y, pl)); + pnormalize(&x, &y, pl); r = result = findregion(x, y); if (r != NULL && r->units != NULL) { diff --git a/src/bindings.c b/src/bindings.c index caac5b99c..e60b7fe78 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -481,7 +481,7 @@ static int tolua_get_region(lua_State * L) int x = (int)tolua_tonumber(L, 1, 0); int y = (int)tolua_tonumber(L, 2, 0); region *r; - assert(!pnormalize(&x, &y, findplane(x, y))); + pnormalize(&x, &y, findplane(x, y)); r = findregion(x, y); tolua_pushusertype(L, r, TOLUA_CAST "region"); return 1; diff --git a/src/kernel/region.c b/src/kernel/region.c index bcae52e35..60baed49d 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -268,7 +268,7 @@ static int hash_requests; static int hash_misses; #endif -bool pnormalize(int *x, int *y, const plane * pl) +void pnormalize(int *x, int *y, const plane * pl) { if (pl) { if (x) { @@ -284,7 +284,6 @@ bool pnormalize(int *x, int *y, const plane * pl) *y = ny % height + pl->miny; } } - return false; /* TBD */ } static region *rfindhash(int x, int y) @@ -343,8 +342,9 @@ region *r_connect(const region * r, direction_t dir) int x, y; region *rmodify = (region *)r; assert(dir >= 0 && dir < MAXDIRECTIONS); - if (r->connect[dir]) + if (r->connect[dir]) { return r->connect[dir]; + } assert(dir < MAXDIRECTIONS); x = r->x + delta_x[dir]; y = r->y + delta_y[dir]; @@ -757,10 +757,6 @@ int rsettrees(const region * r, int ageclass, int value) return 0; } -static region *last; - -static unsigned int max_index = 0; - region *region_create(int uid) { region *r = (region *)calloc(1, sizeof(region)); @@ -770,16 +766,13 @@ region *region_create(int uid) return r; } -region *new_region(int x, int y, struct plane *pl, int uid) -{ - region *r; +static region *last; +static unsigned int max_index; - pnormalize(&x, &y, pl); - r = region_create(uid); +void add_region(region *r, int x, int y) { r->x = x; r->y = y; - r->age = 1; - r->_plane = pl; + r->_plane = findplane(x, y); rhash(r); if (last) { addlist(&last, r); @@ -790,6 +783,15 @@ region *new_region(int x, int y, struct plane *pl, int uid) last = r; assert(r->next == NULL); r->index = ++max_index; +} + +region *new_region(int x, int y, struct plane *pl, int uid) +{ + region *r; + r = region_create(uid); + r->age = 1; + add_region(r, x, y); + assert(pl == r->_plane); return r; } diff --git a/src/kernel/region.h b/src/kernel/region.h index 436434024..e4982406e 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -226,11 +226,12 @@ extern "C" { char *buffer, size_t size); struct region *region_create(int uid); + void add_region(region *r, int x, int y); struct region *new_region(int x, int y, struct plane *pl, int uid); void remove_region(region ** rlist, region * r); void terraform_region(struct region *r, const struct terrain_type *terrain); void init_region(struct region *r); - bool pnormalize(int *x, int *y, const struct plane *pl); + void pnormalize(int *x, int *y, const struct plane *pl); extern const int delta_x[MAXDIRECTIONS]; extern const int delta_y[MAXDIRECTIONS]; diff --git a/src/kernel/save.c b/src/kernel/save.c index c2879bf0e..1d67c2860 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -624,24 +624,17 @@ static region *readregion(gamedata *data, int x, int y) READ_INT(data->store, &uid); r = findregionbyid(uid); if (r == NULL) { - plane *pl = findplane(x, y); - r = new_region(x, y, pl, uid); + r = region_create(uid); } else { - assert(uid == 0 || r->uid == uid); - while (r->attribs) - a_remove(&r->attribs, r->attribs); - if (r->land) { - free_land(r->land); - r->land = 0; - } - while (r->resources) { - rawmaterial *rm = r->resources; - r->resources = rm->next; - free(rm); - } - r->land = 0; + /* make sure this was not read earlier */ + assert(r->next == NULL); + assert(r->attribs == NULL); + assert(r->land == NULL); + assert(r->resources == NULL); } + /* add region to the global list: */ + add_region(r, x, y); if (data->version < LANDDISPLAY_VERSION) { read_regioninfo(data, r, info, sizeof(info)); }