1 /* src/server/field_of_view.c */
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 "map.h" /* yx_to_map_pos() */
10 #include "things.h" /* Thing */
11 #include "yx_uint8.h" /* yx_uint8 */
12 #include "world.h" /* world */
16 /* Number of degrees a circle is divided into. The greater it is, the greater
17 * the angle precision. But make it one whole zero larger and bizarre FOV bugs
18 * appear on large maps, probably due to value overflows (TODO: more research!).
20 #define CIRCLE 3600000
24 /* Angle of a shadow. */
27 struct shadow_angle * next;
34 /* Move "yx" into hex direction "d". */
35 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
37 /* Move "yx" into hex direction "d". If this moves "yx" beyond the minimal (0)
38 * or maximal (UINT8_MAX) column or row, it wraps to the opposite side. Such
39 * wrapping is returned as a wraps enum value and stored, so that further calls
40 * to move "yx" back into the opposite direction may unwrap it again. Pass an
41 * "unwrap" of !0 to re-set the internal wrap memory to 0.
43 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap);
45 /* Wrapper to mv_yx_in_dir_wrap(), returns 1 if the wrapped function moved "yx"
46 * within the wrap borders and the map size, else 0.
48 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx);
50 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
51 static uint32_t correct_angle(int32_t angle);
53 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
54 * it meets the shadow from the right or the left. Returns 1 on success, else 0.
56 static uint8_t try_merge(struct shadow_angle * shadow,
57 uint32_t left_angle, uint32_t right_angle);
59 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
60 * existing shadow angle in "shadows". On success, see if this leads to any
61 * additional shadow angle overlaps and merge these accordingly. Return 1 on
64 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
65 struct shadow_angle ** shadows);
67 /* If "pos_in_map" in angle between"left_angle" to "right_angle" to the viewing
68 * actor is in a shadow from the shadow list "shadows", mark it as HIDDEN on the
69 * "fov_map"; else, if the world map features a viewing obstacle on the world
70 * map, calculate its shadow angle to the viewer and add it to "shadows".
72 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
73 struct shadow_angle ** shadows, uint16_t pos_in_map,
76 /* Free shadow angles list "angles". */
77 static void free_angles(struct shadow_angle * angles);
79 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
80 * on the circle of that distance to the origin on hex "hex_i" (as counted from
81 * the circle's rightmost point), for setting shaded cells in "fov_map" and
82 * potentially adding a new shadow to linked shadow angle list "shadows".
84 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
85 struct yx_uint8 * test_pos,
86 struct shadow_angle ** shadows);
90 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
94 yx->x = yx->x + (yx->y % 2);
103 yx->x = yx->x + (yx->y % 2);
108 yx->x = yx->x - !(yx->y % 2);
117 yx->x = yx->x - !(yx->y % 2);
124 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
126 static int8_t wrap_west_east = 0;
127 static int8_t wrap_north_south = 0;
130 wrap_west_east = wrap_north_south = 0;
133 struct yx_uint8 original;
136 mv_yx_in_hex_dir(d, yx);
137 if (strchr("edc", d) && yx->x < original.x)
141 else if (strchr("xsw", d) && yx->x > original.x)
145 if (strchr("we", d) && yx->y > original.y)
149 else if (strchr("xc", d) && yx->y < original.y)
153 return (wrap_west_east != 0) + (wrap_north_south != 0);
158 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
160 uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
161 if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
170 static uint32_t correct_angle(int32_t angle)
174 angle = angle + CIRCLE;
176 while (angle > CIRCLE)
178 angle = angle - CIRCLE;
185 static uint8_t try_merge(struct shadow_angle * shadow,
186 uint32_t left_angle, uint32_t right_angle)
188 if ( shadow->right_angle <= left_angle + 1
189 && shadow->right_angle >= right_angle)
191 shadow->right_angle = right_angle;
193 else if ( shadow->left_angle + 1 >= right_angle
194 && shadow->left_angle <= left_angle)
196 shadow->left_angle = left_angle;
207 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
208 struct shadow_angle ** shadows)
210 uint8_t angle_merge = 0;
211 struct shadow_angle * shadow;
212 for (shadow = *shadows; shadow; shadow = shadow->next)
214 if (try_merge(shadow, left_angle, right_angle))
221 struct shadow_angle * shadow1;
222 for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
224 struct shadow_angle * last_shadow = NULL;
225 struct shadow_angle * shadow2;
226 for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
228 if ( shadow1 != shadow2
229 && try_merge(shadow1, shadow2->left_angle,
230 shadow2->right_angle))
232 struct shadow_angle * to_free = shadow2;
235 last_shadow->next = shadow2->next;
236 shadow2 = last_shadow;
240 *shadows = shadow2->next;
245 last_shadow = shadow2;
254 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
255 struct shadow_angle ** shadows, uint16_t pos_in_map,
258 struct shadow_angle * shadow_i;
259 if (fov_map[pos_in_map] & VISIBLE)
261 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
263 if ( left_angle <= shadow_i->left_angle
264 && right_angle >= shadow_i->right_angle)
266 fov_map[pos_in_map] = HIDDEN;
271 if ('X' == world.map.cells[pos_in_map])
273 if (!try_merging_angles(left_angle, right_angle, shadows))
275 struct shadow_angle * shadow;
276 shadow = try_malloc(sizeof(struct shadow_angle), __func__);
277 shadow->left_angle = left_angle;
278 shadow->right_angle = right_angle;
282 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
286 shadow_i->next = shadow;
298 static void free_angles(struct shadow_angle * angles)
302 free_angles(angles->next);
309 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
310 struct yx_uint8 * test_pos,
311 struct shadow_angle ** shadows)
313 int32_t left_angle_uncorrected = ((CIRCLE / 12) / dist)
314 - ((hex_i * (CIRCLE / 6)) / dist);
315 int32_t right_angle_uncorrected = left_angle_uncorrected
316 - (CIRCLE / (6 * dist));
317 uint32_t left_angle = correct_angle(left_angle_uncorrected);
318 uint32_t right_angle = correct_angle(right_angle_uncorrected);
319 uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
320 uint16_t pos_in_map = yx_to_map_pos(test_pos);
321 set_shadow(left_angle, right_angle_1st, shadows, pos_in_map, fov_map);
322 if (right_angle_1st != right_angle)
325 set_shadow(left_angle, right_angle, shadows, pos_in_map, fov_map);
331 extern uint8_t * build_fov_map(struct Thing * eye)
333 uint32_t map_size = world.map.length * world.map.length;
334 uint8_t * fov_map = try_malloc(map_size, __func__);
335 memset(fov_map, VISIBLE, map_size);
336 struct yx_uint8 test_pos = eye->pos;
337 struct shadow_angle * shadows = NULL;
338 char * circle_dirs = "xswedc";
340 uint8_t first_round, circle_on_map;
341 for (first_round = 1, dist = 1, circle_on_map = 1; circle_on_map; dist++)
345 mv_yx_in_dir_legal('c', &test_pos);
348 uint8_t i_dir = first_round = circle_on_map = 0;
349 uint16_t i_dist, hex_i;
350 for (hex_i = 0, i_dist = 1; hex_i < 6 * dist; i_dist++, hex_i++)
352 if (mv_yx_in_dir_legal(dir, &test_pos))
354 eval_position(dist, hex_i, fov_map, &test_pos, &shadows);
357 dir = circle_dirs[i_dir];
365 mv_yx_in_dir_wrap(0, NULL, 1);
366 free_angles(shadows);