From bfb5c8f0664ea980461b9df0082da26349fbdfc8 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Thu, 10 Oct 2019 13:54:26 -0400 Subject: Revamp Tile Heightmap Collision so that it can do side wall collisions, and bottom ceiling collisions. I also made a new C based test program, which is much faster than the shell script. --- clld.c | 37 ++++++++--- test-mvmt.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test-mvmt.sh | 4 +- test-thm-mvmt.sh | 6 +- 4 files changed, 232 insertions(+), 12 deletions(-) create mode 100644 test-mvmt.c diff --git a/clld.c b/clld.c index 81954a6..07f3c8b 100644 --- a/clld.c +++ b/clld.c @@ -97,6 +97,7 @@ int main(int argc, char **argv) { * the process of getting collision data, to one for loop. */ struct colis tmp; + int hmpeak; /* Get collision type. */ char *buf = NULL; size_t size; @@ -161,6 +162,7 @@ int main(int argc, char **argv) { char *tmptok = strtok((i == 0) ? buf : NULL,","); tmptok = (tmptok == NULL) ? strtok(NULL,"\n") : tmptok; tmp.bitmask[i] = (uint8_t *)((atoi(tmptok) <= tmp.h) ? atoi(tmptok) : tmp.h); + hmpeak = (atoi(tmptok)+1 >= hmpeak) ? atoi(tmptok)+1 : hmpeak; } } @@ -249,24 +251,43 @@ int main(int argc, char **argv) { floor.b1 = floor.pos.y; floor.b2 = floor.pos.y+floor.h; + cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0; /* Check for a collision, on the y axis. */ - cursor.cly = ((cursor.b2 >= floor.b1-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= floor.b2-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; + cursor.cly = ((cursor.b2 >= (floor.b1-hmpeak-1)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak+1)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; int newy = 0; if (cursor.cly) { if (cursor.a2-floor.a1 < floor.h) { + cursor.yvel = 0; newy = (int)floor.bitmask[cursor.a2-floor.a1]; /* Is the Floor, really a floor? */ - if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && !floor.flp) - cursor.pos.y = floor.pos.y-(cursor.h-newy)-(int)cursor.yvel; + if (cursor.b2 > (floor.b2-hmpeak-1)-(int)cursor.yvel && floor.b2-cursor.b2 >= 0 && !floor.flp) + cursor.pos.y = (floor.b2-newy-1)-(int)cursor.yvel; + else if (cursor.b1 < floor.b2-(int)cursor.yvel && floor.b2-cursor.b2 < 0 && !floor.flp) + cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel; /* Is the Floor, actually a ceiling? */ - else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.flp) - cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel; + if (cursor.b1 < (floor.b1+hmpeak+1)-(int)cursor.yvel && floor.b1-cursor.b1 >= 0 && floor.flp) + cursor.pos.y = (floor.pos.y+newy+1)-(int)cursor.yvel; + else if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && floor.b1-cursor.b1 < 0 && cursor.yvel > 0 && floor.flp) + cursor.pos.y = (floor.pos.y-(int)cursor.yvel-1)-cursor.h; } } + if (cursor.clx) { + /* Is the Cursor coming from the left? */ + if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < 0 && !floor.flp && cursor.xvel > 0) + cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1; + else if (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b2-cursor.b2 < 0 && !floor.flp && cursor.xvel < 0) + cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1; + /* Is the Cursor coming from the right? */ + if (cursor.a1 < (floor.a2-cursor.w)-(int)cursor.xvel+1 && floor.b1-cursor.b1 < 0 && floor.flp && cursor.xvel < 0) + cursor.pos.x = (floor.a2-cursor.w)-(int)cursor.xvel+1; + else if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b1-cursor.b1 < 0 && floor.flp && cursor.xvel > 0) + cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1; + cursor.xvel = 0; + } cursor.gnded = (cursor.pos.y == floor.pos.y-(cursor.h-newy)) ? 1 : 0; - printf("%u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.cly, cursor.gnded, newy, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); + printf("%u\n%u\n%i\nfloor.b2-cursor.b2: %i, floor.b1-cursor.b1: %i\n%i, %i\n%07.06f, %07.06f\n", cursor.cly, cursor.gnded, newy, floor.b2-cursor.b2, floor.b1-cursor.b1, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; @@ -341,7 +362,7 @@ int main(int argc, char **argv) { printf("floor.x: %i, floor.y: %i\n", floor.pos.x, floor.pos.y); printf ("r: %i\n", (floor.pos.x+floor.w)-(int)cursor.xvel+1); } - printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); + printf("%u, %u\n%u\n%i, %i\n%07.06f, %07.06f\n\0", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; @@ -375,7 +396,7 @@ int main(int argc, char **argv) { printf("fx: %07.06f, fy: %07.06f\n", cursor.pos.fx, cursor.pos.fy); printf("floor.fx: %07.06f, floor.fy: %07.06f\n", floor.pos.fx, floor.pos.fy); } - printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); + printf("%u, %u\n%u\n%07.06f, %07.06f\n%07.06f, %07.06f\n\0", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); } } } diff --git a/test-mvmt.c b/test-mvmt.c new file mode 100644 index 0000000..952dd9d --- /dev/null +++ b/test-mvmt.c @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int match(const char *string, char *pattern) { + int status; + regex_t re; + if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) { + return(0); + } + status = regexec(&re, string, (size_t) 0, NULL, 0); + regfree(&re); + if (status != 0) { + return(0); + } + return(1); +} + +void clstdin() { + int stdincp = dup(STDIN_FILENO); + tcdrain(stdincp); + tcflush(stdincp, TCIFLUSH); + close(stdincp); +} + +int startpipe(char *path, char *input , char *output, ssize_t *size) { + int fd0[2]; + int fd1[2]; + pid_t pid; + if (pipe(fd0) < 0 || pipe(fd1) < 0) { + fprintf(stderr, "Error occured when piping.\n"); + return -2; + } + if ((pid = fork()) < 0) { + fprintf(stderr, "Error occured when forking.\n"); + return -3; + } else if (pid == 0) { + close(fd0[1]); + close(fd1[0]); + if (fd0[0] != STDIN_FILENO) { + if (dup2(fd0[0], STDIN_FILENO) != STDIN_FILENO) + fprintf(stderr, "Error occured while duplicating stdin.\n"); + close(fd0[0]); + } + if (fd1[1] != STDOUT_FILENO) { + if (dup2(fd1[1], STDOUT_FILENO) != STDOUT_FILENO) + fprintf(stderr, "Error occured while duplicating stdout.\n"); + close(fd1[1]); + } + /*fprintf(stderr,"%s\n", path);*/ + if (path == NULL) + fprintf(stderr, "oof, yet even more errors, this time with getcwd.\n"); + if (execl(path, "clld", (char *)0) < 0) { + fprintf(stderr, "System error has occured.\n"); + return -4; + } + return 0; + /*printf("%s\n", path);*/ + } else { + close(fd0[0]); + close(fd1[1]); + /*if (write(fd0[1], input, strlen(input)) != strlen(input)) + fprintf(stderr, "Error occured while trying to write to clld.\n"); + close(fd0[1]); + if ((*size = read(fd1[0], output, 129) < 0)) + fprintf(stderr, "Error occured while trying to read the output of clld.\n"); + close(fd1[0]); + if (size == 0) { + fprintf(stderr, "Child process has closed it's pipe.\n"); + return 0; + }*/ + write(fd0[1], input, strlen(input)); + usleep(2000); + close(fd0[1]); + *size = read(fd1[0], output, 129); + close(fd1[0]); + return 0; + /*printf("rv: %i\n", rv);*/ + /*printf("%s", output);*/ + } + return 0; +} + +void split(char *str, char **splitstr) { + char *p; + int i=0; + p = strtok(str,"\n"); + while(p!= NULL) { + /*printf("%s\n", p);*/ + splitstr[i] = malloc(strlen(p) + 1); + if (splitstr[i]) + strcpy(splitstr[i], p); + i++; + p = strtok (NULL, "\n"); + } +} + +int main (int argc, char **argv) { + uint8_t u = 0, d = 0, l = 0; + uint8_t r = 0; + int frame = 0; + double x1 = 224, y1 = 128; + int w1 = 16, h1 = 16; + double xvel = 0, yvel = 0; + int x2 = 256, y2 = 256; + int w2 = 64, h2 = 64; + int stat; + char *keys; + char *buf = malloc(8193); + FILE *fp; + fp = popen("actkbd -Ps", "r"); + printf("\033[2J"); + if (fp == NULL) { + fprintf(stderr, "Error occured when trying to run actkbd.\n"); + free(buf); + return 1; + } + while (fgets(buf, 8193, fp) != NULL) { + ssize_t size = 0; + char *tmp = malloc(4096); + char *input = malloc(2049); + char *path = malloc(4102); + char ln[129]; + frame = atoi(strtok(buf, " ")); + keys = strtok(NULL, " "); + + for(int ch = 0; ch 0) ? (u ? (yvel-=0.5) : (yvel-=0.25)) : (yvel=yvel)); + (l) ? (xvel-=0.25) : ((xvel < 0) ? (r ? (xvel+=0.5) : (xvel+=0.25)) : (xvel=xvel)); + (r) ? (xvel+=0.25) : ((xvel > 0) ? (l ? (xvel-=0.5) : (xvel-=0.25)) : (xvel=xvel)); + yvel = (yvel > 5) ? 5 : (yvel < -5 ? -5 : yvel); + xvel = (xvel > 5) ? 5 : (xvel < -5 ? -5 : xvel); + x1+=xvel; + y1+=yvel; + + printf("\033[1;1H\033[2K%s", keys); + printf( + "\033[E\033[2Kframe: %i, u: %u, d: %u, l: %u, r: %u" + "\033[E\033[2Kx1: %i, y1: %i" + "\033[E\033[2Kxvel: %07.06f, yvel: %07.06f" + "\n\033[2K", frame, u, d, l, r, (int)x1, (int)y1, xvel, yvel); + + if (match(keys, "KEY_Q")) { + printf("\033[2J\033[1;1H"); + free(buf); + free(tmp); + free(path); + free(input); + clstdin(); + clstdin(); + return 0; + } + free(tmp); + free(path); + free(input); + } + free(buf); +} diff --git a/test-mvmt.sh b/test-mvmt.sh index b53ed56..0f743ca 100755 --- a/test-mvmt.sh +++ b/test-mvmt.sh @@ -9,8 +9,8 @@ x2=256 y2=256 w1=16 h1=16 -w2=128 -h2=128 +w2=64 +h2=64 u=0 d=0 l=0 diff --git a/test-thm-mvmt.sh b/test-thm-mvmt.sh index ab3678e..2957c48 100755 --- a/test-thm-mvmt.sh +++ b/test-thm-mvmt.sh @@ -8,7 +8,8 @@ yspd=$yvel x2=256 y2=256 flip=0 -heightmap="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1" +#heightmap="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1" +heightmap="16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16" #heightmap="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" #heightmap="16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1" w1=16 @@ -29,8 +30,9 @@ while read frame key held; do [ "${key#*KEY_S}" != "$key" ] && negy="" && y1=$(echo "$y1+$yspd" | bc) [ "${key#*KEY_A}" != "$key" ] && negx="-" && x1=$(echo "$x1-$xspd" | bc) [ "${key#*KEY_D}" != "$key" ] && negx="" && x1=$(echo "$x1+$xspd" | bc) + printf "0\n$w1,$h1\n$x1,$y1\n$negx$xspd,$negy$yspd\n$w2,$h2\n$flip\n$heightmap\n$x2,$y2" | ./clld xy=$(printf "0\n$w1,$h1\n$x1,$y1\n$negx$xspd,$negy$yspd\n$w2,$h2\n$flip\n$heightmap\n$x2,$y2" | ./clld | tail -n2 | tr -d ',' | head -n1) x1=$(echo "$xy" | awk '{print $1}') y1=$(echo "$xy" | awk '{print $2}') - printf "\33[1;1H$x1 $y1 \33[2;1H$negx$xspd $negy$yspd \33[3;1H \b\b" + #printf "\33[1;1H$x1 $y1 \33[2;1H$negx$xspd $negy$yspd \33[3;1H \b\b" done -- cgit v1.2.3-13-gbd6f