UFO: Alien Invasion
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1
5
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
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_LoadLibrary(const char* path, const char* prefix);
254
255void Com_Drop(void) __attribute__((noreturn));
256void Com_Quit(void);
257void Com_BreakIntoDebugger(void);
258void Com_WriteConfigToFile(const char* filename);
260
261int Com_ServerState(void);
262void Com_SetServerState(int state);
263void Com_SetRandomSeed(unsigned int seed);
264const char* Com_UnsignedIntToBinary(uint32_t x);
265const char* Com_ByteToBinary(byte x);
266
267#include "md4.h"
268const char* Com_MD5File(const char* fn, int length = 0);
269const char* Com_MD5Buffer(const byte* buf, size_t len);
270
271extern cvar_t* http_proxy;
272extern cvar_t* http_timeout;
273extern cvar_t* developer;
274extern cvar_t* sv_dedicated;
275extern cvar_t* sv_maxclients;
276extern cvar_t* sv_gametype;
278extern cvar_t* port;
279
280extern cvar_t* sys_priority;
281extern cvar_t* sys_affinity;
282extern cvar_t* sys_os;
283extern cvar_t* hwclass;
284
285#define MAXCMDLINE 256
286
287typedef void (*vPrintfPtr_t)(const char* fmt, va_list ap);
290
291void Qcommon_Init(int argc, char** argv);
292void Qcommon_Frame(void);
293void Qcommon_Shutdown(void);
294void Com_SetGameType(void);
295void Com_ReadFromPipe(void);
296float Com_GrenadeTarget(const vec3_t from, const vec3_t at, float speed, bool launched, bool rolled, vec3_t v0);
298void Con_Print(const char* txt);
299
300/* Event timing */
301
302typedef void event_func(int now, void* data);
303typedef bool event_check_func(int now, void* data);
304typedef bool event_delay_func(int now, void* data);
305typedef void event_notify_delay_func(int now, void* data, int delay);
309typedef bool event_filter(int when, event_func* func, event_check_func* check, void* data);
310typedef void event_clean_func(void* data);
311
330
332
335
336/*
337==============================================================
338CLIENT / SERVER SYSTEMS
339==============================================================
340*/
341
342void CL_Init(void);
343void CL_Drop(void);
344void CL_Shutdown(void);
345int CL_Milliseconds(void);
346void CL_Frame(int now, void* data);
347void CL_SlowFrame(int now, void* data);
348bool CL_ParseClientData(const char* type, const char* name, const char** text);
349void SCR_BeginLoadingPlaque(void);
350void SCR_EndLoadingPlaque(void);
351void CL_InitAfter(void);
352
353void SV_Init(void);
354void SV_Clear(void);
355void SV_Shutdown(const char* finalmsg, bool reconnect);
356void SV_ShutdownWhenEmpty(void);
357void SV_Frame(int now, void*);
360
361/*============================================================================ */
362
370
371/*============================================================================ */
372
373int Com_Argc(void);
374const char* Com_Argv(int arg); /* range and null checked */
375void Com_ClearArgv(int arg);
376const char* Com_MacroExpandString(const char* text);
377
378bool Com_ConsoleCompleteCommand(const char* s, char* target, size_t bufSize, uint32_t* pos, uint32_t offset);
379
380void Key_Init(void);
381
382
385#define REMOVE_ELEM(array, index, n) \
386do { \
387 size_t idx__ = (index); \
388 size_t n__ = --(n); \
389 assert(idx__ <= n__); \
390 memmove((array) + idx__, (array) + idx__ + 1, (n__ - idx__) * sizeof(*(array))); \
391 OBJZERO((array)[n__]); \
392} while (0)
393
396#define REMOVE_ELEM_ADJUST_IDX(array, index, n) \
397do { \
398 size_t idx__ = (index); \
399 size_t n__; \
400 size_t i__; \
401 REMOVE_ELEM(array, index, n); \
402 n__ = (n); \
403 for (i__ = idx__; i__ < n__; ++i__) \
404 --(array)[i__].idx; \
405} while (0)
406
407#define HASH_Add(hash, elem, index) \
408do { \
409 const int hashIndex = (index); \
410 assert(hashIndex >= 0); \
411 assert(hashIndex < lengthof(hash)); \
412 (elem)->hash_next = (hash)[hashIndex]; \
413 (hash)[hashIndex] = (elem); \
414} while (0)
415
416#define HASH_Delete(anchor) (*(anchor) = (*(anchor))->hash_next)
Byte order functions header.
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).
cvar_t * http_proxy
Definition common.cpp:47
memPool_t * com_cmdSysPool
Definition common.cpp:68
cvar_t * sys_os
Definition common.cpp:61
memPool_t * com_cvarSysPool
Definition common.cpp:70
cvar_t * sys_priority
Definition common.cpp:59
cvar_t * port
Definition common.cpp:58
cvar_t * sys_affinity
Definition common.cpp:60
cvar_t * sv_dedicated
Definition common.cpp:51
cvar_t * hwclass
Definition common.cpp:62
cvar_t * http_timeout
Definition common.cpp:48
memPool_t * com_aliasSysPool
Definition common.cpp:67
cvar_t * developer
Definition common.cpp:46
memPool_t * com_networkPool
Definition common.cpp:73
cvar_t * masterserver_url
Definition common.cpp:57
cvar_t * sv_gametype
Definition common.cpp:56
memPool_t * com_genericPool
Definition common.cpp:72
memPool_t * com_cmodelSysPool
Definition common.cpp:69
memPool_t * com_fileSysPool
Definition common.cpp:71
const char * Com_ByteToBinary(byte x)
Definition common.cpp:1022
cvar_t * sv_maxclients
Definition g_main.cpp:43
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:734
bool event_delay_func(int now, void *data)
Definition common.h:304
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:600
void event_clean_func(void *data)
Definition common.h:310
void(* vPrintfPtr_t)(const char *fmt, va_list ap)
Definition common.h:287
void Qcommon_Frame(void)
This is the function that is called directly from main().
Definition common.cpp:1511
bool CL_ParseClientData(const char *type, const char *name, const char **text)
Called at client startup.
Definition cl_main.cpp:770
void Com_SetServerState(int state)
Definition common.cpp:587
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)
void Com_ReadFromPipe(void)
Read whatever is in com_pipefile, if anything, and execute it.
Definition common.cpp:1278
void Con_Print(const char *txt)
Handles cursor positioning, line wrapping, etc All console printing must go through this in order to ...
int Com_ServerState(void)
Check whether we are the server or have a singleplayer tactical mission.
Definition common.cpp:578
SharedPtr< scheduleEvent_t > ScheduleEventPtr
Definition common.h:331
void Com_Quit(void)
Definition common.cpp:551
void Qcommon_Init(int argc, char **argv)
Init function.
Definition common.cpp:1095
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:273
void Com_WriteConfigToFile(const char *filename)
Definition common.cpp:998
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:1083
void Com_Drop(void) __attribute__((noreturn))
Definition common.cpp:507
void * Com_LoadLibrary(const char *path, const char *prefix)
Attempts to load a library using a filename suffix list.
Definition common.cpp:126
void Com_SetGameType(void)
Definition common.cpp:832
void event_notify_delay_func(int now, void *data, int delay)
Definition common.h:305
const char * Com_UnsignedIntToBinary(uint32_t x)
Definition common.cpp:1038
void event_func(int now, void *data)
Definition common.h:302
const char * Com_MD5Buffer(const byte *buf, size_t len)
Compute the md5sum of the given buffer.
Definition md5.cpp:300
void Com_SetRandomSeed(unsigned int seed)
Definition common.cpp:1016
void(* exceptionCallback_t)(void)
Definition common.h:240
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:647
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:1380
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:1464
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:350
void Com_ClearArgv(int arg)
Reset com_argv entry to empty string.
Definition common.cpp:620
bool Com_CheckConfigStringIndex(int index)
Definition common.cpp:877
bool event_check_func(int now, void *data)
Definition common.h:303
void Com_EndRedirect(void)
End the redirection of packets/output.
Definition common.cpp:367
void SCR_BeginLoadingPlaque(void)
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
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:380
void CL_Frame(int now, void *data)
Definition cl_main.cpp:1047
void Com_BreakIntoDebugger(void)
Definition common.cpp:512
void Qcommon_SetPrintFunction(vPrintfPtr_t func)
Definition common.cpp:1078
void CL_Shutdown(void)
Saves configuration file and shuts the client systems down.
Definition cl_main.cpp:1219
bool event_filter(int when, event_func *func, event_check_func *check, void *data)
Definition common.h:309
const char * Com_Argv(int arg)
Returns an argument of script commandline.
Definition common.cpp:608
void Qcommon_Shutdown(void)
Definition common.cpp:1555
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
Defined CONSTANTS (Macros are elsewhere).
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
event_check_func * check
Definition common.h:316
event_delay_func * delay
Called when the check failed and we have to delay events in the queue.
Definition common.h:324
void * data
Definition common.h:328
event_notify_delay_func * notifyDelay
Definition common.h:325
int delayFollowing
Definition common.h:314
event_clean_func * clean
Definition common.h:327
event_func * func
Definition common.h:315
void * notifyDelayUserData
Definition common.h:326
System specific stuff.
Cross-platform type definitions.
vec_t vec3_t[3]
Definition ufotypes.h:39