summaryrefslogtreecommitdiff
path: root/clld.c
blob: 6d6d3a0cb11d4bc299e187185bf9e03700688f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Main Program file.
 *
 * Platform: 	Anything with a C compiler.
 * Author:	mr b0nk 500 <b0nk@mrb0nk500.xyz>
 * Notes: 	Who would even use this kind of collision checker?
 * Created:	September 27, 2019 @ 09:25PM
 */

#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)
#define max(a, b) (abs(a-b)+(a+b))/2
#define min(a, b) -max(-a, -b)

enum {THM, AABB};

/* 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<type>	Force the collision type to be\n"
		"			the specified type (THM=0, AABB=1, PPX=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 {
		/* 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;
	};

	/* 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;
	/* Get collision type. */
	char *buf = NULL;
	size_t size;

	getline(&buf, &size, stdin);

	if (colltype == -1)
		colltype = atoi(strtok(buf, "\n"));

	for (int coll = 0; coll <= 1; coll++) {
		buf = NULL;
		getline(&buf, &size, 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, stdin);
			floor.flp = buf[0] - '0';
			/* Get Heightmap */
			buf = NULL;
                        size_t newidth = getline(&buf, &size, 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, 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. */
		if(!coll) {
			buf = NULL;
			getline(&buf, &size, stdin);
			cursor.xvel = atof(strtok(buf, ","));
			cursor.yvel = atof(strtok(NULL, "\n"));
		}

		/* Tile Heightmap Collision. */
		if (colltype == THM && 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;

				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)-(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.heightmap[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.heightmap[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.w) {
						cursor.yvel = 0;
						newy = (int)floor.heightmap[cursor.a2-floor.a1];
						/* Is the Floor, really a floor? */
						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)-(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;
					}
				}
				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)));
				}
				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;
				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;

				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-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.heightmap[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.heightmap[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.w) {
						cursor.yvel = 0;
						newy = (int)floor.heightmap[(int)cursor.c2-(int)floor.c1];
						/* Is the Floor, really a floor? */
						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? */
						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.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);
			}
		}

		/* Axis Aligned Bounding Box Collision. */
		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;
				/* Is the Cursor is on the ground? */
				cursor.gnded = (cursor.b2 == floor.b1-(int)cursor.yvel-1) ? 1 : 0;

				/* Check for collisions, on both axies. */
				cursor.clx = ((cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.b2 > floor.b1) && (cursor.a1 < floor.a2-(int)cursor.xvel+1 && cursor.b1 < floor.b2)) ? 1 : 0;
				cursor.cly = ((cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.a2 > floor.a1) && (cursor.b1 < floor.b2-(int)cursor.yvel+1 && cursor.a1 < floor.a2)) ? 1 : 0;

				if (cursor.clx) {
					/* Is the Cursor coming from the left? */
					if (cursor.a2 > floor.a1-(int)cursor.xvel-1 && cursor.xvel > 0)
						cursor.pos.x = (floor.pos.x-(int)cursor.xvel)-cursor.w-1;
					/* Is the Cursor coming from the right? */
					else if (cursor.a1 < floor.a2-(int)cursor.xvel+1 && cursor.xvel < 0)
						cursor.pos.x = (floor.pos.x+floor.w)-(int)cursor.xvel+1;
					cursor.xvel = 0;
				}
				if (cursor.cly) {
					/* Is the Cursor coming from the top? */
					if (cursor.b2 > floor.b1-(int)cursor.yvel-1 && cursor.yvel > 0)
						cursor.pos.y = (floor.pos.y-(int)cursor.yvel)-cursor.h-1;
					/* Is the Cursor coming from the bottom? */
					else if (cursor.b1 < floor.b2-(int)cursor.yvel+1 && cursor.yvel < 0)
						cursor.pos.y = (floor.pos.y+floor.h)-(int)cursor.yvel+1;
					cursor.yvel = 0;
				}

				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 ("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\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;
				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;

				cursor.gnded = (cursor.d2 == floor.d1-cursor.yvel-1) ? 1 : 0;
				cursor.clx = ((cursor.c2 > floor.c1-cursor.xvel-1 && cursor.d2 > floor.d1) && (cursor.c1 < floor.c2-cursor.xvel+1 && cursor.d1 < floor.d2)) ? 1 : 0;
				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;

				if (cursor.clx) {
					if (cursor.c2 > floor.c1-cursor.xvel-1 && cursor.xvel > 0)
						cursor.pos.fx = (floor.pos.fx-cursor.xvel)-(double)cursor.w-1;
					else if (cursor.c1 < floor.c2-cursor.xvel+1 && cursor.xvel < 0)
						cursor.pos.fx = (floor.pos.fx+(double)floor.w)-cursor.xvel+1;
					cursor.xvel = 0;
				}
				if (cursor.cly) {
					if (cursor.d2 > floor.d1-cursor.yvel-1 && cursor.yvel > 0)
						cursor.pos.fy = (floor.pos.fy-cursor.yvel)-(double)cursor.h-1;
					else if (cursor.d1 < floor.d2-cursor.yvel+1 && cursor.yvel < 0)
						cursor.pos.fy = (floor.pos.fy+(double)floor.h)-cursor.yvel+1;
					cursor.yvel = 0;
				}
				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%07.06f, %07.06f\n\0", cursor.clx, cursor.cly, cursor.gnded, cursor.pos.fx, cursor.pos.fy, cursor.xvel, cursor.yvel);
			}
		}
	}
	uint8_t a = 0;
	unsigned int i = 0;
	free(buf);
	if(colltype == THM) {
		free(floor.heightmap);
	}
	fflush(stdout);

	return 0;
}