home · contact · privacy
30d64a5ddea02da915d6bd733450f60f166e73b9
[plomrogue] / src / server / map.c
1 /* src/server/map.c */
2
3 #include "map.h"
4 #include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, (U)INT*_(MIN|MAX) */
5 #include <stdlib.h> /* free() */
6 #include <string.h> /* strchr() */
7 #include "../common/rexit.h" /* exit_err() */
8 #include "../common/try_malloc.h" /* try_malloc() */
9 #include "../common/yx_uint8.h" /* yx_uint8 */
10 #include "rrand.h" /* rrand() */
11 #include "world.h" /* global world */
12
13
14
15 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
16 static void mv_yx_in_dir(char d, struct yx_uint8 * yx);
17
18 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d", do wrapping
19  * logic, return 1 if "yx" ends outside of the original wrap space, else 0.
20  */
21 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx);
22
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.
26  */
27 static uint8_t iter_limit(uint8_t init);
28
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);
31
32 /* Fill map with '~' cells. */
33 static void make_sea();
34
35 /* Put island of '.' cells inside map sea. */
36 static void make_sea();
37
38 /* Put tree cells of 'X' on island. */
39 static void make_trees();
40
41
42
43 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
44 {
45     if      (d == 'e')
46     {
47         yx->x = yx->x + (yx->y % 2);
48         yx->y--;
49     }
50     else if (d == 'd')
51     {
52         yx->x++;
53     }
54     else if (d == 'c')
55     {
56         yx->x = yx->x + (yx->y % 2);
57         yx->y++;
58     }
59     else if (d == 'x')
60     {
61         yx->x = yx->x - !(yx->y % 2);
62         yx->y++;
63     }
64     else if (d == 's')
65     {
66         yx->x--;
67     }
68     else if (d == 'w')
69     {
70         yx->x = yx->x - !(yx->y % 2);
71         yx->y--;
72     }
73 }
74
75
76
77 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx)
78 {
79     static int8_t wrap_west_east   = 0;
80     static int8_t wrap_north_south = 0;
81     char * err = "Too much wrapping in mv_yx_in_dir_wrap().";
82     exit_err(   INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
83              || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south, err);
84     if (!yx)
85     {
86         wrap_west_east = wrap_north_south = 0;
87         return 0;
88     }
89     struct yx_uint8 original;
90     original.y = yx->y;
91     original.x = yx->x;
92     mv_yx_in_dir(d, yx);
93     if      (strchr("edc", d) && yx->x < original.x)
94     {
95         wrap_west_east++;
96     }
97     else if (strchr("xsw", d) && yx->x > original.x)
98     {
99         wrap_west_east--;
100     }
101     if      (strchr("we", d) && yx->y > original.y)
102     {
103         wrap_north_south--;
104     }
105     else if (strchr("xc", d) && yx->y < original.y)
106     {
107         wrap_north_south++;
108     }
109     return (wrap_west_east != 0) + (wrap_north_south != 0);
110 }
111
112
113
114 static uint8_t iter_limit(uint8_t init)
115 {
116     static uint32_t i = 0;
117     char * err = "Map generation reached iteration limit. Change map size?";
118     if (init)
119     {
120         i = 0;
121         return 0;
122     }
123     i++;
124     exit_err(256 * UINT16_MAX == i, err);
125     return 1;
126 }
127
128
129
130 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
131 {
132     uint8_t ind = pos.y % 2;
133     uint8_t diag_west = pos.x + ind > 0;
134     uint8_t diag_east = pos.x + ind <= world.map.length - 1;
135     uint16_t pos_i = (pos.y * world.map.length) + pos.x;
136     if (   (   pos.y > 0                    && diag_east
137             && type == world.map.cells[pos_i - world.map.length + ind])
138         || (   pos.x < world.map.length - 1
139             && type == world.map.cells[pos_i + 1])
140         || (   pos.y < world.map.length - 1 && diag_east
141             && type == world.map.cells[pos_i + world.map.length + ind])
142         || (   pos.y > 0                    && diag_west
143             && type == world.map.cells[pos_i - world.map.length - !ind])
144         || (   pos.x > 0
145             && type == world.map.cells[pos_i - 1])
146         || (   pos.y < world.map.length - 1 && diag_west
147             && type == world.map.cells[pos_i + world.map.length - !ind]))
148     {
149         return 1;
150     }
151     return 0;
152 }
153
154
155
156 static void make_sea()
157 {
158     uint16_t y, x;
159     for (y = 0; y < world.map.length; y++)
160     {
161         for (x = 0;
162              x < world.map.length;
163              world.map.cells[(y * world.map.length) + x] = '~', x++);
164     }
165 }
166
167
168
169 static void make_island()
170 {
171     char type = '.';
172     uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
173     uint32_t size = world.map.length * world.map.length;
174     world.map.cells[(size / 2) + add_half_width] = type;
175     struct yx_uint8 pos;
176     iter_limit(1);
177     while (iter_limit(0))
178     {
179         pos.y = rrand() % world.map.length;
180         pos.x = rrand() % world.map.length;
181         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
182         if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
183         {
184             if (   pos.y == 0 || pos.y == world.map.length - 1
185                 || pos.x == 0 || pos.x == world.map.length - 1)
186             {
187                 break;
188             }
189             world.map.cells[pos_i] = type;
190         }
191     }
192 }
193
194
195
196 static void make_trees()
197 {
198     char type = 'X';
199     struct yx_uint8 pos;
200     uint16_t n_trees = (world.map.length * world.map.length) / 16;
201     uint16_t i_trees = 0;
202     iter_limit(1);
203     while (i_trees <= n_trees && iter_limit(0))
204     {
205         uint8_t single_allowed = rrand() % 32;
206         pos.y = rrand() % world.map.length;
207         pos.x = rrand() % world.map.length;
208         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
209         if ('.' == world.map.cells[pos_i]
210             && (!single_allowed || is_neighbor(pos, type)))
211         {
212             world.map.cells[pos_i] = type;
213             i_trees++;
214         }
215     }
216 }
217
218
219
220 extern void remake_map()
221 {
222     free(world.map.cells);
223     world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
224     uint32_t store_seed = world.seed;
225     world.seed = world.seed_map;
226     make_sea();
227     make_island();
228     make_trees();
229     world.seed = store_seed;
230 }
231
232
233
234 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
235 {
236     uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx);
237     if (yx && !wraptest && yx->x < world.map.length && yx->y < world.map.length)
238     {
239         return 1;
240     }
241     return 0;
242 }