diff options
Diffstat (limited to 'clld.c')
-rw-r--r-- | clld.c | 107 |
1 files changed, 70 insertions, 37 deletions
@@ -151,14 +151,14 @@ int main(int argc, char **argv) { floor.flp = buf[0] - '0'; /* Get Heightmap */ buf = NULL; - size_t newheight = getline(&buf, &size, stdin); + size_t newidth = getline(&buf, &size, stdin); /* * Format is: * - * 0, 1, 2, 3, ... Until element h-1. + * 0, 1, 2, 3, ... Until element w-1. */ - tmp.h = (newheight >= tmp.h) ? tmp.h : newheight; - for (unsigned int i = 0; i <tmp.h; i++) { + tmp.w = (newidth >= tmp.w) ? tmp.w : newidth; + for (unsigned int i = 0; i <tmp.w; i++) { 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); @@ -253,41 +253,53 @@ int main(int argc, char **argv) { 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-hmpeak-1)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak+1)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; + cursor.cly = ((cursor.b2 >= (floor.b1-hmpeak)-(int)cursor.yvel && cursor.a2 >= floor.a1) && (cursor.b1 <= (floor.b2+hmpeak)-(int)cursor.yvel && cursor.a1 <= floor.a2)) ? 1 : 0; int newy = 0; + if (cursor.clx) { + if (cursor.a2-floor.a1 < 0 || cursor.a2-floor.a1 > floor.w) { + /* Is the Cursor coming from the left? */ + if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && floor.b2-cursor.b2 < (int)floor.bitmask[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 < (int)floor.bitmask[floor.w-1] && !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 && cursor.b1-floor.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 && cursor.b1-floor.b1 < 0 && floor.flp && cursor.xvel > 0) + cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1; + cursor.xvel = 0; + } + } if (cursor.cly) { - if (cursor.a2-floor.a1 < floor.h) { + if (cursor.a2-floor.a1 < floor.w) { cursor.yvel = 0; newy = (int)floor.bitmask[cursor.a2-floor.a1]; /* Is the Floor, really a floor? */ - 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; + if (cursor.b2 > (floor.b2-hmpeak)-(int)cursor.yvel && floor.b2-cursor.b2 >= 0 && !floor.flp) + cursor.pos.y = ((floor.b2-cursor.h)-newy)-(int)cursor.yvel; + else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && floor.b2-cursor.b1 <= 0 && !floor.flp) + cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1; /* Is the Floor, actually a ceiling? */ - 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) + if (cursor.b1 < (floor.b1+hmpeak)-(int)cursor.yvel && cursor.b1-floor.b1 >= 0 && floor.flp) + cursor.pos.y = (floor.pos.y+newy)-(int)cursor.yvel; + else if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.b2-floor.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.b2-cursor.h)-newy)-(int)cursor.yvel) ? 1 : 0; + if (verbose >= 1) { + printf( + "cursor.a1-(floor.a2-(floor.w/2)): %i\n" + "cursor.a2-(floor.a2-(floor.w/2)): %i\n" + "(cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)): %i\n" + "(cursor.a2-(cursor.w/2)): %i\n" + , cursor.a1-(floor.a2-(floor.w/2)) + , cursor.a2-(floor.a2-(floor.w/2)) + , (cursor.a2-(cursor.w/2))-(floor.a2-(floor.w/2)) + , (cursor.a2-(cursor.w/2))); } - cursor.gnded = (cursor.pos.y == floor.pos.y-(cursor.h-newy)) ? 1 : 0; - - 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); + printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.x, cursor.pos.y, cursor.xvel, cursor.yvel); } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; @@ -298,24 +310,45 @@ int main(int argc, char **argv) { floor.d1 = floor.pos.fy; floor.d2 = floor.pos.fy+floor.h; + cursor.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0; /* Check for a collision, on the y axis. */ - cursor.cly = ((cursor.d2 > floor.d1-cursor.yvel-1 && cursor.c2 > floor.c1) && (cursor.d1 < floor.d2-cursor.yvel+1 && cursor.c1 < floor.c2)) ? 1 : 0; + cursor.cly = ((cursor.d2 >= (floor.d1-hmpeak)-cursor.yvel && cursor.c2 >= floor.c1) && (cursor.d1 <= (floor.d2+hmpeak)-cursor.yvel && cursor.c1 <= floor.c2)) ? 1 : 0; int newy = 0; + if (cursor.clx) { + if (cursor.c2-floor.c1 < -1 || cursor.c2-floor.c1 > floor.w) { + /* Is the Cursor coming from the left? */ + if (cursor.c2 > floor.c1-cursor.xvel-1 && floor.d2-cursor.d2 < (int)floor.bitmask[0] && !floor.flp && cursor.xvel > 0) + cursor.pos.fx = (floor.pos.x-cursor.xvel)-cursor.w-1; + else if (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && floor.d2-cursor.d2 < (int)floor.bitmask[floor.w-1] && !floor.flp && cursor.xvel < 0) + cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1; + /* Is the Cursor coming from the right? */ + if (cursor.c1 < (floor.c2-cursor.w)-cursor.xvel+1 && cursor.d1-floor.d1 < 0 && floor.flp && cursor.xvel < 0) + cursor.pos.fx = (floor.c2-cursor.w)-cursor.xvel+1; + else if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d1-floor.d1 < 0 && floor.flp && cursor.xvel > 0) + cursor.pos.fx = (floor.pos.x-cursor.xvel)-cursor.w-1; + cursor.xvel = 0; + } + } if (cursor.cly) { - if (cursor.c2-floor.c1 < floor.h) { + if (cursor.c2-floor.c1 < floor.w) { + cursor.yvel = 0; newy = (int)floor.bitmask[(int)cursor.c2-(int)floor.c1]; /* Is the Floor, really a floor? */ - if (cursor.d2 > floor.d1-cursor.yvel-1 && !floor.flp) - cursor.pos.fy = floor.pos.fy-(cursor.h-newy)-cursor.yvel+1; + if (cursor.d2 > (floor.d2-hmpeak)-cursor.yvel && floor.d2-cursor.d2 >= 0 && !floor.flp) + cursor.pos.fy = ((floor.d2-cursor.h)-newy)-cursor.yvel; + else if (cursor.d1 < floor.d2-cursor.yvel+1 && floor.d2-cursor.d1 <= 0 && !floor.flp) + cursor.pos.fy = (floor.d2)-cursor.yvel+1; /* Is the Floor, actually a ceiling? */ - else if (cursor.d1 < floor.d2-cursor.yvel+1 && floor.flp) - cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel-1; + if (cursor.d1 < (floor.d1+hmpeak)-cursor.yvel && cursor.d1-floor.d1 >= 0 && floor.flp) + cursor.pos.fy = (floor.pos.fy+newy)-cursor.yvel; + else if (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.d2-floor.d1 < 0 && cursor.yvel > 0 && floor.flp) + cursor.pos.fy = (floor.pos.fy-cursor.yvel-1)-cursor.h; } } - cursor.gnded = (cursor.pos.fy == floor.pos.fy-(cursor.h-newy)) ? 1 : 0; - - printf("%u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.cly, cursor.gnded, newy, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); + cursor.gnded = (cursor.pos.fy == ((floor.d2-cursor.h)-newy)-cursor.yvel) ? 1 : 0; + printf("cursor.c2-floor.c1: %07.06f\n", cursor.c2-floor.c1); + printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", cursor.clx, cursor.cly, cursor.gnded, newy, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel); } } |