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; data->type = type;
switch (type) { switch (type) {
case TSTRING: case TSTRING:
data->data.str = strdup(value.v); data->data.str = value.v?strdup(value.v):NULL;
break; break;
case TINTEGER: case TINTEGER:
data->data.i = value.i; data->data.i = value.i;

View File

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

View File

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

View File

@ -171,5 +171,8 @@ bind_building(lua_State * L)
.def_readwrite("size", &building::size) .def_readwrite("size", &building::size)
.def("add_action", &building_addaction) .def("add_action", &building_addaction)
.property("objects", &eressea::get_objects<building>) .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("alliance", &faction_getalliance, &faction_setalliance)
.property("race", &faction_getrace, &faction_setrace) .property("race", &faction_getrace, &faction_setrace)
.property("objects", &eressea::get_objects<faction>) .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) next_tag(int hash, const state * st)
{ {
while (st && hash!=MAXTHASH) { while (st && hash!=MAXTHASH) {
tag * t = st->selected->tags[hash]; tag * node = st->selected->tags[hash];
if (t!=NULL) return t; while (node!=NULL) {
region * r = findregion((short)node->coord.x, (short)node->coord.y);
if (r) {
return node;
}
node = node->nexthash;
}
++hash; ++hash;
} }
return NULL; return NULL;
@ -42,15 +48,18 @@ next_tag(int hash, const state * st)
class selectedregion { class selectedregion {
public: public:
static tag * next(tag * node) { static tag * next(tag * self) {
if (node->nexthash) { tag * node = self->nexthash;
return node->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 key = ((c->x << 12) ^ c->y);
unsigned int hash = key & (MAXTHASH-1); unsigned int hash = key & (MAXTHASH-1);
return next_tag(++hash, current_state); return next_tag(hash+1, current_state);
} }
static region * value(tag * node) { static region * value(tag * node) {
@ -66,21 +75,33 @@ selected_regions(void)
} }
static 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 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 void
bind_gmtool(lua_State * L) bind_gmtool(lua_State * L)
{ {
module(L, "gmtool")[ module(L, "gmtool")[
def("open", &gmtool_open),
def("close", &gmtool_close),
def("editor", &run_mapper), def("editor", &run_mapper),
def("get_selection", &selected_regions, return_stl_iterator), def("get_selection", &selected_regions, return_stl_iterator),
def("get_cursor", &current_region), def("get_cursor", &current_region),

View File

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

View File

@ -52,7 +52,7 @@ namespace eressea {
case TSHIP: case TSHIP:
return object(L, (ship*) val.v); return object(L, (ship*) val.v);
case TSTRING: case TSTRING:
return object(L, std::string((const char*) val.v)); return object(L, (const char*) val.v);
default: default:
assert(!"not implemented"); assert(!"not implemented");
} }
@ -88,7 +88,7 @@ namespace eressea {
template<> void objects::set<const char *, TSTRING>(const char * name, template<> void objects::set<const char *, TSTRING>(const char * name,
const char * value) { const char * value) {
variant val = { 0 }; variant val = { 0 };
val.v = strdup(value); if (value) val.v = strdup(value);
set_object(mAttribPtr, name, TSTRING, val); 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("buildings", &region_buildings, return_stl_iterator)
.property("ships", &region_ships, return_stl_iterator) .property("ships", &region_ships, return_stl_iterator)
.property("objects", &eressea::get_objects<region>) .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("size", &ship::size)
.def_readwrite("coast", &ship::coast) .def_readwrite("coast", &ship::coast)
.property("objects", &eressea::get_objects<ship>) .property("objects", &eressea::get_objects<ship>)
.scope [
def("create", &add_ship)
]
]; ];
} }

View File

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

View File

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

View File

@ -1,130 +1,135 @@
local function test_pure()
free_game()
local r = region.create(0, 0, "plain")
end
local function test_read_write() local function test_read_write()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
local u = unit.create(f, r) local u = unit.create(f, r)
u.number = 2 u.number = 2
local fno = f.id local fno = f.id
local uno = u.id local uno = u.id
local result = 0 local result = 0
assert(r.terrain=="plain") assert(r.terrain=="plain")
result = write_game("test_read_write.dat", "binary") result = write_game("test_read_write.dat", "binary")
assert(result==0) assert(result==0)
assert(get_region(0, 0)~=nil) assert(get_region(0, 0)~=nil)
assert(get_faction(fno)~=nil) assert(get_faction(fno)~=nil)
assert(get_unit(uno)~=nil) assert(get_unit(uno)~=nil)
r = nil r = nil
f = nil f = nil
u = nil u = nil
free_game() free_game()
assert(get_region(0, 0)==nil) assert(get_region(0, 0)==nil)
assert(get_faction(fno)==nil) assert(get_faction(fno)==nil)
assert(get_unit(uno)==nil) assert(get_unit(uno)==nil)
result = read_game("test_read_write.dat", "binary") result = read_game("test_read_write.dat", "binary")
assert(result==0) assert(result==0)
assert(get_region(0, 0)~=nil) assert(get_region(0, 0)~=nil)
assert(get_faction(fno)~=nil) assert(get_faction(fno)~=nil)
assert(get_unit(uno)~=nil) assert(get_unit(uno)~=nil)
free_game() free_game()
end end
local function test_gmtool() local function test_gmtool()
free_game() free_game()
local r1 = region.create(1, 0, "plain") local r1 = region.create(1, 0, "plain")
local r2 = region.create(1, 1, "plain") local r2 = region.create(1, 1, "plain")
local r3 = region.create(1, 2, "plain") local r3 = region.create(1, 2, "plain")
gmtool.open() gmtool.open()
gmtool.select(r1, true) gmtool.select(r1, true)
gmtool.select_at(0, 1, true) gmtool.select_at(0, 1, true)
gmtool.select(r2, true) gmtool.select(r2, true)
gmtool.select_at(0, 2, true) gmtool.select_at(0, 2, true)
gmtool.select(r3, false) gmtool.select(r3, false)
gmtool.select(r3, true) gmtool.select(r3, true)
gmtool.select_at(0, 3, false) gmtool.select_at(0, 3, false)
gmtool.select(r3, false) gmtool.select(r3, false)
local selections = 0 local selections = 0
for r in gmtool.get_selection() do for r in gmtool.get_selection() do
selections=selections+1 selections=selections+1
end end
assert(selections==2) assert(selections==2)
print(gmtool.get_cursor()) assert(gmtool.get_cursor()==nil)
gmtool.close() gmtool.close()
end end
local function test_faction() local function test_faction()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
assert(f) assert(f)
f.info = "Spazz" f.info = "Spazz"
assert(f.info=="Spazz") assert(f.info=="Spazz")
f:add_item("donotwant", 42) f:add_item("donotwant", 42)
f:add_item("stone", 42) f:add_item("stone", 42)
f:add_item("sword", 42) f:add_item("sword", 42)
local items = 0 local items = 0
for u in f.items do for u in f.items do
items = items + 1 items = items + 1
end end
assert(items==2) assert(items==2)
unit.create(f, r) unit.create(f, r)
unit.create(f, r) unit.create(f, r)
local units = 0 local units = 0
for u in f.units do for u in f.units do
units = units + 1 units = units + 1
end end
assert(units==2) assert(units==2)
end end
local function test_unit() local function test_unit()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
local u = unit.create(f, r) local u = unit.create(f, r)
u.number = 20 u.number = 20
u.name = "Enno" u.name = "Enno"
assert(u.name=="Enno") assert(u.name=="Enno")
u.info = "Spazz" u.info = "Spazz"
assert(u.info=="Spazz") assert(u.info=="Spazz")
u:add_item("sword", 4) u:add_item("sword", 4)
assert(u:get_item("sword")==4) assert(u:get_item("sword")==4)
assert(u:get_pooled("sword")==4) assert(u:get_pooled("sword")==4)
u:use_pooled("sword", 2) u:use_pooled("sword", 2)
assert(u:get_item("sword")==2) assert(u:get_item("sword")==2)
end end
local function test_region() local function test_region()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
r:set_resource("horse", 42) r:set_resource("horse", 42)
r:set_resource("money", 45) r:set_resource("money", 45)
r:set_resource("peasant", 200) r:set_resource("peasant", 200)
assert(r:get_resource("horse") == 42) assert(r:get_resource("horse") == 42)
assert(r:get_resource("money") == 45) assert(r:get_resource("money") == 45)
assert(r:get_resource("peasant") == 200) assert(r:get_resource("peasant") == 200)
end end
local function test_building() local function test_building()
free_game() free_game()
local u local u
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local b = building.create(r, "castle") local b = building.create(r, "castle")
u = unit.create(f, r) u = unit.create(f, r)
u.number = 1 u.number = 1
u.building = b u.building = b
u = unit.create(f, r) u = unit.create(f, r)
u.number = 2 u.number = 2
-- u.building = b -- u.building = b
u = unit.create(f, r) u = unit.create(f, r)
u.number = 3 u.number = 3
u.building = b u.building = b
local units = 0 local units = 0
for u in b.units do for u in b.units do
units = units + 1 units = units + 1
end end
assert(units==2) assert(units==2)
end end
local function loadscript(name) local function loadscript(name)
@ -136,38 +141,50 @@ local function loadscript(name)
end end
local function test_message() local function test_message()
free_game() free_game()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
local u = unit.create(f, r) local u = unit.create(f, r)
local msg = message.create("item_create_spell") local msg = message.create("item_create_spell")
msg:set_unit("mage", u) msg:set_unit("mage", u)
msg:set_int("number", 1) msg:set_int("number", 1)
msg:set_resource("item", "sword") msg:set_resource("item", "sword")
msg:send_region(r) msg:send_region(r)
msg:send_faction(f) msg:send_faction(f)
return msg return msg
end end
local function test_hashtable() local function test_hashtable()
free_game() free_game()
local f = faction.create("enno@eressea.de", "human", "de") local f = faction.create("enno@eressea.de", "human", "de")
f.objects:set("enno", "smart guy") f.objects:set("enno", "smart guy")
f.objects:set("age", 10) f.objects:set("age", 10)
assert(f.objects:get("jesus") == nil) assert(f.objects:get("jesus") == nil)
assert(f.objects:get("enno") == "smart guy") assert(f.objects:get("enno") == "smart guy")
assert(f.objects:get("age") == 10) assert(f.objects:get("age") == 10)
f.objects:set("age", nil) f.objects:set("age", nil)
assert(f.objects:get("age") == nil) assert(f.objects:get("age") == nil)
end end
loadscript("extensions.lua") loadscript("extensions.lua")
test_read_write() tests = {
test_region() ["test_pure"] = test_pure,
test_faction() ["test_read_write"] = test_read_write,
test_building() ["test_faction"] = test_faction,
test_unit() ["test_region"] = test_region,
test_message() ["test_building"] = test_building,
test_hashtable() ["test_unit"] = test_unit,
test_gmtool() ["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