UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
common.h
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 1997-2001 Id Software, Inc.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #pragma once
27 
28 #include "../shared/ufotypes.h"
29 #include "../shared/byte.h"
30 #include "../shared/shared.h"
31 #include "../shared/mathlib.h"
32 #include "../shared/defines.h"
33 #include "cvar.h"
34 #include "mem.h"
35 
36 #define UFO_VERSION "2.6-dev"
37 #define GAME_TITLE "UFO:AI"
38 #define GAME_TITLE_LONG "UFO:Alien Invasion"
39 
40 #ifdef _WIN32
41 # define BUILDSTRING_OS "Win32"
42 # ifndef SO_EXT
43 # define SO_EXT "dll"
44 # endif
45 
46 #elif defined __linux__
47 # define BUILDSTRING_OS "Linux"
48 # ifndef SO_EXT
49 # define SO_EXT "so"
50 # endif
51 
52 #elif defined(__FreeBSD__)
53 # define BUILDSTRING_OS "FreeBSD"
54 # ifndef SO_EXT
55 # define SO_EXT "so"
56 # endif
57 
58 #elif defined(__OpenBSD__)
59 # define BUILDSTRING_OS "OpenBSD"
60 # ifndef SO_EXT
61 # define SO_EXT "so"
62 # endif
63 
64 #elif defined(__NetBSD__)
65 # define BUILDSTRING_OS "NetBSD"
66 # ifndef SO_EXT
67 # define SO_EXT "so"
68 # endif
69 
70 #elif defined __sun
71 # define BUILDSTRING_OS "Solaris"
72 # ifndef SO_EXT
73 # define SO_EXT "so"
74 # endif
75 
76 #elif defined (__APPLE__) || defined (MACOSX)
77 # define BUILDSTRING_OS "MacOSX"
78 # ifndef SO_EXT
79 # define SO_EXT "dylib"
80 # endif
81 
82 #elif defined ANDROID
83 # define BUILDSTRING_OS "Android"
84 # ifndef SO_EXT
85 # define SO_EXT "so"
86 # endif
87 
88 #else
89 # define BUILDSTRING_OS "Unknown"
90 #endif
91 
92 #if defined __alpha__ || defined __alpha || defined _M_ALPHA
93 # define CPUSTRING "Alpha"
94 #elif defined __amd64__ || defined __amd64 || defined __x86_64__ || defined __x86_64 || defined _M_X64
95 # define CPUSTRING "AMD64"
96 #elif defined __arm__ || defined __thumb__ || defined _ARM
97 # define CPUSTRING "ARM"
98 #elif defined i386 || defined __i386__ || defined __i386 || defined _M_IX86 || defined __X86__ || defined _X86_ || defined __THW_INTEL__ || defined __I86__ || defined __INTEL__
99 # define CPUSTRING "IA-32"
100 #elif defined __ia64__ || defined _IA64 || defined __IA64__ || defined __ia64 || defined _M_IA64
101 # define CPUSTRING "IA-64"
102 #elif defined __mips__ || defined mips || defined __mips || defined __MIPS__
103 # define CPUSTRING "MIPS"
104 #elif defined __powerpc || defined __powerpc__ || defined __POWERPC__ || defined __ppc__ || defined _M_PPC || defined _ARCH_PPC
105 # define CPUSTRING "PowerPC"
106 #elif defined __sparc__ || defined __sparc
107 # define CPUSTRING "SPARC"
108 #else
109 # define CPUSTRING "Unknown"
110 #endif
111 
112 #ifdef DEBUG
113 # define BUILDSTRING_VARIANT "DEBUG"
114 #else
115 # define BUILDSTRING_VARIANT "RELEASE"
116 #endif
117 
118 #ifdef UFO_REVISION
119 # define BUILDSTRING BUILDSTRING_OS " " BUILDSTRING_VARIANT " build " UFO_REVISION
120 #else
121 # define BUILDSTRING BUILDSTRING_OS " " BUILDSTRING_VARIANT
122 #endif
123 
124 #define MASTER_SERVER "http://ufoai.org/" /* sponsored by NineX */
125 
126 /*
127 ==============================================================
128 PROTOCOL
129 ==============================================================
130 */
131 
132 /* protocol.h -- communications protocols */
133 
134 #define PROTOCOL_VERSION 18
135 
136 #define PORT_CLIENT 27901
137 #define PORT_SERVER 27910
138 
143 enum svc_ops_e {
145 
156  svc_oob = 0xff
157 };
158 
159 #define MAX_SVC_STUFFTEXT 1024
160 #define MAX_SVC_PRINT 1024
161 #define MAX_SVC_RECONNECT 256
162 #define MAX_SVC_DISCONNECT 256
163 #define MAX_CLC_STRINGCMD 512
164 
165 typedef int32_t svc_ops_t;
166 
167 /*============================================== */
168 
172 enum clc_ops_e {
183 };
184 
185 #define SOUND_ATTN_NONE 0.0f
186 #define SOUND_ATTN_NORM 1.0f
187 #define SOUND_ATTN_IDLE 1.2f
188 #define SOUND_ATTN_STATIC 3.0f
189 #define SOUND_ATTN_MAX SOUND_ATTN_STATIC
190 
191 #include "../ports/system.h"
192 #include "cmd.h"
193 #include "cvar.h"
194 #include "cmodel.h"
195 #include "filesys.h"
196 #include "scripts.h"
197 #include "net.h"
198 #include "dbuffer.h"
199 #include "netpack.h"
200 
201 /*
202 ==============================================================
203 MISC
204 ==============================================================
205 */
206 
207 #define ANGLE2SHORT(x) ((int)((x)*65536/360) & 65535)
208 #define SHORT2ANGLE(x) ((x)*(360.0/65536))
209 
210 #define ERR_FATAL 0 /* exit the entire game with a popup window */
211 #define ERR_DROP 1 /* print to console and disconnect from game */
212 #define ERR_QUIT 2 /* not an error, just a normal exit */
213 
214 #define Q_COLOR_ESCAPE '^'
215 #define Q_IsColorString(p) ((p) && *(p) == Q_COLOR_ESCAPE && *((p) + 1) && isalnum(*((p) + 1)))
216 
217 #define S_COLOR_BLACK "^0"
218 #define S_COLOR_RED "^1"
219 #define S_COLOR_GREEN "^2"
220 #define S_COLOR_YELLOW "^3"
221 #define S_COLOR_BLUE "^4"
222 #define S_COLOR_CYAN "^5"
223 #define S_COLOR_MAGENTA "^6"
224 #define S_COLOR_WHITE "^7"
225 
226 #define CON_COLOR_BLACK 0
227 #define CON_COLOR_RED 1
228 #define CON_COLOR_GREEN 2
229 #define CON_COLOR_YELLOW 3
230 #define CON_COLOR_BLUE 4
231 #define CON_COLOR_CYAN 5
232 #define CON_COLOR_MAGENTA 6
233 #define CON_COLOR_WHITE 7
234 #define MAX_COLORS 8
235 
236 void Com_BeginRedirect(struct net_stream* stream, char* buffer, int buffersize);
237 void Com_EndRedirect(void);
238 void Com_vPrintf(const char* fmt, va_list);
239 
241 
245 struct comDrop_t {};
247 public:
248  const char* gamedir;
249  comRestart_t (const char* _gamedir) : gamedir(_gamedir ? strdup(_gamedir) : nullptr) {}
250  virtual ~comRestart_t () { free(static_cast<void*>(const_cast<char*>(gamedir))); }
251 };
252 
253 void Com_Drop(void) __attribute__((noreturn));
254 void Com_Quit(void);
255 void Com_BreakIntoDebugger(void);
256 void Com_WriteConfigToFile(const char* filename);
258 
259 int Com_ServerState(void);
260 void Com_SetServerState(int state);
261 void Com_SetRandomSeed(unsigned int seed);
262 const char* Com_UnsignedIntToBinary(uint32_t x);
263 const char* Com_ByteToBinary(byte x);
264 
265 #include "md4.h"
266 const char* Com_MD5File(const char* fn, int length = 0);
267 const char* Com_MD5Buffer(const byte* buf, size_t len);
268 
269 extern cvar_t* http_proxy;
270 extern cvar_t* http_timeout;
271 extern cvar_t* developer;
272 extern cvar_t* sv_dedicated;
273 extern cvar_t* sv_maxclients;
274 extern cvar_t* sv_gametype;
275 extern cvar_t* masterserver_url;
276 extern cvar_t* port;
277 
278 extern cvar_t* sys_priority;
279 extern cvar_t* sys_affinity;
280 extern cvar_t* sys_os;
281 extern cvar_t* hwclass;
282 
283 /* Time information. */
290 typedef struct date_s {
291  int day;
292  int sec;
293 } date_t;
294 
295 /* Time Constants */
296 #define DAYS_PER_YEAR 365
297 #define DAYS_PER_YEAR_AVG 365.25
298 
299 #define MONTHS_PER_YEAR 12
300 #define SEASONS_PER_YEAR 4
301 #define SECONDS_PER_DAY 86400
302 #define SECONDS_PER_HOUR 3600
303 #define SECONDS_PER_MINUTE 60
304 #define MINUTES_PER_HOUR 60
305 #define HOURS_PER_DAY 24
306 
307 #define MAXCMDLINE 256
308 
309 typedef void (*vPrintfPtr_t)(const char* fmt, va_list ap);
312 
313 void Qcommon_Init(int argc, char** argv);
314 void Qcommon_Frame(void);
315 void Qcommon_Shutdown(void);
316 void Com_SetGameType(void);
317 void Com_ReadFromPipe(void);
318 float Com_GrenadeTarget(const vec3_t from, const vec3_t at, float speed, bool launched, bool rolled, vec3_t v0);
320 void Con_Print(const char* txt);
321 
322 /* Event timing */
323 
324 typedef void event_func(int now, void* data);
325 typedef bool event_check_func(int now, void* data);
326 typedef bool event_delay_func(int now, void* data);
327 typedef void event_notify_delay_func(int now, void* data, int delay);
331 typedef bool event_filter(int when, event_func* func, event_check_func* check, void* data);
332 typedef void event_clean_func(void* data);
333 
335  int when;
350  void* data;
351 };
352 
354 
355 ScheduleEventPtr Schedule_Event(int when, event_func* func, event_check_func* check, event_clean_func* clean, void* data);
356 int CL_FilterEventQueue(event_filter* filter);
357 
358 /*
359 ==============================================================
360 CLIENT / SERVER SYSTEMS
361 ==============================================================
362 */
363 
364 void CL_Init(void);
365 void CL_Drop(void);
366 void CL_Shutdown(void);
367 int CL_Milliseconds(void);
368 void CL_Frame(int now, void* data);
369 void CL_SlowFrame(int now, void* data);
370 bool CL_ParseClientData(const char* type, const char* name, const char** text);
371 void SCR_BeginLoadingPlaque(void);
372 void SCR_EndLoadingPlaque(void);
373 void CL_InitAfter(void);
374 
375 void SV_Init(void);
376 void SV_Clear(void);
377 void SV_Shutdown(const char* finalmsg, bool reconnect);
378 void SV_ShutdownWhenEmpty(void);
379 void SV_Frame(int now, void*);
380 mapData_t* SV_GetMapData(void);
382 
383 /*============================================================================ */
384 
386 extern memPool_t* com_cmdSysPool;
388 extern memPool_t* com_cvarSysPool;
389 extern memPool_t* com_fileSysPool;
390 extern memPool_t* com_genericPool;
391 extern memPool_t* com_networkPool;
392 
393 /*============================================================================ */
394 
395 int Com_Argc(void);
396 const char* Com_Argv(int arg); /* range and null checked */
397 void Com_ClearArgv(int arg);
398 const char* Com_MacroExpandString(const char* text);
399 void Com_UploadCrashDump(const char* crashDumpFile);
400 
401 bool Com_ConsoleCompleteCommand(const char* s, char* target, size_t bufSize, uint32_t* pos, uint32_t offset);
402 
403 void Key_Init(void);
404 
405 
408 #define REMOVE_ELEM(array, index, n) \
409 do { \
410  size_t idx__ = (index); \
411  size_t n__ = --(n); \
412  assert(idx__ <= n__); \
413  memmove((array) + idx__, (array) + idx__ + 1, (n__ - idx__) * sizeof(*(array))); \
414  OBJZERO((array)[n__]); \
415 } while (0)
416 
419 #define REMOVE_ELEM_ADJUST_IDX(array, index, n) \
420 do { \
421  size_t idx__ = (index); \
422  size_t n__; \
423  size_t i__; \
424  REMOVE_ELEM(array, index, n); \
425  n__ = (n); \
426  for (i__ = idx__; i__ < n__; ++i__) \
427  --(array)[i__].idx; \
428 } while (0)
429 
430 #define HASH_Add(hash, elem, index) \
431 do { \
432  const int hashIndex = (index); \
433  assert(hashIndex >= 0); \
434  assert(hashIndex < lengthof(hash)); \
435  (elem)->hash_next = (hash)[hashIndex]; \
436  (hash)[hashIndex] = (elem); \
437 } while (0)
438 
439 #define HASH_Delete(anchor) (*(anchor) = (*(anchor))->hash_next)
int CL_Milliseconds(void)
Definition: cl_main.cpp:1208
memPool_t * com_fileSysPool
Definition: common.cpp:72
void event_clean_func(void *data)
Definition: common.h:332
mapTiles_t * SV_GetMapTiles(void)
Definition: sv_main.cpp:952
cvar_t * sys_os
Definition: common.cpp:61
cvar_t * developer
Definition: common.cpp:46
struct date_s date_t
Engine-side time information in the game.
void(* exceptionCallback_t)(void)
Definition: common.h:240
Memory handling with sentinel checking and pools with tags for grouped free'ing.
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
Common model code header (for bsp and others)
void Con_Print(const char *txt)
Handles cursor positioning, line wrapping, etc All console printing must go through this in order to ...
Definition: cl_console.cpp:318
ScheduleEventPtr Schedule_Event(int when, event_func *func, event_check_func *check, event_clean_func *clean, void *data)
Schedules an event to run on or after the given time, and when its check function returns true...
Definition: common.cpp:1362
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
cvar_t * http_proxy
Definition: common.cpp:47
cvar_t * sv_maxclients
Definition: g_main.cpp:43
cvar_t * sv_dedicated
Definition: common.cpp:51
memPool_t * com_cmodelSysPool
Definition: common.cpp:70
void SV_ShutdownWhenEmpty(void)
Will eventually shutdown the server once all clients have disconnected.
Definition: sv_main.cpp:1085
bool CL_ParseClientData(const char *type, const char *name, const char **text)
Called at client startup.
Definition: cl_main.cpp:771
void event_notify_delay_func(int now, void *data, int delay)
Definition: common.h:327
void event_func(int now, void *data)
Definition: common.h:324
const char * filename
Definition: ioapi.h:41
const char * gamedir
Definition: common.h:248
int delayFollowing
Definition: common.h:336
int day
Definition: common.h:291
int Com_ServerState(void)
Check whether we are the server or have a singleplayer tactical mission.
Definition: common.cpp:538
Command text buffering and command execution header.
#define nullptr
Definition: cxx.h:53
void Cvar_WriteVariables(qFILE *f)
appends lines containing "set variable value" for all variables with the archive flag set to true...
Definition: cvar.cpp:868
const char * Com_ByteToBinary(byte x)
Definition: common.cpp:1005
memPool_t * com_cvarSysPool
Definition: common.cpp:71
event_func * func
Definition: common.h:337
#define __attribute__(x)
Definition: cxx.h:37
void CL_Drop(void)
Ensures the right menu cvars are set after error drop or map change.
Definition: cl_main.cpp:167
voidpf void * buf
Definition: ioapi.h:42
cvar_t * port
Definition: common.cpp:58
vPrintfPtr_t Qcommon_GetPrintFunction(void)
Definition: common.cpp:1066
virtual ~comRestart_t()
Definition: common.h:250
svc_ops_e
server to client the svc_strings[] array in cl_parse.c should mirror this
Definition: common.h:143
memPool_t * com_networkPool
Definition: common.cpp:74
void Qcommon_Frame(void)
This is the function that is called directly from main()
Definition: common.cpp:1493
void Key_Init(void)
Definition: cl_keys.cpp:776
bool Com_ConsoleCompleteCommand(const char *s, char *target, size_t bufSize, uint32_t *pos, uint32_t offset)
Console completion for command and variables.
Definition: common.cpp:717
const char * Com_MD5File(const char *fn, int length=0)
Compute the md5sum of a given file.
Definition: md5.cpp:257
Header for script parsing functions.
mapData_t * SV_GetMapData(void)
Definition: sv_main.cpp:947
int CL_FilterEventQueue(event_filter *filter)
Filters every event in the queue using the given function. Keeps all events for which the function re...
Definition: common.cpp:1446
const char * Com_MD5Buffer(const byte *buf, size_t len)
Compute the md5sum of the given buffer.
Definition: md5.cpp:300
void Com_ClearArgv(int arg)
Reset com_argv entry to empty string.
Definition: common.cpp:580
void Com_UploadCrashDump(const char *crashDumpFile)
Definition: common.cpp:683
const char * Com_UnsignedIntToBinary(uint32_t x)
Definition: common.cpp:1021
void Qcommon_Init(int argc, char **argv)
Init function.
Definition: common.cpp:1078
const char * Com_Argv(int arg)
Returns an argument of script commandline.
Definition: common.cpp:568
memPool_t * com_cmdSysPool
Definition: common.cpp:69
memPool_t * com_genericPool
Definition: common.cpp:73
QGL_EXTERN GLuint GLsizei GLsizei * length
Definition: r_gl.h:110
void Com_EndRedirect(void)
End the redirection of packets/output.
Definition: common.cpp:325
void Com_Quit(void)
Definition: common.cpp:511
void CL_Init(void)
Definition: cl_main.cpp:1142
void SV_Clear(void)
Cleanup when the whole game process is shutting down.
Definition: sv_main.cpp:1030
void SCR_BeginLoadingPlaque(void)
Definition: cl_screen.cpp:269
void Com_SetRandomSeed(unsigned int seed)
Definition: common.cpp:999
bool Com_CheckConfigStringIndex(int index)
Definition: common.cpp:860
void CL_Shutdown(void)
Saves configuration file and shuts the client systems down.
Definition: cl_main.cpp:1220
Engine-side time information in the game.
Definition: common.h:290
Cvar (console variable) header file.
int Com_Argc(void)
Returns the script commandline argument count.
Definition: common.cpp:560
comRestart_t(const char *_gamedir)
Definition: common.h:249
void SV_Init(void)
Only called once at startup, not for each game.
Definition: sv_main.cpp:960
void Qcommon_Shutdown(void)
Definition: common.cpp:1537
event_notify_delay_func * notifyDelay
Definition: common.h:347
int32_t svc_ops_t
Definition: common.h:165
memPool_t * com_aliasSysPool
Definition: common.cpp:68
void Com_SetGameType(void)
Definition: common.cpp:815
const char * Com_MacroExpandString(const char *text)
Expands strings with cvar values that are dereferenced by a '*cvar'.
Definition: common.cpp:607
cvar_t * sv_gametype
Definition: common.cpp:56
int sec
Definition: common.h:292
const GLuint *typedef void(APIENTRY *GenRenderbuffersEXT_t)(GLsizei
Definition: r_gl.h:189
QGL_EXTERN GLuint index
Definition: r_gl.h:110
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
void Com_BreakIntoDebugger(void)
Definition: common.cpp:470
void * data
Definition: common.h:350
cvar_t * hwclass
Definition: common.cpp:62
void Com_vPrintf(const char *fmt, va_list)
Definition: common.cpp:338
float Com_GrenadeTarget(const vec3_t from, const vec3_t at, float speed, bool launched, bool rolled, vec3_t v0)
Calculates parabola-type shot.
Definition: common.cpp:231
cvar_t * sys_affinity
Definition: common.cpp:60
void CL_Frame(int now, void *data)
Definition: cl_main.cpp:1048
voidpf stream
Definition: ioapi.h:42
void Com_WriteConfigToFile(const char *filename)
Definition: common.cpp:981
void SCR_EndLoadingPlaque(void)
Definition: cl_screen.cpp:277
QGL_EXTERN GLuint GLchar GLuint * len
Definition: r_gl.h:99
cvar_t * sys_priority
Definition: common.cpp:59
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
void Com_Drop(void) __attribute__((noreturn))
Definition: common.cpp:465
void Qcommon_SetPrintFunction(vPrintfPtr_t func)
Definition: common.cpp:1061
event_check_func * check
Definition: common.h:338
clc_ops_e
client to server
Definition: common.h:172
vec_t vec3_t[3]
Definition: ufotypes.h:39
void * notifyDelayUserData
Definition: common.h:348
bool event_filter(int when, event_func *func, event_check_func *check, void *data)
Definition: common.h:331
void CL_InitAfter(void)
Init function for clients - called after menu was initialized and ufo-scripts were parsed...
Definition: cl_main.cpp:738
cvar_t * http_timeout
Definition: common.cpp:48
cvar_t * masterserver_url
Definition: common.cpp:57
event_clean_func * clean
Definition: common.h:349
void CL_SlowFrame(int now, void *data)
Definition: cl_main.cpp:1109
GLsizei const GLvoid * data
Definition: r_gl.h:152
Filesystem header file.
void Com_BeginRedirect(struct net_stream *stream, char *buffer, int buffersize)
Redirect packets/output from server to client.
Definition: common.cpp:308
SharedPtr< scheduleEvent_t > ScheduleEventPtr
Definition: common.h:353
voidpf uLong offset
Definition: ioapi.h:45
event_delay_func * delay
Called when the check failed and we have to delay events in the queue.
Definition: common.h:346
void(* vPrintfPtr_t)(const char *fmt, va_list ap)
Definition: common.h:309
uint8_t byte
Definition: ufotypes.h:34
QGL_EXTERN GLuint GLsizei bufSize
Definition: r_gl.h:110
bool event_check_func(int now, void *data)
Definition: common.h:325
void Com_ReadFromPipe(void)
Read whatever is in com_pipefile, if anything, and execute it.
Definition: common.cpp:1260
void Com_SetServerState(int state)
Definition: common.cpp:547
void SV_Frame(int now, void *)
Definition: sv_main.cpp:837
void SV_Shutdown(const char *finalmsg, bool reconnect)
Called when each game quits, before Sys_Quit or Sys_Error.
Definition: sv_main.cpp:1042
bool event_delay_func(int now, void *data)
Definition: common.h:326