2015-06-26 14:31:21 +02:00
|
|
|
/*
|
2010-08-08 10:06:34 +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 "bind_building.h"
|
|
|
|
#include "bind_unit.h"
|
|
|
|
|
2010-08-15 04:41:18 +02:00
|
|
|
#include <kernel/config.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
|
2015-06-19 13:17:01 +02:00
|
|
|
#include <util/log.h>
|
2010-08-15 04:41:18 +02:00
|
|
|
#include <util/language.h>
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <tolua.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int tolua_buildinglist_next(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building **building_ptr =
|
|
|
|
(building **)lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
building *u = *building_ptr;
|
|
|
|
if (u != NULL) {
|
|
|
|
tolua_pushusertype(L, (void *)u, TOLUA_CAST "building");
|
|
|
|
*building_ptr = u->next;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0; /* no more values to return */
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 07:07:57 +02:00
|
|
|
static int tolua_building_set_working(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
2014-07-05 07:07:57 +02:00
|
|
|
bool flag = !!lua_toboolean(L, 2);
|
2016-08-21 20:12:28 +02:00
|
|
|
if (flag) self->flags |= BLD_MAINTAINED;
|
|
|
|
else self->flags &= ~BLD_MAINTAINED;
|
2014-07-05 07:07:57 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_building_get_working(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
2016-08-21 20:12:28 +02:00
|
|
|
bool flag = (self->flags&BLD_MAINTAINED) != 0;
|
2014-07-05 07:07:57 +02:00
|
|
|
lua_pushboolean(L, flag);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushusertype(L, building_getregion(self), TOLUA_CAST "region");
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_set_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
building_setregion(self, (region *)tolua_tousertype(L, 2, 0));
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_info(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, self->display);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_set_info(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *info = tolua_tostring(L, 2, 0);
|
|
|
|
free(self->display);
|
|
|
|
if (info)
|
2017-01-10 16:31:05 +01:00
|
|
|
self->display = strdup(info);
|
2015-01-30 20:37:14 +01:00
|
|
|
else
|
|
|
|
self->display = NULL;
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_name(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, building_getname(self));
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_set_name(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
building_setname(self, tolua_tostring(L, 2, 0));
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:13:01 +01:00
|
|
|
static int tolua_building_get_maxsize(lua_State * L)
|
|
|
|
{
|
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushinteger(L, self->type->maxsize);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_size(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->size);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_set_size(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
self->size = (int)tolua_tonumber(L, 2, 0);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_units(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
unit **unit_ptr = (unit **)lua_newuserdata(L, sizeof(unit *));
|
|
|
|
unit *u = self->region->units;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
while (u && u->building != self)
|
|
|
|
u = u->next;
|
|
|
|
luaL_getmetatable(L, "unit");
|
|
|
|
lua_setmetatable(L, -2);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
*unit_ptr = u;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushcclosure(L, tolua_unitlist_nextb, 1);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_id(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->no);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_type(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, self->type->_name);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_typename(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *b = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (b) {
|
|
|
|
int size = (int)tolua_tonumber(L, 2, b->size);
|
|
|
|
tolua_pushstring(L, buildingtype(b->type, b, size));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_get_owner(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *b = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
unit *u = b ? building_owner(b) : NULL;
|
|
|
|
tolua_pushusertype(L, u, TOLUA_CAST "unit");
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_set_owner(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *b = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
unit *u = (unit *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (b != u->building) {
|
|
|
|
u_set_building(u, b);
|
|
|
|
}
|
|
|
|
building_set_owner(u);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_create(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r = (region *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *bname = tolua_tostring(L, 2, 0);
|
2015-06-26 14:31:21 +02:00
|
|
|
if (!r) {
|
|
|
|
log_error("building.create expects a region as argument 1");
|
2015-11-05 08:36:32 +01:00
|
|
|
} else if (!bname) {
|
2015-06-26 14:31:21 +02:00
|
|
|
log_error("building.create expects a name as argument 2");
|
2015-11-05 08:36:32 +01:00
|
|
|
} else {
|
2015-01-30 20:37:14 +01:00
|
|
|
const building_type *btype = bt_find(bname);
|
|
|
|
if (btype) {
|
|
|
|
building *b = new_building(btype, r, default_locale);
|
|
|
|
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
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 int tolua_building_tostring(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushstring(L, buildingname(self));
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_building_destroy(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building *self = (building *)tolua_tousertype(L, 1, 0);
|
|
|
|
remove_building(&self->region->buildings, self);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_building_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
/* register user types */
|
|
|
|
tolua_usertype(L, TOLUA_CAST "building");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_cclass(L, TOLUA_CAST "building", TOLUA_CAST "building", TOLUA_CAST "",
|
|
|
|
NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "building");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "create", tolua_building_create);
|
|
|
|
tolua_function(L, TOLUA_CAST "destroy", tolua_building_destroy);
|
|
|
|
tolua_function(L, TOLUA_CAST "__tostring", tolua_building_tostring);
|
|
|
|
|
|
|
|
tolua_variable(L, TOLUA_CAST "id", tolua_building_get_id, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "owner", tolua_building_get_owner,
|
|
|
|
tolua_building_set_owner);
|
|
|
|
tolua_variable(L, TOLUA_CAST "type", tolua_building_get_type, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "name", tolua_building_get_name,
|
|
|
|
tolua_building_set_name);
|
|
|
|
tolua_variable(L, TOLUA_CAST "info", tolua_building_get_info,
|
|
|
|
tolua_building_set_info);
|
|
|
|
tolua_variable(L, TOLUA_CAST "units", tolua_building_get_units, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "region", tolua_building_get_region,
|
|
|
|
tolua_building_set_region);
|
2015-11-16 01:13:01 +01:00
|
|
|
tolua_variable(L, TOLUA_CAST "maxsize", tolua_building_get_maxsize, NULL);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_variable(L, TOLUA_CAST "size", tolua_building_get_size,
|
|
|
|
tolua_building_set_size);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_typename", tolua_building_get_typename);
|
|
|
|
tolua_variable(L, TOLUA_CAST "working", tolua_building_get_working, tolua_building_set_working);
|
|
|
|
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|