nexus/astar.c

131 lines
2.5 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "astar.h"
#include "defs.h"
#include "flag.h"
#include "move.h"
#include "nexus.h"
#include "lf.h"
#include "map.h"
#include "objects.h"
#include "text.h"
int calcg(lifeform_t *lf, cell_t *thiscell, node_t *parent, int dirfromparent) {
int fromstart;
object_t *o;
int dooropen;
enum ATTRBRACKET wis = AT_EXHIGH;
if (lf) {
wis = getattrbracket(getattr(lf, A_WIS), A_WIS, NULL);
}
if (isorthogonal(dirfromparent)) {
fromstart = parent->fromstart + 10;
} else {
fromstart = parent->fromstart + 14;
}
// closed doors count for more.
if (wis >= AT_AVERAGE) {
o = hasdoor(thiscell);
if (o && isdoor(o, &dooropen) && !dooropen) {
fromstart += 10;
}
}
// lf's wisdom will affect what is the "best" path
if (wis >= AT_GTAVERAGE) {
// avoid mud, etc
o = hasobwithflag(thiscell->obpile, F_REDUCEMOVEMENT);
if (o) {
int howmuch;
sumflags(o->flags, F_REDUCEMOVEMENT, &howmuch, NULL, NULL);
if (howmuch > 0) {
fromstart += (10*howmuch);
}
}
}
return fromstart;
}
int calcg_map(cell_t *thiscell, node_t *parent, int dirfromparent) {
int fromstart;
/*
if (isorthogonal(dirfromparent)) {
fromstart = parent->fromstart + 10;
} else {
fromstart = parent->fromstart + 14;
}
*/
fromstart = parent->fromstart + 10;
return fromstart;
}
int calcheuristic(cell_t *c, cell_t *end) {
int h,xd,yd;
xd = abs(c->x - end->x);
yd = abs(c->y - end->y);
if (xd > yd) {
h = (14*yd) + 10*(xd - yd);
} else {
h = (14*xd) + 10*(yd - xd);
}
return h;
}
//
int calch_map(cell_t *c, int destroomid) {
int h,xd,yd;
cell_t *end = NULL;
end = getroommidcell(c->map, destroomid);
// returns distance to centre of given roomid
xd = abs(c->x - end->x);
yd = abs(c->y - end->y);
if (xd > yd) {
h = (14*yd) + 10*(xd - yd);
} else {
h = (14*xd) + 10*(yd - xd);
}
return h;
}
void clearnode(node_t *n) {
n->c = NULL;
n->parent = NULL;
n->dirfromparent = D_NONE;
n->fromstart = -1;
n->heur = -1;
n->cost = -1;
}
// inserts node 'n' into list 'list' (sorted by cost)
// returns index of insert position.
int insert(node_t *n, node_t *list, int *listcount) {
int pos,i;
// add it to open list, with parent = node[cur]
pos = -1;
for (pos = 0; pos < *listcount; pos++) {
if (n->cost <= list[pos].cost) {
// add here.
break;
}
}
// shuffle others up
for (i = *listcount; i > pos; i--) {
list[i] = list[i-1];
}
(*listcount)++;
// fill in .
list[pos] = *n;
return pos;
}