From 59c1c2332485861e2b19f412321e8dda4a43cc8f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 1 Nov 2014 18:34:53 +0100 Subject: [PATCH] refactor config.c, move the find* functions into the modules they belong to. --- src/battle.c | 28 -------------- src/creation.c | 2 +- src/kernel/config.c | 90 -------------------------------------------- src/kernel/config.h | 24 +++--------- src/kernel/faction.c | 34 +++++++++++++++++ src/kernel/faction.h | 4 ++ src/kernel/unit.c | 33 ++++++++++++++++ src/kernel/unit.h | 13 +++++-- src/laws.c | 28 ++++++++++++++ src/laws.h | 1 + 10 files changed, 116 insertions(+), 141 deletions(-) diff --git a/src/battle.c b/src/battle.c index 7b2bc236b..a5e553f2e 100644 --- a/src/battle.c +++ b/src/battle.c @@ -210,34 +210,6 @@ static void message_faction(battle * b, faction * f, struct message *m) add_message(&f->battles->msgs, m); } -int armedmen(const unit * u, bool siege_weapons) -{ - item *itm; - int n = 0; - if (!(urace(u)->flags & RCF_NOWEAPONS)) { - if (effskill(u, SK_WEAPONLESS) >= 1) { - /* kann ohne waffen bewachen: fuer drachen */ - n = u->number; - } - else { - /* alle Waffen werden gezaehlt, und dann wird auf die Anzahl - * Personen minimiert */ - for (itm = u->items; itm; itm = itm->next) { - const weapon_type *wtype = resource2weapon(itm->type->rtype); - if (wtype == NULL || (!siege_weapons && (wtype->flags & WTF_SIEGE))) - continue; - if (effskill(u, wtype->skill) >= wtype->minskill) - n += itm->number; - /* if (effskill(u, wtype->skill) >= wtype->minskill) n += itm->number; */ - if (n > u->number) - break; - } - n = _min(n, u->number); - } - } - return n; -} - void message_all(battle * b, message * m) { bfaction *bf; diff --git a/src/creation.c b/src/creation.c index f8e0ae502..7cf540f30 100644 --- a/src/creation.c +++ b/src/creation.c @@ -1,5 +1,5 @@ /* -Copyright (c) 1998-2010, Enno Rehling +Copyright (c) 1998-2014, Enno Rehling Katja Zedel diff --git a/src/kernel/config.c b/src/kernel/config.c index 1cff32930..b2889af8a 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -520,34 +520,6 @@ int shipspeed(const ship * sh, const unit * u) return (int)k; } -#define FMAXHASH 2039 -faction *factionhash[FMAXHASH]; - -void fhash(faction * f) -{ - int index = f->no % FMAXHASH; - f->nexthash = factionhash[index]; - factionhash[index] = f; -} - -void funhash(faction * f) -{ - int index = f->no % FMAXHASH; - faction **fp = factionhash + index; - while (*fp && (*fp) != f) - fp = &(*fp)->nexthash; - *fp = f->nexthash; -} - -static faction *ffindhash(int no) -{ - int index = no % FMAXHASH; - faction *f = factionhash[index]; - while (f && f->no != no) - f = f->nexthash; - return f; -} - /* ----------------------------------------------------------------------- */ void verify_data(void) @@ -633,29 +605,6 @@ unsigned int atoip(const char *s) return n; } -region *findunitregion(const unit * su) -{ -#ifndef SLOW_REGION - return su->region; -#else - region *r; - const unit *u; - - for (r = regions; r; r = r->next) { - for (u = r->units; u; u = u->next) { - if (su == u) { - return r; - } - } - } - - /* This should never happen */ - assert(!"Die unit wurde nicht gefunden"); - - return (region *) NULL; -#endif -} - bool unit_has_cursed_item(unit * u) { item *itm = u->items; @@ -1036,50 +985,11 @@ param_t getparam(const struct locale * lang) return s ? findparam(s, lang) : NOPARAM; } -faction *findfaction(int n) -{ - faction *f = ffindhash(n); - return f; -} - faction *getfaction(void) { return findfaction(getid()); } -unit *findunitr(const region * r, int n) -{ - unit *u; - - /* findunit regional! */ - - for (u = r->units; u; u = u->next) - if (u->no == n) - return u; - - return 0; -} - -unit *findunit(int n) -{ - if (n <= 0) { - return NULL; - } - return ufindhash(n); -} - -unit *findunitg(int n, const region * hint) -{ - - /* Abfangen von Syntaxfehlern. */ - if (n <= 0) - return NULL; - - /* findunit global! */ - hint = 0; - return ufindhash(n); -} - unit *getnewunit(const region * r, const faction * f) { int n; diff --git a/src/kernel/config.h b/src/kernel/config.h index 95da6ba63..570d7c953 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -102,11 +102,6 @@ extern "C" { #define i2b(i) ((bool)((i)?(true):(false))) - typedef struct strlist { - struct strlist *next; - char *s; - } strlist; - #define fval(u, i) ((u)->flags & (i)) #define fset(u, i) ((u)->flags |= (i)) #define freset(u, i) ((u)->flags &= ~(i)) @@ -126,9 +121,13 @@ extern "C" { /* special units */ void make_undead_unit(struct unit *); - void addstrlist(strlist ** SP, const char *s); + typedef struct strlist { + struct strlist *next; + char *s; + } strlist; - int armedmen(const struct unit *u, bool siege_weapons); + void addstrlist(strlist ** SP, const char *s); + void freestrlist(strlist * s); unsigned int atoip(const char *s); unsigned int getuint(void); @@ -179,15 +178,8 @@ extern "C" { int alliedgroup(const struct plane *pl, const struct faction *f, const struct faction *f2, const struct ally *sf, int mode); - struct faction *findfaction(int n); struct faction *getfaction(void); - struct unit *findunitg(int n, const struct region *hint); - struct unit *findunit(int n); - - struct unit *findunitr(const struct region *r, int n); - struct region *findunitregion(const struct unit *su); - char *estring(const char *s); char *estring_i(char *s); char *cstring(const char *s); @@ -240,7 +232,6 @@ extern "C" { * sonst großes Unglück. Durch asserts an ein paar Stellen abgesichert. */ void verify_data(void); - void freestrlist(strlist * s); int change_hitpoints(struct unit *u, int value); @@ -251,9 +242,6 @@ extern "C" { struct region *firstregion(struct faction *f); struct region *lastregion(struct faction *f); - void fhash(struct faction *f); - void funhash(struct faction *f); - bool idle(struct faction *f); bool unit_has_cursed_item(struct unit *u); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 5cb152e47..b1adb70eb 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -99,6 +99,40 @@ void free_faction(faction * f) freelist(f->ursprung); } +#define FMAXHASH 2039 +faction *factionhash[FMAXHASH]; + +void fhash(faction * f) +{ + int index = f->no % FMAXHASH; + f->nexthash = factionhash[index]; + factionhash[index] = f; +} + +void funhash(faction * f) +{ + int index = f->no % FMAXHASH; + faction **fp = factionhash + index; + while (*fp && (*fp) != f) + fp = &(*fp)->nexthash; + *fp = f->nexthash; +} + +static faction *ffindhash(int no) +{ + int index = no % FMAXHASH; + faction *f = factionhash[index]; + while (f && f->no != no) + f = f->nexthash; + return f; +} + +faction *findfaction(int n) +{ + faction *f = ffindhash(n); + return f; +} + void set_show_item(faction * f, const struct item_type *itype) { attrib *a = a_add(&f->attribs, a_new(&at_showitem)); diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 92878ceef..25d8f20b5 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -112,6 +112,10 @@ typedef struct faction { extern struct faction *factions; +void fhash(struct faction *f); +void funhash(struct faction *f); + +struct faction *findfaction(int n); struct faction *get_monsters(void); struct faction *get_or_create_monsters(void); int max_magicians(const faction * f); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index a99023919..affd4471d 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -72,6 +72,39 @@ attrib_type at_creator = { /* Rest ist NULL; temporaeres, nicht alterndes Attribut */ }; +unit *findunitr(const region * r, int n) +{ + unit *u; + + /* findunit regional! */ + + for (u = r->units; u; u = u->next) + if (u->no == n) + return u; + + return 0; +} + +unit *findunit(int n) +{ + if (n <= 0) { + return NULL; + } + return ufindhash(n); +} + +unit *findunitg(int n, const region * hint) +{ + + /* Abfangen von Syntaxfehlern. */ + if (n <= 0) + return NULL; + + /* findunit global! */ + hint = 0; + return ufindhash(n); +} + #define UMAXHASH MAXUNITS static unit *unithash[UMAXHASH]; static unit *delmarker = (unit *)unithash; /* a funny hack */ diff --git a/src/kernel/unit.h b/src/kernel/unit.h index a3db732dd..6a089d2d4 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -114,6 +114,7 @@ extern "C" { int wants; /* enno: attribut? */ } unit; + extern struct attrib_type at_creator; extern struct attrib_type at_alias; extern struct attrib_type at_siege; extern struct attrib_type at_target; @@ -216,9 +217,9 @@ extern "C" { int number, const struct race *rc, int id, const char *dname, struct unit *creator); - extern void uhash(struct unit *u); - extern void uunhash(struct unit *u); - extern struct unit *ufindhash(int i); + void uhash(struct unit *u); + void uunhash(struct unit *u); + struct unit *ufindhash(int i); const char *unit_getname(const struct unit *u); void unit_setname(struct unit *u, const char *name); @@ -241,7 +242,11 @@ extern "C" { void remove_empty_units_in_region(struct region * r); void remove_empty_units(void); - extern struct attrib_type at_creator; + struct unit *findunitg(int n, const struct region *hint); + struct unit *findunit(int n); + + struct unit *findunitr(const struct region *r, int n); + #ifdef __cplusplus } #endif diff --git a/src/laws.c b/src/laws.c index a64cda300..474f37299 100755 --- a/src/laws.c +++ b/src/laws.c @@ -4182,6 +4182,34 @@ void process(void) } +int armedmen(const unit * u, bool siege_weapons) +{ + item *itm; + int n = 0; + if (!(urace(u)->flags & RCF_NOWEAPONS)) { + if (effskill(u, SK_WEAPONLESS) >= 1) { + /* kann ohne waffen bewachen: fuer drachen */ + n = u->number; + } + else { + /* alle Waffen werden gezaehlt, und dann wird auf die Anzahl + * Personen minimiert */ + for (itm = u->items; itm; itm = itm->next) { + const weapon_type *wtype = resource2weapon(itm->type->rtype); + if (wtype == NULL || (!siege_weapons && (wtype->flags & WTF_SIEGE))) + continue; + if (effskill(u, wtype->skill) >= wtype->minskill) + n += itm->number; + /* if (effskill(u, wtype->skill) >= wtype->minskill) n += itm->number; */ + if (n > u->number) + break; + } + n = _min(n, u->number); + } + } + return n; +} + int siege_cmd(unit * u, order * ord) { region *r = u->region; diff --git a/src/laws.h b/src/laws.h index 8ab6ea469..02fdc6c09 100755 --- a/src/laws.h +++ b/src/laws.h @@ -99,6 +99,7 @@ extern "C" { int modifier); bool seefaction(const struct faction *f, const struct region *r, const struct unit *u, int modifier); + int armedmen(const struct unit *u, bool siege_weapons); #ifdef __cplusplus