4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_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" /* struct yx_uint8 */
9 #include "rrand.h" /* rrand() */
10 #include "world.h" /* global world */
14 /* Call this too often with "init" of 0 and the game exits with an error message
15 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
16 * to 0. Iteration limit is currently 256 * UINT16_MAX.
18 static uint8_t iter_limit(uint8_t init);
20 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
21 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
23 /* Fill map with '~' cells. */
24 static void make_sea();
26 /* Put island of '.' cells inside map sea. */
27 static void make_sea();
29 /* Put tree cells of 'X' on island. */
30 static void make_trees();
34 static uint8_t iter_limit(uint8_t init)
36 static uint32_t i = 0;
37 char * err = "Map generation reached iteration limit. Change map size?";
44 exit_err(256 * UINT16_MAX == i, err);
50 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
52 uint8_t ind = pos.y % 2;
53 uint8_t diag_west = pos.x + ind > 0;
54 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
55 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
56 if ( ( pos.y > 0 && diag_east
57 && type == world.map.cells[pos_i - world.map.length + ind])
58 || ( pos.x < world.map.length - 1
59 && type == world.map.cells[pos_i + 1])
60 || ( pos.y < world.map.length - 1 && diag_east
61 && type == world.map.cells[pos_i + world.map.length + ind])
62 || ( pos.y > 0 && diag_west
63 && type == world.map.cells[pos_i - world.map.length - !ind])
65 && type == world.map.cells[pos_i - 1])
66 || ( pos.y < world.map.length - 1 && diag_west
67 && type == world.map.cells[pos_i + world.map.length - !ind]))
76 static void make_sea()
79 for (y = 0; y < world.map.length; y++)
83 world.map.cells[(y * world.map.length) + x] = '~', x++);
89 static void make_island()
92 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
93 uint32_t size = world.map.length * world.map.length;
94 world.map.cells[(size / 2) + add_half_width] = type;
99 pos.y = rrand() % world.map.length;
100 pos.x = rrand() % world.map.length;
101 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
102 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
104 if ( pos.y == 0 || pos.y == world.map.length - 1
105 || pos.x == 0 || pos.x == world.map.length - 1)
109 world.map.cells[pos_i] = type;
116 static void make_trees()
120 uint16_t n_trees = (world.map.length * world.map.length) / 16;
121 uint16_t i_trees = 0;
123 while (i_trees <= n_trees && iter_limit(0))
125 uint8_t single_allowed = rrand() % 32;
126 pos.y = rrand() % world.map.length;
127 pos.x = rrand() % world.map.length;
128 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
129 if ('.' == world.map.cells[pos_i]
130 && (!single_allowed || is_neighbor(pos, type)))
132 world.map.cells[pos_i] = type;
140 extern void remake_map()
142 free(world.map.cells);
143 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
144 uint32_t store_seed = world.seed;
145 world.seed = world.seed_map;
149 world.seed = store_seed;
154 extern uint8_t is_passable(struct yx_uint8 pos)
156 uint8_t passable = 0;
157 if (pos.x < world.map.length && pos.y < world.map.length)
159 passable = ('.' == world.map.cells[(pos.y * world.map.length) + pos.x]);