home · contact · privacy
Server: Minor refactoring.
[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 /* Call this too often with "init" of 0 and the game exits with an error message
19  * about reaching an iteration limit. An "init" of 1 sets the iteration counter
20  * to 0. Iteration limit is currently 256 * UINT16_MAX.
21  */
22 static uint8_t iter_limit(uint8_t init);
23
24 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
25 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
26
27 /* Fill map with '~' cells. */
28 static void make_sea();
29
30 /* Put island of '.' cells inside map sea. */
31 static void make_sea();
32
33 /* Put tree cells of 'X' on island. */
34 static void make_trees();
35
36
37
38 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
39 {
40     if      (d == 'e')
41     {
42         yx->x = yx->x + (yx->y % 2);
43         yx->y--;
44     }
45     else if (d == 'd')
46     {
47         yx->x++;
48     }
49     else if (d == 'c')
50     {
51         yx->x = yx->x + (yx->y % 2);
52         yx->y++;
53     }
54     else if (d == 'x')
55     {
56         yx->x = yx->x - !(yx->y % 2);
57         yx->y++;
58     }
59     else if (d == 's')
60     {
61         yx->x--;
62     }
63     else if (d == 'w')
64     {
65         yx->x = yx->x - !(yx->y % 2);
66         yx->y--;
67     }
68 }
69
70
71
72 static uint8_t iter_limit(uint8_t init)
73 {
74     static uint32_t i = 0;
75     char * err = "Map generation reached iteration limit. Change map size?";
76     if (init)
77     {
78         i = 0;
79         return 0;
80     }
81     i++;
82     exit_err(256 * UINT16_MAX == i, err);
83     return 1;
84 }
85
86
87
88 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
89 {
90     uint8_t ind = pos.y % 2;
91     uint8_t diag_west = pos.x + ind > 0;
92     uint8_t diag_east = pos.x + ind <= world.map.length - 1;
93     uint16_t pos_i = (pos.y * world.map.length) + pos.x;
94     if (   (   pos.y > 0                    && diag_east
95             && type == world.map.cells[pos_i - world.map.length + ind])
96         || (   pos.x < world.map.length - 1
97             && type == world.map.cells[pos_i + 1])
98         || (   pos.y < world.map.length - 1 && diag_east
99             && type == world.map.cells[pos_i + world.map.length + ind])
100         || (   pos.y > 0                    && diag_west
101             && type == world.map.cells[pos_i - world.map.length - !ind])
102         || (   pos.x > 0
103             && type == world.map.cells[pos_i - 1])
104         || (   pos.y < world.map.length - 1 && diag_west
105             && type == world.map.cells[pos_i + world.map.length - !ind]))
106     {
107         return 1;
108     }
109     return 0;
110 }
111
112
113
114 static void make_sea()
115 {
116     uint16_t y, x;
117     for (y = 0; y < world.map.length; y++)
118     {
119         for (x = 0;
120              x < world.map.length;
121              world.map.cells[(y * world.map.length) + x] = '~', x++);
122     }
123 }
124
125
126
127 static void make_island()
128 {
129     char type = '.';
130     uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
131     uint32_t size = world.map.length * world.map.length;
132     world.map.cells[(size / 2) + add_half_width] = type;
133     struct yx_uint8 pos;
134     iter_limit(1);
135     while (iter_limit(0))
136     {
137         pos.y = rrand() % world.map.length;
138         pos.x = rrand() % world.map.length;
139         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
140         if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
141         {
142             if (   pos.y == 0 || pos.y == world.map.length - 1
143                 || pos.x == 0 || pos.x == world.map.length - 1)
144             {
145                 break;
146             }
147             world.map.cells[pos_i] = type;
148         }
149     }
150 }
151
152
153
154 static void make_trees()
155 {
156     char type = 'X';
157     struct yx_uint8 pos;
158     uint16_t n_trees = (world.map.length * world.map.length) / 16;
159     uint16_t i_trees = 0;
160     iter_limit(1);
161     while (i_trees <= n_trees && iter_limit(0))
162     {
163         uint8_t single_allowed = rrand() % 32;
164         pos.y = rrand() % world.map.length;
165         pos.x = rrand() % world.map.length;
166         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
167         if ('.' == world.map.cells[pos_i]
168             && (!single_allowed || is_neighbor(pos, type)))
169         {
170             world.map.cells[pos_i] = type;
171             i_trees++;
172         }
173     }
174 }
175
176
177
178 extern void remake_map()
179 {
180     free(world.map.cells);
181     world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
182     uint32_t store_seed = world.seed;
183     world.seed = world.seed_map;
184     make_sea();
185     make_island();
186     make_trees();
187     world.seed = store_seed;
188 }
189
190
191
192 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
193 {
194     static int8_t wrap_west_east   = 0;
195     static int8_t wrap_north_south = 0;
196     char * err = "Too much wrapping in mv_yx_in_dir_wrap().";
197     exit_err(   INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
198              || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south, err);
199     if (!yx)
200     {
201         wrap_west_east = wrap_north_south = 0;
202         return 0;
203     }
204     struct yx_uint8 original;
205     original.y = yx->y;
206     original.x = yx->x;
207     mv_yx_in_dir(dir, yx);
208     if      (strchr("edc", dir) && yx->x < original.x)
209     {
210         wrap_west_east++;
211     }
212     else if (strchr("xsw", dir) && yx->x > original.x)
213     {
214         wrap_west_east--;
215     }
216     if      (strchr("we", dir) && yx->y > original.y)
217     {
218         wrap_north_south--;
219     }
220     else if (strchr("xc", dir) && yx->y < original.y)
221     {
222         wrap_north_south++;
223     }
224     if (   !((wrap_west_east != 0) + (wrap_north_south != 0))
225         && yx->x < world.map.length && yx->y < world.map.length)
226     {
227         return 1;
228     }
229     return 0;
230 }