fixing luabind

This commit is contained in:
Enno Rehling 2008-12-20 17:06:05 +00:00
parent e32eebfd15
commit c23da92768
14 changed files with 213 additions and 158 deletions

View File

@ -193,7 +193,7 @@ object_set(attrib * a, object_type type, variant value)
data->type = type;
switch (type) {
case TSTRING:
data->data.str = strdup(value.v);
data->data.str = value.v?strdup(value.v):NULL;
break;
case TINTEGER:
data->data.i = value.i;

View File

@ -1897,13 +1897,12 @@ eval_race(struct opstack ** stack, const void * userdata)
static void
eval_order(struct opstack ** stack, const void * userdata) /* order -> string */
{
const faction * report = (const faction*)userdata;
const struct order * ord = (const struct order *)opop(stack).v;
static char buf[256];
size_t len;
variant var;
unused(report);
unused(userdata);
write_order(ord, buf, sizeof(buf));
len = strlen(buf);
var.v = strcpy(balloc(len+1), buf);

View File

@ -27,7 +27,7 @@ extern "C" {
extern int force_color;
struct state * state_open();
struct state * state_open(void);
void state_close(struct state * );
#ifdef __cplusplus

View File

@ -171,5 +171,8 @@ bind_building(lua_State * L)
.def_readwrite("size", &building::size)
.def("add_action", &building_addaction)
.property("objects", &eressea::get_objects<building>)
.scope [
def("create", &add_building)
]
];
}

View File

@ -332,5 +332,8 @@ bind_faction(lua_State * L)
.property("alliance", &faction_getalliance, &faction_setalliance)
.property("race", &faction_getrace, &faction_setrace)
.property("objects", &eressea::get_objects<faction>)
.scope [
def("create", &add_faction)
]
];
}

View File

@ -33,8 +33,14 @@ static tag *
next_tag(int hash, const state * st)
{
while (st && hash!=MAXTHASH) {
tag * t = st->selected->tags[hash];
if (t!=NULL) return t;
tag * node = st->selected->tags[hash];
while (node!=NULL) {
region * r = findregion((short)node->coord.x, (short)node->coord.y);
if (r) {
return node;
}
node = node->nexthash;
}
++hash;
}
return NULL;
@ -42,15 +48,18 @@ next_tag(int hash, const state * st)
class selectedregion {
public:
static tag * next(tag * node) {
if (node->nexthash) {
return node->nexthash;
static tag * next(tag * self) {
tag * node = self->nexthash;
while (node) {
region * r = findregion((short)node->coord.x, (short)node->coord.y);
if (r) return node;
node = node->nexthash;
}
coordinate * c = &node->coord;
coordinate * c = &self->coord;
unsigned int key = ((c->x << 12) ^ c->y);
unsigned int hash = key & (MAXTHASH-1);
return next_tag(++hash, current_state);
return next_tag(hash+1, current_state);
}
static region * value(tag * node) {
@ -66,21 +75,33 @@ selected_regions(void)
}
static void
gmtool_select_coordinate(int x, int y, int select)
gmtool_select_coordinate(int x, int y, bool select)
{
select_coordinate(current_state->selected, x, y, select);
select_coordinate(current_state->selected, x, y, select?1:0);
}
static void
gmtool_select_region(region& r, int select)
gmtool_select_region(region& r, bool select)
{
select_coordinate(current_state->selected, r.x, r.y, select);
select_coordinate(current_state->selected, r.x, r.y, select?1:0);
}
static void gmtool_open(void)
{
state_open();
}
static void gmtool_close(void)
{
state_close(current_state);
}
void
bind_gmtool(lua_State * L)
{
module(L, "gmtool")[
def("open", &gmtool_open),
def("close", &gmtool_close),
def("editor", &run_mapper),
def("get_selection", &selected_regions, return_stl_iterator),
def("get_cursor", &current_region),

View File

@ -190,7 +190,6 @@ bind_message(lua_State * L)
.def(constructor<const char *>())
.def(tostring(const_self))
.def("create", create_lua_message)
.def("set_unit", &lua_message::set_unit)
.def("set_region", &lua_message::set_region)
.def("set_resource", &lua_message::set_resource)
@ -198,5 +197,8 @@ bind_message(lua_State * L)
.def("set_string", &lua_message::set_string)
.def("send_faction", &lua_message::send_faction)
.def("send_region", &lua_message::send_region)
.scope [
def("create", &create_lua_message)
]
];
}

View File

@ -52,7 +52,7 @@ namespace eressea {
case TSHIP:
return object(L, (ship*) val.v);
case TSTRING:
return object(L, std::string((const char*) val.v));
return object(L, (const char*) val.v);
default:
assert(!"not implemented");
}
@ -88,7 +88,7 @@ namespace eressea {
template<> void objects::set<const char *, TSTRING>(const char * name,
const char * value) {
variant val = { 0 };
val.v = strdup(value);
if (value) val.v = strdup(value);
set_object(mAttribPtr, name, TSTRING, val);
}

View File

@ -377,5 +377,8 @@ bind_region(lua_State * L)
.property("buildings", &region_buildings, return_stl_iterator)
.property("ships", &region_ships, return_stl_iterator)
.property("objects", &eressea::get_objects<region>)
.scope [
def("create", &region_terraform)
]
];
}

View File

@ -107,5 +107,8 @@ bind_ship(lua_State * L)
.def_readwrite("size", &ship::size)
.def_readwrite("coast", &ship::coast)
.property("objects", &eressea::get_objects<ship>)
.scope [
def("create", &add_ship)
]
];
}

View File

@ -19,6 +19,7 @@
using namespace luabind;
#include <util/language.h>
#include <util/rng.h>
#include <kernel/region.h>
#include <kernel/skill.h>
#include <kernel/terrainid.h>

View File

@ -600,5 +600,8 @@ bind_unit(lua_State * L)
.property("race", &unit_getrace, &unit_setrace)
.property("hp_max", &unit_hpmax)
.property("objects", &eressea::get_objects<unit>)
.scope [
def("create", &add_unit)
]
];
}

View File

@ -1,3 +1,8 @@
local function test_pure()
free_game()
local r = region.create(0, 0, "plain")
end
local function test_read_write()
free_game()
local r = region.create(0, 0, "plain")
@ -48,7 +53,7 @@ local function test_gmtool()
selections=selections+1
end
assert(selections==2)
print(gmtool.get_cursor())
assert(gmtool.get_cursor()==nil)
gmtool.close()
end
@ -163,11 +168,23 @@ local function test_hashtable()
end
loadscript("extensions.lua")
test_read_write()
test_region()
test_faction()
test_building()
test_unit()
test_message()
test_hashtable()
test_gmtool()
tests = {
["test_pure"] = test_pure,
["test_read_write"] = test_read_write,
["test_faction"] = test_faction,
["test_region"] = test_region,
["test_building"] = test_building,
["test_unit"] = test_unit,
["test_message"] = test_message,
["test_hashtable"] = test_hashtable,
["test_gmtool"] = test_gmtool
}
for k, v in pairs(tests) do
local status, err = pcall(v)
if not status then
print("[FAIL] " .. k .. ": " .. err)
else
print("[OK] " .. k)
end
end