5 * this library implements a generic library for hexgrids for games.
6 * it avoids display issues, other than some general provisions
9 * This file is written by Nathan Wagner and dedicated to the
13 /* max size for cantor numbers is 16383, or 32767 if non-negative,
14 * assuming a 32 bit int
17 /* map from cartesian coordinates to hex coords */
18 /* cartesian coordinates are positive Y up, hex coords
19 * are positive Y down. You may need to pass in the
20 * negative of your Y coordinate
22 * width is the distance from one hexside to the opposite
23 * hexside. i and j are the cartesian coordinates of the hex
24 * the x,y point falls in. If a point happens to fall exactly
25 * on a hex boundary, the result will depend on how the machine
26 * does the floating point math, but should be consistent.
28 int HL_hexbin(double width, double x, double y, int *i, int *j);
30 /* generate the cantor mapping of a hexes x,y coordinate.
31 * See http://en.wikipedia.org/wiki/Cantor_pairing_function.
32 * We modify the basic formula to support negative coordinates
34 * The cantor mapping is *not* a contiguous mapping, as it maps
35 * onto a triangle, so you can't use the cantor mapping as
36 * an index into an array. Or, you can, but the array
37 * will be (I think) twice as large as it needs to be
39 * See also http://mathworld.wolfram.com/PairingFunction.html
40 * for a reference to a square mapping by Stein that doesn't
41 * have this problem, at least for square maps.
43 * We use the cantor mapping so that the generated integer
44 * is independent of the size of the grid.
47 /* Cartesian functions */
48 int HL_cantor_xy(int x, int y);
49 int HL_cantor_xyp(int *xy);
50 int HL_cantor_x(int cantor);
51 int HL_cantor_y(int cantor);
53 /* Isometric functions */
54 int HL_cantor_ijk(int i, int j, int k);
55 int HL_cantor_ijkp(int *ijk);
56 int HL_cantor_i(int cantor);
57 int HL_cantor_k(int cantor);
58 int HL_cantor_j(int cantor);
60 int HL_cantor_bin(double x, double y);
62 /* A general decode function */
63 int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k);
64 int HL_cantor_arrays(int can, int *xy, int *ijk);
67 * layout assistance functions
69 * HL_center_x(int cantor) returns the cartesian X coordinate
70 * of a given hex, assuming a width of 1.0. Scaling can
71 * be done by the caller.
73 * Similarly for HL_center_y() and HL_hexcenter()
75 double HL_center_x(int cantor);
76 double HL_center_y(int cantor);
77 int HL_hexcenter(int cantor, double *x, double *y);
79 /* how far is it on the hex grid */
80 int HL_distance(int from_cantor, int to_cantor);
82 /* returns 0 for <0 or >5 */
83 int HL_adjacent_hex(int start, int dir);
85 /* module direction */
86 int HL_adjhex(int start, int dir);
87 int HL_hexes_at_range(int hex, int range, int *list, int size);
88 int HL_hexes_within_range(int hex, int range, int *list, int size);
90 typedef double (*HL_costfunc)(int start, int finish);
92 /* writes up to len cantors to path. returns last
94 * perhaps it should return the last value not written, if the
96 * either way, it can then be used in a loop
98 int HL_path(int start, int end, int *path, int len);
100 int HL_map_bounds_ok(int xdim, int ydim);
101 int HL_map_max_dimension(void);
106 void HL_trianglefan(int cantor, double *);
109 int x,y; /* hmm. would be nice to be able to just point to a generic */
116 struct HL_astar_hex {
119 struct HL_astar_hex *parent;
121 struct HL_astar_hex *next;
122 struct HL_astar_hex *prev;
128 double (*metric)(int,int);
129 double (*heuristic)(int,int);
130 int (*neighbor)(int,int);
131 int nodes, allocated;
132 struct HL_astar_hex *sets;
133 struct HL_astar_hex *open;
134 struct HL_astar_hex *closed;
135 struct HL_astar_hex *from, *to;
140 int HL_findpath(struct HL_astar *s, int loops);
141 void HL_astar_free(struct HL_astar *s);
142 struct HL_astar *HL_astar_clear(struct HL_astar *s);
143 struct HL_astar *HL_astar_init(struct HL_astar *s);
144 void HL_vertices(int cantor, double *vc);
146 extern double HL_fand[16];
147 extern float HL_fanf[16];
148 extern double HL_hfand[16]; /* horizontal grids */
149 extern float HL_hfanf[16]; /* horizontal grids */
150 extern double HL_square;