File: gt2aMath.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (364 lines) | stat: -rw-r--r-- 4,748 bytes parent folder | download | duplicates (2)
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
/*
GameSpy GT2 SDK
GT2Action - sample app
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2000 GameSpy Industries, Inc

*/

#include <math.h>
#include <float.h>
#include <limits.h>
#include "gt2aMain.h"
#include "gt2aMath.h"

#define PI                3.1415926535897932384626433832795
#define PI_DIV_180        (PI / 180)

byte * CopyV3b
(
	V3b dest,
	const V3b src
)
{
	dest[0] = src[0];
	dest[1] = src[1];
	dest[2] = src[2];

	return dest;
}

byte * SetV3b
(
	V3b dest,
	byte src0,
	byte src1,
	byte src2
)
{
	dest[0] = src0;
	dest[1] = src1;
	dest[2] = src2;

	return dest;
}

float * CopyV2f
(
	V2f dest,
	const V2f src
)
{
	dest[0] = src[0];
	dest[1] = src[1];

	return dest;
}

float * SetV2f
(
	V2f dest,
	float src0,
	float src1
)
{
	dest[0] = src0;
	dest[1] = src1;

	return dest;
}

float * ScaleV2f
(
	V2f dest,
	const V2f src,
	float scale
)
{
	dest[0] = (src[0] * scale);
	dest[1] = (src[1] * scale);

	return dest;
}

float * AddV2f
(
	V2f dest,
	const V2f src0,
	const V2f src1
)
{
	dest[0] = (src0[0] + src1[0]);
	dest[1] = (src0[1] + src1[1]);

	return dest;
}

float * SubV2f
(
	V2f dest,
	const V2f src0,
	const V2f src1
)
{
	dest[0] = (src0[0] - src1[0]);
	dest[1] = (src0[1] - src1[1]);

	return dest;
}

float * SubScalarV2f
(
	V2f dest,
	const V2f src,
	float scalar
)
{
	dest[0] = (src[0] - scalar);
	dest[1] = (src[1] - scalar);

	return dest;
}

float LenV2f
(
	const V2f src
)
{
	return (float)sqrt((src[0] * src[0]) + (src[1] * src[1]));
}

float DistanceV2f
(
	const V2f src0,
	const V2f src1
)
{
	V2f temp;

	SubV2f(temp, src0, src1);

	return LenV2f(temp);
}

float * ClampV2f
(
	V2f dest,
	const V2f src,
	float min,
	float max
)
{
	float temp;
	int i;

	for(i = 0 ; i < 2 ; i++)
	{
		temp = src[i];
		if(temp > max)
			dest[i] = max;
		else if(temp < min)
			dest[i] = min;
		else
			dest[i] = temp;
	}

	return dest;
}

float ClampRotation
(
	float rotation
)
{
	while(rotation >= 360)
		rotation -= 360;
	while(rotation < 0)
		rotation += 360;
	return rotation;
}

int ClampInt
(
	int num,
	int min,
	int max
)
{
	if(num < min)
		num = min;
	else if(num > max)
		num = max;
	return num;
}

float * RotationToVector
(
	V2f dest,
	float rotation
)
{
	// Convert to radians.
	//////////////////////
	rotation = (float)(rotation * PI_DIV_180);

	dest[0] = (float)sin(rotation);
	dest[1] = (float)cos(rotation);

	return dest;
}

float * ComputeNewPosition
(
	V2f dest,
	const V2f position,
	int motion,
	float rotation,
	unsigned long diff,
	int unitsPerSec,
	GT2Bool clamp
)
{
	V2f forward;
	float scale;

	// Check for no motion.
	///////////////////////
	if(!motion)
	{
		CopyV2f(dest, position);
		return dest;
	}

	// Get the direction we're facing.
	//////////////////////////////////
	RotationToVector(forward, rotation);

	// Scale it based on the amount of time.
	////////////////////////////////////////
	scale = (float)(diff * unitsPerSec / 1000);
	if(motion == BACKWARD)
		scale = -scale;
	ScaleV2f(forward, forward, scale);

	// Update the position.
	///////////////////////
	AddV2f(dest, position, forward);

	// Clamp it.
	////////////
	if(clamp)
		ClampV2f(dest, dest, 0, MAP_MAX);

	return dest;
}

float ComputeNewRotation
(
	float rotation,
	int turning,
	unsigned long diff,
	float degreesPerMilliSec
)
{
	// Turn.
	////////
	rotation += (turning * (diff * degreesPerMilliSec));

	// Clamp it to between 0 and 360.
	/////////////////////////////////
	while(rotation >= 360)
		rotation -= 360;
	while(rotation < 0)
		rotation += 360;

	return rotation;
}

float RandomFloat
(
	float minFloat,
	float maxFloat,
	GT2Bool maxInclusive
)
{
	float num;

	num = rand();
	num *= (maxFloat - minFloat);
	num /= (RAND_MAX + (maxInclusive?0:1));
	num += minFloat;

	return num;
}

int RandomInt
(
	int minInt,
	int maxInt
)
{
	return (minInt + ((rand() * (maxInt - minInt + 1)) / (RAND_MAX + 1)));
}

unsigned short RotationToUnsignedShort
(
	float rotation
)
{
	rotation *= 0xFFFF;
	rotation /= 360;
	if(rotation < 0)
		rotation = 0;
	if(rotation > 0xFFFF)
		rotation = 0xFFFF;

	return (unsigned short)rotation;
}

float UnsignedShortToRotation
(
	unsigned short packedRotation
)
{
	float rotation;

	rotation = packedRotation;
	rotation *= 360;
	rotation /= 0xFFFF;

	return ClampRotation(rotation);
}

unsigned short PositionToUnsignedShort
(
	float position
)
{
	position += (MAP_EXTRA / 2);
	position *= 0xFFFF;
	position /= (MAP_MAX + MAP_EXTRA);
	if(position < 0)
		position = 0;
	else if(position > USHRT_MAX)
		position = USHRT_MAX;

	return (unsigned short)position;
}

float UnsignedShortToPosition
(
	unsigned short packedPosition
)
{
	float position;

	position = packedPosition;
	position *= (MAP_MAX + MAP_EXTRA);
	position /= 0xFFFF;
	position -= (MAP_EXTRA / 2);

	return position;
}