From d633c2a9fbeb74764eeaf36c73f3cc7f9874f7bd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Mar 2017 19:36:26 +0100 Subject: [PATCH] BUG 2310: counting units and people. reduce number of count_all loops made, make f->num_units and f->num_people be correct. --- src/creport.c | 2 +- src/kernel/faction.c | 14 ++------------ src/kernel/faction.h | 7 +------ src/kernel/save.c | 5 +---- src/kernel/unit.c | 6 +++--- src/laws.c | 9 ++++----- src/modules/score.c | 4 ++-- src/report.c | 10 ++-------- src/summary.c | 8 ++++---- 9 files changed, 20 insertions(+), 45 deletions(-) diff --git a/src/creport.c b/src/creport.c index d16f87f22..fd307c5ec 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1558,7 +1558,7 @@ report_computer(const char *filename, report_context * ctx, const char *bom) translate(prefix, LOC(f->locale, prefix))); } fprintf(F, "%d;Rekrutierungskosten\n", f->race->recruitcost); - fprintf(F, "%d;Anzahl Personen\n", count_all(f)); + fprintf(F, "%d;Anzahl Personen\n", f->num_people); fprintf(F, "\"%s\";Magiegebiet\n", magic_school[f->magiegebiet]); if (rc_changed(&rc_cache)) { diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 139474048..03cea282f 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -712,7 +712,7 @@ int count_faction(const faction * f, int flags) 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; + int x = u->number; if (f->race != rc) { if (!playerrace(rc)) { if (flags&COUNT_MONSTERS) { @@ -732,16 +732,6 @@ int count_faction(const faction * f, int flags) 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); @@ -752,7 +742,7 @@ int count_maxmigrants(const faction * f) int formula = rc_migrants_formula(f->race); if (formula == MIGRANTS_LOG10) { - int nsize = count_all(f); + int nsize = f->num_people; if (nsize > 0) { int x = (int)(log10(nsize / 50.0) * 20); if (x < 0) x = 0; diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 80248210a..9b290e5da 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -77,9 +77,8 @@ extern "C" { magic_t magiegebiet; int newbies; int num_people; /* Anzahl Personen ohne Monster */ - int num_total; /* Anzahl Personen mit Monstern */ + int num_units; int options; - int no_units; struct ally *allies; /* alliedgroup and others should check sf.faction.alive before using a faction from f.allies */ struct group *groups; /* alliedgroup and others should check sf.faction.alive before using a faction from f.groups */ int nregions; @@ -165,14 +164,10 @@ extern "C" { #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); int max_magicians(const struct faction * f); struct faction *getfaction(void); diff --git a/src/kernel/save.c b/src/kernel/save.c index e487737f9..5be078d8a 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -657,10 +657,7 @@ unit *read_unit(struct gamedata *data) if (f != u->faction) { u_setfaction(u, f); } - if (u->faction) { - ++u->faction->no_units; - } - else { + if (!u->faction) { log_error("unit %s has faction == NULL", itoa36(u->no)); return 0; } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 595accba2..2239cf81b 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1129,7 +1129,7 @@ void u_setfaction(unit * u, faction * f) if (u->faction == f) return; if (u->faction) { - --u->faction->no_units; + --u->faction->num_units; set_number(u, 0); join_group(u, NULL); free_orders(&u->orders); @@ -1166,7 +1166,7 @@ void u_setfaction(unit * u, faction * f) set_number(u, cnt); } if (f) { - ++f->no_units; + ++f->num_units; } } @@ -1581,7 +1581,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace, int maxheroes(const struct faction *f) { - int nsize = count_all(f); + int nsize = f->num_people; if (nsize == 0) return 0; else { diff --git a/src/laws.c b/src/laws.c index d66cf496b..6722194a9 100644 --- a/src/laws.c +++ b/src/laws.c @@ -1280,7 +1280,7 @@ static void remove_idle_players(void) else if (turn != f->lastorders) { char info[256]; sprintf(info, "%d Einheiten, %d Personen", - f->no_units, f->num_total); + f->num_units, f->num_people); } fp = &f->next; } @@ -2519,7 +2519,7 @@ int promotion_cmd(unit * u, struct order *ord) u_race(u))); return 0; } - people = count_all(u->faction) * u->number; + people = u->faction->num_people * u->number; money = get_pooled(u, rsilver, GET_ALL, people); if (people > money) { @@ -3020,8 +3020,7 @@ static int maxunits(const faction * f) int checkunitnumber(const faction * f, int add) { int alimit, flimit; - int flags = COUNT_DEFAULT | COUNT_MIGRANTS | COUNT_UNITS; - int fno = count_faction(f, flags) + add; + int fno = f->num_units + add; flimit = rule_faction_limit(); if (flimit && fno > flimit) { return 2; @@ -3039,7 +3038,7 @@ int checkunitnumber(const faction * f, int add) for (f2 = factions; f2; f2 = f2->next) { if (f != f2 && f->alliance == f2->alliance) { - unitsinalliance += count_faction(f2, flags); + unitsinalliance += f2->num_units; if (unitsinalliance > alimit) { return 1; } diff --git a/src/modules/score.c b/src/modules/score.c index 0776d6507..efb3465d4 100644 --- a/src/modules/score.c +++ b/src/modules/score.c @@ -160,7 +160,7 @@ void score(void) faction *f; fwrite(utf8_bom, 1, 3, scoreFP); for (f = factions; f; f = f->next) - if (!fval(f, FFL_NPC) && f->num_total != 0) { + if (!fval(f, FFL_NPC) && f->num_people != 0) { char score[32]; write_score(score, sizeof(score), f->score); fprintf(scoreFP, "%s ", score); @@ -195,7 +195,7 @@ void score(void) if (f->alliance && f->alliance == a) { alliance_factions++; alliance_score += f->score; - alliance_number += f->num_total; + alliance_number += f->num_people; if (token != NULL) { unit *u = f->units; while (u != NULL) { diff --git a/src/report.c b/src/report.c index 2f8ba7bce..dbcf54ff1 100644 --- a/src/report.c +++ b/src/report.c @@ -2106,14 +2106,8 @@ report_plaintext(const char *filename, report_context * ctx, RENDER(f, buf, sizeof(buf), ("nr_score", "score average", score, avg)); centre(out, buf, true); } - 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_units = f->num_units; + no_people = f->num_people; m = msg_message("nr_population", "population units limit", no_people, no_units, rule_faction_limit()); nr_render(m, f->locale, buf, sizeof(buf), f); msg_release(m); diff --git a/src/summary.c b/src/summary.c index 86273e67d..71bb30efc 100644 --- a/src/summary.c +++ b/src/summary.c @@ -132,12 +132,12 @@ static void out_faction(FILE * file, const struct faction *f) fprintf(file, "%s (%s/%d) (%.3s/%.3s), %d Einh., %d Pers., %d NMR\n", f->name, itoa36(f->no), f_get_alliance(f) ? f->alliance->id : 0, LOC(default_locale, rc_name_s(f->race, NAME_SINGULAR)), magic_school[f->magiegebiet], - count_units(f), f->num_total, turn - f->lastorders); + f->num_units, f->num_people, turn - f->lastorders); } else { fprintf(file, "%s (%.3s/%.3s), %d Einh., %d Pers., %d NMR\n", factionname(f), LOC(default_locale, rc_name_s(f->race, NAME_SINGULAR)), - magic_school[f->magiegebiet], count_units(f), f->num_total, + magic_school[f->magiegebiet], f->num_units, f->num_people, turn - f->lastorders); } } @@ -394,7 +394,7 @@ summary *make_summary(void) } ++plang->number; f->nregions = 0; - f->num_total = 0; + f->num_people = 0; if (f->units) { s->factions++; /* Problem mit Monsterpartei ... */ @@ -476,7 +476,7 @@ summary *make_summary(void) } } - f->num_total += u->number; + f->num_people += u->number; orace = (int)old_race(u_race(u)); if (orace >= 0) { s->poprace[orace] += u->number;