3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
9 #include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, (U)INT*_(MIN|MAX) */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* memset() */
12 #include "../common/rexit.h" /* exit_err() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "../common/yx_uint8.h" /* yx_uint8 */
15 #include "rrand.h" /* rrand() */
16 #include "world.h" /* global world */
20 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
21 static void mv_yx_in_dir(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 iter_limit(uint8_t init)
79 static uint32_t i = 0;
80 char * err = "Map generation reached iteration limit. Change map size?";
87 exit_err(256 * UINT16_MAX == i, err);
93 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
95 uint8_t ind = pos.y % 2;
96 uint8_t diag_west = pos.x + ind > 0;
97 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
98 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
99 if ( ( pos.y > 0 && diag_east
100 && type == world.map.cells[pos_i - world.map.length + ind])
101 || ( pos.x < world.map.length - 1
102 && type == world.map.cells[pos_i + 1])
103 || ( pos.y < world.map.length - 1 && diag_east
104 && type == world.map.cells[pos_i + world.map.length + ind])
105 || ( pos.y > 0 && diag_west
106 && type == world.map.cells[pos_i - world.map.length - !ind])
108 && type == world.map.cells[pos_i - 1])
109 || ( pos.y < world.map.length - 1 && diag_west
110 && type == world.map.cells[pos_i + world.map.length - !ind]))
119 static void make_sea()
122 for (y = 0; y < world.map.length; y++)
125 x < world.map.length;
126 world.map.cells[(y * world.map.length) + x] = '~', x++);
132 static void make_island()
135 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
136 uint32_t size = world.map.length * world.map.length;
137 world.map.cells[(size / 2) + add_half_width] = type;
140 while (iter_limit(0))
142 pos.y = rrand() % world.map.length;
143 pos.x = rrand() % world.map.length;
144 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
145 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
147 if ( pos.y == 0 || pos.y == world.map.length - 1
148 || pos.x == 0 || pos.x == world.map.length - 1)
152 world.map.cells[pos_i] = type;
159 static void make_trees()
163 uint16_t n_trees = (world.map.length * world.map.length) / 16;
164 uint16_t i_trees = 0;
166 while (i_trees <= n_trees && iter_limit(0))
168 uint8_t single_allowed = rrand() % 32;
169 pos.y = rrand() % world.map.length;
170 pos.x = rrand() % world.map.length;
171 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
172 if ('.' == world.map.cells[pos_i]
173 && (!single_allowed || is_neighbor(pos, type)))
175 world.map.cells[pos_i] = type;
183 extern void remake_map()
185 free(world.map.cells);
186 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
187 uint32_t store_seed = world.seed;
188 world.seed = world.seed_map;
192 world.seed = store_seed;
197 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
199 static int8_t wrap_west_east = 0;
200 static int8_t wrap_north_south = 0;
203 wrap_west_east = wrap_north_south = 0;
206 char * err = "Too much wrapping in mv_yx_in_dir_legal().";
207 exit_err( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
208 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south,err);
209 struct yx_uint8 original = *yx;
210 mv_yx_in_dir(dir, yx);
211 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
215 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
219 if (('w' == dir || 'e' == dir) && yx->y > original.y)
223 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
227 if ( !wrap_west_east && !wrap_north_south
228 && yx->x < world.map.length && yx->y < world.map.length)
237 extern void init_empty_map(char ** map)
239 *map = try_malloc(world.map.length * world.map.length, __func__);
240 memset(*map, ' ', world.map.length * world.map.length);