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 /* Call this too often with "init" of 0 and the game exits with an error message
19 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
20 * to 0. Iteration limit is currently 256 * UINT16_MAX.
22 static uint8_t iter_limit(uint8_t init);
24 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
25 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
27 /* Fill map with '~' cells. */
28 static void make_sea();
30 /* Put island of '.' cells inside map sea. */
31 static void make_sea();
33 /* Put tree cells of 'X' on island. */
34 static void make_trees();
38 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
42 yx->x = yx->x + (yx->y % 2);
51 yx->x = yx->x + (yx->y % 2);
56 yx->x = yx->x - !(yx->y % 2);
65 yx->x = yx->x - !(yx->y % 2);
72 static uint8_t iter_limit(uint8_t init)
74 static uint32_t i = 0;
75 char * err = "Map generation reached iteration limit. Change map size?";
82 exit_err(256 * UINT16_MAX == i, err);
88 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
90 uint8_t ind = pos.y % 2;
91 uint8_t diag_west = pos.x + ind > 0;
92 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
93 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
94 if ( ( pos.y > 0 && diag_east
95 && type == world.map.cells[pos_i - world.map.length + ind])
96 || ( pos.x < world.map.length - 1
97 && type == world.map.cells[pos_i + 1])
98 || ( pos.y < world.map.length - 1 && diag_east
99 && type == world.map.cells[pos_i + world.map.length + ind])
100 || ( pos.y > 0 && diag_west
101 && type == world.map.cells[pos_i - world.map.length - !ind])
103 && type == world.map.cells[pos_i - 1])
104 || ( pos.y < world.map.length - 1 && diag_west
105 && type == world.map.cells[pos_i + world.map.length - !ind]))
114 static void make_sea()
117 for (y = 0; y < world.map.length; y++)
120 x < world.map.length;
121 world.map.cells[(y * world.map.length) + x] = '~', x++);
127 static void make_island()
130 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
131 uint32_t size = world.map.length * world.map.length;
132 world.map.cells[(size / 2) + add_half_width] = type;
135 while (iter_limit(0))
137 pos.y = rrand() % world.map.length;
138 pos.x = rrand() % world.map.length;
139 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
140 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
142 if ( pos.y == 0 || pos.y == world.map.length - 1
143 || pos.x == 0 || pos.x == world.map.length - 1)
147 world.map.cells[pos_i] = type;
154 static void make_trees()
158 uint16_t n_trees = (world.map.length * world.map.length) / 16;
159 uint16_t i_trees = 0;
161 while (i_trees <= n_trees && iter_limit(0))
163 uint8_t single_allowed = rrand() % 32;
164 pos.y = rrand() % world.map.length;
165 pos.x = rrand() % world.map.length;
166 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
167 if ('.' == world.map.cells[pos_i]
168 && (!single_allowed || is_neighbor(pos, type)))
170 world.map.cells[pos_i] = type;
178 extern void remake_map()
180 free(world.map.cells);
181 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
182 uint32_t store_seed = world.seed;
183 world.seed = world.seed_map;
187 world.seed = store_seed;
192 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
194 static int8_t wrap_west_east = 0;
195 static int8_t wrap_north_south = 0;
198 wrap_west_east = wrap_north_south = 0;
201 char * err = "Too much wrapping in mv_yx_in_dir_legal().";
202 exit_err( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
203 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south, err);
204 struct yx_uint8 original;
207 mv_yx_in_dir(dir, yx);
208 if (strchr("edc", dir) && yx->x < original.x)
212 else if (strchr("xsw", dir) && yx->x > original.x)
216 if (strchr("we", dir) && yx->y > original.y)
220 else if (strchr("xc", dir) && yx->y < original.y)
224 if ( !((wrap_west_east != 0) + (wrap_north_south != 0))
225 && yx->x < world.map.length && yx->y < world.map.length)