File: modeltest.c

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (301 lines) | stat: -rw-r--r-- 7,312 bytes parent folder | download | duplicates (3)
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2007-2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>

#ifndef WIN32
#include <stdbool.h>
#include <limits.h>
#include <unistd.h>
#else
typedef int bool;
#define PATH_MAX 255
#define true 1
#define false 0
#endif

#define iV_IMD_TEX	0x00000200
#define iV_IMD_XNOCUL	0x00002000
#define iV_IMD_TEXANIM	0x00004000
#define iV_IMD_TCMASK	0x00010000
#define MAX_POLYGON_SIZE 3 // the game can't handle more

typedef struct {
	int index[MAX_POLYGON_SIZE];
	int texCoord[MAX_POLYGON_SIZE][2];
	int vertices;
	int frames, rate, width, height; // animation data
	bool cull;
} WZ_FACE;

typedef struct {
	int x, y, z, reindex;
	bool dupe;
} WZ_POSITION;

static void check_pie(const char *input)
{
	int num, x, y, z, levels, level;
	FILE *fp = fopen(input, "r");
	char s[200];

	if (!fp)
	{
		fprintf(stderr, "Unable to open %s from %s: %s\n", input, getcwd(s, sizeof(s) - 1), strerror(errno));
		exit(1);
	}
	num = fscanf(fp, "PIE %d\n", &x);
	if (num != 1)
	{
		fprintf(stderr, "Bad PIE file %s\n", input);
		exit(1);
	}
 	if (x != 2 && x != 3)
	{
		fprintf(stderr, "Unknown PIE version %d in %s\n", x, input);
		exit(1);
	}
	num = fscanf(fp, "TYPE %x\n", &x); // ignore
	if (num != 1)
	{
		fprintf(stderr, "Bad TYPE directive in %s\n", input);
		exit(1);
	}
	if (!(x & iV_IMD_TEX))
	{
		fprintf(stderr, "Missing texture flag in %s\n", input);
		exit(1);
	}
	num = fscanf(fp, "TEXTURE %d %s %d %d\n", &z, s, &x, &y);
	if (num != 4)
	{
		fprintf(stderr, "Bad TEXTURE directive in %s\n", input);
		exit(1);
	}
	for (;;)
	{
		num = fscanf(fp, "EVENT %d %s\n", &x, s);
		if (num == 0)
			break;
		if (num != 2)
		{
			fprintf(stderr, "Bad EVENT directive in %s\n", input);
			exit(1);
		}
		if (x < 1
		 || x > 3)
		{
			fprintf(stderr, "Bad type for EVENT directive in %s: %d\n", input, x);
			exit(1);
		}
	}
	num = fscanf(fp, "LEVELS %d\n", &levels);
	if (num != 1)
	{
		fprintf(stderr, "Bad LEVELS directive in %s\n", input);
		exit(1);
	}
	for (level = 0; level < levels; level++)
	{
		int j, points, faces;

		num = fscanf(fp, "\nLEVEL %d\n", &x);
		if (num != 1)
		{
			fprintf(stderr, "Bad LEVEL directive in %s.\n", input);
			exit(1);
		}
		if (level + 1 != x)
		{
			fprintf(stderr, "LEVEL directive in %s was %d should be %d.\n", input, x, level + 1);
			exit(1);
		}
		num = fscanf(fp, "POINTS %d\n", &points);
		if (num != 1)
		{
			fprintf(stderr, "Bad POINTS directive in %s, level %d.\n", input, level);
			exit(1);
		}
		for (j = 0; j < points; j++)
		{
			double a, b, c;
			num = fscanf(fp, "%lf %lf %lf\n", &a, &b, &c);
			if (num != 3)
			{
				fprintf(stderr, "File %s. Bad POINTS entry level %d, number %d.\n", input, level, j);
				exit(1);
			}
		}
		num = fscanf(fp, "POLYGONS %d", &faces);
		if (num != 1)
		{
			fprintf(stderr, "Bad POLYGONS directive in %s, level %d.\n", input, level);
			exit(1);
		}
		for (j = 0; j < faces; ++j)
		{
			int k;
			unsigned int flags;

			num = fscanf(fp, "\n%x", &flags);
			if (num != 1)
			{
				fprintf(stderr, "File %s. Bad POLYGONS flag entry level %d, number %d\n", input, level, j);
				exit(1);
			}
			if (!(flags & iV_IMD_TEX))
			{
				fprintf(stderr, "File %s. Bad polygon flag entry level %d, number %d - no texture flag!\n", input, level, j);
				exit(1);
			}
			if (flags & iV_IMD_XNOCUL)
			{
				fprintf(stderr, "File %s. Bad polygon flag entry level %d, number %d - face culling not supported anymore!\n", input, level, j);
				exit(1);
			}
			num = fscanf(fp, "%d", &k);
			if (num != 1)
			{
				fprintf(stderr, "File %s. Bad POLYGONS vertices entry level %d, number %d\n", input, level, j);
				exit(1);
			}
			if (k != 3)
			{
				fprintf(stderr, "File %s. Bad POLYGONS vertices entry level %d, number %d -- non-triangle polygon found\n", input, level, j);
				exit(1);
			}
			// Read in vertex indices and texture coordinates
			for (k = 0; k < 3; k++)
			{
				int l;
				num = fscanf(fp, "%d", &l);
				if (num != 1)
				{
					fprintf(stderr, "File %s. Bad vertex position entry level %d, number %d\n", input, level, j);
					exit(1);
				}
			}
			if (flags & iV_IMD_TEXANIM)
			{
				int frames, rate; float width, height;
				num = fscanf(fp, "%d %d %f %f", &frames, &rate, &width, &height);
				if (num != 4)
				{
					fprintf(stderr, "File %s - Bad texture animation entry level %d, number %d.\n", input, level, j);
					exit(1);
				}
				if (frames <= 1)
				{
					fprintf(stderr, "File %s - Level %d, polygon %d has a single animation frame.\n", input, level, j);
					exit(1);
				}
			}
			for (k = 0; k < 3; k++)
			{
				double t, u;
				num = fscanf(fp, "%lf %lf", &t, &u);
				if (num != 2)
				{
					fprintf(stderr, "File %s. Bad texture coordinate entry level %d, number %d\n", input, level, j);
					exit(1);
				}
			}
		}
		num = fscanf(fp, "\nCONNECTORS %d", &x);
		if (num == 1)
		{
			if (x < 0)
			{
				fprintf(stderr, "Bad CONNECTORS directive in %s, level %d\n", input, level);
				exit(EXIT_FAILURE);
			}
			for (j = 0; j < x; ++j)
			{
				int a, b, c;

				num = fscanf(fp, "\n%d %d %d", &a, &b, &c);
				if (num != 3)
				{
					fprintf(stderr, "File %s. Bad CONNECTORS directive entry level %d, number %d\n", input, level, j);
					exit(1);
				}
			}
		}
		num = fscanf(fp, "\nANIMOBJECT %*d %*d %d", &x);
		if (num == 1)
		{
			if (x < 0)
			{
				fprintf(stderr, "Bad ANIMOBJECT directive in %s, level %d\n", input, level);
				exit(1);
			}
			for (j = 0; j < x; ++j)
			{
				int frame;
				float xpos, ypos, zpos, xrot, yrot, zrot, xscale, yscale, zscale;

				num = fscanf(fp, "\n%d %f %f %f %f %f %f %f %f %f",
						&frame, &xpos, &ypos, &zpos, &xrot, &yrot, &zrot, &xscale, &yscale, &zscale);
				if (num != 10)
				{
					fprintf(stderr, "File %s. Bad ANIMOBJECT directive entry level %d, number %d\n", input, level, j);
					exit(1);
				}
			}
		}
	}
	fclose(fp);
}

int main(int argc, char **argv)
{
	char datapath[PATH_MAX], fullpath[PATH_MAX];
	FILE *fp = fopen("modellist.txt", "r");
	if (!fp)
	{
		fprintf(stderr, "%s: Failed to open list file\n", argv[0]);
		return -1;
	}
	strcpy(datapath, getenv("srcdir"));
	strcat(datapath, "/../data/");
	while (!feof(fp))
	{
		char filename[PATH_MAX];

		if (fscanf(fp, "%254s\n", filename) != 1)
		{
			fprintf(stderr, "%s: Failed to read filename\n", argv[0]);
			return -1;
		}
		printf("Testing model: %s\n", filename);
		strcpy(fullpath, datapath);
		strcat(fullpath, filename);
		check_pie(fullpath);
	}
	fclose(fp);
	return 0;
}