home · contact · privacy
440c84c2d6bea8a050673b30a7f3ff4a1321a5fc
[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
88
89 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
90 {
91     if     (d == 'e')
92     {
93         yx->x = yx->x + (yx->y % 2);
94         yx->y--;
95     }
96     else if (d == 'd')
97     {
98         yx->x++;
99     }
100     else if (d == 'c')
101     {
102         yx->x = yx->x + (yx->y % 2);
103         yx->y++;
104     }
105     else if (d == 'x')
106     {
107         yx->x = yx->x - !(yx->y % 2);
108         yx->y++;
109     }
110     else if (d == 's')
111     {
112         yx->x--;
113     }
114     else if (d == 'w')
115     {
116         yx->x = yx->x - !(yx->y % 2);
117         yx->y--;
118     }
119 }
120
121
122
123 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
124 {
125     static int8_t wrap_west_east   = 0;
126     static int8_t wrap_north_south = 0;
127     if (unwrap)
128     {
129         wrap_west_east = wrap_north_south = 0;
130         return 0;
131     }
132     struct yx_uint8 original;
133     original.y = yx->y;
134     original.x = yx->x;
135     mv_yx_in_hex_dir(d, yx);
136     if      (strchr("edc", d) && yx->x < original.x)
137     {
138         wrap_west_east++;
139     }
140     else if (strchr("xsw", d) && yx->x > original.x)
141     {
142         wrap_west_east--;
143     }
144     if      (strchr("we", d) && yx->y > original.y)
145     {
146         wrap_north_south--;
147     }
148     else if (strchr("xc", d) && yx->y < original.y)
149     {
150         wrap_north_south++;
151     }
152     return (wrap_west_east != 0) + (wrap_north_south != 0);
153 }
154
155
156
157 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
158 {
159     uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
160     if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
161     {
162         return 1;
163     }
164     return 0;
165 }
166
167
168
169 static uint32_t correct_angle(int32_t angle)
170 {
171     while (angle < 0)
172     {
173         angle = angle + CIRCLE;
174     }
175     while (angle > CIRCLE)
176     {
177         angle = angle - CIRCLE;
178     }
179     return angle;
180 }
181
182
183
184 static uint8_t try_merge(struct shadow_angle * shadow,
185                          uint32_t left_angle, uint32_t right_angle)
186 {
187     if      (   shadow->right_angle <= left_angle + 1
188              && shadow->right_angle >= right_angle)
189     {
190         shadow->right_angle = right_angle;
191     }
192     else if (   shadow->left_angle + 1 >= right_angle
193              && shadow->left_angle     <= left_angle)
194     {
195         shadow->left_angle = left_angle;
196     }
197     else
198     {
199         return 0;
200     }
201     return 1;
202 }
203
204
205
206 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
207                                   struct shadow_angle ** shadows)
208 {
209     uint8_t angle_merge = 0;
210     struct shadow_angle * shadow;
211     for (shadow = *shadows; shadow; shadow = shadow->next)
212     {
213         if (try_merge(shadow, left_angle, right_angle))
214         {
215             angle_merge = 1;
216         }
217     }
218     if (angle_merge)
219     {
220         struct shadow_angle * shadow1;
221         for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
222         {
223             struct shadow_angle * last_shadow = NULL;
224             struct shadow_angle * shadow2;
225             for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
226             {
227                 if (   shadow1 != shadow2
228                     && try_merge(shadow1, shadow2->left_angle,
229                                           shadow2->right_angle))
230                 {
231                     struct shadow_angle * to_free = shadow2;
232                     if (last_shadow)
233                     {
234                         last_shadow->next = shadow2->next;
235                         shadow2 = last_shadow;
236                     }
237                     else
238                     {
239                         *shadows = shadow2->next;
240                         shadow2 = *shadows;
241                     }
242                     free(to_free);
243                 }
244                 last_shadow = shadow2;
245             }
246         }
247     }
248     return angle_merge;
249 }
250
251
252
253 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
254                        struct shadow_angle ** shadows, uint16_t pos_in_map,
255                        uint8_t * fov_map)
256 {
257     struct shadow_angle * shadow_i;
258     if (fov_map[pos_in_map] & VISIBLE)
259     {
260         for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
261         {
262             if (   left_angle <=  shadow_i->left_angle
263                 && right_angle >= shadow_i->right_angle)
264             {
265                 fov_map[pos_in_map] = HIDDEN;
266                 return;
267             }
268         }
269     }
270     if ('X' == world.map.cells[pos_in_map])
271     {
272         if (!try_merging_angles(left_angle, right_angle, shadows))
273         {
274             struct shadow_angle * shadow;
275             shadow = try_malloc(sizeof(struct shadow_angle), __func__);
276             shadow->left_angle  = left_angle;
277             shadow->right_angle = right_angle;
278             shadow->next = NULL;
279             if (*shadows)
280             {
281                 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
282                 {
283                     if (!shadow_i->next)
284                     {
285                         shadow_i->next = shadow;
286                         return;
287                     }
288                 }
289             }
290             *shadows = shadow;
291         }
292     }
293 }
294
295
296
297 static void free_angles(struct shadow_angle * angles)
298 {
299     if (angles->next)
300     {
301         free_angles(angles->next);
302     }
303     free(angles);
304 }
305
306
307
308 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
309                           struct yx_uint8 * test_pos,
310                           struct shadow_angle ** shadows)
311 {
312     int32_t left_angle_uncorrected =   ((CIRCLE / 12) / dist)
313                                      - ((hex_i * (CIRCLE / 6)) / dist);
314     int32_t right_angle_uncorrected =   left_angle_uncorrected
315                                       - (CIRCLE / (6 * dist));
316     uint32_t left_angle  = correct_angle(left_angle_uncorrected);
317     uint32_t right_angle = correct_angle(right_angle_uncorrected);
318     uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
319     uint16_t pos_in_map = test_pos->y * world.map.length + test_pos->x;
320     set_shadow(left_angle, right_angle_1st, shadows, pos_in_map, fov_map);
321     if (right_angle_1st != right_angle)
322     {
323         left_angle = CIRCLE;
324         set_shadow(left_angle, right_angle, shadows, pos_in_map, fov_map);
325     }
326 }
327
328
329
330 extern void build_fov_map(struct Thing * t)
331 {
332     uint32_t map_size = world.map.length * world.map.length;
333     t->fov_map = t->fov_map ? t->fov_map : try_malloc(map_size, __func__);
334     memset(t->fov_map, VISIBLE, map_size);
335     struct yx_uint8 test_pos = t->pos;
336     struct shadow_angle * shadows = NULL;
337     char * circle_dirs = "xswedc";
338     uint16_t dist;
339     uint8_t first_round, circle_on_map;
340     for (first_round = 1, dist = 1, circle_on_map = 1; circle_on_map; dist++)
341     {
342         if (!first_round)
343         {
344             mv_yx_in_dir_legal('c', &test_pos);
345         }
346         char dir = 'd';
347         uint8_t i_dir = first_round = circle_on_map = 0;
348         uint16_t i_dist, hex_i;
349         for (hex_i = 0, i_dist = 1; hex_i < 6 * dist; i_dist++, hex_i++)
350         {
351             if (mv_yx_in_dir_legal(dir, &test_pos))
352             {
353                 eval_position(dist, hex_i, t->fov_map, &test_pos, &shadows);
354                 circle_on_map = 1;
355             }
356             dir = circle_dirs[i_dir];
357             if (dist == i_dist)
358             {
359                 i_dist = 0;
360                 i_dir++;
361             }
362         }
363     }
364     mv_yx_in_dir_wrap(0, NULL, 1);
365     free_angles(shadows);
366 }