4 #include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, (U)INT*_(MIN|MAX) */
5 #include <stdlib.h> /* free() */
6 #include "../common/rexit.h" /* exit_err() */
7 #include "../common/try_malloc.h" /* try_malloc() */
8 #include "../common/yx_uint8.h" /* yx_uint8 */
9 #include "rrand.h" /* rrand() */
10 #include "world.h" /* global world */
14 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
15 static void mv_yx_in_dir(char d, struct yx_uint8 * yx);
17 /* Call this too often with "init" of 0 and the game exits with an error message
18 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
19 * to 0. Iteration limit is currently 256 * UINT16_MAX.
21 static uint8_t iter_limit(uint8_t init);
23 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
24 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
26 /* Fill map with '~' cells. */
27 static void make_sea();
29 /* Put island of '.' cells inside map sea. */
30 static void make_sea();
32 /* Put tree cells of 'X' on island. */
33 static void make_trees();
37 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
41 yx->x = yx->x + (yx->y % 2);
50 yx->x = yx->x + (yx->y % 2);
55 yx->x = yx->x - !(yx->y % 2);
64 yx->x = yx->x - !(yx->y % 2);
71 static uint8_t iter_limit(uint8_t init)
73 static uint32_t i = 0;
74 char * err = "Map generation reached iteration limit. Change map size?";
81 exit_err(256 * UINT16_MAX == i, err);
87 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
89 uint8_t ind = pos.y % 2;
90 uint8_t diag_west = pos.x + ind > 0;
91 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
92 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
93 if ( ( pos.y > 0 && diag_east
94 && type == world.map.cells[pos_i - world.map.length + ind])
95 || ( pos.x < world.map.length - 1
96 && type == world.map.cells[pos_i + 1])
97 || ( pos.y < world.map.length - 1 && diag_east
98 && type == world.map.cells[pos_i + world.map.length + ind])
99 || ( pos.y > 0 && diag_west
100 && type == world.map.cells[pos_i - world.map.length - !ind])
102 && type == world.map.cells[pos_i - 1])
103 || ( pos.y < world.map.length - 1 && diag_west
104 && type == world.map.cells[pos_i + world.map.length - !ind]))
113 static void make_sea()
116 for (y = 0; y < world.map.length; y++)
119 x < world.map.length;
120 world.map.cells[(y * world.map.length) + x] = '~', x++);
126 static void make_island()
129 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
130 uint32_t size = world.map.length * world.map.length;
131 world.map.cells[(size / 2) + add_half_width] = type;
134 while (iter_limit(0))
136 pos.y = rrand() % world.map.length;
137 pos.x = rrand() % world.map.length;
138 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
139 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
141 if ( pos.y == 0 || pos.y == world.map.length - 1
142 || pos.x == 0 || pos.x == world.map.length - 1)
146 world.map.cells[pos_i] = type;
153 static void make_trees()
157 uint16_t n_trees = (world.map.length * world.map.length) / 16;
158 uint16_t i_trees = 0;
160 while (i_trees <= n_trees && iter_limit(0))
162 uint8_t single_allowed = rrand() % 32;
163 pos.y = rrand() % world.map.length;
164 pos.x = rrand() % world.map.length;
165 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
166 if ('.' == world.map.cells[pos_i]
167 && (!single_allowed || is_neighbor(pos, type)))
169 world.map.cells[pos_i] = type;
177 extern void remake_map()
179 free(world.map.cells);
180 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
181 uint32_t store_seed = world.seed;
182 world.seed = world.seed_map;
186 world.seed = store_seed;
191 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
193 static int8_t wrap_west_east = 0;
194 static int8_t wrap_north_south = 0;
197 wrap_west_east = wrap_north_south = 0;
200 char * err = "Too much wrapping in mv_yx_in_dir_legal().";
201 exit_err( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
202 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south, err);
203 struct yx_uint8 original;
206 mv_yx_in_dir(dir, yx);
207 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
211 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
215 if (('w' == dir || 'e' == dir) && yx->y > original.y)
219 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
223 if ( !wrap_west_east && !wrap_north_south
224 && yx->x < world.map.length && yx->y < world.map.length)