1 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT8_MIN, INT8_MAX */
5 /* Coordinate for maps of max. 256x256 cells. */
12 /* Storage for map_length, set by set_maplength(). */
13 static uint16_t maplength = 0;
14 extern void set_maplength(uint16_t maplength_input)
16 maplength = maplength_input;
21 /* Pseudo-randomness seed for rrand(), set by seed_rrand(). */
22 static uint32_t seed = 0;
26 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
27 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
31 yx->x = yx->x + (yx->y % 2);
40 yx->x = yx->x + (yx->y % 2);
45 yx->x = yx->x - !(yx->y % 2);
54 yx->x = yx->x - !(yx->y % 2);
61 /* Move "yx" into hex direction "dir". Available hex directions are: 'e'
62 * (north-east), 'd' (east), 'c' (south-east), 'x' (south-west), 's' (west), 'w'
63 * (north-west). Returns 1 if the move was legal, 0 if not, and -1 when internal
64 * wrapping limits were exceeded.
66 * A move is legal if "yx" ends up within the the map and the original wrap
67 * space. The latter is left to a neighbor wrap space if "yx" moves beyond the
68 * minimal (0) or maximal (UINT8_MAX) column or row of possible map space – in
69 * which case "yx".y or "yx".x will snap to the respective opposite side. The
70 * current wrapping state is kept between successive calls until a "yx" of NULL
71 * is passed, in which case the function does nothing but zero the wrap state.
72 * Successive wrapping may move "yx" several wrap spaces into either direction,
73 * or return it into the original wrap space.
75 static int8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
77 static int8_t wrap_west_east = 0;
78 static int8_t wrap_north_south = 0;
81 wrap_west_east = wrap_north_south = 0;
84 if ( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
85 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south)
89 struct yx_uint8 original = *yx;
90 mv_yx_in_dir(dir, yx);
91 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
95 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
99 if (('w' == dir || 'e' == dir) && yx->y > original.y)
103 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
107 if ( !wrap_west_east && !wrap_north_south
108 && yx->x < maplength && yx->y < maplength)
117 /* Wrapper around mv_yx_in_dir_legal() that stores the new coordinate in the
118 * globals res_y, res_x.
122 extern uint8_t mv_yx_in_dir_legal_wrap(char dir, uint8_t y, uint8_t x)
127 uint8_t result = mv_yx_in_dir_legal(dir, &yx);
135 /* With set_seed set, set seed global to seed_input. In any case, return it. */
136 extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input)
147 /* Return 16-bit number pseudo-randomly generated via Linear Congruential
148 * Generator algorithm with some proven constants. Use instead of any rand() to
149 * ensure portability of the same pseudo-randomness across systems.
151 extern uint16_t rrand()
152 { /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
153 seed = ((seed * 1103515245) + 12345) % 4294967296;
154 return (seed >> 16); /* Ignore less random least significant bits. */