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 */
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <kernel/attrib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/base36.h>
|
2018-09-29 11:37:17 +02:00
|
|
|
#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>
|
2017-12-29 06:13:28 +01:00
|
|
|
#include <util/macros.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/resolve.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
|
|
|
|
#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 {
|
2015-01-30 20:37:14 +01:00
|
|
|
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
|
|
|
{
|
2018-12-15 19:38:40 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
{
|
2015-01-30 20:37:14 +01: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");
|
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
UNUSED_ARG(data);
|
2015-01-30 20:37:14 +01:00
|
|
|
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
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
createunit_data *td = (createunit_data *)t->data.v;
|
2016-02-16 07:30:26 +01:00
|
|
|
write_faction_reference(td->f->_alive ? td->f : NULL, store);
|
2015-01-30 20:37:14 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-13 15:25:07 +01:00
|
|
|
static int createunit_read(trigger * t, gamedata *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
createunit_data *td = (createunit_data *)t->data.v;
|
2017-09-19 11:42:02 +02:00
|
|
|
int id;
|
2016-02-17 13:55:48 +01:00
|
|
|
int result = AT_READ_OK;
|
2017-09-21 16:26:53 +02:00
|
|
|
|
2018-11-01 09:53:23 +01:00
|
|
|
id = read_faction_reference(data, &td->f);
|
2017-09-21 16:26:53 +02:00
|
|
|
if (id <= 0) {
|
2016-02-17 13:55:48 +01:00
|
|
|
result = AT_READ_FAIL;
|
|
|
|
}
|
2016-02-23 08:35:31 +01:00
|
|
|
|
2018-11-01 21:08:59 +01:00
|
|
|
read_region_reference(data, &td->r);
|
2017-09-19 11:42:02 +02:00
|
|
|
td->race = read_race_reference(data->store);
|
2016-02-17 13:55:48 +01:00
|
|
|
if (!td->race) {
|
|
|
|
result = AT_READ_FAIL;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
2016-02-13 15:25:07 +01:00
|
|
|
READ_INT(data->store, &td->number);
|
2016-02-17 13:55:48 +01:00
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
trigger_type tt_createunit = {
|
2015-01-30 20:37:14 +01:00
|
|
|
"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,
|
2015-01-30 20:37:14 +01:00
|
|
|
const struct race * rc, int number)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01: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
|
|
|
}
|