4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include "../common/rexit.h" /* exit_err() */
6 #include "../common/try_malloc.h" /* try_malloc() */
7 #include "../common/yx_uint8.h" /* struct yx_uint8 */
8 #include "rrand.h" /* rrand() */
9 #include "world.h" /* global world */
13 /* Call this too often with "init" of 0 and the game exits with an error message
14 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
15 * to 0. Iteration limit is currently 256 * UINT16_MAX.
17 static uint8_t iter_limit(uint8_t init);
19 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
20 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
22 /* Fill map with '~' cells. */
23 static void make_sea();
25 /* Put island of '.' cells inside map sea. */
26 static void make_sea();
28 /* Put tree cells of 'X' on island. */
29 static void make_trees();
33 static uint8_t iter_limit(uint8_t init)
35 static uint32_t i = 0;
36 char * err = "Map generation reached iteration limit. Change map size?";
43 exit_err(256 * UINT16_MAX == i, err);
49 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
51 uint8_t ind = pos.y % 2;
52 uint8_t diag_west = pos.x + ind > 0;
53 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
54 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
55 if ( ( pos.y > 0 && diag_east
56 && type == world.map.cells[pos_i - world.map.length + ind])
57 || ( pos.x < world.map.length - 1
58 && type == world.map.cells[pos_i + 1])
59 || ( pos.y < world.map.length - 1 && diag_east
60 && type == world.map.cells[pos_i + world.map.length + ind])
61 || ( pos.y > 0 && diag_west
62 && type == world.map.cells[pos_i - world.map.length - !ind])
64 && type == world.map.cells[pos_i - 1])
65 || ( pos.y < world.map.length - 1 && diag_west
66 && type == world.map.cells[pos_i + world.map.length - !ind]))
75 static void make_sea()
78 for (y = 0; y < world.map.length; y++)
82 world.map.cells[(y * world.map.length) + x] = '~', x++);
88 static void make_island()
91 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
92 uint32_t size = world.map.length * world.map.length;
93 world.map.cells[(size / 2) + add_half_width] = type;
98 pos.y = rrand() % world.map.length;
99 pos.x = rrand() % world.map.length;
100 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
101 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
103 if ( pos.y == 0 || pos.y == world.map.length - 1
104 || pos.x == 0 || pos.x == world.map.length - 1)
108 world.map.cells[pos_i] = type;
115 static void make_trees()
119 uint16_t n_trees = (world.map.length * world.map.length) / 16;
120 uint16_t i_trees = 0;
122 while (i_trees <= n_trees && iter_limit(0))
124 uint8_t single_allowed = rrand() % 32;
125 pos.y = rrand() % world.map.length;
126 pos.x = rrand() % world.map.length;
127 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
128 if ('.' == world.map.cells[pos_i]
129 && (!single_allowed || is_neighbor(pos, type)))
131 world.map.cells[pos_i] = type;
139 extern void init_map()
141 char * f_name = "init_map()";
142 world.map.cells = try_malloc(world.map.length * world.map.length, f_name);
150 extern uint8_t is_passable(struct yx_uint8 pos)
152 uint8_t passable = 0;
153 if (pos.x < world.map.length && pos.y < world.map.length)
155 passable = ('.' == world.map.cells[(pos.y * world.map.length) + pos.x]);
162 extern uint16_t yx_to_map_pos(struct yx_uint8 * yx)
164 return (yx->y * world.map.length) + yx->x;