refactor config.c, move the find* functions into the modules they belong to.

This commit is contained in:
Enno Rehling 2014-11-01 18:34:53 +01:00
parent 27f94783bc
commit 59c1c23324
10 changed files with 116 additions and 141 deletions

View File

@ -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;

View File

@ -1,5 +1,5 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Copyright (c) 1998-2014, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>

View File

@ -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;

View File

@ -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);

View File

@ -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));

View File

@ -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);

View File

@ -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 */

View File

@ -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

View File

@ -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;

View File

@ -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