From 43a0368fb4ead71e8402115bf0575e00aa58778a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 24 Sep 2020 20:12:51 +0200 Subject: [PATCH] lighthouses protect from drifting do not check for maintenance in update_lighthouse, but when the lighthouse is used. --- src/lighthouse.c | 29 ++++++++++++++++++++--------- src/lighthouse.test.c | 5 +++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/lighthouse.c b/src/lighthouse.c index 7915bdf44..257836d20 100644 --- a/src/lighthouse.c +++ b/src/lighthouse.c @@ -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,13 +117,19 @@ 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); - return true; + 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; + } + } + } } } diff --git a/src/lighthouse.test.c b/src/lighthouse.test.c index 9dc6d2999..8230b97f2 100644 --- a/src/lighthouse.test.c +++ b/src/lighthouse.test.c @@ -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();