From 535a9e6e5376cd8a78e5d774c42378ea46702727 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 26 Apr 2014 12:24:51 -0700 Subject: [PATCH] fix number of units in the report header --- src/kernel/config.c | 43 ++++++++++++++++++------- src/kernel/config.h | 9 +++--- src/kernel/faction.c | 2 +- src/kernel/unit.c | 76 ++++++++++++++++++++++++-------------------- src/report.c | 10 ++++-- 5 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 5c285bec6..41e994e5c 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1196,19 +1196,40 @@ void update_lighthouse(building * lh) int count_all(const faction * f) { #ifndef NDEBUG - int n = 0; - unit *u; - for (u = f->units; u; u = u->nextF) { - if (playerrace(u_race(u))) { - n += u->number; - assert(f == u->faction); + unit *u; + int np = 0, n = 0; + for (u = f->units; u; u = u->nextF) { + assert(f == u->faction); + n += u->number; + if (playerrace(u_race(u))) { + np += u->number; + } + } + if (f->num_people != np) { + 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); } - } - if (f->num_people != n) { - log_error("# of people in %s is != num_people: %d should be %d.\n", factionid(f), f->num_people, n); - } #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) diff --git a/src/kernel/config.h b/src/kernel/config.h index f4b2ceff6..b7c7e053c 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -256,9 +256,10 @@ extern "C" { extern int rule_alliance_limit(void); extern int rule_faction_limit(void); - extern int count_all(const struct faction *f); - extern int count_migrants(const struct faction *f); - extern int count_maxmigrants(const struct faction *f); + int count_units(const struct faction * f); + int count_all(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 const struct race *findrace(const char *, const struct locale *); @@ -449,7 +450,7 @@ extern "C" { extern struct attrib_type at_guard; extern void free_gamedata(void); #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 # define count_unit(u) 1 #endif diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 456cc3133..34d7071e2 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -112,7 +112,7 @@ faction *get_monsters(void) if (!monsters) { faction *f; for (f = factions; f; f = f->next) { - if (f->flags & FFL_NPC) { + if ((f->flags & FFL_NPC) && !(f->flags & FFL_DEFENDER)) { return monsters = f; } } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 1be61c138..568d8161f 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1070,43 +1070,51 @@ struct building *inside_building(const struct unit *u) void u_setfaction(unit * u, faction * f) { - int cnt = u->number; + int cnt = u->number; + if (u->faction == f) + return; + if (u->faction) { + if (count_unit(u)) { + --u->faction->no_units; + } + set_number(u, 0); + join_group(u, NULL); + free_orders(&u->orders); + set_order(&u->thisorder, NULL); - if (u->faction == f) - return; - if (u->faction) { - set_number(u, 0); - if (count_unit(u)) - --u->faction->no_units; - join_group(u, NULL); - free_orders(&u->orders); - set_order(&u->thisorder, NULL); + if (u->nextF) { + u->nextF->prevF = u->prevF; + } + if (u->prevF) { + u->prevF->nextF = u->nextF; + } + else { + u->faction->units = u->nextF; + } + } - if (u->nextF) - u->nextF->prevF = u->prevF; - if (u->prevF) - u->prevF->nextF = u->nextF; - else - u->faction->units = u->nextF; - } + if (f != NULL) { + if (f->units) { + f->units->prevF = u; + } + u->prevF = NULL; + u->nextF = f->units; + f->units = u; + } + else { + u->nextF = NULL; + } - if (f != NULL) { - if (f->units) - f->units->prevF = u; - u->prevF = NULL; - u->nextF = f->units; - f->units = u; - } else - u->nextF = NULL; - - u->faction = f; - if (u->region) - update_interval(f, u->region); - if (cnt && f) { - set_number(u, cnt); - if (count_unit(u)) - ++f->no_units; - } + u->faction = f; + if (u->region) { + update_interval(f, u->region); + } + if (cnt) { + set_number(u, cnt); + } + if (f && count_unit(u)) { + ++f->no_units; + } } /* vorsicht Sprüche können u->number == RS_FARVISION haben! */ diff --git a/src/report.c b/src/report.c index 415897f3a..261d0a2cc 100644 --- a/src/report.c +++ b/src/report.c @@ -2199,8 +2199,14 @@ report_plaintext(const char *filename, report_context * ctx, f->num_people = no_people; } #else - no_units = f->no_units; - no_people = f->num_people; + 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; + } #endif m = msg_message("nr_population", "population units", no_people, no_units); nr_render(m, f->locale, buf, sizeof(buf), f);