From c23da92768e0264311f2bcac5185ea404d74e21f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 20 Dec 2008 17:06:05 +0000 Subject: [PATCH] fixing luabind --- src/common/attributes/object.c | 2 +- src/common/kernel/alliance.c | 2 +- src/common/kernel/reports.c | 3 +- src/eressea/gmtool.h | 2 +- src/eressea/lua/building.cpp | 3 + src/eressea/lua/faction.cpp | 3 + src/eressea/lua/gm.cpp | 43 +++-- src/eressea/lua/message.cpp | 4 +- src/eressea/lua/objects.cpp | 4 +- src/eressea/lua/region.cpp | 3 + src/eressea/lua/ship.cpp | 3 + src/eressea/lua/test.cpp | 1 + src/eressea/lua/unit.cpp | 3 + src/scripts/tests.lua | 295 +++++++++++++++++---------------- 14 files changed, 213 insertions(+), 158 deletions(-) diff --git a/src/common/attributes/object.c b/src/common/attributes/object.c index d3c977f8e..aa5eef22e 100644 --- a/src/common/attributes/object.c +++ b/src/common/attributes/object.c @@ -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; diff --git a/src/common/kernel/alliance.c b/src/common/kernel/alliance.c index f1ea5ee68..30a537507 100644 --- a/src/common/kernel/alliance.c +++ b/src/common/kernel/alliance.c @@ -355,4 +355,4 @@ void alliance_setname(alliance * self, const char * name) free(self->name); if (name) self->name = strdup(name); else self->name = NULL; -} \ No newline at end of file +} diff --git a/src/common/kernel/reports.c b/src/common/kernel/reports.c index d0c2f63d5..e690fe4a9 100644 --- a/src/common/kernel/reports.c +++ b/src/common/kernel/reports.c @@ -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); diff --git a/src/eressea/gmtool.h b/src/eressea/gmtool.h index 4864a1230..a60c37a83 100644 --- a/src/eressea/gmtool.h +++ b/src/eressea/gmtool.h @@ -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 diff --git a/src/eressea/lua/building.cpp b/src/eressea/lua/building.cpp index 49543e2d7..031fb8cf4 100644 --- a/src/eressea/lua/building.cpp +++ b/src/eressea/lua/building.cpp @@ -171,5 +171,8 @@ bind_building(lua_State * L) .def_readwrite("size", &building::size) .def("add_action", &building_addaction) .property("objects", &eressea::get_objects) + .scope [ + def("create", &add_building) + ] ]; } diff --git a/src/eressea/lua/faction.cpp b/src/eressea/lua/faction.cpp index 1863d3a84..d5fef4355 100644 --- a/src/eressea/lua/faction.cpp +++ b/src/eressea/lua/faction.cpp @@ -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) + .scope [ + def("create", &add_faction) + ] ]; } diff --git a/src/eressea/lua/gm.cpp b/src/eressea/lua/gm.cpp index cd19d6fa3..61311c093 100644 --- a/src/eressea/lua/gm.cpp +++ b/src/eressea/lua/gm.cpp @@ -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", ¤t_region), diff --git a/src/eressea/lua/message.cpp b/src/eressea/lua/message.cpp index 139ca35a7..e1c32a6f9 100644 --- a/src/eressea/lua/message.cpp +++ b/src/eressea/lua/message.cpp @@ -190,7 +190,6 @@ bind_message(lua_State * L) .def(constructor()) .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) + ] ]; } diff --git a/src/eressea/lua/objects.cpp b/src/eressea/lua/objects.cpp index 77b63066f..5ec723045 100644 --- a/src/eressea/lua/objects.cpp +++ b/src/eressea/lua/objects.cpp @@ -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 * name, const char * value) { variant val = { 0 }; - val.v = strdup(value); + if (value) val.v = strdup(value); set_object(mAttribPtr, name, TSTRING, val); } diff --git a/src/eressea/lua/region.cpp b/src/eressea/lua/region.cpp index aea0e8758..387f8ecac 100644 --- a/src/eressea/lua/region.cpp +++ b/src/eressea/lua/region.cpp @@ -377,5 +377,8 @@ bind_region(lua_State * L) .property("buildings", ®ion_buildings, return_stl_iterator) .property("ships", ®ion_ships, return_stl_iterator) .property("objects", &eressea::get_objects) + .scope [ + def("create", ®ion_terraform) + ] ]; } diff --git a/src/eressea/lua/ship.cpp b/src/eressea/lua/ship.cpp index b1c9ef8bc..34c29fda9 100644 --- a/src/eressea/lua/ship.cpp +++ b/src/eressea/lua/ship.cpp @@ -107,5 +107,8 @@ bind_ship(lua_State * L) .def_readwrite("size", &ship::size) .def_readwrite("coast", &ship::coast) .property("objects", &eressea::get_objects) + .scope [ + def("create", &add_ship) + ] ]; } diff --git a/src/eressea/lua/test.cpp b/src/eressea/lua/test.cpp index 05d51e7b9..916f52493 100644 --- a/src/eressea/lua/test.cpp +++ b/src/eressea/lua/test.cpp @@ -19,6 +19,7 @@ using namespace luabind; #include +#include #include #include #include diff --git a/src/eressea/lua/unit.cpp b/src/eressea/lua/unit.cpp index 30749a8cd..500263e5b 100644 --- a/src/eressea/lua/unit.cpp +++ b/src/eressea/lua/unit.cpp @@ -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) + .scope [ + def("create", &add_unit) + ] ]; } diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 5cb3b267a..1481d9edd 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -1,130 +1,135 @@ +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") - local f = faction.create("enno@eressea.de", "human", "de") - local u = unit.create(f, r) - u.number = 2 - local fno = f.id - local uno = u.id - local result = 0 - assert(r.terrain=="plain") - result = write_game("test_read_write.dat", "binary") - assert(result==0) - assert(get_region(0, 0)~=nil) - assert(get_faction(fno)~=nil) - assert(get_unit(uno)~=nil) - r = nil - f = nil - u = nil - free_game() - assert(get_region(0, 0)==nil) - assert(get_faction(fno)==nil) - assert(get_unit(uno)==nil) - result = read_game("test_read_write.dat", "binary") - assert(result==0) - assert(get_region(0, 0)~=nil) - assert(get_faction(fno)~=nil) - assert(get_unit(uno)~=nil) - free_game() + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("enno@eressea.de", "human", "de") + local u = unit.create(f, r) + u.number = 2 + local fno = f.id + local uno = u.id + local result = 0 + assert(r.terrain=="plain") + result = write_game("test_read_write.dat", "binary") + assert(result==0) + assert(get_region(0, 0)~=nil) + assert(get_faction(fno)~=nil) + assert(get_unit(uno)~=nil) + r = nil + f = nil + u = nil + free_game() + assert(get_region(0, 0)==nil) + assert(get_faction(fno)==nil) + assert(get_unit(uno)==nil) + result = read_game("test_read_write.dat", "binary") + assert(result==0) + assert(get_region(0, 0)~=nil) + assert(get_faction(fno)~=nil) + assert(get_unit(uno)~=nil) + free_game() end local function test_gmtool() - free_game() - local r1 = region.create(1, 0, "plain") - local r2 = region.create(1, 1, "plain") - local r3 = region.create(1, 2, "plain") - gmtool.open() - gmtool.select(r1, true) - gmtool.select_at(0, 1, true) - gmtool.select(r2, true) - gmtool.select_at(0, 2, true) - gmtool.select(r3, false) - gmtool.select(r3, true) - gmtool.select_at(0, 3, false) - gmtool.select(r3, false) - - local selections = 0 - for r in gmtool.get_selection() do - selections=selections+1 - end - assert(selections==2) - print(gmtool.get_cursor()) + free_game() + local r1 = region.create(1, 0, "plain") + local r2 = region.create(1, 1, "plain") + local r3 = region.create(1, 2, "plain") + gmtool.open() + gmtool.select(r1, true) + gmtool.select_at(0, 1, true) + gmtool.select(r2, true) + gmtool.select_at(0, 2, true) + gmtool.select(r3, false) + gmtool.select(r3, true) + gmtool.select_at(0, 3, false) + gmtool.select(r3, false) + + local selections = 0 + for r in gmtool.get_selection() do + selections=selections+1 + end + assert(selections==2) + assert(gmtool.get_cursor()==nil) - gmtool.close() + gmtool.close() end local function test_faction() - free_game() - local r = region.create(0, 0, "plain") - local f = faction.create("enno@eressea.de", "human", "de") - assert(f) - f.info = "Spazz" - assert(f.info=="Spazz") - f:add_item("donotwant", 42) - f:add_item("stone", 42) - f:add_item("sword", 42) - local items = 0 - for u in f.items do - items = items + 1 - end - assert(items==2) - unit.create(f, r) - unit.create(f, r) - local units = 0 - for u in f.units do - units = units + 1 - end - assert(units==2) + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("enno@eressea.de", "human", "de") + assert(f) + f.info = "Spazz" + assert(f.info=="Spazz") + f:add_item("donotwant", 42) + f:add_item("stone", 42) + f:add_item("sword", 42) + local items = 0 + for u in f.items do + items = items + 1 + end + assert(items==2) + unit.create(f, r) + unit.create(f, r) + local units = 0 + for u in f.units do + units = units + 1 + end + assert(units==2) end local function test_unit() - free_game() - local r = region.create(0, 0, "plain") - local f = faction.create("enno@eressea.de", "human", "de") - local u = unit.create(f, r) - u.number = 20 - u.name = "Enno" - assert(u.name=="Enno") - u.info = "Spazz" - assert(u.info=="Spazz") - u:add_item("sword", 4) - assert(u:get_item("sword")==4) - assert(u:get_pooled("sword")==4) - u:use_pooled("sword", 2) - assert(u:get_item("sword")==2) + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("enno@eressea.de", "human", "de") + local u = unit.create(f, r) + u.number = 20 + u.name = "Enno" + assert(u.name=="Enno") + u.info = "Spazz" + assert(u.info=="Spazz") + u:add_item("sword", 4) + assert(u:get_item("sword")==4) + assert(u:get_pooled("sword")==4) + u:use_pooled("sword", 2) + assert(u:get_item("sword")==2) end local function test_region() - free_game() - local r = region.create(0, 0, "plain") - r:set_resource("horse", 42) - r:set_resource("money", 45) - r:set_resource("peasant", 200) - assert(r:get_resource("horse") == 42) - assert(r:get_resource("money") == 45) - assert(r:get_resource("peasant") == 200) + free_game() + local r = region.create(0, 0, "plain") + r:set_resource("horse", 42) + r:set_resource("money", 45) + r:set_resource("peasant", 200) + assert(r:get_resource("horse") == 42) + assert(r:get_resource("money") == 45) + assert(r:get_resource("peasant") == 200) end local function test_building() - free_game() - local u - local f = faction.create("enno@eressea.de", "human", "de") - local r = region.create(0, 0, "plain") - local b = building.create(r, "castle") - u = unit.create(f, r) - u.number = 1 - u.building = b - u = unit.create(f, r) - u.number = 2 - -- u.building = b - u = unit.create(f, r) - u.number = 3 - u.building = b - local units = 0 - for u in b.units do - units = units + 1 - end - assert(units==2) + free_game() + local u + local f = faction.create("enno@eressea.de", "human", "de") + local r = region.create(0, 0, "plain") + local b = building.create(r, "castle") + u = unit.create(f, r) + u.number = 1 + u.building = b + u = unit.create(f, r) + u.number = 2 + -- u.building = b + u = unit.create(f, r) + u.number = 3 + u.building = b + local units = 0 + for u in b.units do + units = units + 1 + end + assert(units==2) end local function loadscript(name) @@ -136,38 +141,50 @@ local function loadscript(name) end local function test_message() - free_game() - local r = region.create(0, 0, "plain") - local f = faction.create("enno@eressea.de", "human", "de") - local u = unit.create(f, r) - local msg = message.create("item_create_spell") - msg:set_unit("mage", u) - msg:set_int("number", 1) - msg:set_resource("item", "sword") - msg:send_region(r) - msg:send_faction(f) - - return msg + free_game() + local r = region.create(0, 0, "plain") + local f = faction.create("enno@eressea.de", "human", "de") + local u = unit.create(f, r) + local msg = message.create("item_create_spell") + msg:set_unit("mage", u) + msg:set_int("number", 1) + msg:set_resource("item", "sword") + msg:send_region(r) + msg:send_faction(f) + + return msg end local function test_hashtable() - free_game() - local f = faction.create("enno@eressea.de", "human", "de") - f.objects:set("enno", "smart guy") - f.objects:set("age", 10) - assert(f.objects:get("jesus") == nil) - assert(f.objects:get("enno") == "smart guy") - assert(f.objects:get("age") == 10) - f.objects:set("age", nil) - assert(f.objects:get("age") == nil) + free_game() + local f = faction.create("enno@eressea.de", "human", "de") + f.objects:set("enno", "smart guy") + f.objects:set("age", 10) + assert(f.objects:get("jesus") == nil) + assert(f.objects:get("enno") == "smart guy") + assert(f.objects:get("age") == 10) + f.objects:set("age", nil) + assert(f.objects:get("age") == nil) 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