diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 3fe4a1ae8..34f46b169 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -594,7 +594,7 @@ function test_guard_resources() end local function is_flag_set(flags, flag) - return math.mod(flags, flag*2) - math.mod(flags, flag) == flag; + return math.fmod(flags, flag*2) - math.fmod(flags, flag) == flag; end function test_hero_hero_transfer() @@ -803,7 +803,7 @@ end local function find_in_report(f, pattern, extension) extension = extension or "nr" local filename = config.reportpath .. "/" .. get_turn() .. "-" .. itoa36(f.id) .. "." .. extension - local report = io.open(filename, 'rt'); + local report = io.open(filename, 'r'); assert_not_nil(report) t = report:read("*all") report:close() @@ -893,7 +893,7 @@ function test_parser() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/orders.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") assert_not_nil(file) file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") @@ -956,7 +956,7 @@ function test_bug_1814() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/1814.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write("; parse error follows: '\n") @@ -977,7 +977,7 @@ function test_bug_1679() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/1679.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write("NACH W\n") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8421e4692..633509d2d 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ set (ERESSEA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "Eressea Cor add_subdirectory(bindings) -find_package (Lua51 REQUIRED) +find_package (Lua 5 REQUIRED) find_package (ToLua REQUIRED) find_package (LibXml2 REQUIRED) find_package (Curses REQUIRED) diff --git a/src/bindings/CMakeLists.txt b/src/bindings/CMakeLists.txt index 94efddcb2..56310c2d3 100755 --- a/src/bindings/CMakeLists.txt +++ b/src/bindings/CMakeLists.txt @@ -9,7 +9,7 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC) set (BINDINGS_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "Eressea Lua Bindings") -find_package (Lua51 REQUIRED) +find_package (Lua 5 REQUIRED) find_package (ToLua REQUIRED) find_package (LibXml2 REQUIRED) find_package (Curses REQUIRED) diff --git a/src/bindings/bind_building.c b/src/bindings/bind_building.c index 4e51a0a65..230ba5b88 100644 --- a/src/bindings/bind_building.c +++ b/src/bindings/bind_building.c @@ -21,7 +21,6 @@ without prior permission by the authors of Eressea. #include -#include #include int tolua_buildinglist_next(lua_State * L) diff --git a/src/bindings/bind_faction.c b/src/bindings/bind_faction.c index 111b49f37..d5efd226c 100644 --- a/src/bindings/bind_faction.c +++ b/src/bindings/bind_faction.c @@ -29,7 +29,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include int tolua_factionlist_next(lua_State * L) diff --git a/src/bindings/bind_gmtool.c b/src/bindings/bind_gmtool.c index a55cb75b7..654f16f30 100644 --- a/src/bindings/bind_gmtool.c +++ b/src/bindings/bind_gmtool.c @@ -11,7 +11,6 @@ #include #include -#include #include static int tolua_run_mapper(lua_State * L) diff --git a/src/bindings/bind_hashtable.c b/src/bindings/bind_hashtable.c index 2069e272b..17c6c4975 100644 --- a/src/bindings/bind_hashtable.c +++ b/src/bindings/bind_hashtable.c @@ -23,7 +23,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bind_message.c b/src/bindings/bind_message.c index 6ff30e98a..2959c6c5c 100644 --- a/src/bindings/bind_message.c +++ b/src/bindings/bind_message.c @@ -13,7 +13,6 @@ #include /* lua includes */ -#include #include #include diff --git a/src/bindings/bind_region.c b/src/bindings/bind_region.c index 58f64203c..b5fb16108 100644 --- a/src/bindings/bind_region.c +++ b/src/bindings/bind_region.c @@ -37,7 +37,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bind_ship.c b/src/bindings/bind_ship.c index d5319dfe0..ddfa883ad 100644 --- a/src/bindings/bind_ship.c +++ b/src/bindings/bind_ship.c @@ -22,7 +22,6 @@ without prior permission by the authors of Eressea. #include -#include #include int tolua_shiplist_next(lua_State * L) diff --git a/src/bindings/bind_sqlite.c b/src/bindings/bind_sqlite.c index 0df9e530f..6f6c5ea29 100644 --- a/src/bindings/bind_sqlite.c +++ b/src/bindings/bind_sqlite.c @@ -16,7 +16,6 @@ without prior permission by the authors of Eressea. #include "bindings.h" #include -#include #include #define LTYPE_DB TOLUA_CAST "db" diff --git a/src/bindings/bind_storage.c b/src/bindings/bind_storage.c index 5e7768d2d..e2b83b670 100644 --- a/src/bindings/bind_storage.c +++ b/src/bindings/bind_storage.c @@ -22,7 +22,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include static int tolua_storage_create(lua_State * L) diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c index 543a9f9d5..5d4dd9b47 100755 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -51,7 +51,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bindings.c b/src/bindings/bindings.c index b452eb366..904e3e015 100755 --- a/src/bindings/bindings.c +++ b/src/bindings/bindings.c @@ -1160,34 +1160,9 @@ int tolua_bindings_open(lua_State * L) return 1; } -static const struct { - const char *name; - int (*func) (lua_State *); -} lualibs[] = { - { - "", luaopen_base}, { - LUA_TABLIBNAME, luaopen_table}, { - LUA_IOLIBNAME, luaopen_io}, { - LUA_STRLIBNAME, luaopen_string}, { - LUA_MATHLIBNAME, luaopen_math}, { - LUA_LOADLIBNAME, luaopen_package}, { - LUA_DBLIBNAME, luaopen_debug}, -#if LUA_VERSION_NUM>=501 - { - LUA_OSLIBNAME, luaopen_os}, -#endif - { - NULL, NULL} -}; - static void openlibs(lua_State * L) { - int i; - for (i = 0; lualibs[i].func; ++i) { - lua_pushcfunction(L, lualibs[i].func); - lua_pushstring(L, lualibs[i].name); - lua_call(L, 1, 0); - } + luaL_openlibs(L); } void lua_done(lua_State * L) { @@ -1195,7 +1170,7 @@ void lua_done(lua_State * L) { } lua_State *lua_init(void) { - lua_State *L = lua_open(); + lua_State *L = luaL_newstate(); openlibs(L); #ifdef BINDINGS_TOLUA diff --git a/src/bindings/helpers.c b/src/bindings/helpers.c index d6d73d7b2..5658f590c 100644 --- a/src/bindings/helpers.c +++ b/src/bindings/helpers.c @@ -31,8 +31,8 @@ without prior permission by the authors of Eressea. #include -#include #include +#include #include @@ -48,8 +48,7 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *o strlcpy(fname, iname, sizeof(fname)); strlcat(fname, "_give", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, s, TOLUA_CAST "unit"); tolua_pushusertype(L, d, TOLUA_CAST "unit"); @@ -81,8 +80,7 @@ static int limit_resource(const region * r, const resource_type * rtype) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_limit", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); @@ -111,8 +109,7 @@ produce_resource(region * r, const resource_type * rtype, int norders) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_produce", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushnumber(L, (lua_Number) norders); @@ -140,8 +137,7 @@ static int lc_age(struct attrib *a) if (fname != NULL) { lua_State *L = (lua_State *) global.vm_state; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (fparam) { @@ -201,8 +197,7 @@ static int lua_callspell(castorder * co) fname = fbuf; } - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { int nparam = 4; tolua_pushusertype(L, r, TOLUA_CAST "region"); @@ -254,8 +249,7 @@ static int lua_initfamiliar(unit * u) strlcpy(fname, "initfamiliar_", sizeof(fname)); strlcat(fname, u->race->_name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); @@ -290,8 +284,7 @@ lua_changeresource(unit * u, const struct resource_type *rtype, int delta) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_changeresource", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) delta); @@ -321,8 +314,7 @@ static int lua_getresource(unit * u, const struct resource_type *rtype) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_getresource", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); @@ -351,8 +343,7 @@ static bool lua_canuse_item(const unit * u, const struct item_type *itype) lua_State *L = (lua_State *) global.vm_state; const char *fname = "item_canuse"; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushstring(L, itype->rtype->_name[0]); @@ -381,8 +372,7 @@ lua_wage(const region * r, const faction * f, const race * rc, int in_turn) const char *fname = "wage"; int result = -1; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushusertype(L, (void *)f, TOLUA_CAST "faction"); @@ -413,8 +403,7 @@ static void lua_agebuilding(building * b) strlcpy(fname, "age_", sizeof(fname)); strlcat(fname, b->type->_name, sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); @@ -435,8 +424,7 @@ static int lua_building_protection(building * b, unit * u) const char *fname = "building_protection"; int result = 0; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -462,8 +450,7 @@ static double lua_building_taxes(building * b, int level) const char *fname = "building_taxes"; double result = 0.0F; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushnumber(L, level); @@ -489,8 +476,7 @@ static int lua_maintenance(const unit * u) const char *fname = "maintenance"; int result = -1; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -519,8 +505,7 @@ static int lua_equipmentcallback(const struct equipment *eq, unit * u) strlcpy(fname, "equip_", sizeof(fname)); strlcat(fname, eq->name, sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -551,8 +536,7 @@ lua_useitem(struct unit *u, const struct item_type *itype, int amount, strlcpy(fname, "use_", sizeof(fname)); strlcat(fname, itype->rtype->_name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount); @@ -582,8 +566,7 @@ static int lua_recruit(struct unit *u, const struct archetype *arch, int amount) strlcpy(fname, "recruit_", sizeof(fname)); strlcat(fname, arch->name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount); diff --git a/src/kernel/move.c b/src/kernel/move.c index b7c30a677..8b3572f6d 100644 --- a/src/kernel/move.c +++ b/src/kernel/move.c @@ -621,12 +621,7 @@ static bool is_freezing(const unit * u) return true; } -#define SA_HARBOUR 1 -#define SA_COAST 1 -#define SA_NO_INSECT -1 -#define SA_NO_COAST -2 - -static bool is_ship_allowed(struct ship *sh, const region * r) +int check_ship_allowed(struct ship *sh, const region * r) { int c = 0; static const building_type *bt_harbour = NULL; @@ -634,7 +629,7 @@ static bool is_ship_allowed(struct ship *sh, const region * r) if (bt_harbour == NULL) bt_harbour = bt_find("harbour"); - if (r_insectstalled(r)) { + if (sh->region && r_insectstalled(r)) { /* insekten dürfen nicht hier rein. haben wir welche? */ unit *u; @@ -654,7 +649,7 @@ static bool is_ship_allowed(struct ship *sh, const region * r) } } - if (buildingtype_exists(r, bt_harbour, true)) + if (bt_harbour && buildingtype_exists(r, bt_harbour, true)) return SA_HARBOUR; for (c = 0; sh->type->coasts[c] != NULL; ++c) { if (sh->type->coasts[c] == r->terrain) @@ -746,7 +741,7 @@ static void drifting_ships(region * r) region *rn; dir = (direction_t) ((d + d_offset) % MAXDIRECTIONS); rn = rconnect(r, dir); - if (rn != NULL && fval(rn->terrain, SAIL_INTO) && is_ship_allowed(sh, rn) > 0) { + if (rn != NULL && fval(rn->terrain, SAIL_INTO) && check_ship_allowed(sh, rn) > 0) { rnext = rn; if (!fval(rnext->terrain, SEA_REGION)) break; @@ -1809,7 +1804,7 @@ sail(unit * u, order * ord, bool move_on_land, region_list ** routep) } } - reason = is_ship_allowed(sh, next_point); + reason = check_ship_allowed(sh, next_point); if (reason<0) { /* for some reason or another, we aren't allowed in there.. */ if (check_leuchtturm(current_point, NULL) || reason == SA_NO_INSECT) { diff --git a/src/kernel/move.h b/src/kernel/move.h index 80744ce9f..c76c362c1 100644 --- a/src/kernel/move.h +++ b/src/kernel/move.h @@ -66,7 +66,13 @@ extern "C" { const struct building_type *bt); extern struct attrib_type at_speedup; + +#define SA_HARBOUR 2 +#define SA_COAST 1 +#define SA_NO_INSECT -1 +#define SA_NO_COAST -2 + extern int check_ship_allowed(struct ship *sh, const struct region * r); #ifdef __cplusplus } #endif diff --git a/src/kernel/move_test.c b/src/kernel/move_test.c index cea15292e..640b9ada8 100644 --- a/src/kernel/move_test.c +++ b/src/kernel/move_test.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include @@ -78,5 +80,7 @@ CuSuite *get_move_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_building_type_exists); + SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast); + SUITE_ADD_TEST(suite, test_ship_allowed_with_harbor); return suite; } diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 141bd4ba9..96745c59e 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -171,7 +171,6 @@ ship *new_ship(const ship_type * stype, region * r, const struct locale *lang) const char *sname = 0; assert(stype); - assert(r); sh->no = newcontainerid(); sh->coast = NODIRECTION; sh->type = stype; @@ -188,7 +187,9 @@ ship *new_ship(const ship_type * stype, region * r, const struct locale *lang) slprintf(buffer, sizeof(buffer), "%s %s", sname, shipid(sh)); sh->name = strdup(buffer); shash(sh); - addlist(&r->ships, sh); + if (r) { + addlist(&r->ships, sh); + } return sh; } diff --git a/src/tests.c b/src/tests.c index a0c5dcd94..dd05c7d93 100644 --- a/src/tests.c +++ b/src/tests.c @@ -138,6 +138,26 @@ ship * test_create_ship(region * r, const ship_type * stype) return s; } +ship_type * test_create_shiptype(const char ** names) +{ + ship_type * stype = (ship_type*)calloc(sizeof(ship_type), 1); + stype->name[0] = strdup(names[0]); + stype->name[1] = strdup(names[1]); + locale_setstring(default_locale, names[0], names[0]); + st_register(stype); + return stype; +} + +building_type * test_create_buildingtype(const char * name) +{ + building_type * btype = (building_type*)calloc(sizeof(building_type), 1); + btype->flags = BTF_NAMECHANGE; + btype->_name = strdup(name); + locale_setstring(default_locale, name, name); + bt_register(btype); + return btype; +} + item_type * test_create_itemtype(const char ** names) { resource_type * rtype; item_type * itype; @@ -160,16 +180,14 @@ void test_create_world(void) terrain_type *t_plain, *t_ocean; region *island[2]; int i; - building_type *btype; - ship_type *stype; item_type * itype; - const char * horses[2] = { "horse", "horse_p" }; + const char * names[] = { "horse", "horse_p", "boat", "boat_p" }; make_locale("de"); init_resources(); assert(!olditemtype[I_HORSE]); - itype = test_create_itemtype(horses); + itype = test_create_itemtype(names); olditemtype[I_HORSE] = itype; t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); @@ -192,17 +210,8 @@ void test_create_world(void) test_create_race("human"); - btype = (building_type*)calloc(sizeof(building_type), 1); - btype->flags = BTF_NAMECHANGE; - btype->_name = strdup("castle"); - locale_setstring(default_locale, "castle", "castle"); - bt_register(btype); - - stype = (ship_type*)calloc(sizeof(ship_type), 1); - stype->name[0] = strdup("boat"); - stype->name[1] = strdup("boat_p"); - locale_setstring(default_locale, "boat", "boat"); - st_register(stype); + test_create_buildingtype("castle"); + test_create_shiptype(names+2); } int main(int argc, char ** argv) { diff --git a/src/tests.h b/src/tests.h index 2ea13633e..15aa7a230 100644 --- a/src/tests.h +++ b/src/tests.h @@ -39,6 +39,9 @@ extern "C" { struct building * test_create_building(struct region * r, const struct building_type * btype); struct ship * test_create_ship(struct region * r, const struct ship_type * stype); struct item_type * test_create_itemtype(const char ** names); + struct ship_type *test_create_shiptype(const char **names); + struct building_type *test_create_buildingtype(const char *name); + int RunAllTests(void); #ifdef __cplusplus diff --git a/src/util/console.c b/src/util/console.c index 28586f704..b850678c5 100644 --- a/src/util/console.c +++ b/src/util/console.c @@ -23,6 +23,10 @@ #define LUA_MAXINPUT 512 #endif +#if LUA_VERSION_NUM >= 502 +#define lua_strlen(L, idx) lua_rawlen(L, idx) +#endif + #if defined(LUA_USE_READLINE) #include #include @@ -109,7 +113,7 @@ static int report(lua_State * L, int status) static int traceback(lua_State * L) { - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + lua_getglobal(L, "debug"); if (!lua_istable(L, -1)) { lua_pop(L, 1); return 1; @@ -149,8 +153,7 @@ static int docall(lua_State * L, int narg, int clear) static const char *get_prompt(lua_State * L, int firstline) { const char *p = NULL; - lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); p = lua_tostring(L, -1); if (p == NULL) p = (firstline ? PROMPT : PROMPT2);