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