improve lighthouse tests.

you must be inside lighthouse to get a report from it.
This commit is contained in:
Enno Rehling 2016-09-17 13:20:02 +02:00
parent 951bf5718e
commit 7349ec0aa9
4 changed files with 35 additions and 22 deletions

View File

@ -71,15 +71,18 @@ int lighthouse_range(const building * b, const faction * f)
if (skill_enabled(SK_PERCEPTION)) { if (skill_enabled(SK_PERCEPTION)) {
region *r = b->region; region *r = b->region;
int c = 0; int c = 0;
int cap = buildingcapacity(b);
unit *u; unit *u;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
if (u->building == b || u == building_owner(b)) { if (u->building == b || u == building_owner(b)) {
if (u->building == b) { if (u->building == b) {
c += u->number; c += u->number;
} }
if (c > buildingcapacity(b)) if (c > cap) {
break; break;
if (f == NULL || u->faction == f) { }
else if (f == NULL || u->faction == f) {
int sk = effskill(u, SK_PERCEPTION, 0) / 3; int sk = effskill(u, SK_PERCEPTION, 0) / 3;
d = _max(d, sk); d = _max(d, sk);
d = _min(maxd, d); d = _min(maxd, d);

View File

@ -16,33 +16,38 @@
static void test_lighthouse_range(CuTest * tc) static void test_lighthouse_range(CuTest * tc)
{ {
faction *f; unit *u1, *u2;
unit *u;
region *r1, *r2; region *r1, *r2;
building *b; building *b;
test_setup(); test_setup();
r1 = test_create_region(0, 0, 0); r1 = test_create_region(0, 0, 0);
r2 = test_create_region(1, 0, 0); r2 = test_create_region(1, 0, 0);
f = test_create_faction(0); u1 = test_create_unit(test_create_faction(0), r1);
u = test_create_unit(f, r1); u2 = test_create_unit(test_create_faction(0), r1);
b = test_create_building(r1, test_create_buildingtype("lighthouse")); b = test_create_building(r1, test_create_buildingtype("lighthouse"));
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL));
CuAssertIntEquals(tc, 0, lighthouse_range(b, f)); CuAssertIntEquals(tc, 0, lighthouse_range(b, u1->faction));
b->size = 10; b->size = 10;
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL));
u->building = b; u1->building = b;
set_level(u, SK_PERCEPTION, 3); u2->building = b;
u1->number = 10;
set_level(u1, SK_PERCEPTION, 3);
set_level(u2, SK_PERCEPTION, 3);
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL));
b->flags |= BLD_MAINTAINED; b->flags |= BLD_MAINTAINED;
CuAssertIntEquals(tc, 1, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 1, lighthouse_range(b, NULL));
set_level(u, SK_PERCEPTION, 6); set_level(u1, SK_PERCEPTION, 6);
CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 2, lighthouse_range(b, u1->faction));
CuAssertIntEquals(tc, 0, lighthouse_range(b, u2->faction));
b->size = 100; b->size = 100;
update_lighthouse(b);
CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL));
set_level(u, SK_PERCEPTION, 9); set_level(u1, SK_PERCEPTION, 9);
CuAssertIntEquals(tc, 3, lighthouse_range(b, NULL)); CuAssertIntEquals(tc, 3, lighthouse_range(b, NULL));
CuAssertIntEquals(tc, 3, lighthouse_range(b, f)); CuAssertIntEquals(tc, 3, lighthouse_range(b, u1->faction));
CuAssertIntEquals(tc, 1, lighthouse_range(b, u2->faction));
CuAssertIntEquals(tc, 0, lighthouse_range(b, test_create_faction(0))); CuAssertIntEquals(tc, 0, lighthouse_range(b, test_create_faction(0)));
test_cleanup(); test_cleanup();
} }

View File

@ -1350,8 +1350,7 @@ void prepare_report(report_context *ctx, faction *f)
faction_add_seen(f, r, seen_unit); faction_add_seen(f, r, seen_unit);
} }
if (fval(r, RF_LIGHTHOUSE)) { if (fval(r, RF_LIGHTHOUSE)) {
// TODO: is the building big enough for the unit? if (u->building && u->building->type == bt_lighthouse && inside_building(u)) {
if (u->building && u->building->type == bt_lighthouse) {
/* we are in a lighthouse. add the regions we can see from here! */ /* we are in a lighthouse. add the regions we can see from here! */
prepare_lighthouse(u->building, ctx); prepare_lighthouse(u->building, ctx);
} }

View File

@ -12,6 +12,7 @@
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/ship.h> #include <kernel/ship.h>
#include <kernel/terrain.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/spellbook.h> #include <kernel/spellbook.h>
@ -264,24 +265,29 @@ static void test_prepare_lighthouse(CuTest *tc) {
unit *u; unit *u;
building *b; building *b;
building_type *btype; building_type *btype;
const struct terrain_type *t_ocean, *t_plain;
test_setup(); test_setup();
t_ocean = test_create_terrain("ocean", SEA_REGION);
t_plain = test_create_terrain("plain", LAND_REGION);
f = test_create_faction(0); f = test_create_faction(0);
r1 = test_create_region(0, 0, 0); r1 = test_create_region(0, 0, t_plain);
r2 = test_create_region(1, 0, 0); r2 = test_create_region(1, 0, t_ocean);
r3 = test_create_region(2, 0, 0); r3 = test_create_region(2, 0, t_ocean);
btype = test_create_buildingtype("lighthouse"); btype = test_create_buildingtype("lighthouse");
b = test_create_building(r1, btype); b = test_create_building(r1, btype);
b->flags |= BLD_MAINTAINED;
b->size = 10; b->size = 10;
update_lighthouse(b);
u = test_create_unit(f, r1); u = test_create_unit(f, r1);
u->building = b; u->building = b;
update_lighthouse(b); set_level(u, SK_PERCEPTION, 3);
prepare_report(&ctx, f); prepare_report(&ctx, f);
CuAssertPtrEquals(tc, r1, ctx.first); CuAssertPtrEquals(tc, r1, ctx.first);
CuAssertPtrEquals(tc, r3, ctx.last); CuAssertPtrEquals(tc, NULL, ctx.last);
CuAssertIntEquals(tc, seen_unit, r1->seen.mode); CuAssertIntEquals(tc, seen_unit, r1->seen.mode);
CuAssertIntEquals(tc, seen_lighthouse, r2->seen.mode); CuAssertIntEquals(tc, seen_lighthouse, r2->seen.mode);
CuAssertIntEquals(tc, seen_none, r3->seen.mode); CuAssertIntEquals(tc, seen_neighbour, r3->seen.mode);
test_cleanup(); test_cleanup();
} }
@ -314,7 +320,7 @@ static void test_prepare_report(CuTest *tc) {
prepare_report(&ctx, f); prepare_report(&ctx, f);
CuAssertPtrEquals(tc, regions, ctx.first); CuAssertPtrEquals(tc, regions, ctx.first);
CuAssertPtrEquals(tc, r, ctx.last); CuAssertPtrEquals(tc, r, ctx.last);
CuAssertIntEquals(tc, seen_neighbour, r->seen.mode); CuAssertIntEquals(tc, seen_none, r->seen.mode);
test_cleanup(); test_cleanup();
} }