From 9baba00087bd8bdaf2d68ecbaad2b2352d20267a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Sep 2016 21:29:42 +0200 Subject: [PATCH 1/6] use mtype->key instead of repeated hashstring calls. --- src/util/crmessage.c | 4 ++-- src/util/nrmessage.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/crmessage.c b/src/util/crmessage.c index 98afe0f05..56f1e5dcd 100644 --- a/src/util/crmessage.c +++ b/src/util/crmessage.c @@ -81,7 +81,7 @@ static crmessage_type *crtypes[CRMAXHASH]; static crmessage_type *crt_find(const struct message_type *mtype) { - unsigned int hash = hashstring(mtype->name) % CRMAXHASH; + unsigned int hash = mtype->key % CRMAXHASH; crmessage_type *found = NULL; crmessage_type *type = crtypes[hash]; while (type) { @@ -94,7 +94,7 @@ static crmessage_type *crt_find(const struct message_type *mtype) void crt_register(const struct message_type *mtype) { - unsigned int hash = hashstring(mtype->name) % CRMAXHASH; + unsigned int hash = mtype->key % CRMAXHASH; crmessage_type *crt = crtypes[hash]; while (crt && crt->mtype != mtype) { crt = crt->next; diff --git a/src/util/nrmessage.c b/src/util/nrmessage.c index e3b0bcc0b..79638ec30 100644 --- a/src/util/nrmessage.c +++ b/src/util/nrmessage.c @@ -40,7 +40,7 @@ nrmessage_type *nrt_find(const struct locale * lang, const struct message_type * mtype) { nrmessage_type *found = NULL; - unsigned int hash = hashstring(mtype->name) % NRT_MAXHASH; + unsigned int hash = mtype->key % NRT_MAXHASH; nrmessage_type *type = nrtypes[hash]; while (type) { if (type->mtype == mtype) { @@ -101,7 +101,7 @@ void nrt_register(const struct message_type *mtype, const struct locale *lang, const char *string, int level, const char *section) { - unsigned int hash = hashstring(mtype->name) % NRT_MAXHASH; + unsigned int hash = mtype->key % NRT_MAXHASH; nrmessage_type *nrt = nrtypes[hash]; while (nrt && (nrt->lang != lang || nrt->mtype != mtype)) { nrt = nrt->next; From b0905f8e6e9512c4859327e8896754c85712168e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 23 Sep 2016 17:03:18 +0200 Subject: [PATCH 2/6] incomplete new get_regions_distance implementation --- src/reports.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/reports.c b/src/reports.c index ff076a866..e0c2642ba 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1112,6 +1112,29 @@ void reports_done(void) { } } +static int get_regions_distance_arr(region *r, int radius, + region *result[], int size) +{ + int n = 0, i; + + if (size>n) { + result[n++] = r; + } + for (i = 0; i != n; ++i) { + region *adj[MAXDIRECTIONS]; + int d; + + r = result[i]; + get_neighbours(r, adj); + for (d = 0; d != MAXDIRECTIONS; ++d) { + r = adj[d]; + if (size>n) { + result[n++] = r; + } + } + } +} + static quicklist *get_regions_distance(region * root, int radius) { quicklist *ql, *rlist = NULL; From c755c1d69e52c147138c3e4e84b93c577474e502 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 23 Sep 2016 19:51:33 +0200 Subject: [PATCH 3/6] for short lighthouse ranges, don't use lists. --- src/reports.c | 67 ++++++++++++++++++++++++++++++++++------------ src/reports.h | 3 ++- src/reports.test.c | 38 ++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/reports.c b/src/reports.c index e0c2642ba..3e67f2f2b 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1112,30 +1112,43 @@ void reports_done(void) { } } -static int get_regions_distance_arr(region *r, int radius, - region *result[], int size) +int get_regions_distance_arr(region *rc, int radius, region *result[], int size) { int n = 0, i; if (size>n) { - result[n++] = r; + result[n++] = rc; } for (i = 0; i != n; ++i) { - region *adj[MAXDIRECTIONS]; - int d; - + region *r; + int dist; + r = result[i]; - get_neighbours(r, adj); - for (d = 0; d != MAXDIRECTIONS; ++d) { - r = adj[d]; - if (size>n) { - result[n++] = r; + dist = distance(rc, r); + if (dist n) { + if (dist < distance(rc, r)) { + result[n++] = r; + } + } + else { + return -1; + } + } } } } + return n; } -static quicklist *get_regions_distance(region * root, int radius) +quicklist *get_regions_distance(region * root, int radius) { quicklist *ql, *rlist = NULL; int qi = 0; @@ -1193,11 +1206,8 @@ static void add_seen_nb(faction *f, region *r, seen_mode mode) { /** mark all regions seen by the lighthouse. */ -static void prepare_lighthouse(building * b, report_context *ctx) -{ - faction *f = ctx->f; - int range = lighthouse_range(b, f); - quicklist *ql, *rlist = get_regions_distance(b->region, range); +static void prepare_lighthouse_ql(faction *f, quicklist *rlist) { + quicklist *ql; int qi; for (ql = rlist, qi = 0; ql; ql_advance(&ql, &qi, 1)) { @@ -1209,6 +1219,29 @@ static void prepare_lighthouse(building * b, report_context *ctx) ql_free(rlist); } +static void prepare_lighthouse(building * b, report_context *ctx) +{ + faction *f = ctx->f; + int range = lighthouse_range(b, f); + + if (range > 2) { + prepare_lighthouse_ql(f, get_regions_distance(b->region, range)); + } + else { + region *result[64]; + int n, i; + + n = get_regions_distance_arr(b->region, range, result, 64); + assert(n > 0 && n <= 64); + for (i = 0; i != n; ++i) { + region *rl = result[i]; + if (!fval(rl->terrain, FORBIDDEN_REGION)) { + add_seen_nb(f, rl, seen_lighthouse); + } + } + } +} + void reorder_units(region * r) { unit **unext = &r->units; diff --git a/src/reports.h b/src/reports.h index 8684dfa3f..e2484de74 100644 --- a/src/reports.h +++ b/src/reports.h @@ -49,7 +49,8 @@ extern "C" { struct unit *can_find(struct faction *, struct faction *); bool omniscient(const struct faction *f); - + struct quicklist *get_regions_distance(struct region * root, int radius); + int get_regions_distance_arr(struct region *r, int radius, struct region *result[], int size); /* funktionen zum schreiben eines reports */ void sparagraph(struct strlist **SP, const char *s, unsigned int indent, char mark); void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark); diff --git a/src/reports.test.c b/src/reports.test.c index d9b427ff2..ffa2d215c 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -425,9 +425,47 @@ static void test_seen_travelthru(CuTest *tc) { test_cleanup(); } +static void test_region_distance(CuTest *tc) { + region *r; + region *result[8]; + test_setup(); + r = test_create_region(0, 0, 0); + CuAssertIntEquals(tc, 1, get_regions_distance_arr(r, 0, result, 8)); + CuAssertPtrEquals(tc, r, result[0]); + CuAssertIntEquals(tc, 1, get_regions_distance_arr(r, 1, result, 8)); + test_create_region(1, 0, 0); + test_create_region(0, 1, 0); + CuAssertIntEquals(tc, 1, get_regions_distance_arr(r, 0, result, 8)); + CuAssertIntEquals(tc, 3, get_regions_distance_arr(r, 1, result, 8)); + CuAssertIntEquals(tc, 3, get_regions_distance_arr(r, 2, result, 8)); + test_cleanup(); +} + +static void test_region_distance_ql(CuTest *tc) { + region *r; + quicklist *ql; + test_setup(); + r = test_create_region(0, 0, 0); + ql = get_regions_distance(r, 0); + CuAssertIntEquals(tc, 1, ql_length(ql)); + CuAssertPtrEquals(tc, r, ql_get(ql, 0)); + ql_free(ql); + test_create_region(1, 0, 0); + test_create_region(0, 1, 0); + ql = get_regions_distance(r, 1); + CuAssertIntEquals(tc, 3, ql_length(ql)); + ql_free(ql); + ql = get_regions_distance(r, 2); + CuAssertIntEquals(tc, 3, ql_length(ql)); + ql_free(ql); + test_cleanup(); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_region_distance); + SUITE_ADD_TEST(suite, test_region_distance_ql); SUITE_ADD_TEST(suite, test_prepare_report); SUITE_ADD_TEST(suite, test_seen_neighbours); SUITE_ADD_TEST(suite, test_seen_travelthru); From 9eb376e0e5b4fe640a0490919c4b5e80d1196d99 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 24 Sep 2016 11:20:33 +0200 Subject: [PATCH 4/6] clumsy repair of duplicate regions for range >= 2 (RF_MARK) --- src/reports.c | 7 ++++++- src/reports.test.c | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/reports.c b/src/reports.c index 3e67f2f2b..be8693e95 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1118,6 +1118,7 @@ int get_regions_distance_arr(region *rc, int radius, region *result[], int size) if (size>n) { result[n++] = rc; + fset(rc, RF_MARK); } for (i = 0; i != n; ++i) { region *r; @@ -1134,8 +1135,9 @@ int get_regions_distance_arr(region *rc, int radius, region *result[], int size) r = adj[d]; if (r) { if (size > n) { - if (dist < distance(rc, r)) { + if (!fval(r, RF_MARK) && dist < distance(rc, r)) { result[n++] = r; + fset(r, RF_MARK); } } else { @@ -1145,6 +1147,9 @@ int get_regions_distance_arr(region *rc, int radius, region *result[], int size) } } } + for (i = 0; i != n; ++i) { + freset(result[i], RF_MARK); + } return n; } diff --git a/src/reports.test.c b/src/reports.test.c index ffa2d215c..bef1047d8 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -425,6 +425,26 @@ static void test_seen_travelthru(CuTest *tc) { test_cleanup(); } +static void test_region_distance_max(CuTest *tc) { + region *r; + region *result[64]; + int x, y; + test_setup(); + r = test_create_region(0, 0, 0); + for (x=-3;x<=3;++x) { + for (y = -3; y <= 3; ++y) { + if (x != 0 || y != 0) { + test_create_region(x, y, 0); + } + } + } + CuAssertIntEquals(tc, 1, get_regions_distance_arr(r, 0, result, 64)); + CuAssertIntEquals(tc, 7, get_regions_distance_arr(r, 1, result, 64)); + CuAssertIntEquals(tc, 19, get_regions_distance_arr(r, 2, result, 64)); + CuAssertIntEquals(tc, 37, get_regions_distance_arr(r, 3, result, 64)); + test_cleanup(); +} + static void test_region_distance(CuTest *tc) { region *r; region *result[8]; @@ -465,6 +485,7 @@ CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_region_distance); + SUITE_ADD_TEST(suite, test_region_distance_max); SUITE_ADD_TEST(suite, test_region_distance_ql); SUITE_ADD_TEST(suite, test_prepare_report); SUITE_ADD_TEST(suite, test_seen_neighbours); From d758f065d533d2cdd8fd1a4bc158841cccfbb85d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 24 Sep 2016 11:33:00 +0200 Subject: [PATCH 5/6] only use lists for lighthouses with range > 3. --- src/reports.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reports.c b/src/reports.c index be8693e95..504ab299d 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1229,7 +1229,7 @@ static void prepare_lighthouse(building * b, report_context *ctx) faction *f = ctx->f; int range = lighthouse_range(b, f); - if (range > 2) { + if (range > 3) { prepare_lighthouse_ql(f, get_regions_distance(b->region, range)); } else { From 2b4d2db112555ecfa8f7704644b36d27f0b2a145 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Mar 2017 16:23:33 +0100 Subject: [PATCH 6/6] VS 15 not yet supported. --- tests/runtests.bat | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/runtests.bat b/tests/runtests.bat index 7a58e5762..41b5f8056 100644 --- a/tests/runtests.bat +++ b/tests/runtests.bat @@ -3,7 +3,6 @@ IF EXIST ..\build-vs10 SET BUILD=..\build-vs10\eressea\Debug IF EXIST ..\build-vs11 SET BUILD=..\build-vs11\eressea\Debug IF EXIST ..\build-vs12 SET BUILD=..\build-vs12\eressea\Debug IF EXIST ..\build-vs14 SET BUILD=..\build-vs14\eressea\Debug -IF EXIST ..\build-vs15 SET BUILD=..\build-vs15\eressea\Debug SET SERVER=%BUILD%\eressea.exe %BUILD%\test_eressea.exe %SERVER% ..\scripts\run-tests.lua