1 #include <stddef.h> /* NULL */
2 #include <stdint.h> /* ?(u)int(8|16|32)_t, ?(U)INT8_(MIN|MAX) */
3 #include <stdlib.h> /* free, malloc */
4 #include <string.h> /* memset */
6 /* Number of degrees a circle is divided into. The greater it is, the greater
7 * the angle precision. But make it one whole zero larger and bizarre FOV bugs
8 * appear on large maps, probably due to value overflows (TODO: more research!).
10 #define CIRCLE 3600000
12 /* Angle of a shadow. */
15 struct shadow_angle * next;
20 /* To be used as temporary storage for world map array. */
21 static char * worldmap = NULL;
23 /* Coordinate for maps of max. 256x256 cells. */
30 /* Storage for map_length, set by set_maplength(). */
31 static uint16_t maplength = 0;
32 extern void set_maplength(uint16_t maplength_input)
34 maplength = maplength_input;
37 /* Pseudo-randomness seed for rrand(), set by seed_rrand(). */
38 static uint32_t seed = 0;
40 /* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
41 static void mv_yx_in_dir(char d, struct yx_uint8 * yx)
45 yx->x = yx->x + (yx->y % 2);
54 yx->x = yx->x + (yx->y % 2);
59 yx->x = yx->x - !(yx->y % 2);
68 yx->x = yx->x - !(yx->y % 2);
73 /* Move "yx" into hex direction "dir". Available hex directions are: 'e'
74 * (north-east), 'd' (east), 'c' (south-east), 'x' (south-west), 's' (west), 'w'
75 * (north-west). Returns 1 if the move was legal, 0 if not, and -1 when internal
76 * wrapping limits were exceeded.
78 * A move is legal if "yx" ends up within the the map and the original wrap
79 * space. The latter is left to a neighbor wrap space if "yx" moves beyond the
80 * minimal (0) or maximal (UINT8_MAX) column or row of possible map space – in
81 * which case "yx".y or "yx".x will snap to the respective opposite side. The
82 * current wrapping state is kept between successive calls until a "yx" of NULL
83 * is passed, in which case the function does nothing but zero the wrap state.
84 * Successive wrapping may move "yx" several wrap spaces into either direction,
85 * or return it into the original wrap space.
87 static int8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
89 static int8_t wrap_west_east = 0;
90 static int8_t wrap_north_south = 0;
93 wrap_west_east = wrap_north_south = 0;
96 if ( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
97 || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south)
101 struct yx_uint8 original = *yx;
102 mv_yx_in_dir(dir, yx);
103 if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
107 else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
111 if (('w' == dir || 'e' == dir) && yx->y > original.y)
115 else if (('x' == dir || 'c' == dir) && yx->y < original.y)
119 if ( !wrap_west_east && !wrap_north_south
120 && yx->x < maplength && yx->y < maplength)
127 /* Wrapper around mv_yx_in_dir_legal() that stores new coordinate in res_y/x,
128 * (return with result_y/x()), and immediately resets the wrapping.
130 static uint8_t res_y = 0;
131 static uint8_t res_x = 0;
132 extern uint8_t mv_yx_in_dir_legal_wrap(char dir, uint8_t y, uint8_t x)
137 uint8_t result = mv_yx_in_dir_legal(dir, &yx);
138 mv_yx_in_dir_legal(0, NULL);
143 extern uint8_t result_y()
147 extern uint8_t result_x()
152 /* With set_seed set, set seed global to seed_input. In any case, return it. */
153 extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input)
162 /* Return 16-bit number pseudo-randomly generated via Linear Congruential
163 * Generator algorithm with some proven constants. Use instead of any rand() to
164 * ensure portability of the same pseudo-randomness across systems.
166 extern uint16_t rrand()
167 { /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
168 seed = ((seed * 1103515245) + 12345) % 4294967296;
169 return (seed >> 16); /* Ignore less random least significant bits. */
172 /* Free shadow angles list "angles". */
173 static void free_angles(struct shadow_angle * angles)
177 free_angles(angles->next);
182 /* Recalculate angle < 0 or > CIRCLE to a value between these two limits. */
183 static uint32_t correct_angle(int32_t angle)
187 angle = angle + CIRCLE;
189 while (angle > CIRCLE)
191 angle = angle - CIRCLE;
196 /* Try merging the angle between "left_angle" and "right_angle" to "shadow" if
197 * it meets the shadow from the right or the left. Returns 1 on success, else 0.
199 static uint8_t try_merge(struct shadow_angle * shadow,
200 uint32_t left_angle, uint32_t right_angle)
202 if ( shadow->right_angle <= left_angle + 1
203 && shadow->right_angle >= right_angle)
205 shadow->right_angle = right_angle;
207 else if ( shadow->left_angle + 1 >= right_angle
208 && shadow->left_angle <= left_angle)
210 shadow->left_angle = left_angle;
219 /* Try merging the shadow angle between "left_angle" and "right_angle" into an
220 * existing shadow angle in "shadows". On success, see if this leads to any
221 * additional shadow angle overlaps and merge these accordingly. Return 1 on
224 static uint8_t try_merging_angles(uint32_t left_angle, uint32_t right_angle,
225 struct shadow_angle ** shadows)
227 uint8_t angle_merge = 0;
228 struct shadow_angle * shadow;
229 for (shadow = *shadows; shadow; shadow = shadow->next)
231 if (try_merge(shadow, left_angle, right_angle))
238 struct shadow_angle * shadow1;
239 for (shadow1 = *shadows; shadow1; shadow1 = shadow1->next)
241 struct shadow_angle * last_shadow = NULL;
242 struct shadow_angle * shadow2;
243 for (shadow2 = *shadows; shadow2; shadow2 = shadow2->next)
245 if ( shadow1 != shadow2
246 && try_merge(shadow1, shadow2->left_angle,
247 shadow2->right_angle))
249 struct shadow_angle * to_free = shadow2;
252 last_shadow->next = shadow2->next;
253 shadow2 = last_shadow;
257 *shadows = shadow2->next;
262 last_shadow = shadow2;
269 /* To "shadows", add shadow defined by "left_angle" and "right_angle", either as
270 * new entry or as part of an existing shadow (swallowed whole or extending it).
271 * Return 1 on malloc error, else 0.
273 static uint8_t set_shadow(uint32_t left_angle, uint32_t right_angle,
274 struct shadow_angle ** shadows)
276 struct shadow_angle * shadow_i;
277 if (!try_merging_angles(left_angle, right_angle, shadows))
279 struct shadow_angle * shadow;
280 shadow = malloc(sizeof(struct shadow_angle));
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;
304 /* Test whether angle between "left_angle" and "right_angle", or at least
305 * "middle_angle", is captured inside one of the shadow angles in "shadows". If
306 * so, set hex in "fov_map" indexed by "pos_in_map" to 'H'. If the whole angle
307 * and not just "middle_angle" is captured, return 1. Any other case: 0.
309 static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
310 uint32_t middle_angle, struct shadow_angle ** shadows,
311 uint16_t pos_in_map, char * fov_map)
313 struct shadow_angle * shadow_i;
314 if (fov_map[pos_in_map] == 'v')
316 for (shadow_i = *shadows; shadow_i; shadow_i = shadow_i->next)
318 if ( left_angle <= shadow_i->left_angle
319 && right_angle >= shadow_i->right_angle)
321 fov_map[pos_in_map] = 'H';
324 if ( middle_angle < shadow_i->left_angle
325 && middle_angle > shadow_i->right_angle)
327 fov_map[pos_in_map] = 'H';
334 /* Evaluate map position "test_pos" in distance "dist" to the view origin, and
335 * on the circle of that distance to the origin on hex "hex_i" (as counted from
336 * the circle's rightmost point), for setting shaded hexes in "fov_map" and
337 * potentially adding a new shadow to linked shadow angle list "shadows".
338 * Return 1 on malloc error, else 0.
340 static uint8_t eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
341 struct yx_uint8 * test_pos,
342 struct shadow_angle ** shadows)
344 int32_t left_angle_uncorrected = ((CIRCLE / 12) / dist)
345 - (hex_i * (CIRCLE / 6) / dist);
346 int32_t right_angle_uncorrected = left_angle_uncorrected
347 - (CIRCLE / (6 * dist));
348 uint32_t left_angle = correct_angle(left_angle_uncorrected);
349 uint32_t right_angle = correct_angle(right_angle_uncorrected);
350 uint32_t right_angle_1st = right_angle > left_angle ? 0 : right_angle;
351 uint32_t middle_angle = 0;
354 middle_angle = right_angle + ((left_angle - right_angle) / 2);
356 uint16_t pos_in_map = test_pos->y * maplength + test_pos->x;
357 uint8_t all_shaded = shade_hex(left_angle, right_angle_1st, middle_angle,
358 shadows, pos_in_map, fov_map);
359 if (!all_shaded && 'X' == worldmap[pos_in_map])
361 if (set_shadow(left_angle, right_angle_1st, shadows))
365 if (right_angle_1st != right_angle)
368 if (set_shadow(left_angle, right_angle, shadows))
377 /* Update field of view in "fovmap" of "worldmap_input" as seen from "y"/"x".
378 * Return 1 on malloc error, else 0.
380 extern uint8_t build_fov_map(uint8_t y, uint8_t x,
381 char * fovmap, char * worldmap_input)
383 worldmap = worldmap_input;
384 struct shadow_angle * shadows = NULL;
385 struct yx_uint8 test_pos;
388 char * circledirs_string = "xswedc";
390 uint8_t circle_is_on_map;
391 for (circle_i = 1, circle_is_on_map = 1; circle_is_on_map; circle_i++)
393 circle_is_on_map = 0;
394 if (1 < circle_i) /* All circles but the 1st are */
395 { /* moved into starting from a */
396 mv_yx_in_dir_legal('c', &test_pos);/* previous circle's last hex, */
397 } /* i.e. from the upper left. */
398 char dir_char = 'd'; /* Circle's 1st hex is entered by rightward move.*/
399 uint8_t dir_char_pos_in_circledirs_string = UINT8_MAX;
400 uint16_t dist_i, hex_i;
401 for (hex_i=0, dist_i=circle_i; hex_i < 6 * circle_i; dist_i++, hex_i++)
403 if (circle_i < dist_i)
406 dir_char=circledirs_string[++dir_char_pos_in_circledirs_string];
408 if (mv_yx_in_dir_legal(dir_char, &test_pos))
410 if (eval_position(circle_i, hex_i, fovmap, &test_pos, &shadows))
414 circle_is_on_map = 1;
418 mv_yx_in_dir_legal(0, NULL);
419 free_angles(shadows);