From 3055f517dc966ba7fc6c4ce55ba5a49149c1e23e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 22:49:43 +0200 Subject: [PATCH 1/3] move guard implementation into a module (WIP) --- src/CMakeLists.txt | 1 + src/guard.c | 129 ++++++++++++++++++++++++++++++++++++++++++++ src/guard.h | 23 ++++++++ src/kernel/config.c | 75 -------------------------- src/kernel/unit.c | 3 ++ src/laws.c | 32 ++--------- src/laws.h | 4 +- 7 files changed, 162 insertions(+), 105 deletions(-) create mode 100644 src/guard.c create mode 100644 src/guard.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0067463a6..10d4b2597 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,6 +87,7 @@ set (ERESSEA_SRC names.c lighthouse.c reports.c + guard.c prefix.c donations.c seen.c diff --git a/src/guard.c b/src/guard.c new file mode 100644 index 000000000..c744de126 --- /dev/null +++ b/src/guard.c @@ -0,0 +1,129 @@ +/* +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. +**/ + +#include +#include +#include "guard.h" + +#include +#include +#include +#include +#include +#include + +#include + +void update_guards(void) +{ + const region *r; + + for (r = regions; r; r = r->next) { + unit *u; + for (u = r->units; u; u = u->next) { + if (fval(u, UFL_GUARD)) { + if (can_start_guarding(u) != E_GUARD_OK) { + setguard(u, GUARD_NONE); + } + else { + attrib *a = a_find(u->attribs, &at_guard); + if (a && a->data.i == (int)guard_flags(u)) { + /* this is really rather not necessary */ + a_remove(&u->attribs, a); + } + } + } + } + } +} + +unsigned int guard_flags(const unit * u) +{ + unsigned int flags = + GUARD_CREWS | GUARD_LANDING | GUARD_TRAVELTHRU | GUARD_TAX; +#if GUARD_DISABLES_PRODUCTION == 1 + flags |= GUARD_PRODUCE; +#endif +#if GUARD_DISABLES_RECRUIT == 1 + flags |= GUARD_RECRUIT; +#endif + switch (old_race(u_race(u))) { + case RC_ELF: + if (u->faction->race != u_race(u)) + break; + /* else fallthrough */ + case RC_TREEMAN: + flags |= GUARD_TREES; + break; + case RC_IRONKEEPER: + flags = GUARD_MINING; + break; + default: + /* TODO: This should be configuration variables, all of it */ + break; + } + return flags; +} + +void setguard(unit * u, unsigned int flags) +{ + /* setzt die guard-flags der Einheit */ + attrib *a = NULL; + assert(flags == 0 || !fval(u, UFL_MOVED)); + assert(flags == 0 || u->status < ST_FLEE); + if (fval(u, UFL_GUARD)) { + a = a_find(u->attribs, &at_guard); + } + if (flags == GUARD_NONE) { + freset(u, UFL_GUARD); + if (a) + a_remove(&u->attribs, a); + return; + } + fset(u, UFL_GUARD); + fset(u->region, RF_GUARDED); + if (flags == guard_flags(u)) { + if (a) + a_remove(&u->attribs, a); + } + else { + if (!a) + a = a_add(&u->attribs, a_new(&at_guard)); + a->data.i = (int)flags; + } +} + +unsigned int getguard(const unit * u) +{ + attrib *a; + + assert(fval(u, UFL_GUARD) || (u->building && u == building_owner(u->building)) + || !"you're doing it wrong! check is_guard first"); + a = a_find(u->attribs, &at_guard); + if (a) { + return (unsigned int)a->data.i; + } + return guard_flags(u); +} + +void guard(unit * u, unsigned int mask) +{ + unsigned int flags = guard_flags(u); + setguard(u, flags & mask); +} diff --git a/src/guard.h b/src/guard.h new file mode 100644 index 000000000..b2041012d --- /dev/null +++ b/src/guard.h @@ -0,0 +1,23 @@ +#pragma once + +#ifndef H_GUARD +#define H_GUARD +#ifdef __cplusplus +extern "C" { +#endif + + struct unit; + + typedef enum { E_GUARD_OK, E_GUARD_UNARMED, E_GUARD_NEWBIE, E_GUARD_FLEEING } guard_t; + guard_t can_start_guarding(const struct unit * u); + void update_guards(void); + unsigned int guard_flags(const struct unit * u); + unsigned int getguard(const struct unit * u); + void setguard(struct unit * u, unsigned int flags); + + void guard(struct unit * u, unsigned int mask); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/kernel/config.c b/src/kernel/config.c index 40b9f95cd..fa11c004f 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1195,47 +1195,6 @@ void setstatus(struct unit *u, int status) } } -void setguard(unit * u, unsigned int flags) -{ - /* setzt die guard-flags der Einheit */ - attrib *a = NULL; - assert(flags == 0 || !fval(u, UFL_MOVED)); - assert(flags == 0 || u->status < ST_FLEE); - if (fval(u, UFL_GUARD)) { - a = a_find(u->attribs, &at_guard); - } - if (flags == GUARD_NONE) { - freset(u, UFL_GUARD); - if (a) - a_remove(&u->attribs, a); - return; - } - fset(u, UFL_GUARD); - fset(u->region, RF_GUARDED); - if (flags == guard_flags(u)) { - if (a) - a_remove(&u->attribs, a); - } - else { - if (!a) - a = a_add(&u->attribs, a_new(&at_guard)); - a->data.i = (int)flags; - } -} - -unsigned int getguard(const unit * u) -{ - attrib *a; - - assert(fval(u, UFL_GUARD) || (u->building && u == building_owner(u->building)) - || !"you're doing it wrong! check is_guard first"); - a = a_find(u->attribs, &at_guard); - if (a) { - return (unsigned int)a->data.i; - } - return guard_flags(u); -} - #ifndef HAVE_STRDUP char *_strdup(const char *s) { @@ -1248,40 +1207,6 @@ bool faction_id_is_unused(int id) return findfaction(id) == NULL; } -unsigned int guard_flags(const unit * u) -{ - unsigned int flags = - GUARD_CREWS | GUARD_LANDING | GUARD_TRAVELTHRU | GUARD_TAX; -#if GUARD_DISABLES_PRODUCTION == 1 - flags |= GUARD_PRODUCE; -#endif -#if GUARD_DISABLES_RECRUIT == 1 - flags |= GUARD_RECRUIT; -#endif - switch (old_race(u_race(u))) { - case RC_ELF: - if (u->faction->race != u_race(u)) - break; - /* else fallthrough */ - case RC_TREEMAN: - flags |= GUARD_TREES; - break; - case RC_IRONKEEPER: - flags = GUARD_MINING; - break; - default: - /* TODO: This should be configuration variables, all of it */ - break; - } - return flags; -} - -void guard(unit * u, unsigned int mask) -{ - unsigned int flags = guard_flags(u); - setguard(u, flags & mask); -} - int besieged(const unit * u) { /* belagert kann man in schiffen und burgen werden */ diff --git a/src/kernel/unit.c b/src/kernel/unit.c index a12edd310..064bdd87e 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -43,6 +43,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include "guard.h" + /* util includes */ #include #include @@ -1957,3 +1959,4 @@ void produceexp(struct unit *u, skill_t sk, int n) { produceexp_ex(u, sk, n, learn_skill); } + diff --git a/src/laws.c b/src/laws.c index ac6d62ff7..b29280bb4 100755 --- a/src/laws.c +++ b/src/laws.c @@ -37,6 +37,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "wormhole.h" #include "prefix.h" #include "calendar.h" +#include "guard.h" /* kernel includes */ #include @@ -2660,13 +2661,9 @@ int combatspell_cmd(unit * u, struct order *ord) return 0; } -/* ------------------------------------------------------------- */ /* Beachten: einige Monster sollen auch unbewaffent die Region bewachen - * können */ - -enum { E_GUARD_OK, E_GUARD_UNARMED, E_GUARD_NEWBIE, E_GUARD_FLEEING }; - -static int can_start_guarding(const unit * u) +* können */ +guard_t can_start_guarding(const unit * u) { if (u->status >= ST_FLEE || fval(u, UFL_FLEEING)) return E_GUARD_FLEEING; @@ -2679,29 +2676,6 @@ static int can_start_guarding(const unit * u) return E_GUARD_OK; } -void update_guards(void) -{ - const region *r; - - for (r = regions; r; r = r->next) { - unit *u; - for (u = r->units; u; u = u->next) { - if (fval(u, UFL_GUARD)) { - if (can_start_guarding(u) != E_GUARD_OK) { - setguard(u, GUARD_NONE); - } - else { - attrib *a = a_find(u->attribs, &at_guard); - if (a && a->data.i == (int)guard_flags(u)) { - /* this is really rather not necessary */ - a_remove(&u->attribs, a); - } - } - } - } - } -} - int guard_on_cmd(unit * u, struct order *ord) { assert(getkeyword(ord) == K_GUARD); diff --git a/src/laws.h b/src/laws.h index bd36fce51..0a677f384 100755 --- a/src/laws.h +++ b/src/laws.h @@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define H_GC_LAWS #include +#include "guard.h" #ifdef __cplusplus extern "C" { @@ -111,7 +112,8 @@ extern "C" { #define FORCE_LEAVE_POSTCOMBAT 1 #define FORCE_LEAVE_ALL 2 bool rule_force_leave(int flag); - bool help_enter(struct unit *uo, struct unit *u); + bool help_enter(struct unit *uo, struct unit *u); + guard_t can_start_guarding(const struct unit * u); #ifdef __cplusplus } From dd9420fc38ca61852b572a7330f20265c738ab39 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 23:38:31 +0200 Subject: [PATCH 2/3] move at_guard into guard module as well --- src/guard.c | 11 +++++++++++ src/guard.h | 3 +++ src/kernel/config.c | 12 ++---------- src/kernel/config.h | 1 - tests/data/test.dat | Bin 159 -> 0 bytes tests/orders.txt | 3 --- 6 files changed, 16 insertions(+), 14 deletions(-) delete mode 100644 tests/data/test.dat delete mode 100644 tests/orders.txt diff --git a/src/guard.c b/src/guard.c index c744de126..9e3585a41 100644 --- a/src/guard.c +++ b/src/guard.c @@ -21,6 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "guard.h" +#include #include #include #include @@ -30,6 +31,16 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include +attrib_type at_guard = { + "guard", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + a_writeint, + a_readint, + ATF_UNIQUE +}; + void update_guards(void) { const region *r; diff --git a/src/guard.h b/src/guard.h index b2041012d..564be61a2 100644 --- a/src/guard.h +++ b/src/guard.h @@ -9,6 +9,9 @@ extern "C" { struct unit; typedef enum { E_GUARD_OK, E_GUARD_UNARMED, E_GUARD_NEWBIE, E_GUARD_FLEEING } guard_t; + + extern struct attrib_type at_guard; + guard_t can_start_guarding(const struct unit * u); void update_guards(void); unsigned int guard_flags(const struct unit * u); diff --git a/src/kernel/config.c b/src/kernel/config.c index fa11c004f..1db17a495 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -51,6 +51,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "types.h" #include "unit.h" + #include #include @@ -74,6 +75,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "donations.h" +#include "guard.h" #include "prefix.h" #ifdef USE_LIBXML2 @@ -1177,16 +1179,6 @@ attrib_type at_germs = { ATF_UNIQUE }; -attrib_type at_guard = { - "guard", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - a_writeint, - a_readint, - ATF_UNIQUE -}; - void setstatus(struct unit *u, int status) { assert(status >= ST_AGGRO && status <= ST_FLEE); diff --git a/src/kernel/config.h b/src/kernel/config.h index 4dc870ff6..91e130706 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -306,7 +306,6 @@ extern "C" { void free_gamedata(void); - extern struct attrib_type at_guard; extern struct helpmode helpmodes[]; extern const char *parameters[]; extern const char *localenames[]; diff --git a/tests/data/test.dat b/tests/data/test.dat deleted file mode 100644 index a4f9d46ede1b6d416e983a0c697415f89d3cc6f7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 159 zcma!HWME)oU|?WoW=_pZX%Yo885n?Kt)CXpv}fSR%`eR>NzBaaU|{6*&d*Lw$}CRI z0V&}q%8f5B$F=rT>TeNatQea6+%*kbC%}Om# s&0%KEtt=|aU|`PA%wc9J&dkYXV$aM=PR%P$1=|nO$mj?nfmVPh0AQIZ-2eap diff --git a/tests/orders.txt b/tests/orders.txt deleted file mode 100644 index 3e1e89acb..000000000 --- a/tests/orders.txt +++ /dev/null @@ -1,3 +0,0 @@ -ERESSEA 72vx "naeg86" -EINHEIT nqLx -BENENNEN EINHEIT 'Goldene Herde' From 71c4cb53c0a02f3942fdd99d4f5636af5cd553db Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 23:41:37 +0200 Subject: [PATCH 3/3] add missing guard.h include everywhere, remove duplicate prototypes from config.h --- src/alchemy.test.c | 2 ++ src/bind_unit.c | 1 + src/kernel/config.h | 10 ---------- src/move.test.c | 2 ++ src/spells/combatspells.c | 1 + tests/data/inactive | 2 -- 6 files changed, 6 insertions(+), 12 deletions(-) delete mode 100644 tests/data/inactive diff --git a/src/alchemy.test.c b/src/alchemy.test.c index 2a639243b..7c01be81b 100644 --- a/src/alchemy.test.c +++ b/src/alchemy.test.c @@ -11,6 +11,8 @@ #include #include +#include "guard.h" + #include #include diff --git a/src/bind_unit.c b/src/bind_unit.c index bd270318a..ed8eb74c3 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -19,6 +19,7 @@ without prior permission by the authors of Eressea. #include "move.h" #include "reports.h" #include "seen.h" +#include "guard.h" /* attributes includes */ #include diff --git a/src/kernel/config.h b/src/kernel/config.h index 91e130706..9449744ef 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -224,16 +224,6 @@ extern "C" { void setstatus(struct unit *u, int status); /* !< sets combatstatus of a unit */ - void setguard(struct unit *u, unsigned int flags); - /* !< setzt die guard-flags der Einheit */ - unsigned int getguard(const struct unit *u); - /* liest die guard-flags der Einheit */ - void guard(struct unit *u, unsigned int mask); - /* Einheit setzt "BEWACHE", rassenspezifzisch. - * 'mask' kann einzelne flags zusätzlich und-maskieren. - */ - unsigned int guard_flags(const struct unit *u); - int besieged(const struct unit *u); int maxworkingpeasants(const struct region *r); bool has_horses(const struct unit *u); diff --git a/src/move.test.c b/src/move.test.c index de515be16..f89665e72 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -2,6 +2,8 @@ #include #include "move.h" +#include "guard.h" + #include #include #include diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 4b8cc8b51..b20c6b42f 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -28,6 +28,7 @@ #include #include +#include #include #include diff --git a/tests/data/inactive b/tests/data/inactive deleted file mode 100644 index 7e1d0e64e..000000000 --- a/tests/data/inactive +++ /dev/null @@ -1,2 +0,0 @@ -c93c:Menschen:1:4 -c93c:Menschen:1:4