lighthouses protect from drifting

do not check for maintenance in update_lighthouse, but when the lighthouse is used.
This commit is contained in:
Enno Rehling 2020-09-24 20:12:51 +02:00
parent 65c05f37d2
commit 43a0368fb4
2 changed files with 23 additions and 11 deletions

View File

@ -21,7 +21,12 @@ attrib_type at_lighthouse = {
bool is_lighthouse(const building_type *btype)
{
return is_building_type(btype, "lighthouse");
static int config;
static const building_type *bt_lighthouse;
if (bt_changed(&config)) {
bt_lighthouse = bt_find("lighthouse");
}
return btype == bt_lighthouse;
}
/* update_lighthouse: call this function whenever the size of a lighthouse changes
@ -81,7 +86,7 @@ void remove_lighthouse(const building *lh) {
int lighthouse_range(const building * b)
{
if (b->size >= 10 && (b->flags & BLD_MAINTAINED)) {
if (b->size >= 10) {
return (int)log10(b->size) + 1;
}
return 0;
@ -112,14 +117,20 @@ bool lighthouse_guarded(const region * r)
for (a = a_find(r->attribs, &at_lighthouse); a && a->type == &at_lighthouse;
a = a->next) {
building *b = (building *)a->data.v;
assert(is_building_type(b->type, "lighthouse"));
if ((b->flags & BLD_MAINTAINED) && b->size >= 10) {
int maxd = (int)log10(b->size) + 1;
int d = distance(r, b->region);
assert(maxd >= d);
if (b->flags & BLD_MAINTAINED) {
if (r == b->region) {
return true;
}
else {
int maxd = lighthouse_range(b);
if (maxd > 0) {
int d = distance(r, b->region);
if (maxd >= d) {
return true;
}
}
}
}
}
return false;

View File

@ -30,8 +30,6 @@ static void test_lighthouse_range(CuTest * tc)
b->size = 9;
CuAssertIntEquals(tc, 0, lighthouse_range(b));
b->size = 10;
CuAssertIntEquals(tc, 0, lighthouse_range(b));
b->flags |= BLD_MAINTAINED;
CuAssertIntEquals(tc, 2, lighthouse_range(b));
u1->building = b;
u2->building = b;
@ -39,6 +37,8 @@ static void test_lighthouse_range(CuTest * tc)
set_level(u1, SK_PERCEPTION, 3);
set_level(u2, SK_PERCEPTION, 3);
CuAssertIntEquals(tc, 0, lighthouse_view_distance(b, u1));
b->flags |= BLD_MAINTAINED;
CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u1));
set_level(u1, SK_PERCEPTION, 6);
CuAssertIntEquals(tc, 1, lighthouse_view_distance(b, u2));
@ -126,6 +126,7 @@ static void test_lighthouse_guard(CuTest * tc) {
CuAssertIntEquals(tc, true, lighthouse_guarded(r3));
CuAssertIntEquals(tc, false, lighthouse_guarded(r4));
b->size = 1; /* size can go down in destroy_cmd */
update_lighthouse(b);
CuAssertIntEquals(tc, false, lighthouse_guarded(r2));
CuAssertIntEquals(tc, false, lighthouse_guarded(r3));
test_teardown();