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