From 1ca67a57d255ac9abde979f152f354998f4e9393 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 17 Sep 2016 14:54:39 +0200 Subject: [PATCH 1/4] add a new test for lighthouse capacity. https://bugs.eressea.de/view.php?id=2237 --- src/reports.c | 3 ++- src/reports.h | 1 + src/reports.test.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/reports.c b/src/reports.c index 48aee609d..ec2e002f5 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1418,8 +1418,9 @@ void prepare_seen(faction *f) link_seen(f->seen, f->first, f->last); } -static void prepare_report(struct report_context *ctx, faction *f) +void prepare_report(struct report_context *ctx, faction *f) { + assert(f->seen); prepare_seen(f); ctx->f = f; ctx->report_time = time(NULL); diff --git a/src/reports.h b/src/reports.h index 584fe7d4a..f9bc9b495 100644 --- a/src/reports.h +++ b/src/reports.h @@ -57,6 +57,7 @@ extern "C" { const struct unit *u, unsigned int indent, int mode); void prepare_seen(struct faction *f); + void prepare_report(struct report_context *ctx, struct faction *f); int reports(void); int write_reports(struct faction *f, time_t ltime); int init_reports(void); diff --git a/src/reports.test.c b/src/reports.test.c index c27a0f6f9..71bd7dd80 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -3,6 +3,7 @@ #include "reports.h" #include "report.h" #include "creport.h" +#include "lighthouse.h" #include "move.h" #include "seen.h" #include "travelthru.h" @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -450,6 +452,41 @@ static void test_arg_resources(CuTest *tc) { test_cleanup(); } +void test_prepare_lighthouse(CuTest *tc) { + building *b; + building_type *btype; + unit *u; + region *r1, *r2; + faction *f; + report_context ctx; + const struct terrain_type *t_ocean, *t_plain; + + test_setup(); + f = test_create_faction(0); + t_ocean = test_create_terrain("ocean", SEA_REGION); + t_plain = test_create_terrain("plain", LAND_REGION); + btype = test_create_buildingtype("lighthouse"); + btype->maxcapacity = 4; + r1 = test_create_region(0, 0, 0); + r2 = test_create_region(0, 0, 0); + b = test_create_building(r1, btype); + b->size = 10; + update_lighthouse(b); + u = test_create_unit(test_create_faction(0), r1); + u->number = 4; + u->building = b; + CuAssertPtrEquals(tc, b, inside_building(u)); + u = test_create_unit(f, r1); + u->building = b; + CuAssertPtrEquals(tc, NULL, inside_building(u)); + u->faction->seen = seen_init(); + prepare_report(&ctx, f); + CuAssertPtrEquals(tc, r1, ctx.first); + CuAssertPtrEquals(tc, r2, ctx.last); + seen_done(f->seen); + test_cleanup(); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -464,5 +501,6 @@ CuSuite *get_reports_suite(void) SUITE_ADD_TEST(suite, test_write_unit); SUITE_ADD_TEST(suite, test_write_spell_syntax); SUITE_ADD_TEST(suite, test_arg_resources); + SUITE_ADD_TEST(suite, test_prepare_lighthouse); return suite; } From c4dbb696814ab192f57ce624203f01150d09150f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 17 Sep 2016 15:29:28 +0200 Subject: [PATCH 2/4] previous test was bad and did not actually exercise prepare_lighthouse. made a slighlty better test, which is failing for the right reasons. --- src/reports.c | 5 +++-- src/reports.test.c | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/reports.c b/src/reports.c index ec2e002f5..183d6ce6c 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1316,9 +1316,10 @@ static void prepare_reports(void) } for (u = r->units; u; u = u->next) { - if (u->building && u->building->type == bt_lighthouse) { + b = u->building; + if (b && b->type == bt_lighthouse) { /* we are in a lighthouse. add the regions we can see from here! */ - prepare_lighthouse(u->building, u->faction); + prepare_lighthouse(b, u->faction); } if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { diff --git a/src/reports.test.c b/src/reports.test.c index 71bd7dd80..14b3c7314 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -458,7 +458,6 @@ void test_prepare_lighthouse(CuTest *tc) { unit *u; region *r1, *r2; faction *f; - report_context ctx; const struct terrain_type *t_ocean, *t_plain; test_setup(); @@ -467,23 +466,26 @@ void test_prepare_lighthouse(CuTest *tc) { t_plain = test_create_terrain("plain", LAND_REGION); btype = test_create_buildingtype("lighthouse"); btype->maxcapacity = 4; - r1 = test_create_region(0, 0, 0); - r2 = test_create_region(0, 0, 0); + r1 = test_create_region(0, 0, t_plain); + r2 = test_create_region(1, 0, t_ocean); b = test_create_building(r1, btype); + b->flags |= BLD_MAINTAINED; b->size = 10; update_lighthouse(b); u = test_create_unit(test_create_faction(0), r1); u->number = 4; u->building = b; + set_level(u, SK_PERCEPTION, 3); + CuAssertIntEquals(tc, 1, lighthouse_range(b, u->faction)); CuAssertPtrEquals(tc, b, inside_building(u)); u = test_create_unit(f, r1); u->building = b; + set_level(u, SK_PERCEPTION, 3); + CuAssertIntEquals(tc, 0, lighthouse_range(b, u->faction)); CuAssertPtrEquals(tc, NULL, inside_building(u)); - u->faction->seen = seen_init(); - prepare_report(&ctx, f); - CuAssertPtrEquals(tc, r1, ctx.first); - CuAssertPtrEquals(tc, r2, ctx.last); - seen_done(f->seen); + init_reports(); + CuAssertPtrNotNull(tc, find_seen(f->seen, r1)); + CuAssertPtrEquals(tc, 0, find_seen(f->seen, r2)); test_cleanup(); } From eb7dd051f85b87355f6ea5214b3ff545a976dd55 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 17 Sep 2016 15:31:04 +0200 Subject: [PATCH 3/4] fix bug 2237, respect lighthouse capacity. --- src/reports.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reports.c b/src/reports.c index 183d6ce6c..101eb75e8 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1317,7 +1317,7 @@ static void prepare_reports(void) for (u = r->units; u; u = u->next) { b = u->building; - if (b && b->type == bt_lighthouse) { + if (b && b->type == bt_lighthouse && inside_building(u)) { /* we are in a lighthouse. add the regions we can see from here! */ prepare_lighthouse(b, u->faction); } From 4c7d51990e095a1a3872188f4a10f90231a0ca28 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 17 Sep 2016 15:37:11 +0200 Subject: [PATCH 4/4] prepare_report should stay static, remove that change. --- src/reports.c | 2 +- src/reports.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/reports.c b/src/reports.c index 101eb75e8..e0d4cff37 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1419,7 +1419,7 @@ void prepare_seen(faction *f) link_seen(f->seen, f->first, f->last); } -void prepare_report(struct report_context *ctx, faction *f) +static void prepare_report(struct report_context *ctx, faction *f) { assert(f->seen); prepare_seen(f); diff --git a/src/reports.h b/src/reports.h index f9bc9b495..584fe7d4a 100644 --- a/src/reports.h +++ b/src/reports.h @@ -57,7 +57,6 @@ extern "C" { const struct unit *u, unsigned int indent, int mode); void prepare_seen(struct faction *f); - void prepare_report(struct report_context *ctx, struct faction *f); int reports(void); int write_reports(struct faction *f, time_t ltime); int init_reports(void);