From 9d8cfc422c3e08f2d162edda233701dbd5629007 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 31 Dec 2017 21:33:31 +0100 Subject: [PATCH 1/8] fix cmake detection of libbsd --- .travis.yml | 1 + CMakeLists.txt | 14 +++++++++++--- src/CMakeLists.txt | 12 +++++++++++- src/util/strings.c | 16 +++++++++++++--- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 325d573ad..62e60194f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ script: s/travis-build addons: apt: packages: + - libbsd-dev - liblua5.1-dev - libtolua-dev - libncurses5-dev diff --git a/CMakeLists.txt b/CMakeLists.txt index 7507bd99f..683757de0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,9 +11,17 @@ if (MSVC) include(MSVC) endif (MSVC) -INCLUDE (CheckSymbolExists) -CHECK_SYMBOL_EXISTS(strlcat string.h HAVE_STRLCAT) -CHECK_SYMBOL_EXISTS(strdup string.h HAVE_STRDUP) +INCLUDE (CheckIncludeFile) +CHECK_INCLUDE_FILE(bsd/string.h HAVE_LIBBSD) + +INCLUDE (CheckFunctionExists) +CHECK_FUNCTION_EXISTS(strdup HAVE_STRDUP) +IF (HAVE_LIBBSD) +INCLUDE (CheckLibraryExists) +CHECK_LIBRARY_EXISTS(bsd strlcat "bsd/string.h" HAVE_STRLCAT) +ELSE (HAVE_LIBBSD) +CHECK_FUNCTION_EXISTS(strlcat HAVE_STRLCAT) +ENDIF(HAVE_LIBBSD) find_package (SQLite3) find_package (BerkeleyDB) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 70ef60b87..4708caf6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -267,14 +267,24 @@ add_test(server test_eressea) install(TARGETS eressea DESTINATION "bin") +if (HAVE_LIBBSD) + add_definitions(-DHAVE_LIBBSD) +endif (HAVE_LIBBSD) + if (HAVE_STRLCAT) add_definitions(-DHAVE_BSDSTRING) -endif(HAVE_STRLCAT) +endif (HAVE_STRLCAT) if (HAVE_STRDUP) add_definitions(-DHAVE_STRDUP) endif(HAVE_STRDUP) +if (HAVE_LIBBSD) +target_link_libraries(test_eressea bsd) +target_link_libraries(eressea bsd) +target_link_libraries(convert bsd) +endif (HAVE_LIBBSD) + if (DB_FOUND) include_directories (${DB_INCLUDE_DIR}) target_link_libraries(convert ${DB_LIBRARIES}) diff --git a/src/util/strings.c b/src/util/strings.c index d3a1b2eec..b33f657ef 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -25,10 +25,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* libc includes */ #include #include -#include #include #include +#ifdef HAVE_LIBBSD +#include +#else +#include +#endif + size_t str_strlcpy(char *dst, const char *src, size_t len) { #ifdef HAVE_BSDSTRING @@ -232,10 +237,15 @@ unsigned int wang_hash(unsigned int a) } char *str_strdup(const char *s) { -#ifdef _MSC_VER +#ifdef HAVE_STRDUP + return strdup(s); +#elif defined(_MSC_VER) return _strdup(s); #else - return strdup(s); + size_t len = strlen(s); + char *dup = malloc(len+1); + memcpy(dup, s, len+1); + return dup; #endif } From 1575a599217b3e8487e490337bdbf5c57d5cce4d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 00:43:08 +0100 Subject: [PATCH 2/8] fix cmake builds on windows. --- CMakeLists.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 683757de0..23ef9c5a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,19 @@ cmake_minimum_required(VERSION 2.8) -project (eressea-server C) -set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH}) if (WIN32) FILE(TO_CMAKE_PATH "${CMAKE_MODULE_PATH}" CMAKE_MODULE_PATH ) FILE(TO_CMAKE_PATH "${CMAKE_PREFIX_PATH}" CMAKE_PREFIX_PATH ) endif(WIN32) +project (eressea-server C) +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH}) + if (MSVC) include(MSVC) -endif (MSVC) +set (HAVE_STRDUP 0) +set (HAVE_STRLCAT 0) +set (HAVE_LIBBSD 0) +else (MSVC) INCLUDE (CheckIncludeFile) CHECK_INCLUDE_FILE(bsd/string.h HAVE_LIBBSD) @@ -23,6 +27,8 @@ ELSE (HAVE_LIBBSD) CHECK_FUNCTION_EXISTS(strlcat HAVE_STRLCAT) ENDIF(HAVE_LIBBSD) +endif (MSVC) + find_package (SQLite3) find_package (BerkeleyDB) find_package (Curses) From 455a929d9e14804a86def50c5f655cb98e210b78 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 01:06:14 +0100 Subject: [PATCH 3/8] help cmake find pdcurses on windows. --- CMakeLists.txt | 12 ++++++++-- cmake/Modules/FindPDCurses.cmake | 38 ++++++++++++++++++++++++++++++++ vs2015-build.bat | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 cmake/Modules/FindPDCurses.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 23ef9c5a4..698d3a4e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,10 +29,18 @@ ENDIF(HAVE_LIBBSD) endif (MSVC) +if (MSVC) +find_package (PDCurses) +SET(CURSES_FOUND ${PDCURSES_FOUND}) +SET(CURSES_LIBRARIES ${PDCURSES_LIBRARY}) +SET(CURSES_INCLUDE_DIR ${PDCURSES_INCLUDE_DIR}) +else (MSVC) +find_package (Curses) +endif (MSVC) + find_package (SQLite3) find_package (BerkeleyDB) -find_package (Curses) -find_package (LibXml2) +find_package (LibXml2 REQUIRED) find_package (ToLua REQUIRED) if (TOLUA_FOUND) if (${TOLUA_VERSION_STRING} VERSION_GREATER "5.2") diff --git a/cmake/Modules/FindPDCurses.cmake b/cmake/Modules/FindPDCurses.cmake new file mode 100644 index 000000000..1e326f4fd --- /dev/null +++ b/cmake/Modules/FindPDCurses.cmake @@ -0,0 +1,38 @@ +# Locate PDCurses library +# This module defines +# PDCURSES_LIBRARIES, the name of the library to link against +# PDCURSES_FOUND, if false, do not try to link to PDCurses +# PDCURSES_INCLUDE_DIR, where to find curses.h + +FIND_PATH(PDCURSES_INCLUDE_DIR curses.h + HINTS + $ENV{PDCURSESDIR} + PATH_SUFFIXES include/pdcurses include) + +FIND_LIBRARY(PDCURSES_LIBRARY + NAMES pdcurses + HINTS + $ENV{PDCURSESDIR} + PATH_SUFFIXES lib64 lib) + +FIND_LIBRARY(PDCURSES_PANEL_LIBRARY + NAMES panel + HINTS + $ENV{PDCURSESDIR} + PATH_SUFFIXES lib64 lib) + +IF(PDCURSES_LIBRARY) + SET(PDCURSES_LIBRARIES ${PDCURSES_LIBRARY}) + IF(PDCURSES_PANEL_LIBRARY) + SET(PDCURSES_LIBRARIES ${PDCURSES_LIBRARIES} ${PDCURSES_PANEL_LIBRARY}) + ENDIF(PDCURSES_PANEL_LIBRARY) +ENDIF(PDCURSES_LIBRARY) + +SET(PDCURSES_FOUND "NO") +IF(PDCURSES_INCLUDE_DIR AND PDCURSES_LIBRARY) +# message(STATUS "Found PDCurses library: ${PDCURSES_LIBRARIES}") + # Set the final string here so the GUI reflects the final state. + SET(PDCURSES_LIBRARIES PDCURSES_LIBRARY} CACHE STRING "Where the PDCurses Library can be found") + + SET(PDCURSES_FOUND "YES") +ENDIF(PDCURSES_INCLUDE_DIR AND PDCURSES_LIBRARY) diff --git a/vs2015-build.bat b/vs2015-build.bat index 262d498bf..1ad2c8da2 100644 --- a/vs2015-build.bat +++ b/vs2015-build.bat @@ -9,5 +9,5 @@ IF exist build-vs%VSVERSION% goto HAVEDIR mkdir build-vs%VSVERSION% :HAVEDIR cd build-vs%VSVERSION% -"%ProgramFiles%\CMake\bin\cmake.exe" -G "Visual Studio %VSVERSION%" -DCMAKE_PREFIX_PATH="%ProgramFiles(x86)%/Lua/5.1;%ERESSEA%/dependencies-win32" -DCMAKE_MODULE_PATH="%SRCDIR%/cmake/Modules" -DCMAKE_SUPPRESS_REGENERATION=TRUE .. +"%ProgramFiles%\CMake\bin\cmake.exe" -G "Visual Studio %VSVERSION%" -DCMAKE_PREFIX_PATH="%ProgramFiles(x86)%\Lua\5.1;%ERESSEA%\dependencies-win32" -DCMAKE_SUPPRESS_REGENERATION=TRUE .. PAUSE From 396e0f6bd770e275142fd4d04df2dbda6c6d9c7c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 01:17:16 +0100 Subject: [PATCH 4/8] remove bsdstring from battle. --- src/battle.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/battle.c b/src/battle.c index da84b2763..fa4038b14 100644 --- a/src/battle.c +++ b/src/battle.c @@ -64,7 +64,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include #include #include @@ -3517,10 +3516,10 @@ static int battle_report(battle * b) for (bf = b->factions; bf; bf = bf->next) { faction *fac = bf->faction; char buf[32 * MAXSIDES]; - char *bufp = buf; - size_t size = sizeof(buf) - 1; message *m; + sbstring sbs; + sbs_init(&sbs, buf, sizeof(buf)); battle_message_faction(b, fac, msg_separator); if (cont) @@ -3540,24 +3539,22 @@ static int battle_report(battle * b) char buffer[32]; if (komma) { - bufp = STRLCPY(bufp, ", ", size); + sbs_strcat(&sbs, ", "); } snprintf(buffer, sizeof(buffer), "%s %2d(%s): ", loc_army, army_index(s), abbrev); - - bufp = STRLCPY(bufp, buffer, size); + sbs_strcat(&sbs, buffer); for (r = FIGHT_ROW; r != NUMROWS; ++r) { if (alive[r]) { if (l != FIGHT_ROW) { - bufp = STRLCPY(bufp, "+", size); + sbs_strcat(&sbs, "+"); } while (k--) { - bufp = STRLCPY(bufp, "0+", size); + sbs_strcat(&sbs, "0+"); } sprintf(buffer, "%d", alive[r]); - - bufp = STRLCPY(bufp, buffer, size); + sbs_strcat(&sbs, buffer); k = 0; l = r + 1; @@ -3569,7 +3566,6 @@ static int battle_report(battle * b) komma = true; } } - *bufp = 0; fbattlerecord(b, fac, buf); } return cont; From 0022f48c39f671452d685a7a6affd0e94d3b0292 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 06:56:57 +0100 Subject: [PATCH 5/8] do not use MIN/MAX --- src/kernel/curse.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/kernel/curse.c b/src/kernel/curse.c index e83323ab5..93df39f49 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -16,7 +16,9 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. **/ +#ifdef _MSC_VER #include +#endif #include #include "curse.h" @@ -324,7 +326,8 @@ const curse_type *ct_find(const char *c) return type; } else { - size_t k = MIN(c_len, strlen(type->cname)); + size_t k = strlen(type->cname); + if (k > c_len) k = c_len; if (!memcmp(c, type->cname, k)) { return type; } @@ -483,7 +486,7 @@ int get_cursedmen(const unit * u, const curse * c) cursedmen = c->data.i; } - return MIN(u->number, cursedmen); + return (u->number < cursedmen) ? u->number : cursedmen; } /* setzt die Anzahl der betroffenen Personen auf cursedmen */ @@ -572,19 +575,19 @@ curse *create_curse(unit * magician, attrib ** ap, const curse_type * ct, /* es gibt schon eins diese Typs */ if (c && ct->mergeflags != NO_MERGE) { if (ct->mergeflags & M_DURATION) { - c->duration = MAX(c->duration, duration); + if (c->duration < duration) c->duration = duration; } else if (ct->mergeflags & M_SUMDURATION) { c->duration += duration; } if (ct->mergeflags & M_MAXEFFECT) { - c->effect = MAX(c->effect, effect); + if (c->effect < effect) c->effect = effect; } else if (ct->mergeflags & M_SUMEFFECT) { c->effect += effect; } if (ct->mergeflags & M_VIGOUR) { - c->vigour = MAX(vigour, c->vigour); + if (c->vigour < vigour) c->vigour = vigour; } else if (ct->mergeflags & M_VIGOUR_ADD) { c->vigour = vigour + c->vigour; From 77926d2d54c4ff7ebfd9ef00593a5b32510c79dd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 07:43:06 +0100 Subject: [PATCH 6/8] add soem debug output to intermittent test --- scripts/tests/e2/ships.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 973d70f8e..06fc78c09 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -58,6 +58,11 @@ function test_speedy_ship_slow() u2:set_skill("sailing", 24) -- sumskill = 50 u1:add_order("NACH O O O O O O O O O O") process_orders() + if 5 ~= u1.region.x then + for _, msg in ipairs(f.messages) do + print(msg) + end + end assert_equal(5, u1.region.x) end From 6a31b432ded52f471a8e57f8e7207cc404fc5a36 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 07:47:06 +0100 Subject: [PATCH 7/8] disable storms in tests --- scripts/tests/e2/ships.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 06fc78c09..3208f0a0d 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -8,6 +8,7 @@ function setup() eressea.settings.set("rules.ship.damage.nocrewocean", "0") eressea.settings.set("rules.ship.damage.nocrew", "0") eressea.settings.set("rules.ship.drifting", "0") + eressea.settings.set("rules.ship.storms", "0") end function test_ship_requires_skill() @@ -58,11 +59,6 @@ function test_speedy_ship_slow() u2:set_skill("sailing", 24) -- sumskill = 50 u1:add_order("NACH O O O O O O O O O O") process_orders() - if 5 ~= u1.region.x then - for _, msg in ipairs(f.messages) do - print(msg) - end - end assert_equal(5, u1.region.x) end From e4859e68c9708b882d46d2510214648a867c0e00 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 1 Jan 2018 08:23:52 +0100 Subject: [PATCH 8/8] do not use MIN and MAX. --- src/items.c | 4 +- src/kernel/connection.c | 8 ++- src/laws.c | 108 ++++++++++++++++++++++++---------------- src/lighthouse.c | 2 +- src/monsters.c | 2 +- src/move.c | 2 +- src/util/parser.c | 2 +- src/util/parser.h | 2 +- 8 files changed, 79 insertions(+), 51 deletions(-) diff --git a/src/items.c b/src/items.c index f4537d48b..c62b1240f 100644 --- a/src/items.c +++ b/src/items.c @@ -1,4 +1,6 @@ +#ifdef _MSC_VER #include +#endif #include "items.h" #include "alchemy.h" @@ -313,7 +315,7 @@ static int heal(unit * user, int effect) { int req = unit_max_hp(user) * user->number - user->hp; if (req > 0) { - req = MIN(req, effect); + if (req > effect) req = effect; effect -= req; user->hp += req; } diff --git a/src/kernel/connection.c b/src/kernel/connection.c index 9bcd6095a..516cb6ead 100644 --- a/src/kernel/connection.c +++ b/src/kernel/connection.c @@ -16,7 +16,9 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. **/ +#ifdef _MSC_VER #include +#endif #include #include "connection.h" @@ -134,7 +136,8 @@ static connection **get_borders_i(const region * r1, const region * r2) int key = reg_hashkey(r1); int k2 = reg_hashkey(r2); - key = MIN(k2, key) % BORDER_MAXHASH; + if (key > k2) key = k2; + key = key % BORDER_MAXHASH; bp = &borders[key]; while (*bp) { connection *b = *bp; @@ -515,9 +518,10 @@ static const char *b_nameroad(const connection * b, const region * r, } } else { - int percent = MAX(1, 100 * local / r->terrain->max_road); if (local) { const char *temp = LOC(f->locale, mkname("border", "a_road_percent")); + int percent = 100 * local / r->terrain->max_road; + if (percent < 1) percent = 1; str_replace(buffer, sizeof(buffer), temp, "$percent", itoa10(percent)); } else { diff --git a/src/laws.c b/src/laws.c index dd61c2572..bccefab20 100644 --- a/src/laws.c +++ b/src/laws.c @@ -17,7 +17,9 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. **/ +#ifdef _MSC_VER #include +#endif #include #include "laws.h" @@ -208,7 +210,8 @@ static void live(region * r) } /* bestes Talent raussuchen */ if (sb != NULL) { - int weeks = MIN(effect, u->number); + int weeks = u->number; + if (weeks > effect) weeks = effect; reduce_skill(u, sb, weeks); ADDMSG(&u->faction->msgs, msg_message("dumbeffect", "unit weeks skill", u, weeks, (skill_t)sb->id)); @@ -276,7 +279,7 @@ static void calculate_emigration(region * r) int max_emigration = MAX_EMIGRATION(rp2 - maxp2); if (max_emigration > 0) { - max_emigration = MIN(max_emigration, max_immigrants); + if (max_emigration > max_immigrants) max_emigration = max_immigrants; r->land->newpeasants += max_emigration; rc->land->newpeasants -= max_emigration; max_immigrants -= max_emigration; @@ -301,8 +304,8 @@ int peasant_luck_effect(int peasants, int luck, int maxp, double variance) int births = 0; double mean; if (luck == 0) return 0; - - mean = peasant_luck_factor() * peasant_growth_factor() * MIN(luck, peasants); + mean = fmin(luck, peasants); + mean *= peasant_luck_factor() * peasant_growth_factor(); mean *= ((peasants / (double)maxp < .9) ? 1 : PEASANTFORCE); births = RAND_ROUND(normalvariate(mean, variance * mean)); @@ -315,11 +318,11 @@ int peasant_luck_effect(int peasants, int luck, int maxp, double variance) static void peasants(region * r, int rule) { - int peasants = rpeasants(r); + int rp = rpeasants(r); int money = rmoney(r); int maxp = max_production(r); int n, satiated; - int dead = 0; + int dead = 0, peasants = rp; if (peasants > 0 && rule > 0) { int luck = 0; @@ -339,7 +342,8 @@ static void peasants(region * r, int rule) /* Alle werden satt, oder halt soviele für die es auch Geld gibt */ - satiated = MIN(peasants, money / maintenance_cost(NULL)); + satiated = money / maintenance_cost(NULL); + if (satiated > peasants) satiated = peasants; rsetmoney(r, money - satiated * maintenance_cost(NULL)); /* Von denjenigen, die nicht satt geworden sind, verhungert der @@ -348,7 +352,8 @@ static void peasants(region * r, int rule) /* Es verhungert maximal die unterernährten Bevölkerung. */ - n = MIN(peasants - satiated, rpeasants(r)); + n = peasants - satiated; + if (n > rp) n = rp; dead += (int)(0.5 + n * PEASANT_STARVATION_CHANCE); if (dead > 0) { @@ -429,13 +434,12 @@ static void horses(region * r) /* Logistisches Wachstum, Optimum bei halbem Maximalbesatz. */ maxhorses = region_maxworkers(r) / 10; - maxhorses = MAX(0, maxhorses); horses = rhorses(r); if (horses > 0) { if (is_cursed(r->attribs, &ct_godcursezone)) { rsethorses(r, (int)(horses * 0.9)); } - else if (maxhorses) { + else if (maxhorses > 0) { double growth = (RESOURCE_QUANTITY * HORSEGROWTH * 200 * (maxhorses - horses)) / maxhorses; @@ -464,7 +468,7 @@ static void horses(region * r) if (r2 && fval(r2->terrain, WALK_INTO)) { int pt = (rhorses(r) * HORSEMOVE) / 100; pt = (int)normalvariate(pt, pt / 4.0); - pt = MAX(0, pt); + if (pt < 0) pt = 0; if (fval(r2, RF_MIGRATION)) rsethorses(r2, rhorses(r2) + pt); else { @@ -571,13 +575,14 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) if (current_season == SEASON_SUMMER || current_season == SEASON_AUTUMN) { double seedchance = 0.01F * RESOURCE_QUANTITY; - int elves = count_race(r, get_race(RC_ELF)); + int mp, elves = count_race(r, get_race(RC_ELF)); direction_t d; a = a_find(r->attribs, &at_germs); if (a && last_weeks_season == SEASON_SPRING) { /* ungekeimte Samen bleiben erhalten, Sprößlinge wachsen */ - sprout = MIN(a->data.sa[1], rtrees(r, 1)); + sprout = rtrees(r, 1); + if (sprout > a->data.sa[1]) sprout = a->data.sa[1]; /* aus dem gesamt Sprößlingepool abziehen */ rsettrees(r, 1, rtrees(r, 1) - sprout); /* zu den Bäumen hinzufügen */ @@ -592,12 +597,14 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) return; } - if (max_production(r) <= 0) + mp = max_production(r); + if (mp <= 0) return; /* Grundchance 1.0% */ /* Jeder Elf in der Region erhöht die Chance marginal */ - elves = MIN(elves, max_production(r) / 8); + mp = mp / 8; + if (elves > mp) elves = mp; if (elves) { seedchance += 1.0 - pow(0.99999, elves * RESOURCE_QUANTITY); } @@ -664,7 +671,8 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) /* Raubbau abfangen, es dürfen nie mehr Samen wachsen, als aktuell * in der Region sind */ - seeds = MIN(a->data.sa[0], rtrees(r, 0)); + seeds = rtrees(r, 0); + if (seeds > a->data.sa[0]) seeds = a->data.sa[0]; sprout = 0; for (i = 0; i < seeds; i++) { @@ -684,7 +692,8 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) * der Region entfernt werden können, da Jungbäume in der gleichen * Runde nachwachsen, wir also nicht mehr zwischen diesjährigen und * 'alten' Jungbäumen unterscheiden könnten */ - sprout = MIN(a->data.sa[1], rtrees(r, 1)); + sprout = rtrees(r, 1); + if (sprout > a->data.sa[1]) sprout = a->data.sa[1]; grownup_trees = 0; for (i = 0; i < sprout; i++) { @@ -707,6 +716,7 @@ growing_herbs(region * r, const int current_season, const int last_weeks_season) * * Jedes Kraut hat eine Wahrscheinlichkeit von (100-(vorhandene * Kräuter))% sich zu vermehren. */ + UNUSED_ARG(last_weeks_season); if (current_season != SEASON_WINTER) { int i; for (i = rherbs(r); i > 0; i--) { @@ -724,7 +734,9 @@ void immigration(void) for (r = regions; r; r = r->next) { if (r->land && r->land->newpeasants) { int rp = rpeasants(r) + r->land->newpeasants; - rsetpeasants(r, MAX(0, rp)); + /* FIXME: kann ernsthaft abs(newpeasants) > rpeasants(r) sein? */ + if (rp < 0) rp = 0; + rsetpeasants(r, rp); } /* Genereate some (0-6 depending on the income) peasants out of nothing */ /* if less than 50 are in the region and there is space and no monster or demon units in the region */ @@ -830,7 +842,7 @@ void demographics(void) peasants(r, peasant_rules); if (r->age > 20) { - double mwp = MAX(region_maxworkers(r), 1); + double mwp = fmax(region_maxworkers(r), 1); double prob = pow(rpeasants(r) / (mwp * wage(r, NULL, NULL, turn) * 0.13), 4.0) * PLAGUE_CHANCE; @@ -905,7 +917,7 @@ static int slipthru(const region * r, const unit * u, const building * b) int can_contact(const region * r, const unit * u, const unit * u2) { /* hier geht es nur um die belagerung von burgen */ - + UNUSED_ARG(r); if (u->building == u2->building) { return 1; } @@ -994,6 +1006,7 @@ int quit_cmd(unit * u, struct order *ord) static bool mayenter(region * r, unit * u, building * b) { unit *u2; + UNUSED_ARG(r); if (fval(b, BLD_UNGUARDED)) return true; u2 = building_owner(b); @@ -1245,7 +1258,7 @@ static void nmr_death(faction * f) static void remove_idle_players(void) { faction **fp; - int timeout = NMRTimeout(); + int i, timeout = NMRTimeout(); log_info(" - beseitige Spieler, die sich zu lange nicht mehr gemeldet haben..."); @@ -1269,7 +1282,9 @@ static void remove_idle_players(void) } log_info(" - beseitige Spieler, die sich nach der Anmeldung nicht gemeldet haben..."); - age = calloc(MAX(4, turn + 1), sizeof(int)); + i = turn + 1; + if (i < 4) i = 4; + age = calloc(i, sizeof(int)); for (fp = &factions; *fp;) { faction *f = *fp; if (!is_monsters(f)) { @@ -2639,8 +2654,7 @@ int combatspell_cmd(unit * u, struct order *ord) /* Optional: STUFE n */ if (findparam(s, u->faction->locale) == P_LEVEL) { /* Merken, setzen kommt erst später */ - level = getint(); - level = MAX(0, level); + level = getuint(); s = gettoken(token, sizeof(token)); } @@ -2867,16 +2881,20 @@ static void age_stonecircle(building *b) { if (!c) { int sk = effskill(mage, SK_MAGIC, 0); float effect = 100; - /* the mage reactivates the circle */ - c = create_curse(mage, &rt->attribs, &ct_astralblock, - (float)MAX(1, sk), MAX(1, sk / 2), effect, 0); - ADDMSG(&r->msgs, - msg_message("astralshield_activate", "region unit", r, mage)); + if (sk > 0) { + int vig = sk; + int dur = (sk + 1) / 2; + /* the mage reactivates the circle */ + c = create_curse(mage, &rt->attribs, &ct_astralblock, + vig, dur, effect, 0); + ADDMSG(&r->msgs, + msg_message("astralshield_activate", "region unit", r, mage)); + } } else { int sk = effskill(mage, SK_MAGIC, 0); - c->duration = MAX(c->duration, sk / 2); - c->vigour = MAX(c->vigour, (float)sk); + if (c->duration < sk / 2) c->duration = sk / 2; + if (c->vigour < sk) c->vigour = sk; } } } @@ -2917,12 +2935,14 @@ static void ageing(void) /* Goliathwasser */ int i = get_effect(u, oldpotiontype[P_STRONG]); if (i > 0) { - change_effect(u, oldpotiontype[P_STRONG], -1 * MIN(u->number, i)); + if (i > u->number) i = u->number; + change_effect(u, oldpotiontype[P_STRONG], - i); } /* Berserkerblut */ i = get_effect(u, oldpotiontype[P_BERSERK]); if (i > 0) { - change_effect(u, oldpotiontype[P_BERSERK], -1 * MIN(u->number, i)); + if (i > u->number) i = u->number; + change_effect(u, oldpotiontype[P_BERSERK], - i); } if (u->attribs) { @@ -2992,13 +3012,14 @@ static int maxunits(const faction * f) { int flimit = rule_faction_limit(); int alimit = rule_alliance_limit(); + UNUSED_ARG(f); if (alimit == 0) { return flimit; } if (flimit == 0) { return alimit; } - return MIN(alimit, flimit); + return (alimit > flimit) ? flimit : alimit; } int checkunitnumber(const faction * f, int add) @@ -3306,7 +3327,7 @@ void monthly_healing(void) p *= u_heal_factor(u); if (u->hp < umhp) { - double maxheal = MAX(u->number, umhp / 20.0); + double maxheal = fmax(u->number, umhp / 20.0); int addhp; if (active_building(u, bt_find("inn"))) { p *= 1.5; @@ -3321,7 +3342,8 @@ void monthly_healing(void) ++addhp; /* Aufaddieren der geheilten HP. */ - u->hp = MIN(u->hp + addhp, umhp); + if (umhp > u->hp + addhp) umhp = u->hp + addhp; + u->hp = umhp; /* soll man an negativer regeneration sterben können? */ assert(u->hp > 0); @@ -3647,7 +3669,7 @@ int claim_cmd(unit * u, struct order *ord) if (itype) { item **iclaim = i_find(&u->faction->items, itype); if (iclaim && *iclaim) { - n = MIN(n, (*iclaim)->number); + if (n > (*iclaim)->number) n = (*iclaim)->number; i_change(iclaim, itype, -n); i_change(&u->items, itype, n); } @@ -3907,7 +3929,7 @@ int armedmen(const unit * u, bool siege_weapons) if (n >= u->number) break; } - n = MIN(n, u->number); + if (n > u->number) n = u->number; } } return n; @@ -3941,9 +3963,9 @@ int siege_cmd(unit * u, order * ord) rt_catapult = rt_find("catapult"); d = i_get(u->items, rt_catapult->itype); - d = MIN(u->number, d); + if (d > u->number) d = u->number; pooled = get_pooled(u, rt_catapultammo, GET_DEFAULT, d); - d = MIN(pooled, d); + if (d > pooled) d = pooled; if (effskill(u, SK_CATAPULT, 0) >= 1) { katapultiere = d; d *= effskill(u, SK_CATAPULT, 0); @@ -3970,11 +3992,11 @@ int siege_cmd(unit * u, order * ord) * einheiten wieder abgesucht werden muessen! */ usetsiege(u, b); - b->besieged += MAX(bewaffnete, katapultiere); + if (katapultiere < bewaffnete) katapultiere = bewaffnete; + b->besieged += katapultiere; /* definitiver schaden eingeschraenkt */ - - d = MIN(d, b->size - 1); + if (d > b->size - 1) d = b->size - 1; /* meldung, schaden anrichten */ if (d && !curse_active(get_curse(b->attribs, &ct_magicwalls))) { diff --git a/src/lighthouse.c b/src/lighthouse.c index ad0cd639a..fbf950fb1 100644 --- a/src/lighthouse.c +++ b/src/lighthouse.c @@ -72,7 +72,7 @@ int lighthouse_range(const building * b, const faction * f, const unit *u) int sk = effskill(u, SK_PERCEPTION, 0) / 3; assert(u->building == b); assert(u->faction == f); - maxd = MIN(maxd, sk); + if (maxd > sk) maxd = sk; } /* E3A rule: no perception req'd */ return maxd; diff --git a/src/monsters.c b/src/monsters.c index 9260f76ac..d1979414d 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -103,7 +103,7 @@ static void reduce_weight(unit * u) int horses = get_resource(u, get_resourcetype(R_HORSE)); if (horses > 0) { - horses = MIN(horses, (u->number * 2)); + if (horses > u->number * 2) horses = u->number * 2; change_resource(u, get_resourcetype(R_HORSE), -horses); } diff --git a/src/move.c b/src/move.c index afee7399f..069c6ff7d 100644 --- a/src/move.c +++ b/src/move.c @@ -2301,7 +2301,7 @@ int follow_ship(unit * u, order * ord) moves = 1; - speed = (int)getuint(); + speed = getuint(); if (speed == 0) { speed = shipspeed(u->ship, u); } diff --git a/src/util/parser.c b/src/util/parser.c index 5a322005f..1ba913c2c 100644 --- a/src/util/parser.c +++ b/src/util/parser.c @@ -246,7 +246,7 @@ int getint(void) return s ? atoi(s) : 0; } -unsigned int getuint(void) +int getuint(void) { int n = getint(); return (n < 0) ? 0 : n; diff --git a/src/util/parser.h b/src/util/parser.h index 52fdbc4f4..7242da1bb 100644 --- a/src/util/parser.h +++ b/src/util/parser.h @@ -28,7 +28,7 @@ extern "C" { bool parser_end(void); const char *getstrtoken(void); const char *gettoken(char *lbuf, size_t bufsize); - unsigned int getuint(void); + int getuint(void); int getint(void); int getid(void); unsigned int atoip(const char *s);