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 // common.h -- general definitions
21 
22 #ifndef COMMON_H
23 #define COMMON_H
24 
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <retro_inline.h>
28 
29 #include "qtypes.h"
30 #include "shell.h"
31 
32 #ifdef NQ_HACK
33 #include "quakedef.h"
34 #endif
35 #ifdef QW_HACK
36 #include "bothdefs.h"
37 #include "protocol.h"
38 #endif
39 
40 #define MAX_NUM_ARGVS 50
41 
42 #define stringify__(x) #x
43 #define stringify(x) stringify__(x)
44 
45 #ifdef QW_HACK
46 #define	MAX_INFO_STRING 196
47 #define	MAX_SERVERINFO_STRING 512
48 #define	MAX_LOCALINFO_STRING 32768
49 #endif
50 
51 //============================================================================
52 
53 typedef struct sizebuf_s {
54     qboolean allowoverflow;	// if false, do a Sys_Error
55     qboolean overflowed;	// set to true if the buffer size failed
56     byte *data;
57     int maxsize;
58     int cursize;
59 } sizebuf_t;
60 
61 #ifdef NQ_HACK
62 void SZ_Alloc(sizebuf_t *buf, int startsize);
63 void SZ_Free(sizebuf_t *buf);
64 #endif
65 void SZ_Clear(sizebuf_t *buf);
66 void SZ_Write(sizebuf_t *buf, const void *data, int length);
67 void SZ_Print(sizebuf_t *buf, const char *data); // strcats onto the sizebuf
68 
69 //============================================================================
70 
71 typedef struct link_s {
72     struct link_s *prev, *next;
73 } link_t;
74 
75 void ClearLink(link_t *l);
76 void RemoveLink(link_t *l);
77 void InsertLinkBefore(link_t *l, link_t *before);
78 void InsertLinkAfter(link_t *l, link_t *after);
79 
80 //============================================================================
81 
82 #ifndef NULL
83 #define NULL ((void *)0)
84 #endif
85 
86 #define Q_MAXCHAR ((char)0x7f)
87 #define Q_MAXSHORT ((short)0x7fff)
88 #define Q_MAXINT ((int)0x7fffffff)
89 #define Q_MAXLONG ((int)0x7fffffff)
90 #define Q_MAXFLOAT ((int)0x7fffffff)
91 
92 #define Q_MINCHAR ((char)0x80)
93 #define Q_MINSHORT ((short)0x8000)
94 #define Q_MININT ((int)0x80000000)
95 #define Q_MINLONG ((int)0x80000000)
96 #define Q_MINFLOAT ((int)0x7fffffff)
97 
98 /*
99  * ========================================================================
100  *                          BYTE ORDER FUNCTIONS
101  * ========================================================================
102  */
103 
bswap16(short s)104 static INLINE short bswap16(short s)
105 {
106     return ((s & 255) << 8) | ((s >> 8) & 255);
107 }
bswap32(int l)108 static INLINE int bswap32(int l)
109 {
110     return
111           (((l >>  0) & 255) << 24)
112         | (((l >>  8) & 255) << 16)
113         | (((l >> 16) & 255) <<  8)
114         | (((l >> 24) & 255) <<  0);
115 }
116 
117 #ifdef MSB_FIRST
BigShort(short s)118 static INLINE short BigShort(short s) { return s; }
BigLong(int l)119 static INLINE int BigLong(int l) { return l; }
BigFloat(float f)120 static INLINE float BigFloat(float f) { return f; }
LittleShort(short s)121 static INLINE short LittleShort(short s) { return bswap16(s); }
LittleLong(int l)122 static INLINE int LittleLong(int l) { return bswap32(l); }
LittleFloat(float f)123 static INLINE float LittleFloat(float f)
124 {
125    union {
126       float f;
127       byte b[4];
128    } dat1, dat2;
129 
130    dat1.f = f;
131    dat2.b[0] = dat1.b[3];
132    dat2.b[1] = dat1.b[2];
133    dat2.b[2] = dat1.b[1];
134    dat2.b[3] = dat1.b[0];
135    return dat2.f;
136 }
137 #else
BigShort(short s)138 static INLINE short BigShort(short s) { return bswap16(s); }
BigLong(int l)139 static INLINE int BigLong(int l) { return bswap32(l); }
BigFloat(float f)140 static INLINE float BigFloat(float f)
141 {
142    union {
143       float f;
144       byte b[4];
145    } dat1, dat2;
146 
147    dat1.f = f;
148    dat2.b[0] = dat1.b[3];
149    dat2.b[1] = dat1.b[2];
150    dat2.b[2] = dat1.b[1];
151    dat2.b[3] = dat1.b[0];
152    return dat2.f;
153 }
LittleShort(short s)154 static INLINE short LittleShort(short s) { return s; }
LittleLong(int l)155 static INLINE int LittleLong(int l) { return l; }
LittleFloat(float f)156 static INLINE float LittleFloat(float f) { return f; }
157 #endif
158 
159 //============================================================================
160 
161 #ifdef QW_HACK
162 extern struct usercmd_s nullcmd;
163 #endif
164 
165 void MSG_WriteChar(sizebuf_t *sb, int c);
166 void MSG_WriteByte(sizebuf_t *sb, int c);
167 void MSG_WriteShort(sizebuf_t *sb, int c);
168 void MSG_WriteLong(sizebuf_t *sb, int c);
169 void MSG_WriteFloat(sizebuf_t *sb, float f);
170 void MSG_WriteString(sizebuf_t *sb, const char *s);
171 void MSG_WriteStringf(sizebuf_t *sb, const char *fmt, ...);
172 void MSG_WriteStringvf(sizebuf_t *sb, const char *fmt, va_list ap);
173 void MSG_WriteCoord(sizebuf_t *sb, float f);
174 void MSG_WriteAngle(sizebuf_t *sb, float f);
175 void MSG_WriteAngle16(sizebuf_t *sb, float f);
176 #ifdef QW_HACK
177 void MSG_WriteDeltaUsercmd(sizebuf_t *sb, const struct usercmd_s *from,
178 			   const struct usercmd_s *cmd);
179 #endif
180 #ifdef NQ_HACK
181 void MSG_WriteControlHeader(sizebuf_t *sb);
182 #endif
183 
184 extern int msg_readcount;
185 extern qboolean msg_badread;	// set if a read goes beyond end of message
186 
187 void MSG_BeginReading(void);
188 #ifdef QW_HACK
189 int MSG_GetReadCount(void);
190 #endif
191 int MSG_ReadChar(void);
192 int MSG_ReadByte(void);
193 int MSG_ReadShort(void);
194 int MSG_ReadLong(void);
195 float MSG_ReadFloat(void);
196 char *MSG_ReadString(void);
197 #ifdef QW_HACK
198 char *MSG_ReadStringLine(void);
199 #endif
200 
201 float MSG_ReadCoord(void);
202 float MSG_ReadAngle(void);
203 float MSG_ReadAngle16(void);
204 #ifdef QW_HACK
205 void MSG_ReadDeltaUsercmd(const struct usercmd_s *from, struct usercmd_s *cmd);
206 #endif
207 #ifdef NQ_HACK
208 int MSG_ReadControlHeader(void);
209 #endif
210 
211 //============================================================================
212 
213 int Q_atoi(const char *str);
214 float Q_atof(const char *str);
215 
216 //============================================================================
217 
218 extern char com_token[1024];
219 extern qboolean com_eof;
220 
221 const char *COM_Parse(const char *data);
222 
223 extern unsigned com_argc;
224 extern const char **com_argv;
225 
226 unsigned COM_CheckParm(const char *parm);
227 #ifdef QW_HACK
228 void COM_AddParm(const char *parm);
229 #endif
230 
231 void COM_Init(void);
232 void COM_InitArgv(int argc, const char **argv);
233 
234 const char *COM_SkipPath(const char *pathname);
235 const char *COM_FileExtension(const char *in);
236 qboolean COM_FileExists(const char *filename);
237 void COM_StripExtension(char *filename);
238 void COM_FileBase(const char *in, char *out, size_t buflen);
239 void COM_DefaultExtension(char *path, const char *extension);
240 int COM_CheckExtension(const char *path, const char *extn);
241 
242 char *va(const char *format, ...);
243 
244 // does a varargs printf into a temp buffer
245 
246 //============================================================================
247 
248 extern int com_filesize;
249 struct cache_user_s;
250 
251 extern char com_basedir[MAX_OSPATH];
252 extern char com_gamedir[MAX_OSPATH];
253 extern char com_savedir[MAX_OSPATH];
254 extern int file_from_pak; // global indicating that file came from a pak
255 
256 void COM_WriteFile(const char *filename, const void *data, int len);
257 int COM_FOpenFile(const char *filename, FILE **file);
258 void COM_ScanDir(struct stree_root *root, const char *path,
259 		 const char *pfx, const char *ext, qboolean stripext);
260 
261 void *COM_LoadStackFile(const char *path, void *buffer, int bufsize,
262 			unsigned long *length);
263 void *COM_LoadTempFile(const char *path);
264 void *COM_LoadHunkFile(const char *path);
265 void COM_LoadCacheFile(const char *path, struct cache_user_s *cu);
266 #ifdef QW_HACK
267 void COM_CreatePath(const char *path);
268 void COM_Gamedir(const char *dir);
269 #endif
270 
271 extern struct cvar_s registered;
272 extern qboolean standard_quake, rogue, hipnotic;
273 
274 #ifdef QW_HACK
275 char *Info_ValueForKey(const char *infostring, const char *key);
276 void Info_RemoveKey(char *infostring, const char *key);
277 void Info_RemovePrefixedKeys(char *infostring, char prefix);
278 void Info_SetValueForKey(char *infostring, const char *key, const char *value,
279 			 int maxsize);
280 void Info_SetValueForStarKey(char *infostring, const char *key,
281 			     const char *value, int maxsize);
282 void Info_Print(const char *infostring);
283 
284 unsigned Com_BlockChecksum(const void *buffer, int length);
285 void Com_BlockFullChecksum(const void *buffer, int len,
286 			   unsigned char outbuf[16]);
287 byte COM_BlockSequenceCheckByte(const byte *base, int length, int sequence,
288 				unsigned mapchecksum);
289 byte COM_BlockSequenceCRCByte(const byte *base, int length, int sequence);
290 
291 int build_number(void);
292 
293 extern char gamedirfile[];
294 #endif
295 
296 
297 // Leilei Colored lighting
298 
299 extern byte	palmap2[64][64][64];	// 18-bit lookup table
300 
301 /* The following FS_*() stdio replacements are necessary if one is
302  * to perform non-sequential reads on files reopened on pak files
303  * because we need the bookkeeping about file start/end positions.
304  * Allocating and filling in the fshandle_t structure is the users'
305  * responsibility when the file is initially opened. */
306 
307 typedef struct _fshandle_t
308 {
309 	FILE *file;
310 	qboolean pak;	/* is the file read from a pak */
311 	long start;	/* file or data start position */
312 	long length;	/* file or data size */
313 	long pos;	/* current position relative to start */
314 } fshandle_t;
315 
316 size_t FS_fread(void *ptr, size_t size, size_t nmemb, fshandle_t *fh);
317 int FS_fseek(fshandle_t *fh, long offset, int whence);
318 long FS_ftell(fshandle_t *fh);
319 void FS_rewind(fshandle_t *fh);
320 int FS_feof(fshandle_t *fh);
321 int FS_ferror(fshandle_t *fh);
322 int FS_fclose(fshandle_t *fh);
323 int FS_fgetc(fshandle_t *fh);
324 char *FS_fgets(char *s, int size, fshandle_t *fh);
325 long FS_filelength (fshandle_t *fh);
326 
327 #endif /* COMMON_H */
328