]> pd.if.org Git - hexagon/blobdiff - hexmap.c
Improved astar api. Added doc file.
[hexagon] / hexmap.c
index bc20d8dfde2fedafaf06932ace7e8b0699a37a5a..baeb9610599e3b7831693b0808044be650cfd244 100644 (file)
--- a/hexmap.c
+++ b/hexmap.c
@@ -21,7 +21,7 @@ double HL_vertexv[] = {
        .288675134594812882254574390251, -0.5};
 
 /* these all are for a hex one unit across */
-double          hexptvd[6][2] = {
+static double          hexptvd[6][2] = {
        {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
        {.288675134594812882254574390251, 0.5}, /* 0.5/sqrt3 */
        {-.288675134594812882254574390251, 0.5},
@@ -30,8 +30,10 @@ double          hexptvd[6][2] = {
        {.288675134594812882254574390251, -0.5}
 };
 
+#if 0
+
 /* TODO how is this related? to the above? */
-double          texptvd[6][2] = {
+static double          texptvd[6][2] = {
        {1.154700538379251529018297561004, 0.5},        /* 2.0/sqrt3 */
        {.866025403784438646763723170753, 1.0}, /* 1.5/sqrt3 */
        {.288675134594812882254574390251, 1.0},
@@ -40,7 +42,7 @@ double          texptvd[6][2] = {
        {.866025403784438646763723170753, 0.0}
 };
 
-double          hexpthd[6][2] = {
+static double          hexpthd[6][2] = {
        {0.0, .577350269189625764509148780502},
        {0.5, .288675134594812882254574390251},
        {0.5, -.288675134594812882254574390251},
@@ -49,6 +51,8 @@ double          hexpthd[6][2] = {
        {-0.5, .288675134594812882254574390251}
 };
 
+#endif
+
 void HL_vertices(int cantor, double *vc) {
        int i;
        double xc, yc;
@@ -101,10 +105,7 @@ int HL_hexcenter(int cantor, double *xc, double *yc) {
  * your input coordinates first.
  */
 int HL_cantor_bin(double x, double y) {
-       int i, j;
-
-       HL_hexbin(1.0, x, y, &i, &j);
-       return HL_cantor_xy(i, j);
+       return HL_hexbin(1.0, x, y, 0, 0);
 }
 
 static int xy2ijk(int x, int y, int *i, int *j, int *k) {
@@ -208,6 +209,12 @@ int HL_hexes_at_range(int hex, int range, int *list, int size) {
        return range * 6;
 }
 
+int HL_adjacent_hex(int start, int dir) {
+       if (dir < 0 || dir > 5) return 0;
+
+       return HL_adjhex(start, dir);
+}
+
 /* direction 0 is positive X , counter clockwise from there */
 int HL_adjhex(int start, int dir) {
        int c[3];
@@ -232,11 +239,6 @@ int HL_adjhex(int start, int dir) {
        return HL_cantor_ijkp(c);
 }
 
-/* returns last hex in found path */
-int HL_findpath(int start, int end, int *path, int size) {
-       return 0;
-}
-
 int HL_cantor_xyp(int *xy) {
        return HL_cantor_xy(xy[0], xy[1]);
 }
@@ -325,165 +327,6 @@ int HL_map_max_dimension(void) {
         return low;
 }
 
-#ifdef ASTAR
-
-struct astar_hex {
-       int hex;
-       int f, g, h;
-       int parent;
-};
-
-struct astar {
-       int open, closed; /* how many in open or closed */
-       int closedroom, openroom; /* how much is malloced */
-       struct astar_hex *openlist;
-       struct astar_hex *closedlist;
-       int known, index;
-       int error;
-};
-
-static void astar_init(struct astar *state) {
-       state->open = 0;
-       state->closed = 0;
-       state->closedroom = 0;
-       state->openroom = 0;
-       state->openlist = 0;
-       state->closedlist = 0;
-       state->error = 0;
-       state->known = 0;
-       state->index = 0;
-
-       return;
-}
-
-static void removeopen(struct astar *s, int hex) {
-       int index;
-
-       if (s->error) return;
-       if (s->open == 0) return;
-
-       if (s->known != hex) {
-               for (index = 0; index < s->open; index++) {
-                       if (s->openlist[index].hex == hex) break;
-               }
-       } else {
-               index = s->index;
-               s->known = 0; s->index = 0;
-       }
-       s->openlist[index] = s->openlist[open];
-       s->open--;
-}
-
-static void addopen(struct astar *s, int hex) {
-       struct astar_hex *mem;
-       if (s->error) return;
-
-       if (s->open + 1 > s->openroom) {
-               mem = realloc(s->openlist, s->openroom * 2 * sizeof *mem);
-               if (mem == NULL) {
-                       s->error = errno;
-                       return;
-               }
-               state->openlist = mem;
-               s->openroom = s->openroom * 2;
-       }
-
-       s->openlist[s->open].hex = hex;
-       s->openlist[s->open].f = 0;
-       s->openlist[s->open].g = 0;
-       s->openlist[s->open].h = 0;
-       s->known = hex;
-       s->index = s->open;
-       s->open = s->open + 1;
-}
-
-int lowrank(struct astar *s) {
-       int f;
-       int hex;
-       int i;
-
-       if (!s || s->open = 0) return 0;
-
-       hex = s->openlist[0].hex;
-       f = s->openlist[0].f;
-
-       for(i=1; i < s->open; i++) {
-               if (s->openlist[i].f < f) {
-                       hex = s->openlist[i].hex;
-                       f = s->openlist[i].f;
-               }
-               /* TODO check s->openlist[i].tiebreak */
-       }
-
-       return hex;
-}
-
-static int default_metric(int a, int b) {
-       return 1;
-}
-
-/*
- * find a path from start to end.  we use A*
- */
-int HL_findpath(int start, int end, int psize, int *path,
-               double (*metric)(int,int)) {
-       int neighbors[6];
-       int count;
-       int cur = 0;
-       struct astar state;
-       int neighbor;
-
-       if (start == end) return 0;
-       if (!metric) metric = default_metric;
-
-       astar_init(&state);
-       addopen(&state, start);
-
-       while (1) {
-               cur = lowrank(&state);
-               if (cur == end) {
-                       break;
-               }
-               addclosed(&state, cur);
-               count = HL_hexes_at_range(cur->hex, 1, neighbors, 6);
-               for (i=0;i<count;i++) {
-                       neighbor = neighbors[i];
-                       cost = g(&state, current) + metric(current, neighbor);
-                       if (inopen(&state, neighbor) && cost < g(&state, neighbor)) {
-                               removeopen(&state, neighbor);
-                       } else
-                       if (inclosed(&state, neighbor) && cost < g(&state, neighbor)) {
-                               /* should never happen with
-                                * admissible heuristic
-                                */
-                               removeclosed(&state, neighbor);
-                       } else
-                       if (!inopen(&state, neighbor) && !inclosed(&state, neighbor)) {
-                               addopen(&state, neighbor);
-                               setg(&state, neighbor, cost);
-                               setrank(&state, neighbor, cost + h(neighbor,end));
-                               setparent(&state, neighbor, current);
-                       } else {
-                               abort();
-                       }
-               }
-       }
-
-       count = 0;
-       while (parent(&state, cur) != start) {
-               count++;
-               cur = parent(&state, cur);
-       }
-       cur = end;
-       while (parent(&state, cur) != start) {
-               path[count--] = cur;
-               cur = parent(&state, cur);
-       }
-
-}
-
-#endif
-
 static int inversenatcantor(int cantor, int *x, int *y) {
        int w, t, py;
 
@@ -575,6 +418,8 @@ static int hex_xy(struct hex *h) {
        return 1;
 }
 
+#if 0
+
 static int hex_iso(struct hex *h) {
        if (h->iso) return 1;
 
@@ -590,6 +435,8 @@ static int hex_iso(struct hex *h) {
        return 1;
 }
 
+#endif
+
 int HL_hexbin(double width, double x, double y, int *i, int *j) {
        double z, rx, ry, rz;
        double abs_dx, abs_dy, abs_dz;