4 #include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <stdlib.h> /* free() */
6 #include <string.h> /* strchr() */
7 #include "../common/rexit.h" /* exit_err() */
8 #include "../common/try_malloc.h" /* try_malloc() */
9 #include "../common/yx_uint8.h" /* yx_uint8 */
10 #include "rrand.h" /* rrand() */
11 #include "world.h" /* global world */
15 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
16 static void mv_yx_in_dir(char d, struct yx_uint8 * yx);
18 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d", do wrapping
19 * logic, return 1 if "yx" ends outside of the original wrap space, else 0.
21 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx);
23 /* Call this too often with "init" of 0 and the game exits with an error message
24 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
25 * to 0. Iteration limit is currently 256 * UINT16_MAX.
27 static uint8_t iter_limit(uint8_t init);
29 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
30 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
32 /* Fill map with '~' cells. */
33 static void make_sea();
35 /* Put island of '.' cells inside map sea. */
36 static void make_sea();
38 /* Put tree cells of 'X' on island. */
39 static void make_trees();
43 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
47 yx->x = yx->x + (yx->y % 2);
56 yx->x = yx->x + (yx->y % 2);
61 yx->x = yx->x - !(yx->y % 2);
70 yx->x = yx->x - !(yx->y % 2);
77 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx)
79 static int8_t wrap_west_east = 0;
80 static int8_t wrap_north_south = 0;
83 wrap_west_east = wrap_north_south = 0;
86 struct yx_uint8 original;
90 if (strchr("edc", d) && yx->x < original.x)
94 else if (strchr("xsw", d) && yx->x > original.x)
98 if (strchr("we", d) && yx->y > original.y)
102 else if (strchr("xc", d) && yx->y < original.y)
106 return (wrap_west_east != 0) + (wrap_north_south != 0);
111 static uint8_t iter_limit(uint8_t init)
113 static uint32_t i = 0;
114 char * err = "Map generation reached iteration limit. Change map size?";
121 exit_err(256 * UINT16_MAX == i, err);
127 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
129 uint8_t ind = pos.y % 2;
130 uint8_t diag_west = pos.x + ind > 0;
131 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
132 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
133 if ( ( pos.y > 0 && diag_east
134 && type == world.map.cells[pos_i - world.map.length + ind])
135 || ( pos.x < world.map.length - 1
136 && type == world.map.cells[pos_i + 1])
137 || ( pos.y < world.map.length - 1 && diag_east
138 && type == world.map.cells[pos_i + world.map.length + ind])
139 || ( pos.y > 0 && diag_west
140 && type == world.map.cells[pos_i - world.map.length - !ind])
142 && type == world.map.cells[pos_i - 1])
143 || ( pos.y < world.map.length - 1 && diag_west
144 && type == world.map.cells[pos_i + world.map.length - !ind]))
153 static void make_sea()
156 for (y = 0; y < world.map.length; y++)
159 x < world.map.length;
160 world.map.cells[(y * world.map.length) + x] = '~', x++);
166 static void make_island()
169 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
170 uint32_t size = world.map.length * world.map.length;
171 world.map.cells[(size / 2) + add_half_width] = type;
174 while (iter_limit(0))
176 pos.y = rrand() % world.map.length;
177 pos.x = rrand() % world.map.length;
178 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
179 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
181 if ( pos.y == 0 || pos.y == world.map.length - 1
182 || pos.x == 0 || pos.x == world.map.length - 1)
186 world.map.cells[pos_i] = type;
193 static void make_trees()
197 uint16_t n_trees = (world.map.length * world.map.length) / 16;
198 uint16_t i_trees = 0;
200 while (i_trees <= n_trees && iter_limit(0))
202 uint8_t single_allowed = rrand() % 32;
203 pos.y = rrand() % world.map.length;
204 pos.x = rrand() % world.map.length;
205 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
206 if ('.' == world.map.cells[pos_i]
207 && (!single_allowed || is_neighbor(pos, type)))
209 world.map.cells[pos_i] = type;
217 extern void remake_map()
219 free(world.map.cells);
220 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
221 uint32_t store_seed = world.seed;
222 world.seed = world.seed_map;
226 world.seed = store_seed;
231 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
233 uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx);
234 if (yx && !wraptest && yx->x < world.map.length && yx->y < world.map.length)