From 4e2abeb5596fb19d6eacb4911291a876e825a59b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 21 Jan 2009 08:25:43 +0000 Subject: [PATCH] lua: resource-level, read_game w/ one parameter --- src/eressea/tolua/bind_region.c | 20 ++++++++++++++++++++ src/eressea/tolua/bind_unit.c | 2 +- src/eressea/tolua/bindings.c | 8 ++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c index 2d2f3b40f..5ad66d727 100644 --- a/src/eressea/tolua/bind_region.c +++ b/src/eressea/tolua/bind_region.c @@ -17,6 +17,7 @@ without prior permission by the authors of Eressea. #include "bind_building.h" #include +#include #include #include #include @@ -128,6 +129,24 @@ static int tolua_region_set_flag(lua_State* tolua_S) return 0; } +static int +tolua_region_get_resourcelevel(lua_State* tolua_S) +{ + region * r = (region *)tolua_tousertype(tolua_S, 1, 0); + const char * type = tolua_tostring(tolua_S, 2, 0); + const resource_type * rtype = rt_find(type); + if (rtype!=NULL) { + const rawmaterial * rm; + for (rm=r->resources;rm;rm=rm->next) { + if (rm->type->rtype==rtype) { + tolua_pushnumber(tolua_S, (lua_Number)rm->level); + return 1; + } + } + } + return 0; +} + static int tolua_region_get_resource(lua_State* tolua_S) { @@ -320,6 +339,7 @@ tolua_region_open(lua_State* tolua_S) tolua_variable(tolua_S, "ships", tolua_region_get_ships, NULL); tolua_variable(tolua_S, "buildings", tolua_region_get_buildings, NULL); tolua_variable(tolua_S, "terrain", tolua_region_get_terrain, NULL); + tolua_function(tolua_S, "get_resourcelevel", tolua_region_get_resourcelevel); tolua_function(tolua_S, "get_resource", tolua_region_get_resource); tolua_function(tolua_S, "set_resource", tolua_region_set_resource); tolua_function(tolua_S, "get_flag", tolua_region_get_flag); diff --git a/src/eressea/tolua/bind_unit.c b/src/eressea/tolua/bind_unit.c index 8cf0f3cdb..dc412d11f 100644 --- a/src/eressea/tolua/bind_unit.c +++ b/src/eressea/tolua/bind_unit.c @@ -15,7 +15,7 @@ without prior permission by the authors of Eressea. #include "bind_unit.h" #include "bindings.h" -// Atributes includes +// attributes includes #include #include diff --git a/src/eressea/tolua/bindings.c b/src/eressea/tolua/bindings.c index 5560d289e..ace4ee16d 100644 --- a/src/eressea/tolua/bindings.c +++ b/src/eressea/tolua/bindings.c @@ -543,8 +543,8 @@ tolua_write_game(lua_State* tolua_S) const char * filename = tolua_tostring(tolua_S, 1, 0); const char * mode = tolua_tostring(tolua_S, 2, 0); - int result, m = IO_TEXT; - if (strcmp(mode, "text")!=0) m = IO_BINARY; + int result, m = IO_BINARY; + if (mode && strcmp(mode, "text")==0) m = IO_TEXT; remove_empty_factions(true); result = writegame(filename, m); @@ -558,8 +558,8 @@ tolua_read_game(lua_State* tolua_S) const char * filename = tolua_tostring(tolua_S, 1, 0); const char * mode = tolua_tostring(tolua_S, 2, 0); - int rv, m = IO_TEXT; - if (strcmp(mode, "text")!=0) m = IO_BINARY; + int rv, m = IO_BINARY; + if (mode && strcmp(mode, "text")==0) m = IO_TEXT; rv = readgame(filename, m, false); tolua_pushnumber(tolua_S, (lua_Number)rv);