1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2000-2006 Tim Angus
5
6 This file is part of Tremulous.
7
8 Tremulous is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 Tremulous is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Tremulous; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ===========================================================================
22 */
23 //
24 #ifndef __Q_SHARED_H
25 #define __Q_SHARED_H
26
27 // q_shared.h -- included first by ALL program modules.
28 // A user mod should never modify this file
29
30 #define Q3_VERSION "tremulous 1.1.0"
31
32 #define MAX_TEAMNAME 32
33
34 #ifdef _MSC_VER
35
36 #pragma warning(disable : 4018) // signed/unsigned mismatch
37 #pragma warning(disable : 4032)
38 #pragma warning(disable : 4051)
39 #pragma warning(disable : 4057) // slightly different base types
40 #pragma warning(disable : 4100) // unreferenced formal parameter
41 #pragma warning(disable : 4115)
42 #pragma warning(disable : 4125) // decimal digit terminates octal escape sequence
43 #pragma warning(disable : 4127) // conditional expression is constant
44 #pragma warning(disable : 4136)
45 #pragma warning(disable : 4152) // nonstandard extension, function/data pointer conversion in expression
46 //#pragma warning(disable : 4201)
47 //#pragma warning(disable : 4214)
48 #pragma warning(disable : 4244)
49 #pragma warning(disable : 4142) // benign redefinition
50 //#pragma warning(disable : 4305) // truncation from const double to float
51 //#pragma warning(disable : 4310) // cast truncates constant value
52 //#pragma warning(disable: 4505) // unreferenced local function has been removed
53 #pragma warning(disable : 4514)
54 #pragma warning(disable : 4702) // unreachable code
55 #pragma warning(disable : 4711) // selected for automatic inline expansion
56 #pragma warning(disable : 4220) // varargs matches remaining parameters
57 //#pragma intrinsic( memset, memcpy )
58 #endif
59
60 //Ignore __attribute__ on non-gcc platforms
61 #ifndef __GNUC__
62 #ifndef __attribute__
63 #define __attribute__(x)
64 #endif
65 #endif
66
67 /**********************************************************************
68 VM Considerations
69
70 The VM can not use the standard system headers because we aren't really
71 using the compiler they were meant for. We use bg_lib.h which contains
72 prototypes for the functions we define for our own use in bg_lib.c.
73
74 When writing mods, please add needed headers HERE, do not start including
75 stuff like <stdio.h> in the various .c files that make up each of the VMs
76 since you will be including system headers files can will have issues.
77
78 Remember, if you use a C library function that is not defined in bg_lib.c,
79 you will have to add your own version for support in the VM.
80
81 **********************************************************************/
82
83 #ifdef Q3_VM
84
85 #include "../game/bg_lib.h"
86
87 #else
88
89 #include <assert.h>
90 #include <math.h>
91 #include <stdio.h>
92 #include <stdarg.h>
93 #include <string.h>
94 #include <stdlib.h>
95 #include <time.h>
96 #include <ctype.h>
97 #include <limits.h>
98
99 #endif
100
101 #include "q_platform.h"
102
103 //=============================================================
104
105 #ifdef Q3_VM
106 typedef int intptr_t;
107 #else
108 # ifndef _MSC_VER
109 # include <stdint.h>
110 # endif
111 #endif
112
113 typedef unsigned char byte;
114
115 typedef enum {qfalse, qtrue} qboolean;
116
117 typedef int qhandle_t;
118 typedef int sfxHandle_t;
119 typedef int fileHandle_t;
120 typedef int clipHandle_t;
121
122 #define PAD(x,y) (((x)+(y)-1) & ~((y)-1))
123
124 #ifdef __GNUC__
125 #define ALIGN(x) __attribute__((aligned(x)))
126 #else
127 #define ALIGN(x)
128 #endif
129
130 #ifndef NULL
131 #define NULL ((void *)0)
132 #endif
133
134 #define MAX_QINT 0x7fffffff
135 #define MIN_QINT (-MAX_QINT-1)
136
137
138 // angle indexes
139 #define PITCH 0 // up / down
140 #define YAW 1 // left / right
141 #define ROLL 2 // fall over
142
143 // the game guarantees that no string from the network will ever
144 // exceed MAX_STRING_CHARS
145 #define MAX_STRING_CHARS 1024 // max length of a string passed to Cmd_TokenizeString
146 #define MAX_STRING_TOKENS 1024 // max tokens resulting from Cmd_TokenizeString
147 #define MAX_TOKEN_CHARS 1024 // max length of an individual token
148
149 #define MAX_INFO_STRING 1024
150 #define MAX_INFO_KEY 1024
151 #define MAX_INFO_VALUE 1024
152
153 #define BIG_INFO_STRING 8192 // used for system info key only
154 #define BIG_INFO_KEY 8192
155 #define BIG_INFO_VALUE 8192
156
157
158 #define MAX_QPATH 64 // max length of a quake game pathname
159 #ifdef PATH_MAX
160 #define MAX_OSPATH PATH_MAX
161 #else
162 #define MAX_OSPATH 256 // max length of a filesystem pathname
163 #endif
164
165 #define MAX_NAME_LENGTH 32 // max length of a client name
166
167 #define MAX_SAY_TEXT 150
168
169 // paramters for command buffer stuffing
170 typedef enum {
171 EXEC_NOW, // don't return until completed, a VM should NEVER use this,
172 // because some commands might cause the VM to be unloaded...
173 EXEC_INSERT, // insert at current position, but don't run yet
174 EXEC_APPEND // add to end of the command buffer (normal case)
175 } cbufExec_t;
176
177
178 //
179 // these aren't needed by any of the VMs. put in another header?
180 //
181 #define MAX_MAP_AREA_BYTES 32 // bit vector of area visibility
182
183
184 // print levels from renderer (FIXME: set up for game / cgame?)
185 typedef enum {
186 PRINT_ALL,
187 PRINT_DEVELOPER, // only print when "developer 1"
188 PRINT_WARNING,
189 PRINT_ERROR
190 } printParm_t;
191
192
193 #ifdef ERR_FATAL
194 #undef ERR_FATAL // this is be defined in malloc.h
195 #endif
196
197 // parameters to the main Error routine
198 typedef enum {
199 ERR_FATAL, // exit the entire game with a popup window
200 ERR_DROP, // print to console and disconnect from game
201 ERR_SERVERDISCONNECT, // don't kill server
202 ERR_DISCONNECT, // client disconnected from the server
203 ERR_NEED_CD // pop up the need-cd dialog
204 } errorParm_t;
205
206
207 // font rendering values used by ui and cgame
208
209 #define PROP_GAP_WIDTH 3
210 #define PROP_SPACE_WIDTH 8
211 #define PROP_HEIGHT 27
212 #define PROP_SMALL_SIZE_SCALE 0.75
213
214 #define BLINK_DIVISOR 200
215 #define PULSE_DIVISOR 75
216
217 #define UI_LEFT 0x00000000 // default
218 #define UI_CENTER 0x00000001
219 #define UI_RIGHT 0x00000002
220 #define UI_FORMATMASK 0x00000007
221 #define UI_SMALLFONT 0x00000010
222 #define UI_BIGFONT 0x00000020 // default
223 #define UI_GIANTFONT 0x00000040
224 #define UI_DROPSHADOW 0x00000800
225 #define UI_BLINK 0x00001000
226 #define UI_INVERSE 0x00002000
227 #define UI_PULSE 0x00004000
228
229 #if defined(_DEBUG) && !defined(BSPC)
230 #define HUNK_DEBUG
231 #endif
232
233 typedef enum {
234 h_high,
235 h_low,
236 h_dontcare
237 } ha_pref;
238
239 #ifdef HUNK_DEBUG
240 #define Hunk_Alloc( size, preference ) Hunk_AllocDebug(size, preference, #size, __FILE__, __LINE__)
241 void *Hunk_AllocDebug( int size, ha_pref preference, char *label, char *file, int line );
242 #else
243 void *Hunk_Alloc( int size, ha_pref preference );
244 #endif
245
246 #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(MACOS_X)
247 // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=371
248 // custom Snd_Memset implementation for glibc memset bug workaround
249 void Snd_Memset (void* dest, const int val, const size_t count);
250 #else
251 #define Snd_Memset Com_Memset
252 #endif
253
254 #define Com_Memset memset
255 #define Com_Memcpy memcpy
256
257 #define CIN_system 1
258 #define CIN_loop 2
259 #define CIN_hold 4
260 #define CIN_silent 8
261 #define CIN_shader 16
262
263 /*
264 ==============================================================
265
266 MATHLIB
267
268 ==============================================================
269 */
270
271
272 typedef float vec_t;
273 typedef vec_t vec2_t[2];
274 typedef vec_t vec3_t[3];
275 typedef vec_t vec4_t[4];
276 typedef vec_t vec5_t[5];
277
278 typedef int fixed4_t;
279 typedef int fixed8_t;
280 typedef int fixed16_t;
281
282 #ifndef M_PI
283 #define M_PI 3.14159265358979323846f // matches value in gcc v2 math.h
284 #endif
285
286 #ifndef M_SQRT2
287 #define M_SQRT2 1.414213562f
288 #endif
289
290 #ifndef M_ROOT3
291 #define M_ROOT3 1.732050808f
292 #endif
293
294 #define NUMVERTEXNORMALS 162
295 extern vec3_t bytedirs[NUMVERTEXNORMALS];
296
297 // all drawing is done to a 640*480 virtual screen size
298 // and will be automatically scaled to the real resolution
299 #define SCREEN_WIDTH 640
300 #define SCREEN_HEIGHT 480
301
302 #define TINYCHAR_WIDTH (SMALLCHAR_WIDTH)
303 #define TINYCHAR_HEIGHT (SMALLCHAR_HEIGHT/2)
304
305 #define SMALLCHAR_WIDTH 8
306 #define SMALLCHAR_HEIGHT 16
307
308 #define BIGCHAR_WIDTH 16
309 #define BIGCHAR_HEIGHT 16
310
311 #define GIANTCHAR_WIDTH 32
312 #define GIANTCHAR_HEIGHT 48
313
314 extern vec4_t colorBlack;
315 extern vec4_t colorRed;
316 extern vec4_t colorGreen;
317 extern vec4_t colorBlue;
318 extern vec4_t colorYellow;
319 extern vec4_t colorMagenta;
320 extern vec4_t colorCyan;
321 extern vec4_t colorWhite;
322 extern vec4_t colorLtGrey;
323 extern vec4_t colorMdGrey;
324 extern vec4_t colorDkGrey;
325
326 #define Q_COLOR_ESCAPE '^'
327 #define Q_IsColorString(p) ( p && *(p) == Q_COLOR_ESCAPE && *((p)+1) && *((p)+1) != Q_COLOR_ESCAPE )
328
329 #define COLOR_BLACK '0'
330 #define COLOR_RED '1'
331 #define COLOR_GREEN '2'
332 #define COLOR_YELLOW '3'
333 #define COLOR_BLUE '4'
334 #define COLOR_CYAN '5'
335 #define COLOR_MAGENTA '6'
336 #define COLOR_WHITE '7'
337 #define ColorIndex(c) ( ( (c) - '0' ) & 7 )
338
339 #define S_COLOR_BLACK "^0"
340 #define S_COLOR_RED "^1"
341 #define S_COLOR_GREEN "^2"
342 #define S_COLOR_YELLOW "^3"
343 #define S_COLOR_BLUE "^4"
344 #define S_COLOR_CYAN "^5"
345 #define S_COLOR_MAGENTA "^6"
346 #define S_COLOR_WHITE "^7"
347
348 extern vec4_t g_color_table[8];
349
350 #define MAKERGB( v, r, g, b ) v[0]=r;v[1]=g;v[2]=b
351 #define MAKERGBA( v, r, g, b, a ) v[0]=r;v[1]=g;v[2]=b;v[3]=a
352
353 #define DEG2RAD( a ) ( ( (a) * M_PI ) / 180.0F )
354 #define RAD2DEG( a ) ( ( (a) * 180.0f ) / M_PI )
355
356 struct cplane_s;
357
358 extern vec3_t vec3_origin;
359 extern vec3_t axisDefault[3];
360
361 #define nanmask (255<<23)
362
363 #define IS_NAN(x) (((*(int *)&x)&nanmask)==nanmask)
364
365 #if idppc
366
Q_rsqrt(float number)367 static ID_INLINE float Q_rsqrt( float number ) {
368 float x = 0.5f * number;
369 float y;
370 #ifdef __GNUC__
371 asm("frsqrte %0,%1" : "=f" (y) : "f" (number));
372 #else
373 y = __frsqrte( number );
374 #endif
375 return y * (1.5f - (x * y * y));
376 }
377
378 #ifdef __GNUC__
Q_fabs(float x)379 static ID_INLINE float Q_fabs(float x) {
380 float abs_x;
381
382 asm("fabs %0,%1" : "=f" (abs_x) : "f" (x));
383 return abs_x;
384 }
385 #else
386 #define Q_fabs __fabsf
387 #endif
388
389 #else
390 float Q_fabs( float f );
391 float Q_rsqrt( float f ); // reciprocal square root
392 #endif
393
394 #define SQRTFAST( x ) ( (x) * Q_rsqrt( x ) )
395
396 signed char ClampChar( int i );
397 signed short ClampShort( int i );
398
399 // this isn't a real cheap function to call!
400 int DirToByte( vec3_t dir );
401 void ByteToDir( int b, vec3_t dir );
402
403 #if 1
404
405 #define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2])
406 #define VectorSubtract(a,b,c) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2])
407 #define VectorAdd(a,b,c) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2])
408 #define VectorCopy(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2])
409 #define VectorScale(v, s, o) ((o)[0]=(v)[0]*(s),(o)[1]=(v)[1]*(s),(o)[2]=(v)[2]*(s))
410 #define VectorMA(v, s, b, o) ((o)[0]=(v)[0]+(b)[0]*(s),(o)[1]=(v)[1]+(b)[1]*(s),(o)[2]=(v)[2]+(b)[2]*(s))
411
412 #else
413
414 #define DotProduct(x,y) _DotProduct(x,y)
415 #define VectorSubtract(a,b,c) _VectorSubtract(a,b,c)
416 #define VectorAdd(a,b,c) _VectorAdd(a,b,c)
417 #define VectorCopy(a,b) _VectorCopy(a,b)
418 #define VectorScale(v, s, o) _VectorScale(v,s,o)
419 #define VectorMA(v, s, b, o) _VectorMA(v,s,b,o)
420
421 #endif
422
423 #ifdef Q3_VM
424 #ifdef VectorCopy
425 #undef VectorCopy
426 // this is a little hack to get more efficient copies in our interpreter
427 typedef struct {
428 float v[3];
429 } vec3struct_t;
430 #define VectorCopy(a,b) (*(vec3struct_t *)b=*(vec3struct_t *)a)
431 #endif
432 #endif
433
434 #define Vector2Set(v, x, y) ((v)[0]=(x), (v)[1]=(y))
435 #define VectorClear(a) ((a)[0]=(a)[1]=(a)[2]=0)
436 #define VectorNegate(a,b) ((b)[0]=-(a)[0],(b)[1]=-(a)[1],(b)[2]=-(a)[2])
437 #define VectorSet(v, x, y, z) ((v)[0]=(x), (v)[1]=(y), (v)[2]=(z))
438 #define Vector4Copy(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2],(b)[3]=(a)[3])
439 #define Vector4Add(a,b,c) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2],(c)[3]=(a)[3]+(b)[3])
440
441 #define SnapVector(v) {v[0]=((int)(v[0]));v[1]=((int)(v[1]));v[2]=((int)(v[2]));}
442 // just in case you do't want to use the macros
443 vec_t _DotProduct( const vec3_t v1, const vec3_t v2 );
444 void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out );
445 void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out );
446 void _VectorCopy( const vec3_t in, vec3_t out );
447 void _VectorScale( const vec3_t in, float scale, vec3_t out );
448 void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc );
449
450 unsigned ColorBytes3 (float r, float g, float b);
451 unsigned ColorBytes4 (float r, float g, float b, float a);
452
453 float NormalizeColor( const vec3_t in, vec3_t out );
454
455 float RadiusFromBounds( const vec3_t mins, const vec3_t maxs );
456 void ClearBounds( vec3_t mins, vec3_t maxs );
457 void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs );
458
459 #if !defined( Q3_VM ) || ( defined( Q3_VM ) && defined( __Q3_VM_MATH ) )
VectorCompare(const vec3_t v1,const vec3_t v2)460 static ID_INLINE int VectorCompare( const vec3_t v1, const vec3_t v2 ) {
461 if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
462 return 0;
463 }
464 return 1;
465 }
466
VectorCompareEpsilon(const vec3_t v1,const vec3_t v2,float epsilon)467 static ID_INLINE int VectorCompareEpsilon(
468 const vec3_t v1, const vec3_t v2, float epsilon )
469 {
470 vec3_t d;
471
472 VectorSubtract( v1, v2, d );
473 d[ 0 ] = fabs( d[ 0 ] );
474 d[ 1 ] = fabs( d[ 1 ] );
475 d[ 2 ] = fabs( d[ 2 ] );
476
477 if( d[ 0 ] > epsilon || d[ 1 ] > epsilon || d[ 2 ] > epsilon )
478 return 0;
479
480 return 1;
481 }
482
VectorLength(const vec3_t v)483 static ID_INLINE vec_t VectorLength( const vec3_t v ) {
484 return (vec_t)sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
485 }
486
VectorLengthSquared(const vec3_t v)487 static ID_INLINE vec_t VectorLengthSquared( const vec3_t v ) {
488 return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
489 }
490
Distance(const vec3_t p1,const vec3_t p2)491 static ID_INLINE vec_t Distance( const vec3_t p1, const vec3_t p2 ) {
492 vec3_t v;
493
494 VectorSubtract (p2, p1, v);
495 return VectorLength( v );
496 }
497
DistanceSquared(const vec3_t p1,const vec3_t p2)498 static ID_INLINE vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 ) {
499 vec3_t v;
500
501 VectorSubtract (p2, p1, v);
502 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
503 }
504
505 // fast vector normalize routine that does not check to make sure
506 // that length != 0, nor does it return length, uses rsqrt approximation
VectorNormalizeFast(vec3_t v)507 static ID_INLINE void VectorNormalizeFast( vec3_t v )
508 {
509 float ilength;
510
511 ilength = Q_rsqrt( DotProduct( v, v ) );
512
513 v[0] *= ilength;
514 v[1] *= ilength;
515 v[2] *= ilength;
516 }
517
VectorInverse(vec3_t v)518 static ID_INLINE void VectorInverse( vec3_t v ){
519 v[0] = -v[0];
520 v[1] = -v[1];
521 v[2] = -v[2];
522 }
523
CrossProduct(const vec3_t v1,const vec3_t v2,vec3_t cross)524 static ID_INLINE void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ) {
525 cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
526 cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
527 cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
528 }
529
530 #else
531 int VectorCompare( const vec3_t v1, const vec3_t v2 );
532
533 vec_t VectorLength( const vec3_t v );
534
535 vec_t VectorLengthSquared( const vec3_t v );
536
537 vec_t Distance( const vec3_t p1, const vec3_t p2 );
538
539 vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 );
540
541 void VectorNormalizeFast( vec3_t v );
542
543 void VectorInverse( vec3_t v );
544
545 void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross );
546
547 #endif
548
549 vec_t VectorNormalize (vec3_t v); // returns vector length
550 vec_t VectorNormalize2( const vec3_t v, vec3_t out );
551 void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out );
552 void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out );
553 int Q_log2(int val);
554
555 float Q_acos(float c);
556
557 int Q_rand( int *seed );
558 float Q_random( int *seed );
559 float Q_crandom( int *seed );
560
561 #define random() ((rand () & 0x7fff) / ((float)0x7fff))
562 #define crandom() (2.0 * (random() - 0.5))
563
564 void vectoangles( const vec3_t value1, vec3_t angles);
565 void AnglesToAxis( const vec3_t angles, vec3_t axis[3] );
566 void AxisToAngles( vec3_t axis[3], vec3_t angles );
567
568 void AxisClear( vec3_t axis[3] );
569 void AxisCopy( vec3_t in[3], vec3_t out[3] );
570
571 void SetPlaneSignbits( struct cplane_s *out );
572 int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *plane);
573
574 float AngleMod(float a);
575 float LerpAngle (float from, float to, float frac);
576 float AngleSubtract( float a1, float a2 );
577 void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 );
578
579 float AngleNormalize360 ( float angle );
580 float AngleNormalize180 ( float angle );
581 float AngleDelta ( float angle1, float angle2 );
582
583 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
584 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal );
585 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
586 void RotateAroundDirection( vec3_t axis[3], vec_t angle );
587 void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up );
588 // perpendicular vector could be replaced by this
589
590 //int PlaneTypeForNormal (vec3_t normal);
591
592 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]);
593 void VectorMatrixMultiply( const vec3_t p, vec3_t m[ 3 ], vec3_t out );
594 void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
595 void PerpendicularVector( vec3_t dst, const vec3_t src );
596 int Q_isnan( float x );
597
598 void GetPerpendicularViewVector( const vec3_t point, const vec3_t p1,
599 const vec3_t p2, vec3_t up );
600 void ProjectPointOntoVector( vec3_t point, vec3_t vStart,
601 vec3_t vEnd, vec3_t vProj );
602 float VectorDistance( vec3_t v1, vec3_t v2 );
603
604 float pointToLineDistance( const vec3_t point, const vec3_t p1, const vec3_t p2 );
605 float VectorMinComponent( vec3_t v );
606 float VectorMaxComponent( vec3_t v );
607
608 vec_t DistanceBetweenLineSegmentsSquared(
609 const vec3_t sP0, const vec3_t sP1,
610 const vec3_t tP0, const vec3_t tP1,
611 float *s, float *t );
612 vec_t DistanceBetweenLineSegments(
613 const vec3_t sP0, const vec3_t sP1,
614 const vec3_t tP0, const vec3_t tP1,
615 float *s, float *t );
616
617 #ifndef MAX
618 #define MAX(x,y) (x)>(y)?(x):(y)
619 #endif
620
621 #ifndef MIN
622 #define MIN(x,y) (x)<(y)?(x):(y)
623 #endif
624
625 //=============================================
626
627 float Com_Clamp( float min, float max, float value );
628
629 char *COM_SkipPath( char *pathname );
630 void COM_StripExtension( const char *in, char *out );
631 void COM_DefaultExtension( char *path, int maxSize, const char *extension );
632
633 void COM_BeginParseSession( const char *name );
634 int COM_GetCurrentParseLine( void );
635 char *COM_Parse( char **data_p );
636 char *COM_ParseExt( char **data_p, qboolean allowLineBreak );
637 int COM_Compress( char *data_p );
638 void COM_ParseError( char *format, ... );
639 void COM_ParseWarning( char *format, ... );
640 //int COM_ParseInfos( char *buf, int max, char infos[][MAX_INFO_STRING] );
641
642 #define MAX_TOKENLENGTH 1024
643
644 #ifndef TT_STRING
645 //token types
646 #define TT_STRING 1 // string
647 #define TT_LITERAL 2 // literal
648 #define TT_NUMBER 3 // number
649 #define TT_NAME 4 // name
650 #define TT_PUNCTUATION 5 // punctuation
651 #endif
652
653 typedef struct pc_token_s
654 {
655 int type;
656 int subtype;
657 int intvalue;
658 float floatvalue;
659 char string[MAX_TOKENLENGTH];
660 } pc_token_t;
661
662 // data is an in/out parm, returns a parsed out token
663
664 void COM_MatchToken( char**buf_p, char *match );
665
666 void SkipBracedSection (char **program);
667 void SkipRestOfLine ( char **data );
668
669 void Parse1DMatrix (char **buf_p, int x, float *m);
670 void Parse2DMatrix (char **buf_p, int y, int x, float *m);
671 void Parse3DMatrix (char **buf_p, int z, int y, int x, float *m);
672
673 void QDECL Com_sprintf (char *dest, int size, const char *fmt, ...);
674
675 char *Com_SkipTokens( char *s, int numTokens, char *sep );
676 char *Com_SkipCharset( char *s, char *sep );
677
678 // mode parm for FS_FOpenFile
679 typedef enum {
680 FS_READ,
681 FS_WRITE,
682 FS_APPEND,
683 FS_APPEND_SYNC
684 } fsMode_t;
685
686 typedef enum {
687 FS_SEEK_CUR,
688 FS_SEEK_END,
689 FS_SEEK_SET
690 } fsOrigin_t;
691
692 //=============================================
693
694 int Q_isprint( int c );
695 int Q_islower( int c );
696 int Q_isupper( int c );
697 int Q_isalpha( int c );
698
699 // portable case insensitive compare
700 int Q_stricmp (const char *s1, const char *s2);
701 int Q_strncmp (const char *s1, const char *s2, int n);
702 int Q_stricmpn (const char *s1, const char *s2, int n);
703 char *Q_strlwr( char *s1 );
704 char *Q_strupr( char *s1 );
705 char *Q_strrchr( const char* string, int c );
706
707 // buffer size safe library replacements
708 void Q_strncpyz( char *dest, const char *src, int destsize );
709 void Q_strcat( char *dest, int size, const char *src );
710
711 // strlen that discounts Quake color sequences
712 int Q_PrintStrlen( const char *string );
713 // removes color sequences from string
714 char *Q_CleanStr( char *string );
715
716 //=============================================
717
718 // 64-bit integers for global rankings interface
719 // implemented as a struct for qvm compatibility
720 typedef struct
721 {
722 byte b0;
723 byte b1;
724 byte b2;
725 byte b3;
726 byte b4;
727 byte b5;
728 byte b6;
729 byte b7;
730 } qint64;
731
732 //=============================================
733 /*
734 short BigShort(short l);
735 short LittleShort(short l);
736 int BigLong (int l);
737 int LittleLong (int l);
738 qint64 BigLong64 (qint64 l);
739 qint64 LittleLong64 (qint64 l);
740 float BigFloat (const float *l);
741 float LittleFloat (const float *l);
742
743 void Swap_Init (void);
744 */
745 char * QDECL va(char *format, ...);
746
747 #define TRUNCATE_LENGTH 64
748 void Com_TruncateLongString( char *buffer, const char *s );
749
750 //=============================================
751
752 //
753 // key / value info strings
754 //
755 char *Info_ValueForKey( const char *s, const char *key );
756 void Info_RemoveKey( char *s, const char *key );
757 void Info_RemoveKey_big( char *s, const char *key );
758 void Info_SetValueForKey( char *s, const char *key, const char *value );
759 void Info_SetValueForKey_Big( char *s, const char *key, const char *value );
760 qboolean Info_Validate( const char *s );
761 void Info_NextPair( const char **s, char *key, char *value );
762
763 // this is only here so the functions in q_shared.c and bg_*.c can link
764 void QDECL Com_Error( int level, const char *error, ... );
765 void QDECL Com_Printf( const char *msg, ... );
766
767
768 /*
769 ==========================================================
770
771 CVARS (console variables)
772
773 Many variables can be used for cheating purposes, so when
774 cheats is zero, force all unspecified variables to their
775 default values.
776 ==========================================================
777 */
778
779 #define CVAR_ARCHIVE 1 // set to cause it to be saved to vars.rc
780 // used for system variables, not for player
781 // specific configurations
782 #define CVAR_USERINFO 2 // sent to server on connect or change
783 #define CVAR_SERVERINFO 4 // sent in response to front end requests
784 #define CVAR_SYSTEMINFO 8 // these cvars will be duplicated on all clients
785 #define CVAR_INIT 16 // don't allow change from console at all,
786 // but can be set from the command line
787 #define CVAR_LATCH 32 // will only change when C code next does
788 // a Cvar_Get(), so it can't be changed
789 // without proper initialization. modified
790 // will be set, even though the value hasn't
791 // changed yet
792 #define CVAR_ROM 64 // display only, cannot be set by user at all
793 #define CVAR_USER_CREATED 128 // created by a set command
794 #define CVAR_TEMP 256 // can be set even when cheats are disabled, but is not archived
795 #define CVAR_CHEAT 512 // can not be changed if cheats are disabled
796 #define CVAR_NORESTART 1024 // do not clear when a cvar_restart is issued
797
798 // nothing outside the Cvar_*() functions should modify these fields!
799 typedef struct cvar_s {
800 char *name;
801 char *string;
802 char *resetString; // cvar_restart will reset to this value
803 char *latchedString; // for CVAR_LATCH vars
804 int flags;
805 qboolean modified; // set each time the cvar is changed
806 int modificationCount; // incremented each time the cvar is changed
807 float value; // atof( string )
808 int integer; // atoi( string )
809 struct cvar_s *next;
810 struct cvar_s *hashNext;
811 } cvar_t;
812
813 #define MAX_CVAR_VALUE_STRING 256
814
815 typedef int cvarHandle_t;
816
817 // the modules that run in the virtual machine can't access the cvar_t directly,
818 // so they must ask for structured updates
819 typedef struct {
820 cvarHandle_t handle;
821 int modificationCount;
822 float value;
823 int integer;
824 char string[MAX_CVAR_VALUE_STRING];
825 } vmCvar_t;
826
827 /*
828 ==============================================================
829
830 COLLISION DETECTION
831
832 ==============================================================
833 */
834
835 #include "surfaceflags.h" // shared with the q3map utility
836
837 // plane types are used to speed some tests
838 // 0-2 are axial planes
839 #define PLANE_X 0
840 #define PLANE_Y 1
841 #define PLANE_Z 2
842 #define PLANE_NON_AXIAL 3
843
844
845 /*
846 =================
847 PlaneTypeForNormal
848 =================
849 */
850
851 #define PlaneTypeForNormal(x) (x[0] == 1.0 ? PLANE_X : (x[1] == 1.0 ? PLANE_Y : (x[2] == 1.0 ? PLANE_Z : PLANE_NON_AXIAL) ) )
852
853 // plane_t structure
854 // !!! if this is changed, it must be changed in asm code too !!!
855 typedef struct cplane_s {
856 vec3_t normal;
857 float dist;
858 byte type; // for fast side tests: 0,1,2 = axial, 3 = nonaxial
859 byte signbits; // signx + (signy<<1) + (signz<<2), used as lookup during collision
860 byte pad[2];
861 } cplane_t;
862
863 typedef enum {
864 TT_NONE,
865
866 TT_AABB,
867 TT_CAPSULE,
868 TT_BISPHERE,
869
870 TT_NUM_TRACE_TYPES
871 } traceType_t;
872
873 // a trace is returned when a box is swept through the world
874 typedef struct {
875 qboolean allsolid; // if true, plane is not valid
876 qboolean startsolid; // if true, the initial point was in a solid area
877 float fraction; // time completed, 1.0 = didn't hit anything
878 vec3_t endpos; // final position
879 cplane_t plane; // surface normal at impact, transformed to world space
880 int surfaceFlags; // surface hit
881 int contents; // contents on other side of surface hit
882 int entityNum; // entity the contacted sirface is a part of
883 float lateralFraction; // fraction of collision tangetially to the trace direction
884 } trace_t;
885
886 // trace->entityNum can also be 0 to (MAX_GENTITIES-1)
887 // or ENTITYNUM_NONE, ENTITYNUM_WORLD
888
889
890 // markfragments are returned by CM_MarkFragments()
891 typedef struct {
892 int firstPoint;
893 int numPoints;
894 } markFragment_t;
895
896
897
898 typedef struct {
899 vec3_t origin;
900 vec3_t axis[3];
901 } orientation_t;
902
903 //=====================================================================
904
905
906 // in order from highest priority to lowest
907 // if none of the catchers are active, bound key strings will be executed
908 #define KEYCATCH_CONSOLE 0x0001
909 #define KEYCATCH_UI 0x0002
910 #define KEYCATCH_MESSAGE 0x0004
911 #define KEYCATCH_CGAME 0x0008
912
913
914 // sound channels
915 // channel 0 never willingly overrides
916 // other channels will allways override a playing sound on that channel
917 typedef enum {
918 CHAN_AUTO,
919 CHAN_LOCAL, // menu sounds, etc
920 CHAN_WEAPON,
921 CHAN_VOICE,
922 CHAN_ITEM,
923 CHAN_BODY,
924 CHAN_LOCAL_SOUND, // chat messages, etc
925 CHAN_ANNOUNCER // announcer voices, etc
926 } soundChannel_t;
927
928
929 /*
930 ========================================================================
931
932 ELEMENTS COMMUNICATED ACROSS THE NET
933
934 ========================================================================
935 */
936
937 #define ANGLE2SHORT(x) ((int)((x)*65536/360) & 65535)
938 #define SHORT2ANGLE(x) ((x)*(360.0/65536))
939
940 #define SNAPFLAG_RATE_DELAYED 1
941 #define SNAPFLAG_NOT_ACTIVE 2 // snapshot used during connection and for zombies
942 #define SNAPFLAG_SERVERCOUNT 4 // toggled every map_restart so transitions can be detected
943
944 //
945 // per-level limits
946 //
947 #define MAX_CLIENTS 64 // absolute limit
948 #define MAX_LOCATIONS 64
949
950 #define GENTITYNUM_BITS 10 // don't need to send any more
951 #define MAX_GENTITIES (1<<GENTITYNUM_BITS)
952
953 // entitynums are communicated with GENTITY_BITS, so any reserved
954 // values that are going to be communcated over the net need to
955 // also be in this range
956 #define ENTITYNUM_NONE (MAX_GENTITIES-1)
957 #define ENTITYNUM_WORLD (MAX_GENTITIES-2)
958 #define ENTITYNUM_MAX_NORMAL (MAX_GENTITIES-2)
959
960
961 #define MAX_MODELS 256 // these are sent over the net as 8 bits
962 #define MAX_SOUNDS 256 // so they cannot be blindly increased
963 #define MAX_GAME_SHADERS 64
964 #define MAX_GAME_PARTICLE_SYSTEMS 64
965
966
967 #define MAX_CONFIGSTRINGS 1024
968
969 // these are the only configstrings that the system reserves, all the
970 // other ones are strictly for servergame to clientgame communication
971 #define CS_SERVERINFO 0 // an info string with all the serverinfo cvars
972 #define CS_SYSTEMINFO 1 // an info string for server system to client system configuration (timescale, etc)
973
974 #define RESERVED_CONFIGSTRINGS 2 // game can't modify below this, only the system can
975
976 #define MAX_GAMESTATE_CHARS 16000
977 typedef struct {
978 int stringOffsets[MAX_CONFIGSTRINGS];
979 char stringData[MAX_GAMESTATE_CHARS];
980 int dataCount;
981 } gameState_t;
982
983 //=========================================================
984
985 // bit field limits
986 #define MAX_STATS 16
987 #define MAX_PERSISTANT 16
988 #define MAX_POWERUPS 16
989 #define MAX_WEAPONS 16
990
991 #define MAX_PS_EVENTS 2
992
993 #define PS_PMOVEFRAMECOUNTBITS 6
994
995 // playerState_t is the information needed by both the client and server
996 // to predict player motion and actions
997 // nothing outside of pmove should modify these, or some degree of prediction error
998 // will occur
999
1000 // you can't add anything to this without modifying the code in msg.c
1001
1002 // playerState_t is a full superset of entityState_t as it is used by players,
1003 // so if a playerState_t is transmitted, the entityState_t can be fully derived
1004 // from it.
1005 typedef struct playerState_s {
1006 int commandTime; // cmd->serverTime of last executed command
1007 int pm_type;
1008 int bobCycle; // for view bobbing and footstep generation
1009 int pm_flags; // ducked, jump_held, etc
1010 int pm_time;
1011
1012 vec3_t origin;
1013 vec3_t velocity;
1014 int weaponTime;
1015 int gravity;
1016 int speed;
1017 int delta_angles[3]; // add to command angles to get view direction
1018 // changed by spawns, rotating objects, and teleporters
1019
1020 int groundEntityNum;// ENTITYNUM_NONE = in air
1021
1022 int legsTimer; // don't change low priority animations until this runs out
1023 int legsAnim; // mask off ANIM_TOGGLEBIT
1024
1025 int torsoTimer; // don't change low priority animations until this runs out
1026 int torsoAnim; // mask off ANIM_TOGGLEBIT
1027
1028 int movementDir; // a number 0 to 7 that represents the reletive angle
1029 // of movement to the view angle (axial and diagonals)
1030 // when at rest, the value will remain unchanged
1031 // used to twist the legs during strafing
1032
1033 vec3_t grapplePoint; // location of grapple to pull towards if PMF_GRAPPLE_PULL
1034
1035 int eFlags; // copied to entityState_t->eFlags
1036
1037 int eventSequence; // pmove generated events
1038 int events[MAX_PS_EVENTS];
1039 int eventParms[MAX_PS_EVENTS];
1040
1041 int externalEvent; // events set on player from another source
1042 int externalEventParm;
1043 int externalEventTime;
1044
1045 int clientNum; // ranges from 0 to MAX_CLIENTS-1
1046 int weapon; // copied to entityState_t->weapon
1047 int weaponstate;
1048
1049 vec3_t viewangles; // for fixed views
1050 int viewheight;
1051
1052 // damage feedback
1053 int damageEvent; // when it changes, latch the other parms
1054 int damageYaw;
1055 int damagePitch;
1056 int damageCount;
1057
1058 int stats[MAX_STATS];
1059 int persistant[MAX_PERSISTANT]; // stats that aren't cleared on death
1060 int powerups[MAX_POWERUPS]; // level.time that the powerup runs out
1061 int ammo[MAX_WEAPONS];
1062
1063 int generic1;
1064 int loopSound;
1065 int jumppad_ent; // jumppad entity hit this frame
1066
1067 // not communicated over the net at all
1068 int ping; // server to game info for scoreboard
1069 int pmove_framecount; // FIXME: don't transmit over the network
1070 int jumppad_frame;
1071 int entityEventSequence;
1072 } playerState_t;
1073
1074
1075 //====================================================================
1076
1077
1078 //
1079 // usercmd_t->button bits, many of which are generated by the client system,
1080 // so they aren't game/cgame only definitions
1081 //
1082 #define BUTTON_ATTACK 1
1083 #define BUTTON_TALK 2 // displays talk balloon and disables actions
1084 #define BUTTON_USE_HOLDABLE 4
1085 #define BUTTON_GESTURE 8
1086 #define BUTTON_WALKING 16 // walking can't just be infered from MOVE_RUN
1087 // because a key pressed late in the frame will
1088 // only generate a small move value for that frame
1089 // walking will use different animations and
1090 // won't generate footsteps
1091 #define BUTTON_ATTACK2 32
1092 #define BUTTON_NEGATIVE 64
1093
1094 #define BUTTON_GETFLAG 128
1095 #define BUTTON_GUARDBASE 256
1096 #define BUTTON_PATROL 512
1097 #define BUTTON_FOLLOWME 1024
1098
1099 #define BUTTON_ANY 2048 // any key whatsoever
1100
1101 #define MOVE_RUN 120 // if forwardmove or rightmove are >= MOVE_RUN,
1102 // then BUTTON_WALKING should be set
1103
1104 // usercmd_t is sent to the server each client frame
1105 typedef struct usercmd_s {
1106 int serverTime;
1107 int angles[3];
1108 int buttons;
1109 byte weapon; // weapon
1110 signed char forwardmove, rightmove, upmove;
1111 } usercmd_t;
1112
1113 //===================================================================
1114
1115 // if entityState->solid == SOLID_BMODEL, modelindex is an inline model number
1116 #define SOLID_BMODEL 0xffffff
1117
1118 typedef enum {
1119 TR_STATIONARY,
1120 TR_INTERPOLATE, // non-parametric, but interpolate between snapshots
1121 TR_LINEAR,
1122 TR_LINEAR_STOP,
1123 TR_SINE, // value = base + sin( time / duration ) * delta
1124 TR_GRAVITY,
1125 TR_BUOYANCY //TA: what the hell is this doing in here anyway?
1126 } trType_t;
1127
1128 typedef struct {
1129 trType_t trType;
1130 int trTime;
1131 int trDuration; // if non 0, trTime + trDuration = stop time
1132 vec3_t trBase;
1133 vec3_t trDelta; // velocity, etc
1134 } trajectory_t;
1135
1136 // entityState_t is the information conveyed from the server
1137 // in an update message about entities that the client will
1138 // need to render in some way
1139 // Different eTypes may use the information in different ways
1140 // The messages are delta compressed, so it doesn't really matter if
1141 // the structure size is fairly large
1142
1143 typedef struct entityState_s {
1144 int number; // entity index
1145 int eType; // entityType_t
1146 int eFlags;
1147
1148 trajectory_t pos; // for calculating position
1149 trajectory_t apos; // for calculating angles
1150
1151 int time;
1152 int time2;
1153
1154 vec3_t origin;
1155 vec3_t origin2;
1156
1157 vec3_t angles;
1158 vec3_t angles2;
1159
1160 int otherEntityNum; // shotgun sources, etc
1161 int otherEntityNum2;
1162
1163 int groundEntityNum; // -1 = in air
1164
1165 int constantLight; // r + (g<<8) + (b<<16) + (intensity<<24)
1166 int loopSound; // constantly loop this sound
1167
1168 int modelindex;
1169 int modelindex2;
1170 int clientNum; // 0 to (MAX_CLIENTS - 1), for players and corpses
1171 int frame;
1172
1173 int solid; // for client side prediction, trap_linkentity sets this properly
1174
1175 int event; // impulse events -- muzzle flashes, footsteps, etc
1176 int eventParm;
1177
1178 // for players
1179 int powerups; // bit flags
1180 int weapon; // determines weapon and flash model, etc
1181 int legsAnim; // mask off ANIM_TOGGLEBIT
1182 int torsoAnim; // mask off ANIM_TOGGLEBIT
1183
1184 int generic1;
1185 } entityState_t;
1186
1187 typedef enum {
1188 CA_UNINITIALIZED,
1189 CA_DISCONNECTED, // not talking to a server
1190 CA_AUTHORIZING, // not used any more, was checking cd key
1191 CA_CONNECTING, // sending request packets to the server
1192 CA_CHALLENGING, // sending challenge packets to the server
1193 CA_CONNECTED, // netchan_t established, getting gamestate
1194 CA_LOADING, // only during cgame initialization, never during main loop
1195 CA_PRIMED, // got gamestate, waiting for first frame
1196 CA_ACTIVE, // game views should be displayed
1197 CA_CINEMATIC // playing a cinematic or a static pic, not connected to a server
1198 } connstate_t;
1199
1200 // font support
1201
1202 #define GLYPH_START 0
1203 #define GLYPH_END 255
1204 #define GLYPH_CHARSTART 32
1205 #define GLYPH_CHAREND 127
1206 #define GLYPHS_PER_FONT GLYPH_END - GLYPH_START + 1
1207 typedef struct {
1208 int height; // number of scan lines
1209 int top; // top of glyph in buffer
1210 int bottom; // bottom of glyph in buffer
1211 int pitch; // width for copying
1212 int xSkip; // x adjustment
1213 int imageWidth; // width of actual image
1214 int imageHeight; // height of actual image
1215 float s; // x offset in image where glyph starts
1216 float t; // y offset in image where glyph starts
1217 float s2;
1218 float t2;
1219 qhandle_t glyph; // handle to the shader with the glyph
1220 char shaderName[32];
1221 } glyphInfo_t;
1222
1223 typedef struct {
1224 glyphInfo_t glyphs [GLYPHS_PER_FONT];
1225 float glyphScale;
1226 char name[MAX_QPATH];
1227 } fontInfo_t;
1228
1229 #define Square(x) ((x)*(x))
1230
1231 // real time
1232 //=============================================
1233
1234
1235 typedef struct qtime_s {
1236 int tm_sec; /* seconds after the minute - [0,59] */
1237 int tm_min; /* minutes after the hour - [0,59] */
1238 int tm_hour; /* hours since midnight - [0,23] */
1239 int tm_mday; /* day of the month - [1,31] */
1240 int tm_mon; /* months since January - [0,11] */
1241 int tm_year; /* years since 1900 */
1242 int tm_wday; /* days since Sunday - [0,6] */
1243 int tm_yday; /* days since January 1 - [0,365] */
1244 int tm_isdst; /* daylight savings time flag */
1245 } qtime_t;
1246
1247
1248 // server browser sources
1249 // TTimo: AS_MPLAYER is no longer used
1250 #define AS_LOCAL 0
1251 #define AS_MPLAYER 1
1252 #define AS_GLOBAL 2
1253 #define AS_FAVORITES 3
1254
1255
1256 // cinematic states
1257 typedef enum {
1258 FMV_IDLE,
1259 FMV_PLAY, // play
1260 FMV_EOF, // all other conditions, i.e. stop/EOF/abort
1261 FMV_ID_BLT,
1262 FMV_ID_IDLE,
1263 FMV_LOOPED,
1264 FMV_ID_WAIT
1265 } e_status;
1266
1267 typedef enum _flag_status {
1268 FLAG_ATBASE = 0,
1269 FLAG_TAKEN, // CTF
1270 FLAG_TAKEN_RED, // One Flag CTF
1271 FLAG_TAKEN_BLUE, // One Flag CTF
1272 FLAG_DROPPED
1273 } flagStatus_t;
1274
1275 typedef enum {
1276 DS_NONE,
1277
1278 DS_PLAYBACK,
1279 DS_RECORDING,
1280
1281 DS_NUM_DEMO_STATES
1282 } demoState_t;
1283
1284
1285 #define MAX_GLOBAL_SERVERS 4096
1286 #define MAX_OTHER_SERVERS 128
1287 #define MAX_PINGREQUESTS 32
1288 #define MAX_SERVERSTATUSREQUESTS 16
1289
1290 #define SAY_ALL 0
1291 #define SAY_TEAM 1
1292 #define SAY_TELL 2
1293
1294 #endif // __Q_SHARED_H
1295