Bug 2509 contd: new_region still creating duplicates

This commit is contained in:
Enno Rehling 2018-11-03 15:48:35 +01:00
parent 6c11e740ff
commit 8bcdb5c381
5 changed files with 28 additions and 32 deletions

View File

@ -448,7 +448,7 @@ static int tolua_region_create(lua_State * L)
return 0; return 0;
} }
assert(!pnormalize(&x, &y, pl)); pnormalize(&x, &y, pl);
r = result = findregion(x, y); r = result = findregion(x, y);
if (r != NULL && r->units != NULL) { if (r != NULL && r->units != NULL) {

View File

@ -481,7 +481,7 @@ static int tolua_get_region(lua_State * L)
int x = (int)tolua_tonumber(L, 1, 0); int x = (int)tolua_tonumber(L, 1, 0);
int y = (int)tolua_tonumber(L, 2, 0); int y = (int)tolua_tonumber(L, 2, 0);
region *r; region *r;
assert(!pnormalize(&x, &y, findplane(x, y))); pnormalize(&x, &y, findplane(x, y));
r = findregion(x, y); r = findregion(x, y);
tolua_pushusertype(L, r, TOLUA_CAST "region"); tolua_pushusertype(L, r, TOLUA_CAST "region");
return 1; return 1;

View File

@ -268,7 +268,7 @@ static int hash_requests;
static int hash_misses; static int hash_misses;
#endif #endif
bool pnormalize(int *x, int *y, const plane * pl) void pnormalize(int *x, int *y, const plane * pl)
{ {
if (pl) { if (pl) {
if (x) { if (x) {
@ -284,7 +284,6 @@ bool pnormalize(int *x, int *y, const plane * pl)
*y = ny % height + pl->miny; *y = ny % height + pl->miny;
} }
} }
return false; /* TBD */
} }
static region *rfindhash(int x, int y) static region *rfindhash(int x, int y)
@ -343,8 +342,9 @@ region *r_connect(const region * r, direction_t dir)
int x, y; int x, y;
region *rmodify = (region *)r; region *rmodify = (region *)r;
assert(dir >= 0 && dir < MAXDIRECTIONS); assert(dir >= 0 && dir < MAXDIRECTIONS);
if (r->connect[dir]) if (r->connect[dir]) {
return r->connect[dir]; return r->connect[dir];
}
assert(dir < MAXDIRECTIONS); assert(dir < MAXDIRECTIONS);
x = r->x + delta_x[dir]; x = r->x + delta_x[dir];
y = r->y + delta_y[dir]; y = r->y + delta_y[dir];
@ -757,10 +757,6 @@ int rsettrees(const region * r, int ageclass, int value)
return 0; return 0;
} }
static region *last;
static unsigned int max_index = 0;
region *region_create(int uid) region *region_create(int uid)
{ {
region *r = (region *)calloc(1, sizeof(region)); region *r = (region *)calloc(1, sizeof(region));
@ -770,16 +766,13 @@ region *region_create(int uid)
return r; return r;
} }
region *new_region(int x, int y, struct plane *pl, int uid) static region *last;
{ static unsigned int max_index;
region *r;
pnormalize(&x, &y, pl); void add_region(region *r, int x, int y) {
r = region_create(uid);
r->x = x; r->x = x;
r->y = y; r->y = y;
r->age = 1; r->_plane = findplane(x, y);
r->_plane = pl;
rhash(r); rhash(r);
if (last) { if (last) {
addlist(&last, r); addlist(&last, r);
@ -790,6 +783,15 @@ region *new_region(int x, int y, struct plane *pl, int uid)
last = r; last = r;
assert(r->next == NULL); assert(r->next == NULL);
r->index = ++max_index; 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; return r;
} }

View File

@ -226,11 +226,12 @@ extern "C" {
char *buffer, size_t size); char *buffer, size_t size);
struct region *region_create(int uid); 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); struct region *new_region(int x, int y, struct plane *pl, int uid);
void remove_region(region ** rlist, region * r); void remove_region(region ** rlist, region * r);
void terraform_region(struct region *r, const struct terrain_type *terrain); void terraform_region(struct region *r, const struct terrain_type *terrain);
void init_region(struct region *r); 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_x[MAXDIRECTIONS];
extern const int delta_y[MAXDIRECTIONS]; extern const int delta_y[MAXDIRECTIONS];

View File

@ -624,24 +624,17 @@ static region *readregion(gamedata *data, int x, int y)
READ_INT(data->store, &uid); READ_INT(data->store, &uid);
r = findregionbyid(uid); r = findregionbyid(uid);
if (r == NULL) { if (r == NULL) {
plane *pl = findplane(x, y); r = region_create(uid);
r = new_region(x, y, pl, uid);
} }
else { else {
assert(uid == 0 || r->uid == uid); /* make sure this was not read earlier */
while (r->attribs) assert(r->next == NULL);
a_remove(&r->attribs, r->attribs); assert(r->attribs == NULL);
if (r->land) { assert(r->land == NULL);
free_land(r->land); assert(r->resources == NULL);
r->land = 0;
}
while (r->resources) {
rawmaterial *rm = r->resources;
r->resources = rm->next;
free(rm);
}
r->land = 0;
} }
/* add region to the global list: */
add_region(r, x, y);
if (data->version < LANDDISPLAY_VERSION) { if (data->version < LANDDISPLAY_VERSION) {
read_regioninfo(data, r, info, sizeof(info)); read_regioninfo(data, r, info, sizeof(info));
} }