server/src/triggers/createunit.c

113 lines
2.5 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <platform.h>
#include "createunit.h"
/* kernel includes */
#include <kernel/faction.h>
#include <kernel/unit.h>
#include <kernel/race.h>
#include <kernel/region.h>
/* util includes */
#include <kernel/attrib.h>
2010-08-08 10:06:34 +02:00
#include <util/base36.h>
#include <kernel/event.h>
2018-09-29 13:21:46 +02:00
#include <kernel/gamedata.h>
2010-08-08 10:06:34 +02:00
#include <util/log.h>
#include <util/macros.h>
2010-08-08 10:06:34 +02:00
#include <util/resolve.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
/* ansi includes */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
/***
** restore a mage that was turned into a toad
**/
typedef struct createunit_data {
struct region *r;
struct faction *f;
const struct race *race;
int number;
2010-08-08 10:06:34 +02:00
} createunit_data;
2011-03-07 08:02:35 +01:00
static void createunit_init(trigger * t)
2010-08-08 10:06:34 +02:00
{
t->data.v = calloc(1, sizeof(createunit_data));
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void createunit_free(trigger * t)
2010-08-08 10:06:34 +02:00
{
free(t->data.v);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int createunit_handle(trigger * t, void *data)
2010-08-08 10:06:34 +02:00
{
/* call an event handler on createunit.
* data.v -> ( variant event, int timer )
*/
createunit_data *td = (createunit_data *)t->data.v;
if (td->r != NULL && td->f != NULL) {
create_unit(td->r, td->f, td->number, td->race, 0, NULL, NULL);
}
else {
log_error("could not perform createunit::handle()\n");
}
UNUSED_ARG(data);
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void createunit_write(const trigger * t, struct storage *store)
2010-08-08 10:06:34 +02:00
{
createunit_data *td = (createunit_data *)t->data.v;
write_faction_reference(td->f->_alive ? td->f : NULL, store);
write_region_reference(td->r, store);
write_race_reference(td->race, store);
WRITE_INT(store, td->number);
2010-08-08 10:06:34 +02:00
}
static int createunit_read(trigger * t, gamedata *data)
2010-08-08 10:06:34 +02:00
{
createunit_data *td = (createunit_data *)t->data.v;
int id;
int result = AT_READ_OK;
2017-09-21 16:26:53 +02:00
id = read_faction_reference(data, &td->f);
2017-09-21 16:26:53 +02:00
if (id <= 0) {
result = AT_READ_FAIL;
}
read_region_reference(data, &td->r);
td->race = read_race_reference(data->store);
if (!td->race) {
result = AT_READ_FAIL;
}
READ_INT(data->store, &td->number);
return result;
2010-08-08 10:06:34 +02:00
}
trigger_type tt_createunit = {
"createunit",
createunit_init,
createunit_free,
createunit_handle,
createunit_write,
createunit_read
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
trigger *trigger_createunit(region * r, struct faction * f,
const struct race * rc, int number)
2010-08-08 10:06:34 +02:00
{
trigger *t = t_new(&tt_createunit);
createunit_data *td = (createunit_data *)t->data.v;
td->r = r;
td->f = f;
td->race = rc;
td->number = number;
return t;
2010-08-08 10:06:34 +02:00
}