From 18e7e14fc9f4430771aa4d6b9eacf17af289dd49 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Jun 2009 08:29:36 +0000 Subject: [PATCH] - new: get_* functions accept base36 strings - new tests for that. - some fixes for create() calls with illegal parameters - removed unused growing_trees() function --- src/common/gamecode/laws.c | 4 ++-- src/common/kernel/building.c | 1 + src/eressea/korrektur.c | 14 -------------- src/eressea/tolua/bind_building.c | 11 +++++++---- src/eressea/tolua/bind_ship.c | 15 ++++++++++----- src/eressea/tolua/bindings.c | 12 ++++++------ src/eressea/tolua/helpers.c | 14 ++++++++++++++ src/eressea/tolua/helpers.h | 1 + src/res/e2k9.xml | 3 ++- src/scripts/tests.lua | 31 +++++++++++++++++++++++++++++++ 10 files changed, 74 insertions(+), 32 deletions(-) diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 1172471f1..6f9ac7aa1 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -675,7 +675,7 @@ count_race(const region *r, const race *rc) extern struct attrib_type at_germs; static void -trees(region * r, const int current_season, const int last_weeks_season) +growing_trees(region * r, const int current_season, const int last_weeks_season) { int growth, grownup_trees, i, seeds, sprout; direction_t d; @@ -852,7 +852,7 @@ demographics(void) plagues(r, false); horses(r); if (current_season != SEASON_WINTER) { - trees(r, current_season, last_weeks_season); + growing_trees(r, current_season, last_weeks_season); } } diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index 16350c6e8..82bea812f 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -124,6 +124,7 @@ building_type * bt_find(const char* name) { const struct building_typelist * btl = buildingtypes; + assert(name); while (btl && strcmp(btl->type->_name, name)) btl = btl->next; if (btl==NULL) { return NULL; diff --git a/src/eressea/korrektur.c b/src/eressea/korrektur.c index 3ab4881f8..251a33c4e 100644 --- a/src/eressea/korrektur.c +++ b/src/eressea/korrektur.c @@ -362,20 +362,6 @@ get_timeout(trigger * td, trigger * tfind) #include #include -int -growing_trees(void) -{ - region *r; - - for(r=regions; r; r=r->next) { - if(rtrees(r, 2)) { - rsettrees(r, 1, rtrees(r, 2)/4); - rsettrees(r, 0, rtrees(r, 2)/2); - } - } - return 0; -} - static int fix_undead(void) { diff --git a/src/eressea/tolua/bind_building.c b/src/eressea/tolua/bind_building.c index f498baeb4..2afffa2c4 100644 --- a/src/eressea/tolua/bind_building.c +++ b/src/eressea/tolua/bind_building.c @@ -127,10 +127,13 @@ tolua_building_create(lua_State* tolua_S) { region * r = (region *)tolua_tousertype(tolua_S, 1, 0); const char * bname = tolua_tostring(tolua_S, 2, 0); - const building_type * btype = bt_find(bname); - building * b = new_building(btype, r, NULL); - tolua_pushusertype(tolua_S, (void*)b, "building"); - return 1; + if (bname) { + const building_type * btype = bt_find(bname); + building * b = new_building(btype, r, NULL); + tolua_pushusertype(tolua_S, (void*)b, "building"); + return 1; + } + return 0; } static int diff --git a/src/eressea/tolua/bind_ship.c b/src/eressea/tolua/bind_ship.c index 624b94ce3..e236c37e6 100644 --- a/src/eressea/tolua/bind_ship.c +++ b/src/eressea/tolua/bind_ship.c @@ -108,11 +108,16 @@ tolua_ship_create(lua_State* tolua_S) { region * r = (region *)tolua_tousertype(tolua_S, 1, 0); const char * sname = tolua_tostring(tolua_S, 2, 0); - const ship_type * stype = st_find(sname); - ship * sh = new_ship(stype, NULL, r); - sh->size = stype->construction->maxsize; - tolua_pushusertype(tolua_S, (void*)sh, "ship"); - return 1; + if (sname) { + const ship_type * stype = st_find(sname); + if (stype) { + ship * sh = new_ship(stype, NULL, r); + sh->size = stype->construction->maxsize; + tolua_pushusertype(tolua_S, (void*)sh, "ship"); + return 1; + } + } + return 0; } static int diff --git a/src/eressea/tolua/bindings.c b/src/eressea/tolua/bindings.c index 5f914ab96..58755b090 100644 --- a/src/eressea/tolua/bindings.c +++ b/src/eressea/tolua/bindings.c @@ -15,7 +15,7 @@ without prior permission by the authors of Eressea. #include "bind_unit.h" #include "bind_faction.h" #include "bind_region.h" - +#include "helpers.h" #include @@ -581,7 +581,7 @@ tolua_read_game(lua_State* tolua_S) static int tolua_get_faction(lua_State* tolua_S) { - int no = (int)tolua_tonumber(tolua_S, 1, 0); + int no = tolua_toid(tolua_S, 1, 0); faction * f = findfaction(no); tolua_pushusertype(tolua_S, f, "faction"); @@ -612,7 +612,7 @@ tolua_get_region_byid(lua_State* tolua_S) static int tolua_get_building(lua_State* tolua_S) { - int no = (int)tolua_tonumber(tolua_S, 1, 0); + int no = tolua_toid(tolua_S, 1, 0); building * b = findbuilding(no); tolua_pushusertype(tolua_S, b, "building"); @@ -622,7 +622,7 @@ tolua_get_building(lua_State* tolua_S) static int tolua_get_ship(lua_State* tolua_S) { - int no = (int)tolua_tonumber(tolua_S, 1, 0); + int no = tolua_toid(tolua_S, 1, 0); ship * sh = findship(no); tolua_pushusertype(tolua_S, sh, "ship"); @@ -632,7 +632,7 @@ tolua_get_ship(lua_State* tolua_S) static int tolua_get_alliance(lua_State* tolua_S) { - int no = (int)tolua_tonumber(tolua_S, 1, 0); + int no = tolua_toid(tolua_S, 1, 0); alliance * f = findalliance(no); tolua_pushusertype(tolua_S, f, "alliance"); @@ -642,7 +642,7 @@ tolua_get_alliance(lua_State* tolua_S) static int tolua_get_unit(lua_State* tolua_S) { - int no = (int)tolua_tonumber(tolua_S, 1, 0); + int no = tolua_toid(tolua_S, 1, 0); unit * u = findunit(no); tolua_pushusertype(tolua_S, u, "unit"); diff --git a/src/eressea/tolua/helpers.c b/src/eressea/tolua/helpers.c index 6977cadb6..9b83c2a23 100644 --- a/src/eressea/tolua/helpers.c +++ b/src/eressea/tolua/helpers.c @@ -529,6 +529,20 @@ lua_recruit(struct unit * u, const struct archetype * arch, int amount) return result; } +int +tolua_toid(lua_State* tolua_S, int idx, int def) +{ + int no = 0; + int type = lua_type(tolua_S, idx); + if (type==LUA_TNUMBER) { + no = (int)tolua_tonumber(tolua_S, idx, def); + } else { + const char * str = tolua_tostring(tolua_S, idx, NULL); + no = str?atoi36(str):def; + } + return no; +} + void register_tolua_helpers(void) { diff --git a/src/eressea/tolua/helpers.h b/src/eressea/tolua/helpers.h index c28588699..9a0470517 100644 --- a/src/eressea/tolua/helpers.h +++ b/src/eressea/tolua/helpers.h @@ -15,6 +15,7 @@ extern "C" { #endif void register_tolua_helpers(void); + int tolua_toid(struct lua_State* tolua_S, int idx, int def); #ifdef __cplusplus } diff --git a/src/res/e2k9.xml b/src/res/e2k9.xml index 4c99fcfb2..862ebf93c 100644 --- a/src/res/e2k9.xml +++ b/src/res/e2k9.xml @@ -18,10 +18,11 @@ - + + diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 49ceff53c..4f1d17bf5 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -416,6 +416,35 @@ function test_upkeep() assert(u:get_item("money")==u.number) end +function test_id() + free_game() + local r = region.create(0, 0, "plain") + + local f = faction.create("enno@eressea.de", "human", "de") + f.id = atoi36("42") + assert(get_faction(42)~=f) + assert(get_faction("42")==f) + assert(get_faction(atoi36("42"))==f) + + local u = unit.create(f, r, 1) + u.id = atoi36("42") + assert(get_unit(42)~=u) + assert(get_unit("42")==u) + assert(get_unit(atoi36("42"))==u) + + local b = building.create(r, "castle") + -- b.id = atoi36("42") + local fortytwo = itoa36(b.id) + assert(get_building(fortytwo)==b) + assert(get_building(atoi36(fortytwo))==b) + + local s = ship.create(r, "boat") + -- s.id = atoi36("42") + local fortytwo = itoa36(s.id) + assert(get_ship(fortytwo)==s) + assert(get_ship(atoi36(fortytwo))==s) +end + function test_herbalism() free_game() local r = region.create(0, 0, "plain") @@ -474,10 +503,12 @@ tests = { ["storage"] = test_storage, ["taxes"] = test_taxes, ["upkeep"] = test_upkeep, + ["id"] = test_id, ["work"] = test_work, ["market"] = test_market } mytests = { + ["id"] = test_id, ["upkeep"] = test_upkeep, ["taxes"] = test_taxes }