From 65c39866e1f71474fa787abb215cde48b859392f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 9 Sep 2015 11:08:33 +0200 Subject: [PATCH] refactor: add a faction_add_seen function that also updates [first, last) interval. add tests for add_seen. disable test_faction_add_seen, looks like it needs a valgrind check? --- src/kernel/faction.c | 5 +++++ src/reports.c | 26 ++++++++----------------- src/seen.c | 21 ++++++++++++++------ src/seen.h | 9 +++++---- src/seen.test.c | 46 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 78 insertions(+), 29 deletions(-) diff --git a/src/kernel/faction.c b/src/kernel/faction.c index e57dd64ed..8d5fabb82 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -311,6 +311,11 @@ void destroyfaction(faction * f) f->spellbook = 0; } + if (f->seen_factions) { + ql_free(f->seen_factions); + f->seen_factions = 0; + } + while (u) { /* give away your stuff, make zombies if you cannot (quest items) */ int result = gift_items(u, GIFT_FRIENDS | GIFT_PEASANTS); diff --git a/src/reports.c b/src/reports.c index a987958b0..2168268bb 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1258,10 +1258,10 @@ static void prepare_lighthouse(building * b, faction * f) int d; get_neighbours(rl, next); - add_seen(f->seen, rl, see_lighthouse, false); + faction_add_seen(f, rl, see_lighthouse); for (d = 0; d != MAXDIRECTIONS; ++d) { if (next[d]) { - add_seen(f->seen, next[d], see_neighbour, false); + faction_add_seen(f, next[d], see_neighbour); } } } @@ -1370,10 +1370,7 @@ void reorder_units(region * r) static void cb_add_seen(region *r, unit *u, void *cbdata) { unused_arg(cbdata); if (u->faction) { - add_seen(u->faction->seen, r, see_travel, false); -#ifdef SMART_INTERVALS - update_interval(u->faction, r); -#endif + faction_add_seen(u->faction, r, see_travel); } } @@ -1398,10 +1395,7 @@ static void prepare_reports(void) if (p) { watcher *w = p->watchers; for (; w; w = w->next) { - add_seen(w->faction->seen, r, w->mode, false); -#ifdef SMART_INTERVALS - update_interval(w->faction, r); -#endif + faction_add_seen(w->faction, r, w->mode); } } @@ -1413,11 +1407,9 @@ static void prepare_reports(void) if (u) { prepare_lighthouse(b, u->faction); if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { + seen_region *sr = faction_add_seen(u->faction, r, see_unit); if (fval(u, UFL_DISBELIEVES)) { - add_seen(u->faction->seen, r, see_unit, true); - } - else { - add_seen(u->faction->seen, r, see_unit, false); + sr->disbelieves = true; } } } @@ -1432,11 +1424,9 @@ static void prepare_reports(void) } if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { + seen_region *sr = faction_add_seen(u->faction, r, see_unit); if (fval(u, UFL_DISBELIEVES)) { - add_seen(u->faction->seen, r, see_unit, true); - } - else { - add_seen(u->faction->seen, r, see_unit, false); + sr->disbelieves = true; } } } diff --git a/src/seen.c b/src/seen.c index 1b147fb19..928b3f55e 100644 --- a/src/seen.c +++ b/src/seen.c @@ -21,6 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "seen.h" #include +#include #include #include @@ -46,7 +47,7 @@ void seen_done(seen_region * seehash[]) reuse = seehash[i]; seehash[i] = NULL; } - /* free(seehash); */ + free(seehash); } void free_seen(void) @@ -137,7 +138,7 @@ void get_seen_interval(struct seen_region *seen[], struct region **firstp, struc *lastp = interval.last; } -bool add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode, bool dis) +seen_region *add_seen(struct seen_region *seehash[], struct region *r, seen_t mode, bool dis) { seen_region *find = find_seen(seehash, r); if (find == NULL) { @@ -147,13 +148,21 @@ bool add_seen(struct seen_region *seehash[], struct region *r, unsigned char mod find = reuse; reuse = reuse->nextHash; find->nextHash = seehash[index]; + find->mode = mode; seehash[index] = find; find->r = r; } - else if (find->mode >= mode) { - return false; + else if (find->mode < mode) { + find->mode = mode; } - find->mode = mode; find->disbelieves |= dis; - return true; + return find; +} + +seen_region *faction_add_seen(faction *f, region *r, seen_t mode) { + assert(f->seen); +#ifdef SMART_INTERVALS + update_interval(f, r); +#endif + return add_seen(f->seen, r, mode, false); } diff --git a/src/seen.h b/src/seen.h index 2a393adf2..d341cb631 100644 --- a/src/seen.h +++ b/src/seen.h @@ -27,7 +27,7 @@ struct seen_region; extern "C" { #endif - enum { + typedef enum { see_none, see_neighbour, see_lighthouse, @@ -35,13 +35,13 @@ extern "C" { see_far, see_unit, see_battle - }; + } seen_t; typedef struct seen_region { struct seen_region *nextHash; struct seen_region *next; struct region *r; - unsigned char mode; + seen_t mode; bool disbelieves; } seen_region; @@ -51,9 +51,10 @@ void free_seen(void); void link_seen(struct seen_region *seehash[], const struct region * first, const struct region * last); struct seen_region *find_seen(struct seen_region *seehash[], const struct region * r); void get_seen_interval(struct seen_region *seen[], struct region **firstp, struct region **lastp); -bool add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode, bool dis); +seen_region *add_seen(struct seen_region *seehash[], struct region *r, seen_t mode, bool dis); void link_seen(struct seen_region *seehash[], const struct region *first, const struct region *last); void seenhash_map(struct seen_region *seen[], void(*cb)(struct seen_region *, void *), void *cbdata); +struct seen_region *faction_add_seen(struct faction *f, struct region *r, seen_t mode); #ifdef __cplusplus } #endif diff --git a/src/seen.test.c b/src/seen.test.c index a181a690d..551f34fb5 100644 --- a/src/seen.test.c +++ b/src/seen.test.c @@ -19,6 +19,48 @@ static void setup_seen(int x, int y) { } } +static void test_add_seen(CuTest *tc) { + region *r; + seen_region **seen, *sr; + + test_cleanup(); + seen = seen_init(); + r = test_create_region(0, 0, 0); + sr = add_seen(seen, r, see_travel, false); + CuAssertPtrEquals(tc, r, sr->r); + CuAssertIntEquals(tc, see_travel, sr->mode); + CuAssertIntEquals(tc, false, sr->disbelieves); + CuAssertPtrEquals(tc, 0, sr->next); + CuAssertPtrEquals(tc, 0, sr->nextHash); + CuAssertPtrEquals(tc, sr, find_seen(seen, r)); + sr = add_seen(seen, r, see_neighbour, true); + CuAssertIntEquals(tc, true, sr->disbelieves); + CuAssertIntEquals(tc, see_travel, sr->mode); + sr = add_seen(seen, r, see_unit, false); + CuAssertIntEquals(tc, true, sr->disbelieves); + CuAssertIntEquals(tc, see_unit, sr->mode); + seen_done(seen); + test_cleanup(); +} + +static void test_faction_add_seen(CuTest *tc) { + region *r; + faction *f; + seen_region *sr; + + test_cleanup(); + f = test_create_faction(0); + f->seen = seen_init(); + r = test_create_region(0, 0, 0); + sr = faction_add_seen(f, r, see_unit); + r = test_create_region(0, 1, 0); + CuAssertIntEquals(tc, false, sr->disbelieves); + CuAssertPtrEquals(tc, regions, f->first); + CuAssertPtrEquals(tc, r, f->last); + seen_done(f->seen); + test_cleanup(); +} + static void test_prepare_seen(CuTest *tc) { region *r; faction *f; @@ -29,7 +71,7 @@ static void test_prepare_seen(CuTest *tc) { r = test_create_region(0, 0, 0); u = test_create_unit(f, r); f->seen = seen_init(); - add_seen(f->seen, r, see_unit, false); + faction_add_seen(f, r, see_unit); setup_seen(0, 0); r = test_create_region(2, 2, 0); setup_seen(2, 2); @@ -146,6 +188,8 @@ static void test_seenhash_map(CuTest *tc) { CuSuite *get_seen_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_add_seen); + DISABLE_TEST(suite, test_faction_add_seen); SUITE_ADD_TEST(suite, test_prepare_seen); SUITE_ADD_TEST(suite, test_seen_travelthru); SUITE_ADD_TEST(suite, test_seen_region);