From 40d9ebfa943b59981db007e06fb8abcfe4f1b1e3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Aug 2016 19:37:47 +0100 Subject: [PATCH] reduce code duplication: itemcloak code was copied between NR and CR. --- src/creport.c | 30 ++++++++--------------------- src/kernel/config.c | 17 +++++------------ src/reports.c | 40 ++++++++++++++++++--------------------- src/reports.h | 2 +- src/spells/combatspells.c | 8 +------- 5 files changed, 33 insertions(+), 64 deletions(-) diff --git a/src/creport.c b/src/creport.c index 6ba9f5e39..39ecd9c2b 100644 --- a/src/creport.c +++ b/src/creport.c @@ -742,13 +742,10 @@ void cr_output_unit(stream *out, const region * r, const faction * f, const char *str; const item_type *lasttype; int pr; - item *itm, *show; + item *itm, *show = NULL; building *b; const char *pzTmp; skill *sv; - bool itemcloak = false; - static const curse_type *itemcloak_ct = 0; - static bool init = false; item result[MAX_INVENTORY]; const faction *sf; const char *prefix; @@ -758,15 +755,6 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (fval(u_race(u), RCF_INVISIBLE)) return; - if (!init) { - init = true; - itemcloak_ct = ct_find("itemcloak"); - } - if (itemcloak_ct != NULL) { - curse * cu = get_curse(u->attribs, itemcloak_ct); - itemcloak = cu && curse_active(cu); - } - stream_printf(out, "EINHEIT %d\n", u->no); stream_printf(out, "\"%s\";Name\n", unit_getname(u)); str = u_description(u, f->locale); @@ -970,16 +958,14 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (f == u->faction || omniscient(f)) { show = u->items; } - else if (!itemcloak && mode >= see_unit) { - int n = report_items(u->items, result, MAX_INVENTORY, u, f); - assert(n >= 0); - if (n > 0) - show = result; - else - show = NULL; - } else { - show = NULL; + if (mode >= see_unit) { + int n = report_items(u, result, MAX_INVENTORY, u, f); + assert(n >= 0); + if (n > 0) { + show = result; + } + } } lasttype = NULL; for (itm = show; itm; itm = itm->next) { diff --git a/src/kernel/config.c b/src/kernel/config.c index cda449a23..feaf76aa8 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -897,18 +897,10 @@ default_wage(const region * r, const faction * f, const race * rc, int in_turn) { building *b = largestbuilding(r, &cmp_wage, false); int esize = 0; - curse *c; double wage; attrib *a; const building_type *artsculpture_type = bt_find("artsculpture"); - static const curse_type *drought_ct, *blessedharvest_ct; - static bool init; - - if (!init) { - init = true; - drought_ct = ct_find("drought"); - blessedharvest_ct = ct_find("blessedharvest"); - } + const struct curse_type *ctype; if (b != NULL) { /* TODO: this reveals imaginary castles */ @@ -937,7 +929,7 @@ default_wage(const region * r, const faction * f, const race * rc, int in_turn) } if (rule_blessed_harvest() == HARVEST_WORK) { /* E1 rules */ - wage += curse_geteffect(get_curse(r->attribs, blessedharvest_ct)); + wage += curse_geteffect(get_curse(r->attribs, ct_find("blessedharvest"))); } } @@ -954,8 +946,9 @@ default_wage(const region * r, const faction * f, const race * rc, int in_turn) } /* Bei einer Dürre verdient man nur noch ein Viertel */ - if (drought_ct) { - c = get_curse(r->attribs, drought_ct); + ctype = ct_find("drought"); + if (ctype) { + curse *c = get_curse(r->attribs, ctype); if (curse_active(c)) wage /= curse_geteffect(c); } diff --git a/src/reports.c b/src/reports.c index d27f2ad70..7173bcf3f 100644 --- a/src/reports.c +++ b/src/reports.c @@ -265,16 +265,24 @@ static size_t buforder(char *buffer, size_t size, const order * ord, int mode) * \param viewer: the faction looking at the items */ int -report_items(const item * items, item * result, int size, const unit * owner, -const faction * viewer) +report_items(const unit *u, item * result, int size, const unit * owner, + const faction * viewer) { - const item *itm; + const item *itm, *items = u->items; int n = 0; /* number of results */ + bool itemcloak = false; + const curse_type *itemcloak_ct = ct_find("itemcloak"); - assert(owner == NULL || viewer != owner->faction - || !"not required for owner=viewer!"); + assert(owner == NULL || viewer != owner->faction); assert(size); + if (itemcloak_ct) { + curse * cu = get_curse(u->attribs, itemcloak_ct); + itemcloak = cu && curse_active(cu); + } + if (itemcloak) { + return 0; + } for (itm = items; itm; itm = itm->next) { item *ishow; const char *ic; @@ -455,20 +463,12 @@ size_t size) const char *pzTmp, *str; building *b; bool isbattle = (bool)(mode == see_battle); - item *itm; - item *show; + item *itm, *show = NULL; faction *fv = visible_faction(f, u); char *bufp = buf; - bool itemcloak = false; - const curse_type *itemcloak_ct = 0; int result = 0; item results[MAX_INVENTORY]; - itemcloak_ct = ct_find("itemcloak"); - if (itemcloak_ct) { - itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); - } - bufp = STRLCPY(bufp, unitname(u), size); if (!isbattle) { @@ -599,16 +599,12 @@ size_t size) if (f == u->faction || omniscient(f)) { show = u->items; } - else if (!itemcloak && mode >= see_unit) { - int n = report_items(u->items, results, MAX_INVENTORY, u, f); + else if (mode >= see_unit) { + int n = report_items(u, results, MAX_INVENTORY, u, f); assert(n >= 0); - if (n > 0) + if (n > 0) { show = results; - else - show = NULL; - } - else { - show = NULL; + } } for (itm = show; itm; itm = itm->next) { const char *ic; diff --git a/src/reports.h b/src/reports.h index 044e65042..be99fbba7 100644 --- a/src/reports.h +++ b/src/reports.h @@ -107,7 +107,7 @@ extern "C" { void view_neighbours(struct seen_region **seen, struct region * r, struct faction * f); int report_resources(const struct seen_region *sr, struct resource_report *result, int size, const struct faction *viewer); - int report_items(const struct item *items, struct item *result, int size, + int report_items(const struct unit *u, struct item *result, int size, const struct unit *owner, const struct faction *viewer); void report_item(const struct unit *owner, const struct item *i, const struct faction *viewer, const char **name, const char **basename, diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 0ee3755b3..bcbbb1b52 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -959,13 +959,7 @@ int sp_strong_wall(struct castorder * co) unit *mage = fi->unit; building *burg; double effect; - static bool init = false; message *msg; - static const curse_type *strongwall_ct; - if (!init) { - init = true; - strongwall_ct = ct_find("strongwall"); - } if (!mage->building) { return 0; @@ -973,7 +967,7 @@ int sp_strong_wall(struct castorder * co) burg = mage->building; effect = power / 4; - create_curse(mage, &burg->attribs, strongwall_ct, power, 1, effect, 0); + create_curse(mage, &burg->attribs, ct_find("strongwall"), power, 1, effect, 0); msg = msg_message("sp_strongwalls_effect", "mage building", mage, mage->building);