2010-08-08 10:06:34 +02:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2015-01-30 20:37:14 +01:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
2017-01-26 17:41:21 +01:00
|
|
|
#include <selist.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "pathfinder.h"
|
|
|
|
|
|
|
|
#include "region.h"
|
|
|
|
#include "terrain.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2016-04-09 14:26:10 +02:00
|
|
|
#define MAXDEPTH 1024
|
|
|
|
|
2012-06-24 07:41:07 +02:00
|
|
|
bool allowed_walk(const region * src, const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (fval(r->terrain, WALK_INTO))
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 07:41:07 +02:00
|
|
|
bool allowed_fly(const region * src, const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (fval(r->terrain, FLY_INTO))
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct node {
|
2015-01-30 20:37:14 +01:00
|
|
|
struct node *next;
|
|
|
|
region *r;
|
|
|
|
struct node *prev;
|
|
|
|
int distance;
|
2010-08-08 10:06:34 +02:00
|
|
|
} node;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static node *node_garbage;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void pathfinder_cleanup(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
while (node_garbage) {
|
|
|
|
node *n = node_garbage;
|
|
|
|
node_garbage = n->next;
|
|
|
|
free(n);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static node *new_node(region * r, int distance, node * prev)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
node *n;
|
|
|
|
if (node_garbage != NULL) {
|
|
|
|
n = node_garbage;
|
|
|
|
node_garbage = n->next;
|
|
|
|
}
|
2018-12-15 19:38:40 +01:00
|
|
|
else {
|
2015-01-30 20:37:14 +01:00
|
|
|
n = malloc(sizeof(node));
|
2018-12-15 19:38:40 +01:00
|
|
|
if (!n) abort();
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
n->next = NULL;
|
|
|
|
n->prev = prev;
|
|
|
|
n->r = r;
|
|
|
|
n->distance = distance;
|
|
|
|
return n;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static node *free_node(node * n)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
node *s = n->next;
|
|
|
|
n->next = node_garbage;
|
|
|
|
node_garbage = n;
|
|
|
|
return s;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void free_nodes(node * root)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
while (root != NULL) {
|
|
|
|
region *r = root->r;
|
|
|
|
freset(r, RF_MARK);
|
|
|
|
root = free_node(root);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:57:51 +02:00
|
|
|
struct selist *regions_in_range(struct region *handle_start, int maxdist,
|
2015-01-30 20:37:14 +01:00
|
|
|
bool(*allowed) (const struct region *, const struct region *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-01-26 17:41:21 +01:00
|
|
|
selist * rlist = NULL;
|
2018-04-28 15:57:51 +02:00
|
|
|
node *root = new_node(handle_start, 0, NULL);
|
|
|
|
node **handle_end = &root->next;
|
2015-01-30 20:37:14 +01:00
|
|
|
node *n = root;
|
|
|
|
|
|
|
|
while (n != NULL) {
|
|
|
|
region *r = n->r;
|
|
|
|
int depth = n->distance + 1;
|
|
|
|
int d;
|
|
|
|
|
|
|
|
if (n->distance >= maxdist)
|
|
|
|
break;
|
|
|
|
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
|
|
|
region *rn = rconnect(r, d);
|
|
|
|
if (rn == NULL)
|
|
|
|
continue;
|
|
|
|
if (fval(rn, RF_MARK))
|
|
|
|
continue; /* already been there */
|
|
|
|
if (allowed && !allowed(r, rn))
|
|
|
|
continue; /* can't go there */
|
|
|
|
|
|
|
|
/* add the region to the list of available ones. */
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_push(&rlist, rn);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
|
|
|
/* make sure we don't go here again, and put the region into the set for
|
|
|
|
further BFS'ing */
|
|
|
|
fset(rn, RF_MARK);
|
2018-04-28 15:57:51 +02:00
|
|
|
*handle_end = new_node(rn, depth, n);
|
|
|
|
handle_end = &(*handle_end)->next;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
n = n->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
free_nodes(root);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return rlist;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:57:51 +02:00
|
|
|
static region **internal_path_find(region * handle_start, const region * target,
|
2015-01-30 20:37:14 +01:00
|
|
|
int maxlen, bool(*allowed) (const region *, const region *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
static region *path[MAXDEPTH + 2]; /* STATIC_RETURN: used for return, not across calls */
|
|
|
|
direction_t d;
|
2018-04-28 15:57:51 +02:00
|
|
|
node *root = new_node(handle_start, 0, NULL);
|
|
|
|
node **handle_end = &root->next;
|
2015-01-30 20:37:14 +01:00
|
|
|
node *n = root;
|
|
|
|
bool found = false;
|
|
|
|
assert(maxlen <= MAXDEPTH);
|
2018-04-28 15:57:51 +02:00
|
|
|
fset(handle_start, RF_MARK);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
|
|
|
while (n != NULL) {
|
|
|
|
region *r = n->r;
|
|
|
|
int depth = n->distance + 1;
|
|
|
|
if (n->distance >= maxlen)
|
|
|
|
break;
|
|
|
|
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
|
|
|
region *rn = rconnect(r, d);
|
|
|
|
if (rn == NULL)
|
|
|
|
continue;
|
|
|
|
if (fval(rn, RF_MARK))
|
|
|
|
continue; /* already been there */
|
|
|
|
if (!allowed(r, rn))
|
|
|
|
continue; /* can't go there */
|
|
|
|
if (rn == target) {
|
|
|
|
int i = depth;
|
|
|
|
path[i + 1] = NULL;
|
|
|
|
path[i] = rn;
|
|
|
|
while (n) {
|
|
|
|
path[--i] = n->r;
|
|
|
|
n = n->prev;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fset(rn, RF_MARK);
|
2018-04-28 15:57:51 +02:00
|
|
|
*handle_end = new_node(rn, depth, n);
|
|
|
|
handle_end = &(*handle_end)->next;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
n = n->next;
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
free_nodes(root);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (found)
|
2015-01-30 20:37:14 +01:00
|
|
|
return path;
|
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 07:41:07 +02:00
|
|
|
bool
|
2018-04-28 15:57:51 +02:00
|
|
|
path_exists(region * handle_start, const region * target, int maxlen,
|
2015-01-30 20:37:14 +01:00
|
|
|
bool(*allowed) (const region *, const region *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2018-04-28 15:57:51 +02:00
|
|
|
assert((!fval(handle_start, RF_MARK) && !fval(target, RF_MARK))
|
2015-01-30 20:37:14 +01:00
|
|
|
|| !"Some Algorithm did not clear its RF_MARKs!");
|
2018-04-28 15:57:51 +02:00
|
|
|
if (handle_start == target)
|
2015-01-30 20:37:14 +01:00
|
|
|
return true;
|
2018-04-28 15:57:51 +02:00
|
|
|
if (internal_path_find(handle_start, target, maxlen, allowed) != NULL)
|
2015-01-30 20:37:14 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:57:51 +02:00
|
|
|
region **path_find(region * handle_start, const region * target, int maxlen,
|
2015-01-30 20:37:14 +01:00
|
|
|
bool(*allowed) (const region *, const region *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2018-04-28 15:57:51 +02:00
|
|
|
assert((!fval(handle_start, RF_MARK) && !fval(target, RF_MARK))
|
2015-01-30 20:37:14 +01:00
|
|
|
|| !"Did you call path_init()?");
|
2018-04-28 15:57:51 +02:00
|
|
|
return internal_path_find(handle_start, target, maxlen, allowed);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|