1 /* src/server/field_of_view.c
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.
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 */
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!).
26 #define CIRCLE 3600000
30 /* Angle of a shadow. */
33 struct shadow_angle * next;
40 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
41 static uint32_t correct_angle(int32_t angle);
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.
46 static uint8_t try_merge(struct shadow_angle * shadow,
47 uint32_t left_angle, uint32_t right_angle);
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
54 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
55 struct shadow_angle ** shadows);
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.
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);
66 /* Free shadow angles list "angles". */
67 static void free_angles(struct shadow_angle * angles);
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".
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);
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.
81 static void update_map_memory(struct Thing * t, uint32_t map_size);
85 static uint32_t correct_angle(int32_t angle)
89 angle = angle + CIRCLE;
91 while (angle > CIRCLE)
93 angle = angle - CIRCLE;
100 static uint8_t try_merge(struct shadow_angle * shadow,
101 uint32_t left_angle, uint32_t right_angle)
103 if ( shadow->right_angle <= left_angle + 1
104 && shadow->right_angle >= right_angle)
106 shadow->right_angle = right_angle;
108 else if ( shadow->left_angle + 1 >= right_angle
109 && shadow->left_angle <= left_angle)
111 shadow->left_angle = left_angle;
122 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
123 struct shadow_angle ** shadows)
125 uint8_t angle_merge = 0;
126 struct shadow_angle * shadow;
127 for (shadow = *shadows; shadow; shadow = shadow->next)
129 if (try_merge(shadow, left_angle, right_angle))
136 struct shadow_angle * shadow1;
137 for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
139 struct shadow_angle * last_shadow = NULL;
140 struct shadow_angle * shadow2;
141 for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
143 if ( shadow1 != shadow2
144 && try_merge(shadow1, shadow2->left_angle,
145 shadow2->right_angle))
147 struct shadow_angle * to_free = shadow2;
150 last_shadow->next = shadow2->next;
151 shadow2 = last_shadow;
155 *shadows = shadow2->next;
160 last_shadow = shadow2;
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)
173 struct shadow_angle * shadow_i;
174 if (fov_map[pos_in_map] == 'v')
176 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
178 if ( left_angle <= shadow_i->left_angle
179 && right_angle >= shadow_i->right_angle)
181 fov_map[pos_in_map] = 'H';
184 if ( middle_angle < shadow_i->left_angle
185 && middle_angle > shadow_i->right_angle)
187 fov_map[pos_in_map] = 'H';
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).
199 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
200 struct shadow_angle ** shadows)
202 struct shadow_angle * shadow_i;
203 if (!try_merging_angles(left_angle, right_angle, shadows))
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;
212 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
216 shadow_i->next = shadow;
227 static void free_angles(struct shadow_angle * angles)
231 free_angles(angles->next);
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)
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;
252 middle_angle = right_angle + ((left_angle - right_angle) / 2);
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])
259 set_shadow(left_angle, right_angle_1st, shadows);
260 if (right_angle_1st != right_angle)
263 set_shadow(left_angle, right_angle, shadows);
270 static void update_map_memory(struct Thing * t_eye, uint32_t map_size)
274 t_eye->mem_map = try_malloc(map_size, __func__);
275 memset(t_eye->mem_map, ' ', map_size);
278 for (i = 0; i < map_size; i++)
280 if (' ' == t_eye->mem_map[i] && t_eye->fov_map[i] == 'v')
282 t_eye->mem_map[i] = world.map.cells[i];
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)
291 if ('v' == t_eye->fov_map[tm->pos.y * world.map.length + tm->pos.x])
295 tm_prev->next = tm->next;
299 t_eye->t_mem = tm->next;
306 struct Thing * t = world.things;
307 for (; t; t = t->next)
310 && 'v' == t_eye->fov_map[t->pos.y * world.map.length + t->pos.x])
312 add_thing_to_memory_map(t_eye, t->type, t->pos.y, t->pos.x);
319 extern void build_fov_map(struct Thing * t)
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";
328 uint8_t circle_is_on_map;
329 for (circle_i = 1, circle_is_on_map = 1; circle_is_on_map; circle_i++)
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++)
341 if (circle_i < dist_i)
344 dir_char=circledirs_string[++dir_char_pos_in_circledirs_string];
346 if (mv_yx_in_dir_legal(dir_char, &test_pos))
348 eval_position(circle_i, hex_i, t->fov_map, &test_pos, &shadows);
349 circle_is_on_map = 1;
353 mv_yx_in_dir_legal(0, NULL);
354 free_angles(shadows);
355 update_map_memory(t, map_size);