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 "../common/rexit.h" /* exit_err() */
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "../common/yx_uint8.h" /* yx_uint8 */
14 #include "rrand.h" /* rrand() */
15 #include "world.h" /* global world */
19 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
20 static void mv_yx_in_dir(char d, struct yx_uint8 * yx);
22 /* Call this too often with "init" of 0 and the game exits with an error message
23 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
24 * to 0. Iteration limit is currently 256 * UINT16_MAX.
26 static uint8_t iter_limit(uint8_t init);
28 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
29 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
31 /* Fill map with '~' cells. */
32 static void make_sea();
34 /* Put island of '.' cells inside map sea. */
35 static void make_sea();
37 /* Put tree cells of 'X' on island. */
38 static void make_trees();
42 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
46 yx->x = yx->x + (yx->y % 2);
55 yx->x = yx->x + (yx->y % 2);
60 yx->x = yx->x - !(yx->y % 2);
69 yx->x = yx->x - !(yx->y % 2);
76 static uint8_t iter_limit(uint8_t init)
78 static uint32_t i = 0;
79 char * err = "Map generation reached iteration limit. Change map size?";
86 exit_err(256 * UINT16_MAX == i, err);
92 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
94 uint8_t ind = pos.y % 2;
95 uint8_t diag_west = pos.x + ind > 0;
96 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
97 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
98 if ( ( pos.y > 0 && diag_east
99 && type == world.map.cells[pos_i - world.map.length + ind])
100 || ( pos.x < world.map.length - 1
101 && type == world.map.cells[pos_i + 1])
102 || ( pos.y < world.map.length - 1 && diag_east
103 && type == world.map.cells[pos_i + world.map.length + ind])
104 || ( pos.y > 0 && diag_west
105 && type == world.map.cells[pos_i - world.map.length - !ind])
107 && type == world.map.cells[pos_i - 1])
108 || ( pos.y < world.map.length - 1 && diag_west
109 && type == world.map.cells[pos_i + world.map.length - !ind]))
118 static void make_sea()
121 for (y = 0; y < world.map.length; y++)
124 x < world.map.length;
125 world.map.cells[(y * world.map.length) + x] = '~', x++);
131 static void make_island()
134 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
135 uint32_t size = world.map.length * world.map.length;
136 world.map.cells[(size / 2) + add_half_width] = type;
139 while (iter_limit(0))
141 pos.y = rrand() % world.map.length;
142 pos.x = rrand() % world.map.length;
143 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
144 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
146 if ( pos.y == 0 || pos.y == world.map.length - 1
147 || pos.x == 0 || pos.x == world.map.length - 1)
151 world.map.cells[pos_i] = type;
158 static void make_trees()
162 uint16_t n_trees = (world.map.length * world.map.length) / 16;
163 uint16_t i_trees = 0;
165 while (i_trees <= n_trees && iter_limit(0))
167 uint8_t single_allowed = rrand() % 32;
168 pos.y = rrand() % world.map.length;
169 pos.x = rrand() % world.map.length;
170 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
171 if ('.' == world.map.cells[pos_i]
172 && (!single_allowed || is_neighbor(pos, type)))
174 world.map.cells[pos_i] = type;
182 extern void remake_map()
184 free(world.map.cells);
185 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
186 uint32_t store_seed = world.seed;
187 world.seed = world.seed_map;
191 world.seed = store_seed;
196 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
198 static int8_t wrap_west_east = 0;
199 static int8_t wrap_north_south = 0;
202 wrap_west_east = wrap_north_south = 0;
205 char * err = "Too much wrapping in mv_yx_in_dir_legal().";
206 exit_err( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
207 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south,err);
208 struct yx_uint8 original = *yx;
209 mv_yx_in_dir(dir, yx);
210 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
214 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
218 if (('w' == dir || 'e' == dir) && yx->y > original.y)
222 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
226 if ( !wrap_west_east && !wrap_north_south
227 && yx->x < world.map.length && yx->y < world.map.length)