fix number of units in the report header

This commit is contained in:
Enno Rehling 2014-04-26 12:24:51 -07:00
parent 2c4cae4c7e
commit 535a9e6e53
5 changed files with 88 additions and 52 deletions

View file

@ -1196,19 +1196,40 @@ void update_lighthouse(building * lh)
int count_all(const faction * f) int count_all(const faction * f)
{ {
#ifndef NDEBUG #ifndef NDEBUG
int n = 0;
unit *u; unit *u;
int np = 0, n = 0;
for (u = f->units; u; u = u->nextF) { for (u = f->units; u; u = u->nextF) {
if (playerrace(u_race(u))) {
n += u->number;
assert(f == u->faction); assert(f == u->faction);
n += u->number;
if (playerrace(u_race(u))) {
np += u->number;
} }
} }
if (f->num_people != n) { if (f->num_people != np) {
log_error("# of people in %s is != num_people: %d should be %d.\n", factionid(f), f->num_people, n); log_error("# of people in %s is != num_people: %d should be %d.\n", factionid(f), f->num_people, np);
}
if (f->num_total != n) {
log_error("# of men in %s is != num_total: %d should be %d.\n", factionid(f), f->num_total, n);
} }
#endif #endif
return f->num_people; return (f->flags & FFL_NPC) ? f->num_total : f->num_people;
}
int count_units(const faction * f)
{
#ifndef NDEBUG
unit *u;
int n = 0, np = 0;
for (u = f->units; u; u = u->nextF) {
++n;
if (playerrace(u_race(u))) ++np;
}
n = (f->flags & FFL_NPC) ? n : np;
if (f->no_units && n != f->no_units) {
log_warning("# of units in %s is != no_units: %d should be %d.\n", factionid(f), f->no_units, n);
}
#endif
return n;
} }
int count_migrants(const faction * f) int count_migrants(const faction * f)

View file

@ -256,9 +256,10 @@ extern "C" {
extern int rule_alliance_limit(void); extern int rule_alliance_limit(void);
extern int rule_faction_limit(void); extern int rule_faction_limit(void);
extern int count_all(const struct faction *f); int count_units(const struct faction * f);
extern int count_migrants(const struct faction *f); int count_all(const struct faction *f);
extern int count_maxmigrants(const struct faction *f); int count_migrants(const struct faction *f);
int count_maxmigrants(const struct faction *f);
extern bool has_limited_skills(const struct unit *u); extern bool has_limited_skills(const struct unit *u);
extern const struct race *findrace(const char *, const struct locale *); extern const struct race *findrace(const char *, const struct locale *);
@ -449,7 +450,7 @@ extern "C" {
extern struct attrib_type at_guard; extern struct attrib_type at_guard;
extern void free_gamedata(void); extern void free_gamedata(void);
#if 1 /* disable to count all units */ #if 1 /* disable to count all units */
# define count_unit(u) playerrace(u_race(u)) # define count_unit(u) (u->number>0 && playerrace(u_race(u)))
#else #else
# define count_unit(u) 1 # define count_unit(u) 1
#endif #endif

View file

@ -112,7 +112,7 @@ faction *get_monsters(void)
if (!monsters) { if (!monsters) {
faction *f; faction *f;
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
if (f->flags & FFL_NPC) { if ((f->flags & FFL_NPC) && !(f->flags & FFL_DEFENDER)) {
return monsters = f; return monsters = f;
} }
} }

View file

@ -1071,40 +1071,48 @@ struct building *inside_building(const struct unit *u)
void u_setfaction(unit * u, faction * f) void u_setfaction(unit * u, faction * f)
{ {
int cnt = u->number; int cnt = u->number;
if (u->faction == f) if (u->faction == f)
return; return;
if (u->faction) { if (u->faction) {
set_number(u, 0); if (count_unit(u)) {
if (count_unit(u))
--u->faction->no_units; --u->faction->no_units;
}
set_number(u, 0);
join_group(u, NULL); join_group(u, NULL);
free_orders(&u->orders); free_orders(&u->orders);
set_order(&u->thisorder, NULL); set_order(&u->thisorder, NULL);
if (u->nextF) if (u->nextF) {
u->nextF->prevF = u->prevF; u->nextF->prevF = u->prevF;
if (u->prevF) }
if (u->prevF) {
u->prevF->nextF = u->nextF; u->prevF->nextF = u->nextF;
else }
else {
u->faction->units = u->nextF; u->faction->units = u->nextF;
} }
}
if (f != NULL) { if (f != NULL) {
if (f->units) if (f->units) {
f->units->prevF = u; f->units->prevF = u;
}
u->prevF = NULL; u->prevF = NULL;
u->nextF = f->units; u->nextF = f->units;
f->units = u; f->units = u;
} else }
else {
u->nextF = NULL; u->nextF = NULL;
}
u->faction = f; u->faction = f;
if (u->region) if (u->region) {
update_interval(f, u->region); update_interval(f, u->region);
if (cnt && f) { }
if (cnt) {
set_number(u, cnt); set_number(u, cnt);
if (count_unit(u)) }
if (f && count_unit(u)) {
++f->no_units; ++f->no_units;
} }
} }

View file

@ -2199,8 +2199,14 @@ report_plaintext(const char *filename, report_context * ctx,
f->num_people = no_people; f->num_people = no_people;
} }
#else #else
no_units = f->no_units; no_units = count_units(f);
no_people = count_all(f);
if (f->flags & FFL_NPC) {
no_people = f->num_total;
}
else {
no_people = f->num_people; no_people = f->num_people;
}
#endif #endif
m = msg_message("nr_population", "population units", no_people, no_units); m = msg_message("nr_population", "population units", no_people, no_units);
nr_render(m, f->locale, buf, sizeof(buf), f); nr_render(m, f->locale, buf, sizeof(buf), f);