Merge branch 'develop' of https://github.com/ennorehling/eressea into develop

This commit is contained in:
Enno Rehling 2018-11-02 22:31:48 +01:00
commit c981bfb766
17 changed files with 63 additions and 64 deletions

View file

@ -98,7 +98,7 @@ static int obs_read(variant *var, void *owner, struct gamedata *data)
obs_data *od = (obs_data *)var->v; obs_data *od = (obs_data *)var->v;
UNUSED_ARG(owner); UNUSED_ARG(owner);
read_faction_reference(data, &od->f, NULL); read_faction_reference(data, &od->f);
READ_INT(data->store, &od->skill); READ_INT(data->store, &od->skill);
READ_INT(data->store, &od->timer); READ_INT(data->store, &od->timer);
return AT_READ_OK; return AT_READ_OK;

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

@ -359,15 +359,14 @@ void resolve_building(building *b)
resolve(RESOLVE_BUILDING | b->no, b); resolve(RESOLVE_BUILDING | b->no, b);
} }
int read_building_reference(gamedata * data, building **bp, resolve_fun fun) int read_building_reference(gamedata * data, building **bp)
{ {
int id; int id;
READ_INT(data->store, &id); READ_INT(data->store, &id);
if (id > 0) { if (id > 0) {
*bp = findbuilding(id); *bp = findbuilding(id);
if (*bp == NULL) { if (*bp == NULL) {
*bp = NULL; *bp = building_create(id);
ur_add(RESOLVE_BUILDING | id, (void**)bp, fun);
} }
} }
else { else {
@ -376,17 +375,23 @@ int read_building_reference(gamedata * data, building **bp, resolve_fun fun)
return id; return id;
} }
building *building_create(int id)
{
building *b = (building *)calloc(1, sizeof(building));
b->no = id;
bhash(b);
return b;
}
building *new_building(const struct building_type * btype, region * r, building *new_building(const struct building_type * btype, region * r,
const struct locale * lang) const struct locale * lang)
{ {
building **bptr = &r->buildings; building **bptr = &r->buildings;
building *b = (building *)calloc(1, sizeof(building)); int id = newcontainerid();
building *b = building_create(id);
const char *bname; const char *bname;
char buffer[32]; char buffer[32];
b->no = newcontainerid();
bhash(b);
b->type = btype; b->type = btype;
b->region = r; b->region = r;
while (*bptr) while (*bptr)

View file

@ -127,6 +127,7 @@ extern "C" {
const char *write_buildingname(const building * b, char *ibuf, const char *write_buildingname(const building * b, char *ibuf,
size_t size); size_t size);
int buildingcapacity(const struct building *b); int buildingcapacity(const struct building *b);
struct building *building_create(int id);
struct building *new_building(const struct building_type *typ, struct building *new_building(const struct building_type *typ,
struct region *r, const struct locale *lang); struct region *r, const struct locale *lang);
int build_building(struct unit *u, const struct building_type *typ, int build_building(struct unit *u, const struct building_type *typ,
@ -166,7 +167,7 @@ extern "C" {
void resolve_building(building *b); void resolve_building(building *b);
void write_building_reference(const struct building *b, void write_building_reference(const struct building *b,
struct storage *store); struct storage *store);
int read_building_reference(struct gamedata * data, struct building **bp, resolve_fun fun); int read_building_reference(struct gamedata * data, struct building **bp);
struct building *findbuilding(int n); struct building *findbuilding(int n);

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

@ -54,7 +54,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/parser.h> #include <util/parser.h>
#include <util/password.h> #include <util/password.h>
#include <util/path.h> #include <util/path.h>
#include <util/resolve.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/strings.h> #include <util/strings.h>
#include <util/variant.h> #include <util/variant.h>
@ -331,20 +330,14 @@ bool checkpasswd(const faction * f, const char *passwd)
return true; return true;
} }
void resolve_faction(faction *f) int read_faction_reference(gamedata * data, faction **fp)
{
resolve(RESOLVE_FACTION | f->no, f);
}
int read_faction_reference(gamedata * data, faction **fp, resolve_fun fun)
{ {
int id; int id;
READ_INT(data->store, &id); READ_INT(data->store, &id);
if (id > 0) { if (id > 0) {
*fp = findfaction(id); *fp = findfaction(id);
if (*fp == NULL) { if (*fp == NULL) {
*fp = NULL; *fp = faction_create(id);
ur_add(RESOLVE_FACTION | id, (void **)fp, fun);
} }
} }
else { else {

View file

@ -133,10 +133,7 @@ extern "C" {
void write_faction_reference(const struct faction *f, void write_faction_reference(const struct faction *f,
struct storage *store); struct storage *store);
int read_faction_reference(struct gamedata *data, struct faction **fp, resolve_fun fun); int read_faction_reference(struct gamedata *data, struct faction **fp);
#define RESOLVE_FACTION (TYP_FACTION << 24)
void resolve_faction(struct faction *f);
void renumber_faction(faction * f, int no); void renumber_faction(faction * f, int no);
void free_factions(void); void free_factions(void);

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); *rp = region_create(id);
} }
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

@ -123,7 +123,7 @@ static void read_alliances(gamedata *data)
READ_INT(store, &al->flags); READ_INT(store, &al->flags);
} }
if (data->version >= ALLIANCELEADER_VERSION) { if (data->version >= ALLIANCELEADER_VERSION) {
read_faction_reference(data, &al->_leader, NULL); read_faction_reference(data, &al->_leader);
READ_INT(store, &id); READ_INT(store, &id);
} }
else { else {
@ -248,7 +248,7 @@ static void read_owner(gamedata *data, region_owner ** powner)
owner->flags = 0; owner->flags = 0;
} }
if (data->version >= OWNER_3_VERSION) { if (data->version >= OWNER_3_VERSION) {
read_faction_reference(data, &owner->last_owner, NULL); read_faction_reference(data, &owner->last_owner);
} }
else if (data->version >= OWNER_2_VERSION) { else if (data->version >= OWNER_2_VERSION) {
int id; int id;
@ -261,7 +261,7 @@ static void read_owner(gamedata *data, region_owner ** powner)
else { else {
owner->last_owner = NULL; owner->last_owner = NULL;
} }
read_faction_reference(data, &owner->owner, NULL); read_faction_reference(data, &owner->owner);
*powner = owner; *powner = owner;
} }
else { else {
@ -420,10 +420,7 @@ unit *read_unit(gamedata *data)
u_setfaction(u, NULL); u_setfaction(u, NULL);
} }
else { else {
u = (unit *)calloc(1, sizeof(unit)); u = unit_create(n);
assert_alloc(u);
u->no = n;
uhash(u);
} }
READ_INT(data->store, &n); READ_INT(data->store, &n);
@ -1087,7 +1084,6 @@ faction *read_faction(gamedata * data)
if (data->version >= REGIONOWNER_VERSION) { if (data->version >= REGIONOWNER_VERSION) {
read_spellbook(FactionSpells() ? &f->spellbook : 0, data, get_spell_level_faction, (void *)f); read_spellbook(FactionSpells() ? &f->spellbook : 0, data, get_spell_level_faction, (void *)f);
} }
resolve_faction(f);
return f; return f;
} }

View file

@ -1375,6 +1375,12 @@ void name_unit(unit * u)
} }
} }
unit *unit_create(int id)
{
unit *u = (unit *)calloc(1, sizeof(unit));
createunitid(u, id);
return u;
}
/** creates a new unit. /** creates a new unit.
* *
* @param dname: name, set to NULL to get a default. * @param dname: name, set to NULL to get a default.
@ -1383,7 +1389,7 @@ void name_unit(unit * u)
unit *create_unit(region * r, faction * f, int number, const struct race *urace, unit *create_unit(region * r, faction * f, int number, const struct race *urace,
int id, const char *dname, unit * creator) int id, const char *dname, unit * creator)
{ {
unit *u = (unit *)calloc(1, sizeof(unit)); unit *u = unit_create(id);
assert(urace); assert(urace);
u_setrace(u, urace); u_setrace(u, urace);
@ -1395,10 +1401,6 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
set_number(u, number); set_number(u, number);
/* die nummer der neuen einheit muss vor name_unit generiert werden,
* da der default name immer noch 'Nummer u->no' ist */
createunitid(u, id);
/* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen /* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen
* enthalten */ * enthalten */
if (r) if (r)

View file

@ -206,8 +206,9 @@ extern "C" {
int invisible(const struct unit *target, const struct unit *viewer); int invisible(const struct unit *target, const struct unit *viewer);
void free_unit(struct unit *u); void free_unit(struct unit *u);
extern void name_unit(struct unit *u); void name_unit(struct unit *u);
extern struct unit *create_unit(struct region *r1, struct faction *f, struct unit *unit_create(int id);
struct unit *create_unit(struct region *r1, struct faction *f,
int number, const struct race *rc, int id, const char *dname, int number, const struct race *rc, int id, const char *dname,
struct unit *creator); struct unit *creator);

View file

@ -50,7 +50,7 @@ static void xmasgate_write(const trigger * t, struct storage *store)
static int xmasgate_read(trigger * t, struct gamedata *data) static int xmasgate_read(trigger * t, struct gamedata *data)
{ {
if (read_building_reference(data, (building **)&t->data.v, NULL) <= 0) { if (read_building_reference(data, (building **)&t->data.v) <= 0) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }
return AT_READ_OK; return AT_READ_OK;

View file

@ -87,7 +87,7 @@ static int changefaction_read(trigger * t, gamedata *data)
changefaction_data *td = (changefaction_data *)t->data.v; changefaction_data *td = (changefaction_data *)t->data.v;
read_unit_reference(data, &td->unit, NULL); read_unit_reference(data, &td->unit, NULL);
return read_faction_reference(data, &td->faction, NULL) > 0 ? AT_READ_OK : AT_READ_FAIL; return read_faction_reference(data, &td->faction) > 0 ? AT_READ_OK : AT_READ_FAIL;
} }
trigger_type tt_changefaction = { trigger_type tt_changefaction = {

View file

@ -94,12 +94,12 @@ static int createunit_read(trigger * t, gamedata *data)
int id; int id;
int result = AT_READ_OK; int result = AT_READ_OK;
id = read_faction_reference(data, &td->f, NULL); id = read_faction_reference(data, &td->f);
if (id <= 0) { if (id <= 0) {
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

@ -75,8 +75,8 @@ static void gate_write(const trigger * t, struct storage *store)
static int gate_read(trigger * t, gamedata *data) 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);
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;
} }