home · contact · privacy
Add auto-mapping / map memory.
[plomrogue] / src / server / field_of_view.c
1 /* src/server/field_of_view.c */
2
3 #include "field_of_view.h"
4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int32_t */
5 #include <stdlib.h> /* free() */
6 #include <string.h> /* memset() */
7 #include "../common/rexit.h" /* exit_trouble() */
8 #include "../common/try_malloc.h" /* try_malloc() */
9 #include "things.h" /* Thing */
10 #include "yx_uint8.h" /* yx_uint8 */
11 #include "world.h" /* world  */
12
13
14
15 /* Number of degrees a circle is divided into. The greater it is, the greater
16  * the angle precision. But make it one whole zero larger and bizarre FOV bugs
17  * appear on large maps, probably due to value overflows (TODO: more research!).
18  */
19 #define CIRCLE 3600000
20
21
22
23 /* Angle of a shadow. */
24 struct shadow_angle
25 {
26     struct shadow_angle * next;
27     uint32_t left_angle;
28     uint32_t right_angle;
29 };
30
31
32
33 /* Move "yx" into hex direction "d". */
34 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
35
36 /* Move "yx" into hex direction "d". If this moves "yx" beyond the minimal (0)
37  * or maximal (UINT8_MAX) column or row, it wraps to the opposite side. Such
38  * wrapping is returned as a wraps enum value and stored, so that further calls
39  * to move "yx" back into the opposite direction may unwrap it again. Pass an
40  * "unwrap" of !0 to re-set the internal wrap memory to 0.
41  */
42 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap);
43
44 /* Wrapper to mv_yx_in_dir_wrap(), returns 1 if the wrapped function moved "yx"
45  * within the wrap borders and the map size, else 0.
46  */
47 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx);
48
49 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
50 static uint32_t correct_angle(int32_t angle);
51
52 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
53  * it meets the shadow from the right or the left. Returns 1 on success, else 0.
54  */
55 static uint8_t try_merge(struct shadow_angle * shadow,
56                          uint32_t left_angle, uint32_t right_angle);
57
58 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
59  * existing shadow angle in "shadows". On success, see if this leads to any
60  * additional shadow angle overlaps and merge these accordingly. Return 1 on
61  * success, else 0.
62  */
63 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
64                                   struct shadow_angle ** shadows);
65
66 /* If "pos_in_map" in angle between"left_angle" to "right_angle" to the viewing
67  * actor is in a shadow from the shadow list "shadows", mark it as HIDDEN on the
68  * "fov_map"; else, if the world map features a viewing obstacle on the world
69  * map, calculate its shadow angle to the viewer and add it to "shadows".
70  */
71 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
72                        struct shadow_angle ** shadows, uint16_t pos_in_map,
73                        uint8_t * fov_map);
74
75 /* Free shadow angles list "angles". */
76 static void free_angles(struct shadow_angle * angles);
77
78 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
79  * on the circle of that distance to the origin on hex "hex_i" (as counted from
80  * the circle's rightmost point), for setting shaded cells in "fov_map" and
81  * potentially adding a new shadow to linked shadow angle list "shadows".
82  */
83 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
84                           struct yx_uint8 * test_pos,
85                           struct shadow_angle ** shadows);
86
87 /* Update "t"'s .mem_map memory with what's in its current field of view. */
88 static void update_map_memory(struct Thing * t, uint32_t map_size);
89
90
91
92 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
93 {
94     if     (d == 'e')
95     {
96         yx->x = yx->x + (yx->y % 2);
97         yx->y--;
98     }
99     else if (d == 'd')
100     {
101         yx->x++;
102     }
103     else if (d == 'c')
104     {
105         yx->x = yx->x + (yx->y % 2);
106         yx->y++;
107     }
108     else if (d == 'x')
109     {
110         yx->x = yx->x - !(yx->y % 2);
111         yx->y++;
112     }
113     else if (d == 's')
114     {
115         yx->x--;
116     }
117     else if (d == 'w')
118     {
119         yx->x = yx->x - !(yx->y % 2);
120         yx->y--;
121     }
122 }
123
124
125
126 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
127 {
128     static int8_t wrap_west_east   = 0;
129     static int8_t wrap_north_south = 0;
130     if (unwrap)
131     {
132         wrap_west_east = wrap_north_south = 0;
133         return 0;
134     }
135     struct yx_uint8 original;
136     original.y = yx->y;
137     original.x = yx->x;
138     mv_yx_in_hex_dir(d, yx);
139     if      (strchr("edc", d) && yx->x < original.x)
140     {
141         wrap_west_east++;
142     }
143     else if (strchr("xsw", d) && yx->x > original.x)
144     {
145         wrap_west_east--;
146     }
147     if      (strchr("we", d) && yx->y > original.y)
148     {
149         wrap_north_south--;
150     }
151     else if (strchr("xc", d) && yx->y < original.y)
152     {
153         wrap_north_south++;
154     }
155     return (wrap_west_east != 0) + (wrap_north_south != 0);
156 }
157
158
159
160 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
161 {
162     uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
163     if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
164     {
165         return 1;
166     }
167     return 0;
168 }
169
170
171
172 static uint32_t correct_angle(int32_t angle)
173 {
174     while (angle < 0)
175     {
176         angle = angle + CIRCLE;
177     }
178     while (angle > CIRCLE)
179     {
180         angle = angle - CIRCLE;
181     }
182     return angle;
183 }
184
185
186
187 static uint8_t try_merge(struct shadow_angle * shadow,
188                          uint32_t left_angle, uint32_t right_angle)
189 {
190     if      (   shadow->right_angle <= left_angle + 1
191              && shadow->right_angle >= right_angle)
192     {
193         shadow->right_angle = right_angle;
194     }
195     else if (   shadow->left_angle + 1 >= right_angle
196              && shadow->left_angle     <= left_angle)
197     {
198         shadow->left_angle = left_angle;
199     }
200     else
201     {
202         return 0;
203     }
204     return 1;
205 }
206
207
208
209 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
210                                   struct shadow_angle ** shadows)
211 {
212     uint8_t angle_merge = 0;
213     struct shadow_angle * shadow;
214     for (shadow = *shadows; shadow; shadow = shadow->next)
215     {
216         if (try_merge(shadow, left_angle, right_angle))
217         {
218             angle_merge = 1;
219         }
220     }
221     if (angle_merge)
222     {
223         struct shadow_angle * shadow1;
224         for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
225         {
226             struct shadow_angle * last_shadow = NULL;
227             struct shadow_angle * shadow2;
228             for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
229             {
230                 if (   shadow1 != shadow2
231                     && try_merge(shadow1, shadow2->left_angle,
232                                           shadow2->right_angle))
233                 {
234                     struct shadow_angle * to_free = shadow2;
235                     if (last_shadow)
236                     {
237                         last_shadow->next = shadow2->next;
238                         shadow2 = last_shadow;
239                     }
240                     else
241                     {
242                         *shadows = shadow2->next;
243                         shadow2 = *shadows;
244                     }
245                     free(to_free);
246                 }
247                 last_shadow = shadow2;
248             }
249         }
250     }
251     return angle_merge;
252 }
253
254
255
256 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
257                        struct shadow_angle ** shadows, uint16_t pos_in_map,
258                        uint8_t * fov_map)
259 {
260     struct shadow_angle * shadow_i;
261     if (fov_map[pos_in_map] & VISIBLE)
262     {
263         for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
264         {
265             if (   left_angle <=  shadow_i->left_angle
266                 && right_angle >= shadow_i->right_angle)
267             {
268                 fov_map[pos_in_map] = HIDDEN;
269                 return;
270             }
271         }
272     }
273     if ('X' == world.map.cells[pos_in_map])
274     {
275         if (!try_merging_angles(left_angle, right_angle, shadows))
276         {
277             struct shadow_angle * shadow;
278             shadow = try_malloc(sizeof(struct shadow_angle), __func__);
279             shadow->left_angle  = left_angle;
280             shadow->right_angle = right_angle;
281             shadow->next = NULL;
282             if (*shadows)
283             {
284                 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
285                 {
286                     if (!shadow_i->next)
287                     {
288                         shadow_i->next = shadow;
289                         return;
290                     }
291                 }
292             }
293             *shadows = shadow;
294         }
295     }
296 }
297
298
299
300 static void free_angles(struct shadow_angle * angles)
301 {
302     if (angles->next)
303     {
304         free_angles(angles->next);
305     }
306     free(angles);
307 }
308
309
310
311 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
312                           struct yx_uint8 * test_pos,
313                           struct shadow_angle ** shadows)
314 {
315     int32_t left_angle_uncorrected =   ((CIRCLE / 12) / dist)
316                                      - ((hex_i * (CIRCLE / 6)) / dist);
317     int32_t right_angle_uncorrected =   left_angle_uncorrected
318                                       - (CIRCLE / (6 * dist));
319     uint32_t left_angle  = correct_angle(left_angle_uncorrected);
320     uint32_t right_angle = correct_angle(right_angle_uncorrected);
321     uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
322     uint16_t pos_in_map = test_pos->y * world.map.length + test_pos->x;
323     set_shadow(left_angle, right_angle_1st, shadows, pos_in_map, fov_map);
324     if (right_angle_1st != right_angle)
325     {
326         left_angle = CIRCLE;
327         set_shadow(left_angle, right_angle, shadows, pos_in_map, fov_map);
328     }
329 }
330
331
332
333 static void update_map_memory(struct Thing * t, uint32_t map_size)
334 {
335     if (!t->mem_map)
336     {
337         t->mem_map = try_malloc(map_size, __func__);
338         memset(t->mem_map, ' ', map_size);
339     }
340     uint32_t i;
341     for (i = 0; i < map_size; i++)
342     {
343         if (' ' == t->mem_map[i] && t->fov_map[i] & VISIBLE)
344         {
345             t->mem_map[i] = world.map.cells[i];
346         }
347     }
348 }
349
350
351
352 extern void build_fov_map(struct Thing * t)
353 {
354     uint32_t map_size = world.map.length * world.map.length;
355     t->fov_map = t->fov_map ? t->fov_map : try_malloc(map_size, __func__);
356     memset(t->fov_map, VISIBLE, map_size);
357     struct yx_uint8 test_pos = t->pos;
358     struct shadow_angle * shadows = NULL;
359     char * circle_dirs = "xswedc";
360     uint16_t dist;
361     uint8_t first_round, circle_on_map;
362     for (first_round = 1, dist = 1, circle_on_map = 1; circle_on_map; dist++)
363     {
364         if (!first_round)
365         {
366             mv_yx_in_dir_legal('c', &test_pos);
367         }
368         char dir = 'd';
369         uint8_t i_dir = first_round = circle_on_map = 0;
370         uint16_t i_dist, hex_i;
371         for (hex_i = 0, i_dist = 1; hex_i < 6 * dist; i_dist++, hex_i++)
372         {
373             if (mv_yx_in_dir_legal(dir, &test_pos))
374             {
375                 eval_position(dist, hex_i, t->fov_map, &test_pos, &shadows);
376                 circle_on_map = 1;
377             }
378             dir = circle_dirs[i_dir];
379             if (dist == i_dist)
380             {
381                 i_dist = 0;
382                 i_dir++;
383             }
384         }
385     }
386     mv_yx_in_dir_wrap(0, NULL, 1);
387     free_angles(shadows);
388     update_map_memory(t, map_size);
389 }