/* * Main Program file. * * Platform: Anything with a C compiler. * Author: mr b0nk 500 * Notes: Who would even use this kind of collision checker? * Created: September 27, 2019 @ 09:25PM */ #include #include #include #include #include #define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) enum {THM, AABB, PPX }; /* Collision type */ int colltype = -1; /* Position type */ int postype = -1; /* Level of verbosity. */ int verbose = 0; static int usage() { printf( "clld Version MFIYA.\n" "Usage: clld [options]\n" "Options:\n" " -c Force the collision type to be\n" " the specified type (0-2).\n" " -v[level] Sets the verbosity level (0-3).\n" ); return 0; } int main(int argc, char **argv) { struct pos { int x; int y; int z; double fx; double fy; double fz; }; struct colis { unsigned int w; unsigned int h; struct pos pos; int a1; int a2; int b1; int b2; double c1; double c2; double d1; double d2; uint8_t **bitmask; unsigned int clx : 1; unsigned int cly : 1; unsigned int gnded : 1; }; struct colis cursor; struct colis floor; struct colis tmp; char *buf = NULL; size_t size; /*getline(&buf, &size, stdin);*/ /*if (postype == -1) postype = atoi(strtok(buf, "\n"));*/ /*buf = NULL;*/ getline(&buf, &size, stdin); if (colltype == -1) colltype = atoi(strtok(buf, "\n")); printf("%i\n", colltype); for (int coll = 0; coll <= 1; coll++) { buf = NULL; getline(&buf, &size, stdin); /* , */ tmp.w = atoi(strtok(buf, ",")); tmp.h = atoi(strtok(NULL, "\n")); /* Initialize y axis of bitmask. */ if (colltype == THM || colltype == PPX) tmp.bitmask = malloc(sizeof(uint8_t *)*tmp.h); /* Initialize x axis of bitmask. */ if (colltype == PPX) for(unsigned int i = 0; i= tmp.w) ? tmp.w : newidth; for (unsigned int i = 0; i = tmp.h) ? tmp.h : newheight; for (unsigned int i = 0; i = 1) { if (colltype == THM && coll) for (unsigned int i = 0; i= 1) printf("flt: %u\n", flt); if (flt) { tmp.pos.fx = atof(strtok(buf,",")); if(postype) tmp.pos.fy = atof(strtok(NULL,",")); tmp2 = strtok(NULL, "\n"); TERASGN(postype, tmp.pos.fz, tmp.pos.fy, atof((tmp2 == NULL ? strtok(NULL,"\0") : tmp2))); (coll) ? (floor.pos = tmp.pos) : (cursor.pos = tmp.pos); if (verbose >= 1) { printf("%07.06f, %07.06f%s", (!coll) ? cursor.pos.fx : floor.pos.fx, (!coll) ? cursor.pos.fy : floor.pos.fy, (postype) ? ", " : "\n"); if(postype) printf("%07.06f\n", (!coll) ? cursor.pos.fz : floor.pos.fz); } } else { tmp.pos.x = atoi(strtok(buf,",")); if(postype) tmp.pos.y = atoi(strtok(NULL,",")); tmp2 = strtok(NULL, "\n"); TERASGN(postype, tmp.pos.z, tmp.pos.y, atoi((tmp2 == NULL ? strtok(NULL,"\0") : tmp2))); (coll) ? (floor.pos = tmp.pos) : (cursor.pos = tmp.pos); if (verbose >= 1) { printf("%i, %i%s", (!coll) ? cursor.pos.x : floor.pos.x, (!coll) ? cursor.pos.y : floor.pos.y, (postype) ? ", " : "\n"); if(postype) printf("%i\n", (!coll) ? cursor.pos.z : floor.pos.z); } } if (colltype == AABB && coll) { if (!flt) { cursor.a1 = cursor.pos.x; cursor.a2 = cursor.pos.x+cursor.w; cursor.b1 = cursor.pos.y; cursor.b2 = cursor.pos.y+cursor.h; floor.a1 = floor.pos.x; floor.a2 = floor.pos.x+floor.w; floor.b1 = floor.pos.y; floor.b2 = floor.pos.y+floor.h; } else { cursor.c1 = cursor.pos.fx; cursor.c2 = cursor.pos.fx+cursor.w; cursor.d1 = cursor.pos.fy; cursor.d2 = cursor.pos.fy+cursor.h; floor.c1 = floor.pos.fx; floor.c2 = floor.pos.fx+floor.w; floor.d1 = floor.pos.fy; floor.d2 = floor.pos.fy+floor.h; } if (!flt) { cursor.gnded = (cursor.b2 == floor.b1) ? 1 : 0; cursor.clx = ((cursor.a2 >= floor.a1 && cursor.a1 <= floor.a2) && (cursor.b1+(cursor.h/2) >= floor.b1 && cursor.b2-(cursor.h/2) <= floor.b2)) ? 1 : 0; cursor.cly = ((cursor.b2 >= floor.b1 && cursor.b1 <= floor.b2) && (cursor.a1+(cursor.w/2) >= floor.a1 && cursor.a2-(cursor.w/2) <= floor.a2)) ? 1 : 0; } else { cursor.gnded = (cursor.d2 == floor.d1) ? 1 : 0; cursor.clx = ((cursor.c2 >= floor.c1 && cursor.c1 <= floor.c2) && (cursor.d1+(cursor.h/2) >= floor.b1 && cursor.d1 <= floor.d2)) ? 1 : 0; cursor.cly = ((cursor.d2 >= floor.d1 && cursor.d1 <= floor.d2) && (cursor.c1+(cursor.w/2) >= floor.c1 && cursor.c1 <= floor.c2)) ? 1 : 0; } if (verbose >= 1) printf("cursor.pos.x: %i\ncursor.w: %u, cursor.h: %u\nfloor.pos.x: %i\nfloor.w: %u, floor.h: %u\n", cursor.pos.x, cursor.w, cursor.h, floor.pos.x, floor.w, floor.h); if(!flt) { if (cursor.clx) { if (cursor.a2 >= floor.a1 && cursor.a2 < floor.a2) cursor.pos.x = floor.pos.x-cursor.w; else if (cursor.a1 <= floor.a2 && cursor.a1 > floor.a1) cursor.pos.x = floor.pos.x+floor.w; } if (cursor.cly) { if (cursor.b2 >= floor.b1 && cursor.b2 < floor.b2) cursor.pos.y = floor.pos.y-cursor.h; else if (cursor.b1 <= floor.b2 && cursor.b1 > floor.b1) cursor.pos.y = floor.pos.y+floor.h; } if (verbose >= 1) { printf("cursor.gnded: %u\nclx: %u, cly: %u\n", cursor.gnded, cursor.clx, cursor.cly); printf("x: %i, y: %i\n", cursor.pos.x, cursor.pos.y); printf("floor.x: %i, floor.y: %i\n", floor.pos.x, floor.pos.y); } printf("%u, %u\n%u\n%i, %i\n", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.x, cursor.pos.y); } else { if (cursor.clx) { if (cursor.c2 >= floor.c1) cursor.pos.fx = floor.pos.fx-cursor.w; else if (cursor.c1 <= floor.c2) cursor.pos.fx = floor.pos.fx+floor.w; } if (cursor.cly ) { if (cursor.d2 >= floor.d1) cursor.pos.fy = floor.pos.fy-cursor.h; else if (cursor.d1 <= floor.d2) cursor.pos.fy = floor.pos.fy+floor.h; } if (verbose >= 1) { printf("cursor.gnded: %u\nclx: %u, cly: %u\n", cursor.gnded, cursor.clx, cursor.cly); 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", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy); } } } uint8_t a = 0; unsigned int i = 0; free(buf); if(colltype == THM || colltype == PPX) { while (!a && colltype == PPX) (i