2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2014-07-26 22:52:25 +02:00
|
|
|
+-------------------+
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
+-------------------+
|
|
|
|
|
|
|
|
This program may not be used, modified or distributed
|
|
|
|
without prior permission by the authors of Eressea.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/version.h>
|
|
|
|
#include <util/attrib.h>
|
2016-02-13 13:42:02 +01:00
|
|
|
#include <util/gamedata.h>
|
2014-07-26 22:52:25 +02:00
|
|
|
#include <util/log.h>
|
|
|
|
#include <util/resolve.h>
|
|
|
|
|
|
|
|
#include <storage.h>
|
|
|
|
|
|
|
|
#include <tolua.h>
|
|
|
|
#include <lua.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2014-08-02 23:05:01 +02:00
|
|
|
#include <string.h>
|
2014-07-26 22:52:25 +02:00
|
|
|
|
|
|
|
typedef struct building_action {
|
|
|
|
char *fname;
|
|
|
|
char *param;
|
|
|
|
} building_action;
|
|
|
|
|
2015-12-16 22:18:44 +01:00
|
|
|
static int lc_age(struct attrib *a, void *owner)
|
2014-07-26 22:52:25 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building_action *data = (building_action *)a->data.v;
|
|
|
|
const char *fname = data->fname;
|
|
|
|
const char *fparam = data->param;
|
2015-12-17 12:23:07 +01:00
|
|
|
building *b = (building *)owner;
|
2015-01-30 20:37:14 +01:00
|
|
|
int result = -1;
|
2014-07-26 22:52:25 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
assert(b != NULL);
|
|
|
|
if (fname != NULL) {
|
|
|
|
lua_State *L = (lua_State *)global.vm_state;
|
2014-07-26 22:52:25 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_getglobal(L, fname);
|
|
|
|
if (lua_isfunction(L, -1)) {
|
|
|
|
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building");
|
|
|
|
if (fparam) {
|
|
|
|
tolua_pushstring(L, fparam);
|
|
|
|
}
|
2014-07-26 22:52:25 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
if (lua_pcall(L, fparam ? 2 : 1, 1, 0) != 0) {
|
|
|
|
const char *error = lua_tostring(L, -1);
|
|
|
|
log_error("lc_age(%s) calling '%s': %s.\n", buildingname(b), fname, error);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (int)lua_tonumber(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("lc_age(%s) calling '%s': not a function.\n", buildingname(b), fname);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return (result != 0) ? AT_AGE_KEEP : AT_AGE_REMOVE;
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *NULLSTRING = "(null)";
|
|
|
|
|
|
|
|
static void lc_init(struct attrib *a)
|
|
|
|
{
|
|
|
|
a->data.v = calloc(1, sizeof(building_action));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lc_done(struct attrib *a)
|
|
|
|
{
|
|
|
|
building_action *data = (building_action *)a->data.v;
|
|
|
|
if (data->fname)
|
|
|
|
free(data->fname);
|
|
|
|
if (data->param)
|
|
|
|
free(data->param);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lc_write(const struct attrib *a, const void *owner, struct storage *store)
|
|
|
|
{
|
|
|
|
building_action *data = (building_action *)a->data.v;
|
|
|
|
const char *fname = data->fname;
|
|
|
|
const char *fparam = data->param;
|
|
|
|
|
2015-12-17 12:59:12 +01:00
|
|
|
unused_arg(owner);
|
2014-07-26 22:52:25 +02:00
|
|
|
WRITE_TOK(store, fname);
|
|
|
|
WRITE_TOK(store, fparam ? fparam : NULLSTRING);
|
|
|
|
}
|
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
static int lc_read(struct attrib *a, void *owner, gamedata *data)
|
2014-07-26 22:52:25 +02:00
|
|
|
{
|
2016-02-13 13:42:02 +01:00
|
|
|
struct storage *store = data->store;
|
2014-07-26 22:52:25 +02:00
|
|
|
char name[NAMESIZE];
|
2016-02-13 13:42:02 +01:00
|
|
|
building_action *bd = (building_action *)a->data.v;
|
2015-12-17 12:23:07 +01:00
|
|
|
building *b = (building *)owner;
|
|
|
|
int result = 0;
|
2016-02-13 14:09:36 +01:00
|
|
|
if (data->version < ATTRIBOWNER_VERSION) {
|
2016-02-13 20:38:32 +01:00
|
|
|
result = read_reference(&b, data, read_building_reference, resolve_building);
|
2015-12-17 12:23:07 +01:00
|
|
|
assert(b == owner);
|
|
|
|
}
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_TOK(store, name, sizeof(name));
|
2014-07-26 22:52:25 +02:00
|
|
|
if (strcmp(name, "tunnel_action") == 0) {
|
|
|
|
/* E2: Weltentor has a new module, doesn't need this any longer */
|
|
|
|
result = 0;
|
2015-12-17 12:23:07 +01:00
|
|
|
b = 0;
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-02-13 13:42:02 +01:00
|
|
|
bd->fname = _strdup(name);
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
2014-12-12 23:00:30 +01:00
|
|
|
READ_TOK(store, name, sizeof(name));
|
|
|
|
if (strcmp(name, "tnnL") == 0) {
|
|
|
|
/* tunnel_action was the old Weltentore, their code has changed. ignore this object */
|
|
|
|
result = 0;
|
2015-12-17 12:23:07 +01:00
|
|
|
b = 0;
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
2014-12-12 23:00:30 +01:00
|
|
|
if (strcmp(name, NULLSTRING) == 0)
|
2016-02-13 13:42:02 +01:00
|
|
|
bd->param = 0;
|
2014-12-12 23:00:30 +01:00
|
|
|
else {
|
2016-02-13 13:42:02 +01:00
|
|
|
bd->param = _strdup(name);
|
2014-07-26 22:52:25 +02:00
|
|
|
}
|
2015-12-17 12:23:07 +01:00
|
|
|
if (result == 0 && !b) {
|
2014-07-26 22:52:25 +02:00
|
|
|
return AT_READ_FAIL;
|
|
|
|
}
|
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
attrib_type at_building_action = {
|
|
|
|
"lcbuilding",
|
|
|
|
lc_init, lc_done,
|
|
|
|
lc_age,
|
|
|
|
lc_write, lc_read
|
|
|
|
};
|
|
|
|
|
|
|
|
void building_addaction(building * b, const char *fname, const char *param)
|
|
|
|
{
|
|
|
|
attrib *a = a_add(&b->attribs, a_new(&at_building_action));
|
|
|
|
building_action *data = (building_action *)a->data.v;
|
|
|
|
data->fname = _strdup(fname);
|
|
|
|
if (param) {
|
|
|
|
data->param = _strdup(param);
|
|
|
|
}
|
|
|
|
}
|