]> git.xonotic.org Git - xonotic/darkplaces.git/blob - common.h
eliminated qbyte type, now uses unsigned char throughout the engine for this purpose
[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
25 // MSVC has a different name for several standard functions
26 #ifdef WIN32
27 # define strcasecmp stricmp
28 # define strncasecmp strnicmp
29 #endif
30
31 // Create our own define for Mac OS X
32 #if defined(__APPLE__) && defined(__MACH__)
33 # define MACOSX
34 #endif
35
36 #ifdef SUNOS
37 #include <sys/file.h>           // Needed for FNDELAY
38 # define model_t dp_model_t // Workaround conflict with /usr/include/sys/model.h
39 #endif
40
41 //============================================================================
42
43 typedef struct sizebuf_s
44 {
45         qboolean        allowoverflow;  // if false, do a Sys_Error
46         qboolean        overflowed;             // set to true if the buffer size failed
47         unsigned char           *data;
48         int                     maxsize;
49         int                     cursize;
50 } sizebuf_t;
51
52 void SZ_Clear (sizebuf_t *buf);
53 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
54 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
55 void SZ_HexDumpToConsole(const sizebuf_t *buf);
56
57 void Com_HexDumpToConsole(const unsigned char *data, int size);
58
59 unsigned short CRC_Block(const unsigned char *data, size_t size);
60
61
62 //============================================================================
63 //                                                      Endianess handling
64 //============================================================================
65
66 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
67
68 // Initializations
69 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
70 # undef BYTE_ORDER
71 # undef LITTLE_ENDIAN
72 # undef BIG_ENDIAN
73 # define LITTLE_ENDIAN 1234
74 # define BIG_ENDIAN 4321
75 #endif
76
77 // If we still don't know the CPU endianess at this point, we try to guess
78 #ifndef BYTE_ORDER
79 # if defined(WIN32)
80 #  define BYTE_ORDER LITTLE_ENDIAN
81 # else
82 #  if defined(SUNOS)
83 #   if defined(__i386) || defined(__amd64)
84 #    define BYTE_ORDER LITTLE_ENDIAN
85 #   else
86 #    define BYTE_ORDER BIG_ENDIAN
87 #   endif
88 #  else
89 #   warning "Unable to determine the CPU endianess. Defaulting to little endian"
90 #   define BYTE_ORDER LITTLE_ENDIAN
91 #  endif
92 # endif
93 #endif
94
95
96 short ShortSwap (short l);
97 int LongSwap (int l);
98 float FloatSwap (float f);
99
100 #if BYTE_ORDER == LITTLE_ENDIAN
101 // little endian
102 #define BigShort(l) ShortSwap(l)
103 #define LittleShort(l) (l)
104 #define BigLong(l) LongSwap(l)
105 #define LittleLong(l) (l)
106 #define BigFloat(l) FloatSwap(l)
107 #define LittleFloat(l) (l)
108 #else
109 // big endian
110 #define BigShort(l) (l)
111 #define LittleShort(l) ShortSwap(l)
112 #define BigLong(l) (l)
113 #define LittleLong(l) LongSwap(l)
114 #define BigFloat(l) (l)
115 #define LittleFloat(l) FloatSwap(l)
116 #endif
117
118 unsigned int BuffBigLong (const unsigned char *buffer);
119 unsigned short BuffBigShort (const unsigned char *buffer);
120 unsigned int BuffLittleLong (const unsigned char *buffer);
121 unsigned short BuffLittleShort (const unsigned char *buffer);
122
123
124 //============================================================================
125
126 // these versions are purely for internal use, never sent in network protocol
127 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
128 typedef enum protocolversion_e
129 {
130         PROTOCOL_UNKNOWN,
131         PROTOCOL_DARKPLACES7, // added QuakeWorld-style movement protocol to allow more consistent prediction
132         PROTOCOL_DARKPLACES6, // various changes
133         PROTOCOL_DARKPLACES5, // uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
134         PROTOCOL_DARKPLACES4, // various changes
135         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
136         PROTOCOL_DARKPLACES2, // various changes
137         PROTOCOL_DARKPLACES1, // uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
138         PROTOCOL_QUAKEDP, // darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
139         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)
140         PROTOCOL_QUAKE, // quake (aka netquake/normalquake/nq) protocol
141 }
142 protocolversion_t;
143
144 void MSG_WriteChar (sizebuf_t *sb, int c);
145 void MSG_WriteByte (sizebuf_t *sb, int c);
146 void MSG_WriteShort (sizebuf_t *sb, int c);
147 void MSG_WriteLong (sizebuf_t *sb, int c);
148 void MSG_WriteFloat (sizebuf_t *sb, float f);
149 void MSG_WriteString (sizebuf_t *sb, const char *s);
150 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
151 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
152 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
153 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
154 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
155 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
156 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
157 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
158 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
159 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
160
161 extern  int                     msg_readcount;
162 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
163
164 void MSG_BeginReading (void);
165 int MSG_ReadLittleShort (void);
166 int MSG_ReadBigShort (void);
167 int MSG_ReadLittleLong (void);
168 int MSG_ReadBigLong (void);
169 float MSG_ReadLittleFloat (void);
170 float MSG_ReadBigFloat (void);
171 char *MSG_ReadString (void);
172 int MSG_ReadBytes (int numbytes, unsigned char *out);
173
174 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
175 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
176 #define MSG_ReadShort MSG_ReadLittleShort
177 #define MSG_ReadLong MSG_ReadLittleLong
178 #define MSG_ReadFloat MSG_ReadLittleFloat
179
180 float MSG_ReadAngle8i (void);
181 float MSG_ReadAngle16i (void);
182 float MSG_ReadAngle32f (void);
183 float MSG_ReadCoord13i (void);
184 float MSG_ReadCoord16i (void);
185 float MSG_ReadCoord32f (void);
186 float MSG_ReadCoord (protocolversion_t protocol);
187 void MSG_ReadVector (float *v, protocolversion_t protocol);
188 float MSG_ReadAngle (protocolversion_t protocol);
189
190 //============================================================================
191
192 extern char com_token[1024];
193
194 int COM_ParseToken(const char **datapointer, int returnnewline);
195 int COM_ParseTokenConsole(const char **datapointer);
196
197 extern int com_argc;
198 extern const char **com_argv;
199
200 int COM_CheckParm (const char *parm);
201 void COM_Init (void);
202 void COM_Shutdown (void);
203 void COM_InitArgv (void);
204 void COM_InitGameType (void);
205
206 char    *va(const char *format, ...);
207 // does a varargs printf into a temp buffer
208
209
210 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
211 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
212 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
213
214 // dpsnprintf and dpvsnprintf
215 // return the number of printed characters, excluding the final '\0'
216 // or return -1 if the buffer isn't big enough to contain the entire string.
217 // buffer is ALWAYS null-terminated
218 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...);
219 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
220
221
222 //============================================================================
223
224 extern  struct cvar_s   registered;
225 extern  struct cvar_s   cmdline;
226
227 typedef enum gamemode_e
228 {
229         GAME_NORMAL,
230         GAME_HIPNOTIC,
231         GAME_ROGUE,
232         GAME_NEHAHRA,
233         GAME_NEXUIZ,
234         GAME_TRANSFUSION,
235         GAME_GOODVSBAD2,
236         GAME_TEU,
237         GAME_BATTLEMECH,
238         GAME_ZYMOTIC,
239         GAME_FNIGGIUM,
240         GAME_SETHERAL,
241         GAME_SOM,
242         GAME_TENEBRAE, // full of evil hackery
243         GAME_NEOTERIC,
244         GAME_OPENQUARTZ, //this game sucks
245         GAME_PRYDON,
246         GAME_NETHERWORLD,
247         GAME_THEHUNTED,
248         GAME_DEFEATINDETAIL2,
249 }
250 gamemode_t;
251
252 extern gamemode_t gamemode;
253 extern const char *gamename;
254 extern const char *gamedirname1;
255 extern const char *gamedirname2;
256 extern const char *gamescreenshotname;
257 extern const char *gameuserdirname;
258 extern char com_modname[MAX_OSPATH];
259
260 void COM_ToLowerString (const char *in, char *out, size_t size_out);
261 void COM_ToUpperString (const char *in, char *out, size_t size_out);
262 int COM_StringBeginsWith(const char *s, const char *match);
263
264 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
265
266 typedef struct stringlist_s
267 {
268         struct stringlist_s *next;
269         char *text;
270 } stringlist_t;
271
272 int matchpattern(char *in, char *pattern, int caseinsensitive);
273 stringlist_t *stringlistappend(stringlist_t *current, char *text);
274 void stringlistfree(stringlist_t *current);
275 stringlist_t *stringlistsort(stringlist_t *start);
276 stringlist_t *listdirectory(const char *path);
277 void freedirectory(stringlist_t *list);
278
279 char *SearchInfostring(const char *infostring, const char *key);
280
281
282 // strlcat and strlcpy, from OpenBSD
283 // Most (all?) BSDs already have them
284 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
285 # define HAVE_STRLCAT 1
286 # define HAVE_STRLCPY 1
287 #endif
288
289 #ifndef HAVE_STRLCAT
290 /*
291  * Appends src to string dst of size siz (unlike strncat, siz is the
292  * full size of dst, not space left).  At most siz-1 characters
293  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
294  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
295  * If retval >= siz, truncation occurred.
296  */
297 size_t strlcat(char *dst, const char *src, size_t siz);
298 #endif  // #ifndef HAVE_STRLCAT
299
300 #ifndef HAVE_STRLCPY
301 /*
302  * Copy src to string dst of size siz.  At most siz-1 characters
303  * will be copied.  Always NUL terminates (unless siz == 0).
304  * Returns strlen(src); if retval >= siz, truncation occurred.
305  */
306 size_t strlcpy(char *dst, const char *src, size_t siz);
307
308 #endif  // #ifndef HAVE_STRLCPY
309
310 #endif
311