Merge pull request #676 from ennorehling/develop

BUG 2313: count of units and people is wrong
This commit is contained in:
Enno Rehling 2017-03-12 17:36:02 +01:00 committed by GitHub
commit 121b54f58b
9 changed files with 100 additions and 30 deletions

16
CHANGELOG.md Normal file
View File

@ -0,0 +1,16 @@
# Geplante Aenderungen in zukuenftigen Eressea-Versionen
Als Anhaltspunkt fuer die Empfaenger der woechentlichen Testauswertungnen
will ich versuchen, die Liste meiner Aenderungen seit dem letzten Release
zu dokumentieren.
## Version 3.12.0
- [other] optimierte Berechnung der Sichtbarkeit von Leuchttuermen
- [bug] Einheitenlimit bei GIB PERSON beachten
- [bug] Einheitenlimit bei ALLIANCE JOIN beachten
- [rule] Einheiten- und Personenzahl im Report beinhaltet *alle* Einheiten der Partei.
- [other] Statistik fuer Spielleiter zeigt das Parteisilber nicht mehr an.
- [other] Berechnung der Message-Ids optimiert.

View File

@ -316,8 +316,12 @@ static void test_recruit(CuTest *tc) {
f = test_create_faction(0); f = test_create_faction(0);
u = test_create_unit(f, test_create_region(0, 0, 0)); u = test_create_unit(f, test_create_region(0, 0, 0));
CuAssertIntEquals(tc, 1, u->number); CuAssertIntEquals(tc, 1, u->number);
CuAssertIntEquals(tc, 1, f->num_people);
CuAssertIntEquals(tc, 1, f->num_units);
add_recruits(u, 1, 1); add_recruits(u, 1, 1);
CuAssertIntEquals(tc, 2, u->number); CuAssertIntEquals(tc, 2, u->number);
CuAssertIntEquals(tc, 2, f->num_people);
CuAssertIntEquals(tc, 1, f->num_units);
CuAssertPtrEquals(tc, u, f->units); CuAssertPtrEquals(tc, u, f->units);
CuAssertPtrEquals(tc, NULL, u->nextF); CuAssertPtrEquals(tc, NULL, u->nextF);
CuAssertPtrEquals(tc, NULL, u->prevF); CuAssertPtrEquals(tc, NULL, u->prevF);

View File

@ -81,7 +81,6 @@ extern "C" {
int options; int options;
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;
score_t score; score_t score;
struct alliance *alliance; struct alliance *alliance;
int alliance_joindate; /* the turn on which the faction joined its current alliance (or left the last one) */ int alliance_joindate; /* the turn on which the faction joined its current alliance (or left the last one) */

View File

@ -438,9 +438,14 @@ int remove_unit(unit ** ulist, unit * u)
*ulist = u->next; *ulist = u->next;
} }
if (u->faction && u->faction->units == u) { if (u->faction) {
if (count_unit(u)) {
--u->faction->num_units;
}
if (u->faction->units == u) {
u->faction->units = u->nextF; u->faction->units = u->nextF;
} }
}
if (u->prevF) { if (u->prevF) {
u->prevF->nextF = u->nextF; u->prevF->nextF = u->nextF;
} }
@ -1125,12 +1130,13 @@ 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;
if (u->faction == f) if (u->faction == f)
return; return;
if (u->faction) { if (u->faction) {
if (count_unit(u)) {
--u->faction->num_units; --u->faction->num_units;
set_number(u, 0); u->faction->num_people -= u->number;
}
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);
@ -1162,14 +1168,19 @@ void u_setfaction(unit * u, faction * f)
if (u->region) { if (u->region) {
update_interval(f, u->region); update_interval(f, u->region);
} }
if (cnt) { if (f && count_unit(u)) {
set_number(u, cnt);
}
if (f) {
++f->num_units; ++f->num_units;
f->num_people += u->number;
} }
} }
bool count_unit(const unit *u)
{
const race *rc = u_race(u);
/* spells are invisible. units we cannot see do not count to our limit */
return rc == NULL || (rc->flags & RCF_INVISIBLE) == 0;
}
void set_number(unit * u, int count) void set_number(unit * u, int count)
{ {
assert(count >= 0); assert(count >= 0);
@ -1178,10 +1189,10 @@ void set_number(unit * u, int count)
if (count == 0) { if (count == 0) {
u->flags &= ~(UFL_HERO); u->flags &= ~(UFL_HERO);
} }
if (u->faction) { if (u->faction && count_unit(u)) {
u->faction->num_people += count - u->number; u->faction->num_people += count - u->number;
} }
u->number = (unsigned short)count; u->number = count;
} }
void remove_skill(unit * u, skill_t sk) void remove_skill(unit * u, skill_t sk)
@ -1484,6 +1495,8 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
unit *u = (unit *)calloc(1, sizeof(unit)); unit *u = (unit *)calloc(1, sizeof(unit));
assert(urace); assert(urace);
u_setrace(u, urace);
u->irace = NULL;
if (f) { if (f) {
assert(faction_alive(f)); assert(faction_alive(f));
u_setfaction(u, f); u_setfaction(u, f);
@ -1496,8 +1509,6 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
} }
} }
} }
u_setrace(u, urace);
u->irace = NULL;
set_number(u, number); set_number(u, number);
@ -1803,8 +1814,24 @@ const struct race *u_race(const struct unit *u)
void u_setrace(struct unit *u, const struct race *rc) void u_setrace(struct unit *u, const struct race *rc)
{ {
assert(rc); assert(rc);
if (!u->faction) {
u->_race = rc; u->_race = rc;
} }
else {
int n = 0;
if (count_unit(u)) {
--n;
}
u->_race = rc;
if (count_unit(u)) {
++n;
}
if (n != 0) {
u->faction->num_units += n;
u->faction->num_people += n * u->number;
}
}
}
void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level) void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level)
{ {

View File

@ -130,6 +130,7 @@ extern "C" {
int weight(const struct unit *u); int weight(const struct unit *u);
void renumber_unit(struct unit *u, int no); void renumber_unit(struct unit *u, int no);
bool count_unit(const unit *u); /* unit counts towards faction.num_units and faction.num_people */
const struct race *u_irace(const struct unit *u); const struct race *u_irace(const struct unit *u);
const struct race *u_race(const struct unit *u); const struct race *u_race(const struct unit *u);

View File

@ -519,6 +519,43 @@ static void test_heal_factor(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_unlimited_units(CuTest *tc) {
race *rc;
faction *f;
unit *u;
test_setup();
f = test_create_faction(NULL);
rc = test_create_race("spell");
rc->flags |= RCF_INVISIBLE;
CuAssertIntEquals(tc, 0, f->num_units);
CuAssertIntEquals(tc, 0, f->num_people);
u = test_create_unit(f, test_create_region(0, 0, NULL));
CuAssertTrue(tc, count_unit(u));
CuAssertIntEquals(tc, 1, f->num_units);
CuAssertIntEquals(tc, 1, f->num_people);
u_setfaction(u, NULL);
CuAssertIntEquals(tc, 0, f->num_units);
CuAssertIntEquals(tc, 0, f->num_people);
u_setfaction(u, f);
CuAssertIntEquals(tc, 1, f->num_units);
CuAssertIntEquals(tc, 1, f->num_people);
u_setrace(u, rc);
CuAssertTrue(tc, !count_unit(u));
CuAssertIntEquals(tc, 0, f->num_units);
CuAssertIntEquals(tc, 0, f->num_people);
scale_number(u, 10);
CuAssertIntEquals(tc, 0, f->num_units);
CuAssertIntEquals(tc, 0, f->num_people);
u_setrace(u, f->race);
CuAssertIntEquals(tc, 1, f->num_units);
CuAssertIntEquals(tc, 10, f->num_people);
remove_unit(&u->region->units, u);
CuAssertIntEquals(tc, 0, f->num_units);
CuAssertIntEquals(tc, 0, f->num_people);
test_cleanup();
}
CuSuite *get_unit_suite(void) CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -534,6 +571,7 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction); SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction);
SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); SUITE_ADD_TEST(suite, test_remove_empty_units_in_region);
SUITE_ADD_TEST(suite, test_names); SUITE_ADD_TEST(suite, test_names);
SUITE_ADD_TEST(suite, test_unlimited_units);
SUITE_ADD_TEST(suite, test_default_name); SUITE_ADD_TEST(suite, test_default_name);
SUITE_ADD_TEST(suite, test_skillmod); SUITE_ADD_TEST(suite, test_skillmod);
SUITE_ADD_TEST(suite, test_skill_hunger); SUITE_ADD_TEST(suite, test_skill_hunger);

View File

@ -1242,7 +1242,6 @@ static void prepare_lighthouse_ql(faction *f, selist *rlist) {
add_seen_nb(f, rl, seen_lighthouse); add_seen_nb(f, rl, seen_lighthouse);
} }
} }
selist_free(rlist);
} }
static void prepare_lighthouse(faction *f, region *r, int range) static void prepare_lighthouse(faction *f, region *r, int range)

View File

@ -199,8 +199,6 @@ static bool can_set_factionstealth(const unit * u, const faction * f)
} }
ru = ru->next; ru = ru->next;
} }
if (ru != NULL)
break;
} }
mu = mu->nextF; mu = mu->nextF;
} }

View File

@ -393,8 +393,6 @@ summary *make_summary(void)
plang->locale = lang; plang->locale = lang;
} }
++plang->number; ++plang->number;
f->nregions = 0;
f->num_people = 0;
if (f->units) { if (f->units) {
s->factions++; s->factions++;
/* Problem mit Monsterpartei ... */ /* Problem mit Monsterpartei ... */
@ -433,11 +431,6 @@ summary *make_summary(void)
s->peasants += rpeasants(r); s->peasants += rpeasants(r);
s->peasantmoney += rmoney(r); s->peasantmoney += rmoney(r);
/* Einheiten Info. nregions darf nur einmal pro Partei
* incrementiert werden. */
for (u = r->units; u; u = u->next)
freset(u->faction, FFL_SELECT);
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
int orace; int orace;
f = u->faction; f = u->faction;
@ -470,13 +463,8 @@ summary *make_summary(void)
if (aktskill > s->maxskill) if (aktskill > s->maxskill)
s->maxskill = aktskill; s->maxskill = aktskill;
} }
if (!fval(f, FFL_SELECT)) {
f->nregions++;
fset(f, FFL_SELECT);
}
} }
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;