From 5f7860b6e6aba2002f4fc7340b79a8697392750a Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Sun, 21 Mar 2021 12:47:51 +0100 Subject: [PATCH 01/15] =?UTF-8?q?fix=20f=C3=BCr=20Schild=20des=20Fisches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/battle.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/battle.c b/src/battle.c index 425ccf6b1..a7c97fc6b 100644 --- a/src/battle.c +++ b/src/battle.c @@ -111,6 +111,7 @@ const troop no_troop = { 0, 0 }; static int max_turns; static int rule_damage; static int rule_loot; +static float loot_divisor; static int flee_chance_max_percent; static int flee_chance_base; static int flee_chance_skill_bonus; @@ -148,6 +149,7 @@ static void init_rules(void) rule_vampire = config_get_int("rules.combat.demon_vampire", 0); rule_loot = config_get_int("rules.combat.loot", LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT); + loot_divisor = config_get_flt("rules.items.loot_divisor", 1); /* new formula to calculate to-hit-chance */ skill_formula = config_get_int("rules.combat.skill_formula", FORMULA_ORIG); @@ -1234,7 +1236,7 @@ static int apply_magicshield(int reduced_damage, fighter *df, /* jeder Schaden wird um effect% reduziert bis der Schild duration * Trefferpunkte aufgefangen hat */ if (me->typ == SHIELD_REDUCE) { - int hp = reduced_damage * (me->effect / 100); + int hp = reduced_damage * me->effect / 100; reduced_damage -= hp; me->duration -= hp; } @@ -2440,10 +2442,9 @@ static int loot_quota(const unit * src, const unit * dst, { UNUSED_ARG(type); if (dst && src && src->faction != dst->faction) { - double divisor = config_get_flt("rules.items.loot_divisor", 1); - assert(divisor <= 0 || divisor >= 1); - if (divisor >= 1) { - double r = n / divisor; + assert(loot_divisor <= 0 || loot_divisor >= 1); + if (loot_divisor > 1) { + double r = n / loot_divisor; int x = (int)r; r = r - x; From b48ade8598ded4f128d4f71b54b200f3dcffef97 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 16 Mar 2021 18:14:25 +0100 Subject: [PATCH 02/15] remove tolua submodule, build with system library (WIP) --- .gitmodules | 3 -- .travis.yml | 2 +- CMakeLists.txt | 2 +- cmake/Modules/FindToLua.cmake | 73 +++++++++++++++++++++++++++++++++++ tolua | 1 - 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 cmake/Modules/FindToLua.cmake delete mode 160000 tolua diff --git a/.gitmodules b/.gitmodules index 4c0b4a9ae..306652a59 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,6 +5,3 @@ [submodule "clibs"] path = clibs url = https://github.com/ennorehling/clibs -[submodule "tolua"] - path = tolua - url = https://github.com/ennorehling/tolua.git diff --git a/.travis.yml b/.travis.yml index 8d5e50027..cd71ee585 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ addons: packages: - libbsd-dev - libdb-dev - - liblua5.3-dev + - libtolua-dev - libncurses5-dev - libsqlite3-dev - libexpat1-dev diff --git a/CMakeLists.txt b/CMakeLists.txt index 24c49a514..ed4ef5d7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ if (NOT CURSES_FOUND) find_package (Curses) endif (NOT CURSES_FOUND) +find_package (ToLua 5.2 REQUIRED) #find_package (BerkeleyDB REQUIRED) find_package (SQLite3 REQUIRED) find_package (IniParser REQUIRED) @@ -60,7 +61,6 @@ endif() enable_testing() -add_subdirectory (tolua) add_subdirectory (tools) add_subdirectory (storage) add_subdirectory (clibs) diff --git a/cmake/Modules/FindToLua.cmake b/cmake/Modules/FindToLua.cmake new file mode 100644 index 000000000..92732981b --- /dev/null +++ b/cmake/Modules/FindToLua.cmake @@ -0,0 +1,73 @@ +# - Try to find the ToLua library +# Once done this will define +# +# TOLUA_FOUND - System has ToLua +# TOLUA_INCLUDE_DIR - The ToLua include directory +# TOLUA_LIBRARIES - The libraries needed to use ToLua +# TOLUA_DEFINITIONS - Compiler switches required for using ToLua +# TOLUA_EXECUTABLE - The ToLua command line shell +# TOLUA_VERSION_STRING - the version of ToLua found (since CMake 2.8.8) + +#============================================================================= +# Copyright 2006-2009 Kitware, Inc. +# Copyright 2006 Alexander Neundorf +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# use pkg-config to get the directories and then use these values +# in the find_path() and find_library() calls +find_package(PkgConfig QUIET) +PKG_CHECK_MODULES(PC_TOLUA QUIET ToLua) +set(TOLUA_DEFINITIONS ${PC_TOLUA_CFLAGS_OTHER}) + +find_path(TOLUA_INCLUDE_DIR NAMES tolua.h + HINTS + ${PC_TOLUA_DIR}/include + ${PC_TOLUA_INCLUDEDIR} + ${PC_TOLUA_INCLUDE_DIRS} + ) +find_library(TOLUA_LIBRARY NAMES tolua + HINTS + ${PC_TOLUA_DIR}/lib + ${PC_TOLUA_LIBDIR} + ${PC_TOLUA_LIBRARY_DIRS} + ) +find_program(TOLUA_EXECUTABLE tolua + HINTS + ${PC_TOLUA_DIR}/bin + ${PC_TOLUA_LIBDIR} + ${PC_TOLUA_LIBRARY_DIRS} +) + +SET( TOLUA_LIBRARIES "${TOLUA_LIBRARY}" CACHE STRING "ToLua Libraries") + +if(PC_TOLUA_VERSION) + set(TOLUA_VERSION_STRING ${PC_TOLUA_VERSION}) +elseif(TOLUA_INCLUDE_DIR AND EXISTS "${TOLUA_INCLUDE_DIR}/tolua.h") + file(STRINGS "${TOLUA_INCLUDE_DIR}/tolua.h" tolua_version_str + REGEX "^#define[\t ]+TOLUA_VERSION[\t ]+\".*\"") + + string(REGEX REPLACE "^#define[\t ]+TOLUA_VERSION[\t ]+\"tolua ([^\"]*)\".*" "\\1" + TOLUA_VERSION_STRING "${tolua_version_str}") + unset(tolua_version_str) +else(PC_TOLUA_VERSION) +message(ERROR "TOLUA_VERSION_STRING cannot be determined") +endif(PC_TOLUA_VERSION) + +# handle the QUIETLY and REQUIRED arguments and set TOLUA_FOUND to TRUE if +# all listed variables are TRUE +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(ToLua + REQUIRED_VARS TOLUA_LIBRARY TOLUA_INCLUDE_DIR TOLUA_EXECUTABLE + VERSION_VAR TOLUA_VERSION_STRING) + +mark_as_advanced(TOLUA_INCLUDE_DIR TOLUA_LIBRARIES TOLUA_EXECUTABLE) + diff --git a/tolua b/tolua deleted file mode 160000 index 7c73f64e0..000000000 --- a/tolua +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7c73f64e0a0001f49f04c51921e4c15167e4e67b From bd3e26d6344433e3ecc6573429472a4635986dea Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 16 Mar 2021 21:29:05 +0100 Subject: [PATCH 03/15] on macos, there is apparently no ncursesw --- CMakeLists.txt | 54 ++++++++++++++++++++++++++--------------------- tests/run-turn.sh | 2 +- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed4ef5d7b..1aa12d758 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.13) +cmake_minimum_required(VERSION 3.19) if (WIN32) file(TO_CMAKE_PATH "${CMAKE_MODULE_PATH}" CMAKE_MODULE_PATH ) @@ -13,17 +13,41 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") endif (WIN32) if (MSVC) -find_package (PDCurses) -set (CURSES_FOUND ${PDCURSES_FOUND}) -set (CURSES_LIBRARIES ${PDCURSES_LIBRARY}) -set (CURSES_INCLUDE_DIRS ${PDCURSES_INCLUDE_DIR}) + find_package (PDCurses) + set (CURSES_FOUND ${PDCURSES_FOUND}) + set (CURSES_LIBRARIES ${PDCURSES_LIBRARY}) + set (CURSES_INCLUDE_DIRS ${PDCURSES_INCLUDE_DIR}) +endif(MSVC) + +if (NOT CURSES_FOUND) + set(CURSES_NEED_WIDE TRUE) + set(CURSES_NEED_NCURSES TRUE) + find_package (Curses) + if (NOT CURSES_FOUND) + set(CURSES_NEED_WIDE FALSE) + find_package (Curses REQUIRED) + endif(NOT CURSES_FOUND) +endif (NOT CURSES_FOUND) + +find_package (ToLua 5.2 REQUIRED) +#find_package (BerkeleyDB REQUIRED) +find_package (SQLite3 REQUIRED) +find_package (IniParser REQUIRED) +find_package (CJSON REQUIRED) +find_package (EXPAT REQUIRED) + +find_package (Lua) +if (NOT LUA_FOUND) +find_package (Lua51 REQUIRED) +endif() + +if (MSVC) set (HAVE_STRDUP 0) set (HAVE_STRLCAT 0) set (HAVE_LIBBSD 0) set (HAVE_SIGNAL_H 0) set (HAVE_EXECINFO_H 0) else (MSVC) - include (CheckIncludeFile) check_include_file(signal.h HAVE_SIGNAL_H) check_include_file(execinfo.h HAVE_EXECINFO_H) @@ -41,24 +65,6 @@ if (HAVE_LIBBSD) endif (HAVE_LIBBSD) endif (MSVC) -if (NOT CURSES_FOUND) - set(CURSES_NEED_WIDE TRUE) - set(CURSES_NEED_NCURSES TRUE) - find_package (Curses) -endif (NOT CURSES_FOUND) - -find_package (ToLua 5.2 REQUIRED) -#find_package (BerkeleyDB REQUIRED) -find_package (SQLite3 REQUIRED) -find_package (IniParser REQUIRED) -find_package (CJSON REQUIRED) -find_package (EXPAT REQUIRED) - -find_package (Lua) -if (NOT LLUA_FOUND) -find_package (Lua51 REQUIRED) -endif() - enable_testing() add_subdirectory (tools) diff --git a/tests/run-turn.sh b/tests/run-turn.sh index ad2e0e6b8..7a3b83d8b 100755 --- a/tests/run-turn.sh +++ b/tests/run-turn.sh @@ -27,7 +27,7 @@ ROOT=`pwd` while [ ! -d $ROOT/.git ]; do ROOT=`dirname $ROOT` done - +export LUA_PATH="$ROOT/scripts/?.lua;$LUA_PATH" cd $ROOT/tests setup cleanup From ecafff5ffec55b7a268a0865aea8ba7f3cb5b78d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 16 Mar 2021 21:35:33 +0100 Subject: [PATCH 04/15] we do not need cmake 3.19 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aa12d758..9875c8ca4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.19) +cmake_minimum_required(VERSION 3.13) if (WIN32) file(TO_CMAKE_PATH "${CMAKE_MODULE_PATH}" CMAKE_MODULE_PATH ) From 72eec3e5e8268c4ab28fab783e61adccb0002f13 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Mar 2021 18:21:49 +0100 Subject: [PATCH 05/15] use new storage module version --- storage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage b/storage index da12b773a..97bba1317 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit da12b773a284b9c88848c14c4d578d047dca5f6b +Subproject commit 97bba1317f6a3aee7ce8ebc781c978bc58f75fe6 From 812a416217bafc26c2f0691a7070c673cdf492a9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Mar 2021 18:42:09 +0100 Subject: [PATCH 06/15] fix error message --- cmake/Modules/FindToLua.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/Modules/FindToLua.cmake b/cmake/Modules/FindToLua.cmake index 92732981b..cf70eff76 100644 --- a/cmake/Modules/FindToLua.cmake +++ b/cmake/Modules/FindToLua.cmake @@ -59,7 +59,7 @@ elseif(TOLUA_INCLUDE_DIR AND EXISTS "${TOLUA_INCLUDE_DIR}/tolua.h") TOLUA_VERSION_STRING "${tolua_version_str}") unset(tolua_version_str) else(PC_TOLUA_VERSION) -message(ERROR "TOLUA_VERSION_STRING cannot be determined") +message ("TOLUA_VERSION_STRING cannot be determined") endif(PC_TOLUA_VERSION) # handle the QUIETLY and REQUIRED arguments and set TOLUA_FOUND to TRUE if From 9a1765d917ac03c17e822032a711e0b60b17c184 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Mar 2021 18:35:25 +0100 Subject: [PATCH 07/15] terraforming changes herbs https://bugs.eressea.de/view.php?id=2730 --- src/kernel/region.c | 49 +++++++++++++++++++++++++-------------------- src/kernel/region.h | 2 +- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/kernel/region.c b/src/kernel/region.c index 6fade14fc..abe75fba3 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1102,6 +1102,31 @@ void init_region(region *r) } } +static void reset_herbs(region *r) { + const item_type *itype = NULL; + if (r->terrain->herbs) { + int len = 0; + while (r->terrain->herbs[len]) + ++len; + if (len) + itype = r->terrain->herbs[rng_int() % len]; + } + if (itype != NULL) { + rsetherbtype(r, itype); + rsetherbs(r, 50 + rng_int() % 31); + } + else { + rsetherbtype(r, NULL); + } + + if (rng_int() % 100 < 3) { + fset(r, RF_MALLORN); + } + else { + freset(r, RF_MALLORN); + } +} + /* Resourcen loeschen, die im aktuellen terrain nicht (mehr) vorkommen koennen */ static void reset_rawmaterials(region *r) { const terrain_type * terrain = r->terrain; @@ -1208,28 +1233,7 @@ static void create_land(region *r) { nb = NULL; } - const item_type *itype = NULL; - if (r->terrain->herbs) { - int len = 0; - while (r->terrain->herbs[len]) - ++len; - if (len) - itype = r->terrain->herbs[rng_int() % len]; - } - if (itype != NULL) { - rsetherbtype(r, itype); - rsetherbs(r, 50 + rng_int() % 31); - } - else { - rsetherbtype(r, NULL); - } - - if (rng_int() % 100 < 3) { - fset(r, RF_MALLORN); - } - else { - freset(r, RF_MALLORN); - } + reset_herbs(r); } void terraform_region(region * r, const terrain_type * terrain) @@ -1238,6 +1242,7 @@ void terraform_region(region * r, const terrain_type * terrain) assert(terrain); r->terrain = terrain; + reset_herbs(r); reset_rawmaterials(r); terraform_resources(r); diff --git a/src/kernel/region.h b/src/kernel/region.h index 9a7563712..30cee9029 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -188,7 +188,7 @@ extern "C" { #define rbuildings(r) ((r)->buildings) -#define rherbtype(r) ((r)->land?(r)->land->herbtype:0) +#define rherbtype(r) ((r)->land?(r)->land->herbtype:NULL) bool r_isforest(const struct region *r); From bc1a9b27ef66e30c6c64489acab1c4aa767b41b5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Mar 2021 20:47:05 +0100 Subject: [PATCH 08/15] https://bugs.eressea.de/view.php?id=2732 Einige Untotenrassen konnten nicht desertieren. --- res/e3a/races.xml | 6 +++--- res/eressea/races.xml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/res/e3a/races.xml b/res/e3a/races.xml index d039b067d..25d0248b2 100644 --- a/res/e3a/races.xml +++ b/res/e3a/races.xml @@ -732,7 +732,7 @@ - + @@ -750,7 +750,7 @@ - + @@ -783,7 +783,7 @@ - + diff --git a/res/eressea/races.xml b/res/eressea/races.xml index ac182a2f8..e994cbb0a 100644 --- a/res/eressea/races.xml +++ b/res/eressea/races.xml @@ -982,7 +982,7 @@ giveunit="yes" getitem="yes" recruitethereal="yes" equipment="yes"> - + @@ -999,7 +999,7 @@ giveunit="yes" getitem="yes" recruitethereal="yes" equipment="yes"> - + @@ -1030,7 +1030,7 @@ giveunit="yes" getitem="yes" recruitethereal="yes" equipment="yes"> - + From d7b0da327b11ef57f803259557cba1d1566f2ba8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Mar 2021 21:00:19 +0100 Subject: [PATCH 09/15] fix recent change for terraforming herbs --- src/kernel/region.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/kernel/region.c b/src/kernel/region.c index abe75fba3..723c4c438 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1242,7 +1242,6 @@ void terraform_region(region * r, const terrain_type * terrain) assert(terrain); r->terrain = terrain; - reset_herbs(r); reset_rawmaterials(r); terraform_resources(r); @@ -1265,6 +1264,9 @@ void terraform_region(region * r, const terrain_type * terrain) r->land = calloc(1, sizeof(land_region)); create_land(r); } + else { + reset_herbs(r); + } init_region(r); } From 7fcecc9468e70afd7a66fbc52b4af2256edadb3e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 18 Mar 2021 21:02:22 +0100 Subject: [PATCH 10/15] https://bugs.eressea.de/view.php?id=2732 fix permanent smurfs --- src/items.c | 27 ++++++++++++--------------- src/kernel/save.c | 14 +++++++++++--- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/items.c b/src/items.c index 7912ff350..27185c0b4 100644 --- a/src/items.c +++ b/src/items.c @@ -287,22 +287,19 @@ struct order *ord) change_effect(u, itype, 100 * amount); } else { - const race *irace = u_irace(u); - if (irace == u_race(u)) { - const race *rcfailure = rc_find("smurf"); - if (!rcfailure) { - rcfailure = rc_find("toad"); - } - if (rcfailure) { - trigger *trestore = trigger_changerace(u, u_race(u), irace); - if (trestore) { - int duration = 2 + rng_int() % 8; + const race *rcfailure = rc_find("smurf"); + if (!rcfailure) { + rcfailure = rc_find("toad"); + } + if (rcfailure) { + trigger *trestore = trigger_changerace(u, u_race(u), u->irace); + if (trestore) { + int duration = 2 + rng_int() % 8; - add_trigger(&u->attribs, "timer", trigger_timeout(duration, - trestore)); - u->irace = NULL; - u_setrace(u, rcfailure); - } + add_trigger(&u->attribs, "timer", trigger_timeout(duration, + trestore)); + u->irace = NULL; + u_setrace(u, rcfailure); } } } diff --git a/src/kernel/save.c b/src/kernel/save.c index 0006bd6ac..5bb7eebf8 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -383,11 +383,12 @@ unit *read_unit(gamedata *data) char obuf[DISPLAYSIZE]; faction *f; char rname[32]; - static const struct race * rc_demon; + static const struct race *rc_demon, *rc_smurf; static int config; if (rc_changed(&config)) { rc_demon = get_race(RC_DAEMON); + rc_smurf = rc_find("smurf"); } READ_INT(data->store, &n); @@ -529,11 +530,18 @@ unit *read_unit(gamedata *data) } read_attribs(data, &u->attribs, u); if (rc_demon) { - if (u_race(u) == rc_demon) { + const struct race *rc = u_race(u); + if (rc == rc_smurf) { + if (!u->attribs) { + log_error("%s was a %s in a %s faction", unitname(u), rc->_name, u->faction->race->_name); + u->_race = u->faction->race; + } + } + if (rc == rc_demon) { if (data->version < FIX_SHAPESHIFT_VERSION) { const char* zRace = get_racename(u->attribs); if (zRace) { - const struct race* rc = rc_find(zRace); + rc = rc_find(zRace); if (rc) { set_racename(&u->attribs, NULL); u->irace = rc; From fb52bc8846675d571981de0e3fe7dbcaaa9161af Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 19 Mar 2021 20:32:28 +0100 Subject: [PATCH 11/15] clarification: at_dict is deprecated but has a conversion. --- src/attributes/attributes.c | 2 -- src/helpers.c | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/attributes/attributes.c b/src/attributes/attributes.c index d8fe35833..00aca5757 100644 --- a/src/attributes/attributes.c +++ b/src/attributes/attributes.c @@ -14,7 +14,6 @@ #include "stealth.h" #include "magic.h" #include "movement.h" -#include "dict.h" #include "otherfaction.h" #include "overrideroads.h" #include "racename.h" @@ -198,7 +197,6 @@ void register_attributes(void) at_deprecate("gm", a_readint); at_deprecate("guard", a_readint); /* used to contain guard-flags (v3.10.0-259-g8597e8b) */ at_register(&at_stealth); - at_register(&at_dict); at_register(&at_unitdissolve); at_register(&at_observer); at_register(&at_overrideroads); diff --git a/src/helpers.c b/src/helpers.c index b6f76abf5..d90c2dae2 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -2,6 +2,8 @@ #include #endif +#include "attributes/dict.h" + #include "helpers.h" #include "vortex.h" #include "alchemy.h" @@ -358,6 +360,7 @@ void register_tolua_helpers(void) { tt_register(&tt_caldera); at_register(&at_direction); + at_register(&at_dict); /* deprecated, but has an upgrade path */ at_deprecate("lcbuilding", building_action_read); callbacks.equip_unit = lua_equipunit; From d06262e9a2cd7ccc71fd3e9091cd8cfe2fae8538 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 19 Mar 2021 23:30:34 +0100 Subject: [PATCH 12/15] indentation --- scripts/run-turn.lua | 150 +++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/scripts/run-turn.lua b/scripts/run-turn.lua index c4d1c5a63..5e66fbc36 100644 --- a/scripts/run-turn.lua +++ b/scripts/run-turn.lua @@ -16,15 +16,15 @@ function open_game(turn) return eressea.read_game(file .. ".dat") end -function callbacks(rules, name, ...) - for k, v in pairs(rules) do - if 'table' == type(v) then - cb = v[name] - if 'function' == type(cb) then - cb(...) - end - end - end +local function callbacks(rules, name, ...) + for k, v in pairs(rules) do + if 'table' == type(v) then + cb = v[name] + if 'function' == type(cb) then + cb(...) + end + end + end end local function write_emails(locales) @@ -49,8 +49,8 @@ local function write_emails(locales) end local function join_path(a, b) - if a then return a .. '/' .. b end - return b + if a then return a .. '/' .. b end + return b end local function write_addresses() @@ -85,23 +85,23 @@ local function write_aliases() end local function write_htpasswd() - local out = io.open(join_path(config.basepath, "htpasswd"), "w") - if out then - for f in factions() do - if f.password then - out:write(itoa36(f.id) .. ":" .. f.password .. "\n") - end - end - out:close() + local out = io.open(join_path(config.basepath, "htpasswd"), "w") + if out then + for f in factions() do + if f.password then + out:write(itoa36(f.id) .. ":" .. f.password .. "\n") + end end + out:close() + end end local function write_files(locales) - write_reports() - write_summary() - write_database() - write_passwords() - write_htpasswd() + write_reports() + write_summary() + write_database() + write_passwords() + write_htpasswd() end local function write_scores() @@ -123,69 +123,69 @@ local function write_scores() end function process(rules, orders) - local confirmed_multis = { } - local suspected_multis = { } + local confirmed_multis = { } + local suspected_multis = { } - if open_game(get_turn())~=0 then - eressea.log.error("could not read game") - return -1 - end + if open_game(get_turn())~=0 then + eressea.log.error("could not read game") + return -1 + end - turn_begin() - -- create orders for monsters: - plan_monsters() - -- read orders for players: - if eressea.read_orders(orders) ~= 0 then - print("could not read " .. orders) - return -1 - end + turn_begin() + -- create orders for monsters: + plan_monsters() + -- read orders for players: + if eressea.read_orders(orders) ~= 0 then + print("could not read " .. orders) + return -1 + end - if nmr_check(config.maxnmrs or 80)~=0 then - return -1 - end - callbacks(rules, 'init') - - -- run the turn: - turn_process() - callbacks(rules, 'update') - turn_end() -- ageing, etc. + if nmr_check(config.maxnmrs or 80)~=0 then + return -1 + end + callbacks(rules, 'init') + + -- run the turn: + turn_process() + callbacks(rules, 'update') + turn_end() -- ageing, etc. - write_files(config.locales) - update_scores() + write_files(config.locales) + update_scores() - file = '' .. get_turn() .. '.dat' - if eressea.write_game(file)~=0 then - eressea.log.error("could not write game") - return -1 - end - return 0 + file = '' .. get_turn() .. '.dat' + if eressea.write_game(file)~=0 then + eressea.log.error("could not write game") + return -1 + end + return 0 end function run_turn(rules) - local turn = get_turn() - if turn<0 then - turn = read_turn() - set_turn(turn) - end + local turn = get_turn() + if turn<0 then + turn = read_turn() + set_turn(turn) + end - orderfile = orderfile or config.basepath .. '/orders.' .. turn - eressea.log.debug("executing turn " .. get_turn() .. " with " .. orderfile .. " with rules=" .. config.rules) - local result = process(rules, orderfile) - return result + orderfile = orderfile or config.basepath .. '/orders.' .. turn + eressea.log.debug("executing turn " .. get_turn() .. " with " .. orderfile .. " with rules=" .. config.rules) + local result = process(rules, orderfile) + return result end function file_exists(name) - local f=io.open(name,"r") - if not f then - return false - end - io.close(f) - return true + local f=io.open(name,"r") + if not f then + return false + end + io.close(f) + return true end if file_exists('execute.lock') then - eressea.log.error("Lockfile exists, aborting.") - assert(false) + eressea.log.error("Lockfile exists, aborting.") + assert(false) end math.randomseed(rng.random()) @@ -195,9 +195,9 @@ require 'eressea.xmlconf' -- read xml data local rules = {} if config.rules then - rules = require('eressea.' .. config.rules) - eressea.log.info('loaded ' .. #rules .. ' modules for ' .. config.rules) + rules = require('eressea.' .. config.rules) + eressea.log.info('loaded ' .. #rules .. ' modules for ' .. config.rules) else - eressea.log.warning('no rule modules loaded, specify a game in eressea.ini or with -r') + eressea.log.warning('no rule modules loaded, specify a game in eressea.ini or with -r') end run_turn(rules) From c0cd90bf62d5f921230e9b71c9c091c4bdddb275 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 20 Mar 2021 10:02:38 +0100 Subject: [PATCH 13/15] eliminate type conversion warning --- clibs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clibs b/clibs index 8c3ce73e8..61a19671c 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 8c3ce73e861c952d04a28949b4a80e68c2cce8fd +Subproject commit 61a19671c25184ffece2a2668c37e5b69e9f16a7 From 76e99c6957d19506a1ed5f455ba47dce20a01893 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Mar 2021 18:09:21 +0100 Subject: [PATCH 14/15] eliminate some type conversion warnings (MSVC) --- src/battle.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/battle.c b/src/battle.c index a7c97fc6b..b93ecb12f 100644 --- a/src/battle.c +++ b/src/battle.c @@ -111,7 +111,7 @@ const troop no_troop = { 0, 0 }; static int max_turns; static int rule_damage; static int rule_loot; -static float loot_divisor; +static double loot_divisor; static int flee_chance_max_percent; static int flee_chance_base; static int flee_chance_skill_bonus; @@ -3167,7 +3167,8 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack) int dwp[WMAX]; int wcount[WMAX]; int wused[WMAX]; - int oi = 0, di = 0, w = 0; + int oi = 0, di = 0; + unsigned int w = 0; for (itm = u->items; itm && w != WMAX; itm = itm->next) { const weapon_type *wtype = resource2weapon(itm->type->rtype); if (wtype == NULL || itm->number == 0) @@ -3182,9 +3183,9 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack) } assert(w != WMAX); } - assert(w >= 0); - fig->weapons = (weapon *)calloc((size_t)(w + 1), sizeof(weapon)); - memcpy(fig->weapons, weapons, (size_t)w * sizeof(weapon)); + fig->weapons = malloc((1 + w) * sizeof(weapon)); + memcpy(fig->weapons, weapons, w * sizeof(weapon)); + fig->weapons[w].type = NULL; for (i = 0; i != w; ++i) { int j, o = 0, d = 0; From dea614990218d6f10a6500375d568ef125b6480d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Mar 2021 18:35:08 +0100 Subject: [PATCH 15/15] fix review comments, extract meffect_protection and add a test. --- src/battle.c | 47 ++++++++++++++++++++++++++++------------------- src/battle.h | 1 + src/battle.test.c | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/battle.c b/src/battle.c index b93ecb12f..b13c64b0d 100644 --- a/src/battle.c +++ b/src/battle.c @@ -778,7 +778,28 @@ int select_magicarmor(troop t) return ma; } -/* Sind side ds und Magier des meffect verbuendet, dann return 1*/ +int meffect_apply(struct meffect *me, int damage) { + assert(0 <= damage); /* damage sollte hier immer mindestens 0 sein */ + /* jeder Schaden wird um effect% reduziert bis der Schild duration + * Trefferpunkte aufgefangen hat */ + if (me->typ == SHIELD_REDUCE && me->effect <= 100) { + int hp = damage * me->effect / 100; + if (hp > me->duration) { + hp = me->duration; + } + damage -= hp; + me->duration -= hp; + } + /* gibt Ruestung +effect fuer duration Treffer */ + else if (me->typ == SHIELD_ARMOR) { + damage -= me->effect; + if (damage < 0) damage = 0; + me->duration--; + } + return damage; +} + +/* Sind side ds und Magier des meffect verbuendet? */ bool meffect_protection(battle * b, meffect * s, side * ds) { UNUSED_ARG(b); @@ -793,7 +814,7 @@ bool meffect_protection(battle * b, meffect * s, side * ds) return false; } -/* Sind side as und Magier des meffect verfeindet, dann return 1*/ +/* Sind side as und Magier des meffect verfeindet? */ bool meffect_blocked(battle * b, meffect * s, side * as) { UNUSED_ARG(b); @@ -1219,37 +1240,25 @@ static int apply_race_resistance(int reduced_damage, fighter *df, return reduced_damage; } -static int apply_magicshield(int reduced_damage, fighter *df, +static int apply_magicshield(int damage, fighter *df, const weapon_type *awtype, battle *b, bool magic) { side *ds = df->side; selist *ql; int qi; - if (reduced_damage <= 0) + if (damage <= 0) { return 0; + } /* Schilde */ for (qi = 0, ql = b->meffects; ql; selist_advance(&ql, &qi, 1)) { meffect *me = (meffect *)selist_get(ql, qi); if (meffect_protection(b, me, ds) != 0) { - assert(0 <= reduced_damage); /* rda sollte hier immer mindestens 0 sein */ - /* jeder Schaden wird um effect% reduziert bis der Schild duration - * Trefferpunkte aufgefangen hat */ - if (me->typ == SHIELD_REDUCE) { - int hp = reduced_damage * me->effect / 100; - reduced_damage -= hp; - me->duration -= hp; - } - /* gibt Ruestung +effect fuer duration Treffer */ - if (me->typ == SHIELD_ARMOR) { - reduced_damage -= me->effect; - if (reduced_damage < 0) reduced_damage = 0; - me->duration--; - } + damage = meffect_apply(me, damage); } } - return reduced_damage; + return damage; } bool diff --git a/src/battle.h b/src/battle.h index 83bcaf09a..da6fea9a9 100644 --- a/src/battle.h +++ b/src/battle.h @@ -254,6 +254,7 @@ extern "C" { void battle_message_faction(struct battle * b, struct faction * f, struct message *m); double tactics_chance(const struct unit *u, int skilldiff); + int meffect_apply(struct meffect *me, int damage); #ifdef __cplusplus } #endif diff --git a/src/battle.test.c b/src/battle.test.c index 665aeff1e..10bcae721 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -448,6 +448,30 @@ static void test_calculate_armor(CuTest * tc) test_teardown(); } +static void test_spells_reduce_damage(CuTest *tc) +{ + struct meffect me; + + me.typ = SHIELD_ARMOR; + me.duration = 10; + me.effect = 5; + CuAssertIntEquals(tc, 5, meffect_apply(&me, 10)); + CuAssertIntEquals(tc, 9, me.duration); + CuAssertIntEquals(tc, 5, me.effect); + CuAssertIntEquals(tc, 0, meffect_apply(&me, 1)); + CuAssertIntEquals(tc, 8, me.duration); + CuAssertIntEquals(tc, 5, me.effect); + + me.typ = SHIELD_REDUCE; + me.duration = 10; + me.effect = 50; + CuAssertIntEquals(tc, 5, meffect_apply(&me, 10)); + CuAssertIntEquals(tc, 5, me.duration); + CuAssertIntEquals(tc, 50, me.effect); + CuAssertIntEquals(tc, 7, meffect_apply(&me, 12)); + CuAssertIntEquals(tc, 0, me.duration); +} + static void test_magic_resistance(CuTest *tc) { troop dt; @@ -898,6 +922,7 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_calculate_armor); SUITE_ADD_TEST(suite, test_natural_armor); SUITE_ADD_TEST(suite, test_magic_resistance); + SUITE_ADD_TEST(suite, test_spells_reduce_damage); SUITE_ADD_TEST(suite, test_projectile_armor); SUITE_ADD_TEST(suite, test_tactics_chance); SUITE_ADD_TEST(suite, test_terminate);