1 #include <stddef.h> /* NULL */
2 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT8_MIN, INT8_MAX */
6 /* Coordinate for maps of max. 256x256 cells. */
13 /* Storage for map_length, set by set_maplength(). */
14 static uint16_t maplength = 0;
15 extern void set_maplength(uint16_t maplength_input)
17 maplength = maplength_input;
22 /* Pseudo-randomness seed for rrand(), set by seed_rrand(). */
23 static uint32_t seed = 0;
27 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
28 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
32 yx->x = yx->x + (yx->y % 2);
41 yx->x = yx->x + (yx->y % 2);
46 yx->x = yx->x - !(yx->y % 2);
55 yx->x = yx->x - !(yx->y % 2);
62 /* Move "yx" into hex direction "dir". Available hex directions are: 'e'
63 * (north-east), 'd' (east), 'c' (south-east), 'x' (south-west), 's' (west), 'w'
64 * (north-west). Returns 1 if the move was legal, 0 if not, and -1 when internal
65 * wrapping limits were exceeded.
67 * A move is legal if "yx" ends up within the the map and the original wrap
68 * space. The latter is left to a neighbor wrap space if "yx" moves beyond the
69 * minimal (0) or maximal (UINT8_MAX) column or row of possible map space – in
70 * which case "yx".y or "yx".x will snap to the respective opposite side. The
71 * current wrapping state is kept between successive calls until a "yx" of NULL
72 * is passed, in which case the function does nothing but zero the wrap state.
73 * Successive wrapping may move "yx" several wrap spaces into either direction,
74 * or return it into the original wrap space.
76 static int8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
78 static int8_t wrap_west_east = 0;
79 static int8_t wrap_north_south = 0;
82 wrap_west_east = wrap_north_south = 0;
85 if ( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
86 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south)
90 struct yx_uint8 original = *yx;
91 mv_yx_in_dir(dir, yx);
92 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
96 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
100 if (('w' == dir || 'e' == dir) && yx->y > original.y)
104 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
108 if ( !wrap_west_east && !wrap_north_south
109 && yx->x < maplength && yx->y < maplength)
118 /* Wrapper around mv_yx_in_dir_legal() that stores new coordinate in res_y/x,
119 * (return with result_y/x()), and immediately resets the wrapping.
121 static uint8_t res_y = 0;
122 static uint8_t res_x = 0;
123 extern uint8_t mv_yx_in_dir_legal_wrap(char dir, uint8_t y, uint8_t x)
128 uint8_t result = mv_yx_in_dir_legal(dir, &yx);
129 mv_yx_in_dir_legal(0, NULL);
134 extern uint8_t result_y()
138 extern uint8_t result_x()
145 /* With set_seed set, set seed global to seed_input. In any case, return it. */
146 extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input)
157 /* Return 16-bit number pseudo-randomly generated via Linear Congruential
158 * Generator algorithm with some proven constants. Use instead of any rand() to
159 * ensure portability of the same pseudo-randomness across systems.
161 extern uint16_t rrand()
162 { /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
163 seed = ((seed * 1103515245) + 12345) % 4294967296;
164 return (seed >> 16); /* Ignore less random least significant bits. */