summaryrefslogtreecommitdiff
path: root/clld.c
blob: 446b775bdaa3db9b7a05be6e3b7e56893eb0376c (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
/*
 * 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)

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<type>	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;
		uint8_t **bitmask;
		unsigned int clds : 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);
		/* <w>, <h> */
		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.h; i++)
				tmp.bitmask[i] = malloc(sizeof(uint8_t)*tmp.w);
		/* Per Pixel Colision. */
		if (colltype == PPX) {
			for (unsigned int j = 0; j<tmp.h; j++) {
				buf = NULL;
				size_t newidth = getline(&buf, &size, stdin);
				/*
				 * Format is:
				 *
				 * 000111000... Until bit w-1.
				 * .
				 * .
				 *
				 * Until bit h-1.
				 */
				tmp.w = (newidth >= tmp.w) ? tmp.w : newidth;
				for (unsigned int i = 0; i <tmp.w; i++)
					tmp.bitmask[j][i] = buf[i] - '0';
			}
		}

		/* Tile Heightmap Collision. */
		if (colltype == THM && coll) {
			buf = NULL;
                        size_t newheight = getline(&buf, &size, stdin);
			/*
			 * Format is:
			 *
			 * 0, 1, 2, 3, ... Until element h-1.
			 */
			tmp.h = (newheight >= tmp.h) ? tmp.h : newheight;
			for (unsigned int i = 0; i <tmp.h; i++) {
				char *tmptok = strtok((i == 0) ? buf : NULL,",");
				if(atoi(tmptok) <= tmp.h)
					tmp.bitmask[i] = (uint8_t *)atoi((tmptok == NULL ? strtok(NULL,"\n") : tmptok));
			}
		}



		(coll) ? (floor.w = tmp.w) : (cursor.w = tmp.w);
		(coll) ? (floor.h = tmp.h) : (cursor.h = tmp.h);
		(coll) ? (floor.bitmask = tmp.bitmask) : (cursor.bitmask = tmp.bitmask);

		if (colltype == THM && coll)
			 for (unsigned int i = 0; i<floor.h; i++)
				 printf((i<floor.h-1) ? "%u, " : ("%u\n"), floor.bitmask[i]);


		if (colltype == PPX) {
			for (unsigned int j = 0; j<((coll) ? floor.h : cursor.h); j++) {
				for (unsigned int i = 0; i<((coll) ? floor.w : cursor.w); i++)
					if((coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i])
						printf("%u", (coll) ? floor.bitmask[j][i] : cursor.bitmask[j][i]);
					else
						printf(" ");
				printf("\n");
			}
		}


		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. */
		if (strchr(buf, '.') != NULL) {
			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);
			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);
			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);
		}
	}
	uint8_t a = 0;
	unsigned int i = 0;
	free(buf);
	if(colltype == THM || colltype == PPX) {
		while (!a && colltype == PPX)
			(i<tmp.h) ? (free(tmp.bitmask[i]), ++i) : (a=1);
		free(cursor.bitmask);
		free(floor.bitmask);
	}
	fflush(stdout);

	return 0;
}