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