From 8971f59ecc5cc0547db3306b328d86f6f95e6b82 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Dec 2014 17:22:26 +0100 Subject: [PATCH] move more things out of config.c. - lighthouses into their own module. - lastregion/firstregion into reports.c. --- src/CMakeLists.txt | 1 + src/kernel/build.c | 1 + src/kernel/building.c | 1 + src/kernel/config.c | 204 ------------------------------------------ src/kernel/config.h | 12 --- src/kernel/faction.c | 4 +- src/kernel/save.c | 1 + src/kernel/ship.c | 2 - src/lighthouse.c | 150 +++++++++++++++++++++++++++++++ src/lighthouse.h | 36 ++++++++ src/move.c | 1 + src/reports.c | 67 ++++++++++++++ 12 files changed, 260 insertions(+), 220 deletions(-) create mode 100644 src/lighthouse.c create mode 100644 src/lighthouse.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f04c0cffc..b64ae7072 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,6 +70,7 @@ set (ERESSEA_SRC upkeep.c vortex.c names.c + lighthouse.c reports.c eressea.c callback.c diff --git a/src/kernel/build.c b/src/kernel/build.c index 808f7f87d..6c0b8c76e 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -25,6 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "move.h" #include "laws.h" #include "skill.h" +#include "lighthouse.h" /* kernel includes */ #include diff --git a/src/kernel/building.c b/src/kernel/building.c index ffed936a2..0fcce549e 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -30,6 +30,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "region.h" #include "skill.h" #include "save.h" +#include "lighthouse.h" #include "version.h" /* util includes */ diff --git a/src/kernel/config.c b/src/kernel/config.c index b154c2074..30f49a186 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -508,12 +508,10 @@ int shipspeed(const ship * sh, const unit * u) k *= SHIPSPEED; #endif -#ifdef SHIPDAMAGE if (sh->damage) k = (k * (sh->size * DAMAGE_SCALE - sh->damage) + sh->size * DAMAGE_SCALE - 1) / (sh->size * DAMAGE_SCALE); -#endif return (int)k; } @@ -733,56 +731,6 @@ int alliedunit(const unit * u, const faction * f2, int mode) return 0; } -static attrib_type at_lighthouse = { - "lighthouse" - /* Rest ist NULL; temporäres, nicht alterndes Attribut */ -}; - -/* update_lighthouse: call this function whenever the size of a lighthouse changes - * it adds temporary markers to the surrounding regions. - * The existence of markers says nothing about the quality of the observer in - * the lighthouse, for this may change more frequently. - */ -void update_lighthouse(building * lh) -{ - const struct building_type *bt_lighthouse = bt_find("lighthouse"); - if (bt_lighthouse && lh->type == bt_lighthouse) { - region *r = lh->region; - int d = (int)log10(lh->size) + 1; - int x; - - if (lh->size > 0) { - r->flags |= RF_LIGHTHOUSE; - } - - for (x = -d; x <= d; ++x) { - int y; - for (y = -d; y <= d; ++y) { - attrib *a; - region *r2; - int px = r->x + x, py = r->y + y; - pnormalize(&px, &py, rplane(r)); - r2 = findregion(px, py); - if (!r2 || !fval(r2->terrain, SEA_REGION)) - continue; - if (distance(r, r2) > d) - continue; - a = a_find(r2->attribs, &at_lighthouse); - while (a && a->type == &at_lighthouse) { - building *b = (building *)a->data.v; - if (b == lh) - break; - a = a->next; - } - if (!a) { - a = a_add(&r2->attribs, a_new(&at_lighthouse)); - a->data.v = (void *)lh; - } - } - } - } -} - int count_faction(const faction * f, int flags) { unit *u; @@ -1181,158 +1129,6 @@ int maxworkingpeasants(const struct region *r) return _max(size-treespace, _min(size / 10 , 200)); } -int lighthouse_range(const building * b, const faction * f) -{ - int d = 0; - if (fval(b, BLD_WORKING) && b->size >= 10) { - int maxd = (int)log10(b->size) + 1; - - if (skill_enabled(SK_PERCEPTION)) { - region *r = b->region; - int c = 0; - unit *u; - for (u = r->units; u; u = u->next) { - if (u->building == b || u == building_owner(b)) { - if (u->building == b) { - c += u->number; - } - if (c > buildingcapacity(b)) - break; - if (f == NULL || u->faction == f) { - int sk = eff_skill(u, SK_PERCEPTION, r) / 3; - d = _max(d, sk); - d = _min(maxd, d); - if (d == maxd) - break; - } - } - else if (c) - break; /* first unit that's no longer in the house ends the search */ - } - } - else { - /* E3A rule: no perception req'd */ - return maxd; - } - } - return d; -} - -bool check_leuchtturm(region * r, faction * f) -{ - attrib *a; - - if (!fval(r->terrain, SEA_REGION)) - return false; - - for (a = a_find(r->attribs, &at_lighthouse); a && a->type == &at_lighthouse; - a = a->next) { - building *b = (building *)a->data.v; - - assert(b->type == bt_find("lighthouse")); - if (fval(b, BLD_WORKING) && b->size >= 10) { - int maxd = (int)log10(b->size) + 1; - - if (skill_enabled(SK_PERCEPTION) && f) { - region *r2 = b->region; - unit *u; - int c = 0; - int d = 0; - - for (u = r2->units; u; u = u->next) { - if (u->building == b) { - c += u->number; - if (c > buildingcapacity(b)) - break; - if (f == NULL || u->faction == f) { - if (!d) - d = distance(r, r2); - if (maxd < d) - break; - if (eff_skill(u, SK_PERCEPTION, r) >= d * 3) - return true; - } - } - else if (c) - break; /* first unit that's no longer in the house ends the search */ - } - } - else { - /* E3A rule: no perception req'd */ - return maxd; - } - } - } - - return false; -} - -region *lastregion(faction * f) -{ -#ifdef SMART_INTERVALS - unit *u = f->units; - region *r = f->last; - - if (u == NULL) - return NULL; - if (r != NULL) - return r->next; - - /* it is safe to start in the region of the first unit. */ - f->last = u->region; - /* if regions have indices, we can skip ahead: */ - for (u = u->nextF; u != NULL; u = u->nextF) { - r = u->region; - if (r->index > f->last->index) - f->last = r; - } - - /* we continue from the best region and look for travelthru etc. */ - for (r = f->last->next; r; r = r->next) { - plane *p = rplane(r); - - /* search the region for travelthru-attributes: */ - if (fval(r, RF_TRAVELUNIT)) { - attrib *ru = a_find(r->attribs, &at_travelunit); - while (ru && ru->type == &at_travelunit) { - u = (unit *)ru->data.v; - if (u->faction == f) { - f->last = r; - break; - } - ru = ru->next; - } - } - if (f->last == r) - continue; - if (check_leuchtturm(r, f)) - f->last = r; - if (p && is_watcher(p, f)) { - f->last = r; - } - } - return f->last->next; -#else - return NULL; -#endif -} - -region *firstregion(faction * f) -{ -#ifdef SMART_INTERVALS - region *r = f->first; - - if (f->units == NULL) - return NULL; - if (r != NULL) - return r; - - return f->first = regions; -#else - return regions; -#endif -} - void **blk_list[1024]; int list_index; int blk_index; diff --git a/src/kernel/config.h b/src/kernel/config.h index e7d8e4011..9a44e7540 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -30,7 +30,6 @@ extern "C" { /* TODO: move these settings to settings.h or into configuration files */ #define GOBLINKILL /* Goblin-Spezialklau kann tödlich enden */ #define HERBS_ROT /* herbs owned by units have a chance to rot. */ -#define SHIPDAMAGE /* Schiffsbeschädigungen */ #define INSECT_POTION /* Spezialtrank für Insekten */ #define ORCIFICATION /* giving snotlings to the peasants gets counted */ @@ -86,7 +85,6 @@ extern "C" { * von struct unitname, etc. zurückgegeben werden. ohne die 0 */ #define BAGCAPACITY 20000 /* soviel paßt in einen Bag of Holding */ -#define STRENGTHCAPACITY 50000 /* zusätzliche Tragkraft beim Kraftzauber (deprecated) */ #define STRENGTHMULTIPLIER 50 /* multiplier for trollbelt */ /* ----------------- Befehle ----------------------------------- */ @@ -105,12 +103,6 @@ extern "C" { bool faction_id_is_unused(int); int max_magicians(const struct faction * f); - /* leuchtturm */ - bool check_leuchtturm(struct region *r, struct faction *f); - void update_lighthouse(struct building *lh); - int lighthouse_range(const struct building *b, - const struct faction *f); - int findoption(const char *s, const struct locale *lang); /* special units */ @@ -227,10 +219,6 @@ extern "C" { int weight(const struct unit *u); void changeblockchaos(void); - /* intervall, in dem die regionen der partei zu finden sind */ - struct region *firstregion(struct faction *f); - struct region *lastregion(struct faction *f); - bool idle(struct faction *f); bool unit_has_cursed_item(const struct unit *u); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index dbbd24aa0..f300a11c8 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -200,9 +200,9 @@ static int unused_faction_id(void) int id = rng_int() % MAX_FACTION_ID; while (!faction_id_is_unused(id)) { - id++; - if (id == MAX_FACTION_ID) + if (++id == MAX_FACTION_ID) { id = 0; + } } return id; diff --git a/src/kernel/save.c b/src/kernel/save.c index c390a6640..c4ea62b63 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -46,6 +46,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "terrain.h" #include "terrainid.h" /* only for conversion code */ #include "unit.h" +#include "lighthouse.h" #include "version.h" /* attributes includes */ diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 8e2c00100..8f29737dd 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -274,11 +274,9 @@ int shipcapacity(const ship * sh) if (sh->type->construction && sh->size != sh->type->construction->maxsize) return 0; -#ifdef SHIPDAMAGE if (sh->damage) { i = (int)ceil(i * (1.0 - sh->damage / sh->size / (double)DAMAGE_SCALE)); } -#endif return i; } diff --git a/src/lighthouse.c b/src/lighthouse.c new file mode 100644 index 000000000..f05687e69 --- /dev/null +++ b/src/lighthouse.c @@ -0,0 +1,150 @@ +#include +#include "lighthouse.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static attrib_type at_lighthouse = { + "lighthouse" + /* Rest ist NULL; temporäres, nicht alterndes Attribut */ +}; + +/* update_lighthouse: call this function whenever the size of a lighthouse changes +* it adds temporary markers to the surrounding regions. +* The existence of markers says nothing about the quality of the observer in +* the lighthouse, for this may change more frequently. +*/ +void update_lighthouse(building * lh) +{ + const struct building_type *bt_lighthouse = bt_find("lighthouse"); + if (bt_lighthouse && lh->type == bt_lighthouse) { + region *r = lh->region; + int d = (int)log10(lh->size) + 1; + int x; + + if (lh->size > 0) { + r->flags |= RF_LIGHTHOUSE; + } + + for (x = -d; x <= d; ++x) { + int y; + for (y = -d; y <= d; ++y) { + attrib *a; + region *r2; + int px = r->x + x, py = r->y + y; + pnormalize(&px, &py, rplane(r)); + r2 = findregion(px, py); + if (!r2 || !fval(r2->terrain, SEA_REGION)) + continue; + if (distance(r, r2) > d) + continue; + a = a_find(r2->attribs, &at_lighthouse); + while (a && a->type == &at_lighthouse) { + building *b = (building *)a->data.v; + if (b == lh) + break; + a = a->next; + } + if (!a) { + a = a_add(&r2->attribs, a_new(&at_lighthouse)); + a->data.v = (void *)lh; + } + } + } + } +} + +int lighthouse_range(const building * b, const faction * f) +{ + int d = 0; + if (fval(b, BLD_WORKING) && b->size >= 10) { + int maxd = (int)log10(b->size) + 1; + + if (skill_enabled(SK_PERCEPTION)) { + region *r = b->region; + int c = 0; + unit *u; + for (u = r->units; u; u = u->next) { + if (u->building == b || u == building_owner(b)) { + if (u->building == b) { + c += u->number; + } + if (c > buildingcapacity(b)) + break; + if (f == NULL || u->faction == f) { + int sk = eff_skill(u, SK_PERCEPTION, r) / 3; + d = _max(d, sk); + d = _min(maxd, d); + if (d == maxd) + break; + } + } + else if (c) + break; /* first unit that's no longer in the house ends the search */ + } + } + else { + /* E3A rule: no perception req'd */ + return maxd; + } + } + return d; +} + +bool check_leuchtturm(region * r, faction * f) +{ + attrib *a; + + if (!fval(r->terrain, SEA_REGION)) + return false; + + for (a = a_find(r->attribs, &at_lighthouse); a && a->type == &at_lighthouse; + a = a->next) { + building *b = (building *)a->data.v; + + assert(b->type == bt_find("lighthouse")); + if (fval(b, BLD_WORKING) && b->size >= 10) { + int maxd = (int)log10(b->size) + 1; + + if (skill_enabled(SK_PERCEPTION) && f) { + region *r2 = b->region; + unit *u; + int c = 0; + int d = 0; + + for (u = r2->units; u; u = u->next) { + if (u->building == b) { + c += u->number; + if (c > buildingcapacity(b)) + break; + if (f == NULL || u->faction == f) { + if (!d) + d = distance(r, r2); + if (maxd < d) + break; + if (eff_skill(u, SK_PERCEPTION, r) >= d * 3) + return true; + } + } + else if (c) + break; /* first unit that's no longer in the house ends the search */ + } + } + else { + /* E3A rule: no perception req'd */ + return maxd; + } + } + } + + return false; +} diff --git a/src/lighthouse.h b/src/lighthouse.h new file mode 100644 index 000000000..874e0fe8c --- /dev/null +++ b/src/lighthouse.h @@ -0,0 +1,36 @@ +/* +Copyright (c) 1998-2010, 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 LIGHTHOUSE_H +#define LIGHTHOUSE_H + +#ifdef __cplusplus +extern "C" { +#endif + + /* leuchtturm */ + bool check_leuchtturm(struct region *r, struct faction *f); + void update_lighthouse(struct building *lh); + int lighthouse_range(const struct building *b, + const struct faction *f); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/move.c b/src/move.c index 5ace63929..0e9111de4 100644 --- a/src/move.c +++ b/src/move.c @@ -25,6 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "alchemy.h" #include "vortex.h" #include "monster.h" +#include "lighthouse.h" #include #include diff --git a/src/reports.c b/src/reports.c index ad43fdfe9..c01a49bf2 100644 --- a/src/reports.c +++ b/src/reports.c @@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "reports.h" #include "laws.h" +#include "lighthouse.h" /* kernel includes */ #include @@ -1600,6 +1601,72 @@ static void prepare_reports(void) } } +static region *lastregion(faction * f) +{ +#ifdef SMART_INTERVALS + unit *u = f->units; + region *r = f->last; + + if (u == NULL) + return NULL; + if (r != NULL) + return r->next; + + /* it is safe to start in the region of the first unit. */ + f->last = u->region; + /* if regions have indices, we can skip ahead: */ + for (u = u->nextF; u != NULL; u = u->nextF) { + r = u->region; + if (r->index > f->last->index) + f->last = r; + } + + /* we continue from the best region and look for travelthru etc. */ + for (r = f->last->next; r; r = r->next) { + plane *p = rplane(r); + + /* search the region for travelthru-attributes: */ + if (fval(r, RF_TRAVELUNIT)) { + attrib *ru = a_find(r->attribs, &at_travelunit); + while (ru && ru->type == &at_travelunit) { + u = (unit *)ru->data.v; + if (u->faction == f) { + f->last = r; + break; + } + ru = ru->next; + } + } + if (f->last == r) + continue; + if (check_leuchtturm(r, f)) + f->last = r; + if (p && is_watcher(p, f)) { + f->last = r; + } + } + return f->last->next; +#else + return NULL; +#endif +} + +static region *firstregion(faction * f) +{ +#ifdef SMART_INTERVALS + region *r = f->first; + + if (f->units == NULL) + return NULL; + if (r != NULL) + return r; + + return f->first = regions; +#else + return regions; +#endif +} + static seen_region **prepare_report(faction * f) { struct seen_region *sr;