[Fix] A* bugs where because of a typo. Changed to Djikstra now though.
This commit is contained in:
parent
bc0ebf678e
commit
47cf24d254
42
src/space.c
42
src/space.c
@ -165,7 +165,7 @@ static double A_h(StarSystem* n, StarSystem* g);
|
|||||||
static double A_g(SysNode* n);
|
static double A_g(SysNode* n);
|
||||||
static SysNode* A_add(SysNode* first, SysNode* cur);
|
static SysNode* A_add(SysNode* first, SysNode* cur);
|
||||||
static SysNode* A_rm(SysNode* first, StarSystem* cur);
|
static SysNode* A_rm(SysNode* first, StarSystem* cur);
|
||||||
static int A_in(SysNode* first, StarSystem* cur);
|
static SysNode* A_in(SysNode* first, StarSystem* cur);
|
||||||
static SysNode* A_lowest(SysNode* first);
|
static SysNode* A_lowest(SysNode* first);
|
||||||
static void A_freeList(SysNode* first);
|
static void A_freeList(SysNode* first);
|
||||||
|
|
||||||
@ -188,8 +188,11 @@ static SysNode* A_newNode(StarSystem* sys, SysNode* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static double A_h(StarSystem* n, StarSystem* g) {
|
static double A_h(StarSystem* n, StarSystem* g) {
|
||||||
|
(void)n;
|
||||||
|
(void)g;
|
||||||
// Euclidean distance.
|
// Euclidean distance.
|
||||||
return sqrt(pow2(n->pos.x - g->pos.x) + pow2(n->pos.y - g->pos.y))/100.;
|
//return sqrt(pow2(n->pos.x - g->pos.x) + pow2(n->pos.y - g->pos.y))/100.;
|
||||||
|
return 0.;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the g from a node.
|
// Get the g from a node.
|
||||||
@ -217,8 +220,8 @@ static SysNode* A_rm(SysNode* first, StarSystem* cur) {
|
|||||||
SysNode* n, *p;
|
SysNode* n, *p;
|
||||||
|
|
||||||
if(first->sys == cur) {
|
if(first->sys == cur) {
|
||||||
first->next = NULL;
|
|
||||||
n = first->next;
|
n = first->next;
|
||||||
|
first->next = NULL;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,18 +239,18 @@ static SysNode* A_rm(SysNode* first, StarSystem* cur) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if node is in linked list.
|
// Check if node is in linked list.
|
||||||
static int A_in(SysNode* first, StarSystem* cur) {
|
static SysNode* A_in(SysNode* first, StarSystem* cur) {
|
||||||
SysNode* n;
|
SysNode* n;
|
||||||
|
|
||||||
if(first == NULL)
|
if(first == NULL)
|
||||||
return 0;
|
return NULL;
|
||||||
|
|
||||||
n = first;
|
n = first;
|
||||||
do {
|
do {
|
||||||
if(n->sys == cur)
|
if(n->sys == cur)
|
||||||
return 1;
|
return n;
|
||||||
} while((n=n->next) != NULL);
|
} while((n=n->next) != NULL);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the lowest ranking node from a linked list of nodes.
|
// Return the lowest ranking node from a linked list of nodes.
|
||||||
@ -288,10 +291,11 @@ static void A_freeList(SysNode* first) {
|
|||||||
StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
|
StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
|
||||||
int i, cost;
|
int i, cost;
|
||||||
|
|
||||||
StarSystem* ssys, *esys, **res;
|
StarSystem* sys, *ssys, *esys, **res;
|
||||||
|
|
||||||
SysNode* cur, *neighbour;
|
SysNode* cur, *neighbour;
|
||||||
SysNode* open, *closed;
|
SysNode* open, *closed;
|
||||||
|
SysNode* ocost, *ccost;
|
||||||
|
|
||||||
A_gc = NULL;
|
A_gc = NULL;
|
||||||
|
|
||||||
@ -308,21 +312,27 @@ StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
|
|||||||
// Get best from open and toss to closed.
|
// Get best from open and toss to closed.
|
||||||
open = A_rm(open, cur->sys);
|
open = A_rm(open, cur->sys);
|
||||||
closed = A_add(closed, cur);
|
closed = A_add(closed, cur);
|
||||||
|
cost = A_g(cur) + 1;
|
||||||
|
|
||||||
for(i = 0; i < cur->sys->njumps; i++) {
|
for(i = 0; i < cur->sys->njumps; i++) {
|
||||||
neighbour = A_newNode(&systems_stack[cur->sys->jumps[i]], cur);
|
sys = &systems_stack[cur->sys->jumps[i]];
|
||||||
cost = A_g(cur) + 1;
|
neighbour = A_newNode(sys, NULL);
|
||||||
|
|
||||||
if(A_in(open, neighbour->sys))
|
ocost = A_in(open, sys);
|
||||||
open = A_rm(open, neighbour->sys); // New path is better.
|
if((ocost != NULL) && (cost < ocost->g)) {
|
||||||
|
open = A_rm(open, sys); // New path is better.
|
||||||
|
}
|
||||||
|
|
||||||
if(A_in(closed, neighbour->sys))
|
ccost = A_in(closed, sys);
|
||||||
closed = A_rm(closed, neighbour->sys); // Shouldn't happen.
|
if(ccost != NULL) {
|
||||||
|
closed = A_rm(closed, sys); // Shouldn't happen.
|
||||||
|
}
|
||||||
|
|
||||||
if(!A_in(open, neighbour->sys) && !A_in(closed, neighbour->sys)) {
|
if((ocost == NULL) && (ccost == NULL)) {
|
||||||
neighbour->g = cost;
|
neighbour->g = cost;
|
||||||
|
neighbour->r = A_g(neighbour) + A_h(cur->sys, sys);
|
||||||
|
neighbour->parent = cur;
|
||||||
open = A_add(open, neighbour);
|
open = A_add(open, neighbour);
|
||||||
neighbour->r = (double)A_g(neighbour) + A_h(neighbour->sys, esys);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user