UFO: Alien Invasion
common.h
Go to the documentation of this file.
1
6/*
7Copyright (C) 1997-2001 Id Software, Inc.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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==============================================================
128PROTOCOL
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
145
156 svc_oob = 0xff
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
165typedef int32_t svc_ops_t;
166
167/*============================================== */
168
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==============================================================
203MISC
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
236void Com_BeginRedirect(struct net_stream* stream, char* buffer, int buffersize);
237void Com_EndRedirect(void);
238void Com_vPrintf(const char* fmt, va_list);
239
241
245struct comDrop_t {};
247public:
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
253void Com_Drop(void) __attribute__((noreturn));
254void Com_Quit(void);
255void Com_BreakIntoDebugger(void);
256void Com_WriteConfigToFile(const char* filename);
258
259int Com_ServerState(void);
260void Com_SetServerState(int state);
261void Com_SetRandomSeed(unsigned int seed);
262const char* Com_UnsignedIntToBinary(uint32_t x);
263const char* Com_ByteToBinary(byte x);
264
265#include "md4.h"
266const char* Com_MD5File(const char* fn, int length = 0);
267const char* Com_MD5Buffer(const byte* buf, size_t len);
268
269extern cvar_t* http_proxy;
270extern cvar_t* http_timeout;
271extern cvar_t* developer;
272extern cvar_t* sv_dedicated;
273extern cvar_t* sv_maxclients;
274extern cvar_t* sv_gametype;
276extern cvar_t* port;
277
278extern cvar_t* sys_priority;
279extern cvar_t* sys_affinity;
280extern cvar_t* sys_os;
281extern cvar_t* hwclass;
282
283#define MAXCMDLINE 256
284
285typedef void (*vPrintfPtr_t)(const char* fmt, va_list ap);
288
289void Qcommon_Init(int argc, char** argv);
290void Qcommon_Frame(void);
291void Qcommon_Shutdown(void);
292void Com_SetGameType(void);
293void Com_ReadFromPipe(void);
294float Com_GrenadeTarget(const vec3_t from, const vec3_t at, float speed, bool launched, bool rolled, vec3_t v0);
296void Con_Print(const char* txt);
297
298/* Event timing */
299
300typedef void event_func(int now, void* data);
301typedef bool event_check_func(int now, void* data);
302typedef bool event_delay_func(int now, void* data);
303typedef void event_notify_delay_func(int now, void* data, int delay);
307typedef bool event_filter(int when, event_func* func, event_check_func* check, void* data);
308typedef void event_clean_func(void* data);
309
311 int when;
326 void* data;
327};
328
330
333
334/*
335==============================================================
336CLIENT / SERVER SYSTEMS
337==============================================================
338*/
339
340void CL_Init(void);
341void CL_Drop(void);
342void CL_Shutdown(void);
343int CL_Milliseconds(void);
344void CL_Frame(int now, void* data);
345void CL_SlowFrame(int now, void* data);
346bool CL_ParseClientData(const char* type, const char* name, const char** text);
347void SCR_BeginLoadingPlaque(void);
348void SCR_EndLoadingPlaque(void);
349void CL_InitAfter(void);
350
351void SV_Init(void);
352void SV_Clear(void);
353void SV_Shutdown(const char* finalmsg, bool reconnect);
354void SV_ShutdownWhenEmpty(void);
355void SV_Frame(int now, void*);
358
359/*============================================================================ */
360
368
369/*============================================================================ */
370
371int Com_Argc(void);
372const char* Com_Argv(int arg); /* range and null checked */
373void Com_ClearArgv(int arg);
374const char* Com_MacroExpandString(const char* text);
375void Com_UploadCrashDump(const char* crashDumpFile);
376
377bool Com_ConsoleCompleteCommand(const char* s, char* target, size_t bufSize, uint32_t* pos, uint32_t offset);
378
379void Key_Init(void);
380
381
384#define REMOVE_ELEM(array, index, n) \
385do { \
386 size_t idx__ = (index); \
387 size_t n__ = --(n); \
388 assert(idx__ <= n__); \
389 memmove((array) + idx__, (array) + idx__ + 1, (n__ - idx__) * sizeof(*(array))); \
390 OBJZERO((array)[n__]); \
391} while (0)
392
395#define REMOVE_ELEM_ADJUST_IDX(array, index, n) \
396do { \
397 size_t idx__ = (index); \
398 size_t n__; \
399 size_t i__; \
400 REMOVE_ELEM(array, index, n); \
401 n__ = (n); \
402 for (i__ = idx__; i__ < n__; ++i__) \
403 --(array)[i__].idx; \
404} while (0)
405
406#define HASH_Add(hash, elem, index) \
407do { \
408 const int hashIndex = (index); \
409 assert(hashIndex >= 0); \
410 assert(hashIndex < lengthof(hash)); \
411 (elem)->hash_next = (hash)[hashIndex]; \
412 (hash)[hashIndex] = (elem); \
413} while (0)
414
415#define HASH_Delete(anchor) (*(anchor) = (*(anchor))->hash_next)
const char * gamedir
Definition: common.h:248
virtual ~comRestart_t()
Definition: common.h:250
comRestart_t(const char *_gamedir)
Definition: common.h:249
Command text buffering and command execution header.
Common model code header (for bsp and others)
const char * Com_ByteToBinary(byte x)
Definition: common.cpp:1005
cvar_t * sv_maxclients
Definition: g_main.cpp:43
cvar_t * http_proxy
Definition: common.cpp:47
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
memPool_t * com_cmdSysPool
Definition: common.cpp:69
bool event_delay_func(int now, void *data)
Definition: common.h:302
clc_ops_e
client to server
Definition: common.h:172
@ clc_stringcmd
Definition: common.h:181
@ clc_oob
Definition: common.h:182
@ clc_teaminfo
Definition: common.h:177
@ clc_bad
Definition: common.h:173
@ clc_action
Definition: common.h:179
@ clc_userinfo
Definition: common.h:180
@ clc_nop
Definition: common.h:174
@ clc_ack
Definition: common.h:175
@ clc_initactorstates
Definition: common.h:178
@ clc_endround
Definition: common.h:176
int Com_Argc(void)
Returns the script commandline argument count.
Definition: common.cpp:560
void event_clean_func(void *data)
Definition: common.h:308
void(* vPrintfPtr_t)(const char *fmt, va_list ap)
Definition: common.h:285
void Qcommon_Frame(void)
This is the function that is called directly from main()
Definition: common.cpp:1493
bool CL_ParseClientData(const char *type, const char *name, const char **text)
Called at client startup.
Definition: cl_main.cpp:770
cvar_t * sys_os
Definition: common.cpp:61
memPool_t * com_cvarSysPool
Definition: common.cpp:71
void Com_SetServerState(int state)
Definition: common.cpp:547
cvar_t * sys_priority
Definition: common.cpp:59
cvar_t * port
Definition: common.cpp:58
void Key_Init(void)
Definition: cl_keys.cpp:776
void SV_Frame(int now, void *)
Definition: sv_main.cpp:837
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
void SCR_EndLoadingPlaque(void)
Definition: cl_screen.cpp:277
void Com_ReadFromPipe(void)
Read whatever is in com_pipefile, if anything, and execute it.
Definition: common.cpp:1260
cvar_t * sys_affinity
Definition: common.cpp:60
void Com_UploadCrashDump(const char *crashDumpFile)
Definition: common.cpp:683
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
int Com_ServerState(void)
Check whether we are the server or have a singleplayer tactical mission.
Definition: common.cpp:538
cvar_t * sv_dedicated
Definition: common.cpp:51
cvar_t * hwclass
Definition: common.cpp:62
SharedPtr< scheduleEvent_t > ScheduleEventPtr
Definition: common.h:329
void Com_Quit(void)
Definition: common.cpp:511
void Qcommon_Init(int argc, char **argv)
Init function.
Definition: common.cpp:1078
cvar_t * http_timeout
Definition: common.cpp:48
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
void Com_WriteConfigToFile(const char *filename)
Definition: common.cpp:981
void CL_Drop(void)
Ensures the right menu cvars are set after error drop or map change.
Definition: cl_main.cpp:167
vPrintfPtr_t Qcommon_GetPrintFunction(void)
Definition: common.cpp:1066
void Com_Drop(void) __attribute__((noreturn))
Definition: common.cpp:465
void Com_SetGameType(void)
Definition: common.cpp:815
void event_notify_delay_func(int now, void *data, int delay)
Definition: common.h:303
memPool_t * com_aliasSysPool
Definition: common.cpp:68
const char * Com_UnsignedIntToBinary(uint32_t x)
Definition: common.cpp:1021
void event_func(int now, void *data)
Definition: common.h:300
const char * Com_MD5Buffer(const byte *buf, size_t len)
Compute the md5sum of the given buffer.
Definition: md5.cpp:300
cvar_t * developer
Definition: common.cpp:46
void Com_SetRandomSeed(unsigned int seed)
Definition: common.cpp:999
void(* exceptionCallback_t)(void)
Definition: common.h:240
memPool_t * com_networkPool
Definition: common.cpp:74
mapData_t * SV_GetMapData(void)
Definition: sv_main.cpp:947
void SV_ShutdownWhenEmpty(void)
Will eventually shutdown the server once all clients have disconnected.
Definition: sv_main.cpp:1085
mapTiles_t * SV_GetMapTiles(void)
Definition: sv_main.cpp:952
const char * Com_MacroExpandString(const char *text)
Expands strings with cvar values that are dereferenced by a '*cvar'.
Definition: common.cpp:607
cvar_t * masterserver_url
Definition: common.cpp:57
svc_ops_e
server to client the svc_strings[] array in cl_parse.c should mirror this
Definition: common.h:143
@ svc_event
Definition: common.h:155
@ svc_configstring
Definition: common.h:154
@ svc_stufftext
Definition: common.h:152
@ svc_reconnect
Definition: common.h:150
@ svc_ping
Definition: common.h:148
@ svc_bad
Definition: common.h:144
@ svc_serverdata
Definition: common.h:153
@ svc_oob
Definition: common.h:156
@ svc_print
Definition: common.h:151
@ svc_disconnect
Definition: common.h:149
@ svc_nop
Definition: common.h:147
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
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
void SV_Shutdown(const char *finalmsg, bool reconnect)
Called when each game quits, before Sys_Quit or Sys_Error.
Definition: sv_main.cpp:1042
void Com_BeginRedirect(struct net_stream *stream, char *buffer, int buffersize)
Redirect packets/output from server to client.
Definition: common.cpp:308
void Com_ClearArgv(int arg)
Reset com_argv entry to empty string.
Definition: common.cpp:580
bool Com_CheckConfigStringIndex(int index)
Definition: common.cpp:860
bool event_check_func(int now, void *data)
Definition: common.h:301
void Com_EndRedirect(void)
End the redirection of packets/output.
Definition: common.cpp:325
void SCR_BeginLoadingPlaque(void)
Definition: cl_screen.cpp:269
void CL_SlowFrame(int now, void *data)
Definition: cl_main.cpp:1108
const char * Com_MD5File(const char *fn, int length=0)
Compute the md5sum of a given file.
Definition: md5.cpp:257
cvar_t * sv_gametype
Definition: common.cpp:56
void SV_Clear(void)
Cleanup when the whole game process is shutting down.
Definition: sv_main.cpp:1030
int32_t svc_ops_t
Definition: common.h:165
void SV_Init(void)
Only called once at startup, not for each game.
Definition: sv_main.cpp:960
void Com_vPrintf(const char *fmt, va_list)
Definition: common.cpp:338
void CL_Frame(int now, void *data)
Definition: cl_main.cpp:1047
void Com_BreakIntoDebugger(void)
Definition: common.cpp:470
memPool_t * com_genericPool
Definition: common.cpp:73
void Qcommon_SetPrintFunction(vPrintfPtr_t func)
Definition: common.cpp:1061
memPool_t * com_cmodelSysPool
Definition: common.cpp:70
void CL_Shutdown(void)
Saves configuration file and shuts the client systems down.
Definition: cl_main.cpp:1219
memPool_t * com_fileSysPool
Definition: common.cpp:72
bool event_filter(int when, event_func *func, event_check_func *check, void *data)
Definition: common.h:307
const char * Com_Argv(int arg)
Returns an argument of script commandline.
Definition: common.cpp:568
void Qcommon_Shutdown(void)
Definition: common.cpp:1537
void CL_InitAfter(void)
Init function for clients - called after menu was initialized and ufo-scripts were parsed.
Definition: cl_main.cpp:737
void CL_Init(void)
Definition: cl_main.cpp:1141
int CL_Milliseconds(void)
Definition: cl_main.cpp:1207
Cvar (console variable) header file.
#define __attribute__(x)
Definition: cxx.h:37
#define nullptr
Definition: cxx.h:53
Filesystem header file.
voidpf stream
Definition: ioapi.h:42
const char * filename
Definition: ioapi.h:41
voidpf void * buf
Definition: ioapi.h:42
voidpf uLong offset
Definition: ioapi.h:45
Memory handling with sentinel checking and pools with tags for grouped free'ing.
QGL_EXTERN GLuint GLchar GLuint * len
Definition: r_gl.h:99
QGL_EXTERN void(APIENTRY *qglActiveTexture)(GLenum texture)
QGL_EXTERN GLsizei const GLvoid * data
Definition: r_gl.h:89
QGL_EXTERN GLuint GLsizei GLsizei * length
Definition: r_gl.h:110
QGL_EXTERN GLuint index
Definition: r_gl.h:110
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
QGL_EXTERN GLuint GLsizei bufSize
Definition: r_gl.h:110
Header for script parsing functions.
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
Definition: filesys.h:54
event_check_func * check
Definition: common.h:314
event_delay_func * delay
Called when the check failed and we have to delay events in the queue.
Definition: common.h:322
void * data
Definition: common.h:326
event_notify_delay_func * notifyDelay
Definition: common.h:323
int delayFollowing
Definition: common.h:312
event_clean_func * clean
Definition: common.h:325
event_func * func
Definition: common.h:313
void * notifyDelayUserData
Definition: common.h:324
vec_t vec3_t[3]
Definition: ufotypes.h:39