#ifndef HEXLIB_H_ #define HEXLIB_H_ 1 struct HL_hex { int x, y; }; struct HL_isohex { int i,j,k; }; struct HL_point { double x, y; }; struct HL_hex HL_hex_xy(int x, int y); int HL_hex_cmp(struct HL_hex const *a, struct HL_hex const *b); int HL_hex_eq(struct HL_hex const *a, struct HL_hex const *b); /* * this library implements a generic library for hexgrids for games. * it avoids display issues, other than some general provisions * for data * * This file is written by Nathan Wagner and dedicated to the * public domain. */ /* max size for cantor numbers is 16383, or 32767 if non-negative, * assuming a 32 bit int */ /* map from cartesian coordinates to hex coords */ /* cartesian coordinates are positive Y up, hex coords * are positive Y down. You may need to pass in the * negative of your Y coordinate * * width is the distance from one hexside to the opposite * hexside. i and j are the cartesian coordinates of the hex * the x,y point falls in. If a point happens to fall exactly * on a hex boundary, the result will depend on how the machine * does the floating point math, but should be consistent. */ struct HL_hex HL_hexbin(double width, double x, double y); struct HL_hex HL_bin(double x, double y); /* generate the cantor mapping of a hexes x,y coordinate. * See http://en.wikipedia.org/wiki/Cantor_pairing_function. * We modify the basic formula to support negative coordinates * * The cantor mapping is *not* a contiguous mapping, as it maps * onto a triangle, so you can't use the cantor mapping as * an index into an array. Or, you can, but the array * will be (I think) twice as large as it needs to be * * See also http://mathworld.wolfram.com/PairingFunction.html * for a reference to a square mapping by Stein that doesn't * have this problem, at least for square maps. * * We use the cantor mapping so that the generated integer * is independent of the size of the grid. */ /* Cantor encoding/decoding functions */ int HL_cantor(struct HL_hex h); int HL_cantor_xy(int x, int y); struct HL_hex HL_uncantor(int cantor); /* * layout assistance functions * * HL_center_x(int cantor) returns the cartesian X coordinate * of a given hex, assuming a width of 1.0. Scaling can * be done by the caller. * * Similarly for HL_center_y() and HL_hexcenter() */ struct HL_point HL_hexcenter(struct HL_hex hex); /* find a point based on a unit hex, given angle and distance from center, * where a distance of 1 is on the hex side. probably not useful * for points outside the hex, that is, distance should almost certainly * be less than or equal to 1.0 */ struct HL_point HL_polar(double angle, double distance, double *d); /* how far is it on the hex grid */ int HL_distance(struct HL_hex from, struct HL_hex to); /* module direction */ struct HL_hex HL_adjhex(struct HL_hex, int dir); int HL_adjhexp(struct HL_hex, int dir, struct HL_hex *adj); int HL_hexes_at_range(struct HL_hex, int range, struct HL_hex *list, int size); int HL_hexes_within_range(struct HL_hex, int range, struct HL_hex *list, int size); typedef double (*HL_costfunc)(int start, int finish); /* writes up to len cantors to path. returns last * value written * perhaps it should return the last value not written, if the * path is incomplete? * either way, it can then be used in a loop */ int HL_path(int start, int end, int *path, int len); int HL_map_bounds_ok(int xdim, int ydim); int HL_map_max_dimension(void); /* * Plotting support */ void HL_trianglefan(struct HL_hex hex, double *); /* * Pathfinding */ struct HL_astar_hex { struct HL_hex hex; double f, g, h; struct HL_astar_hex *parent; int open; struct HL_astar_hex *next; struct HL_astar_hex *prev; }; struct HL_astar { struct HL_hex start, goal; int pathlen; double (*metric)(struct HL_hex,struct HL_hex); double (*heuristic)(struct HL_hex,struct HL_hex); int (*neighbor)(struct HL_hex,int dir, struct HL_hex *n); int nodes, allocated; struct HL_astar_hex *open; struct HL_astar_hex *closed; struct HL_astar_hex *from, *to; int known, index; int error, malloced; }; int HL_findpath(struct HL_astar *s, int loops); void HL_astar_free(struct HL_astar *s); struct HL_astar *HL_astar_clear(struct HL_astar *s); struct HL_astar *HL_astar_init(struct HL_astar *s); void HL_vertices(struct HL_hex hex, double *vc); extern double HL_vertexv[12]; extern double HL_fand[16]; extern float HL_fanf[16]; extern double HL_hfand[16]; /* horizontal grids */ extern float HL_hfanf[16]; /* horizontal grids */ extern double HL_square; #endif