diff --git a/src/lighthouse.c b/src/lighthouse.c index 58d522807..54591cd16 100644 --- a/src/lighthouse.c +++ b/src/lighthouse.c @@ -71,15 +71,18 @@ int lighthouse_range(const building * b, const faction * f) if (skill_enabled(SK_PERCEPTION)) { region *r = b->region; int c = 0; + int cap = buildingcapacity(b); unit *u; + for (u = r->units; u; u = u->next) { if (u->building == b || u == building_owner(b)) { if (u->building == b) { c += u->number; } - if (c > buildingcapacity(b)) + if (c > cap) { break; - if (f == NULL || u->faction == f) { + } + else if (f == NULL || u->faction == f) { int sk = effskill(u, SK_PERCEPTION, 0) / 3; d = _max(d, sk); d = _min(maxd, d); diff --git a/src/lighthouse.test.c b/src/lighthouse.test.c index 193de58a6..391146d61 100644 --- a/src/lighthouse.test.c +++ b/src/lighthouse.test.c @@ -16,33 +16,38 @@ static void test_lighthouse_range(CuTest * tc) { - faction *f; - unit *u; + unit *u1, *u2; region *r1, *r2; building *b; test_setup(); r1 = test_create_region(0, 0, 0); r2 = test_create_region(1, 0, 0); - f = test_create_faction(0); - u = test_create_unit(f, r1); + u1 = test_create_unit(test_create_faction(0), r1); + u2 = test_create_unit(test_create_faction(0), r1); b = test_create_building(r1, test_create_buildingtype("lighthouse")); 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; CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL)); - u->building = b; - set_level(u, SK_PERCEPTION, 3); + u1->building = b; + 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)); b->flags |= BLD_MAINTAINED; CuAssertIntEquals(tc, 1, lighthouse_range(b, NULL)); - set_level(u, SK_PERCEPTION, 6); - CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL)); + set_level(u1, SK_PERCEPTION, 6); + CuAssertIntEquals(tc, 2, lighthouse_range(b, u1->faction)); + CuAssertIntEquals(tc, 0, lighthouse_range(b, u2->faction)); b->size = 100; + update_lighthouse(b); 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, 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))); test_cleanup(); } diff --git a/src/reports.c b/src/reports.c index 50e220393..915aa8a1b 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1350,8 +1350,7 @@ void prepare_report(report_context *ctx, faction *f) faction_add_seen(f, r, seen_unit); } if (fval(r, RF_LIGHTHOUSE)) { - // TODO: is the building big enough for the unit? - if (u->building && u->building->type == bt_lighthouse) { + if (u->building && u->building->type == bt_lighthouse && inside_building(u)) { /* we are in a lighthouse. add the regions we can see from here! */ prepare_lighthouse(u->building, ctx); } diff --git a/src/reports.test.c b/src/reports.test.c index 503f8a1be..1e3f800e4 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -264,24 +265,29 @@ static void test_prepare_lighthouse(CuTest *tc) { unit *u; building *b; building_type *btype; + const struct terrain_type *t_ocean, *t_plain; test_setup(); + t_ocean = test_create_terrain("ocean", SEA_REGION); + t_plain = test_create_terrain("plain", LAND_REGION); f = test_create_faction(0); - r1 = test_create_region(0, 0, 0); - r2 = test_create_region(1, 0, 0); - r3 = test_create_region(2, 0, 0); + r1 = test_create_region(0, 0, t_plain); + r2 = test_create_region(1, 0, t_ocean); + r3 = test_create_region(2, 0, t_ocean); btype = test_create_buildingtype("lighthouse"); b = test_create_building(r1, btype); + b->flags |= BLD_MAINTAINED; b->size = 10; + update_lighthouse(b); u = test_create_unit(f, r1); u->building = b; - update_lighthouse(b); + set_level(u, SK_PERCEPTION, 3); prepare_report(&ctx, f); 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_lighthouse, r2->seen.mode); - CuAssertIntEquals(tc, seen_none, r3->seen.mode); + CuAssertIntEquals(tc, seen_neighbour, r3->seen.mode); test_cleanup(); } @@ -314,7 +320,7 @@ static void test_prepare_report(CuTest *tc) { prepare_report(&ctx, f); CuAssertPtrEquals(tc, regions, ctx.first); CuAssertPtrEquals(tc, r, ctx.last); - CuAssertIntEquals(tc, seen_neighbour, r->seen.mode); + CuAssertIntEquals(tc, seen_none, r->seen.mode); test_cleanup(); }