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 "yx_uint8.h" /* mv_yx_in_dir_wrap() */
11 #include "world.h" /* global world */
15 /* Call this too often with "init" of 0 and the game exits with an error message
16 * about reaching an iteration limit. An "init" of 1 sets the iteration counter
17 * to 0. Iteration limit is currently 256 * UINT16_MAX.
19 static uint8_t iter_limit(uint8_t init);
21 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
22 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
24 /* Fill map with '~' cells. */
25 static void make_sea();
27 /* Put island of '.' cells inside map sea. */
28 static void make_sea();
30 /* Put tree cells of 'X' on island. */
31 static void make_trees();
35 static uint8_t iter_limit(uint8_t init)
37 static uint32_t i = 0;
38 char * err = "Map generation reached iteration limit. Change map size?";
45 exit_err(256 * UINT16_MAX == i, err);
51 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
53 uint8_t ind = pos.y % 2;
54 uint8_t diag_west = pos.x + ind > 0;
55 uint8_t diag_east = pos.x + ind <= world.map.length - 1;
56 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
57 if ( ( pos.y > 0 && diag_east
58 && type == world.map.cells[pos_i - world.map.length + ind])
59 || ( pos.x < world.map.length - 1
60 && type == world.map.cells[pos_i + 1])
61 || ( pos.y < world.map.length - 1 && diag_east
62 && type == world.map.cells[pos_i + world.map.length + ind])
63 || ( pos.y > 0 && diag_west
64 && type == world.map.cells[pos_i - world.map.length - !ind])
66 && type == world.map.cells[pos_i - 1])
67 || ( pos.y < world.map.length - 1 && diag_west
68 && type == world.map.cells[pos_i + world.map.length - !ind]))
77 static void make_sea()
80 for (y = 0; y < world.map.length; y++)
84 world.map.cells[(y * world.map.length) + x] = '~', x++);
90 static void make_island()
93 uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
94 uint32_t size = world.map.length * world.map.length;
95 world.map.cells[(size / 2) + add_half_width] = type;
100 pos.y = rrand() % world.map.length;
101 pos.x = rrand() % world.map.length;
102 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
103 if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
105 if ( pos.y == 0 || pos.y == world.map.length - 1
106 || pos.x == 0 || pos.x == world.map.length - 1)
110 world.map.cells[pos_i] = type;
117 static void make_trees()
121 uint16_t n_trees = (world.map.length * world.map.length) / 16;
122 uint16_t i_trees = 0;
124 while (i_trees <= n_trees && iter_limit(0))
126 uint8_t single_allowed = rrand() % 32;
127 pos.y = rrand() % world.map.length;
128 pos.x = rrand() % world.map.length;
129 uint16_t pos_i = (pos.y * world.map.length) + pos.x;
130 if ('.' == world.map.cells[pos_i]
131 && (!single_allowed || is_neighbor(pos, type)))
133 world.map.cells[pos_i] = type;
141 extern void remake_map()
143 free(world.map.cells);
144 world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
145 uint32_t store_seed = world.seed;
146 world.seed = world.seed_map;
150 world.seed = store_seed;
155 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
157 uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
158 if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)