forked from github/server
for short lighthouse ranges, don't use lists.
This commit is contained in:
parent
b0905f8e6e
commit
c755c1d69e
3 changed files with 90 additions and 18 deletions
|
@ -1112,30 +1112,43 @@ void reports_done(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_regions_distance_arr(region *r, int radius,
|
int get_regions_distance_arr(region *rc, int radius, region *result[], int size)
|
||||||
region *result[], int size)
|
|
||||||
{
|
{
|
||||||
int n = 0, i;
|
int n = 0, i;
|
||||||
|
|
||||||
if (size>n) {
|
if (size>n) {
|
||||||
result[n++] = r;
|
result[n++] = rc;
|
||||||
}
|
}
|
||||||
for (i = 0; i != n; ++i) {
|
for (i = 0; i != n; ++i) {
|
||||||
|
region *r;
|
||||||
|
int dist;
|
||||||
|
|
||||||
|
r = result[i];
|
||||||
|
dist = distance(rc, r);
|
||||||
|
if (dist<radius) {
|
||||||
region *adj[MAXDIRECTIONS];
|
region *adj[MAXDIRECTIONS];
|
||||||
int d;
|
int d;
|
||||||
|
|
||||||
r = result[i];
|
|
||||||
get_neighbours(r, adj);
|
get_neighbours(r, adj);
|
||||||
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
||||||
r = adj[d];
|
r = adj[d];
|
||||||
|
if (r) {
|
||||||
if (size > n) {
|
if (size > n) {
|
||||||
|
if (dist < distance(rc, r)) {
|
||||||
result[n++] = 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;
|
quicklist *ql, *rlist = NULL;
|
||||||
int qi = 0;
|
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.
|
/** mark all regions seen by the lighthouse.
|
||||||
*/
|
*/
|
||||||
static void prepare_lighthouse(building * b, report_context *ctx)
|
static void prepare_lighthouse_ql(faction *f, quicklist *rlist) {
|
||||||
{
|
quicklist *ql;
|
||||||
faction *f = ctx->f;
|
|
||||||
int range = lighthouse_range(b, f);
|
|
||||||
quicklist *ql, *rlist = get_regions_distance(b->region, range);
|
|
||||||
int qi;
|
int qi;
|
||||||
|
|
||||||
for (ql = rlist, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
|
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);
|
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)
|
void reorder_units(region * r)
|
||||||
{
|
{
|
||||||
unit **unext = &r->units;
|
unit **unext = &r->units;
|
||||||
|
|
|
@ -49,7 +49,8 @@ extern "C" {
|
||||||
struct unit *can_find(struct faction *, struct faction *);
|
struct unit *can_find(struct faction *, struct faction *);
|
||||||
|
|
||||||
bool omniscient(const struct faction *f);
|
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 */
|
/* funktionen zum schreiben eines reports */
|
||||||
void sparagraph(struct strlist **SP, const char *s, unsigned int indent, char mark);
|
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);
|
void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark);
|
||||||
|
|
|
@ -425,9 +425,47 @@ static void test_seen_travelthru(CuTest *tc) {
|
||||||
test_cleanup();
|
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 *get_reports_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
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_prepare_report);
|
||||||
SUITE_ADD_TEST(suite, test_seen_neighbours);
|
SUITE_ADD_TEST(suite, test_seen_neighbours);
|
||||||
SUITE_ADD_TEST(suite, test_seen_travelthru);
|
SUITE_ADD_TEST(suite, test_seen_travelthru);
|
||||||
|
|
Loading…
Reference in a new issue