4 #include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, (U)INT*_(MIN|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;
81 char * err = "Too much wrapping in mv_yx_in_dir_wrap().";
82 exit_err(INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south, err);
83 exit_err(INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south, err);
86 wrap_west_east = wrap_north_south = 0;
89 struct yx_uint8 original;
93 if (strchr("edc", d) && yx->x < original.x)
97 else if (strchr("xsw", d) && yx->x > original.x)
101 if (strchr("we", d) && yx->y > original.y)
105 else if (strchr("xc", d) && yx->y < original.y)
109 return (wrap_west_east != 0) + (wrap_north_south != 0);
114 static uint8_t iter_limit(uint8_t init)
116 static uint32_t i = 0;
117 char * err = "Map generation reached iteration limit. Change map size?";
124 exit_err(256 * UINT16_MAX == i, err);
130 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
132 uint8_t ind = pos.y % 2;
133 uint8_t diag_west = pos.x + ind > 0;
134 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
135 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
136 if ( ( pos.y > 0 && diag_east
137 && type == world.map.cells[pos_i - world.map.length + ind])
138 || ( pos.x < world.map.length - 1
139 && type == world.map.cells[pos_i + 1])
140 || ( pos.y < world.map.length - 1 && diag_east
141 && type == world.map.cells[pos_i + world.map.length + ind])
142 || ( pos.y > 0 && diag_west
143 && type == world.map.cells[pos_i - world.map.length - !ind])
145 && type == world.map.cells[pos_i - 1])
146 || ( pos.y < world.map.length - 1 && diag_west
147 && type == world.map.cells[pos_i + world.map.length - !ind]))
156 static void make_sea()
159 for (y = 0; y < world.map.length; y++)
162 x < world.map.length;
163 world.map.cells[(y * world.map.length) + x] = '~', x++);
169 static void make_island()
172 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
173 uint32_t size = world.map.length * world.map.length;
174 world.map.cells[(size / 2) + add_half_width] = type;
177 while (iter_limit(0))
179 pos.y = rrand() % world.map.length;
180 pos.x = rrand() % world.map.length;
181 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
182 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
184 if ( pos.y == 0 || pos.y == world.map.length - 1
185 || pos.x == 0 || pos.x == world.map.length - 1)
189 world.map.cells[pos_i] = type;
196 static void make_trees()
200 uint16_t n_trees = (world.map.length * world.map.length) / 16;
201 uint16_t i_trees = 0;
203 while (i_trees <= n_trees && iter_limit(0))
205 uint8_t single_allowed = rrand() % 32;
206 pos.y = rrand() % world.map.length;
207 pos.x = rrand() % world.map.length;
208 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
209 if ('.' == world.map.cells[pos_i]
210 && (!single_allowed || is_neighbor(pos, type)))
212 world.map.cells[pos_i] = type;
220 extern void remake_map()
222 free(world.map.cells);
223 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
224 uint32_t store_seed = world.seed;
225 world.seed = world.seed_map;
229 world.seed = store_seed;
234 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
236 uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx);
237 if (yx && !wraptest && yx->x < world.map.length && yx->y < world.map.length)