separate lighthouse_view_distance from lighthouse_range.

This commit is contained in:
Enno Rehling 2018-07-18 20:49:17 +02:00
parent 7b7d3cb759
commit 2282160916
5 changed files with 39 additions and 30 deletions

View File

@ -36,7 +36,7 @@ void update_lighthouse(building * lh)
r->flags |= RF_LIGHTHOUSE; r->flags |= RF_LIGHTHOUSE;
if (lh->size >= 10) { if (lh->size >= 10) {
int d = lighthouse_range(lh, NULL, NULL); int d = lighthouse_range(lh);
int x; int x;
for (x = -d; x <= d; ++x) { for (x = -d; x <= d; ++x) {
int y; int y;
@ -79,18 +79,24 @@ void remove_lighthouse(const building *lh) {
} }
} }
int lighthouse_range(const building * b, const faction * f, const unit *u) int lighthouse_range(const building * b)
{ {
if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) { if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) {
int maxd = (int)log10(b->size) + 1; return (int)log10(b->size) + 1;
}
return 0;
}
if (u && skill_enabled(SK_PERCEPTION)) { int lighthouse_view_distance(const building * b, const unit *u)
{
if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) {
int maxd = lighthouse_range(b);
if (maxd > 0 && u && skill_enabled(SK_PERCEPTION)) {
int sk = effskill(u, SK_PERCEPTION, 0) / 3; int sk = effskill(u, SK_PERCEPTION, 0) / 3;
assert(u->building == b); assert(u->building == b);
assert(u->faction == f);
if (maxd > sk) maxd = sk; if (maxd > sk) maxd = sk;
} }
/* E3A rule: no perception req'd */
return maxd; return maxd;
} }
return 0; return 0;

View File

@ -39,8 +39,8 @@ extern "C" {
bool lighthouse_guarded(const struct region *r); bool lighthouse_guarded(const struct region *r);
void update_lighthouse(struct building *b); void update_lighthouse(struct building *b);
void remove_lighthouse(const struct building *lh); void remove_lighthouse(const struct building *lh);
int lighthouse_range(const struct building *b, const struct faction *f, int lighthouse_range(const struct building *b);
const struct unit *u); int lighthouse_view_distance(const struct building *b, const struct unit *u);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -26,30 +26,33 @@ static void test_lighthouse_range(CuTest * tc)
u1 = test_create_unit(test_create_faction(NULL), r); u1 = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(test_create_faction(NULL), r); u2 = test_create_unit(test_create_faction(NULL), r);
b = test_create_building(r, test_create_buildingtype("lighthouse")); b = test_create_building(r, test_create_buildingtype("lighthouse"));
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL, NULL)); CuAssertIntEquals(tc, 0, lighthouse_range(b));
CuAssertIntEquals(tc, 0, lighthouse_range(b, u1->faction, NULL)); b->size = 9;
CuAssertIntEquals(tc, 0, lighthouse_range(b));
b->size = 10; b->size = 10;
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL, NULL)); CuAssertIntEquals(tc, 0, lighthouse_range(b));
b->flags |= BLD_MAINTAINED;
CuAssertIntEquals(tc, 2, lighthouse_range(b));
u1->building = b; u1->building = b;
u2->building = b; u2->building = b;
u1->number = 10; u1->number = 10;
set_level(u1, SK_PERCEPTION, 3); set_level(u1, SK_PERCEPTION, 3);
set_level(u2, SK_PERCEPTION, 3); set_level(u2, SK_PERCEPTION, 3);
CuAssertIntEquals(tc, 0, lighthouse_range(b, NULL, NULL));
b->flags |= BLD_MAINTAINED; CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u1));
CuAssertIntEquals(tc, 1, lighthouse_range(b, u1->faction, u1));
set_level(u1, SK_PERCEPTION, 6); set_level(u1, SK_PERCEPTION, 6);
CuAssertIntEquals(tc, 2, lighthouse_range(b, u1->faction, u1)); CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u2));
/* lighthouse_range does not check inside_building */ CuAssertIntEquals(tc, 2, lighthouse_view_distance(b, u1));
CuAssertIntEquals(tc, 1, lighthouse_range(b, u2->faction, u2));
b->size = 100; b->size = 100;
update_lighthouse(b); update_lighthouse(b);
CuAssertIntEquals(tc, 3, lighthouse_range(b, NULL, NULL)); CuAssertIntEquals(tc, 3, lighthouse_range(b));
CuAssertIntEquals(tc, 2, lighthouse_range(b, u1->faction, u1)); CuAssertIntEquals(tc, 2, lighthouse_view_distance(b, u1));
set_level(u1, SK_PERCEPTION, 9); set_level(u1, SK_PERCEPTION, 9);
CuAssertIntEquals(tc, 3, lighthouse_range(b, u1->faction, u1)); CuAssertIntEquals(tc, 3, lighthouse_view_distance(b, u1));
CuAssertIntEquals(tc, 1, lighthouse_range(b, u2->faction, u2)); CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u2));
b->size = 99;
CuAssertIntEquals(tc, 2, lighthouse_view_distance(b, u1));
test_teardown(); test_teardown();
} }
@ -111,7 +114,7 @@ static void test_lighthouse_guard(CuTest * tc) {
b = test_create_building(r1, test_create_buildingtype("lighthouse")); b = test_create_building(r1, test_create_buildingtype("lighthouse"));
b->flags |= BLD_MAINTAINED; b->flags |= BLD_MAINTAINED;
b->size = 10; b->size = 10;
CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL, NULL)); CuAssertIntEquals(tc, 2, lighthouse_range(b));
update_lighthouse(b); update_lighthouse(b);
CuAssertIntEquals(tc, RF_LIGHTHOUSE, r1->flags&RF_LIGHTHOUSE); CuAssertIntEquals(tc, RF_LIGHTHOUSE, r1->flags&RF_LIGHTHOUSE);
CuAssertPtrEquals(tc, NULL, r1->attribs); CuAssertPtrEquals(tc, NULL, r1->attribs);

View File

@ -1596,7 +1596,7 @@ void prepare_report(report_context *ctx, faction *f)
for (b = rbuildings(r); b; b = b->next) { for (b = rbuildings(r); b; b = b->next) {
if (b && b->type == bt_lighthouse) { if (b && b->type == bt_lighthouse) {
/* region owners get maximum range */ /* region owners get maximum range */
int lhr = lighthouse_range(b, NULL, NULL); int lhr = lighthouse_view_distance(b, NULL);
if (lhr > range) range = lhr; if (lhr > range) range = lhr;
} }
} }
@ -1613,7 +1613,7 @@ void prepare_report(report_context *ctx, faction *f)
*/ */
if (!fval(r, RF_LIGHTHOUSE)) { if (!fval(r, RF_LIGHTHOUSE)) {
/* it's enough to add the region once, and if there are /* it's enough to add the region once, and if there are
* no lighthouses, there is no need to look at more units */ * no lighthouses here, there is no need to look at more units */
break; break;
} }
} }
@ -1633,14 +1633,14 @@ void prepare_report(report_context *ctx, faction *f)
/* unit is one of ours, and inside the current lighthouse */ /* unit is one of ours, and inside the current lighthouse */
if (br == 0) { if (br == 0) {
/* lazy-calculate the range */ /* lazy-calculate the range */
br = lighthouse_range(b, f, u); br = lighthouse_view_distance(b, u);
}
if (br > range) { if (br > range) {
range = br; range = br;
} }
} }
} }
} }
}
if (range > 0) { if (range > 0) {
/* we are in at least one lighthouse. add the regions we can see from here! */ /* we are in at least one lighthouse. add the regions we can see from here! */
prepare_lighthouse(f, r, range); prepare_lighthouse(f, r, range);

View File

@ -495,7 +495,7 @@ void test_prepare_lighthouse_capacity(CuTest *tc) {
u1->number = 4; u1->number = 4;
u1->building = b; u1->building = b;
set_level(u1, SK_PERCEPTION, 3); set_level(u1, SK_PERCEPTION, 3);
CuAssertIntEquals(tc, 1, lighthouse_range(b, u1->faction, u1)); CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u1));
CuAssertPtrEquals(tc, b, inside_building(u1)); CuAssertPtrEquals(tc, b, inside_building(u1));
u2 = test_create_unit(f, r1); u2 = test_create_unit(f, r1);
u2->building = b; u2->building = b;
@ -597,7 +597,7 @@ static void test_prepare_lighthouse_owners(CuTest *tc)
u = test_create_unit(test_create_faction(NULL), r1); u = test_create_unit(test_create_faction(NULL), r1);
u->building = b; u->building = b;
region_set_owner(b->region, f, 0); region_set_owner(b->region, f, 0);
CuAssertIntEquals(tc, 2, lighthouse_range(b, NULL, NULL)); CuAssertIntEquals(tc, 2, lighthouse_view_distance(b, NULL));
prepare_report(&ctx, f); prepare_report(&ctx, f);
CuAssertPtrEquals(tc, r1, ctx.first); CuAssertPtrEquals(tc, r1, ctx.first);
CuAssertPtrEquals(tc, NULL, ctx.last); CuAssertPtrEquals(tc, NULL, ctx.last);