trigger_type.read gets a gamedata object

github issue #479
This commit is contained in:
Enno Rehling 2016-02-13 15:25:07 +01:00
parent 73a6b96bc7
commit 2d30d4cce6
18 changed files with 75 additions and 59 deletions

View File

@ -101,7 +101,7 @@ static int tolua_storage_tostring(lua_State * L)
{ {
gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0); gamedata *data = (gamedata *)tolua_tousertype(L, 1, 0);
char name[64]; char name[64];
_snprintf(name, sizeof(name), "<gamedata %p ver=%d>", data, data->version); _snprintf(name, sizeof(name), "<gamedata %p ver=%d>", (void *)data, data->version);
lua_pushstring(L, name); lua_pushstring(L, name);
return 1; return 1;
} }

View File

@ -449,10 +449,10 @@ static void caldera_write(const trigger * t, struct storage *store)
write_building_reference(b, store); write_building_reference(b, store);
} }
static int caldera_read(trigger * t, struct storage *store) static int caldera_read(trigger * t, struct gamedata *data)
{ {
int rb = int rb =
read_reference(&t->data.v, store, read_building_reference, read_reference(&t->data.v, data->store, read_building_reference,
resolve_building); resolve_building);
if (rb == 0 && !t->data.v) { if (rb == 0 && !t->data.v) {
return AT_READ_FAIL; return AT_READ_FAIL;

View File

@ -28,6 +28,7 @@
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -47,10 +48,10 @@ static void xmasgate_write(const trigger * t, struct storage *store)
WRITE_TOK(store, itoa36(b->no)); WRITE_TOK(store, itoa36(b->no));
} }
static int xmasgate_read(trigger * t, struct storage *store) static int xmasgate_read(trigger * t, struct gamedata *data)
{ {
int bc = int bc =
read_reference(&t->data.v, store, read_building_reference, read_reference(&t->data.v, data->store, read_building_reference,
resolve_building); resolve_building);
if (bc == 0 && !t->data.v) { if (bc == 0 && !t->data.v) {
return AT_READ_FAIL; return AT_READ_FAIL;

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/base36.h> #include <util/base36.h>
@ -82,12 +83,13 @@ static void changefaction_write(const trigger * t, struct storage *store)
write_faction_reference(td->faction->_alive ? td->faction : NULL, store); write_faction_reference(td->faction->_alive ? td->faction : NULL, store);
} }
static int changefaction_read(trigger * t, struct storage *store) static int changefaction_read(trigger * t, gamedata *data)
{ {
variant var; variant var;
changefaction_data *td = (changefaction_data *)t->data.v; changefaction_data *td = (changefaction_data *)t->data.v;
read_reference(&td->unit, store, read_unit_reference, resolve_unit);
var = read_faction_reference(store); read_reference(&td->unit, data->store, read_unit_reference, resolve_unit);
var = read_faction_reference(data->store);
if (var.i == 0) { if (var.i == 0) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }

View File

@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/base36.h> #include <util/base36.h>
@ -86,12 +87,12 @@ static void changerace_write(const trigger * t, struct storage *store)
write_race_reference(td->irace, store); write_race_reference(td->irace, store);
} }
static int changerace_read(trigger * t, struct storage *store) static int changerace_read(trigger * t, gamedata *data)
{ {
changerace_data *td = (changerace_data *)t->data.v; changerace_data *td = (changerace_data *)t->data.v;
read_reference(&td->u, store, read_unit_reference, resolve_unit); read_reference(&td->u, data->store, read_unit_reference, resolve_unit);
td->race = (const struct race *)read_race_reference(store).v; td->race = (const struct race *)read_race_reference(data->store).v;
td->irace = (const struct race *)read_race_reference(store).v; td->irace = (const struct race *)read_race_reference(data->store).v;
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -29,6 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/base36.h> #include <util/base36.h>
@ -68,10 +69,10 @@ static void clonedied_write(const trigger * t, struct storage *store)
write_unit_reference(u, store); write_unit_reference(u, store);
} }
static int clonedied_read(trigger * t, struct storage *store) static int clonedied_read(trigger * t, gamedata *data)
{ {
int result = int result =
read_reference(&t->data.v, store, read_unit_reference, resolve_unit); read_reference(&t->data.v, data->store, read_unit_reference, resolve_unit);
if (result == 0 && t->data.v == NULL) { if (result == 0 && t->data.v == NULL) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/base36.h> #include <util/base36.h>
@ -93,30 +94,30 @@ static void createcurse_write(const trigger * t, struct storage *store)
WRITE_INT(store, td->men); WRITE_INT(store, td->men);
} }
static int createcurse_read(trigger * t, struct storage *store) static int createcurse_read(trigger * t, gamedata *data)
{ {
createcurse_data *td = (createcurse_data *)t->data.v; createcurse_data *td = (createcurse_data *)t->data.v;
char zText[128]; char zText[128];
float flt; float flt;
read_reference(&td->mage, store, read_unit_reference, resolve_unit); read_reference(&td->mage, data->store, read_unit_reference, resolve_unit);
read_reference(&td->target, store, read_unit_reference, resolve_unit); read_reference(&td->target, data->store, read_unit_reference, resolve_unit);
READ_TOK(store, zText, sizeof(zText)); READ_TOK(data->store, zText, sizeof(zText));
td->type = ct_find(zText); td->type = ct_find(zText);
READ_FLT(store, &flt); READ_FLT(data->store, &flt);
td->vigour = flt; td->vigour = flt;
READ_INT(store, &td->duration); READ_INT(data->store, &td->duration);
if (global.data_version < CURSEFLOAT_VERSION) { if (data->version < CURSEFLOAT_VERSION) {
int n; int n;
READ_INT(store, &n); READ_INT(data->store, &n);
td->effect = (float)n; td->effect = (float)n;
} }
else { else {
READ_FLT(store, &flt); READ_FLT(data->store, &flt);
td->effect = flt; td->effect = flt;
} }
READ_INT(store, &td->men); READ_INT(data->store, &td->men);
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -31,6 +31,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -88,12 +89,12 @@ static void createunit_write(const trigger * t, struct storage *store)
WRITE_INT(store, td->number); WRITE_INT(store, td->number);
} }
static int createunit_read(trigger * t, struct storage *store) static int createunit_read(trigger * t, gamedata *data)
{ {
createunit_data *td = (createunit_data *)t->data.v; createunit_data *td = (createunit_data *)t->data.v;
variant var; variant var;
int result = AT_READ_OK; int result = AT_READ_OK;
var = read_faction_reference(store); var = read_faction_reference(data->store);
if (var.i > 0) { if (var.i > 0) {
td->f = findfaction(var.i); td->f = findfaction(var.i);
if (!td->f) { if (!td->f) {
@ -105,13 +106,13 @@ static int createunit_read(trigger * t, struct storage *store)
} }
// read_reference(&td->f, store, read_faction_reference, resolve_faction); // read_reference(&td->f, store, read_faction_reference, resolve_faction);
read_reference(&td->r, store, read_region_reference, read_reference(&td->r, data->store, read_region_reference,
RESOLVE_REGION(global.data_version)); RESOLVE_REGION(global.data_version));
td->race = (const struct race *)read_race_reference(store).v; td->race = (const struct race *)read_race_reference(data->store).v;
if (!td->race) { if (!td->race) {
result = AT_READ_FAIL; result = AT_READ_FAIL;
} }
READ_INT(store, &td->number); READ_INT(data->store, &td->number);
return result; return result;
} }

View File

@ -22,6 +22,7 @@
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -72,14 +73,14 @@ static void gate_write(const trigger * t, struct storage *store)
write_region_reference(r, store); write_region_reference(r, store);
} }
static int gate_read(trigger * t, struct storage *store) 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 = int bc =
read_reference(&gd->gate, store, read_building_reference, resolve_building); read_reference(&gd->gate, data->store, read_building_reference, resolve_building);
int rc = int rc =
read_reference(&gd->target, store, read_region_reference, read_reference(&gd->target, data->store, read_region_reference,
RESOLVE_REGION(global.data_version)); RESOLVE_REGION(global.data_version));
if (bc == 0 && rc == 0) { if (bc == 0 && rc == 0) {

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -82,15 +83,15 @@ static void giveitem_write(const trigger * t, struct storage *store)
WRITE_TOK(store, td->itype->rtype->_name); WRITE_TOK(store, td->itype->rtype->_name);
} }
static int giveitem_read(trigger * t, struct storage *store) static int giveitem_read(trigger * t, gamedata *data)
{ {
giveitem_data *td = (giveitem_data *)t->data.v; giveitem_data *td = (giveitem_data *)t->data.v;
char zText[128]; char zText[128];
int result = read_reference(&td->u, store, read_unit_reference, resolve_unit); int result = read_reference(&td->u, data->store, read_unit_reference, resolve_unit);
READ_INT(store, &td->number); READ_INT(data->store, &td->number);
READ_TOK(store, zText, sizeof(zText)); READ_TOK(data->store, zText, sizeof(zText));
td->itype = it_find(zText); td->itype = it_find(zText);
assert(td->itype); assert(td->itype);

View File

@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -58,10 +59,10 @@ static void killunit_write(const trigger * t, struct storage *store)
write_unit_reference(u, store); write_unit_reference(u, store);
} }
static int killunit_read(trigger * t, struct storage *store) static int killunit_read(trigger * t, gamedata *data)
{ {
int result = int result =
read_reference(&t->data.v, store, read_unit_reference, resolve_unit); read_reference(&t->data.v, data->store, read_unit_reference, resolve_unit);
if (result == 0 && t->data.v == NULL) { if (result == 0 && t->data.v == NULL) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -77,12 +78,12 @@ static void removecurse_write(const trigger * t, struct storage *store)
WRITE_INT(store, td->curse ? td->curse->no : 0); WRITE_INT(store, td->curse ? td->curse->no : 0);
} }
static int removecurse_read(trigger * t, struct storage *store) static int removecurse_read(trigger * t, gamedata *data)
{ {
removecurse_data *td = (removecurse_data *)t->data.v; removecurse_data *td = (removecurse_data *)t->data.v;
read_reference(&td->target, store, read_unit_reference, resolve_unit); read_reference(&td->target, data->store, read_unit_reference, resolve_unit);
read_reference(&td->curse, store, read_int, resolve_curse); read_reference(&td->curse, data->store, read_int, resolve_curse);
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -33,6 +33,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/rng.h> #include <util/rng.h>
@ -125,10 +126,10 @@ static void shock_write(const trigger * t, struct storage *store)
} }
} }
static int shock_read(trigger * t, struct storage *store) static int shock_read(trigger * t, gamedata *data)
{ {
int result = int result =
read_reference(&t->data.v, store, read_unit_reference, resolve_unit); read_reference(&t->data.v, data->store, read_unit_reference, resolve_unit);
if (result == 0 && t->data.v == NULL) { if (result == 0 && t->data.v == NULL) {
return AT_READ_FAIL; return AT_READ_FAIL;
} }

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <storage.h> #include <storage.h>
@ -72,11 +73,11 @@ static void timeout_write(const trigger * t, struct storage *store)
write_triggers(store, td->triggers); write_triggers(store, td->triggers);
} }
static int timeout_read(trigger * t, struct storage *store) static int timeout_read(trigger * t, gamedata *data)
{ {
timeout_data *td = (timeout_data *)t->data.v; timeout_data *td = (timeout_data *)t->data.v;
READ_INT(store, &td->timer); READ_INT(data->store, &td->timer);
read_triggers(store, &td->triggers); read_triggers(data, &td->triggers);
if (td->timer > 20) { if (td->timer > 20) {
trigger *tr = td->triggers; trigger *tr = td->triggers;
log_warning("there is a timeout lasting for another %d turns\n", td->timer); log_warning("there is a timeout lasting for another %d turns\n", td->timer);

View File

@ -22,6 +22,7 @@
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/log.h> #include <util/log.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -48,10 +49,10 @@ static void unguard_write(const trigger * t, struct storage *store)
write_building_reference((building *)t->data.v, store); write_building_reference((building *)t->data.v, store);
} }
static int unguard_read(trigger * t, struct storage *store) static int unguard_read(trigger * t, gamedata *data)
{ {
int rb = int rb =
read_reference(&t->data.v, store, read_building_reference, read_reference(&t->data.v, data->store, read_building_reference,
resolve_building); resolve_building);
if (rb == 0 && !t->data.v) { if (rb == 0 && !t->data.v) {
return AT_READ_FAIL; return AT_READ_FAIL;

View File

@ -20,6 +20,7 @@ without prior permission by the authors of Eressea.
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/event.h> #include <util/event.h>
#include <util/gamedata.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/language.h> #include <util/language.h>
#include <util/log.h> #include <util/log.h>
@ -84,17 +85,17 @@ static void unitmessage_write(const trigger * t, struct storage *store)
WRITE_INT(store, td->level); WRITE_INT(store, td->level);
} }
static int unitmessage_read(trigger * t, struct storage *store) static int unitmessage_read(trigger * t, gamedata *data)
{ {
unitmessage_data *td = (unitmessage_data *)t->data.v; unitmessage_data *td = (unitmessage_data *)t->data.v;
char zText[256]; char zText[256];
int result = int result =
read_reference(&td->target, store, read_unit_reference, resolve_unit); read_reference(&td->target, data->store, read_unit_reference, resolve_unit);
READ_TOK(store, zText, sizeof(zText)); READ_TOK(data->store, zText, sizeof(zText));
td->string = _strdup(zText); td->string = _strdup(zText);
READ_INT(store, &td->type); READ_INT(data->store, &td->type);
READ_INT(store, &td->level); READ_INT(data->store, &td->level);
if (result == 0 && td->target == NULL) { if (result == 0 && td->target == NULL) {
return AT_READ_FAIL; return AT_READ_FAIL;

View File

@ -43,20 +43,20 @@ void write_triggers(struct storage *store, const trigger * t)
WRITE_TOK(store, "end"); WRITE_TOK(store, "end");
} }
int read_triggers(struct storage *store, trigger ** tp) int read_triggers(struct gamedata *data, trigger ** tp)
{ {
for (;;) { for (;;) {
trigger_type *ttype; trigger_type *ttype;
char zText[128]; char zText[128];
READ_TOK(store, zText, sizeof(zText)); READ_TOK(data->store, zText, sizeof(zText));
if (!strcmp(zText, "end")) if (!strcmp(zText, "end"))
break; break;
ttype = tt_find(zText); ttype = tt_find(zText);
assert(ttype || !"unknown trigger-type"); assert(ttype || !"unknown trigger-type");
*tp = t_new(ttype); *tp = t_new(ttype);
if (ttype->read) { if (ttype->read) {
int i = ttype->read(*tp, store); int i = ttype->read(*tp, data);
switch (i) { switch (i) {
case AT_READ_OK: case AT_READ_OK:
tp = &(*tp)->next; tp = &(*tp)->next;
@ -152,7 +152,7 @@ static int read_handler(attrib * a, void *owner, gamedata *data)
READ_TOK(store, zText, sizeof(zText)); READ_TOK(store, zText, sizeof(zText));
hi->event = _strdup(zText); hi->event = _strdup(zText);
read_triggers(store, &hi->triggers); read_triggers(data, &hi->triggers);
if (hi->triggers != NULL) { if (hi->triggers != NULL) {
return AT_READ_OK; return AT_READ_OK;
} }

View File

@ -27,6 +27,7 @@ extern "C" {
struct attrib; struct attrib;
struct trigger; struct trigger;
struct storage; struct storage;
struct gamedata;
typedef struct trigger_type { typedef struct trigger_type {
const char *name; const char *name;
@ -34,7 +35,7 @@ extern "C" {
void(*finalize) (struct trigger *); void(*finalize) (struct trigger *);
int(*handle) (struct trigger *, void *); int(*handle) (struct trigger *, void *);
void(*write) (const struct trigger *, struct storage * store); void(*write) (const struct trigger *, struct storage * store);
int(*read) (struct trigger *, struct storage * store); int(*read) (struct trigger *, struct gamedata * store);
struct trigger_type *next; struct trigger_type *next;
} trigger_type; } trigger_type;
@ -73,7 +74,7 @@ extern "C" {
/* functions for making complex triggers: */ /* functions for making complex triggers: */
void free_triggers(trigger * triggers); /* release all these triggers */ void free_triggers(trigger * triggers); /* release all these triggers */
void write_triggers(struct storage *store, const trigger * t); void write_triggers(struct storage *store, const trigger * t);
int read_triggers(struct storage *store, trigger ** tp); int read_triggers(struct gamedata *data, trigger ** tp);
int handle_triggers(trigger ** triggers, void *data); int handle_triggers(trigger ** triggers, void *data);
extern struct attrib_type at_eventhandler; extern struct attrib_type at_eventhandler;