From 47cf24d254f2606adda08a32c710c1ae5dcf7166 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 16 Jun 2013 16:24:39 +0100
Subject: [PATCH] [Fix] A* bugs where because of a typo. Changed to Djikstra
 now though.

---
 src/space.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/space.c b/src/space.c
index f1ace4c..ba1114e 100644
--- a/src/space.c
+++ b/src/space.c
@@ -165,7 +165,7 @@ static double A_h(StarSystem* n, StarSystem* g);
 static double A_g(SysNode* n);
 static SysNode* A_add(SysNode* first, SysNode* 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 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) {
+  (void)n;
+  (void)g;
   // 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.
@@ -217,8 +220,8 @@ static SysNode* A_rm(SysNode* first, StarSystem* cur) {
   SysNode* n, *p;
 
   if(first->sys == cur) {
-    first->next = NULL;
     n = first->next;
+    first->next = NULL;
     return n;
   }
 
@@ -236,18 +239,18 @@ static SysNode* A_rm(SysNode* first, StarSystem* cur) {
 }
 
 // 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;
   
   if(first == NULL)
-    return 0;
+    return NULL;
 
   n = first;
   do {
     if(n->sys == cur)
-      return 1;
+      return n;
   } while((n=n->next) != NULL);
-  return 0;
+  return NULL;
 }
 
 // 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) {
   int i, cost;
 
-  StarSystem* ssys, *esys, **res;
+  StarSystem* sys, *ssys, *esys, **res;
 
   SysNode* cur, *neighbour;
   SysNode* open, *closed;
+  SysNode* ocost, *ccost;
 
   A_gc = NULL;
 
@@ -308,21 +312,27 @@ StarSystem** system_getJumpPath(int* njumps, char* sysstart, char* sysend) {
     // Get best from open and toss to closed.
     open = A_rm(open, cur->sys);
     closed = A_add(closed, cur);
+    cost = A_g(cur) + 1;
 
     for(i = 0; i < cur->sys->njumps; i++) {
-      neighbour = A_newNode(&systems_stack[cur->sys->jumps[i]], cur);
-      cost = A_g(cur) + 1;
+      sys = &systems_stack[cur->sys->jumps[i]];
+      neighbour = A_newNode(sys, NULL);
 
-      if(A_in(open, neighbour->sys))
-        open = A_rm(open, neighbour->sys); // New path is better.
+      ocost = A_in(open, sys);
+      if((ocost != NULL) && (cost < ocost->g)) {
+        open = A_rm(open, sys); // New path is better.
+      }
 
-      if(A_in(closed, neighbour->sys))
-        closed = A_rm(closed, neighbour->sys); // Shouldn't happen.
+      ccost = A_in(closed, sys);
+      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->r = A_g(neighbour) + A_h(cur->sys, sys);
+        neighbour->parent = cur;
         open = A_add(open, neighbour);
-        neighbour->r = (double)A_g(neighbour) + A_h(neighbour->sys, esys);
       }
     }
   }