BUG 2310: counting units and people.

reduce number of count_all loops made,
make f->num_units and f->num_people be correct.
This commit is contained in:
Enno Rehling 2017-03-11 19:36:26 +01:00
parent 0f2744fcf3
commit d633c2a9fb
9 changed files with 20 additions and 45 deletions

View File

@ -1558,7 +1558,7 @@ report_computer(const char *filename, report_context * ctx, const char *bom)
translate(prefix, LOC(f->locale, prefix))); translate(prefix, LOC(f->locale, prefix)));
} }
fprintf(F, "%d;Rekrutierungskosten\n", f->race->recruitcost); 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]); fprintf(F, "\"%s\";Magiegebiet\n", magic_school[f->magiegebiet]);
if (rc_changed(&rc_cache)) { if (rc_changed(&rc_cache)) {

View File

@ -712,7 +712,7 @@ int count_faction(const faction * f, int flags)
int n = 0; int n = 0;
for (u = f->units; u; u = u->nextF) { for (u = f->units; u; u = u->nextF) {
const race *rc = u_race(u); const race *rc = u_race(u);
int x = (flags&COUNT_UNITS) ? 1 : u->number; int x = u->number;
if (f->race != rc) { if (f->race != rc) {
if (!playerrace(rc)) { if (!playerrace(rc)) {
if (flags&COUNT_MONSTERS) { if (flags&COUNT_MONSTERS) {
@ -732,16 +732,6 @@ int count_faction(const faction * f, int flags)
return n; 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) int count_migrants(const faction * f)
{ {
return count_faction(f, COUNT_MIGRANTS); return count_faction(f, COUNT_MIGRANTS);
@ -752,7 +742,7 @@ int count_maxmigrants(const faction * f)
int formula = rc_migrants_formula(f->race); int formula = rc_migrants_formula(f->race);
if (formula == MIGRANTS_LOG10) { if (formula == MIGRANTS_LOG10) {
int nsize = count_all(f); int nsize = f->num_people;
if (nsize > 0) { if (nsize > 0) {
int x = (int)(log10(nsize / 50.0) * 20); int x = (int)(log10(nsize / 50.0) * 20);
if (x < 0) x = 0; if (x < 0) x = 0;

View File

@ -77,9 +77,8 @@ extern "C" {
magic_t magiegebiet; magic_t magiegebiet;
int newbies; int newbies;
int num_people; /* Anzahl Personen ohne Monster */ int num_people; /* Anzahl Personen ohne Monster */
int num_total; /* Anzahl Personen mit Monstern */ int num_units;
int options; int options;
int no_units;
struct ally *allies; /* alliedgroup and others should check sf.faction.alive before using a faction from f.allies */ 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 */ struct group *groups; /* alliedgroup and others should check sf.faction.alive before using a faction from f.groups */
int nregions; int nregions;
@ -165,14 +164,10 @@ extern "C" {
#define COUNT_MONSTERS 0x01 #define COUNT_MONSTERS 0x01
#define COUNT_MIGRANTS 0x02 #define COUNT_MIGRANTS 0x02
#define COUNT_DEFAULT 0x04 #define COUNT_DEFAULT 0x04
#define COUNT_ALL 0x07
#define COUNT_UNITS 0x10
int count_faction(const struct faction * f, int flags); int count_faction(const struct faction * f, int flags);
int count_migrants(const struct faction * f); int count_migrants(const struct faction * f);
int count_maxmigrants(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); int max_magicians(const struct faction * f);
struct faction *getfaction(void); struct faction *getfaction(void);

View File

@ -657,10 +657,7 @@ unit *read_unit(struct gamedata *data)
if (f != u->faction) { if (f != u->faction) {
u_setfaction(u, f); u_setfaction(u, f);
} }
if (u->faction) { if (!u->faction) {
++u->faction->no_units;
}
else {
log_error("unit %s has faction == NULL", itoa36(u->no)); log_error("unit %s has faction == NULL", itoa36(u->no));
return 0; return 0;
} }

View File

@ -1129,7 +1129,7 @@ void u_setfaction(unit * u, faction * f)
if (u->faction == f) if (u->faction == f)
return; return;
if (u->faction) { if (u->faction) {
--u->faction->no_units; --u->faction->num_units;
set_number(u, 0); set_number(u, 0);
join_group(u, NULL); join_group(u, NULL);
free_orders(&u->orders); free_orders(&u->orders);
@ -1166,7 +1166,7 @@ void u_setfaction(unit * u, faction * f)
set_number(u, cnt); set_number(u, cnt);
} }
if (f) { 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 maxheroes(const struct faction *f)
{ {
int nsize = count_all(f); int nsize = f->num_people;
if (nsize == 0) if (nsize == 0)
return 0; return 0;
else { else {

View File

@ -1280,7 +1280,7 @@ static void remove_idle_players(void)
else if (turn != f->lastorders) { else if (turn != f->lastorders) {
char info[256]; char info[256];
sprintf(info, "%d Einheiten, %d Personen", sprintf(info, "%d Einheiten, %d Personen",
f->no_units, f->num_total); f->num_units, f->num_people);
} }
fp = &f->next; fp = &f->next;
} }
@ -2519,7 +2519,7 @@ int promotion_cmd(unit * u, struct order *ord)
u_race(u))); u_race(u)));
return 0; return 0;
} }
people = count_all(u->faction) * u->number; people = u->faction->num_people * u->number;
money = get_pooled(u, rsilver, GET_ALL, people); money = get_pooled(u, rsilver, GET_ALL, people);
if (people > money) { if (people > money) {
@ -3020,8 +3020,7 @@ static int maxunits(const faction * f)
int checkunitnumber(const faction * f, int add) int checkunitnumber(const faction * f, int add)
{ {
int alimit, flimit; int alimit, flimit;
int flags = COUNT_DEFAULT | COUNT_MIGRANTS | COUNT_UNITS; int fno = f->num_units + add;
int fno = count_faction(f, flags) + add;
flimit = rule_faction_limit(); flimit = rule_faction_limit();
if (flimit && fno > flimit) { if (flimit && fno > flimit) {
return 2; return 2;
@ -3039,7 +3038,7 @@ int checkunitnumber(const faction * f, int add)
for (f2 = factions; f2; f2 = f2->next) { for (f2 = factions; f2; f2 = f2->next) {
if (f != f2 && f->alliance == f2->alliance) { if (f != f2 && f->alliance == f2->alliance) {
unitsinalliance += count_faction(f2, flags); unitsinalliance += f2->num_units;
if (unitsinalliance > alimit) { if (unitsinalliance > alimit) {
return 1; return 1;
} }

View File

@ -160,7 +160,7 @@ void score(void)
faction *f; faction *f;
fwrite(utf8_bom, 1, 3, scoreFP); fwrite(utf8_bom, 1, 3, scoreFP);
for (f = factions; f; f = f->next) 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]; char score[32];
write_score(score, sizeof(score), f->score); write_score(score, sizeof(score), f->score);
fprintf(scoreFP, "%s ", score); fprintf(scoreFP, "%s ", score);
@ -195,7 +195,7 @@ void score(void)
if (f->alliance && f->alliance == a) { if (f->alliance && f->alliance == a) {
alliance_factions++; alliance_factions++;
alliance_score += f->score; alliance_score += f->score;
alliance_number += f->num_total; alliance_number += f->num_people;
if (token != NULL) { if (token != NULL) {
unit *u = f->units; unit *u = f->units;
while (u != NULL) { while (u != NULL) {

View File

@ -2106,14 +2106,8 @@ report_plaintext(const char *filename, report_context * ctx,
RENDER(f, buf, sizeof(buf), ("nr_score", "score average", score, avg)); RENDER(f, buf, sizeof(buf), ("nr_score", "score average", score, avg));
centre(out, buf, true); centre(out, buf, true);
} }
no_units = count_units(f); no_units = f->num_units;
no_people = count_all(f); no_people = f->num_people;
if (f->flags & FFL_NPC) {
no_people = f->num_total;
}
else {
no_people = f->num_people;
}
m = msg_message("nr_population", "population units limit", no_people, no_units, rule_faction_limit()); m = msg_message("nr_population", "population units limit", no_people, no_units, rule_faction_limit());
nr_render(m, f->locale, buf, sizeof(buf), f); nr_render(m, f->locale, buf, sizeof(buf), f);
msg_release(m); msg_release(m);

View File

@ -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", 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, 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], 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 { else {
fprintf(file, "%s (%.3s/%.3s), %d Einh., %d Pers., %d NMR\n", fprintf(file, "%s (%.3s/%.3s), %d Einh., %d Pers., %d NMR\n",
factionname(f), LOC(default_locale, rc_name_s(f->race, NAME_SINGULAR)), 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); turn - f->lastorders);
} }
} }
@ -394,7 +394,7 @@ summary *make_summary(void)
} }
++plang->number; ++plang->number;
f->nregions = 0; f->nregions = 0;
f->num_total = 0; f->num_people = 0;
if (f->units) { if (f->units) {
s->factions++; s->factions++;
/* Problem mit Monsterpartei ... */ /* 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)); orace = (int)old_race(u_race(u));
if (orace >= 0) { if (orace >= 0) {
s->poprace[orace] += u->number; s->poprace[orace] += u->number;