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 <math.h> /* pow() */
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int32_t, UINT8_MAX */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* memset() */
14 #include "../common/rexit.h" /* exit_trouble() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "map.h" /* mv_yx_in_dir_legal(), init_empty_map() */
18 #include "rrand.h" /* rrand() */
19 #include "things.h" /* Thing, ThingInMemory, add_thing_to_memory_map() */
20 #include "world.h" /* world */
24 /* Number of degrees a circle is divided into. The greater it is, the greater
25 * the angle precision. But make it one whole zero larger and bizarre FOV bugs
26 * appear on large maps, probably due to value overflows (TODO: more research!).
28 #define CIRCLE 3600000
32 /* Angle of a shadow. */
35 struct shadow_angle * next;
42 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
43 static uint32_t correct_angle(int32_t angle);
45 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
46 * it meets the shadow from the right or the left. Returns 1 on success, else 0.
48 static uint8_t try_merge(struct shadow_angle * shadow,
49 uint32_t left_angle, uint32_t right_angle);
51 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
52 * existing shadow angle in "shadows". On success, see if this leads to any
53 * additional shadow angle overlaps and merge these accordingly. Return 1 on
56 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
57 struct shadow_angle ** shadows);
59 /* Test whether angle between "left_angle" and "right_angle", or at least
60 * "middle_angle", is captured inside one of the shadow angles in "shadows". If
61 * so, set hex in "fov_map" indexed by "pos_in_map" to 'H'. If the whole angle
62 * and not just "middle_angle" is captured, return 1. Any other case: 0.
64 static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
65 uint32_t middle_angle, struct shadow_angle ** shadows,
66 uint16_t pos_in_map, char * fov_map);
68 /* To "shadows", add shadow defined by "left_angle" and "right_angle", either as
69 * new entry or as part of an existing shadow (swallowed whole or extending it).
71 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
72 struct shadow_angle ** shadows);
74 /* Free shadow angles list "angles". */
75 static void free_angles(struct shadow_angle * angles);
77 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
78 * on the circle of that distance to the origin on hex "hex_i" (as counted from
79 * the circle's rightmost point), for setting shaded hexes in "fov_map" and
80 * potentially adding a new shadow to linked shadow angle list "shadows".
82 static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
83 struct yx_uint8 * test_pos,
84 struct shadow_angle ** shadows);
86 /* Update "t_eye"'s things-on-map memory by removing from its .t_mem all
87 * memorized thing in FOV, and adding inanimate things in FOV to it.
89 static void add_things_to_map_memory(struct Thing * t_eye);
93 static uint32_t correct_angle(int32_t angle)
97 angle = angle + CIRCLE;
99 while (angle > CIRCLE)
101 angle = angle - CIRCLE;
108 static uint8_t try_merge(struct shadow_angle * shadow,
109 uint32_t left_angle, uint32_t right_angle)
111 if ( shadow->right_angle <= left_angle + 1
112 && shadow->right_angle >= right_angle)
114 shadow->right_angle = right_angle;
116 else if ( shadow->left_angle + 1 >= right_angle
117 && shadow->left_angle <= left_angle)
119 shadow->left_angle = left_angle;
130 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
131 struct shadow_angle ** shadows)
133 uint8_t angle_merge = 0;
134 struct shadow_angle * shadow;
135 for (shadow = *shadows; shadow; shadow = shadow->next)
137 if (try_merge(shadow, left_angle, right_angle))
144 struct shadow_angle * shadow1;
145 for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
147 struct shadow_angle * last_shadow = NULL;
148 struct shadow_angle * shadow2;
149 for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
151 if ( shadow1 != shadow2
152 && try_merge(shadow1, shadow2->left_angle,
153 shadow2->right_angle))
155 struct shadow_angle * to_free = shadow2;
158 last_shadow->next = shadow2->next;
159 shadow2 = last_shadow;
163 *shadows = shadow2->next;
168 last_shadow = shadow2;
177 static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
178 uint32_t middle_angle, struct shadow_angle ** shadows,
179 uint16_t pos_in_map, char * fov_map)
181 struct shadow_angle * shadow_i;
182 if (fov_map[pos_in_map] == 'v')
184 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
186 if ( left_angle <= shadow_i->left_angle
187 && right_angle >= shadow_i->right_angle)
189 fov_map[pos_in_map] = 'H';
192 if ( middle_angle < shadow_i->left_angle
193 && middle_angle > shadow_i->right_angle)
195 fov_map[pos_in_map] = 'H';
204 static void set_shadow(uint32_t left_angle, uint32_t right_angle,
205 struct shadow_angle ** shadows)
207 struct shadow_angle * shadow_i;
208 if (!try_merging_angles(left_angle, right_angle, shadows))
210 struct shadow_angle * shadow;
211 shadow = try_malloc(sizeof(struct shadow_angle), __func__);
212 shadow->left_angle = left_angle;
213 shadow->right_angle = right_angle;
217 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
221 shadow_i->next = shadow;
232 static void free_angles(struct shadow_angle * angles)
236 free_angles(angles->next);
243 static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
244 struct yx_uint8 * test_pos,
245 struct shadow_angle ** shadows)
247 int32_t left_angle_uncorrected = ((CIRCLE / 12) / dist)
248 - (hex_i * (CIRCLE / 6) / dist);
249 int32_t right_angle_uncorrected = left_angle_uncorrected
250 - (CIRCLE / (6 * dist));
251 uint32_t left_angle = correct_angle(left_angle_uncorrected);
252 uint32_t right_angle = correct_angle(right_angle_uncorrected);
253 uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
254 uint32_t middle_angle = 0;
257 middle_angle = right_angle + ((left_angle - right_angle) / 2);
259 uint16_t pos_in_map = test_pos->y * world.map.length + test_pos->x;
260 uint8_t all_shaded = shade_hex(left_angle, right_angle_1st, middle_angle,
261 shadows, pos_in_map, fov_map);
262 if (!all_shaded && 'X' == world.map.cells[pos_in_map])
264 set_shadow(left_angle, right_angle_1st, shadows);
265 if (right_angle_1st != right_angle)
268 set_shadow(left_angle, right_angle, shadows);
275 static void add_things_to_map_memory(struct Thing * t_eye)
277 struct ThingInMemory * tm = t_eye->t_mem;
278 struct ThingInMemory * tm_prev = NULL;
279 struct ThingInMemory * tm_next = NULL;
280 for (; tm; tm = tm_next)
283 if ('v' == t_eye->fov_map[tm->pos.y * world.map.length + tm->pos.x])
287 tm_prev->next = tm->next;
291 t_eye->t_mem = tm->next;
298 struct Thing * t = world.things;
299 for (; t; t = t->next)
302 && 'v' == t_eye->fov_map[t->pos.y * world.map.length + t->pos.x])
304 add_thing_to_memory_map(t_eye, t->type, t->pos.y, t->pos.x);
311 extern void update_map_memory(struct Thing * t_eye, uint8_t age_map)
315 init_empty_map(&(t_eye->mem_map));
317 if (!t_eye->mem_depth_map)
319 init_empty_map(&(t_eye->mem_depth_map));
322 for (i = 0; i < (uint32_t) (world.map.length * world.map.length); i++)
324 if ('v' == t_eye->fov_map[i])
326 t_eye->mem_depth_map[i] = '0';
327 if (' ' == t_eye->mem_map[i])
329 t_eye->mem_map[i] = world.map.cells[i];
334 '0' <= t_eye->mem_depth_map[i] && '9' > t_eye->mem_depth_map[i]
335 && !(rrand() % (uint16_t) pow(2, t_eye->mem_depth_map[i] - 48)))
337 t_eye->mem_depth_map[i]++;
340 add_things_to_map_memory(t_eye);
345 extern void build_fov_map(struct Thing * t)
347 uint32_t map_size = world.map.length * world.map.length;
348 t->fov_map = t->fov_map ? t->fov_map : try_malloc(map_size, __func__);
349 memset(t->fov_map, 'v', map_size);
350 struct shadow_angle * shadows = NULL;
351 struct yx_uint8 test_pos = t->pos;
352 char * circledirs_string = "xswedc";
354 uint8_t circle_is_on_map;
355 for (circle_i = 1, circle_is_on_map = 1; circle_is_on_map; circle_i++)
357 circle_is_on_map = 0;
358 if (1 < circle_i) /* All circles but the 1st are */
359 { /* moved into starting from a */
360 mv_yx_in_dir_legal('c', &test_pos);/* previous circle's last hex, */
361 } /* i.e. from the upper left. */
362 char dir_char = 'd'; /* Circle's 1st hex is entered by rightward move.*/
363 uint8_t dir_char_pos_in_circledirs_string = UINT8_MAX;
364 uint16_t dist_i, hex_i;
365 for (hex_i=0, dist_i=circle_i; hex_i < 6 * circle_i; dist_i++, hex_i++)
367 if (circle_i < dist_i)
370 dir_char=circledirs_string[++dir_char_pos_in_circledirs_string];
372 if (mv_yx_in_dir_legal(dir_char, &test_pos))
374 eval_position(circle_i, hex_i, t->fov_map, &test_pos, &shadows);
375 circle_is_on_map = 1;
379 mv_yx_in_dir_legal(0, NULL);
380 free_angles(shadows);