|
| 1 | +#include "fov_pascal.h" |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#include "map_inline.h" |
| 8 | + |
| 9 | +static void pascal_scan_line( |
| 10 | + const TCODFOV_Map2D* __restrict transparent, // Input transparency |
| 11 | + TCODFOV_Map2D* __restrict out, |
| 12 | + int pov_x, // X position |
| 13 | + int scan_y, // Y position |
| 14 | + int iteration, // Distance from pov_y, always `abs(scan_y - pov_y)` |
| 15 | + const double* __restrict prev_row, // Previous row data |
| 16 | + double* __restrict next_row, // Active row to be written |
| 17 | + int x_begin, |
| 18 | + int x_end, |
| 19 | + int_fast8_t x_step // Step direction: -1 or 1 |
| 20 | +) { |
| 21 | + for (int x = x_begin; x != x_end; x += x_step) { |
| 22 | + int_fast8_t casts = 0; // Number of tiles cast from |
| 23 | + double visiblity = 0.0; |
| 24 | + |
| 25 | + // Cast diagonal |
| 26 | + ++casts; |
| 27 | + visiblity += prev_row[x - x_step]; |
| 28 | + |
| 29 | + if (pov_x - iteration <= x && x <= pov_x + iteration) { // Cast from previous row |
| 30 | + ++casts; |
| 31 | + visiblity += prev_row[x]; |
| 32 | + } |
| 33 | + if (x <= pov_x - iteration || pov_x + iteration <= x) { // Cast from adjacent tile |
| 34 | + ++casts; |
| 35 | + visiblity += next_row[x - x_step]; |
| 36 | + } |
| 37 | + visiblity *= (1.0 / (double)casts); |
| 38 | + TCODFOV_map2d_set_d(out, x, scan_y, visiblity); |
| 39 | + if (visiblity) visiblity *= TCODFOV_map2d_get_d(transparent, x, scan_y); |
| 40 | + next_row[x] = visiblity; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// @brief Compute the next row of visiblity, continue until out-of-bounds |
| 45 | +static void pascal_scan_next_row( |
| 46 | + const TCODFOV_Map2D* __restrict transparent, // Input transparency |
| 47 | + TCODFOV_Map2D* __restrict out, |
| 48 | + int pov_x, // X position |
| 49 | + int scan_y, // Y position |
| 50 | + int_fast8_t scan_dir, // Y step direction, -1 or 1 |
| 51 | + int iteration, // Distance from pov_y, always `abs(scan_y - pov_y)` |
| 52 | + double* __restrict prev_row, // Previous row light-level |
| 53 | + double* __restrict next_row // This row light-level |
| 54 | +) { |
| 55 | + if (scan_y < 0 || scan_y >= TCODFOV_map2d_get_height(out)) return; // Finished, out-of-bounds |
| 56 | + |
| 57 | + // Compute tile at pov_x, scan_y |
| 58 | + TCODFOV_map2d_set_d(out, pov_x, scan_y, prev_row[pov_x]); |
| 59 | + next_row[pov_x] = prev_row[pov_x] * TCODFOV_map2d_get_d(transparent, pov_x, scan_y); |
| 60 | + |
| 61 | + // Compute tiles on sides of active row |
| 62 | + pascal_scan_line(transparent, out, pov_x, scan_y, iteration, prev_row, next_row, pov_x - 1, -1, -1); |
| 63 | + pascal_scan_line( |
| 64 | + transparent, out, pov_x, scan_y, iteration, prev_row, next_row, pov_x + 1, TCODFOV_map2d_get_width(out), 1); |
| 65 | + |
| 66 | + // Swap next_row and prev_row and continue |
| 67 | + pascal_scan_next_row(transparent, out, pov_x, scan_y + scan_dir, scan_dir, iteration + 1, next_row, prev_row); |
| 68 | +} |
| 69 | + |
| 70 | +static void pascal_scan_init( |
| 71 | + const TCODFOV_Map2D* __restrict transparent, // Input transparency |
| 72 | + TCODFOV_Map2D* __restrict out, |
| 73 | + int pov_x, |
| 74 | + int pov_y, |
| 75 | + double* __restrict row // Holds the light level this tile will cast onto others |
| 76 | +) { |
| 77 | + // Source always starts at 1.0 |
| 78 | + TCODFOV_map2d_set_d(out, pov_x, pov_y, 1.0); |
| 79 | + // But the data stored in row is multiplied by the maps transparency |
| 80 | + row[pov_x] = TCODFOV_map2d_get_d(transparent, pov_x, pov_y); |
| 81 | + double visiblity = row[pov_x]; // Visiblity of the active cast |
| 82 | + for (int x = pov_x - 1; x >= 0; --x) { |
| 83 | + // Output visiblity is the light level cast from the previous tile. |
| 84 | + // But the value stored in row is after the transparency is applied. |
| 85 | + TCODFOV_map2d_set_d(out, x, pov_y, visiblity); |
| 86 | + if (visiblity != 0) visiblity *= TCODFOV_map2d_get_d(transparent, x, pov_y); |
| 87 | + row[x] = visiblity; |
| 88 | + } |
| 89 | + // Repeat for the other direction |
| 90 | + visiblity = row[pov_x]; |
| 91 | + for (int x = pov_x + 1; x < TCODFOV_map2d_get_width(out); ++x) { |
| 92 | + TCODFOV_map2d_set_d(out, x, pov_y, visiblity); |
| 93 | + if (visiblity != 0) visiblity *= TCODFOV_map2d_get_d(transparent, x, pov_y); |
| 94 | + row[x] = visiblity; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +TCODFOV_Error TCODFOV_pascal_diffusion_2d( |
| 99 | + const TCODFOV_Map2D* __restrict transparent, TCODFOV_Map2D* __restrict out, int pov_x, int pov_y) { |
| 100 | + // Holds the light-level to cast to adjacent rows |
| 101 | + double* __restrict row = malloc(TCODFOV_map2d_get_width(out) * 3 * sizeof(*row)); |
| 102 | + |
| 103 | + if (!row) { |
| 104 | + TCODFOV_set_errorv("Out of memory."); |
| 105 | + return TCODFOV_E_OUT_OF_MEMORY; |
| 106 | + } |
| 107 | + |
| 108 | + double* __restrict row2 = row + TCODFOV_map2d_get_width(out); |
| 109 | + double* __restrict row3 = row + TCODFOV_map2d_get_width(out) * 2; |
| 110 | + |
| 111 | + pascal_scan_init(transparent, out, pov_x, pov_y, row); |
| 112 | + |
| 113 | + memcpy(row2, row, TCODFOV_map2d_get_width(out) * sizeof(*row)); |
| 114 | + pascal_scan_next_row(transparent, out, pov_x, pov_y - 1, -1, 1, row2, row3); |
| 115 | + memcpy(row2, row, TCODFOV_map2d_get_width(out) * sizeof(*row)); |
| 116 | + pascal_scan_next_row(transparent, out, pov_x, pov_y + 1, 1, 1, row2, row3); |
| 117 | + return TCODFOV_E_OK; |
| 118 | +} |
0 commit comments