home · contact · privacy
34bfa138eb06c1b5f641f0ff386d78d0f7e9d3d6
[plomrogue] / src / server / field_of_view.c
1 /* src/server/field_of_view.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 "field_of_view.h"
9 #include <stddef.h> /* NULL */
10 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int32_t, UINT8_MAX */
11 #include <stdlib.h> /* free() */
12 #include <string.h> /* memset() */
13 #include "../common/rexit.h" /* exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "../common/yx_uint8.h" /* yx_uint8 */
16 #include "map.h" /* mv_yx_in_dir_legal() */
17 #include "things.h" /* Thing, ThingInMemory, add_thing_to_memory_map() */
18 #include "world.h" /* world  */
19
20
21
22 /* Number of degrees a circle is divided into. The greater it is, the greater
23  * the angle precision. But make it one whole zero larger and bizarre FOV bugs
24  * appear on large maps, probably due to value overflows (TODO: more research!).
25  */
26 #define CIRCLE 3600000
27
28
29
30 /* Angle of a shadow. */
31 struct shadow_angle
32 {
33     struct shadow_angle * next;
34     uint32_t left_angle;
35     uint32_t right_angle;
36 };
37
38
39
40 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
41 static uint32_t correct_angle(int32_t angle);
42
43 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
44  * it meets the shadow from the right or the left. Returns 1 on success, else 0.
45  */
46 static uint8_t try_merge(struct shadow_angle * shadow,
47                          uint32_t left_angle, uint32_t right_angle);
48
49 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
50  * existing shadow angle in "shadows". On success, see if this leads to any
51  * additional shadow angle overlaps and merge these accordingly. Return 1 on
52  * success, else 0.
53  */
54 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
55                                   struct shadow_angle ** shadows);
56
57 /* Test whether angle between "left_angle" and "right_angle", or at least
58  * "middle_angle", is captured inside one of the shadow angles in "shadows". If
59  * so, set hex in "fov_map" indexed by "pos_in_map" to 'H'. If the whole angle
60  * and not just "middle_angle" is captured, return 1. Any other case: 0.
61  */
62 static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
63                          uint32_t middle_angle, struct shadow_angle ** shadows,
64                          uint16_t pos_in_map, char * fov_map);
65
66 /* Free shadow angles list "angles". */
67 static void free_angles(struct shadow_angle * angles);
68
69 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
70  * on the circle of that distance to the origin on hex "hex_i" (as counted from
71  * the circle's rightmost point), for setting shaded hexes in "fov_map" and
72  * potentially adding a new shadow to linked shadow angle list "shadows".
73  */
74 static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
75                           struct yx_uint8 * test_pos,
76                           struct shadow_angle ** shadows);
77
78 /* Update "t"'s .mem_map memory with what's in its current FOV, remove from its
79  * .t_mem all memorized things in FOV and add inanimiate things in FOV to it.
80  */
81 static void update_map_memory(struct Thing * t, uint32_t map_size);
82
83
84
85 static uint32_t correct_angle(int32_t angle)
86 {
87     while (angle < 0)
88     {
89         angle = angle + CIRCLE;
90     }
91     while (angle > CIRCLE)
92     {
93         angle = angle - CIRCLE;
94     }
95     return angle;
96 }
97
98
99
100 static uint8_t try_merge(struct shadow_angle * shadow,
101                          uint32_t left_angle, uint32_t right_angle)
102 {
103     if      (   shadow->right_angle <= left_angle + 1
104              && shadow->right_angle >= right_angle)
105     {
106         shadow->right_angle = right_angle;
107     }
108     else if (   shadow->left_angle + 1 >= right_angle
109              && shadow->left_angle     <= left_angle)
110     {
111         shadow->left_angle = left_angle;
112     }
113     else
114     {
115         return 0;
116     }
117     return 1;
118 }
119
120
121
122 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
123                                   struct shadow_angle ** shadows)
124 {
125     uint8_t angle_merge = 0;
126     struct shadow_angle * shadow;
127     for (shadow = *shadows; shadow; shadow = shadow->next)
128     {
129         if (try_merge(shadow, left_angle, right_angle))
130         {
131             angle_merge = 1;
132         }
133     }
134     if (angle_merge)
135     {
136         struct shadow_angle * shadow1;
137         for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
138         {
139             struct shadow_angle * last_shadow = NULL;
140             struct shadow_angle * shadow2;
141             for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
142             {
143                 if (   shadow1 != shadow2
144                     && try_merge(shadow1, shadow2->left_angle,
145                                           shadow2->right_angle))
146                 {
147                     struct shadow_angle * to_free = shadow2;
148                     if (last_shadow)
149                     {
150                         last_shadow->next = shadow2->next;
151                         shadow2 = last_shadow;
152                     }
153                     else
154                     {
155                         *shadows = shadow2->next;
156                         shadow2 = *shadows;
157                     }
158                     free(to_free);
159                 }
160                 last_shadow = shadow2;
161             }
162         }
163     }
164     return angle_merge;
165 }
166
167
168
169 static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
170                          uint32_t middle_angle, struct shadow_angle ** shadows,
171                          uint16_t pos_in_map, char * fov_map)
172 {
173     struct shadow_angle * shadow_i;
174     if (fov_map[pos_in_map] == 'v')
175     {
176         for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
177         {
178             if (   left_angle <=  shadow_i->left_angle
179                 && right_angle >= shadow_i->right_angle)
180             {
181                 fov_map[pos_in_map] = 'H';
182                 return 1;
183             }
184             if (   middle_angle < shadow_i->left_angle
185                 && middle_angle > shadow_i->right_angle)
186             {
187                 fov_map[pos_in_map] = 'H';
188             }
189         }
190     }
191     return 0;
192 }
193
194
195
196 /* To "shadows", add shadow defined by "left_angle" and "right_angle", either as
197  * new entry or as part of an existing shadow (swallowed whole or extending it).
198  */
199 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
200                        struct shadow_angle ** shadows)
201 {
202     struct shadow_angle * shadow_i;
203     if (!try_merging_angles(left_angle, right_angle, shadows))
204     {
205         struct shadow_angle * shadow;
206         shadow = try_malloc(sizeof(struct shadow_angle), __func__);
207         shadow->left_angle  = left_angle;
208         shadow->right_angle = right_angle;
209         shadow->next = NULL;
210         if (*shadows)
211         {
212             for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
213             {
214                 if (!shadow_i->next)
215                 {
216                     shadow_i->next = shadow;
217                     return;
218                 }
219             }
220         }
221         *shadows = shadow;
222     }
223 }
224
225
226
227 static void free_angles(struct shadow_angle * angles)
228 {
229     if (angles->next)
230     {
231         free_angles(angles->next);
232     }
233     free(angles);
234 }
235
236
237
238 static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
239                           struct yx_uint8 * test_pos,
240                           struct shadow_angle ** shadows)
241 {
242     int32_t left_angle_uncorrected =   ((CIRCLE / 12) / dist)
243                                      - (hex_i * (CIRCLE / 6) / dist);
244     int32_t right_angle_uncorrected =   left_angle_uncorrected
245                                       - (CIRCLE / (6 * dist));
246     uint32_t left_angle  = correct_angle(left_angle_uncorrected);
247     uint32_t right_angle = correct_angle(right_angle_uncorrected);
248     uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
249     uint32_t middle_angle = 0;
250     if (right_angle_1st)
251     {
252         middle_angle = right_angle + ((left_angle - right_angle) / 2);
253     }
254     uint16_t pos_in_map = test_pos->y * world.map.length + test_pos->x;
255     uint8_t all_shaded = shade_hex(left_angle, right_angle_1st, middle_angle,
256                                    shadows, pos_in_map, fov_map);
257     if (!all_shaded && 'X' == world.map.cells[pos_in_map])
258     {
259         set_shadow(left_angle, right_angle_1st, shadows);
260         if (right_angle_1st != right_angle)
261         {
262             left_angle = CIRCLE;
263             set_shadow(left_angle, right_angle, shadows);
264         }
265     }
266 }
267
268
269
270 static void update_map_memory(struct Thing * t_eye, uint32_t map_size)
271 {
272     if (!t_eye->mem_map)
273     {
274         t_eye->mem_map = try_malloc(map_size, __func__);
275         memset(t_eye->mem_map, ' ', map_size);
276     }
277     uint32_t i;
278     for (i = 0; i < map_size; i++)
279     {
280         if (' ' == t_eye->mem_map[i] && t_eye->fov_map[i] == 'v')
281         {
282             t_eye->mem_map[i] = world.map.cells[i];
283         }
284     }
285     struct ThingInMemory * tm = t_eye->t_mem;
286     struct ThingInMemory * tm_prev = NULL;
287     struct ThingInMemory * tm_next = NULL;
288     for (; tm; tm = tm_next)
289     {
290         tm_next = tm->next;
291         if ('v' == t_eye->fov_map[tm->pos.y * world.map.length + tm->pos.x])
292         {
293             if (tm_prev)
294             {
295                 tm_prev->next = tm->next;
296             }
297             else
298             {
299                 t_eye->t_mem = tm->next;
300             }
301             free(tm);
302             continue;
303         }
304         tm_prev = tm;
305     }
306     struct Thing * t = world.things;
307     for (; t; t = t->next)
308     {
309         if (   !t->lifepoints
310             && 'v' == t_eye->fov_map[t->pos.y * world.map.length + t->pos.x])
311         {
312             add_thing_to_memory_map(t_eye, t->type, t->pos.y, t->pos.x);
313         }
314     }
315 }
316
317
318
319 extern void build_fov_map(struct Thing * t)
320 {
321     uint32_t map_size = world.map.length * world.map.length;
322     t->fov_map = t->fov_map ? t->fov_map : try_malloc(map_size, __func__);
323     memset(t->fov_map, 'v', map_size);
324     struct shadow_angle * shadows = NULL;
325     struct yx_uint8 test_pos = t->pos;
326     char * circledirs_string = "xswedc";
327     uint16_t circle_i;
328     uint8_t circle_is_on_map;
329     for (circle_i = 1, circle_is_on_map = 1; circle_is_on_map; circle_i++)
330     {
331         circle_is_on_map = 0;
332         if (1 < circle_i)                      /* All circles but the 1st are */
333         {                                      /* moved into starting from a  */
334             mv_yx_in_dir_legal('c', &test_pos);/* previous circle's last hex, */
335         }                                      /* i.e. from the upper left.   */
336         char dir_char = 'd'; /* Circle's 1st hex is entered by rightward move.*/
337         uint8_t dir_char_pos_in_circledirs_string = UINT8_MAX;
338         uint16_t dist_i, hex_i;
339         for (hex_i=0, dist_i=circle_i; hex_i < 6 * circle_i; dist_i++, hex_i++)
340         {
341             if (circle_i < dist_i)
342             {
343                 dist_i = 1;
344                 dir_char=circledirs_string[++dir_char_pos_in_circledirs_string];
345             }
346             if (mv_yx_in_dir_legal(dir_char, &test_pos))
347             {
348                 eval_position(circle_i, hex_i, t->fov_map, &test_pos, &shadows);
349                 circle_is_on_map = 1;
350             }
351         }
352     }
353     mv_yx_in_dir_legal(0, NULL);
354     free_angles(shadows);
355     update_map_memory(t, map_size);
356 }