diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2020-04-29 19:21:12 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2020-04-29 19:21:12 -0400 |
commit | a64ba14ff2c6041a4d5b0413e524f2f7a7e3674e (patch) | |
tree | 63da170768def210ede25b1157a4edbcd57046a9 | |
parent | 9797ea8be2220759eab9ec546f43c6b9a04c7cc5 (diff) |
Converted clld to a library.
I did this to not only make it smaller, but to also
make it faster, and to allow for other programs to use
it more efficiently.
Also, the original program is now in clld-test.c
-rw-r--r-- | Makefile | 27 | ||||
-rw-r--r-- | clld-bench.c | 442 | ||||
-rw-r--r-- | clld-test.c | 240 | ||||
-rw-r--r-- | clld.c | 314 | ||||
-rw-r--r-- | clld.h | 61 |
5 files changed, 336 insertions, 748 deletions
@@ -1,23 +1,32 @@ PREFIX := /usr/local BIN_DIR := $(PREFIX)/bin +ifdef DEBUG +DEBUG_CFLAGS=-DDEBUG +else +DEBUG_CFLAGS= +endif + ifdef PCC PCC_CFLAGS=-D__float128="long double" else PCC_CFLAGS= endif -CFLAGS = $(PCC_CFLAGS) $(CFLAGS_EXTRA) -OBJS = clld.c -OBJ_NAME = clld +CFLAGS = $(CFLAGS_EXTRA) $(PCC_CFLAGS) $(DEBUG_CFLAGS) +OBJS = clld.o all : clean $(OBJS) - $(CC) $(OBJS) $(CFLAGS) -o $(OBJ_NAME) -test-mvmt : - $(CC) test-mvmt.c $(CFLAGS) -o test-mvmt -benchmark: clean - $(CC) clld-bench.c $(CFLAGS) -o clld-bench + +%.o : %.c + $(CC) -c $< -o $@ $(CFLAGS) +test : all clld-test.o + $(CC) clld.o clld-test.o -o clld $(CFLAGS) +test-mvmt : clean test + $(CC) test-mvmt.c -o test-mvmt $(CFLAGS) +benchmark : all clld-bench.o + $(CC) clld.o clld-bench.o -o clld-bench $(CFLAGS) clean : - rm -f $(OBJ_NAME) test-mvmt clld-bench + rm -f clld test-mvmt clld-bench *.o install : install -D -m755 clld $(BIN_DIR)/clld uninstall : diff --git a/clld-bench.c b/clld-bench.c index 84bc768..9fd07cb 100644 --- a/clld-bench.c +++ b/clld-bench.c @@ -7,451 +7,13 @@ * Created: September 27, 2019 @ 09:25PM */ -#include <ctype.h> +#include "clld.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> -#include <unistd.h> -#define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) - -enum {THM, AABB}; - -/* Collision type */ -int colltype = -1; - -/* Position type */ -int postype = -1; - -/* Level of verbosity. */ -int verbose = 0; - -/* Options string. */ -const char *opts = "c:v:h"; - -struct pos { - int x; - int y; - int z; - double fx; - double fy; - double fz; -}; - -struct colis { - /* Cursor/Floor's width, and height */ - int w; - int h; - - /* Cursor's X, and Y velocity. */ - double xvel; - double yvel; - - /* Cursor/Floor's coordinates. */ - struct pos pos; - - /* The four corners of either the Cursor, or the Floor. */ - int a1; - int a2; - int b1; - int b2; - - /* The four floating point corners of either the Cursor, or the Floor. */ - double c1; - double c2; - double d1; - double d2; - - /* Heightmap, used in Tile Heightmap collision. */ - uint8_t *heightmap; - - /* X axis collision. */ - unsigned int clx : 1; - - /* Y axis collision. */ - unsigned int cly : 1; - - /* Cursor is on the ground. */ - unsigned int gnded : 1; - - /* Vertical flip flag. */ - unsigned int flp : 1; -}; - -static void usage() { - puts("clld Version MFIYA."); - puts("Usage: clld [options]"); - puts("Options:"); - puts(" -c[type] Force the collision type to be"); - puts(" the specified type (THM=0, AABB=1)."); - puts(" -v[level] Sets the verbosity level (0-3)."); - puts(" -h What do you fucking think it does."); - puts(" Anyways, it brings up this cool help"); - puts(" message."); -} - -/* Get the direction of a collision. */ -uint8_t isleft = 0; -uint8_t isright = 0; -uint8_t istop = 0; -uint8_t isbottom = 0; -uint8_t isinfloor_x = 0; -uint8_t isinfloor_y = 0; - -uint8_t get_coldir(uint8_t flt, uint8_t col_type, int hmpeak, struct colis *cursor, struct colis *floor) { - uint8_t coldir = 0; - uint8_t isinfloor_left = 0; - uint8_t isinfloor_right = 0; - uint8_t isinfloor_top = 0; - uint8_t isinfloor_bottom = 0; - switch (col_type) { - case AABB: - if (flt) { - isleft = cursor->c2 > floor->c1-cursor->xvel-1; - isright = cursor->c1 < floor->c2-cursor->xvel+1; - istop = cursor->d2 > floor->d1-cursor->yvel-1; - isbottom = cursor->d1 < floor->d2-cursor->yvel+1; - - isinfloor_left = cursor->d2 > floor->d1; - isinfloor_right = cursor->d1 < floor->d2; - isinfloor_top = cursor->c2 > floor->c1; - isinfloor_bottom = cursor->c1 < floor->c2; - } else { - isleft = cursor->a2 > floor->a1-cursor->xvel-1; - isright = cursor->a1 < floor->a2-cursor->xvel+1; - istop = cursor->b2 > floor->b1-cursor->yvel-1; - isbottom = cursor->b1 < floor->b2-cursor->yvel+1; - - isinfloor_left = cursor->b2 > floor->b1; - isinfloor_right = cursor->b1 < floor->b2; - isinfloor_top = cursor->a2 > floor->a1; - isinfloor_bottom = cursor->a1 < floor->a2; - } - isinfloor_x = isinfloor_left && isinfloor_right ; - isinfloor_y = isinfloor_top && isinfloor_bottom; - break; - case THM: - if (flt) { - /* Did the cursor collide with the floor from the left? */ - isleft = cursor->c2 > floor->c1-cursor->xvel-1; - /* Did the cursor collide with the floor from the right? */ - isright = cursor->c1 < (floor->c2-cursor->w)-cursor->xvel+1; - - /* Is the floor's heightmap vertically flipped? */ - if (!floor->flp) { - /* Did the cursor collide with the floor from the top? */ - istop = cursor->d2 > (floor->d2-hmpeak)-cursor->yvel; - /* Did the cursor collide with the floor from the bottom? */ - isbottom = cursor->d1 < floor->d2-cursor->yvel+1; - - /* Is the bottom of the cursor below the floor's leftmost height? */ - isinfloor_left = floor->d2-cursor->d2 < floor->heightmap[0] && cursor->xvel > 0; - /* Is the bottom of the cursor below the floor's rightmost height? */ - isinfloor_right = floor->d2-cursor->d2 < floor->heightmap[floor->w-1] && cursor->xvel < 0; - /* Is the bottom of the cursor at, or above the floor's ground? */ - isinfloor_top = floor->d2-cursor->d2 >= 0; - /* Is the top of the cursor at, or below the floor's ground? */ - isinfloor_bottom = floor->d2-cursor->d1 <= 0; - } else { - /* Did the cursor collide with the floor from the top? */ - istop = cursor->d2 > floor->d1-cursor->yvel-1; - /* Did the cursor collide with the floor from the bottom? */ - isbottom = cursor->d1 < (floor->d1+hmpeak)-cursor->yvel; - - /* Is the bottom of the cursor below the floor's leftmost height? */ - isinfloor_left = floor->d1-cursor->d1 > -floor->heightmap[0] && cursor->xvel > 0; - /* Is the bottom of the cursor below the floor's rightmost height? */ - isinfloor_right = floor->d1-cursor->d1 > -floor->heightmap[floor->w-1] && cursor->xvel < 0; - /* Is the bottom of the cursor at, or above the floor's height? */ - isinfloor_top = floor->d1-cursor->d2 >= 0 && cursor->yvel > 0; - /* Is the top of the cursor at, or above the floor's height? */ - isinfloor_bottom = cursor->d1-floor->d1 >= 0; - } - } else { - /* Did the cursor collide with the floor from the left? */ - isleft = cursor->a2 > floor->a1-cursor->xvel-1; - /* Did the cursor collide with the floor from the right? */ - isright = cursor->a1 < (floor->a2-cursor->w)-cursor->xvel+1; - - /* Is the floor's heightmap vertically flipped? */ - if (!floor->flp) { - /* Did the cursor collide with the floor from the top? */ - istop = cursor->b2 > (floor->b2-hmpeak)-cursor->yvel; - /* Did the cursor collide with the floor from the bottom? */ - isbottom = cursor->b1 < floor->b2-cursor->yvel+1; - - /* Is the bottom of the cursor below the floor's leftmost height? */ - isinfloor_left = floor->b2-cursor->b2 < floor->heightmap[0]; - /* Is the bottom of the cursor below the floor's rightmost height? */ - isinfloor_right = floor->b2-cursor->b2 < floor->heightmap[floor->w-1]; - /* Is the bottom of the cursor at, or above the floor's ground? */ - isinfloor_top = floor->b2-cursor->b2 >= 0; - /* Is the top of the cursor at, or below the floor's ground? */ - isinfloor_bottom = floor->b2-cursor->b1 <= 0; - } else { - /* Did the cursor collide with the floor from the top? */ - istop = cursor->b2 > floor->b1-cursor->yvel-1; - /* Did the cursor collide with the floor from the bottom? */ - isbottom = cursor->b1 < (floor->b1+hmpeak)-cursor->yvel; - - /* Is the bottom of the cursor below the floor's leftmost height? */ - isinfloor_left = floor->b1-cursor->b1 > -floor->heightmap[0]; - /* Is the bottom of the cursor below the floor's rightmost height? */ - isinfloor_right = floor->b1-cursor->b1 > -floor->heightmap[floor->w-1]; - /* Is the bottom of the cursor at, or above the floor's ground? */ - isinfloor_top = floor->b1-cursor->b2 >= 0 && cursor->yvel > 0; - /* Is the top of the cursor at, or below the floor's ground? */ - isinfloor_bottom = cursor->b1-floor->b1 >= 0; - } - } - isinfloor_x = isinfloor_left ^ isinfloor_right ; - isinfloor_y = isinfloor_top ^ isinfloor_bottom; - break; - } - if (isinfloor_x) { - coldir |= isleft << 0; - coldir |= isright << 1; - } - if (isinfloor_y) { - coldir |= istop << 2; - coldir |= isbottom << 3; - } - return coldir; -} - -/* Tile Heightmap Collision. */ -int thm(int hmpeak, struct colis *cursor, struct colis *floor) { - int newy = 0; - cursor->clx = (cursor->a2 > floor->a1-cursor->xvel-1 && cursor->b2 > floor->b1) && (cursor->a1 < (floor->a2-cursor->w)-cursor->xvel+1 && cursor->b1 < floor->b2); - cursor->cly = (cursor->b2 >= floor->b1-cursor->yvel && cursor->a2 >= floor->a1) && (cursor->b1 <= floor->b2-cursor->yvel && cursor->a1 <= floor->a2); - uint8_t coldir = get_coldir(0, THM, hmpeak, cursor, floor); - - if (cursor->clx && (cursor->a2-floor->a1 < 0 || cursor->a2-floor->a1 > floor->w)) { - switch (coldir & 3) { - case 1: cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1; break; - case 2: cursor->pos.x = (floor->a2-cursor->w)-cursor->xvel+1; break; - } - cursor->xvel = 0; - floor->xvel = 0; - } - if (cursor->cly && cursor->a2-floor->a1 < floor->w) { - newy = floor->heightmap[cursor->a2-floor->a1]; - switch (coldir & 12) { - case 4: - if (!floor->flp) { - cursor->pos.y = ((floor->b2-cursor->h)-newy)-cursor->yvel; - } else { - cursor->pos.y = (floor->pos.y-cursor->h)-cursor->yvel; - } - break; - case 8: - if (!floor->flp) { - cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1; - } else { - cursor->pos.y = (floor->pos.y+newy)-cursor->yvel; - } - break; - } - cursor->yvel = 0; - floor->yvel = 0; - } - cursor->gnded = (cursor->pos.y == ((floor->b2-cursor->h)-newy)-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); - printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel); - return 0; -} - -/* Floating point Tile Heightmap Collision. */ -int fthm(int hmpeak, struct colis *cursor, struct colis *floor) { - int newy = 0; - 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); - cursor->cly = (cursor->d2 >= floor->d1-cursor->yvel && cursor->c2 >= floor->c1) && (cursor->d1 <= floor->d2-cursor->yvel && cursor->c1 <= floor->c2); - uint8_t coldir = get_coldir(1, THM, hmpeak, cursor, floor); - - if (cursor->clx && cursor->c2-floor->c1 < -1 || cursor->c2-floor->c1 > floor->w) { - switch (coldir & 3) { - case 1: cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1; break; - case 2: cursor->pos.fx = (floor->c2-cursor->w)-cursor->xvel+1; break; - } - cursor->xvel = 0; - cursor->xvel = 0; - } - if (cursor->cly && cursor->c2-floor->c1 < floor->w) { - newy = floor->heightmap[(int)cursor->c2-(int)floor->c1]; - switch (coldir & 12) { - case 4: - if (!floor->flp) { - cursor->pos.fy = ((floor->d2-cursor->h)-newy)-cursor->yvel; - } else { - cursor->pos.fy = (floor->pos.fy-cursor->h)-cursor->yvel; - } - break; - case 8: - if (!floor->flp) { - cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel; - } else { - cursor->pos.fy = (floor->pos.fy+newy)-cursor->yvel; - } - break; - } - cursor->yvel = 0; - floor->yvel = 0; - } - cursor->gnded = (cursor->pos.fy == ((floor->d2-cursor->h)-newy)-cursor->yvel); - 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); - printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel); - return 0; -} - - -/* Axis Aligned Bounding Box Collision. */ -int aabb(struct colis *cursor, struct colis *floor) { - uint8_t coldir = get_coldir(0, AABB, -1, cursor, floor); - uint8_t left = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0); - uint8_t right = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0); - uint8_t top = (cursor->yvel > 0) && (!floor->yvel || floor->yvel != 0); - uint8_t bottom = (cursor->yvel < 0) && (!floor->yvel || floor->yvel != 0); - - cursor->clx = (coldir & 0x03) == 0x03; - cursor->cly = (coldir & 0x0C) == 0x0C; - /* Is the Cursor is on the ground? */ - cursor->gnded = (cursor->b2 == floor->b1-cursor->yvel-1); - if (cursor->clx) { - if (left) { - cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1; - } else if (!left && floor->xvel != 0) { - if (!right && floor->xvel < 0) { - cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1; - } - } - if (right) { - cursor->pos.x = (floor->pos.x+floor->w)-cursor->xvel+1; - } else if (!right && floor->xvel != 0) { - if (!left && floor->xvel > 0) { - cursor->pos.x = (floor->pos.x+floor->w)-cursor->xvel+1; - } - } - cursor->xvel = 0; - floor->xvel = 0; - } - if (cursor->cly) { - if (top) { - cursor->pos.y = (floor->pos.y-cursor->yvel)-cursor->h-1; - } else if (!top && floor->yvel != 0) { - if (!bottom && floor->yvel < 0) { - cursor->pos.y = (floor->pos.y-cursor->yvel)-cursor->h-1; - } - } - if (bottom) { - cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1; - } else if (!bottom && floor->yvel != 0) { - if (!top && floor->yvel > 0) { - cursor->pos.y = (floor->pos.y+floor->h)-cursor->yvel+1; - } - } - cursor->yvel = 0; - floor->yvel = 0; - } - 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", floor->clx, floor->cly, floor->gnded, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel); - return 0; -} - -/* Floating point Axis Aligned Bounding Box Collision. */ -int faabb(struct colis *cursor, struct colis *floor) { - uint8_t coldir = get_coldir(1, AABB, -1, cursor, floor); - uint8_t left = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0); - uint8_t right = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0); - uint8_t top = (cursor->yvel > 0) && (!floor->yvel || floor->yvel != 0); - uint8_t bottom = (cursor->yvel < 0) && (!floor->yvel || floor->yvel != 0); - - cursor->clx = (coldir & 0x03) == 0x03; - cursor->cly = (coldir & 0x0C) == 0x0C; - /* Is the Cursor is on the ground? */ - cursor->gnded = (cursor->d2 == floor->d1-cursor->yvel-1); - if (cursor->clx) { - if (left) { - cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1; - } else if (!left && floor->xvel != 0) { - if (!right && floor->xvel < 0) { - cursor->pos.fx = (floor->pos.fx-cursor->xvel)-cursor->w-1; - } - } - if (right) { - cursor->pos.fx = (floor->pos.fx+floor->w)-cursor->xvel+1; - } else if (!right && floor->xvel != 0) { - if (!left && floor->xvel > 0) { - cursor->pos.fx = (floor->pos.fx+floor->w)-cursor->xvel+1; - } - } - cursor->xvel = 0; - floor->xvel = 0; - } - if (cursor->cly) { - if (top) { - cursor->pos.fy = (floor->pos.fy-cursor->yvel)-cursor->h-1; - } else if (!top && floor->yvel != 0) { - if (!bottom && floor->yvel < 0) { - cursor->pos.fy = (floor->pos.fy-cursor->yvel)-cursor->h-1; - } - } - if (bottom) { - cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel+1; - } else if (!bottom && floor->yvel != 0) { - if (!top && floor->yvel > 0) { - cursor->pos.fy = (floor->pos.fy+floor->h)-cursor->yvel+1; - } - } - cursor->yvel = 0; - floor->yvel = 0; - } - 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", floor->clx, floor->cly, floor->gnded, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel); - return 0; -} - int main(int argc, char **argv) { - int c; - /* Enable custom error messages, and optional arguments/ */ - opterr = 0; - /* Get options. */ - while ((c = getopt(argc, argv, opts)) != -1) { - switch(c) { - case 'c': - colltype = atoi(optarg); - break; - case 'v': - verbose = atoi(optarg); - break; - case 'h': - usage(); - return 2; - case '?': - if (isprint(optopt)) { - if (optopt == 'c') { - fprintf(stderr, "Setting colltype to default.\n"); - colltype = -1; - break; - } else if (optopt == 'v') { - fprintf(stderr, "Enabling single level verbosity.\n"); - verbose = 1; - break; - } else { - fprintf(stderr, "The option that you have typed in, which in this case\n"); - fprintf(stderr, "is -%c, does not exist.\n", optopt); - fprintf(stderr, "Try looking at the help message, or RTFM, next time.\n"); - } - } else { - fprintf(stderr, "What the fuck are you trying to type in, a binary blob?\n"); - fprintf(stderr, "Because of this, clld will exit, so, stop acting like a\n"); - fprintf(stderr, "jackass.\n"); - } - return -1; - default: ; - } - } - + int colltype = -1; /* Create the Cursor, and the Floor */ struct colis cursor; struct colis floor; diff --git a/clld-test.c b/clld-test.c new file mode 100644 index 0000000..a95181a --- /dev/null +++ b/clld-test.c @@ -0,0 +1,240 @@ +#include "clld.h" +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) + +/* Collision type */ +int colltype = -1; + +/* Position type */ +int postype = -1; + +/* Level of verbosity. */ +int verbose = 0; + +/* Options string. */ +const char *opts = "c:v:h"; + +static int usage() { + printf( + "clld Version MFIYA.\n" + "Usage: clld [options]\n" + "Options:\n" + " -c[type] Force the collision type to be\n" + " the specified type (THM=0, AABB=1).\n" + " -v[level] Sets the verbosity level (0-3).\n" + " -h What do you fucking think it does.\n" + " Anyways, it brings up this cool help\n" + " message.\n" + ); + return 0; +} + +int main(int argc, char **argv) { + int c; + FILE *fp = NULL; + uint8_t isfile = 0; + /* Enable custom error messages, and optional arguments/ */ + opterr = 0; + /* Get options. */ + while (optind < argc) { + if ((c = getopt(argc, argv, opts)) != -1) { + switch(c) { + case 'c': + colltype = atoi(optarg); + break; + case 'v': + verbose = atoi(optarg); + break; + case 'h': + usage(); + return 2; + case '?': + if (isprint(optopt)) { + if (optopt == 'c') { + fprintf(stderr, "Setting colltype to default.\n"); + colltype = -1; + break; + } else if (optopt == 'v') { + fprintf(stderr, "Enabling single level verbosity.\n"); + verbose = 1; + break; + } else { + fprintf(stderr + , "The option that you have typed in, which in this case\n" + "is -%c, does not exist.\n" + "Try looking at the help message, or RTFM, next time.\n" + , optopt); + } + } else { + fprintf(stderr + , "What the fuck are you trying to type in, a binary blob?\n" + "Because of this, clld will exit, so, stop acting like a\n" + "jackass.\n"); + } + return -1; + default: ; + } + } else { + if (!isfile) { + fp = fopen(argv[optind], "r"); + if (fp == NULL) { + fprintf(stderr, "Failed to open file %s\n", optarg); + return -1; + } + } + isfile = (fp != NULL); + optind++; + } + } + + /* Create the Cursor, and the Floor */ + struct colis cursor; + struct colis floor; + + /* + * Create a temporary Cursor/Floor, because it simplifies + * the process of getting collision data, to one for loop. + */ + struct colis tmp; + int hmpeak = 0; + /* Get collision type. */ + char *buf = NULL; + size_t size; + getline(&buf, &size, (isfile) ? fp : stdin); + + if (colltype == -1) + colltype = atoi(strtok(buf, "\n")); + + for (int coll = 0; coll <= 1; coll++) { + buf = NULL; + getline(&buf, &size, (isfile) ? fp : stdin); + /* + * Format is: + * + * <w>, <h> + */ + tmp.w = atoi(strtok(buf, ",")); + tmp.h = atoi(strtok(NULL, "\n")); + /* + * Initialize heightmap. + */ + if (colltype == THM && coll) + tmp.heightmap = malloc(sizeof(uint8_t *)*tmp.w); + /* Get Tile Heightmap data. */ + if (colltype == THM && coll) { + /* Get vertical flip flag */ + buf = NULL; + getline(&buf, &size, (isfile) ? fp : stdin); + floor.flp = buf[0] - '0'; + /* Get Heightmap */ + buf = NULL; + size_t newidth = getline(&buf, &size, (isfile) ? fp : stdin); + + /* + * Format is: + * + * 0, 1, 2, 3, ... Until element w-1. + */ + 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.heightmap[i] = (uint8_t)((atoi(tmptok) <= tmp.h) ? atoi(tmptok) : tmp.h); + hmpeak = (atoi(tmptok)+1 >= hmpeak) ? atoi(tmptok)+1 : hmpeak; + } + + } + + /* + * Copy width, and height to either + * the Cursor, or the Floor. + */ + (coll) ? (floor.w = tmp.w) : (cursor.w = tmp.w); + (coll) ? (floor.h = tmp.h) : (cursor.h = tmp.h); + if (coll) + floor.heightmap = tmp.heightmap; + /* Debugging stuff. */ + if (verbose >= 1) { + if (colltype == THM && coll) + for (unsigned int i = 0; i<floor.h; i++) + printf((i < floor.h-1 && i % 16 != 0 || i == 0) ? "%u, " : ("%u\n"), floor.heightmap[i]); + } + + + uint8_t delms = 0; + char *tmp2; + buf = NULL; + getline(&buf, &size, (isfile) ? fp : stdin); + for (int j = 0; buf[j] != '\0'; j++) + if (buf[j] == ',') + delms++; + postype = (delms == 2) ? 1 : 0; + /* Check for floating point coordinates. */ + uint8_t flt; + flt = (strchr(buf, '.') != NULL) ? 1 : 0; + 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,",")); + /* Are we using 3D coordinates, or 2D coordinats? */ + 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); + } + } + /* Get X, and Y velocity. */ + buf = NULL; + getline(&buf, &size, (isfile) ? fp : stdin); + if (!coll) { + cursor.xvel = atof(strtok(buf, ",")); + cursor.yvel = atof(strtok(NULL, "\n")); + } else { + floor.xvel = atof(strtok(buf, ",")); + floor.yvel = atof(strtok(NULL, "\n")); + } + + (!flt) ? (cursor.a1 = cursor.pos.x) : (cursor.c1 = cursor.pos.fx); + (!flt) ? (cursor.a2 = cursor.pos.x+cursor.w) : (cursor.c2 = cursor.pos.fx+cursor.w); + (!flt) ? (cursor.b1 = cursor.pos.y) : (cursor.d1 = cursor.pos.fy); + (!flt) ? (cursor.b2 = cursor.pos.y+cursor.h) : (cursor.d2 = cursor.pos.fy+cursor.h); + (!flt) ? (floor.a1 = floor.pos.x) : (floor.c1 = floor.pos.fx); + (!flt) ? (floor.a2 = floor.pos.x+floor.w) : (floor.c2 = floor.pos.fx+floor.w); + (!flt) ? (floor.b1 = floor.pos.y) : (floor.d1 = floor.pos.fy); + (!flt) ? (floor.b2 = floor.pos.y+floor.h) : (floor.d2 = floor.pos.fy+floor.h); + + if (coll) { + switch (colltype) { + case THM : (!flt) ? thm(hmpeak, &cursor, &floor) : fthm(hmpeak, &cursor, &floor); break; + case AABB: (!flt) ? aabb(&cursor, &floor ) : faabb(&cursor, &floor ); break; + } + } + } + uint8_t a = 0; + unsigned int i = 0; + free(buf); + if(colltype == THM) { + free(floor.heightmap); + } + fflush(stdout); + return 0; +} @@ -7,91 +7,11 @@ * Created: September 27, 2019 @ 09:25PM */ -#include <ctype.h> +#include "clld.h" #include <stdint.h> +#ifdef DEBUG #include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#define TERASGN(a, b, c, d) (a) ? (b = d) : (c = d) - -enum {THM, AABB}; - -/* Collision type */ -int colltype = -1; - -/* Position type */ -int postype = -1; - -/* Level of verbosity. */ -int verbose = 0; - -/* Options string. */ -const char *opts = "c:v:h"; - -struct pos { - int x; - int y; - int z; - double fx; - double fy; - double fz; -}; - -struct colis { - /* Cursor/Floor's width, and height */ - int w; - int h; - - /* Cursor's X, and Y velocity. */ - double xvel; - double yvel; - - /* Cursor/Floor's coordinates. */ - struct pos pos; - - /* The four corners of either the Cursor, or the Floor. */ - int a1; - int a2; - int b1; - int b2; - - /* The four floating point corners of either the Cursor, or the Floor. */ - double c1; - double c2; - double d1; - double d2; - - /* Heightmap, used in Tile Heightmap collision. */ - uint8_t *heightmap; - - /* X axis collision. */ - unsigned int clx : 1; - - /* Y axis collision. */ - unsigned int cly : 1; - - /* Cursor is on the ground. */ - unsigned int gnded : 1; - - /* Vertical flip flag. */ - unsigned int flp : 1; -}; - -static int usage() { - printf( - "clld Version MFIYA.\n" - "Usage: clld [options]\n" - "Options:\n" - " -c[type] Force the collision type to be\n" - " the specified type (THM=0, AABB=1).\n" - " -v[level] Sets the verbosity level (0-3).\n" - " -h What do you fucking think it does.\n" - " Anyways, it brings up this cool help\n" - " message.\n" - ); - return 0; -} +#endif /* Get the direction of a collision. */ uint8_t isleft = 0; @@ -223,12 +143,11 @@ uint8_t get_coldir(uint8_t flt, uint8_t col_type, int hmpeak, struct colis *curs } /* Tile Heightmap Collision. */ -int thm(int hmpeak, struct colis *cursor, struct colis *floor) { +void thm(int hmpeak, struct colis *cursor, struct colis *floor) { int newy = 0; cursor->clx = (cursor->a2 > floor->a1-cursor->xvel-1 && cursor->b2 > floor->b1) && (cursor->a1 < (floor->a2-cursor->w)-cursor->xvel+1 && cursor->b1 < floor->b2); cursor->cly = (cursor->b2 >= floor->b1-cursor->yvel && cursor->a2 >= floor->a1) && (cursor->b1 <= floor->b2-cursor->yvel && cursor->a1 <= floor->a2); uint8_t coldir = get_coldir(0, THM, hmpeak, cursor, floor); - if (cursor->clx && (cursor->a2-floor->a1 < 0 || cursor->a2-floor->a1 > floor->w)) { switch (coldir & 3) { case 1: cursor->pos.x = (floor->pos.x-cursor->xvel)-cursor->w-1; break; @@ -259,13 +178,14 @@ int thm(int hmpeak, struct colis *cursor, struct colis *floor) { floor->yvel = 0; } cursor->gnded = (cursor->pos.y == ((floor->b2-cursor->h)-newy)-cursor->yvel); + #ifdef DEBUG 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); printf("%u, %u\n%u\n%i\n%i, %i\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel); - return 0; + #endif } /* Floating point Tile Heightmap Collision. */ -int fthm(int hmpeak, struct colis *cursor, struct colis *floor) { +void fthm(int hmpeak, struct colis *cursor, struct colis *floor) { int newy = 0; 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); cursor->cly = (cursor->d2 >= floor->d1-cursor->yvel && cursor->c2 >= floor->c1) && (cursor->d1 <= floor->d2-cursor->yvel && cursor->c1 <= floor->c2); @@ -301,14 +221,14 @@ int fthm(int hmpeak, struct colis *cursor, struct colis *floor) { floor->yvel = 0; } cursor->gnded = (cursor->pos.fy == ((floor->d2-cursor->h)-newy)-cursor->yvel); + #ifdef DEBUG 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); printf("%u, %u\n%u\n%i\n%07.06f, %07.06f\n%07.06f, %07.06f\n", floor->clx, floor->cly, floor->gnded, newy, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel); - return 0; + #endif } - /* Axis Aligned Bounding Box Collision. */ -int aabb(struct colis *cursor, struct colis *floor) { +void aabb(struct colis *cursor, struct colis *floor) { uint8_t coldir = get_coldir(0, AABB, -1, cursor, floor); uint8_t left = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0); uint8_t right = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0); @@ -355,13 +275,14 @@ int aabb(struct colis *cursor, struct colis *floor) { cursor->yvel = 0; floor->yvel = 0; } + #ifdef DEBUG 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", floor->clx, floor->cly, floor->gnded, floor->pos.x, floor->pos.y, floor->xvel, floor->yvel); - return 0; + #endif } /* Floating point Axis Aligned Bounding Box Collision. */ -int faabb(struct colis *cursor, struct colis *floor) { +void faabb(struct colis *cursor, struct colis *floor) { uint8_t coldir = get_coldir(1, AABB, -1, cursor, floor); uint8_t left = (cursor->xvel > 0) && (!floor->xvel || floor->xvel != 0); uint8_t right = (cursor->xvel < 0) && (!floor->xvel || floor->xvel != 0); @@ -408,213 +329,8 @@ int faabb(struct colis *cursor, struct colis *floor) { cursor->yvel = 0; floor->yvel = 0; } + #ifdef DEBUG 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", floor->clx, floor->cly, floor->gnded, floor->pos.fx, floor->pos.fy, floor->xvel, floor->yvel); - return 0; -} - -int main(int argc, char **argv) { - int c; - FILE *fp = NULL; - uint8_t isfile = 0; - /* Enable custom error messages, and optional arguments/ */ - opterr = 0; - /* Get options. */ - while (optind < argc) { - if ((c = getopt(argc, argv, opts)) != -1) { - switch(c) { - case 'c': - colltype = atoi(optarg); - break; - case 'v': - verbose = atoi(optarg); - break; - case 'h': - usage(); - return 2; - case '?': - if (isprint(optopt)) { - if (optopt == 'c') { - fprintf(stderr, "Setting colltype to default.\n"); - colltype = -1; - break; - } else if (optopt == 'v') { - fprintf(stderr, "Enabling single level verbosity.\n"); - verbose = 1; - break; - } else { - fprintf(stderr - , "The option that you have typed in, which in this case\n" - "is -%c, does not exist.\n" - "Try looking at the help message, or RTFM, next time.\n" - , optopt); - } - } else { - fprintf(stderr - , "What the fuck are you trying to type in, a binary blob?\n" - "Because of this, clld will exit, so, stop acting like a\n" - "jackass.\n"); - } - return -1; - default: ; - } - } else { - if (!isfile) { - fp = fopen(argv[optind], "r"); - if (fp == NULL) { - fprintf(stderr, "Failed to open file %s\n", optarg); - return -1; - } - } - isfile = (fp != NULL); - optind++; - } - } - - /* Create the Cursor, and the Floor */ - struct colis cursor; - struct colis floor; - - /* - * Create a temporary Cursor/Floor, because it simplifies - * the process of getting collision data, to one for loop. - */ - struct colis tmp; - int hmpeak = 0; - /* Get collision type. */ - char *buf = NULL; - size_t size; - getline(&buf, &size, (isfile) ? fp : stdin); - - if (colltype == -1) - colltype = atoi(strtok(buf, "\n")); - - for (int coll = 0; coll <= 1; coll++) { - buf = NULL; - getline(&buf, &size, (isfile) ? fp : stdin); - /* - * Format is: - * - * <w>, <h> - */ - tmp.w = atoi(strtok(buf, ",")); - tmp.h = atoi(strtok(NULL, "\n")); - /* - * Initialize heightmap. - */ - if (colltype == THM && coll) - tmp.heightmap = malloc(sizeof(uint8_t *)*tmp.w); - /* Get Tile Heightmap data. */ - if (colltype == THM && coll) { - /* Get vertical flip flag */ - buf = NULL; - getline(&buf, &size, (isfile) ? fp : stdin); - floor.flp = buf[0] - '0'; - /* Get Heightmap */ - buf = NULL; - size_t newidth = getline(&buf, &size, (isfile) ? fp : stdin); - - /* - * Format is: - * - * 0, 1, 2, 3, ... Until element w-1. - */ - 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.heightmap[i] = (uint8_t)((atoi(tmptok) <= tmp.h) ? atoi(tmptok) : tmp.h); - hmpeak = (atoi(tmptok)+1 >= hmpeak) ? atoi(tmptok)+1 : hmpeak; - } - - } - - /* - * Copy width, and height to either - * the Cursor, or the Floor. - */ - (coll) ? (floor.w = tmp.w) : (cursor.w = tmp.w); - (coll) ? (floor.h = tmp.h) : (cursor.h = tmp.h); - if (coll) - floor.heightmap = tmp.heightmap; - /* Debugging stuff. */ - if (verbose >= 1) { - if (colltype == THM && coll) - for (unsigned int i = 0; i<floor.h; i++) - printf((i < floor.h-1 && i % 16 != 0 || i == 0) ? "%u, " : ("%u\n"), floor.heightmap[i]); - } - - - uint8_t delms = 0; - char *tmp2; - buf = NULL; - getline(&buf, &size, (isfile) ? fp : stdin); - for (int j = 0; buf[j] != '\0'; j++) - if (buf[j] == ',') - delms++; - postype = (delms == 2) ? 1 : 0; - /* Check for floating point coordinates. */ - uint8_t flt; - flt = (strchr(buf, '.') != NULL) ? 1 : 0; - 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,",")); - /* Are we using 3D coordinates, or 2D coordinats? */ - 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); - } - } - /* Get X, and Y velocity. */ - buf = NULL; - getline(&buf, &size, (isfile) ? fp : stdin); - if (!coll) { - cursor.xvel = atof(strtok(buf, ",")); - cursor.yvel = atof(strtok(NULL, "\n")); - } else { - floor.xvel = atof(strtok(buf, ",")); - floor.yvel = atof(strtok(NULL, "\n")); - } - - (!flt) ? (cursor.a1 = cursor.pos.x) : (cursor.c1 = cursor.pos.fx); - (!flt) ? (cursor.a2 = cursor.pos.x+cursor.w) : (cursor.c2 = cursor.pos.fx+cursor.w); - (!flt) ? (cursor.b1 = cursor.pos.y) : (cursor.d1 = cursor.pos.fy); - (!flt) ? (cursor.b2 = cursor.pos.y+cursor.h) : (cursor.d2 = cursor.pos.fy+cursor.h); - (!flt) ? (floor.a1 = floor.pos.x) : (floor.c1 = floor.pos.fx); - (!flt) ? (floor.a2 = floor.pos.x+floor.w) : (floor.c2 = floor.pos.fx+floor.w); - (!flt) ? (floor.b1 = floor.pos.y) : (floor.d1 = floor.pos.fy); - (!flt) ? (floor.b2 = floor.pos.y+floor.h) : (floor.d2 = floor.pos.fy+floor.h); - - if (coll) { - switch (colltype) { - case THM : (!flt) ? thm(hmpeak, &cursor, &floor) : fthm(hmpeak, &cursor, &floor); break; - case AABB: (!flt) ? aabb(&cursor, &floor ) : faabb(&cursor, &floor ); break; - } - } - } - uint8_t a = 0; - unsigned int i = 0; - free(buf); - if(colltype == THM) { - free(floor.heightmap); - } - fflush(stdout); - return 0; + #endif } @@ -0,0 +1,61 @@ +#include <stdint.h> + +enum {THM, AABB}; + +struct pos { + int x; + int y; + int z; + double fx; + double fy; + double fz; +}; + +struct colis { + /* Cursor/Floor's width, and height */ + int w; + int h; + + /* Cursor's X, and Y velocity. */ + double xvel; + double yvel; + + /* Cursor/Floor's coordinates. */ + struct pos pos; + + /* The four corners of either the Cursor, or the Floor. */ + int a1; + int a2; + int b1; + int b2; + + /* The four floating point corners of either the Cursor, or the Floor. */ + double c1; + double c2; + double d1; + double d2; + + /* Heightmap, used in Tile Heightmap collision. */ + uint8_t *heightmap; + + /* X axis collision. */ + unsigned int clx : 1; + + /* Y axis collision. */ + unsigned int cly : 1; + + /* Cursor is on the ground. */ + unsigned int gnded : 1; + + /* Vertical flip flag. */ + unsigned int flp : 1; +}; + +/* Tile Heightmap Collision. */ +extern void thm(int hmpeak, struct colis *cursor, struct colis *floor); +/* Floating point Tile Heightmap Collision. */ +extern void fthm(int hmpeak, struct colis *cursor, struct colis *floor); +/* Axis Aligned Bounding Box Collision. */ +extern void aabb(struct colis *cursor, struct colis *floor); +/* Floating point Axis Aligned Bounding Box Collision. */ +extern void faabb(struct colis *cursor, struct colis *floor); |