From 4619f77af9dd98e0be906c97857c6f7c816d95c4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Dec 2017 15:43:22 +0100 Subject: [PATCH 01/25] add a shellcheck test, fix some scripts --- .travis.yml | 1 + process/Makefile | 6 ++++++ process/compress.sh | 19 ++++++++++--------- process/run-turn | 12 ++++++------ process/send-bz2-report | 9 +++++---- process/send-zip-report | 17 +++++++++-------- process/sendreports.sh | 21 +++++++-------------- s/travis-build | 2 ++ 8 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 process/Makefile diff --git a/.travis.yml b/.travis.yml index a6b898c6c..325d573ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ addons: - libxml2-dev - valgrind - cppcheck + - shellcheck os: - linux notifications: diff --git a/process/Makefile b/process/Makefile new file mode 100644 index 000000000..194c9dd19 --- /dev/null +++ b/process/Makefile @@ -0,0 +1,6 @@ +SCRIPTS=compress.sh send-bz2-report send-zip-report \ +run-turn sendreports.sh +IGNORE=sendreport.sh create-orders + +shellcheck: $(SCRIPTS) + shellcheck $(SCRIPTS) diff --git a/process/compress.sh b/process/compress.sh index 116b3d061..4c0147978 100755 --- a/process/compress.sh +++ b/process/compress.sh @@ -1,24 +1,25 @@ #!/bin/bash -if [ -z $ERESSEA ]; then +if [ -z "$ERESSEA" ]; then echo "You need to define the \$ERESSEA environment variable to run $0" exit -2 fi -GAME=$ERESSEA/game-$1 -GAME_NAME=$(grep -w name $GAME/eressea.ini | sed 's/.*=\s*//') +GAME="$ERESSEA/game-$1" +GAME_NAME=$(grep -w name "$GAME/eressea.ini" | sed 's/.*=\s*//') TURN=$2 -if [ -z $TURN ] +if [ -z "$TURN" ] then - TURN=`cat $GAME/turn` + TURN=$(cat "$GAME/turn") fi -if [ ! -d $GAME/reports ]; then +if [ ! -d "$GAME/reports" ]; then echo "cannot find reports directory in $GAME" exit -1 fi -cd $GAME/reports -$ERESSEA/server/bin/compress.py $TURN "$GAME_NAME" -cd - +cd "$GAME/reports" || exit +"$ERESSEA/server/bin/compress.py" "$TURN" "$GAME_NAME" +cd - || exit + diff --git a/process/run-turn b/process/run-turn index c0d0d6a40..9fa6a325c 100755 --- a/process/run-turn +++ b/process/run-turn @@ -1,17 +1,17 @@ #!/bin/sh -GAME=$1 -TURN=$2 +GAME="$1" +TURN="$2" -if [ ! -d $ERESSEA/game-$GAME ] ; then +if [ ! -d "$ERESSEA/game-$GAME" ] ; then echo "No such game: $GAME" exit 1 fi -cd $ERESSEA/game-$GAME +cd "$ERESSEA/game-$GAME" || exit echo "running turn $TURN, game $GAME" -$ERESSEA/server/bin/eressea -v3 -t $TURN run-turn.lua +"$ERESSEA/server/bin/eressea" -v3 -t "$TURN" run-turn.lua mkdir -p log -ln -f eressea.log log/eressea.log.$TURN +ln -f eressea.log "log/eressea.log.$TURN" diff --git a/process/send-bz2-report b/process/send-bz2-report index 2282a9f24..c9fb87840 100755 --- a/process/send-bz2-report +++ b/process/send-bz2-report @@ -1,13 +1,12 @@ #!/bin/bash -if [ -z $ERESSEA ]; then - ERESSEA=`echo $PWD |sed -e 's/\/game.*//'` +if [ -z "$ERESSEA" ]; then + ERESSEA=$(echo "$PWD" |sed -e 's/\/game.*//') echo "Assuming that ERESSEA=$ERESSEA" fi if [ ! -f reports.txt ]; then echo "need to run $0 from the report direcory" exit -2 fi -source $HOME/bin/functions.sh TEMPLATE=report-mail.txt if [ "$1" == "-Lde" ] @@ -26,4 +25,6 @@ addr=$1 subj=$2 shift 2 -cat $ERESSEA/server/etc/$TEMPLATE | mutt -F $ERESSEA/etc/muttrc -s "$subj" -a $* -- $addr +mutt -F "$ERESSEA/etc/muttrc" -s "$subj" -a "$@" -- "$addr" \ + < "$ERESSEA/server/etc/$TEMPLATE" + diff --git a/process/send-zip-report b/process/send-zip-report index 6ea2ea8e7..4859ef4e5 100755 --- a/process/send-zip-report +++ b/process/send-zip-report @@ -1,6 +1,7 @@ #!/bin/bash -if [ -z $ERESSEA ]; then - ERESSEA=`echo $PWD |sed -e 's/\/game.*//'` + +if [ -z "$ERESSEA" ]; then + ERESSEA=$(echo "$PWD" |sed -e 's/\/game.*//') echo "Assuming that ERESSEA=$ERESSEA" fi if [ ! -f reports.txt ]; then @@ -9,7 +10,7 @@ if [ ! -f reports.txt ]; then fi PWD=$(pwd) -GAME=$(dirname $PWD) +GAME=$(dirname "$PWD") TEMPLATE=report-mail.txt if [ "$1" == "-Lde" ] @@ -24,13 +25,13 @@ then shift fi -if [ -e $GAME/$TEMPLATE ]; then -TEMPLATE=$GAME/$TEMPLATE +if [ -e "$GAME/$TEMPLATE" ]; then +TEMPLATE="$GAME/$TEMPLATE" else -TEMPLATE=$ERESSEA/server/etc/$TEMPLATE +TEMPLATE="$ERESSEA/server/etc/$TEMPLATE" fi -if [ ! -e $TEMPLATE ]; then +if [ ! -e "$TEMPLATE" ]; then echo "no such email template: $TEMPLATE" exit -3 fi @@ -42,7 +43,7 @@ done addr=$1 subject=$2 shift 2 -mutt -F $ERESSEA/etc/muttrc -s "$subject" -a $* -- $addr < $TEMPLATE +mutt -F "$ERESSEA/etc/muttrc" -s "$subject" -a "$@" -- "$addr" < "$TEMPLATE" if [ $? -ne 0 ] ; then echo "Sending failed for email/report: $2/$3" diff --git a/process/sendreports.sh b/process/sendreports.sh index c2bfa741c..d1adba943 100755 --- a/process/sendreports.sh +++ b/process/sendreports.sh @@ -3,30 +3,23 @@ ## ## Prepare the report -if [ -z $ERESSEA ]; then +if [ -z "$ERESSEA" ]; then echo "You have to define the \$ERESSEA environment variable to run $0" exit -2 fi -source $HOME/bin/functions.sh -if [ ! -z $1 ]; then - GAME=$ERESSEA/game-$1 +if [ ! -z "$1" ]; then + GAME="$ERESSEA/game-$1" else GAME=$ERESSEA fi -cd $GAME/reports || abort "could not chdir to reports directory" +cd "$GAME/reports" || exit for REPORT in *.sh do echo -n "Sending " - basename $REPORT .sh - bash $REPORT + basename "$REPORT" .sh + bash "$REPORT" done -cd - - -if [ -e $GAME/ages.sh ]; then - cd $GAME - ./ages.sh - cd - -fi +cd - || exit diff --git a/s/travis-build b/s/travis-build index 4c5237fdf..d861109d9 100755 --- a/s/travis-build +++ b/s/travis-build @@ -26,4 +26,6 @@ cppcheck --version cppcheck --quiet --error-exitcode=1 src s/runtests -V integration_tests +cd process +make From 0857e363e5f400b9e3fe48214f962d96f4999279 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Dec 2017 16:26:02 +0100 Subject: [PATCH 02/25] fix travis shellcheck --- s/travis-build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s/travis-build b/s/travis-build index d861109d9..2da3ed7b5 100755 --- a/s/travis-build +++ b/s/travis-build @@ -26,6 +26,6 @@ cppcheck --version cppcheck --quiet --error-exitcode=1 src s/runtests -V integration_tests -cd process +cd ../process make From a02c80cf76eedb0c6f27b2026a646aeff82be1ad Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Dec 2017 16:42:14 +0100 Subject: [PATCH 03/25] Check exit code directly --- process/send-zip-report | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/process/send-zip-report b/process/send-zip-report index 4859ef4e5..89f742b82 100755 --- a/process/send-zip-report +++ b/process/send-zip-report @@ -43,8 +43,7 @@ done addr=$1 subject=$2 shift 2 -mutt -F "$ERESSEA/etc/muttrc" -s "$subject" -a "$@" -- "$addr" < "$TEMPLATE" -if [ $? -ne 0 ] ; then - echo "Sending failed for email/report: $2/$3" -fi +mutt -F "$ERESSEA/etc/muttrc" -s "$subject" -a "$@" -- "$addr" \ + < "$TEMPLATE" || echo "Sending failed for email/report: $2/$3" + From 88f698df6e0236a12190d9b0d17fa0ef01678418 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Dec 2017 17:30:41 +0100 Subject: [PATCH 04/25] fix the error in create-orders --- process/Makefile | 4 ++-- process/create-orders | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/process/Makefile b/process/Makefile index 194c9dd19..8edfed0b8 100644 --- a/process/Makefile +++ b/process/Makefile @@ -1,6 +1,6 @@ -SCRIPTS=compress.sh send-bz2-report send-zip-report \ +SCRIPTS=compress.sh send-bz2-report send-zip-report create-orders \ run-turn sendreports.sh -IGNORE=sendreport.sh create-orders +IGNORE=sendreport.sh shellcheck: $(SCRIPTS) shellcheck $(SCRIPTS) diff --git a/process/create-orders b/process/create-orders index b0ca292f4..7016741ac 100755 --- a/process/create-orders +++ b/process/create-orders @@ -3,26 +3,25 @@ GAME=$1 TURN=$2 -if [ ! -d $ERESSEA/game-$GAME ] ; then +if [ ! -d "$ERESSEA/game-$GAME" ] ; then echo "No such game: $GAME" exit 1 fi -cd $ERESSEA/game-$GAME +cd "$ERESSEA/game-$GAME" || exit -if [ -d orders.dir.$TURN ]; then +if [ -d "orders.dir.$TURN" ]; then echo "orders.dir.$TURN already exists" else - mv orders.dir orders.dir.$TURN + mv orders.dir "orders.dir.$TURN" mkdir -p orders.dir fi -ls -1rt orders.dir.$TURN/turn-* | xargs cat > orders.$TURN +ls -1rt "orders.dir.$TURN/turn-*" | xargs cat > "orders.$TURN" lockfile -r3 -l120 orders.queue.lock if [ -e orders.queue ] ; then - mv orders.queue orders.dir.$TURN/orders.queue + mv orders.queue "orders.dir.$TURN/orders.queue" fi rm -f orders.queue.lock -fi From 05425b510157cf958cc512016fa4d90b116c0315 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 4 Dec 2017 19:20:48 +0100 Subject: [PATCH 05/25] pull economy requests into header, rename the struct. --- src/bind_ship.c | 72 ++++++++++++++++----------- src/creport.c | 2 +- src/economy.c | 118 ++++++++++++++++++++------------------------ src/economy.h | 17 +++++-- src/economy.test.c | 15 +----- src/kernel/region.c | 4 +- src/kernel/region.h | 2 +- src/laws.c | 6 +-- src/report.c | 4 +- 9 files changed, 121 insertions(+), 119 deletions(-) diff --git a/src/bind_ship.c b/src/bind_ship.c index c5e0735ed..cbb623e68 100644 --- a/src/bind_ship.c +++ b/src/bind_ship.c @@ -45,28 +45,35 @@ int tolua_shiplist_next(lua_State * L) static int tolua_ship_get_id(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); lua_pushinteger(L, self->no); return 1; } static int tolua_ship_get_name(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); tolua_pushstring(L, ship_getname(self)); return 1; } +static int tolua_ship_get_size(lua_State * L) +{ + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + lua_pushinteger(L, self->size); + return 1; +} + static int tolua_ship_get_display(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); tolua_pushstring(L, self->display); return 1; } static int tolua_ship_get_region(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); if (self) { tolua_pushusertype(L, self->region, TOLUA_CAST "region"); return 1; @@ -76,8 +83,8 @@ static int tolua_ship_get_region(lua_State * L) static int tolua_ship_set_region(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - region *r = (region *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + region *r = (region *)tolua_tousertype(L, 2, NULL); if (self) { move_ship(self, self->region, r, NULL); } @@ -86,22 +93,29 @@ static int tolua_ship_set_region(lua_State * L) static int tolua_ship_set_name(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - ship_setname(self, tolua_tostring(L, 2, 0)); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + ship_setname(self, tolua_tostring(L, 2, NULL)); + return 0; +} + +static int tolua_ship_set_size(lua_State * L) +{ + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + self->size = lua_tointeger(L, 2); return 0; } static int tolua_ship_set_display(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); free(self->display); - self->display = strdup(tolua_tostring(L, 2, 0)); + self->display = strdup(tolua_tostring(L, 2, NULL)); return 0; } static int tolua_ship_get_units(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); unit **unit_ptr = (unit **)lua_newuserdata(L, sizeof(unit *)); unit *u = self->region->units; @@ -118,8 +132,8 @@ static int tolua_ship_get_units(lua_State * L) static int tolua_ship_create(lua_State * L) { - region *r = (region *)tolua_tousertype(L, 1, 0); - const char *sname = tolua_tostring(L, 2, 0); + region *r = (region *)tolua_tousertype(L, 1, NULL); + const char *sname = tolua_tostring(L, 2, NULL); if (sname) { const ship_type *stype = st_find(sname); if (stype) { @@ -138,40 +152,40 @@ static int tolua_ship_create(lua_State * L) static int tolua_ship_tostring(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); lua_pushstring(L, shipname(self)); return 1; } static int tolua_ship_get_flags(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); lua_pushinteger(L, self->flags); return 1; } static int tolua_ship_set_flags(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - self->flags = (int)tolua_tonumber(L, 2, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + self->flags = (int)lua_tointeger(L, 2); return 0; } static int tolua_ship_set_coast(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); if (lua_isnil(L, 2)) { self->coast = NODIRECTION; } else if (lua_isnumber(L, 2)) { - self->coast = (direction_t)tolua_tonumber(L, 2, 0); + self->coast = (direction_t)lua_tointeger(L, 2); } return 0; } static int tolua_ship_get_coast(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); if (self->coast) { lua_pushinteger(L, self->coast); return 1; @@ -181,28 +195,28 @@ static int tolua_ship_get_coast(lua_State * L) static int tolua_ship_get_type(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); tolua_pushstring(L, self->type->_name); return 1; } static int tolua_ship_get_damage(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); lua_pushinteger(L, self->damage); return 1; } static int tolua_ship_set_damage(lua_State * L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - self->damage = (int)tolua_tonumber(L, 2, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + self->damage = (int)lua_tointeger(L, 2); return 0; } static int tolua_ship_get_curse(lua_State *L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + const char *name = tolua_tostring(L, 2, NULL); if (self->attribs) { curse * c = get_curse(self->attribs, ct_find(name)); if (c) { @@ -214,8 +228,8 @@ static int tolua_ship_get_curse(lua_State *L) { } static int tolua_ship_has_attrib(lua_State *L) { - ship *self = (ship *)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); + ship *self = (ship *)tolua_tousertype(L, 1, NULL); + const char *name = tolua_tostring(L, 2, NULL); attrib * a = a_find(self->attribs, at_find(name)); lua_pushboolean(L, a != NULL); return 1; @@ -236,6 +250,8 @@ void tolua_ship_open(lua_State * L) tolua_variable(L, TOLUA_CAST "id", tolua_ship_get_id, NULL); tolua_variable(L, TOLUA_CAST "name", tolua_ship_get_name, tolua_ship_set_name); + tolua_variable(L, TOLUA_CAST "size", tolua_ship_get_size, + tolua_ship_set_size); tolua_variable(L, TOLUA_CAST "info", tolua_ship_get_display, tolua_ship_set_display); tolua_variable(L, TOLUA_CAST "units", tolua_ship_get_units, NULL); diff --git a/src/creport.c b/src/creport.c index b04629b1d..3d2c46e7d 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1388,7 +1388,7 @@ static void cr_output_region(FILE * F, report_context * ctx, region * r) else { fprintf(F, "%d;Rekruten\n", rpeasants(r) / RECRUITFRACTION); } - if (production(r)) { + if (max_production(r)) { int p_wage = wage(r, NULL, NULL, turn + 1); fprintf(F, "%d;Lohn\n", p_wage); if (is_mourning(r, turn + 1)) { diff --git a/src/economy.c b/src/economy.c index 5629bd75d..bfe916ebb 100644 --- a/src/economy.c +++ b/src/economy.c @@ -81,26 +81,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -typedef struct request { - struct request *next; - struct unit *unit; - struct order *ord; - int qty; - int no; - union { - bool goblin; /* stealing */ - const struct luxury_type *ltype; /* trading */ - } type; -} request; - static int working; -static request entertainers[1024]; -static request *nextentertainer; +static production entertainers[1024]; +static production *nextentertainer; static int entertaining; static unsigned int norders; -static request *g_requests; +static production *g_requests; #define RECRUIT_MERGE 1 static int rules_recruit = -1; @@ -153,12 +141,12 @@ static void scramble(void *data, unsigned int n, size_t width) } } -static void expandorders(region * r, request * requests) +static void expandorders(region * r, production * requests) { unit *u; - request *o; + production *o; - /* Alle Units ohne request haben ein -1, alle units mit orders haben ein + /* Alle Units ohne production haben ein -1, alle units mit orders haben ein * 0 hier stehen */ for (u = r->units; u; u = u->next) @@ -174,7 +162,7 @@ static void expandorders(region * r, request * requests) if (norders > 0) { int i = 0; - g_requests = (request *)calloc(norders, sizeof(request)); + g_requests = (production *)calloc(norders, sizeof(production)); for (o = requests; o; o = o->next) { if (o->qty > 0) { unsigned int j; @@ -185,13 +173,13 @@ static void expandorders(region * r, request * requests) } } } - scramble(g_requests, norders, sizeof(request)); + scramble(g_requests, norders, sizeof(production)); } else { g_requests = NULL; } while (requests) { - request *o = requests->next; + production *o = requests->next; free_order(requests->ord); free(requests); requests = o; @@ -203,21 +191,21 @@ static void expandorders(region * r, request * requests) typedef struct recruitment { struct recruitment *next; faction *f; - request *requests; + production *requests; int total, assigned; } recruitment; -/** Creates a list of recruitment structs, one for each faction. Adds every quantifyable request +/** Creates a list of recruitment structs, one for each faction. Adds every quantifyable production * to the faction's struct and to total. */ -static recruitment *select_recruitment(request ** rop, +static recruitment *select_recruitment(production ** rop, int(*quantify) (const struct race *, int), int *total) { recruitment *recruits = NULL; while (*rop) { recruitment *rec = recruits; - request *ro = *rop; + production *ro = *rop; unit *u = ro->unit; const race *rc = u_race(u); int qty = quantify(rc, ro->qty); @@ -294,7 +282,7 @@ static int do_recruiting(recruitment * recruits, int available) int n = 0; int rest, mintotal = INT_MAX; - /* find smallest request */ + /* find smallest production */ for (rec = recruits; rec != NULL; rec = rec->next) { int want = rec->total - rec->assigned; if (want > 0) { @@ -310,7 +298,7 @@ static int do_recruiting(recruitment * recruits, int available) } rest = available - mintotal * n; - /* assign size of smallest request for everyone if possible; in the end roll dice to assign + /* assign size of smallest production for everyone if possible; in the end roll dice to assign * small rest */ for (rec = recruits; rec != NULL; rec = rec->next) { int want = rec->total - rec->assigned; @@ -330,7 +318,7 @@ static int do_recruiting(recruitment * recruits, int available) /* do actual recruiting */ for (rec = recruits; rec != NULL; rec = rec->next) { - request *req; + production *req; int get = rec->assigned; for (req = rec->requests; req; req = req->next) { @@ -379,7 +367,7 @@ void free_recruitments(recruitment * recruits) recruitment *rec = recruits; recruits = rec->next; while (rec->requests) { - request *req = rec->requests; + production *req = rec->requests; rec->requests = req->next; free_order(req->ord); free(req); @@ -389,7 +377,7 @@ void free_recruitments(recruitment * recruits) } /* Rekrutierung */ -static void expandrecruit(region * r, request * recruitorders) +static void expandrecruit(region * r, production * recruitorders) { recruitment *recruits = NULL; @@ -430,11 +418,11 @@ static int recruit_cost(const faction * f, const race * rc) return -1; } -static void recruit(unit * u, struct order *ord, request ** recruitorders) +static void recruit(unit * u, struct order *ord, production ** recruitorders) { region *r = u->region; plane *pl; - request *o; + production *o; int recruitcost = -1; const faction *f = u->faction; const struct race *rc = u_race(u); @@ -555,7 +543,7 @@ static void recruit(unit * u, struct order *ord, request ** recruitorders) return; } - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->qty = n; o->unit = u; o->ord = copy_order(ord); @@ -751,7 +739,7 @@ void maintain_buildings(region * r) void economics(region * r) { unit *u; - request *recruitorders = NULL; + production *recruitorders = NULL; /* Geben vor Selbstmord (doquit)! Hier alle unmittelbaren Befehle. * Rekrutieren vor allen Einnahmequellen. Bewachen JA vor Steuern @@ -1441,7 +1429,7 @@ const attrib_type at_luxuries = { "luxuries", NULL, free_luxuries, NULL, NULL, NULL }; -static void expandbuying(region * r, request * buyorders) +static void expandbuying(region * r, production * buyorders) { const resource_type *rsilver = get_resourcetype(R_SILVER); int max_products; @@ -1554,12 +1542,12 @@ attrib_type at_trades = { NO_READ }; -static void buy(unit * u, request ** buyorders, struct order *ord) +static void buy(unit * u, production ** buyorders, struct order *ord) { char token[128]; region *r = u->region; int n, k; - request *o; + production *o; attrib *a; const item_type *itype = NULL; const luxury_type *ltype = NULL; @@ -1650,7 +1638,7 @@ static void buy(unit * u, request ** buyorders, struct order *ord) ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "luxury_notsold", "")); return; } - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->type.ltype = ltype; /* sollte immer gleich sein */ o->unit = u; @@ -1670,7 +1658,7 @@ static void add_income(unit * u, int type, int want, int qty) /* Steuers�tze in % bei Burggr��e */ static int tax_per_size[7] = { 0, 6, 12, 18, 24, 30, 36 }; -static void expandselling(region * r, request * sellorders, int limit) +static void expandselling(region * r, production * sellorders, int limit) { int money, price, max_products; unsigned int j; @@ -1859,7 +1847,7 @@ static void expandselling(region * r, request * sellorders, int limit) } } -static bool sell(unit * u, request ** sellorders, struct order *ord) +static bool sell(unit * u, production ** sellorders, struct order *ord) { char token[128]; bool unlimited = true; @@ -1952,7 +1940,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) } else { attrib *a; - request *o; + production *o; int k, available; if (!r_demand(r, ltype)) { @@ -1977,7 +1965,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) cmistake(u, ord, 264, MSG_COMMERCE); return false; } - /* Hier wird request->type verwendet, weil die obere limit durch + /* Hier wird production->type verwendet, weil die obere limit durch * das silber gegeben wird (region->money), welches f�r alle * (!) produkte als summe gilt, als nicht wie bei der * produktion, wo f�r jedes produkt einzeln eine obere limite @@ -2000,7 +1988,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) assert(n >= 0); /* die Menge der verkauften G�ter merken */ a->data.i += n; - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->unit = u; o->qty = n; o->type.ltype = ltype; @@ -2012,7 +2000,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) /* ------------------------------------------------------------- */ -static void expandstealing(region * r, request * stealorders) +static void expandstealing(region * r, production * stealorders) { const resource_type *rsilver = get_resourcetype(R_SILVER); unsigned int j; @@ -2406,12 +2394,12 @@ message * check_steal(const unit * u, struct order *ord) { return 0; } -static void steal_cmd(unit * u, struct order *ord, request ** stealorders) +static void steal_cmd(unit * u, struct order *ord, production ** stealorders) { const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER); int n, i, id, effsk; bool goblin = false; - request *o; + production *o; unit *u2 = NULL; region *r = u->region; faction *f = NULL; @@ -2508,7 +2496,7 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders) /* wer dank unsichtbarkeitsringen klauen kann, muss nicht unbedingt ein * guter dieb sein, schliesslich macht man immer noch sehr viel laerm */ - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->unit = u; o->qty = 1; /* Betrag steht in u->wants */ o->no = u2->no; @@ -2526,7 +2514,7 @@ static void expandentertainment(region * r) { unit *u; int m = entertainmoney(r); - request *o; + production *o; for (o = &entertainers[0]; o != nextentertainer; ++o) { double part = m / (double)entertaining; @@ -2551,7 +2539,7 @@ void entertain_cmd(unit * u, struct order *ord) { region *r = u->region; int max_e; - request *o; + production *o; static int entertainbase = 0; static int entertainperlevel = 0; keyword_t kwd; @@ -2604,7 +2592,7 @@ void entertain_cmd(unit * u, struct order *ord) * \return number of working spaces taken by players */ static void -expandwork(region * r, request * work_begin, request * work_end, int maxwork) +expandwork(region * r, production * work_begin, production * work_end, int maxwork) { int earnings; /* n: verbleibende Einnahmen */ @@ -2612,7 +2600,7 @@ expandwork(region * r, request * work_begin, request * work_end, int maxwork) int jobs = maxwork; int p_wage = wage(r, NULL, NULL, turn); int money = rmoney(r); - request *o; + production *o; for (o = work_begin; o != work_end; ++o) { unit *u = o->unit; @@ -2655,7 +2643,7 @@ expandwork(region * r, request * work_begin, request * work_end, int maxwork) rsetmoney(r, money + earnings); } -static int do_work(unit * u, order * ord, request * o) +static int do_work(unit * u, order * ord, production * o) { if (playerrace(u_race(u))) { region *r = u->region; @@ -2690,7 +2678,7 @@ static int do_work(unit * u, order * ord, request * o) return -1; } -static void expandloot(region * r, request * lootorders) +static void expandloot(region * r, production * lootorders) { unit *u; unsigned int i; @@ -2727,7 +2715,7 @@ static void expandloot(region * r, request * lootorders) } } -void expandtax(region * r, request * taxorders) +void expandtax(region * r, production * taxorders) { unit *u; unsigned int i; @@ -2751,13 +2739,13 @@ void expandtax(region * r, request * taxorders) } } -void tax_cmd(unit * u, struct order *ord, request ** taxorders) +void tax_cmd(unit * u, struct order *ord, production ** taxorders) { /* Steuern werden noch vor der Forschung eingetrieben */ region *r = u->region; unit *u2; int n; - request *o; + production *o; int max; keyword_t kwd; static int taxperlevel = 0; @@ -2819,20 +2807,20 @@ void tax_cmd(unit * u, struct order *ord, request ** taxorders) * fraktionen werden dann bei eintreiben unter allen eintreibenden * einheiten aufgeteilt. */ - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(taxorders, o); return; } -void loot_cmd(unit * u, struct order *ord, request ** lootorders) +void loot_cmd(unit * u, struct order *ord, production ** lootorders) { region *r = u->region; unit *u2; int n; int max; - request *o; + production *o; keyword_t kwd; kwd = init_order_depr(ord); @@ -2884,7 +2872,7 @@ void loot_cmd(unit * u, struct order *ord, request ** lootorders) u->wants = MIN(n * skbonus * 10, max); } - o = (request *)calloc(1, sizeof(request)); + o = (production *)calloc(1, sizeof(production)); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(lootorders, o); @@ -2895,8 +2883,8 @@ void loot_cmd(unit * u, struct order *ord, request ** lootorders) #define MAX_WORKERS 2048 void auto_work(region * r) { - request workers[MAX_WORKERS]; - request *nextworker = workers; + production workers[MAX_WORKERS]; + production *nextworker = workers; unit *u; for (u = r->units; u; u = u->next) { @@ -2964,11 +2952,11 @@ static bool rule_autowork(void) { void produce(struct region *r) { - request workers[MAX_WORKERS]; - request *taxorders, *lootorders, *sellorders, *stealorders, *buyorders; + production workers[MAX_WORKERS]; + production *taxorders, *lootorders, *sellorders, *stealorders, *buyorders; unit *u; bool limited = true; - request *nextworker = workers; + production *nextworker = workers; static int bt_cache; static const struct building_type *caravan_bt; static int rc_cache; diff --git a/src/economy.h b/src/economy.h index 7b0eed8c9..8628a0813 100644 --- a/src/economy.h +++ b/src/economy.h @@ -45,7 +45,18 @@ extern "C" { struct faction; struct order; struct message; - struct request; + + typedef struct production { + struct production *next; + struct unit *unit; + struct order *ord; + int qty; + int no; + union { + bool goblin; /* stealing */ + const struct luxury_type *ltype; /* trading */ + } type; + } production; int income(const struct unit *u); int entertainmoney(const struct region *r); @@ -61,8 +72,8 @@ extern "C" { void split_allocations(struct region *r); int give_control_cmd(struct unit *u, struct order *ord); void give_control(struct unit * u, struct unit * u2); - void tax_cmd(struct unit * u, struct order *ord, struct request ** taxorders); - void expandtax(struct region * r, struct request * taxorders); + void tax_cmd(struct unit * u, struct order *ord, struct production ** taxorders); + void expandtax(struct region * r, struct production * taxorders); void add_recruits(struct unit * u, int number, int wanted); struct message * check_steal(const struct unit * u, struct order *ord); diff --git a/src/economy.test.c b/src/economy.test.c index 47194ef86..c3456714b 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -300,26 +300,13 @@ static void test_buy_cmd(CuTest *tc) { test_cleanup(); } -typedef struct request { - struct request *next; - struct unit *unit; - struct order *ord; - int qty; - int no; - union { - bool goblin; /* stealing */ - const struct luxury_type *ltype; /* trading */ - } type; -} request; - static void test_tax_cmd(CuTest *tc) { order *ord; faction *f; region *r; unit *u; item_type *sword, *silver; - request *taxorders = 0; - + production *taxorders = 0; test_setup(); init_resources(); diff --git a/src/kernel/region.c b/src/kernel/region.c index 3dad8e3ab..91afaabd4 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -139,7 +139,7 @@ const char *regionname(const region * r, const faction * f) int region_maxworkers(const region *r) { - int size = production(r); + int size = max_production(r); int treespace = (rtrees(r, 2) + rtrees(r, 1) / 2) * TREESIZE; return MAX(size - treespace, MIN(size / 10, 200)); } @@ -1244,7 +1244,7 @@ void terraform_region(region * r, const terrain_type * terrain) * egal ob durch den spell oder anderes angelegt. **/ #include "curse.h" -int production(const region * r) +int max_production(const region * r) { /* muß rterrain(r) sein, nicht rterrain() wegen rekursion */ int p = r->terrain->size; diff --git a/src/kernel/region.h b/src/kernel/region.h index 7f86f397f..4c0fbc187 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -240,7 +240,7 @@ extern "C" { extern const int delta_x[MAXDIRECTIONS]; extern const int delta_y[MAXDIRECTIONS]; direction_t dir_invert(direction_t dir); - int production(const struct region *r); + int max_production(const struct region *r); void region_set_owner(struct region *r, struct faction *owner, int turn); struct faction *region_get_owner(const struct region *r); diff --git a/src/laws.c b/src/laws.c index f0891a2c6..72b7c4f38 100644 --- a/src/laws.c +++ b/src/laws.c @@ -314,7 +314,7 @@ static void peasants(region * r, int rule) { int peasants = rpeasants(r); int money = rmoney(r); - int maxp = production(r); + int maxp = max_production(r); int n, satiated; int dead = 0; @@ -588,12 +588,12 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) return; } - if (production(r) <= 0) + if (max_production(r) <= 0) return; /* Grundchance 1.0% */ /* Jeder Elf in der Region erhöht die Chance marginal */ - elves = MIN(elves, production(r) / 8); + elves = MIN(elves, max_production(r) / 8); if (elves) { seedchance += 1.0 - pow(0.99999, elves * RESOURCE_QUANTITY); } diff --git a/src/report.c b/src/report.c index acbdee843..23993f489 100644 --- a/src/report.c +++ b/src/report.c @@ -974,7 +974,7 @@ void report_region(struct stream *out, const region * r, faction * f) /* Trees */ trees = rtrees(r, 2); saplings = rtrees(r, 1); - if (production(r)) { + if (max_production(r)) { if (trees > 0 || saplings > 0) { bytes = snprintf(bufp, size, ", %d/%d ", trees, saplings); if (wrptr(&bufp, &size, bytes) != 0) @@ -1311,7 +1311,7 @@ static void statistics(struct stream *out, const region * r, const faction * f) paragraph(out, buf, 2, 2, 0); msg_release(m); } - if (production(r) && (!fval(r->terrain, SEA_REGION) + if (max_production(r) && (!fval(r->terrain, SEA_REGION) || f->race == get_race(RC_AQUARIAN))) { if (markets_module()) { /* hack */ m = From 3864a00482ff9376bf2f084fe8ff14b4a833b2ec Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 4 Dec 2017 20:01:08 +0100 Subject: [PATCH 06/25] remove stealing from economy.c (that file is too big). --- src/CMakeLists.txt | 1 + src/economy.c | 301 +++++++++------------------------------------ src/economy.h | 23 ++-- src/economy.test.c | 8 +- src/steal.c | 237 +++++++++++++++++++++++++++++++++++ 5 files changed, 315 insertions(+), 255 deletions(-) create mode 100644 src/steal.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 07242b312..89e9b90c4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -109,6 +109,7 @@ set (ERESSEA_SRC json.c creport.c report.c + steal.c economy.c give.c items.c diff --git a/src/economy.c b/src/economy.c index bfe916ebb..55e0fafb1 100644 --- a/src/economy.c +++ b/src/economy.c @@ -83,12 +83,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static int working; -static production entertainers[1024]; -static production *nextentertainer; +static econ_request entertainers[1024]; +static econ_request *nextentertainer; static int entertaining; static unsigned int norders; -static production *g_requests; +static econ_request *g_requests; #define RECRUIT_MERGE 1 static int rules_recruit = -1; @@ -141,10 +141,11 @@ static void scramble(void *data, unsigned int n, size_t width) } } -static void expandorders(region * r, production * requests) +unsigned int expand_production(region * r, econ_request * requests, econ_request **results) { unit *u; - production *o; + econ_request *o; + unsigned int norders = 0; /* Alle Units ohne production haben ein -1, alle units mit orders haben ein * 0 hier stehen */ @@ -152,8 +153,6 @@ static void expandorders(region * r, production * requests) for (u = r->units; u; u = u->next) u->n = -1; - norders = 0; - for (o = requests; o; o = o->next) { if (o->qty > 0) { norders += o->qty; @@ -162,28 +161,35 @@ static void expandorders(region * r, production * requests) if (norders > 0) { int i = 0; - g_requests = (production *)calloc(norders, sizeof(production)); + econ_request *split; + split = (econ_request *)calloc(norders, sizeof(econ_request)); for (o = requests; o; o = o->next) { if (o->qty > 0) { unsigned int j; for (j = o->qty; j; j--) { - g_requests[i] = *o; - g_requests[i].unit->n = 0; + split[i] = *o; + split[i].unit->n = 0; i++; } } } - scramble(g_requests, norders, sizeof(production)); + scramble(split, norders, sizeof(econ_request)); + *results = split; } else { - g_requests = NULL; + *results = NULL; } while (requests) { - production *o = requests->next; + econ_request *o = requests->next; free_order(requests->ord); free(requests); requests = o; } + return norders; +} + +static void expandorders(region * r, econ_request * requests) { + norders = expand_production(r, requests, &g_requests); } /* ------------------------------------------------------------- */ @@ -191,21 +197,21 @@ static void expandorders(region * r, production * requests) typedef struct recruitment { struct recruitment *next; faction *f; - production *requests; + econ_request *requests; int total, assigned; } recruitment; /** Creates a list of recruitment structs, one for each faction. Adds every quantifyable production * to the faction's struct and to total. */ -static recruitment *select_recruitment(production ** rop, +static recruitment *select_recruitment(econ_request ** rop, int(*quantify) (const struct race *, int), int *total) { recruitment *recruits = NULL; while (*rop) { recruitment *rec = recruits; - production *ro = *rop; + econ_request *ro = *rop; unit *u = ro->unit; const race *rc = u_race(u); int qty = quantify(rc, ro->qty); @@ -318,7 +324,7 @@ static int do_recruiting(recruitment * recruits, int available) /* do actual recruiting */ for (rec = recruits; rec != NULL; rec = rec->next) { - production *req; + econ_request *req; int get = rec->assigned; for (req = rec->requests; req; req = req->next) { @@ -367,7 +373,7 @@ void free_recruitments(recruitment * recruits) recruitment *rec = recruits; recruits = rec->next; while (rec->requests) { - production *req = rec->requests; + econ_request *req = rec->requests; rec->requests = req->next; free_order(req->ord); free(req); @@ -377,7 +383,7 @@ void free_recruitments(recruitment * recruits) } /* Rekrutierung */ -static void expandrecruit(region * r, production * recruitorders) +static void expandrecruit(region * r, econ_request * recruitorders) { recruitment *recruits = NULL; @@ -418,11 +424,11 @@ static int recruit_cost(const faction * f, const race * rc) return -1; } -static void recruit(unit * u, struct order *ord, production ** recruitorders) +static void recruit(unit * u, struct order *ord, econ_request ** recruitorders) { region *r = u->region; plane *pl; - production *o; + econ_request *o; int recruitcost = -1; const faction *f = u->faction; const struct race *rc = u_race(u); @@ -543,7 +549,7 @@ static void recruit(unit * u, struct order *ord, production ** recruitorders) return; } - o = (production *)calloc(1, sizeof(production)); + o = (econ_request *)calloc(1, sizeof(econ_request)); o->qty = n; o->unit = u; o->ord = copy_order(ord); @@ -739,7 +745,7 @@ void maintain_buildings(region * r) void economics(region * r) { unit *u; - production *recruitorders = NULL; + econ_request *recruitorders = NULL; /* Geben vor Selbstmord (doquit)! Hier alle unmittelbaren Befehle. * Rekrutieren vor allen Einnahmequellen. Bewachen JA vor Steuern @@ -1429,7 +1435,7 @@ const attrib_type at_luxuries = { "luxuries", NULL, free_luxuries, NULL, NULL, NULL }; -static void expandbuying(region * r, production * buyorders) +static void expandbuying(region * r, econ_request * buyorders) { const resource_type *rsilver = get_resourcetype(R_SILVER); int max_products; @@ -1542,12 +1548,12 @@ attrib_type at_trades = { NO_READ }; -static void buy(unit * u, production ** buyorders, struct order *ord) +static void buy(unit * u, econ_request ** buyorders, struct order *ord) { char token[128]; region *r = u->region; int n, k; - production *o; + econ_request *o; attrib *a; const item_type *itype = NULL; const luxury_type *ltype = NULL; @@ -1638,7 +1644,7 @@ static void buy(unit * u, production ** buyorders, struct order *ord) ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "luxury_notsold", "")); return; } - o = (production *)calloc(1, sizeof(production)); + o = (econ_request *)calloc(1, sizeof(econ_request)); o->type.ltype = ltype; /* sollte immer gleich sein */ o->unit = u; @@ -1647,18 +1653,18 @@ static void buy(unit * u, production ** buyorders, struct order *ord) } /* ------------------------------------------------------------- */ -static void add_income(unit * u, int type, int want, int qty) +void add_income(unit * u, income_t type, int want, int qty) { if (want == INT_MAX) want = qty; ADDMSG(&u->faction->msgs, msg_message("income", - "unit region mode wanted amount", u, u->region, type, want, qty)); + "unit region mode wanted amount", u, u->region, (int)type, want, qty)); } /* Steuers�tze in % bei Burggr��e */ static int tax_per_size[7] = { 0, 6, 12, 18, 24, 30, 36 }; -static void expandselling(region * r, production * sellorders, int limit) +static void expandselling(region * r, econ_request * sellorders, int limit) { int money, price, max_products; unsigned int j; @@ -1847,7 +1853,7 @@ static void expandselling(region * r, production * sellorders, int limit) } } -static bool sell(unit * u, production ** sellorders, struct order *ord) +static bool sell(unit * u, econ_request ** sellorders, struct order *ord) { char token[128]; bool unlimited = true; @@ -1940,7 +1946,7 @@ static bool sell(unit * u, production ** sellorders, struct order *ord) } else { attrib *a; - production *o; + econ_request *o; int k, available; if (!r_demand(r, ltype)) { @@ -1988,7 +1994,7 @@ static bool sell(unit * u, production ** sellorders, struct order *ord) assert(n >= 0); /* die Menge der verkauften G�ter merken */ a->data.i += n; - o = (production *)calloc(1, sizeof(production)); + o = (econ_request *)calloc(1, sizeof(econ_request)); o->unit = u; o->qty = n; o->type.ltype = ltype; @@ -1998,48 +2004,6 @@ static bool sell(unit * u, production ** sellorders, struct order *ord) } } -/* ------------------------------------------------------------- */ - -static void expandstealing(region * r, production * stealorders) -{ - const resource_type *rsilver = get_resourcetype(R_SILVER); - unsigned int j; - - assert(rsilver); - - expandorders(r, stealorders); - if (!norders) return; - - /* F�r jede unit in der Region wird Geld geklaut, wenn sie Opfer eines - * Beklauen-Orders ist. Jedes Opfer mu� einzeln behandelt werden. - * - * u ist die beklaute unit. oa.unit ist die klauende unit. - */ - - for (j = 0; j != norders && g_requests[j].unit->n <= g_requests[j].unit->wants; j++) { - unit *u = findunitg(g_requests[j].no, r); - int n = 0; - if (u && u->region == r) { - n = get_pooled(u, rsilver, GET_ALL, INT_MAX); - } - if (n > 10 && rplane(r) && (rplane(r)->flags & PFL_NOALLIANCES)) { - /* In Questen nur reduziertes Klauen */ - n = 10; - } - if (n > 0) { - n = MIN(n, g_requests[j].unit->wants); - use_pooled(u, rsilver, GET_ALL, n); - g_requests[j].unit->n = n; - change_money(g_requests[j].unit, n); - ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount", - u, u->region, n)); - } - add_income(g_requests[j].unit, IC_STEAL, g_requests[j].unit->wants, g_requests[j].unit->n); - fset(g_requests[j].unit, UFL_LONGACTION | UFL_NOTMOVING); - } - free(g_requests); -} - /* ------------------------------------------------------------- */ static void plant(unit * u, int raw) { @@ -2359,162 +2323,11 @@ static void research_cmd(unit * u, struct order *ord) } } -static int max_skill(region * r, faction * f, skill_t sk) -{ - unit *u; - int w = 0; - - for (u = r->units; u; u = u->next) { - if (u->faction == f) { - int effsk = effskill(u, sk, 0); - if (effsk > w) { - w = effsk; - } - } - } - - return w; -} - -message * check_steal(const unit * u, struct order *ord) { - plane *pl; - - if (fval(u_race(u), RCF_NOSTEAL)) { - return msg_feedback(u, ord, "race_nosteal", "race", u_race(u)); - } - - if (fval(u->region->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) { - return msg_feedback(u, ord, "error_onlandonly", ""); - } - - pl = rplane(u->region); - if (pl && fval(pl, PFL_NOATTACK)) { - return msg_feedback(u, ord, "error270", ""); - } - return 0; -} - -static void steal_cmd(unit * u, struct order *ord, production ** stealorders) -{ - const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER); - int n, i, id, effsk; - bool goblin = false; - production *o; - unit *u2 = NULL; - region *r = u->region; - faction *f = NULL; - message * msg; - keyword_t kwd; - - kwd = init_order_depr(ord); - assert(kwd == K_STEAL); - - assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); - - msg = check_steal(u, ord); - if (msg) { - ADDMSG(&u->faction->msgs, msg); - return; - } - id = read_unitid(u->faction, r); - if (id > 0) { - u2 = findunitr(r, id); - } - if (u2 && u2->region == u->region) { - f = u2->faction; - } - else { - /* TODO: is this really necessary? it's the only time we use faction.c/deadhash - * it allows stealing from a unit in a dead faction, but why? */ - f = dfindhash(id); - } - - for (u2 = r->units; u2; u2 = u2->next) { - if (u2->faction == f && cansee(u->faction, r, u2, 0)) - break; - } - - if (!u2) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found", - "")); - return; - } - - if (IsImmune(u2->faction)) { - ADDMSG(&u->faction->msgs, - msg_feedback(u, ord, "newbie_immunity_error", "turns", NewbieImmunity())); - return; - } - - if (u->faction->alliance && u->faction->alliance == u2->faction->alliance) { - cmistake(u, ord, 47, MSG_INCOME); - return; - } - - assert(u->region == u2->region); - if (!can_contact(r, u, u2)) { - ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error60", "")); - return; - } - - effsk = effskill(u, SK_STEALTH, 0); - n = effsk - max_skill(r, f, SK_PERCEPTION); - - if (n <= 0) { - /* Wahrnehmung == Tarnung */ - if (u_race(u) != get_race(RC_GOBLIN) || effsk <= 3) { - ADDMSG(&u->faction->msgs, msg_message("stealfail", "unit target", u, u2)); - if (n == 0) { - ADDMSG(&u2->faction->msgs, msg_message("stealdetect", "unit", u2)); - } - else { - ADDMSG(&u2->faction->msgs, msg_message("thiefdiscover", "unit target", - u, u2)); - } - return; - } - else { - ADDMSG(&u->faction->msgs, msg_message("stealfatal", "unit target", u, - u2)); - ADDMSG(&u2->faction->msgs, msg_message("thiefdiscover", "unit target", u, - u2)); - n = 1; - goblin = true; - } - } - - i = MIN(u->number, i_get(u->items, rring->itype)); - if (i > 0) { - n *= STEALINCOME * (u->number + i * (roqf_factor() - 1)); - } - else { - n *= u->number * STEALINCOME; - } - - u->wants = n; - - /* wer dank unsichtbarkeitsringen klauen kann, muss nicht unbedingt ein - * guter dieb sein, schliesslich macht man immer noch sehr viel laerm */ - - o = (production *)calloc(1, sizeof(production)); - o->unit = u; - o->qty = 1; /* Betrag steht in u->wants */ - o->no = u2->no; - o->type.goblin = goblin; /* Merken, wenn Goblin-Spezialklau */ - addlist(stealorders, o); - - /* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */ - - produceexp(u, SK_STEALTH, MIN(n, u->number)); -} - -/* ------------------------------------------------------------- */ - static void expandentertainment(region * r) { unit *u; int m = entertainmoney(r); - production *o; + econ_request *o; for (o = &entertainers[0]; o != nextentertainer; ++o) { double part = m / (double)entertaining; @@ -2539,7 +2352,7 @@ void entertain_cmd(unit * u, struct order *ord) { region *r = u->region; int max_e; - production *o; + econ_request *o; static int entertainbase = 0; static int entertainperlevel = 0; keyword_t kwd; @@ -2592,7 +2405,7 @@ void entertain_cmd(unit * u, struct order *ord) * \return number of working spaces taken by players */ static void -expandwork(region * r, production * work_begin, production * work_end, int maxwork) +expandwork(region * r, econ_request * work_begin, econ_request * work_end, int maxwork) { int earnings; /* n: verbleibende Einnahmen */ @@ -2600,7 +2413,7 @@ expandwork(region * r, production * work_begin, production * work_end, int maxwo int jobs = maxwork; int p_wage = wage(r, NULL, NULL, turn); int money = rmoney(r); - production *o; + econ_request *o; for (o = work_begin; o != work_end; ++o) { unit *u = o->unit; @@ -2643,7 +2456,7 @@ expandwork(region * r, production * work_begin, production * work_end, int maxwo rsetmoney(r, money + earnings); } -static int do_work(unit * u, order * ord, production * o) +static int do_work(unit * u, order * ord, econ_request * o) { if (playerrace(u_race(u))) { region *r = u->region; @@ -2678,7 +2491,7 @@ static int do_work(unit * u, order * ord, production * o) return -1; } -static void expandloot(region * r, production * lootorders) +static void expandloot(region * r, econ_request * lootorders) { unit *u; unsigned int i; @@ -2715,7 +2528,7 @@ static void expandloot(region * r, production * lootorders) } } -void expandtax(region * r, production * taxorders) +void expandtax(region * r, econ_request * taxorders) { unit *u; unsigned int i; @@ -2739,13 +2552,13 @@ void expandtax(region * r, production * taxorders) } } -void tax_cmd(unit * u, struct order *ord, production ** taxorders) +void tax_cmd(unit * u, struct order *ord, econ_request ** taxorders) { /* Steuern werden noch vor der Forschung eingetrieben */ region *r = u->region; unit *u2; int n; - production *o; + econ_request *o; int max; keyword_t kwd; static int taxperlevel = 0; @@ -2807,20 +2620,20 @@ void tax_cmd(unit * u, struct order *ord, production ** taxorders) * fraktionen werden dann bei eintreiben unter allen eintreibenden * einheiten aufgeteilt. */ - o = (production *)calloc(1, sizeof(production)); + o = (econ_request *)calloc(1, sizeof(econ_request)); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(taxorders, o); return; } -void loot_cmd(unit * u, struct order *ord, production ** lootorders) +void loot_cmd(unit * u, struct order *ord, econ_request ** lootorders) { region *r = u->region; unit *u2; int n; int max; - production *o; + econ_request *o; keyword_t kwd; kwd = init_order_depr(ord); @@ -2872,7 +2685,7 @@ void loot_cmd(unit * u, struct order *ord, production ** lootorders) u->wants = MIN(n * skbonus * 10, max); } - o = (production *)calloc(1, sizeof(production)); + o = (econ_request *)calloc(1, sizeof(econ_request)); o->qty = u->wants / TAXFRACTION; o->unit = u; addlist(lootorders, o); @@ -2883,8 +2696,8 @@ void loot_cmd(unit * u, struct order *ord, production ** lootorders) #define MAX_WORKERS 2048 void auto_work(region * r) { - production workers[MAX_WORKERS]; - production *nextworker = workers; + econ_request workers[MAX_WORKERS]; + econ_request *nextworker = workers; unit *u; for (u = r->units; u; u = u->next) { @@ -2952,11 +2765,11 @@ static bool rule_autowork(void) { void produce(struct region *r) { - production workers[MAX_WORKERS]; - production *taxorders, *lootorders, *sellorders, *stealorders, *buyorders; + econ_request workers[MAX_WORKERS]; + econ_request *taxorders, *lootorders, *sellorders, *stealorders, *buyorders; unit *u; bool limited = true; - production *nextworker = workers; + econ_request *nextworker = workers; static int bt_cache; static const struct building_type *caravan_bt; static int rc_cache; diff --git a/src/economy.h b/src/economy.h index 8628a0813..2689a654b 100644 --- a/src/economy.h +++ b/src/economy.h @@ -46,8 +46,8 @@ extern "C" { struct order; struct message; - typedef struct production { - struct production *next; + typedef struct econ_request { + struct econ_request *next; struct unit *unit; struct order *ord; int qty; @@ -56,7 +56,7 @@ extern "C" { bool goblin; /* stealing */ const struct luxury_type *ltype; /* trading */ } type; - } production; + } econ_request; int income(const struct unit *u); int entertainmoney(const struct region *r); @@ -65,17 +65,26 @@ extern "C" { void produce(struct region *r); void auto_work(struct region *r); - enum { IC_WORK, IC_ENTERTAIN, IC_TAX, IC_TRADE, IC_TRADETAX, IC_STEAL, IC_MAGIC, IC_LOOT }; + unsigned int expand_production(struct region * r, struct econ_request * requests, struct econ_request **results); + + typedef enum income_t { IC_WORK, IC_ENTERTAIN, IC_TAX, IC_TRADE, IC_TRADETAX, IC_STEAL, IC_MAGIC, IC_LOOT } income_t; + void add_income(struct unit * u, income_t type, int want, int qty); + void maintain_buildings(struct region *r); void make_item(struct unit * u, const struct item_type * itype, int want); int make_cmd(struct unit *u, struct order *ord); void split_allocations(struct region *r); int give_control_cmd(struct unit *u, struct order *ord); void give_control(struct unit * u, struct unit * u2); - void tax_cmd(struct unit * u, struct order *ord, struct production ** taxorders); - void expandtax(struct region * r, struct production * taxorders); + + void tax_cmd(struct unit * u, struct order *ord, struct econ_request ** taxorders); + void expandtax(struct region * r, struct econ_request * taxorders); + + struct message * steal_message(const struct unit * u, struct order *ord); + void steal_cmd(struct unit * u, struct order *ord, struct econ_request ** stealorders); + void expandstealing(struct region * r, struct econ_request * stealorders); + void add_recruits(struct unit * u, int number, int wanted); - struct message * check_steal(const struct unit * u, struct order *ord); #ifdef __cplusplus } diff --git a/src/economy.test.c b/src/economy.test.c index c3456714b..437f59d79 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -88,7 +88,7 @@ static void test_steal_okay(CuTest * tc) { rc = test_create_race("human"); rc->flags = 0; setup_steal(&env, ter, rc); - CuAssertPtrEquals(tc, 0, check_steal(env.u, 0)); + CuAssertPtrEquals(tc, 0, steal_message(env.u, 0)); test_cleanup(); } @@ -103,7 +103,7 @@ static void test_steal_nosteal(CuTest * tc) { rc = test_create_race("human"); rc->flags = RCF_NOSTEAL; setup_steal(&env, ter, rc); - CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0)); + CuAssertPtrNotNull(tc, msg = steal_message(env.u, 0)); msg_release(msg); test_cleanup(); } @@ -118,7 +118,7 @@ static void test_steal_ocean(CuTest * tc) { ter = test_create_terrain("ocean", SEA_REGION); rc = test_create_race("human"); setup_steal(&env, ter, rc); - CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0)); + CuAssertPtrNotNull(tc, msg = steal_message(env.u, 0)); msg_release(msg); test_cleanup(); } @@ -306,7 +306,7 @@ static void test_tax_cmd(CuTest *tc) { region *r; unit *u; item_type *sword, *silver; - production *taxorders = 0; + econ_request *taxorders = 0; test_setup(); init_resources(); diff --git a/src/steal.c b/src/steal.c new file mode 100644 index 000000000..13df2c4ce --- /dev/null +++ b/src/steal.c @@ -0,0 +1,237 @@ +/* +Copyright (c) 1998-2014, +Enno Rehling +Katja Zedel + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +**/ + +#include +#include +#include "economy.h" + +#include "laws.h" +#include "skill.h" +#include "study.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +void expandstealing(region * r, econ_request * stealorders) +{ + const resource_type *rsilver = get_resourcetype(R_SILVER); + unsigned int j; + unsigned int norders; + econ_request *requests; + + assert(rsilver); + + norders = expand_production(r, stealorders, &requests); + if (!norders) return; + + /* F�r jede unit in der Region wird Geld geklaut, wenn sie Opfer eines + * Beklauen-Orders ist. Jedes Opfer mu� einzeln behandelt werden. + * + * u ist die beklaute unit. oa.unit ist die klauende unit. + */ + + for (j = 0; j != norders && requests[j].unit->n <= requests[j].unit->wants; j++) { + unit *u = findunitg(requests[j].no, r); + int n = 0; + if (u && u->region == r) { + n = get_pooled(u, rsilver, GET_ALL, INT_MAX); + } + if (n > 10 && rplane(r) && (rplane(r)->flags & PFL_NOALLIANCES)) { + /* In Questen nur reduziertes Klauen */ + n = 10; + } + if (n > 0) { + n = MIN(n, requests[j].unit->wants); + use_pooled(u, rsilver, GET_ALL, n); + requests[j].unit->n = n; + change_money(requests[j].unit, n); + ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount", + u, u->region, n)); + } + add_income(requests[j].unit, IC_STEAL, requests[j].unit->wants, requests[j].unit->n); + fset(requests[j].unit, UFL_LONGACTION | UFL_NOTMOVING); + } + free(requests); +} + +static int max_skill(region * r, struct faction * f, skill_t sk) +{ + unit *u; + int w = 0; + + for (u = r->units; u; u = u->next) { + if (u->faction == f) { + int effsk = effskill(u, sk, 0); + if (effsk > w) { + w = effsk; + } + } + } + + return w; +} + +message * steal_message(const unit * u, struct order *ord) { + plane *pl; + + if (fval(u_race(u), RCF_NOSTEAL)) { + return msg_feedback(u, ord, "race_nosteal", "race", u_race(u)); + } + + if (fval(u->region->terrain, SEA_REGION) && u_race(u) != get_race(RC_AQUARIAN)) { + return msg_feedback(u, ord, "error_onlandonly", ""); + } + + pl = rplane(u->region); + if (pl && fval(pl, PFL_NOATTACK)) { + return msg_feedback(u, ord, "error270", ""); + } + return 0; +} + +void steal_cmd(unit * u, struct order *ord, econ_request ** stealorders) +{ + const resource_type *rring = get_resourcetype(R_RING_OF_NIMBLEFINGER); + int n, i, id, effsk; + bool goblin = false; + econ_request *o; + unit *u2 = NULL; + region *r = u->region; + faction *f = NULL; + message * msg; + keyword_t kwd; + + kwd = init_order_depr(ord); + assert(kwd == K_STEAL); + + assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); + + msg = steal_message(u, ord); + if (msg) { + ADDMSG(&u->faction->msgs, msg); + return; + } + id = read_unitid(u->faction, r); + if (id > 0) { + u2 = findunitr(r, id); + } + if (u2 && u2->region == u->region) { + f = u2->faction; + } + else { + /* TODO: is this really necessary? it's the only time we use faction.c/deadhash + * it allows stealing from a unit in a dead faction, but why? */ + f = dfindhash(id); + } + + for (u2 = r->units; u2; u2 = u2->next) { + if (u2->faction == f && cansee(u->faction, r, u2, 0)) + break; + } + + if (!u2) { + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found", + "")); + return; + } + + if (IsImmune(u2->faction)) { + ADDMSG(&u->faction->msgs, + msg_feedback(u, ord, "newbie_immunity_error", "turns", NewbieImmunity())); + return; + } + + if (u->faction->alliance && u->faction->alliance == u2->faction->alliance) { + cmistake(u, ord, 47, MSG_INCOME); + return; + } + + assert(u->region == u2->region); + if (!can_contact(r, u, u2)) { + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error60", "")); + return; + } + + effsk = effskill(u, SK_STEALTH, 0); + n = effsk - max_skill(r, f, SK_PERCEPTION); + + if (n <= 0) { + /* Wahrnehmung == Tarnung */ + if (u_race(u) != get_race(RC_GOBLIN) || effsk <= 3) { + ADDMSG(&u->faction->msgs, msg_message("stealfail", "unit target", u, u2)); + if (n == 0) { + ADDMSG(&u2->faction->msgs, msg_message("stealdetect", "unit", u2)); + } + else { + ADDMSG(&u2->faction->msgs, msg_message("thiefdiscover", "unit target", + u, u2)); + } + return; + } + else { + ADDMSG(&u->faction->msgs, msg_message("stealfatal", "unit target", u, + u2)); + ADDMSG(&u2->faction->msgs, msg_message("thiefdiscover", "unit target", u, + u2)); + n = 1; + goblin = true; + } + } + + i = MIN(u->number, i_get(u->items, rring->itype)); + if (i > 0) { + n *= STEALINCOME * (u->number + i * (roqf_factor() - 1)); + } + else { + n *= u->number * STEALINCOME; + } + + u->wants = n; + + /* wer dank unsichtbarkeitsringen klauen kann, muss nicht unbedingt ein + * guter dieb sein, schliesslich macht man immer noch sehr viel laerm */ + + o = (econ_request *)calloc(1, sizeof(econ_request)); + o->unit = u; + o->qty = 1; /* Betrag steht in u->wants */ + o->no = u2->no; + o->type.goblin = goblin; /* Merken, wenn Goblin-Spezialklau */ + o->next = *stealorders; + *stealorders = o; + + /* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */ + + produceexp(u, SK_STEALTH, MIN(n, u->number)); +} From 7b2531a8aeb060c6e4b07fec687a38e236568a8e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 6 Dec 2017 20:00:32 +0100 Subject: [PATCH 07/25] writing orders to the CR no longer copies them into a buffer when it can be avoided. --- process/run-turn | 2 +- src/creport.c | 20 ++++++-------------- src/kernel/order.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/order.h | 2 ++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/process/run-turn b/process/run-turn index 9fa6a325c..bb12976c1 100755 --- a/process/run-turn +++ b/process/run-turn @@ -11,7 +11,7 @@ fi cd "$ERESSEA/game-$GAME" || exit echo "running turn $TURN, game $GAME" -"$ERESSEA/server/bin/eressea" -v3 -t "$TURN" run-turn.lua +"$ERESSEA/server/bin/eressea" -t "$TURN" run-turn.lua mkdir -p log ln -f eressea.log "log/eressea.log.$TURN" diff --git a/src/creport.c b/src/creport.c index 3d2c46e7d..89cf4c9ce 100644 --- a/src/creport.c +++ b/src/creport.c @@ -723,18 +723,6 @@ static void cr_output_ship_compat(FILE *F, const ship *sh, const unit *u, cr_output_ship(&strm, sh, u, fcaptain, f, r); } -static int stream_order(stream *out, const struct order *ord, const struct locale *lang) { - const char *str; - char ebuf[1025]; - char obuf[1024]; - write_order(ord, lang, obuf, sizeof(obuf)); - str = escape_string(obuf, ebuf, sizeof(ebuf)); - if (str == ebuf) { - ebuf[1024] = 0; - } - return stream_printf(out, "\"%s\"\n", str); -} - static void cr_output_spells(stream *out, const unit * u, int maxlevel) { spellbook * book = unit_get_spellbook(u); @@ -925,7 +913,9 @@ void cr_output_unit(stream *out, const region * r, const faction * f, for (ord = u->old_orders; ord; ord = ord->next) { /* this new order will replace the old defaults */ if (is_persistent(ord)) { - stream_order(out, ord, lang); + swrite("\"", 1, 1, out); + stream_order(out, ord, lang, true); + swrite("\"\n", 1, 2, out); } } for (ord = u->orders; ord; ord = ord->next) { @@ -933,7 +923,9 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (u->old_orders && is_repeated(kwd)) continue; /* unit has defaults */ if (is_persistent(ord)) { - stream_order(out, ord, lang); + swrite("\"", 1, 1, out); + stream_order(out, ord, lang, true); + swrite("\"\n", 1, 2, out); } } diff --git a/src/kernel/order.c b/src/kernel/order.c index 7d218d3ff..baf26f615 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -23,6 +23,9 @@ #include #include #include +#include + +#include /* libc includes */ #include @@ -135,6 +138,49 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si return sbuffer; } +int stream_order(struct stream *out, const struct order *ord, const struct locale *lang, bool escape) +{ + const char *str, *text; + order_data *od = NULL; + keyword_t kwd = ORD_KEYWORD(ord); + + if (ord->command & CMD_QUIET) { + swrite("!", 1, 1, out); + } + if (ord->command & CMD_PERSIST) { + swrite("@", 1, 1, out); + } + + if (ord->id < 0) { + skill_t sk = (skill_t)(100 + ord->id); + assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS); + text = skillname(sk, lang); + } + else { + od = odata_load(ord->id); + text = OD_STRING(od); + } + if (kwd != NOKEYWORD) { + str = (const char *)LOC(lang, keyword(kwd)); + assert(str); + swrite(str, 1, strlen(str), out); + } + + if (text) { + char obuf[1024]; + swrite(" ", 1, 1, out); + if (escape) { + text = escape_string(text, obuf, sizeof(obuf)); + } + swrite(text, 1, strlen(text), out); + } + if (od) { + odata_release(od); + } + + return 0; +} + void free_order(order * ord) { if (ord != NULL) { diff --git a/src/kernel/order.h b/src/kernel/order.h index 0cbe4989c..02e03ff56 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -22,6 +22,7 @@ extern "C" { #endif struct locale; + struct stream; /* Encapsulation of an order * @@ -69,6 +70,7 @@ extern "C" { char *write_order(const order * ord, const struct locale *lang, char *buffer, size_t size); + int stream_order(struct stream *out, const struct order *ord, const struct locale *lang, bool escape); keyword_t init_order_depr(const struct order *ord); keyword_t init_order(const struct order *ord, const struct locale *lang); From a51a145ba86fb227e398caab95baf54c59293ec0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 6 Dec 2017 20:06:46 +0100 Subject: [PATCH 08/25] make this loop a little more readable. warning: the break has no tests. --- src/steal.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/steal.c b/src/steal.c index 13df2c4ce..250681598 100644 --- a/src/steal.c +++ b/src/steal.c @@ -57,14 +57,21 @@ void expandstealing(region * r, econ_request * stealorders) if (!norders) return; /* F�r jede unit in der Region wird Geld geklaut, wenn sie Opfer eines - * Beklauen-Orders ist. Jedes Opfer mu� einzeln behandelt werden. + * Beklauen-Orders ist. Jedes Opfer muss einzeln behandelt werden. * * u ist die beklaute unit. oa.unit ist die klauende unit. */ - for (j = 0; j != norders && requests[j].unit->n <= requests[j].unit->wants; j++) { - unit *u = findunitg(requests[j].no, r); + for (j = 0; j != norders; j++) { + unit *u; int n = 0; + + if (requests[j].unit->n > requests[j].unit->wants) { + break; + } + + u = findunitg(requests[j].no, r); + if (u && u->region == r) { n = get_pooled(u, rsilver, GET_ALL, INT_MAX); } From 458841981493bfc4ffc261e4e5a7d326afd8fc4f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 6 Dec 2017 20:10:16 +0100 Subject: [PATCH 09/25] ignore difficult shellcheck fix --- process/create-orders | 1 + 1 file changed, 1 insertion(+) diff --git a/process/create-orders b/process/create-orders index 7016741ac..a72af4b00 100755 --- a/process/create-orders +++ b/process/create-orders @@ -16,6 +16,7 @@ else mv orders.dir "orders.dir.$TURN" mkdir -p orders.dir fi +#shellcheck disable=2011 ls -1rt "orders.dir.$TURN/turn-*" | xargs cat > "orders.$TURN" lockfile -r3 -l120 orders.queue.lock From 5e435a7c0bf1087e2e147209f2bf4739cb853545 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 19:26:26 +0100 Subject: [PATCH 10/25] pass cppcheck v 1.54 default checks --- s/travis-build | 11 +++++------ src/kernel/item.c | 10 ++++++---- src/kernel/pool.c | 4 ++-- src/kernel/unit.c | 11 +++++++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/s/travis-build b/s/travis-build index 2da3ed7b5..6b14e8848 100755 --- a/s/travis-build +++ b/s/travis-build @@ -19,13 +19,12 @@ cd tests set -e [ -z $BUILD ] && BUILD=Debug ; export BUILD s/cmake-init -s/build -cd $ROOT -inifile cppcheck --version cppcheck --quiet --error-exitcode=1 src +s/build +cd process +make +cd $ROOT +inifile s/runtests -V integration_tests -cd ../process -make - diff --git a/src/kernel/item.c b/src/kernel/item.c index 9d750cb2f..b2a35669a 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -746,11 +746,13 @@ void init_resources(void) int get_money(const unit * u) { const struct resource_type *rtype = get_resourcetype(R_SILVER); - const item *i = u->items; - while (i && i->type->rtype != rtype) { - i = i->next; + const item *i; + for (i = u->items; i; i = i->next) { + if (i->type->rtype == rtype) { + return i->number; + } } - return i ? i->number : 0; + return 0; } int set_money(unit * u, int v) diff --git a/src/kernel/pool.c b/src/kernel/pool.c index d85846f64..e58bc131e 100644 --- a/src/kernel/pool.c +++ b/src/kernel/pool.c @@ -114,7 +114,7 @@ int change_reservation(unit * u, const item_type * itype, int value) res->type = itype; res->value = value; } - else if (res && res->value + value <= 0) { + else if (res->value + value <= 0) { *rp = res->next; free(res); return 0; @@ -139,7 +139,7 @@ int set_resvalue(unit * u, const item_type * itype, int value) res->type = itype; res->value = value; } - else if (res && value <= 0) { + else if (value <= 0) { *rp = res->next; free(res); return 0; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 2e3dab804..cb42071b0 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -394,11 +394,14 @@ faction *dfindhash(int no) } #else struct faction *dfindhash(int no) { - unit *u = deleted_units; - while (u && u->no != no) { - u = u->next; + unit *u; + + for (u = deleted_units; u; u = u->next) { + if (u->no == no) { + return u->faction; + } } - return u ? u->faction : NULL; + return NULL; } #endif From 9163d166ecab81c1e27cec252523f02e2106fb3c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 19:59:49 +0100 Subject: [PATCH 11/25] cppcheck style fixes. delete dead functions. reactivate patzer_deathcloud. --- scripts/tests/e2/init.lua | 1 + scripts/tests/e2/production.lua | 26 +++++++++ scripts/tests/e2/ships.lua | 13 ++--- src/battle.c | 15 ------ src/kernel/alliance.c | 40 -------------- src/kernel/building.c | 11 ---- src/kernel/config.c | 9 ---- src/kernel/item.c | 21 -------- src/kernel/save.c | 41 --------------- src/kernel/unit.c | 4 -- src/move.c | 16 ------ src/spells.c | 93 ++------------------------------- src/spells/combatspells.c | 49 +---------------- src/sqlite.c | 11 ---- src/util/base36.c | 3 +- src/util/message.c | 4 +- 16 files changed, 45 insertions(+), 312 deletions(-) diff --git a/scripts/tests/e2/init.lua b/scripts/tests/e2/init.lua index fd2f0b423..222723082 100644 --- a/scripts/tests/e2/init.lua +++ b/scripts/tests/e2/init.lua @@ -10,6 +10,7 @@ require 'tests.e2.destroy' require 'tests.e2.guard' require 'tests.e2.stealth' require 'tests.e2.items' +-- require 'tests.e2.ships' require 'tests.items' require 'tests.economy' require 'tests.orders' diff --git a/scripts/tests/e2/production.lua b/scripts/tests/e2/production.lua index 1d31011af..5eae8ec74 100644 --- a/scripts/tests/e2/production.lua +++ b/scripts/tests/e2/production.lua @@ -76,3 +76,29 @@ function test_dwarf_mining_bonus() assert_equal(20, u:get_item('iron')) assert_equal(84, r:get_resource('iron')) end + +function test_build_boat_low_skill() + local r = region.create(0, 0, "plain") + local f = faction.create("human", "build@example.com") + local u = unit.create(f, r, 1) + u:set_skill("shipcraft", 3) -- humans get +1 + u:add_item("log", 10) + u:add_order("MACHE BOOT") + process_orders() + assert_not_equal(nil, u.ship) + assert_equal(4, u.ship.size) + assert_equal(6, u:get_item('log')) +end + +function test_build_boat_high_skill() + local r = region.create(0, 0, "plain") + local f = faction.create("human", "build@example.com") + local u = unit.create(f, r, 1) + u:set_skill("shipcraft", 5) -- humans get +1 + u:add_item("log", 10) + u:add_order("MACHE BOOT") + process_orders() + assert_not_equal(nil, u.ship) + assert_equal(5, u.ship.size) + assert_equal(5, u:get_item('log')) +end diff --git a/scripts/tests/e2/ships.lua b/scripts/tests/e2/ships.lua index 351609a92..0cb9f4131 100644 --- a/scripts/tests/e2/ships.lua +++ b/scripts/tests/e2/ships.lua @@ -10,10 +10,12 @@ end function test_ship_requires_skill() local r1 = region.create(0, 0, "ocean") + assert_not_nil(r1) local r2 = region.create(1, 0, "ocean") + assert_not_nil(r2) local f = faction.create("human", "fake@eressea.de", "de") local u1 = unit.create(f, r1, 1) - u1.name = "fake" + u1.name = "fake" u1.ship = ship.create(r1, "longboat") u1:clear_orders() u1:add_order("NACH O") @@ -22,21 +24,20 @@ function test_ship_requires_skill() assert_equal(r1, u1.region) end -function no_test_ship_happy_case() +function test_ship_happy_case() local r1 = region.create(0, 0, "ocean") local r2 = region.create(1, 0, "ocean") local f = faction.create("human", "hodor@eressea.de", "de") local u1 = unit.create(f, r1, 1) local u2 = unit.create(f, r1, 1) u1.ship = ship.create(r1, "longboat") - u2.ship = u1.ship + u2.ship = u1.ship u1:clear_orders() u1:add_order("NACH O") - u1:set_skill("sailing", 1) -- cptskill = 1 - u2:set_skill("sailing", 9) -- sumskill = 10 + u1:set_skill("sailing", 1) -- cptskill = 1 + u2:set_skill("sailing", 9) -- sumskill = 10 process_orders() assert_equal(r2, u1.ship.region) assert_equal(r2, u1.region) assert_equal(r2, u2.region) end - diff --git a/src/battle.c b/src/battle.c index 7f70eaa4a..3abde12b4 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2285,21 +2285,6 @@ void do_attack(fighter * af) } } -void do_regenerate(fighter * af) -{ - troop ta; - unit *au = af->unit; - - ta.fighter = af; - ta.index = af->fighting; - - while (ta.index--) { - struct person *p = af->person + ta.index; - p->hp += effskill(au, SK_STAMINA, 0); - p->hp = MIN(unit_max_hp(au), p->hp); - } -} - static void add_tactics(tactics * ta, fighter * fig, int value) { if (value == 0 || value < ta->value) diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index d93195e97..ac981479d 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -430,46 +430,6 @@ const char *alliancename(const alliance * al) return ibuf; } -void alliancevictory(void) -{ - const struct building_type *btype = bt_find("stronghold"); - region *r = regions; - alliance *al = alliances; - if (btype == NULL) - return; - while (r != NULL) { - building *b = r->buildings; - while (b != NULL) { - if (b->type == btype) { - unit *u = building_owner(b); - if (u) { - fset(u->faction->alliance, FFL_MARK); - } - } - b = b->next; - } - r = r->next; - } - while (al != NULL) { - if (!fval(al, FFL_MARK)) { - faction **fp; - for (fp = &factions; *fp; ) { - faction *f = *fp; - if (f->alliance == al) { - ADDMSG(&f->msgs, msg_message("alliance::lost", "alliance", al)); - destroyfaction(fp); - } else { - fp = &f->next; - } - } - } - else { - freset(al, FFL_MARK); - } - al = al->next; - } -} - void alliance_setname(alliance * self, const char *name) { free(self->name); diff --git a/src/kernel/building.c b/src/kernel/building.c index 57b8a91e2..7eee3b9e2 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -774,22 +774,11 @@ int cmp_wage(const struct building *b, const building * a) return -1; } -bool is_owner_building(const struct building * b) -{ - region *r = b->region; - if (b->type->taxes && r->land && r->land->ownership) { - unit *u = building_owner(b); - return u && u->faction == r->land->ownership->owner; - } - return false; -} - int building_taxes(const building *b) { assert(b); return b->type->taxes; } - int cmp_taxes(const building * b, const building * a) { faction *f = region_get_owner(b->region); diff --git a/src/kernel/config.c b/src/kernel/config.c index 24f86a978..1d3e6b6d2 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -260,15 +260,6 @@ param_t getparam(const struct locale * lang) return s ? findparam(s, lang) : NOPARAM; } -unit *getnewunit(const region * r, const faction * f) -{ - int n; - n = getid(); - - return findnewunit(r, f, n); -} - - /* -- Erschaffung neuer Einheiten ------------------------------ */ static const char *forbidden[] = { "t", "te", "tem", "temp", NULL }; diff --git a/src/kernel/item.c b/src/kernel/item.c index 9d750cb2f..94e7e0a54 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -631,27 +631,6 @@ int set_item(unit * u, const item_type *itype, int value) return value; } -/* t_item::flags */ -#define FL_ITEM_CURSED (1<<0) -#define FL_ITEM_NOTLOST (1<<1) -#define FL_ITEM_NOTINBAG (1<<2) /* nicht im Bag Of Holding */ -#define FL_ITEM_ANIMAL (1<<3) /* ist ein Tier */ -#define FL_ITEM_MOUNT ((1<<4) | FL_ITEM_ANIMAL) /* ist ein Reittier */ - -typedef struct t_item { - const char *name; - /* [0]: Einzahl fuer eigene; [1]: Mehrzahl fuer eigene; - * [2]: Einzahl fuer Fremde; [3]: Mehrzahl fuer Fremde */ - bool is_resource; - skill_t skill; - int minskill; - int gewicht; - int preis; - unsigned int flags; - void(*benutze_funktion) (struct region *, struct unit *, int amount, - struct order *); -} t_item; - #include "move.h" static int diff --git a/src/kernel/save.c b/src/kernel/save.c index 14e45aa60..386c7523c 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -123,50 +123,9 @@ char *rns(FILE * f, char *c, size_t size) return c; } -/* ------------------------------------------------------------- */ - -/* #define INNER_WORLD */ -/* fürs debuggen nur den inneren Teil der Welt laden */ -/* -9;-27;-1;-19;Sumpfloch */ -int inner_world(region * r) -{ - static int xy[2] = { 18, -45 }; - static int size[2] = { 27, 27 }; - - if (r->x >= xy[0] && r->x < xy[0] + size[0] && r->y >= xy[1] - && r->y < xy[1] + size[1]) - return 2; - if (r->x >= xy[0] - 9 && r->x < xy[0] + size[0] + 9 && r->y >= xy[1] - 9 - && r->y < xy[1] + size[1] + 9) - return 1; - return 0; -} - int maxregions = -1; int loadplane = 0; -enum { - U_MAN, - U_UNDEAD, - U_ILLUSION, - U_FIREDRAGON, - U_DRAGON, - U_WYRM, - U_SPELL, - U_TAVERNE, - U_MONSTER, - U_BIRTHDAYDRAGON, - U_TREEMAN, - MAXTYPES -}; - -race_t typus2race(unsigned char typus) -{ - if (typus > 0 && typus <= 11) - return (race_t)(typus - 1); - return NORACE; -} - static void read_alliances(gamedata *data) { storage *store = data->store; diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 2e3dab804..bda224c0f 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1929,10 +1929,6 @@ int read_unitid(const faction * f, const region * r) char token[16]; const char *s = gettoken(token, sizeof(token)); - /* Da s nun nur einen string enthaelt, suchen wir ihn direkt in der - * paramliste. machen wir das nicht, dann wird getnewunit in s nach der - * nummer suchen, doch dort steht bei temp-units nur "temp" drinnen! */ - if (!s || *s == 0 || !isalnum(*s)) { return -1; } diff --git a/src/move.c b/src/move.c index 904988759..01c5e94b2 100644 --- a/src/move.c +++ b/src/move.c @@ -2323,22 +2323,6 @@ int follow_ship(unit * u, order * ord) return 1; /* true -> Einheitenliste von vorne durchgehen */ } -void destroy_damaged_ships(void) -{ - region *r; - ship *sh, *shn; - - for (r = regions; r; r = r->next) { - for (sh = r->ships; sh;) { - shn = sh->next; - if (sh->damage >= sh->size * DAMAGE_SCALE) { - remove_ship(&sh->region->ships, sh); - } - sh = shn; - } - } -} - /* Bewegung, Verfolgung, Piraterie */ /** ships that folow other ships diff --git a/src/spells.c b/src/spells.c index c9e914904..74f466583 100644 --- a/src/spells.c +++ b/src/spells.c @@ -2358,7 +2358,6 @@ static int sp_earthquake(castorder * co) /* ------------------------------------------------------------- */ void patzer_peasantmob(const castorder * co) { - int anteil = 6, n; unit *u; attrib *a; region *r; @@ -2370,8 +2369,9 @@ void patzer_peasantmob(const castorder * co) faction *f = get_monsters(); const struct locale *lang = f->locale; message *msg; + int anteil, n; - anteil += rng_int() % 4; + anteil = 6 + rng_int() % 4; n = rpeasants(r) * anteil / 10; rsetpeasants(r, rpeasants(r) - n); assert(rpeasants(r) >= 0); @@ -2966,12 +2966,12 @@ static int sp_deathcloud(castorder * co) return co->level; } -void patzer_deathcloud(castorder * co) +static void patzer_deathcloud(const castorder * co) { unit *mage = co->magician.u; int hp = (mage->hp - 2); - change_hitpoints(mage, -rng_int() % hp); + change_hitpoints(mage, -(rng_int() % hp)); ADDMSG(&mage->faction->msgs, msg_message("magic_fumble", "unit region command", mage, mage->region, co->order)); @@ -6144,89 +6144,6 @@ int sp_speed2(castorder * co) return MAX(1, used); } -/* ------------------------------------------------------------- */ -/* Name: Magiefresser - * Stufe: 7 - * Kosten: SPC_LEVEL - * - * Wirkung: - * Kann eine bestimmte Verzauberung angreifen und aufloesen. Die Staerke - * des Zaubers muss staerker sein als die der Verzauberung. - * Syntax: - * ZAUBERE \"Magiefresser\" REGION - * ZAUBERE \"Magiefresser\" EINHEIT - * ZAUBERE \"Magiefresser\" GEBAEUDE - * ZAUBERE \"Magiefresser\" SCHIFF - * - * "kc?c" - * Flags: - * (FARCASTING | SPELLLEVEL | ONSHIPCAST | TESTCANSEE) - */ - /* Jeder gebrochene Zauber verbraucht c->vigour an Zauberkraft - * (force) */ -int sp_q_antimagie(castorder * co) -{ - attrib **ap; - int obj; - curse *c = NULL; - int succ; - region *r = co_get_region(co); - unit *mage = co->magician.u; - int cast_level = co->level; - double force = co->force; - spellparameter *pa = co->par; - const char *ts = NULL; - - obj = pa->param[0]->typ; - - switch (obj) { - case SPP_REGION: - ap = &r->attribs; - ts = regionname(r, mage->faction); - break; - - case SPP_TEMP: - case SPP_UNIT: - { - unit *u = pa->param[0]->data.u; - ap = &u->attribs; - ts = itoa36(u->no); - break; - } - case SPP_BUILDING: - { - building *b = pa->param[0]->data.b; - ap = &b->attribs; - ts = itoa36(b->no); - break; - } - case SPP_SHIP: - { - ship *sh = pa->param[0]->data.sh; - ap = &sh->attribs; - ts = itoa36(sh->no); - break; - } - default: - /* Das Zielobjekt wurde vergessen */ - cmistake(mage, co->order, 203, MSG_MAGIC); - return 0; - } - - succ = break_curse(ap, cast_level, force, c); - - if (succ) { - ADDMSG(&mage->faction->msgs, msg_message("destroy_magic_effect", - "unit region command succ target", mage, mage->region, co->order, succ, - ts)); - } - else { - ADDMSG(&mage->faction->msgs, msg_message("destroy_magic_noeffect", - "unit region command", mage, mage->region, co->order)); - } - return MAX(succ, 1); -} - /* ------------------------------------------------------------- */ /* Name: Fluch brechen * Stufe: 7 @@ -6541,7 +6458,7 @@ static spelldata spell_functions[] = { { "forestfire", sp_forest_fire, patzer_peasantmob }, { "draigdestroymagic", sp_destroy_magic, 0 }, { "unholypower", sp_unholypower, 0 }, - { "deathcloud", sp_deathcloud, patzer_peasantmob }, + { "deathcloud", sp_deathcloud, patzer_deathcloud }, { "summondragon", sp_summondragon, patzer_peasantmob }, { "summonshadowlords", sp_summonshadowlords, patzer_peasantmob }, { "chaossuction", sp_chaossuction, patzer_peasantmob }, diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 8eb240cf7..79bcd52d4 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -622,7 +622,6 @@ int sp_dragonodem(struct castorder * co) troop dt; troop at; int force, enemies; - int killed = 0; const char *damage; /* 11-26 HP */ @@ -641,6 +640,7 @@ int sp_dragonodem(struct castorder * co) } else { struct message *m; + int killed = 0; at.fighter = fi; at.index = 0; @@ -717,53 +717,6 @@ int sp_immolation(struct castorder * co) return level; } -int sp_drainodem(fighter * fi, int level, double power, spell * sp) -{ - battle *b = fi->side->battle; - troop dt; - troop at; - int force, enemies; - int drained = 0; - int killed = 0; - const char *damage; - message *m; - - /* 11-26 HP */ - damage = spell_damage(4); - /* Jungdrache 3->54, Drache 6->216, Wyrm 12->864 Treffer */ - force = lovar(get_force(power, 6)); - - enemies = count_enemies(b, fi, FIGHT_ROW, BEHIND_ROW - 1, SELECT_ADVANCE); - - if (!enemies) { - m = msg_message("spell_out_of_range", "mage spell", fi->unit, sp); - message_all(b, m); - msg_release(m); - return 0; - } - - at.fighter = fi; - at.index = 0; - - while (force && drained < enemies) { - dt = select_enemy(fi, FIGHT_ROW, BEHIND_ROW - 1, SELECT_ADVANCE); - assert(dt.fighter); - if (hits(at, dt, NULL)) { - drain_exp(dt.fighter->unit, 90); - ++drained; - killed += terminate(dt, at, AT_COMBATSPELL, damage, false); - } - --force; - } - - m = - msg_message("cast_drainlife_effect", "mage spell amount", fi->unit, sp, - drained); - message_all(b, m); - msg_release(m); - return level; -} - /* ------------------------------------------------------------- */ /* PRECOMBAT */ diff --git a/src/sqlite.c b/src/sqlite.c index d05d9d1e3..f0464cff0 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -12,17 +12,6 @@ #include #include -faction *get_faction_by_id(int uid) -{ - faction *f; - for (f = factions; f; f = f->next) { - if (f->subscription == uid) { - return f; - } - } - return NULL; -} - typedef struct db_faction { int uid; int no; diff --git a/src/util/base36.c b/src/util/base36.c index 285e9760e..221ece870 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -57,13 +57,14 @@ const char *itoab(int i, int base) static char sstr[80]; char *s, *dst; static int index = 0; /* STATIC_XCALL: used across calls */ - int neg = 0; s = sstr + (index * 20); index = (index + 1) & 3; /* quick for % 4 */ dst = s + 19; (*dst--) = 0; if (i != 0) { + int neg = 0; + if (i < 0) { i = -i; neg = 1; diff --git a/src/util/message.c b/src/util/message.c index 9056ee139..e53010093 100644 --- a/src/util/message.c +++ b/src/util/message.c @@ -60,7 +60,7 @@ arg_type *find_argtype(const char *name) message_type *mt_new(const char *name, const char *args[]) { - int i, nparameters = 0; + int nparameters = 0; message_type *mtype; assert(name != NULL); @@ -85,6 +85,8 @@ message_type *mt_new(const char *name, const char *args[]) mtype->types = NULL; } if (args != NULL) { + int i; + for (i = 0; args[i]; ++i) { const char *x = args[i]; const char *spos = strchr(x, ':'); From 1b9a686101057bab275900510d9091c8070228fb Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 20:17:40 +0100 Subject: [PATCH 12/25] fix berkeley size/ulen error remove weather module (unused) --- conf/eressea.ini | 1 - src/battle.c | 3 +- src/kernel/db/berkeley.c | 6 +- src/modules/CMakeLists.txt | 1 - src/modules/weather.c | 132 ------------------------------------- src/modules/weather.h | 54 --------------- src/triggers/shock.c | 2 +- 7 files changed, 6 insertions(+), 193 deletions(-) delete mode 100644 src/modules/weather.c delete mode 100644 src/modules/weather.h diff --git a/conf/eressea.ini b/conf/eressea.ini index aed9c219c..17547f8f9 100644 --- a/conf/eressea.ini +++ b/conf/eressea.ini @@ -4,7 +4,6 @@ sender = Eressea Server name = Eressea report = reports verbose = 0 -lomem = 0 memcheck = 0 locales = de,en diff --git a/src/battle.c b/src/battle.c index 7f70eaa4a..602fd2ca2 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1009,7 +1009,7 @@ int natural_armor(unit * du) static int rc_specialdamage(const unit *au, const unit *du, const struct weapon_type *wtype) { const race *ar = u_race(au); - int m, modifier = 0; + int modifier = 0; if (wtype != NULL) { if (fval(u_race(du), RCF_DRAGON)) { static int cache; @@ -1022,6 +1022,7 @@ static int rc_specialdamage(const unit *au, const unit *du, const struct weapon_ } } if (wtype->modifiers != NULL) { + int m; for (m = 0; wtype->modifiers[m].value; ++m) { /* weapon damage for this weapon, possibly by race */ if (wtype->modifiers[m].flags & WMF_DAMAGE) { diff --git a/src/kernel/db/berkeley.c b/src/kernel/db/berkeley.c index 5a65ee3b0..c16a6d13a 100644 --- a/src/kernel/db/berkeley.c +++ b/src/kernel/db/berkeley.c @@ -41,10 +41,10 @@ int db_driver_order_save(struct order_data *od) assert(od && od->_str); key.data = &recno; - key.size = sizeof(recno); + key.size = key.ulen = sizeof(recno); key.flags = DB_DBT_USERMEM; data.data = (void *)od->_str; - data.size = strlen(od->_str) + 1; + data.size = data.ulen = strlen(od->_str) + 1; data.flags = DB_DBT_USERMEM; ret = g_dbp->put(g_dbp, NULL, &key, &data, DB_APPEND); assert(ret == 0); @@ -63,7 +63,7 @@ struct order_data *db_driver_order_load(int id) memset(&data, 0, sizeof(DBT)); recno = (db_recno_t)id; key.data = &recno; - key.size = sizeof(recno); + key.size = key.ulen = sizeof(recno); key.flags = DB_DBT_USERMEM; ret = g_dbp->get(g_dbp, NULL, &key, &data, 0); if (ret == 0) { diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 82dee9ec4..5b98897d4 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -4,7 +4,6 @@ autoseed.c gmcmd.c museum.c score.c -weather.c xmas.c ) FOREACH(_FILE ${_FILES}) diff --git a/src/modules/weather.c b/src/modules/weather.c deleted file mode 100644 index ef71197b0..000000000 --- a/src/modules/weather.c +++ /dev/null @@ -1,132 +0,0 @@ -/* -Copyright (c) 1998-2015, Enno Rehling -Katja Zedel - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -**/ - -#ifdef WEATHER - -#include -#include -#include "weather.h" - -/* libc includes */ -#include - -weather *create_weather(region * r, weather_t type) -{ - weather *w; - - w = calloc(1, sizeof(weather)); - w->center[0] = r->x; - w->center[1] = r->y; - w->type = type; - w->move[0] = (rng_int() % 3) - 1; - w->move[1] = (rng_int() % 3) - 1; - - switch (type) { - case WEATHER_STORM: - w->radius = rng_int() % 2 + 1; - break; - case WEATHER_HURRICANE: - w->radius = 1; - break; - default: - w->radius = 0; - } - - addlist(&weathers, w); - - return w; -} - -double distance(int x1, int y1, int x2, int y2) -{ - return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); -} - -/* Diese Funktion ermittelt für jede Region, welches Wetter in ihr - herrscht. Die Wettertypen sind dabei nach ihrer Reihenfolge in der - enumeration priorisiert. - - - Einladen - set_weather(); - - Eigentliche Auswertung - - Veränderungen des Wetters - set_weather(); - - Report generieren - - Abspeichern - - Diese Routine ist sehr rechenaufwendig! - */ - -void set_weather(void) -{ - weather_t i; - weather *w; - short x, y; - int d; - region *r; - - for (r = regions; r; r = r->next) { - r->weathertype = WEATHER_NONE; - } - - for (i = 0; i < MAXWEATHERS; i++) { - for (w = weathers; w; w = w->next) { - if (w->type == i) { - for (x = w->center[0] - w->radius; x <= w->center[0] + w->radius; x++) { - for (y = w->center[1] - w->radius; y <= w->center[1] + w->radius; y++) { - d = distance(w->center[0], w->center[1], x, y); - if (floor(d + 0.5) <= w->radius) { - r = findregion(x, y); - if (r) { - r->weathertype = w->type; - } - } - } - } - } - } - } -} - -void move_weather(void) -{ - weather *w, *wnext; - region *r; - - for (w = weathers; w;) { - wnext = w->next; - w->center[0] = w->center[0] + w->move[0]; - w->center[1] = w->center[1] + w->move[1]; - r = findregion(w->center[0], w->center[1]); - if (!r || rng_int() % 100 < 5) { - removelist(&weathers, w); - } - w = wnext; - } -} - -#else -#include -static const char *copyright = "(c) Eressea PBEM 2000"; - -void init_weather(void) -{ - fputs(copyright, stderr); - /* TODO: Initialization */ -} -#endif diff --git a/src/modules/weather.h b/src/modules/weather.h deleted file mode 100644 index 812f31ee7..000000000 --- a/src/modules/weather.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright (c) 1998-2015, Enno Rehling - Katja Zedel - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -**/ - -#ifndef H_MOD_WEATHER_H -#define H_MOD_WEATHER_H -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef WEATHER -# error "the weather system is disabled" -#endif - - enum { - WEATHER_NONE, - WEATHER_STORM, - WEATHER_HURRICANE, - MAXWEATHERS - }; - - typedef unsigned char weather_t; - - typedef struct weather { - struct weather *next; - weather_t type; /* Typ der Wetterzone */ - int center[2]; /* Koordinaten des Zentrums */ - int radius; - int move[2]; - } weather; - - weather *weathers; - - void set_weather(void); - void move_weather(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/triggers/shock.c b/src/triggers/shock.c index ca4ad67df..690a115df 100644 --- a/src/triggers/shock.c +++ b/src/triggers/shock.c @@ -19,7 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "shock.h" -#include "magic.h" +#include /* kernel includes */ #include From 26c31708dc59cf5d99598f64de4c516cfbf7b4b7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 20:27:43 +0100 Subject: [PATCH 13/25] delete dead code. --- src/kernel/resources.c | 24 ------------------------ src/kernel/save.c | 38 +++----------------------------------- src/move.c | 5 ++--- src/spells.c | 22 ---------------------- 4 files changed, 5 insertions(+), 84 deletions(-) diff --git a/src/kernel/resources.c b/src/kernel/resources.c index 842cb83ba..2f99315e3 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -124,30 +124,6 @@ static void terraform_default(struct rawmaterial *res, const region * r) UNUSED_ARG(r); } -#ifdef RANDOM_CHANGE -static void resource_random_change(int *pvalue, bool used) -{ - int split = 5; - int rnd = rng_int() % 100; - - if (pvalue == 0 || rnd % 10 >= 10) - return; - if (used) - split = 4; - /* if a resource was mined this round, there is a 6% probability - * of a decline and a 4% probability of a raise. */ - /* if it wasn't mined this round, there is an equal probability - * of 5% for a decline or a raise. */ - if (rnd < split) { - (*pvalue)++; - } else { - (*pvalue)--; - } - if ((*pvalue) < 0) - (*pvalue) = 0; -} -#endif - static int visible_default(const rawmaterial * res, int skilllevel) /* resources are visible, if skill equals minimum skill to mine them * plus current level of difficulty */ diff --git a/src/kernel/save.c b/src/kernel/save.c index 386c7523c..ba7692806 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -102,30 +102,6 @@ int firstx = 0, firsty = 0; /* TODO: is this still important? */ int enc_gamedata = ENCODING_UTF8; -/* local symbols */ -static region *current_region; - -char *rns(FILE * f, char *c, size_t size) -{ - char *s = c; - do { - *s = (char)getc(f); - } while (*s != '"'); - - for (;;) { - *s = (char)getc(f); - if (*s == '"') - break; - if (s < c + size) - ++s; - } - *s = 0; - return c; -} - -int maxregions = -1; -int loadplane = 0; - static void read_alliances(gamedata *data) { storage *store = data->store; @@ -635,7 +611,6 @@ static region *readregion(gamedata *data, int x, int y) } else { assert(uid == 0 || r->uid == uid); - current_region = r; while (r->attribs) a_remove(&r->attribs, r->attribs); if (r->land) { @@ -1399,7 +1374,6 @@ int read_game(gamedata *data) building **bp; ship **shp; unit *u; - int rmax = maxregions; storage * store = data->store; const struct building_type *bt_lighthouse = bt_find("lighthouse"); const struct race *rc_spell = rc_find("spell"); @@ -1443,11 +1417,9 @@ int read_game(gamedata *data) /* Regionen */ READ_INT(store, &nread); - assert(nread < MAXREGIONS && nread>=0); - if (rmax < 0) { - rmax = nread; - } - log_debug(" - Einzulesende Regionen: %d/%d", rmax, nread); + assert(nread < MAXREGIONS && nread >=0); + + log_debug(" - Einzulesende Regionen: %d", nread); while (--nread >= 0) { unit **up; @@ -1510,7 +1482,6 @@ int read_game(gamedata *data) update_interval(u->faction, r); } } - --rmax; } read_borders(data); @@ -1572,9 +1543,6 @@ int read_game(gamedata *data) fix_familiars(); } - if (loadplane || maxregions >= 0) { - remove_empty_factions(); - } log_debug("Done loading turn %d.", turn); return 0; diff --git a/src/move.c b/src/move.c index 01c5e94b2..289d7a421 100644 --- a/src/move.c +++ b/src/move.c @@ -1764,10 +1764,9 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) } if (!flying_ship(sh)) { - int stormchance = 0; - int reason; if (storms_enabled) { - int stormyness; + int stormchance = 0; + int stormyness, reason; gamedate date; get_gamedate(turn, &date); stormyness = storms ? storms[date.month] * 5 : 0; diff --git a/src/spells.c b/src/spells.c index 74f466583..833061b0a 100644 --- a/src/spells.c +++ b/src/spells.c @@ -4527,28 +4527,6 @@ int sp_illusionary_shapeshift(castorder * co) return cast_level; } -/* ------------------------------------------------------------- */ -/* Name: Regionstraum analysieren - * Stufe: 9 - * Aura: 18 - * Kosten: SPC_FIX - * Wirkung: - * Zeigt die Verzauberungen eines Objekts an (curse->name, - * curse::info). Aus der Differenz Spruchstaerke und Curse->vigour - * ergibt sich die Chance den Spruch zu identifizieren ((force - - * c->vigour)*10 + 100 %). - */ -int sp_analyseregionsdream(castorder * co) -{ - region *r = co_get_region(co); - unit *mage = co->magician.u; - int cast_level = co->level; - - magicanalyse_region(r, mage, cast_level); - - return cast_level; -} - /* ------------------------------------------------------------- */ /* Name: Traumbilder erkennen * Stufe: 5 From 17ee0e50b6375f4b9e92605810d10fba3612059e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 21:08:11 +0100 Subject: [PATCH 14/25] cppcheck warnings and scope reductions. --- src/bindings.c | 12 +++--------- src/economy.c | 6 ++---- src/json.test.c | 15 +++++++++------ src/kernel/alliance.c | 2 +- src/kernel/building.c | 2 +- src/kernel/item.c | 2 +- src/kernel/save.c | 2 +- src/kernel/ship.c | 2 +- src/kernel/unit.c | 5 ++--- src/kernel/version.c | 2 +- src/kernel/xmlreader.c | 9 +++++---- src/laws.c | 21 +++++++++++---------- src/modules/autoseed.c | 2 +- src/move.c | 3 ++- src/util/attrib.c | 2 +- src/util/lists.c | 10 ---------- src/util/lists.h | 6 ------ 17 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/bindings.c b/src/bindings.c index 7a24a3d07..14f0c96f3 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -710,7 +710,6 @@ static int config_get_resource(lua_State * L) lua_settable(L, -3); } if (itype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, itype->construction->minskill); lua_settable(L, -3); @@ -718,6 +717,7 @@ static int config_get_resource(lua_State * L) lua_pushstring(L, skillnames[itype->construction->skill]); lua_settable(L, -3); if (itype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; itype->construction->materials[i].number; ++i) { @@ -774,7 +774,6 @@ static int config_get_btype(lua_State * L) lua_settable(L, -3); } if (btype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, btype->construction->minskill); lua_settable(L, -3); @@ -782,6 +781,7 @@ static int config_get_btype(lua_State * L) lua_pushstring(L, skillnames[btype->construction->skill]); lua_settable(L, -3); if (btype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; btype->construction->materials[i].number; ++i) { @@ -840,7 +840,6 @@ static int config_get_stype(lua_State * L) } } if (stype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, stype->construction->minskill); lua_settable(L, -3); @@ -848,6 +847,7 @@ static int config_get_stype(lua_State * L) lua_pushstring(L, skillnames[stype->construction->skill]); lua_settable(L, -3); if (stype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; stype->construction->materials[i].number; ++i) { @@ -922,12 +922,6 @@ int tolua_read_xml(lua_State * L) return 1; } -typedef struct event_args { - int hfunction; - int hargs; - const char *sendertype; -} event_args; - static int tolua_report_unit(lua_State * L) { char buffer[512]; diff --git a/src/economy.c b/src/economy.c index 55e0fafb1..759d60cb8 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1763,8 +1763,6 @@ static void expandselling(region * r, econ_request * sellorders, int limit) price = ltype->price * multi; if (money >= price) { - int abgezogenhafen = 0; - int abgezogensteuer = 0; unit *u = g_requests[j].unit; item *itm; attrib *a = a_find(u->attribs, &at_luxuries); @@ -1779,7 +1777,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit) if (hafenowner != NULL) { if (hafenowner->faction != u->faction) { - abgezogenhafen = price / 10; + int abgezogenhafen = price / 10; hafencollected += abgezogenhafen; price -= abgezogenhafen; money -= abgezogenhafen; @@ -1787,7 +1785,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit) } if (maxb != NULL) { if (maxowner->faction != u->faction) { - abgezogensteuer = price * tax_per_size[maxeffsize] / 100; + int abgezogensteuer = price * tax_per_size[maxeffsize] / 100; taxcollected += abgezogensteuer; price -= abgezogensteuer; money -= abgezogensteuer; diff --git a/src/json.test.c b/src/json.test.c index 57cb6e136..74b104329 100644 --- a/src/json.test.c +++ b/src/json.test.c @@ -1,19 +1,22 @@ #include -#include -#include -#include - -#include -#include #include "json.h" #include "tests.h" +#include +#include + +#include +#include + #include +#include + #include #include #include + static char *strip(char *str) { char *s = str, *b = str, *e = str; /* b is where text begins, e where it ends, s where we insert it. */ diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index ac981479d..f0cdf3bbd 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -422,7 +422,7 @@ const char *alliancename(const alliance * al) char *ibuf = idbuf[(++nextbuf) % 8]; if (al && al->name) { - slprintf(ibuf, sizeof(name), "%s (%s)", al->name, itoa36(al->id)); + slprintf(ibuf, sizeof(idbuf[0]), "%s (%s)", al->name, itoa36(al->id)); } else { return NULL; diff --git a/src/kernel/building.c b/src/kernel/building.c index 7eee3b9e2..ea01fe665 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -499,7 +499,7 @@ const char *buildingname(const building * b) static name idbuf[8]; static int nextbuf = 0; char *ibuf = idbuf[(++nextbuf) % 8]; - return write_buildingname(b, ibuf, sizeof(name)); + return write_buildingname(b, ibuf, sizeof(idbuf[0])); } void building_set_owner(struct unit * owner) diff --git a/src/kernel/item.c b/src/kernel/item.c index ac6875330..a633d5463 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -596,7 +596,7 @@ const resource_type *get_resourcetype(resource_t type) { static struct resource_type * rtypes[MAX_RESOURCES]; const resource_type *rtype = NULL; if (update != num_resources) { - memset(rtypes, 0, sizeof(rtypes)); + memset(rtypes, 0, sizeof(resource_type *) * MAX_RESOURCES); update = num_resources; } else { diff --git a/src/kernel/save.c b/src/kernel/save.c index ba7692806..a044601aa 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -301,7 +301,7 @@ int current_turn(void) perror(zText); } else { - int c = fscanf(F, "%d\n", &cturn); + int c = fscanf(F, "%4d\n", &cturn); fclose(F); if (c != 1) { return -1; diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 764500b50..a55eada2e 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -386,7 +386,7 @@ const char *shipname(const ship * sh) static name idbuf[8]; static int nextbuf = 0; char *ibuf = idbuf[(++nextbuf) % 8]; - return write_shipname(sh, ibuf, sizeof(name)); + return write_shipname(sh, ibuf, sizeof(idbuf[0])); } int shipcapacity(const ship * sh) diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 07d1f5236..bc776befa 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1711,7 +1711,6 @@ void unit_addorder(unit * u, order * ord) int unit_max_hp(const unit * u) { int h; - double p; static int config; static bool rule_stamina; h = u_race(u)->hitpoints; @@ -1720,7 +1719,7 @@ int unit_max_hp(const unit * u) rule_stamina = config_get_int("rules.stamina", 1)!=0; } if (rule_stamina) { - p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2; + double p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2; h += (int)(h * p + 0.5); } @@ -1890,7 +1889,7 @@ char *write_unitname(const unit * u, char *buffer, size_t size) const char *unitname(const unit * u) { char *ubuf = idbuf[(++nextbuf) % 8]; - return write_unitname(u, ubuf, sizeof(name)); + return write_unitname(u, ubuf, sizeof(idbuf[0])); } bool unit_name_equals_race(const unit *u) { diff --git a/src/kernel/version.c b/src/kernel/version.c index c7d757773..c0dc8d2bc 100644 --- a/src/kernel/version.c +++ b/src/kernel/version.c @@ -18,6 +18,6 @@ const char *eressea_version(void) { int version_no(const char *str) { int maj = 0, min = 0, pat = 0; - sscanf(str, "%d.%d.%d", &maj, &min, &pat); + sscanf(str, "%4d.%4d.%4d", &maj, &min, &pat); return (maj << 16) | (min << 8) | pat; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 92112ddb6..051bc0999 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -284,13 +284,13 @@ static int parse_buildings(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathObjectPtr buildings; - int i; /* reading eressea/buildings/building */ buildings = xmlXPathEvalExpression(BAD_CAST "/eressea/buildings/building", xpath); if (buildings->nodesetval != NULL) { xmlNodeSetPtr nodes = buildings->nodesetval; + int i; for (i = 0; i != nodes->nodeNr; ++i) { xmlNodePtr node = nodes->nodeTab[i]; xmlChar *propValue; @@ -396,12 +396,12 @@ static int parse_ships(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathObjectPtr ships; - int i; /* reading eressea/ships/ship */ ships = xmlXPathEvalExpression(BAD_CAST "/eressea/ships/ship", xpath); if (ships->nodesetval != NULL) { xmlNodeSetPtr nodes = ships->nodesetval; + int i; for (i = 0; i != nodes->nodeNr; ++i) { xmlNodePtr child, node = nodes->nodeTab[i]; xmlChar *propValue; @@ -810,7 +810,6 @@ static int parse_resources(xmlDocPtr doc) resource_type *rtype; unsigned int flags = RTF_NONE; xmlXPathObjectPtr result; - int k; if (xml_bvalue(node, "pooled", true)) flags |= RTF_POOLED; @@ -828,7 +827,8 @@ static int parse_resources(xmlDocPtr doc) /* reading eressea/resources/resource/function */ xpath->node = node; result = xmlXPathEvalExpression(BAD_CAST "function", xpath); - if (result->nodesetval != NULL) + if (result->nodesetval != NULL) { + int k; for (k = 0; k != result->nodesetval->nodeNr; ++k) { xmlNodePtr node = result->nodesetval->nodeTab[k]; pf_generic fun; @@ -852,6 +852,7 @@ static int parse_resources(xmlDocPtr doc) } xmlFree(propValue); } + } xmlXPathFreeObject(result); if (xml_bvalue(node, "material", false)) { diff --git a/src/laws.c b/src/laws.c index 72b7c4f38..f03f76730 100644 --- a/src/laws.c +++ b/src/laws.c @@ -433,14 +433,15 @@ static void horses(region * r) rsethorses(r, (int)(horses * 0.9)); } else if (maxhorses) { - int i; double growth = (RESOURCE_QUANTITY * HORSEGROWTH * 200 * (maxhorses - horses)) / maxhorses; if (growth > 0) { - if (a_find(r->attribs, &at_horseluck)) + int i; + if (a_find(r->attribs, &at_horseluck)) { growth *= 2; + } /* printf("Horses: <%d> %d -> ", growth, horses); */ i = (int)(0.5 + (horses * 0.0001) * growth); /* printf("%d\n", horses); */ @@ -562,13 +563,13 @@ growing_trees_e3(region * r, const int current_season, static void growing_trees(region * r, const int current_season, const int last_weeks_season) { - int growth, grownup_trees, i, seeds, sprout; - direction_t d; + int grownup_trees, i, seeds, sprout; attrib *a; if (current_season == SEASON_SUMMER || current_season == SEASON_AUTUMN) { double seedchance = 0.01F * RESOURCE_QUANTITY; int 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) { @@ -638,6 +639,7 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) } else if (current_season == SEASON_SPRING) { + int growth; if (is_cursed(r->attribs, &ct_godcursezone)) return; @@ -2153,7 +2155,6 @@ int email_cmd(unit * u, struct order *ord) int password_cmd(unit * u, struct order *ord) { char pwbuf[32]; - int i; const char *s; bool pwok = true; @@ -2161,6 +2162,7 @@ int password_cmd(unit * u, struct order *ord) s = gettoken(pwbuf, sizeof(pwbuf)); if (!s || !*s) { + int i; for (i = 0; i < 6; i++) pwbuf[i] = (char)(97 + rng_int() % 26); pwbuf[6] = 0; @@ -4272,7 +4274,7 @@ void update_subscriptions(void) int subscription, fno; faction *f; - if (fscanf(F, "%d %4s", &subscription, zFaction) <= 0) + if (fscanf(F, "%4d %4s", &subscription, zFaction) <= 0) break; fno = atoi36(zFaction); f = findfaction(fno); @@ -4358,7 +4360,7 @@ bool cansee_unit(const unit * u, const unit * target, int modifier) else if (target->faction == u->faction) return true; else { - int n, rings, o; + int n, rings; if (is_guard(target) || usiege(target) || target->building || target->ship) { @@ -4375,7 +4377,7 @@ bool cansee_unit(const unit * u, const unit * target, int modifier) return false; } if (skill_enabled(SK_PERCEPTION)) { - o = effskill(u, SK_PERCEPTION, target->region); + int o = effskill(u, SK_PERCEPTION, target->region); if (o >= n) { return true; } @@ -4394,7 +4396,6 @@ cansee_durchgezogen(const faction * f, const region * r, const unit * u, /* und es muss niemand aus f in der region sein, wenn sie vom Turm * erblickt wird */ { - int n; unit *u2; if (fval(u_race(u), RCF_INVISIBLE) || u->number == 0) @@ -4402,7 +4403,7 @@ cansee_durchgezogen(const faction * f, const region * r, const unit * u, else if (u->faction == f) return true; else { - int rings; + int rings, n; if (is_guard(u) || usiege(u) || u->building || u->ship) { return true; diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 24153870a..1e877459e 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -155,7 +155,7 @@ newfaction *read_newfactions(const char *filename) email[0] = '\0'; password[0] = '\0'; - if (sscanf(buf, "%54s %19s %7s %15s %d %d", email, race, lang, + if (sscanf(buf, "%54s %19s %7s %15s %4d %4d", email, race, lang, password, &subscription, &alliance) < 3) { break; } diff --git a/src/move.c b/src/move.c index 289d7a421..798c3da27 100644 --- a/src/move.c +++ b/src/move.c @@ -1764,9 +1764,10 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) } if (!flying_ship(sh)) { + int reason; if (storms_enabled) { int stormchance = 0; - int stormyness, reason; + int stormyness; gamedate date; get_gamedate(turn, &date); stormyness = storms ? storms[date.month] * 5 : 0; diff --git a/src/util/attrib.c b/src/util/attrib.c index 10ec660f8..9ef03ca55 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -537,5 +537,5 @@ void a_write_orig(struct storage *store, const attrib * attribs, const void *own void attrib_done(void) { cb_clear(&cb_deprecated); - memset(at_hash, 0, sizeof at_hash); + memset(at_hash, 0, sizeof(at_hash[0]) * MAXATHASH); } diff --git a/src/util/lists.c b/src/util/lists.c index be3deeffd..98dac3711 100644 --- a/src/util/lists.c +++ b/src/util/lists.c @@ -76,16 +76,6 @@ void translist(void *l1, void *l2, void *p) addlist(l2, p); } -void insertlist(void_list ** l, void_list * p) -{ - - /* insert entry p at the beginning of list l */ - - p->next = *l; - *l = p; - -} - void removelist(void *l, void *p) { diff --git a/src/util/lists.h b/src/util/lists.h index 8b9b093df..75920c5d4 100644 --- a/src/util/lists.h +++ b/src/util/lists.h @@ -33,14 +33,8 @@ extern "C" { void freestrlist(strlist * s); void addlist(void *l1, void *p1); void translist(void *l1, void *l2, void *p); -#ifndef MALLOCDBG void freelist(void *p1); void removelist(void *l, void *p); -#else -#define freelist(p) { while (p) { void * p2 = p->next; free(p); p = p2; } } -#define removelist(l,p) { choplist(l, p); free(p); } -#endif - unsigned int listlen(void *l); #ifdef __cplusplus From 5ad66d6b7adb38f22c69a96e922d09995f94fabc Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 21:08:45 +0100 Subject: [PATCH 15/25] cppcheck style fixes --- src/kernel/faction.c | 2 +- src/move.c | 1 + src/spells.c | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 00b62627f..8da74e80e 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -195,7 +195,7 @@ const char *factionname(const faction * f) char *ibuf = idbuf[(++nextbuf) % 8]; if (f && f->name) { - slprintf(ibuf, sizeof(name), "%s (%s)", f->name, itoa36(f->no)); + slprintf(ibuf, sizeof(idbuf[0]), "%s (%s)", f->name, itoa36(f->no)); } else { strcpy(ibuf, "Unbekannte Partei (?)"); diff --git a/src/move.c b/src/move.c index 798c3da27..65c047530 100644 --- a/src/move.c +++ b/src/move.c @@ -1751,6 +1751,7 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) * befahrene Region. */ while (next_point && current_point != next_point && step < k) { + int reason; const terrain_type *tthis = current_point->terrain; /* these values need to be updated if next_point changes (due to storms): */ const terrain_type *tnext = next_point->terrain; diff --git a/src/spells.c b/src/spells.c index 833061b0a..7f21e8654 100644 --- a/src/spells.c +++ b/src/spells.c @@ -5541,8 +5541,6 @@ int sp_showastral(castorder * co) free_regionlist(rl); return cast_level; - UNUSED_ARG(co); - return 0; } #endif From 0c1562c14f437577e7689f2303302964a0d5121e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 21:11:54 +0100 Subject: [PATCH 16/25] add include directories for cppcheck --- s/travis-build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s/travis-build b/s/travis-build index 6b14e8848..9ef0fbdd5 100755 --- a/s/travis-build +++ b/s/travis-build @@ -20,7 +20,7 @@ set -e [ -z $BUILD ] && BUILD=Debug ; export BUILD s/cmake-init cppcheck --version -cppcheck --quiet --error-exitcode=1 src +cppcheck --quiet -Isrc -Iclibs -Istorage -IcJSON --error-exitcode=1 src s/build cd process make From d7403801526d774c9779cf4b9423c25783078f6c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 09:40:17 +0100 Subject: [PATCH 17/25] unused variable, merge bug --- src/move.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/move.c b/src/move.c index 65c047530..798c3da27 100644 --- a/src/move.c +++ b/src/move.c @@ -1751,7 +1751,6 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) * befahrene Region. */ while (next_point && current_point != next_point && step < k) { - int reason; const terrain_type *tthis = current_point->terrain; /* these values need to be updated if next_point changes (due to storms): */ const terrain_type *tnext = next_point->terrain; From 6c18604bce49c98ad90f39de62842270155958f6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 10:24:31 +0100 Subject: [PATCH 18/25] Failing acceptance test for bug 2391. --- scripts/tests/e2/spells.lua | 23 +++++++++++++++++++++++ src/bind_unit.c | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/scripts/tests/e2/spells.lua b/scripts/tests/e2/spells.lua index fc1e3bc3b..fdc23eb94 100644 --- a/scripts/tests/e2/spells.lua +++ b/scripts/tests/e2/spells.lua @@ -9,6 +9,7 @@ function setup() eressea.settings.set("NewbieImmunity", "0") eressea.settings.set("rules.food.flags", "4") eressea.settings.set("rules.peasants.growth.factor", "0") + eressea.settings.set("magic.fumble.enable", "0") end function test_shapeshift() @@ -102,3 +103,25 @@ function test_earn_silver() assert_equal(350, u:get_item("money")) assert_equal(0, r:get_resource("money")) end + +function test_appeasement() + local u1, u2, r1, r2, uno + r1 = region.create(0, 0, 'plain') + r2 = region.create(1, 0, 'plain') + u2 = unit.create(faction.create('human'), r1, 1) + u2.name = 'Angsthase' + u2.magic = 'gwyrrd' + u2:add_spell('appeasement') + u2:set_skill('magic', 5) + u2:add_order('NACH O') + u2:add_order('KAMPFZAUBER STUFE 1 Friedenslied') + u2.aura = 10 + uno = u2.id + u1 = unit.create(faction.create('human'), r1, 1) + u1:set_skill('polearm', 5) + u1:add_order('ATTACKIERE ' .. itoa36(uno)) + process_orders() + u2 = get_unit(uno) + assert_not_nil(u2) + assert_equal(r2, u2.region) +end diff --git a/src/bind_unit.c b/src/bind_unit.c index 00c2b9e09..879af57ec 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -286,7 +286,7 @@ static int tolua_unit_get_magic(lua_State * L) static void unit_setmagic(unit * u, const char *type) { - sc_mage *mage = get_mage_depr(u); + sc_mage *mage = get_mage(u); int mtype; for (mtype = 0; mtype != MAXMAGIETYP; ++mtype) { if (strcmp(magic_school[mtype], type) == 0) From 8efc7f1829602005a590213d2090cbf8950374b0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 10:44:02 +0100 Subject: [PATCH 19/25] BUG 2391: Friedenslied erlaubt Flucht aus der Region. --- scripts/tests/common.lua | 6 ++++-- scripts/tests/e2/spells.lua | 22 ---------------------- scripts/tests/spells.lua | 22 ++++++++++++++++++++++ src/battle.c | 14 +++++++------- src/spells.c | 2 +- src/spells/combatspells.c | 2 +- src/spells/combatspells.h | 2 +- 7 files changed, 36 insertions(+), 34 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 0fda7168e..c186107c4 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -121,9 +121,11 @@ function test_fleeing_units_can_be_transported() u1.number = 100 u1:add_order("ATTACKIEREN " .. itoa36(u2.id)) u2.number = 100 + u2.name = 'Passagier' u2:add_order("FAHREN " .. itoa36(u3.id)) u2:add_order("KAEMPFE FLIEHE") u3.number = 100 + u3.name = 'Transporter' u3:add_order("KAEMPFE FLIEHE") u3:add_order("TRANSPORT " .. itoa36(u2.id)) u3:add_order("NACH O ") @@ -131,8 +133,8 @@ function test_fleeing_units_can_be_transported() u3:add_item("horse", u2.number) u3:add_order("KAEMPFE FLIEHE") process_orders() - assert_equal(u3.region.id, r1.id, "transporter did not move") - assert_equal(u2.region.id, r1.id, "transported unit did not move") + assert_equal(u3.region, r1, "transporter did not move") + assert_equal(u2.region, r1, "transported unit did not move") end function test_plane() diff --git a/scripts/tests/e2/spells.lua b/scripts/tests/e2/spells.lua index fdc23eb94..d05552a2d 100644 --- a/scripts/tests/e2/spells.lua +++ b/scripts/tests/e2/spells.lua @@ -103,25 +103,3 @@ function test_earn_silver() assert_equal(350, u:get_item("money")) assert_equal(0, r:get_resource("money")) end - -function test_appeasement() - local u1, u2, r1, r2, uno - r1 = region.create(0, 0, 'plain') - r2 = region.create(1, 0, 'plain') - u2 = unit.create(faction.create('human'), r1, 1) - u2.name = 'Angsthase' - u2.magic = 'gwyrrd' - u2:add_spell('appeasement') - u2:set_skill('magic', 5) - u2:add_order('NACH O') - u2:add_order('KAMPFZAUBER STUFE 1 Friedenslied') - u2.aura = 10 - uno = u2.id - u1 = unit.create(faction.create('human'), r1, 1) - u1:set_skill('polearm', 5) - u1:add_order('ATTACKIERE ' .. itoa36(uno)) - process_orders() - u2 = get_unit(uno) - assert_not_nil(u2) - assert_equal(r2, u2.region) -end diff --git a/scripts/tests/spells.lua b/scripts/tests/spells.lua index e3c0a3021..1a8442f2e 100644 --- a/scripts/tests/spells.lua +++ b/scripts/tests/spells.lua @@ -81,3 +81,25 @@ function test_create_dreameye() assert_equal(amax - 5, u.aura_max) end +function test_appeasement() + local u1, u2, r1, r2, uno + r1 = region.create(0, 0, 'plain') + r2 = region.create(1, 0, 'plain') + u2 = unit.create(faction.create('human'), r1, 1) + u2.race = 'elf' + u2.name = 'Angsthase' + u2.magic = 'gwyrrd' + u2:set_skill('magic', 5) + u2.aura = 10 + u2:add_spell('appeasement') + u2:add_order('NACH O') + u2:add_order('KAMPFZAUBER STUFE 1 Friedenslied') + uno = u2.id + u1 = unit.create(faction.create('human'), r1, 1) + u1:set_skill('polearm', 5) + u1:add_order('ATTACKIERE ' .. itoa36(uno)) + process_orders() + u2 = get_unit(uno) + assert_not_nil(u2) + assert_equal(r2, u2.region) +end diff --git a/src/battle.c b/src/battle.c index 787bd1a10..721b7bf5d 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2641,14 +2641,14 @@ static void aftermath(battle * b) } } snumber += du->number; - if (relevant) { - flags = UFL_LONGACTION | UFL_NOTMOVING; - if (du->status == ST_FLEE) { - flags -= UFL_NOTMOVING; - } - } if (df->alive == 0) { - flags |= UFL_DEAD; + flags = UFL_DEAD; + } + else if (relevant) { + flags = UFL_LONGACTION; + if ((du->status != ST_FLEE) && (df->run.hp <= 0)) { + flags |= UFL_NOTMOVING; + } } if (flags) { fset(du, flags); diff --git a/src/spells.c b/src/spells.c index 7f21e8654..e2fc3fcd3 100644 --- a/src/spells.c +++ b/src/spells.c @@ -6460,7 +6460,7 @@ static spelldata spell_functions[] = { { "mindblast", sp_mindblast_temp, 0 }, { "orkdream", sp_sweetdreams, 0 }, /* M_CERDDOR */ - { "appeasement", sp_denyattack, 0 }, + { "appeasement", sp_appeasement, 0 }, { "song_of_healing", sp_healing, 0 }, { "generous", sp_generous, 0 }, { "song_of_fear", sp_song_of_fear, 0 }, diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 79bcd52d4..8c139fb9c 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -1262,7 +1262,7 @@ int sp_reeling_arrows(struct castorder * co) /* Magier weicht dem Kampf aus. Wenn er sich bewegen kann, zieht er in * eine Nachbarregion, wobei ein NACH ber�cksichtigt wird. Ansonsten * bleibt er stehen und nimmt nicht weiter am Kampf teil. */ -int sp_denyattack(struct castorder * co) +int sp_appeasement(struct castorder * co) { fighter * fi = co->magician.fig; int level = co->level; diff --git a/src/spells/combatspells.h b/src/spells/combatspells.h index 78da949e8..edd041704 100644 --- a/src/spells/combatspells.h +++ b/src/spells/combatspells.h @@ -30,7 +30,7 @@ extern "C" { int sp_berserk(struct castorder * co); int sp_tiredsoldiers(struct castorder * co); int sp_reeling_arrows(struct castorder * co); - int sp_denyattack(struct castorder * co); + int sp_appeasement(struct castorder * co); int sp_sleep(struct castorder * co); int sp_windshield(struct castorder * co); int sp_strong_wall(struct castorder * co); From abd9b94d374b2548c14477dcac30500abc644815 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 11:00:37 +0100 Subject: [PATCH 20/25] remove some ifdef options to maybe accelerate cppcheck. --- src/alchemy.h | 4 ---- src/kernel/faction.h | 3 --- src/spells.c | 7 +++---- src/study.c | 6 ++---- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/alchemy.h b/src/alchemy.h index 6dec4120b..b89d1c27f 100644 --- a/src/alchemy.h +++ b/src/alchemy.h @@ -41,11 +41,7 @@ extern "C" { /* Stufe 3 */ P_WISE, /* 6 */ P_FOOL, -#ifdef INSECT_POTION - P_WARMTH, -#else P_STEEL, -#endif P_HORSE, P_BERSERK, /* 10 */ /* Stufe 4 */ diff --git a/src/kernel/faction.h b/src/kernel/faction.h index a04acabfa..8b8c88abe 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -84,9 +84,6 @@ extern "C" { score_t score; struct alliance *alliance; int alliance_joindate; /* the turn on which the faction joined its current alliance (or left the last one) */ -#ifdef VICTORY_DELAY - unsigned char victory_delay; -#endif struct unit *units; struct attrib *attribs; struct message_list *msgs; diff --git a/src/spells.c b/src/spells.c index e2fc3fcd3..b25b933fd 100644 --- a/src/spells.c +++ b/src/spells.c @@ -1280,10 +1280,8 @@ static int sp_rosthauch(castorder * co) add_ironweapon(it_find("axe"), it_find("rustyaxe"), 1.0); add_ironweapon(it_find("greatsword"), it_find("rustygreatsword"), 1.0); add_ironweapon(it_find("halberd"), it_find("rustyhalberd"), 0.5f); -#ifndef NO_RUSTY_ARMOR add_ironweapon(it_find("shield"), it_find("rustyshield"), 0.5f); add_ironweapon(it_find("chainmail"), it_find("rustychainmail"), 0.2f); -#endif } if (force > 0) { @@ -5454,7 +5452,8 @@ int sp_fetchastral(castorder * co) return cast_level; } -#ifdef SHOWASTRAL_NOT_BORKED +#define SHOWASTRAL_IS_BORKED +#ifndef SHOWASTRAL_IS_BORKED int sp_showastral(castorder * co) { unit *u; @@ -6493,7 +6492,7 @@ static spelldata spell_functions[] = { { "analyze_magic", sp_analysemagic, 0 }, { "concealing_aura", sp_itemcloak, 0 }, { "tybiedfumbleshield", sp_fumbleshield, 0 }, -#ifdef SHOWASTRAL_NOT_BORKED +#ifndef SHOWASTRAL_IS_BORKED { "show_astral", sp_showastral, 0 }, #endif { "resist_magic", sp_resist_magic_bonus, 0 }, diff --git a/src/study.c b/src/study.c index 9264f9060..a80577af9 100644 --- a/src/study.c +++ b/src/study.c @@ -811,23 +811,21 @@ void produceexp(struct unit *u, skill_t sk, int n) produceexp_ex(u, sk, n, learn_skill); } -#ifndef NO_TESTS static learn_fun inject_learn_fun = 0; void inject_learn(learn_fun fun) { inject_learn_fun = fun; } -#endif + /** days should be scaled by u->number; STUDYDAYS * u->number is one week worth of learning */ void learn_skill(unit *u, skill_t sk, int days) { int leveldays = STUDYDAYS * u->number; int weeks = 0; -#ifndef NO_TESTS + if (inject_learn_fun) { inject_learn_fun(u, sk, days); return; } -#endif while (days >= leveldays) { ++weeks; days -= leveldays; From 643883d53942d9b1f9396649323b5494cc8f454a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 11:17:39 +0100 Subject: [PATCH 21/25] remove some more unused defines. --- src/battle.c | 5 ----- src/kernel/unit.c | 6 ------ src/util/bsdstring.c | 2 -- 3 files changed, 13 deletions(-) diff --git a/src/battle.c b/src/battle.c index 721b7bf5d..a58b0ab63 100644 --- a/src/battle.c +++ b/src/battle.c @@ -2010,11 +2010,6 @@ void dazzle(battle * b, troop * td) { UNUSED_ARG(b); /* Nicht kumulativ ! */ -#ifdef TODO_RUNESWORD - if (td->fighter->weapon[WP_RUNESWORD].count > td->index) { - return; - } -#endif if (td->fighter->person[td->index].flags & (FL_COURAGE|FL_DAZZLED)) { return; } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index bc776befa..ee4da958c 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1600,12 +1600,6 @@ int countheroes(const struct faction *f) n += u->number; u = u->nextF; } -#ifdef DEBUG_MAXHEROES - int m = maxheroes(f); - if (n > m) { - log_warning("%s has %d of %d heroes\n", factionname(f), n, m); - } -#endif return n; } diff --git a/src/util/bsdstring.c b/src/util/bsdstring.c index 0f3d49801..4aa779495 100644 --- a/src/util/bsdstring.c +++ b/src/util/bsdstring.c @@ -78,8 +78,6 @@ char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const return buf; } - - #ifndef HAVE_STRLCAT #define HAVE_STRLCAT size_t strlcat(char *dst, const char *src, size_t siz) From 866abcc92cbb960ef2cb837ed54c634de94f7dfa Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 16:53:36 +0100 Subject: [PATCH 22/25] remove obsolete defines. --- src/bind_unit.c | 35 ----------------------------------- src/study.h | 2 -- 2 files changed, 37 deletions(-) diff --git a/src/bind_unit.c b/src/bind_unit.c index 879af57ec..a7e8f61ab 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -763,38 +763,6 @@ static int tolua_unit_get_spells(lua_State * L) return tolua_selist_push(L, "spellbook", "spell_entry", slist); } -#ifdef TOLUA_ORDERS_CLOSURE -/* TODO: this requires that the locale for write_order is included in the closure */ -static int tolua_orderlist_next(lua_State * L) -{ - order **order_ptr = (order **)lua_touserdata(L, lua_upvalueindex(1)); - order *ord = *order_ptr; - if (ord != NULL) { - char cmd[8192]; - write_order(ord, cmd, sizeof(cmd)); - tolua_pushstring(L, cmd); - *order_ptr = ord->next; - return 1; - } - return 0; -} - -static int tolua_unit_get_orders(lua_State * L) -{ - unit *self = (unit *)tolua_tousertype(L, 1, 0); - - order **order_ptr = (order **)lua_newuserdata(L, sizeof(order *)); - - luaL_getmetatable(L, TOLUA_CAST "order"); - lua_setmetatable(L, -2); - - *order_ptr = self->orders; - - lua_pushcclosure(L, tolua_orderlist_next, 1); - - return 1; -} -#endif static int tolua_unit_get_curse(lua_State *L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); @@ -1006,9 +974,6 @@ void tolua_unit_open(lua_State * L) tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order); tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders); -#ifdef TOLUA_ORDERS_CLOSURE - tolua_variable(L, TOLUA_CAST "orders", tolua_unit_get_orders, 0); -#endif tolua_function(L, TOLUA_CAST "get_curse", tolua_unit_get_curse); tolua_function(L, TOLUA_CAST "has_attrib", tolua_unit_has_attrib); diff --git a/src/study.h b/src/study.h index ff43f1f43..b4b76bb8c 100644 --- a/src/study.h +++ b/src/study.h @@ -56,9 +56,7 @@ extern "C" { extern const struct attrib_type at_learning; -#ifndef NO_TESTS void inject_learn(learn_fun fun); -#endif #ifdef __cplusplus } From ffbc9596d7c69d6fd0ca3ef6b4da0f1dc7511b4e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 21:04:27 +0100 Subject: [PATCH 23/25] we do not need a USE_LIBXML define, xml is a hard requirement. --- src/CMakeLists.txt | 1 - src/convert.c | 6 ------ src/eressea.c | 2 -- src/kernel/config.c | 4 ---- src/kernel/xmlreader.c | 4 ---- src/util/xml.c | 9 --------- src/util/xml.h | 2 -- 7 files changed, 28 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 89e9b90c4..2374c8a1f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -294,5 +294,4 @@ include_directories (${LIBXML2_INCLUDE_DIR}) target_link_libraries(eressea ${LIBXML2_LIBRARIES}) target_link_libraries(convert ${LIBXML2_LIBRARIES}) target_link_libraries(test_eressea ${LIBXML2_LIBRARIES}) -add_definitions(-DUSE_LIBXML2) endif (LIBXML2_FOUND) diff --git a/src/convert.c b/src/convert.c index a67c5a287..5691a3486 100644 --- a/src/convert.c +++ b/src/convert.c @@ -1,10 +1,8 @@ #include -#ifdef USE_LIBXML2 #include #include -#endif #include #include #include @@ -21,12 +19,9 @@ int main(int argc, char **argv) { const char *mode; register_races(); -#ifdef USE_LIBXML2 register_xmlreader(); -#endif if (argc < 2) return usage(); mode = argv[1]; -#ifdef USE_LIBXML2 if (strcmp(mode, "rules")==0) { const char *xmlfile, *catalog; if (argc < 4) return usage(); @@ -36,7 +31,6 @@ int main(int argc, char **argv) { write_rules("rules.dat"); return 0; } -#endif if (strcmp(mode, "po")==0) { return 0; } diff --git a/src/eressea.c b/src/eressea.c index f721e067a..cb02a8bd0 100644 --- a/src/eressea.c +++ b/src/eressea.c @@ -85,9 +85,7 @@ void game_init(void) register_weapons(); register_xerewards(); -#ifdef USE_LIBXML2 register_xmlreader(); -#endif register_attributes(); register_gmcmd(); diff --git a/src/kernel/config.c b/src/kernel/config.c index 1d3e6b6d2..32c7f5450 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -73,11 +73,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "guard.h" #include "prefix.h" -#ifdef USE_LIBXML2 /* libxml includes */ #include #include -#endif /* external libraries */ #include @@ -602,9 +600,7 @@ void kernel_done(void) /* calling this function releases memory assigned to static variables, etc. * calling it is optional, e.g. a release server will most likely not do it. */ -#ifdef USE_LIBXML2 xml_done(); -#endif attrib_done(); item_done(); message_done(); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 051bc0999..ace0a4cc6 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -48,12 +48,10 @@ without prior permission by the authors of Eressea. #include #include -#ifdef USE_LIBXML2 /* libxml includes */ #include #include #include -#endif /* libc includes */ #include @@ -61,7 +59,6 @@ without prior permission by the authors of Eressea. #include #include -#ifdef USE_LIBXML2 static variant xml_fraction(xmlNodePtr node, const char *name) { xmlChar *propValue = xmlGetProp(node, BAD_CAST name); @@ -1761,4 +1758,3 @@ void register_xmlreader(void) xml_register_callback(parse_strings); xml_register_callback(parse_messages); } -#endif diff --git a/src/util/xml.c b/src/util/xml.c index 644698119..ec72179c3 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -16,10 +16,8 @@ #include "log.h" #include "assert.h" -#ifdef USE_LIBXML2 #include #include -#endif /* libc includes */ #include @@ -27,7 +25,6 @@ #include #include -#ifdef USE_LIBXML2 int xml_ivalue(xmlNodePtr node, const char *name, int dflt) { int i = dflt; @@ -110,11 +107,9 @@ void xml_register_callback(xml_callback callback) insert = &(*insert)->next; *insert = reader; } -#endif int read_xml(const char *filename, const char *catalog) { -#ifdef USE_LIBXML2 xml_reader *reader = xmlReaders; xmlDocPtr doc; int result; @@ -141,8 +136,4 @@ int read_xml(const char *filename, const char *catalog) } xmlFreeDoc(doc); return result; -#else - log_error("LIBXML2 disabled, cannot read %s.\n", filename); - return -1; -#endif } diff --git a/src/util/xml.h b/src/util/xml.h index bd22e3e0a..f2dc5fda5 100644 --- a/src/util/xml.h +++ b/src/util/xml.h @@ -19,7 +19,6 @@ extern "C" { #endif -#ifdef USE_LIBXML2 /* new xml functions: */ #include @@ -30,7 +29,6 @@ extern "C" { double xml_fvalue(xmlNodePtr node, const char *name, double dflt); int xml_ivalue(xmlNodePtr node, const char *name, int dflt); bool xml_bvalue(xmlNodePtr node, const char *name, bool dflt); -#endif void xml_done(void); int read_xml(const char *filename, const char *catalog); From 4e12ff536ad51494ebc5453499af7ca4c53a6dfd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 21:06:44 +0100 Subject: [PATCH 24/25] The MUSEUM_MODULE define is unnecessary. --- src/eressea.c | 5 +---- src/give.c | 6 ------ src/gmtool.c | 2 -- src/modules/museum.c | 3 --- src/modules/museum.h | 4 ---- src/settings.h | 6 ------ 6 files changed, 1 insertion(+), 25 deletions(-) diff --git a/src/eressea.c b/src/eressea.c index cb02a8bd0..117895f6e 100644 --- a/src/eressea.c +++ b/src/eressea.c @@ -16,9 +16,7 @@ #include #include -#if MUSEUM_MODULE #include -#endif #include #include #include @@ -41,6 +39,7 @@ void game_done(void) { +#undef CLEANUP_CODE #ifdef CLEANUP_CODE /* Diese Routine enfernt allen allokierten Speicher wieder. Das ist nur * zum Debugging interessant, wenn man Leak Detection hat, und nach @@ -78,9 +77,7 @@ void game_init(void) register_names(); register_resources(); register_itemfunctions(); -#if MUSEUM_MODULE register_museum(); -#endif wormholes_register(); register_weapons(); diff --git a/src/give.c b/src/give.c index 84e2765a6..5f7945887 100644 --- a/src/give.c +++ b/src/give.c @@ -239,12 +239,6 @@ struct order *ord) change_reservation(dest, item2resource(itype), r); } #endif -#endif -#if MUSEUM_MODULE && defined(TODO) - /* TODO: use a trigger for the museum warden! */ - if (a_find(dest->attribs, &at_warden)) { - warden_add_give(src, dest, itype, delta); - } #endif } else { diff --git a/src/gmtool.c b/src/gmtool.c index c33478843..fcb8cff8b 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -24,9 +24,7 @@ #include #include -#if MUSEUM_MODULE #include -#endif #include #include diff --git a/src/modules/museum.c b/src/modules/museum.c index 1eee3529f..572a8c41a 100644 --- a/src/modules/museum.c +++ b/src/modules/museum.c @@ -20,7 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include -#if MUSEUM_MODULE #include "museum.h" /* kernel includes */ @@ -502,5 +501,3 @@ void register_museum(void) register_item_use(use_museumkey, "use_questkey1"); register_item_use(use_museumkey, "use_questkey2"); } - -#endif diff --git a/src/modules/museum.h b/src/modules/museum.h index ed9dc74b0..704d38604 100644 --- a/src/modules/museum.h +++ b/src/modules/museum.h @@ -20,10 +20,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define HEADER_MUSEUM_H #ifdef __cplusplus extern "C" { -#endif - -#if MUSEUM_MODULE == 0 -#error "must define MUSEUM_MODULE to use this module" #endif extern struct attrib_type at_warden; diff --git a/src/settings.h b/src/settings.h index e8693a763..0a017dd7a 100644 --- a/src/settings.h +++ b/src/settings.h @@ -24,12 +24,6 @@ #define ROW_FACTOR 3 /* factor for combat row advancement rule */ -/* optional game components. TODO: These should either be - * configuration variables (XML), script extensions (lua), - * or both. We don't want separate binaries for different games - */ -#define MUSEUM_MODULE 1 - /* TODO: move these settings to settings.h or into configuration files */ #define TREESIZE (8) /* space used by trees (in #peasants) */ #define PEASANTFORCE 0.75 /* Chance einer Vermehrung trotz 90% Auslastung */ From 5576ef37b6a82be58d6683ce2bc5da3ce2a299b3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 9 Dec 2017 21:20:20 +0100 Subject: [PATCH 25/25] remove building_action feature, it seems that it isn't in use. --- src/CMakeLists.txt | 1 - src/building_action.c | 151 ------------------------------------------ src/helpers.c | 15 ++++- src/kernel/building.h | 1 - 4 files changed, 14 insertions(+), 154 deletions(-) delete mode 100644 src/building_action.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2374c8a1f..d3cfdafce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -141,7 +141,6 @@ set (ERESSEA_SRC set(SERVER_SRC main.c - building_action.c console.c helpers.c bind_tolua.c diff --git a/src/building_action.c b/src/building_action.c deleted file mode 100644 index 28cd55a48..000000000 --- a/src/building_action.c +++ /dev/null @@ -1,151 +0,0 @@ -/* -+-------------------+ -| | Enno Rehling -| Eressea PBEM host | Christian Schlittchen -| (c) 1998 - 2008 | Katja Zedel -| | Henning Peters -+-------------------+ - -This program may not be used, modified or distributed -without prior permission by the authors of Eressea. -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include - -typedef struct building_action { - char *fname; - char *param; -} building_action; - -static int lc_age(struct attrib *a, void *owner) -{ - building_action *data = (building_action *)a->data.v; - const char *fname = data->fname; - const char *fparam = data->param; - building *b = (building *)owner; - int result = -1; - - assert(b != NULL); - if (fname != NULL) { - lua_State *L = (lua_State *)global.vm_state; - - lua_getglobal(L, fname); - if (lua_isfunction(L, -1)) { - tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); - if (fparam) { - tolua_pushstring(L, fparam); - } - - if (lua_pcall(L, fparam ? 2 : 1, 1, 0) != 0) { - const char *error = lua_tostring(L, -1); - log_error("lc_age(%s) calling '%s': %s.\n", buildingname(b), fname, error); - lua_pop(L, 1); - } - else { - result = (int)lua_tonumber(L, -1); - lua_pop(L, 1); - } - } - else { - log_error("lc_age(%s) calling '%s': not a function.\n", buildingname(b), fname); - lua_pop(L, 1); - } - } - return (result != 0) ? AT_AGE_KEEP : AT_AGE_REMOVE; -} - -static const char *NULLSTRING = "(null)"; - -static void lc_init(struct attrib *a) -{ - a->data.v = calloc(1, sizeof(building_action)); -} - -static void lc_done(struct attrib *a) -{ - building_action *data = (building_action *)a->data.v; - if (data->fname) - free(data->fname); - if (data->param) - free(data->param); - free(data); -} - -static void lc_write(const struct attrib *a, const void *owner, struct storage *store) -{ - building_action *data = (building_action *)a->data.v; - const char *fname = data->fname; - const char *fparam = data->param; - - UNUSED_ARG(owner); - WRITE_TOK(store, fname); - WRITE_TOK(store, fparam ? fparam : NULLSTRING); -} - -static int lc_read(struct attrib *a, void *owner, gamedata *data) -{ - struct storage *store = data->store; - char name[NAMESIZE]; - building_action *bd = (building_action *)a->data.v; - building *b = (building *)owner; - int result = 0; - if (data->version < ATTRIBOWNER_VERSION) { - READ_INT(data->store, NULL); - } - READ_TOK(store, name, sizeof(name)); - if (strcmp(name, "tunnel_action") == 0) { - /* E2: Weltentor has a new module, doesn't need this any longer */ - result = 0; - b = 0; - } - else { - bd->fname = strdup(name); - } - READ_TOK(store, name, sizeof(name)); - if (strcmp(name, "tnnL") == 0) { - /* tunnel_action was the old Weltentore, their code has changed. ignore this object */ - result = 0; - b = 0; - } - if (strcmp(name, NULLSTRING) == 0) - bd->param = 0; - else { - bd->param = strdup(name); - } - if (result == 0 && !b) { - return AT_READ_FAIL; - } - return AT_READ_OK; -} - -attrib_type at_building_action = { - "lcbuilding", - lc_init, lc_done, - lc_age, - lc_write, lc_read -}; - -void building_addaction(building * b, const char *fname, const char *param) -{ - attrib *a = a_add(&b->attribs, a_new(&at_building_action)); - building_action *data = (building_action *)a->data.v; - data->fname = strdup(fname); - if (param) { - data->param = strdup(param); - } -} diff --git a/src/helpers.c b/src/helpers.c index 5f73ac542..897e39720 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -304,11 +304,24 @@ struct trigger_type tt_caldera = { caldera_read }; + +static int building_action_read(struct attrib *a, void *owner, gamedata *data) +{ + struct storage *store = data->store; + + if (data->version < ATTRIBOWNER_VERSION) { + READ_INT(data->store, NULL); + } + READ_TOK(store, NULL, 0); + READ_TOK(store, NULL, 0); + return AT_READ_DEPR; +} + void register_tolua_helpers(void) { tt_register(&tt_caldera); at_register(&at_direction); - at_register(&at_building_action); + at_deprecate("lcbuilding", building_action_read); callbacks.cast_spell = lua_callspell; callbacks.use_item = use_item_lua; diff --git a/src/kernel/building.h b/src/kernel/building.h index af29c324b..e162e041f 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -71,7 +71,6 @@ extern "C" { } building_type; extern struct selist *buildingtypes; - extern struct attrib_type at_building_action; extern struct attrib_type at_building_generic_type; int cmp_castle_size(const struct building *b, const struct building *a);