making tostring() work

This commit is contained in:
Enno Rehling 2008-12-20 19:45:51 +00:00
parent a3ba0166cf
commit ba644a2e68
8 changed files with 58 additions and 3 deletions

View File

@ -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 "";
}

View File

@ -316,6 +316,7 @@ plane_getbyname(const char * str)
return 0;
}
void
bind_region(lua_State * L)
{

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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()