home · contact · privacy
Server: Minor function renaming.
[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, UINT16_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     if (!yx)
82     {
83         wrap_west_east = wrap_north_south = 0;
84         return 0;
85     }
86     struct yx_uint8 original;
87     original.y = yx->y;
88     original.x = yx->x;
89     mv_yx_in_dir(d, yx);
90     if      (strchr("edc", d) && yx->x < original.x)
91     {
92         wrap_west_east++;
93     }
94     else if (strchr("xsw", d) && yx->x > original.x)
95     {
96         wrap_west_east--;
97     }
98     if      (strchr("we", d) && yx->y > original.y)
99     {
100         wrap_north_south--;
101     }
102     else if (strchr("xc", d) && yx->y < original.y)
103     {
104         wrap_north_south++;
105     }
106     return (wrap_west_east != 0) + (wrap_north_south != 0);
107 }
108
109
110
111 static uint8_t iter_limit(uint8_t init)
112 {
113     static uint32_t i = 0;
114     char * err = "Map generation reached iteration limit. Change map size?";
115     if (init)
116     {
117         i = 0;
118         return 0;
119     }
120     i++;
121     exit_err(256 * UINT16_MAX == i, err);
122     return 1;
123 }
124
125
126
127 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
128 {
129     uint8_t ind = pos.y % 2;
130     uint8_t diag_west = pos.x + ind > 0;
131     uint8_t diag_east = pos.x + ind <= world.map.length - 1;
132     uint16_t pos_i = (pos.y * world.map.length) + pos.x;
133     if (   (   pos.y > 0                    && diag_east
134             && type == world.map.cells[pos_i - world.map.length + ind])
135         || (   pos.x < world.map.length - 1
136             && type == world.map.cells[pos_i + 1])
137         || (   pos.y < world.map.length - 1 && diag_east
138             && type == world.map.cells[pos_i + world.map.length + ind])
139         || (   pos.y > 0                    && diag_west
140             && type == world.map.cells[pos_i - world.map.length - !ind])
141         || (   pos.x > 0
142             && type == world.map.cells[pos_i - 1])
143         || (   pos.y < world.map.length - 1 && diag_west
144             && type == world.map.cells[pos_i + world.map.length - !ind]))
145     {
146         return 1;
147     }
148     return 0;
149 }
150
151
152
153 static void make_sea()
154 {
155     uint16_t y, x;
156     for (y = 0; y < world.map.length; y++)
157     {
158         for (x = 0;
159              x < world.map.length;
160              world.map.cells[(y * world.map.length) + x] = '~', x++);
161     }
162 }
163
164
165
166 static void make_island()
167 {
168     char type = '.';
169     uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
170     uint32_t size = world.map.length * world.map.length;
171     world.map.cells[(size / 2) + add_half_width] = type;
172     struct yx_uint8 pos;
173     iter_limit(1);
174     while (iter_limit(0))
175     {
176         pos.y = rrand() % world.map.length;
177         pos.x = rrand() % world.map.length;
178         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
179         if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
180         {
181             if (   pos.y == 0 || pos.y == world.map.length - 1
182                 || pos.x == 0 || pos.x == world.map.length - 1)
183             {
184                 break;
185             }
186             world.map.cells[pos_i] = type;
187         }
188     }
189 }
190
191
192
193 static void make_trees()
194 {
195     char type = 'X';
196     struct yx_uint8 pos;
197     uint16_t n_trees = (world.map.length * world.map.length) / 16;
198     uint16_t i_trees = 0;
199     iter_limit(1);
200     while (i_trees <= n_trees && iter_limit(0))
201     {
202         uint8_t single_allowed = rrand() % 32;
203         pos.y = rrand() % world.map.length;
204         pos.x = rrand() % world.map.length;
205         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
206         if ('.' == world.map.cells[pos_i]
207             && (!single_allowed || is_neighbor(pos, type)))
208         {
209             world.map.cells[pos_i] = type;
210             i_trees++;
211         }
212     }
213 }
214
215
216
217 extern void remake_map()
218 {
219     free(world.map.cells);
220     world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
221     uint32_t store_seed = world.seed;
222     world.seed = world.seed_map;
223     make_sea();
224     make_island();
225     make_trees();
226     world.seed = store_seed;
227 }
228
229
230
231 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
232 {
233     uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx);
234     if (yx && !wraptest && yx->x < world.map.length && yx->y < world.map.length)
235     {
236         return 1;
237     }
238     return 0;
239 }