1. region_create(uid)

2. read_region_reference never gets a callback, so save it.
This commit is contained in:
Enno Rehling 2018-11-01 21:08:59 +01:00
parent 85fe80d858
commit ba9af6d765
7 changed files with 25 additions and 21 deletions

View file

@ -36,7 +36,7 @@ write_targetregion(const variant *var, const void *owner, struct storage *store)
static int read_targetregion(variant *var, void *owner, gamedata *data) static int read_targetregion(variant *var, void *owner, gamedata *data)
{ {
if (read_region_reference(data, (region **)&var->v, NULL) <= 0) { if (read_region_reference(data, (region **)&var->v) <= 0) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }
return AT_READ_OK; return AT_READ_OK;

View file

@ -229,7 +229,7 @@ int curse_read(variant *var, void *owner, gamedata *data)
READ_INT(store, &c->data.i); READ_INT(store, &c->data.i);
} }
if (c->type->typ == CURSETYP_REGION) { if (c->type->typ == CURSETYP_REGION) {
int rr = read_region_reference(data, (region **)&c->data.v, NULL); int rr = read_region_reference(data, (region **)&c->data.v);
if (ur == 0 && rr == 0 && !c->data.v) { if (ur == 0 && rr == 0 && !c->data.v) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }

View file

@ -242,7 +242,7 @@ static void unhash_uid(region * r)
uidhash[key].r = NULL; uidhash[key].r = NULL;
} }
static void hash_uid(region * r) static void rhash_uid(region * r)
{ {
int uid = r->uid; int uid = r->uid;
for (;;) { for (;;) {
@ -761,32 +761,35 @@ static region *last;
static unsigned int max_index = 0; static unsigned int max_index = 0;
region *region_create(int uid)
{
region *r = (region *)calloc(1, sizeof(region));
assert_alloc(r);
r->uid = uid;
rhash_uid(r);
return r;
}
region *new_region(int x, int y, struct plane *pl, int uid) region *new_region(int x, int y, struct plane *pl, int uid)
{ {
region *r; region *r;
pnormalize(&x, &y, pl); pnormalize(&x, &y, pl);
r = rfindhash(x, y); r = rfindhash(x, y);
if (!r) {
if (r) { r = region_create(uid);
log_error("duplicate region discovered: %s(%d,%d)\n", regionname(r, NULL), x, y);
if (r->units)
log_error("duplicate region contains units\n");
return r;
} }
r = (region *)calloc(sizeof(region), 1);
assert_alloc(r);
r->x = x; r->x = x;
r->y = y; r->y = y;
r->uid = uid;
r->age = 1; r->age = 1;
r->_plane = pl; r->_plane = pl;
rhash(r); rhash(r);
hash_uid(r); if (last) {
if (last)
addlist(&last, r); addlist(&last, r);
else }
else {
addlist(&regions, r); addlist(&regions, r);
}
last = r; last = r;
assert(r->next == NULL); assert(r->next == NULL);
r->index = ++max_index; r->index = ++max_index;
@ -1263,7 +1266,7 @@ void resolve_region(region *r)
resolve(RESOLVE_REGION | r->uid, r); resolve(RESOLVE_REGION | r->uid, r);
} }
int read_region_reference(gamedata * data, region **rp, resolve_fun fun) int read_region_reference(gamedata * data, region **rp)
{ {
struct storage * store = data->store; struct storage * store = data->store;
int id = 0; int id = 0;
@ -1271,7 +1274,7 @@ int read_region_reference(gamedata * data, region **rp, resolve_fun fun)
READ_INT(store, &id); READ_INT(store, &id);
*rp = findregionbyid(id); *rp = findregionbyid(id);
if (*rp == NULL) { if (*rp == NULL) {
ur_add(RESOLVE_REGION | id, (void **)rp, fun); ur_add(RESOLVE_REGION | id, (void **)rp, NULL);
} }
return id; return id;
} }

View file

@ -225,6 +225,7 @@ extern "C" {
const char *write_regionname(const struct region *r, const struct faction *f, const char *write_regionname(const struct region *r, const struct faction *f,
char *buffer, size_t size); char *buffer, size_t size);
struct region *region_create(int uid);
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);
@ -252,7 +253,7 @@ extern "C" {
#define RESOLVE_REGION (TYP_REGION << 24) #define RESOLVE_REGION (TYP_REGION << 24)
void resolve_region(region *r); void resolve_region(region *r);
void write_region_reference(const struct region *r, struct storage *store); void write_region_reference(const struct region *r, struct storage *store);
int read_region_reference(struct gamedata *data, region **rp, resolve_fun fun); int read_region_reference(struct gamedata *data, region **rp);
const char *regionname(const struct region *r, const struct faction *f); const char *regionname(const struct region *r, const struct faction *f);

View file

@ -99,7 +99,7 @@ static int createunit_read(trigger * t, gamedata *data)
result = AT_READ_FAIL; result = AT_READ_FAIL;
} }
read_region_reference(data, &td->r, NULL); read_region_reference(data, &td->r);
td->race = read_race_reference(data->store); td->race = read_race_reference(data->store);
if (!td->race) { if (!td->race) {
result = AT_READ_FAIL; result = AT_READ_FAIL;

View file

@ -76,7 +76,7 @@ static int gate_read(trigger * t, gamedata *data)
{ {
gate_data *gd = (gate_data *)t->data.v; gate_data *gd = (gate_data *)t->data.v;
int bc = read_building_reference(data, &gd->gate, NULL); int bc = read_building_reference(data, &gd->gate, NULL);
int rc = read_region_reference(data, &gd->target, NULL); int rc = read_region_reference(data, &gd->target);
if (bc <= 0 && rc <= 0) { if (bc <= 0 && rc <= 0) {
return AT_READ_FAIL; return AT_READ_FAIL;

View file

@ -103,7 +103,7 @@ static int wormhole_read(variant *var, void *owner, struct gamedata *data)
if (data->version < ATTRIBOWNER_VERSION) { if (data->version < ATTRIBOWNER_VERSION) {
READ_INT(data->store, NULL); READ_INT(data->store, NULL);
} }
id = read_region_reference(data, (region **)&var->v, NULL); id = read_region_reference(data, (region **)&var->v);
return (id <= 0) ? AT_READ_FAIL : AT_READ_OK; return (id <= 0) ? AT_READ_FAIL : AT_READ_OK;
} }