refactoring: move those count_* functions out of config.c

This commit is contained in:
Enno Rehling 2015-11-22 15:45:31 +01:00
parent 3bd458b5e8
commit 5f457f77b4
4 changed files with 81 additions and 78 deletions

View File

@ -94,7 +94,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
#include <math.h>
#include <limits.h> #include <limits.h>
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
@ -521,68 +520,6 @@ int alliedunit(const unit * u, const faction * f2, int mode)
} }
return 0; return 0;
} }
int count_faction(const faction * f, int flags)
{
unit *u;
int n = 0;
for (u = f->units; u; u = u->nextF) {
const race *rc = u_race(u);
int x = (flags&COUNT_UNITS) ? 1 : u->number;
if (f->race != rc) {
if (!playerrace(rc)) {
if (flags&COUNT_MONSTERS) {
n += x;
}
}
else if (flags&COUNT_MIGRANTS) {
if (!is_cursed(u->attribs, C_SLAVE, 0)) {
n += x;
}
}
}
else if (flags&COUNT_DEFAULT) {
n += x;
}
}
return n;
}
int count_units(const faction * f)
{
return count_faction(f, COUNT_ALL | COUNT_UNITS);
}
int count_all(const faction * f)
{
return count_faction(f, COUNT_ALL);
}
int count_migrants(const faction * f)
{
return count_faction(f, COUNT_MIGRANTS);
}
int count_maxmigrants(const faction * f)
{
static int migrants = -1;
if (migrants < 0) {
migrants = config_get_int("rules.migrants.max", INT_MAX);
}
if (migrants == INT_MAX) {
int x = 0;
if (f->race == get_race(RC_HUMAN)) {
int nsize = count_all(f);
if (nsize > 0) {
x = (int)(log10(nsize / 50.0) * 20);
if (x < 0)
x = 0;
}
}
return x;
}
return migrants;
}
void void
parse(keyword_t kword, int(*dofun) (unit *, struct order *), bool thisorder) parse(keyword_t kword, int(*dofun) (unit *, struct order *), bool thisorder)
{ {

View File

@ -119,18 +119,6 @@ struct param;
#define GIVE_DEFAULT (GIVE_SELF|GIVE_PEASANTS|GIVE_LUXURIES|GIVE_HERBS|GIVE_GOODS) #define GIVE_DEFAULT (GIVE_SELF|GIVE_PEASANTS|GIVE_LUXURIES|GIVE_HERBS|GIVE_GOODS)
int rule_give(void); int rule_give(void);
#define COUNT_MONSTERS 0x01
#define COUNT_MIGRANTS 0x02
#define COUNT_DEFAULT 0x04
#define COUNT_ALL 0x07
#define COUNT_UNITS 0x10
int count_faction(const struct faction * f, int flags);
int count_migrants(const struct faction * f);
int count_maxmigrants(const struct faction * f);
int count_all(const struct faction * f);
int count_units(const struct faction * f);
bool has_limited_skills(const struct unit *u); bool has_limited_skills(const struct unit *u);
const struct race *findrace(const char *, const struct locale *); const struct race *findrace(const char *, const struct locale *);

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alliance.h" #include "alliance.h"
#include "ally.h" #include "ally.h"
#include "curse.h"
#include "equipment.h" #include "equipment.h"
#include "group.h" #include "group.h"
#include "item.h" #include "item.h"
@ -54,10 +55,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
faction *factions; faction *factions;
@ -712,3 +714,66 @@ void faction_setorigin(faction * f, int id, int x, int y)
addlist(&f->ursprung, ur); addlist(&f->ursprung, ur);
} }
int count_faction(const faction * f, int flags)
{
unit *u;
int n = 0;
for (u = f->units; u; u = u->nextF) {
const race *rc = u_race(u);
int x = (flags&COUNT_UNITS) ? 1 : u->number;
if (f->race != rc) {
if (!playerrace(rc)) {
if (flags&COUNT_MONSTERS) {
n += x;
}
}
else if (flags&COUNT_MIGRANTS) {
if (!is_cursed(u->attribs, C_SLAVE, 0)) {
n += x;
}
}
}
else if (flags&COUNT_DEFAULT) {
n += x;
}
}
return n;
}
int count_units(const faction * f)
{
return count_faction(f, COUNT_ALL | COUNT_UNITS);
}
int count_all(const faction * f)
{
return count_faction(f, COUNT_ALL);
}
int count_migrants(const faction * f)
{
return count_faction(f, COUNT_MIGRANTS);
}
int count_maxmigrants(const faction * f)
{
static int migrants = -1;
if (migrants < 0) {
migrants = config_get_int("rules.migrants.max", INT_MAX);
}
if (migrants == INT_MAX) {
int x = 0;
if (f->race == get_race(RC_HUMAN)) {
int nsize = count_all(f);
if (nsize > 0) {
x = (int)(log10(nsize / 50.0) * 20);
if (x < 0)
x = 0;
}
}
return x;
}
return migrants;
}

View File

@ -162,6 +162,19 @@ extern "C" {
int count_skill(struct faction *f, skill_t sk); int count_skill(struct faction *f, skill_t sk);
bool faction_id_is_unused(int); bool faction_id_is_unused(int);
#define COUNT_MONSTERS 0x01
#define COUNT_MIGRANTS 0x02
#define COUNT_DEFAULT 0x04
#define COUNT_ALL 0x07
#define COUNT_UNITS 0x10
int count_faction(const struct faction * f, int flags);
int count_migrants(const struct faction * f);
int count_maxmigrants(const struct faction * f);
int count_all(const struct faction * f);
int count_units(const struct faction * f);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif