From f92aa640ce2f2859cc7ba8207ed6d20ed1af4a19 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 14 Sep 2022 22:54:44 +0300 Subject: [PATCH] add visibility table tool --- tools/vistab.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 tools/vistab.c diff --git a/tools/vistab.c b/tools/vistab.c new file mode 100644 index 0000000..6341238 --- /dev/null +++ b/tools/vistab.c @@ -0,0 +1,117 @@ +#include +#include +#include + +#define NUM_DIRS 8 +#define MAX_DIST 4 +#define FOV 60 + +struct cell { + int dx, dy; +}; + +void calc_vis(int dir); +int is_visible(float x, float y, float dir); + +int main(void) +{ + int i; + + printf("struct {int dx, dy;} visoffs[%d][32] = {\n", NUM_DIRS); + + for(i=0; idx * ca->dx + ca->dy * ca->dy; + int zb = cb->dx * cb->dx + cb->dy * cb->dy; + return zb - za; +} + +void calc_vis(int dir) +{ + int i, j, count, col; + struct cell vis[512]; + int x, y; + float theta = (float)(dir + 2) / NUM_DIRS * M_PI * 2.0f; + + fprintf(stderr, "DIR: %d (angle: %g)\n", dir, theta * 180.0f / M_PI); + count = 0; + for(i=0; i 72) { + fputs(",\n\t ", stdout); + col = 5; + } else { + fputs(", ", stdout); + col += 2; + } + } + } + fputs(dir < NUM_DIRS - 1 ? "},\n" : "}\n", stdout); +} + +#define RAD 1.42 + +float ptlinedist(float x, float y, float dir) +{ + return -y * cos(dir) + sin(dir) * x; +} + +int is_visible(float x, float y, float dir) +{ + static const char voffs[][2] = {{-0.5, -0.5}, {0.5, -0.5}, {0.5, 0.5}, {-0.5, 0.5}}; + float d0, d1; + int i; + float hfov = 0.5f * FOV * M_PI / 180.0f; + + d0 = ptlinedist(x, y, dir - hfov); + d1 = ptlinedist(x, y, dir + hfov); + if(d0 >= 0 && d1 <= 0) { + return 1; + } + + for(i=0; i<4; i++) { + d0 = ptlinedist(x + voffs[i][0], y + voffs[i][1], dir - hfov); + d1 = ptlinedist(x + voffs[i][0], y + voffs[i][1], dir + hfov); + + if(d0 >= 0 && d1 <= 0) { + return 1; + } + } + return 0; +} -- 1.7.10.4