]> git.xonotic.org Git - xonotic/darkplaces.git/blob - common.h
CREDITS: Add name
[xonotic/darkplaces.git] / common.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef COMMON_H
22 #define COMMON_H
23
24 #include <stdarg.h>
25 #include "qtypes.h"
26 #include "qdefs.h"
27
28 /// MSVC has a different name for several standard functions
29 #ifdef WIN32
30 # define strcasecmp _stricmp
31 # define strncasecmp _strnicmp
32 #else
33 #include "strings.h"
34 #endif
35
36 // Create our own define for Mac OS X
37 #if defined(__APPLE__) && defined(__MACH__)
38 # define MACOSX
39 #endif
40
41
42 //============================================================================
43
44 #define ContainerOf(ptr, type, member) ((type *)((char *)&(ptr) - offsetof(type, member)))
45
46 typedef struct sizebuf_s
47 {
48         qbool   allowoverflow;  ///< if false, do a Sys_Error
49         qbool   overflowed;             ///< set to true if the buffer size failed
50         unsigned char           *data;
51         int                     maxsize;
52         int                     cursize;
53         int                     readcount;
54         qbool   badread;                // set if a read goes beyond end of message
55 } sizebuf_t;
56
57 void SZ_Clear (sizebuf_t *buf);
58 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
59 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
60 void SZ_HexDumpToConsole(const sizebuf_t *buf);
61
62 void Com_HexDumpToConsole(const unsigned char *data, int size);
63
64 unsigned short CRC_Block(const unsigned char *data, size_t size);
65 unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); // for hash lookup functions that use strcasecmp for comparison
66
67 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
68
69 // these are actually md4sum (mdfour.c)
70 unsigned Com_BlockChecksum (void *buffer, int length);
71 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
72
73 void COM_Init_Commands(void);
74
75
76 //============================================================================
77 //                                                      Endianess handling
78 //============================================================================
79
80 // check mem_bigendian if you need to know the system byte order
81
82 /*! \name Byte order functions.
83  * @{
84  */
85
86 // unaligned memory access crashes on some platform, so always read bytes...
87 #define BigShort(l) BuffBigShort((unsigned char *)&(l))
88 #define LittleShort(l) BuffLittleShort((unsigned char *)&(l))
89 #define BigLong(l) BuffBigLong((unsigned char *)&(l))
90 #define LittleLong(l) BuffLittleLong((unsigned char *)&(l))
91 #define BigFloat(l) BuffBigFloat((unsigned char *)&(l))
92 #define LittleFloat(l) BuffLittleFloat((unsigned char *)&(l))
93
94 /// Extract a big endian 32bit float from the given \p buffer.
95 float BuffBigFloat (const unsigned char *buffer);
96
97 /// Extract a big endian 32bit int from the given \p buffer.
98 int BuffBigLong (const unsigned char *buffer);
99
100 /// Extract a big endian 16bit short from the given \p buffer.
101 short BuffBigShort (const unsigned char *buffer);
102
103 /// Extract a little endian 32bit float from the given \p buffer.
104 float BuffLittleFloat (const unsigned char *buffer);
105
106 /// Extract a little endian 32bit int from the given \p buffer.
107 int BuffLittleLong (const unsigned char *buffer);
108
109 /// Extract a little endian 16bit short from the given \p buffer.
110 short BuffLittleShort (const unsigned char *buffer);
111
112 /// Encode a big endian 32bit int to the given \p buffer
113 void StoreBigLong (unsigned char *buffer, unsigned int i);
114
115 /// Encode a big endian 16bit int to the given \p buffer
116 void StoreBigShort (unsigned char *buffer, unsigned short i);
117
118 /// Encode a little endian 32bit int to the given \p buffer
119 void StoreLittleLong (unsigned char *buffer, unsigned int i);
120
121 /// Encode a little endian 16bit int to the given \p buffer
122 void StoreLittleShort (unsigned char *buffer, unsigned short i);
123 //@}
124
125 //============================================================================
126
127 // these versions are purely for internal use, never sent in network protocol
128 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
129 typedef enum protocolversion_e
130 {
131         PROTOCOL_UNKNOWN,
132         PROTOCOL_DARKPLACES7, ///< added QuakeWorld-style movement protocol to allow more consistent prediction
133         PROTOCOL_DARKPLACES6, ///< various changes
134         PROTOCOL_DARKPLACES5, ///< uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
135         PROTOCOL_DARKPLACES4, ///< various changes
136         PROTOCOL_DARKPLACES3, ///< uses EntityFrame4 entity snapshot encoder/decoder which is broken, this attempted to do partial snapshot updates on a QuakeWorld-like protocol, but it is broken and impossible to fix
137         PROTOCOL_DARKPLACES2, ///< various changes
138         PROTOCOL_DARKPLACES1, ///< uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
139         PROTOCOL_QUAKEDP, ///< darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
140         PROTOCOL_NEHAHRAMOVIE, ///< Nehahra movie protocol, a big nasty hack dating back to early days of the Quake Standards Group (but only ever used by neh_gl.exe), this is potentially backwards compatible with quake protocol as long as no extended features are used (but in actuality the neh_gl.exe which wrote this protocol ALWAYS wrote the extended information)
141         PROTOCOL_QUAKE, ///< quake (aka netquake/normalquake/nq) protocol
142         PROTOCOL_QUAKEWORLD, ///< quakeworld protocol
143         PROTOCOL_NEHAHRABJP, ///< same as QUAKEDP but with 16bit modelindex
144         PROTOCOL_NEHAHRABJP2, ///< same as NEHAHRABJP but with 16bit soundindex
145         PROTOCOL_NEHAHRABJP3 ///< same as NEHAHRABJP2 but with some changes
146 }
147 protocolversion_t;
148
149 /*! \name Message IO functions.
150  * Handles byte ordering and avoids alignment errors
151  * @{
152  */
153
154 void MSG_InitReadBuffer (sizebuf_t *buf, unsigned char *data, int size);
155 void MSG_WriteChar (sizebuf_t *sb, int c);
156 void MSG_WriteByte (sizebuf_t *sb, int c);
157 void MSG_WriteShort (sizebuf_t *sb, int c);
158 void MSG_WriteLong (sizebuf_t *sb, int c);
159 void MSG_WriteFloat (sizebuf_t *sb, vec_t f);
160 void MSG_WriteString (sizebuf_t *sb, const char *s);
161 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
162 void MSG_WriteAngle8i (sizebuf_t *sb, vec_t f);
163 void MSG_WriteAngle16i (sizebuf_t *sb, vec_t f);
164 void MSG_WriteAngle32f (sizebuf_t *sb, vec_t f);
165 void MSG_WriteCoord13i (sizebuf_t *sb, vec_t f);
166 void MSG_WriteCoord16i (sizebuf_t *sb, vec_t f);
167 void MSG_WriteCoord32f (sizebuf_t *sb, vec_t f);
168 void MSG_WriteCoord (sizebuf_t *sb, vec_t f, protocolversion_t protocol);
169 void MSG_WriteVector (sizebuf_t *sb, const vec3_t v, protocolversion_t protocol);
170 void MSG_WriteAngle (sizebuf_t *sb, vec_t f, protocolversion_t protocol);
171
172 void MSG_BeginReading (sizebuf_t *sb);
173 int MSG_ReadLittleShort (sizebuf_t *sb);
174 int MSG_ReadBigShort (sizebuf_t *sb);
175 int MSG_ReadLittleLong (sizebuf_t *sb);
176 int MSG_ReadBigLong (sizebuf_t *sb);
177 float MSG_ReadLittleFloat (sizebuf_t *sb);
178 float MSG_ReadBigFloat (sizebuf_t *sb);
179 char *MSG_ReadString (sizebuf_t *sb, char *string, size_t maxstring);
180 int MSG_ReadBytes (sizebuf_t *sb, int numbytes, unsigned char *out);
181
182 #define MSG_ReadChar(sb) ((sb)->readcount >= (sb)->cursize ? ((sb)->badread = true, -1) : (signed char)(sb)->data[(sb)->readcount++])
183 #define MSG_ReadByte(sb) ((sb)->readcount >= (sb)->cursize ? ((sb)->badread = true, -1) : (unsigned char)(sb)->data[(sb)->readcount++])
184 #define MSG_ReadShort MSG_ReadLittleShort
185 #define MSG_ReadLong MSG_ReadLittleLong
186 #define MSG_ReadFloat MSG_ReadLittleFloat
187
188 float MSG_ReadAngle8i (sizebuf_t *sb);
189 float MSG_ReadAngle16i (sizebuf_t *sb);
190 float MSG_ReadAngle32f (sizebuf_t *sb);
191 float MSG_ReadCoord13i (sizebuf_t *sb);
192 float MSG_ReadCoord16i (sizebuf_t *sb);
193 float MSG_ReadCoord32f (sizebuf_t *sb);
194 float MSG_ReadCoord (sizebuf_t *sb, protocolversion_t protocol);
195 void MSG_ReadVector (sizebuf_t *sb, vec3_t v, protocolversion_t protocol);
196 float MSG_ReadAngle (sizebuf_t *sb, protocolversion_t protocol);
197 //@}
198 //============================================================================
199
200 typedef float (*COM_WordWidthFunc_t) (void *passthrough, const char *w, size_t *length, float maxWidth); // length is updated to the longest fitting string into maxWidth; if maxWidth < 0, all characters are used and length is used as is
201 typedef int (*COM_LineProcessorFunc) (void *passthrough, const char *line, size_t length, float width, qbool isContination);
202 int COM_Wordwrap(const char *string, size_t length, float continuationSize, float maxWidth, COM_WordWidthFunc_t wordWidth, void *passthroughCW, COM_LineProcessorFunc processLine, void *passthroughPL);
203
204 extern char com_token[MAX_INPUTLINE];
205
206 int COM_ParseToken_Simple(const char **datapointer, qbool returnnewline, qbool parsebackslash, qbool parsecomments);
207 int COM_ParseToken_QuakeC(const char **datapointer, qbool returnnewline);
208 int COM_ParseToken_VM_Tokenize(const char **datapointer, qbool returnnewline);
209 int COM_ParseToken_Console(const char **datapointer);
210
211 void COM_Init (void);
212 void COM_Shutdown (void);
213 void COM_InitGameType (void);
214
215 char *va(char *buf, size_t buflen, const char *format, ...) DP_FUNC_PRINTF(3);
216 // does a varargs printf into provided buffer, returns buffer (so it can be called in-line unlike dpsnprintf)
217
218
219 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
220 #ifdef snprintf
221 # undef snprintf
222 #endif
223 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
224 #ifdef vsnprintf
225 # undef vsnprintf
226 #endif
227 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
228
229 // dpsnprintf and dpvsnprintf
230 // return the number of printed characters, excluding the final '\0'
231 // or return -1 if the buffer isn't big enough to contain the entire string.
232 // buffer is ALWAYS null-terminated
233 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...) DP_FUNC_PRINTF(3);
234 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
235
236 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
237 // LadyHavoc: added #undef lines here to avoid warnings in Linux
238 #undef strcat
239 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
240 #undef strncat
241 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
242 #undef strcpy
243 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
244 #undef strncpy
245 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
246 //#undef sprintf
247 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
248
249
250 //============================================================================
251
252 extern  struct cvar_s   registered;
253 extern  struct cvar_s   cmdline;
254
255 typedef enum userdirmode_e
256 {
257         USERDIRMODE_NOHOME, // basedir only
258         USERDIRMODE_HOME, // Windows basedir, general POSIX (~/.)
259         USERDIRMODE_MYGAMES, // pre-Vista (My Documents/My Games/), general POSIX (~/.)
260         USERDIRMODE_SAVEDGAMES, // Vista (%USERPROFILE%/Saved Games/), OSX (~/Library/Application Support/), Linux (~/.config)
261         USERDIRMODE_COUNT
262 }
263 userdirmode_t;
264
265 typedef enum gamemode_e
266 {
267         GAME_NORMAL,
268         GAME_HIPNOTIC,
269         GAME_ROGUE,
270         GAME_QUOTH,
271         GAME_NEHAHRA,
272         GAME_NEXUIZ,
273         GAME_XONOTIC,
274         GAME_TRANSFUSION,
275         GAME_GOODVSBAD2,
276         GAME_TEU,
277         GAME_BATTLEMECH,
278         GAME_ZYMOTIC,
279         GAME_SETHERAL,
280         GAME_TENEBRAE, // full of evil hackery
281         GAME_NEOTERIC,
282         GAME_OPENQUARTZ, //this game sucks
283         GAME_PRYDON,
284         GAME_DELUXEQUAKE,
285         GAME_THEHUNTED,
286         GAME_DEFEATINDETAIL2,
287         GAME_DARSANA,
288         GAME_CONTAGIONTHEORY,
289         GAME_EDU2P,
290         GAME_PROPHECY,
291         GAME_BLOODOMNICIDE,
292         GAME_STEELSTORM, // added by motorsep
293         GAME_STEELSTORM2, // added by motorsep
294         GAME_SSAMMO, // added by motorsep
295         GAME_STEELSTORMREVENANTS, // added by motorsep 07/19/2015
296         GAME_TOMESOFMEPHISTOPHELES, // added by motorsep
297         GAME_STRAPBOMB, // added by motorsep for Urre
298         GAME_MOONHELM,
299         GAME_VORETOURNAMENT,
300         GAME_DOOMBRINGER, // added by Cloudwalk for kristus
301         GAME_BATTLEMETAL, // added by Cloudwalk for Subject9x
302         GAME_COUNT
303 }
304 gamemode_t;
305
306 // Master switch for some hacks/changes that eventually should become cvars.
307 #define IS_NEXUIZ_DERIVED(g) ((g) == GAME_NEXUIZ || (g) == GAME_XONOTIC || (g) == GAME_VORETOURNAMENT)
308 // Pre-csqcmodels era.
309 #define IS_OLDNEXUIZ_DERIVED(g) ((g) == GAME_NEXUIZ || (g) == GAME_VORETOURNAMENT)
310
311 extern gamemode_t gamemode;
312 extern const char *gamename;
313 extern const char *gamenetworkfiltername;
314 extern const char *gamedirname1;
315 extern const char *gamedirname2;
316 extern const char *gamescreenshotname;
317 extern const char *gameuserdirname;
318 extern char com_modname[MAX_OSPATH];
319
320 void COM_ChangeGameTypeForGameDirs(void);
321
322 void COM_ToLowerString (const char *in, char *out, size_t size_out);
323 void COM_ToUpperString (const char *in, char *out, size_t size_out);
324 int COM_StringBeginsWith(const char *s, const char *match);
325
326 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
327
328 size_t COM_StringLengthNoColors(const char *s, size_t size_s, qbool *valid);
329 qbool COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qbool escape_carets);
330 void COM_ToLowerString (const char *in, char *out, size_t size_out);
331 void COM_ToUpperString (const char *in, char *out, size_t size_out);
332
333 typedef struct stringlist_s
334 {
335         /// maxstrings changes as needed, causing reallocation of strings[] array
336         int maxstrings;
337         int numstrings;
338         char **strings;
339 } stringlist_t;
340
341 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
342 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qbool wildcard_least_one);
343 void stringlistinit(stringlist_t *list);
344 void stringlistfreecontents(stringlist_t *list);
345 void stringlistappend(stringlist_t *list, const char *text);
346 void stringlistsort(stringlist_t *list, qbool uniq);
347 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
348
349 char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
350 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
351 void InfoString_Print(char *buffer);
352
353 // strlcat and strlcpy, from OpenBSD
354 // Most (all?) BSDs already have them
355 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
356 # define HAVE_STRLCAT 1
357 # define HAVE_STRLCPY 1
358 #endif
359
360 #ifndef HAVE_STRLCAT
361 /*!
362  * Appends src to string dst of size siz (unlike strncat, siz is the
363  * full size of dst, not space left).  At most siz-1 characters
364  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
365  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
366  * If retval >= siz, truncation occurred.
367  */
368 size_t strlcat(char *dst, const char *src, size_t siz);
369 #endif  // #ifndef HAVE_STRLCAT
370
371 #ifndef HAVE_STRLCPY
372 /*!
373  * Copy src to string dst of size siz.  At most siz-1 characters
374  * will be copied.  Always NUL terminates (unless siz == 0).
375  * Returns strlen(src); if retval >= siz, truncation occurred.
376  */
377 size_t strlcpy(char *dst, const char *src, size_t siz);
378
379 #endif  // #ifndef HAVE_STRLCPY
380
381 void FindFraction(double val, int *num, int *denom, int denomMax);
382
383 // decodes XPM file to XPM array (as if #include'd)
384 char **XPM_DecodeString(const char *in);
385
386 size_t base64_encode(unsigned char *buf, size_t buflen, size_t outbuflen);
387
388 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
389
390 float Com_CalcRoll (const vec3_t angles, const vec3_t velocity, const vec_t angleval, const vec_t velocityval);
391
392 #endif
393