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. */
17 #define CIRCLE 36000000
21 /* Values for mv_yx_in_dir_wrap()'s wrapping directory memory. */
32 /* Angle of a shadow. */
35 struct shadow_angle * next;
42 /* Move "yx" into hex direction "d". */
43 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
45 /* Move "yx" into hex direction "d". If this moves "yx" beyond the minimal (0)
46 * or maximal (UINT8_MAX) column or row, it wraps to the opposite side. Such
47 * wrapping is returned as a wraps enum value and stored, so that further calls
48 * to move "yx" back into the opposite direction may unwrap it again. Pass an
49 * "unwrap" of !0 to re-set the internal wrap memory to 0.
51 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap);
53 /* Wrapper to "mv_yx_in_dir_wrap()", returns 1 if the wrapped function moved
54 * "yx" within the wrap borders and the map size, else 0.
56 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx);
58 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
59 static uint32_t correct_angle(int32_t angle);
61 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
62 * it meets the shadow from the right or the left. Returns 1 on success, else 0.
64 static uint8_t try_merge(struct shadow_angle * shadow,
65 uint32_t left_angle, uint32_t right_angle);
67 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
68 * existing shadow angle in "shadows". On success, see if this leads to any
69 * additional shadow angle overlaps and merge these accordingly. Return 1 on
72 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
73 struct shadow_angle ** shadows);
75 /* If "pos_in_map" in angle between"left_angle" to "right_angle" to the viewing
76 * actor is in a shadow from the shadow list "shadows", mark it as HIDDEN on the
77 * "fov_map"; else, if the world map features a viewing obstacle on the world
78 * map, calculate its shadow angle to the viewer and add it to "shadows".
80 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
81 struct shadow_angle ** shadows, uint16_t pos_in_map,
84 /* Free shadow angles list "angles". */
85 static void free_angles(struct shadow_angle * angles);
87 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
88 * on the circle of that distance to the origin on hex "hex_i" (as counted from
89 * the circle's rightmost point), for setting shaded cells in "fov_map" and
90 * potentially adding a new shadow to linked shadow angle list "shadows".
92 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
93 struct yx_uint8 * test_pos,
94 struct shadow_angle ** shadows);
98 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
102 yx->x = yx->x + (yx->y % 2);
111 yx->x = yx->x + (yx->y % 2);
116 yx->x = yx->x - !(yx->y % 2);
125 yx->x = yx->x - !(yx->y % 2);
132 static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
134 static uint8_t wrap = 0;
140 struct yx_uint8 original;
143 mv_yx_in_hex_dir(d, yx);
144 if (strchr("edc", d) && yx->x < original.x)
146 wrap = wrap & WRAP_W ? wrap ^ WRAP_W : wrap | WRAP_E;
148 else if (strchr("xsw", d) && yx->x > original.x)
150 wrap = wrap & WRAP_E ? wrap ^ WRAP_E : wrap | WRAP_W;
152 if (strchr("we", d) && yx->y > original.y)
154 wrap = wrap & WRAP_S ? wrap ^ WRAP_S : wrap | WRAP_N;
156 else if (strchr("xc", d) && yx->y < original.y)
158 wrap = wrap & WRAP_N ? wrap ^ WRAP_N : wrap | WRAP_S;
165 static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
167 uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
168 if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
177 static uint32_t correct_angle(int32_t angle)
181 angle = angle + CIRCLE;
183 while (angle > CIRCLE)
185 angle = angle - CIRCLE;
192 static uint8_t try_merge(struct shadow_angle * shadow,
193 uint32_t left_angle, uint32_t right_angle)
195 if ( shadow->right_angle <= left_angle + 1
196 && shadow->right_angle >= right_angle)
198 shadow->right_angle = right_angle;
200 else if ( shadow->left_angle + 1 >= right_angle
201 && shadow->left_angle <= left_angle)
203 shadow->left_angle = left_angle;
214 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
215 struct shadow_angle ** shadows)
217 uint8_t angle_merge = 0;
218 struct shadow_angle * shadow;
219 for (shadow = *shadows; shadow; shadow = shadow->next)
221 if (try_merge(shadow, left_angle, right_angle))
228 struct shadow_angle * shadow1;
229 for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
231 struct shadow_angle * last_shadow = NULL;
232 struct shadow_angle * shadow2;
233 for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
235 if ( shadow1 != shadow2
236 && try_merge(shadow1, shadow2->left_angle,
237 shadow2->right_angle))
239 struct shadow_angle * to_free = shadow2;
242 last_shadow->next = shadow2->next;
243 shadow2 = last_shadow;
247 *shadows = shadow2->next;
252 last_shadow = shadow2;
261 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
262 struct shadow_angle ** shadows, uint16_t pos_in_map,
265 char * f_name = "set_shadow()";
266 struct shadow_angle * shadow_i;
267 if (fov_map[pos_in_map] & VISIBLE)
269 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
271 if ( left_angle <= shadow_i->left_angle
272 && right_angle >= shadow_i->right_angle)
274 fov_map[pos_in_map] = HIDDEN;
279 if ('X' == world.map.cells[pos_in_map])
281 if (!try_merging_angles(left_angle, right_angle, shadows))
283 struct shadow_angle * shadow;
284 shadow = try_malloc(sizeof(struct shadow_angle), f_name);
285 shadow->left_angle = left_angle;
286 shadow->right_angle = right_angle;
290 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
294 shadow_i->next = shadow;
306 static void free_angles(struct shadow_angle * angles)
310 free_angles(angles->next);
317 static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map,
318 struct yx_uint8 * test_pos,
319 struct shadow_angle ** shadows)
321 int32_t left_angle_uncorrected = ((CIRCLE / 12) / dist)
322 - ((hex_i * (CIRCLE / 6)) / dist);
323 int32_t right_angle_uncorrected = left_angle_uncorrected
324 - (CIRCLE / (6 * dist));
325 uint32_t left_angle = correct_angle(left_angle_uncorrected);
326 uint32_t right_angle = correct_angle(right_angle_uncorrected);
327 uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
328 uint16_t pos_in_map = yx_to_map_pos(test_pos);
329 set_shadow(left_angle, right_angle_1st, shadows, pos_in_map, fov_map);
330 if (right_angle_1st != right_angle)
333 set_shadow(left_angle, right_angle, shadows, pos_in_map, fov_map);
339 extern uint8_t * build_fov_map(struct Thing * eye)
341 char * f_name = "build_fov_map()";
342 uint32_t map_size = world.map.length * world.map.length;
343 uint8_t * fov_map = try_malloc(map_size, f_name);
344 memset(fov_map, VISIBLE, map_size);
345 struct yx_uint8 test_pos = eye->pos;
346 struct shadow_angle * shadows = NULL;
347 char * circle_dirs = "xswedc";
349 uint8_t first_round, circle_on_map;
350 for (first_round = 1, dist = 1, circle_on_map = 1; circle_on_map; dist++)
354 mv_yx_in_dir_legal('c', &test_pos);
357 uint8_t i_dir = first_round = circle_on_map = 0;
358 uint16_t i_dist, hex_i;
359 for (hex_i = 0, i_dist = 1; hex_i < 6 * dist; i_dist++, hex_i++)
361 if (mv_yx_in_dir_legal(dir, &test_pos))
363 eval_position(dist, hex_i, fov_map, &test_pos, &shadows);
366 dir = circle_dirs[i_dir];
374 mv_yx_in_dir_wrap(0, NULL, 1);
375 free_angles(shadows);