2003-11-10 00:36:11 +01:00
|
|
|
#include <config.h>
|
2003-12-14 11:02:29 +01:00
|
|
|
#include <cstring>
|
2003-11-10 00:36:11 +01:00
|
|
|
#include <eressea.h>
|
2004-07-10 14:54:25 +02:00
|
|
|
#include "list.h"
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
// kernel includes
|
|
|
|
#include <building.h>
|
2004-07-10 14:00:21 +02:00
|
|
|
#include <region.h>
|
2004-07-10 14:54:25 +02:00
|
|
|
#include <unit.h>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
// lua includes
|
|
|
|
#include <lua.hpp>
|
|
|
|
#include <luabind/luabind.hpp>
|
|
|
|
#include <luabind/iterator_policy.hpp>
|
|
|
|
|
2004-07-10 14:54:25 +02:00
|
|
|
// util includes
|
|
|
|
#include <util/base36.h>
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
using namespace luabind;
|
|
|
|
|
2004-07-10 14:00:21 +02:00
|
|
|
static building *
|
|
|
|
add_building(region * r, const char * name)
|
|
|
|
{
|
|
|
|
const building_type * btype = bt_find(name);
|
|
|
|
if (btype==NULL) return NULL;
|
|
|
|
return new_building(btype, r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct lcbuilding_data {
|
|
|
|
building * b;
|
|
|
|
char * fname;
|
|
|
|
} lcbuilding_data;
|
|
|
|
|
|
|
|
static void
|
|
|
|
lc_init(struct attrib *a)
|
|
|
|
{
|
|
|
|
a->data.v = calloc(1, sizeof(lcbuilding_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lc_done(struct attrib *a)
|
|
|
|
{
|
|
|
|
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
|
|
|
|
if (data->fname) free(data->fname);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lc_age(struct attrib * a)
|
|
|
|
{
|
|
|
|
lua_State * L = (lua_State *)global.vm_state;
|
|
|
|
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
|
|
|
|
const char * fname = data->fname;
|
|
|
|
building * b = data->b;
|
|
|
|
|
|
|
|
assert(b!=NULL);
|
|
|
|
if (fname==NULL) return -1;
|
|
|
|
|
|
|
|
luabind::object globals = luabind::get_globals(L);
|
|
|
|
if (globals.at(fname).type()!=LUA_TFUNCTION) return -1;
|
|
|
|
|
|
|
|
return luabind::call_function<int>(L, fname, *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lc_write(const struct attrib * a, FILE* F)
|
|
|
|
{
|
|
|
|
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
|
|
|
|
const char * fname = data->fname;
|
|
|
|
building * b = data->b;
|
|
|
|
|
|
|
|
write_building_reference(b, F);
|
|
|
|
fwritestr(F, fname);
|
2004-07-10 19:16:15 +02:00
|
|
|
fputc(' ', F);
|
2004-07-10 14:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lc_read(struct attrib * a, FILE* F)
|
|
|
|
{
|
|
|
|
char lbuf[256];
|
|
|
|
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
|
|
|
|
|
|
|
|
read_building_reference(&data->b, F);
|
|
|
|
freadstr(F, lbuf, sizeof(lbuf));
|
|
|
|
data->fname = strdup(lbuf);
|
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
attrib_type at_luacall_building = {
|
|
|
|
"lcbuilding",
|
|
|
|
lc_init,
|
|
|
|
lc_done,
|
|
|
|
lc_age,
|
|
|
|
lc_write,
|
|
|
|
lc_read,
|
|
|
|
ATF_UNIQUE
|
|
|
|
};
|
|
|
|
|
2004-07-10 18:40:36 +02:00
|
|
|
static int
|
|
|
|
building_addaction(building& b, const char * fname)
|
2004-07-10 14:00:21 +02:00
|
|
|
{
|
|
|
|
attrib * a = a_add(&b.attribs, a_new(&at_luacall_building));
|
|
|
|
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
|
|
|
|
data->b = &b;
|
|
|
|
data->fname = strdup(fname);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
building_getinfo(const building& b)
|
|
|
|
{
|
|
|
|
return b.display;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
building_setinfo(building& b, const char * info)
|
|
|
|
{
|
|
|
|
set_string(&b.display, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
building_getname(const building& b)
|
|
|
|
{
|
|
|
|
return b.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
building_setname(building& b, const char * name)
|
|
|
|
{
|
|
|
|
set_string(&b.name, name);
|
|
|
|
}
|
|
|
|
|
2004-07-10 14:54:25 +02:00
|
|
|
static std::ostream&
|
|
|
|
operator<<(std::ostream& stream, building& b)
|
|
|
|
{
|
|
|
|
stream << b.name;
|
|
|
|
stream << " (" << itoa36(b.no) << ")";
|
|
|
|
stream << ", " << b.type->_name;
|
|
|
|
stream << " size " << b.size;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
operator==(const building& a, const building&b)
|
|
|
|
{
|
|
|
|
return a.no==b.no;
|
|
|
|
}
|
|
|
|
|
|
|
|
class buildingunit {
|
|
|
|
public:
|
|
|
|
static unit * next(unit * node) {
|
|
|
|
building * b = node->building;
|
|
|
|
do {
|
|
|
|
node = node->next;
|
|
|
|
} while (node !=NULL && node->building!=b);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
static unit * value(unit * node) { return node; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static eressea::list<unit *, unit *, buildingunit>
|
|
|
|
building_units(const building& b) {
|
|
|
|
region * r = b.region;
|
|
|
|
unit * u = r->units;
|
|
|
|
while (u!=NULL && u->building!=&b) u=u->next;
|
|
|
|
return eressea::list<unit *, unit *, buildingunit>(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
void
|
|
|
|
bind_building(lua_State * L)
|
|
|
|
{
|
2004-07-10 14:00:21 +02:00
|
|
|
at_register(&at_luacall_building);
|
2003-11-10 00:36:11 +01:00
|
|
|
module(L)[
|
|
|
|
def("get_building", &findbuilding),
|
2004-07-10 14:00:21 +02:00
|
|
|
def("add_building", &add_building),
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
class_<struct building>("building")
|
2004-07-10 14:54:25 +02:00
|
|
|
.def(tostring(self))
|
|
|
|
.def(self == building())
|
2004-07-10 14:00:21 +02:00
|
|
|
.property("name", &building_getname, &building_setname)
|
2004-07-10 14:54:25 +02:00
|
|
|
.property("info", &building_getinfo, &building_setinfo)
|
|
|
|
.property("units", &building_units, return_stl_iterator)
|
2004-07-10 14:00:21 +02:00
|
|
|
.def_readonly("region", &building::region)
|
2003-11-10 00:36:11 +01:00
|
|
|
.def_readonly("id", &building::no)
|
|
|
|
.def_readwrite("size", &building::size)
|
2004-07-10 18:40:36 +02:00
|
|
|
.def("add_action", &building_addaction)
|
2003-11-10 00:36:11 +01:00
|
|
|
];
|
|
|
|
}
|