diff --git a/src/common/kernel/region.c b/src/common/kernel/region.c index a3ddb9fd9..d1584368c 100644 --- a/src/common/kernel/region.c +++ b/src/common/kernel/region.c @@ -1353,7 +1353,7 @@ region_setinfo(struct region * r, const char * info) const char * region_getinfo(const region * r) { - return (const char *)r->display; + return r->display?r->display:""; } void @@ -1367,6 +1367,8 @@ region_setname(struct region * r, const char * name) const char * region_getname(const region * r) { - if (r->land) return (const char *)r->land->name; - return NULL; + if (r->land && r->land->name) { + return r->land->name; + } + return ""; } diff --git a/src/eressea/lua/region.cpp b/src/eressea/lua/region.cpp index a27b376e4..4e4205038 100644 --- a/src/eressea/lua/region.cpp +++ b/src/eressea/lua/region.cpp @@ -316,6 +316,7 @@ plane_getbyname(const char * str) return 0; } + void bind_region(lua_State * L) { diff --git a/src/eressea/tolua/bind_building.c b/src/eressea/tolua/bind_building.c index 32e32dfb7..05abfd986 100644 --- a/src/eressea/tolua/bind_building.c +++ b/src/eressea/tolua/bind_building.c @@ -119,6 +119,13 @@ tolua_building_create(lua_State* tolua_S) return 1; } +static int +tolua_building_tostring(lua_State *tolua_S) +{ + building * self = (building *)tolua_tousertype(tolua_S, 1, 0); + lua_pushstring(tolua_S, buildingname(self)); + return 1; +} void tolua_building_open(lua_State* tolua_S) @@ -133,6 +140,8 @@ tolua_building_open(lua_State* tolua_S) tolua_cclass(tolua_S, "building", "building", "", NULL); tolua_beginmodule(tolua_S, "building"); { + tolua_function(tolua_S, "__tostring", tolua_building_tostring); + tolua_variable(tolua_S, "id", tolua_building_get_id, NULL); tolua_variable(tolua_S, "name", tolua_building_get_name, tolua_building_set_name); tolua_variable(tolua_S, "units", tolua_building_get_units, NULL); diff --git a/src/eressea/tolua/bind_faction.c b/src/eressea/tolua/bind_faction.c index 832596cee..e6eb5769e 100644 --- a/src/eressea/tolua/bind_faction.c +++ b/src/eressea/tolua/bind_faction.c @@ -374,6 +374,13 @@ static int tolua_faction_get_items(lua_State* tolua_S) return 1; } +static int +tolua_faction_tostring(lua_State *tolua_S) +{ + faction * self = (faction *)tolua_tousertype(tolua_S, 1, 0); + lua_pushstring(tolua_S, factionname(self)); + return 1; +} void tolua_faction_open(lua_State* tolua_S) @@ -388,6 +395,8 @@ tolua_faction_open(lua_State* tolua_S) tolua_cclass(tolua_S, "faction", "faction", "", NULL); tolua_beginmodule(tolua_S, "faction"); { + tolua_function(tolua_S, "__tostring", tolua_faction_tostring); + tolua_variable(tolua_S, "name", tolua_faction_get_name, tolua_faction_set_name); tolua_variable(tolua_S, "info", tolua_faction_get_info, tolua_faction_set_info); tolua_variable(tolua_S, "units", tolua_faction_get_units, NULL); diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c index 0ee515790..2d2f3b40f 100644 --- a/src/eressea/tolua/bind_region.c +++ b/src/eressea/tolua/bind_region.c @@ -288,6 +288,14 @@ tolua_region_setkey(lua_State* tolua_S) return 0; } +static int +tolua_region_tostring(lua_State *tolua_S) +{ + region * self = (region *)tolua_tousertype(tolua_S, 1, 0); + lua_pushstring(tolua_S, regionname(self, NULL)); + return 1; +} + void tolua_region_open(lua_State* tolua_S) { @@ -301,6 +309,7 @@ tolua_region_open(lua_State* tolua_S) tolua_beginmodule(tolua_S, "region"); { tolua_function(tolua_S, "create", tolua_region_create); + tolua_function(tolua_S, "__tostring", tolua_region_tostring); tolua_variable(tolua_S, "id", tolua_region_get_id, NULL); tolua_variable(tolua_S, "x", tolua_region_get_x, NULL); diff --git a/src/eressea/tolua/bind_ship.c b/src/eressea/tolua/bind_ship.c index 5002551ae..357212d7d 100644 --- a/src/eressea/tolua/bind_ship.c +++ b/src/eressea/tolua/bind_ship.c @@ -95,6 +95,14 @@ tolua_ship_create(lua_State* tolua_S) return 1; } +static int +tolua_ship_tostring(lua_State *tolua_S) +{ + ship * self = (ship *)tolua_tousertype(tolua_S, 1, 0); + lua_pushstring(tolua_S, shipname(self)); + return 1; +} + void tolua_ship_open(lua_State* tolua_S) { @@ -107,6 +115,7 @@ tolua_ship_open(lua_State* tolua_S) tolua_cclass(tolua_S, "ship", "ship", "", NULL); tolua_beginmodule(tolua_S, "ship"); { + tolua_function(tolua_S, "__tostring", tolua_ship_tostring); tolua_variable(tolua_S, "id", tolua_ship_get_id, NULL); tolua_variable(tolua_S, "name", tolua_ship_get_name, tolua_ship_set_name); tolua_variable(tolua_S, "units", tolua_ship_get_units, NULL); diff --git a/src/eressea/tolua/bind_unit.c b/src/eressea/tolua/bind_unit.c index 8e3c8505d..aadd3cdaa 100644 --- a/src/eressea/tolua/bind_unit.c +++ b/src/eressea/tolua/bind_unit.c @@ -832,6 +832,14 @@ tolua_unit_create(lua_State* tolua_S) return 1; } +static int +tolua_unit_tostring(lua_State *tolua_S) +{ + unit * self = (unit *)tolua_tousertype(tolua_S, 1, 0); + lua_pushstring(tolua_S, unitname(self)); + return 1; +} + void tolua_unit_open(lua_State * tolua_S) { @@ -845,6 +853,7 @@ tolua_unit_open(lua_State * tolua_S) tolua_cclass(tolua_S, "unit", "unit", "", NULL); tolua_beginmodule(tolua_S, "unit"); { + tolua_function(tolua_S, "__tostring", tolua_unit_tostring); tolua_function(tolua_S, "create", tolua_unit_create); tolua_variable(tolua_S, "name", tolua_unit_get_name, tolua_unit_set_name); diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index ae014720b..365539765 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -108,6 +108,13 @@ local function test_region() assert(r:get_resource("horse") == 42) assert(r:get_resource("money") == 45) assert(r:get_resource("peasant") == 200) + r.name = nil + r.info = nil + assert(r.name=="") + assert(r.info=="") + r.name = "Alabasterheim" + r.info = "Hier wohnen die siebzehn Zwerge" + assert(tostring(r) == "Alabasterheim (0,0)") end local function test_building()