1 /**
2  * @file
3  * @brief Common header file.
4  *
5  * @todo Apparently included by every file - unnecessary?
6  */
7 
8 /*
9 All original material Copyright (C) 2002-2013 UFO: Alien Invasion.
10 
11 Original file from Quake 2 v3.21: quake2-2.31/game/q_shared.h
12 Copyright (C) 1997-2001 Id Software, Inc.
13 
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 
23 See the GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 
29 */
30 
31 #pragma once
32 
33 #include "../shared/ufotypes.h"
34 #include "../shared/shared.h"
35 #include "../shared/mathlib.h"
36 #include "../shared/defines.h"
37 #include "../common/list.h"
38 
39 #include "inv_shared.h"
40 #include "chr_shared.h"
41 #include "q_sizes.h"
42 
43 #ifdef DEDICATED_ONLY
44 /* no gettext support for dedicated servers */
45 # define _(String) String
46 # define ngettext(x, y, cnt) x
47 #endif
48 
49 /*
50 ==============================================================
51 SYSTEM SPECIFIC
52 ==============================================================
53 */
54 
55 /* this is only here so the functions in q_shared.c can link */
56 void Sys_Error(const char* error, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
57 void Com_Printf(const char* msg, ...) __attribute__((format(__printf__, 1, 2)));
58 void Com_DPrintf(int level, const char* msg, ...) __attribute__((format(__printf__, 2, 3)));
59 
60 #define TEAM_NO_ACTIVE -1
61 #define TEAM_CIVILIAN   0
62 #define TEAM_PHALANX    1
63 #define TEAM_ALIEN      7
64 #define TEAM_MAX_HUMAN	TEAM_ALIEN - 1
65 
66 /*
67 ==========================================================
68 ELEMENTS COMMUNICATED ACROSS THE NET
69 ==========================================================
70 */
71 
72 /** add this flag for instant event execution */
73 #define EVENT_INSTANTLY		0x80
74 
75 /**
76  * @brief Possible event values
77  * @sa cl_parse.c for event bindings
78  */
79 typedef enum {
80 	EV_NULL,
81 	EV_RESET,
82 	EV_START,
83 	EV_ENDROUND,	/**< ends the current team's turn CL_DoEndRound */
84 	EV_ENDROUNDANNOUNCE,
85 
86 	EV_RESULTS,
87 	EV_CENTERVIEW,
88 	EV_MOVECAMERA,
89 
90 	EV_ENT_APPEAR,
91 	EV_ENT_PERISH,	/**< empty container or destroy inventory - set le invis to true
92 		* see CL_EntPerish */
93 	EV_ENT_DESTROY,
94 	EV_ADD_BRUSH_MODEL,
95 	EV_ADD_EDICT,
96 
97 	EV_ACTOR_APPEAR,
98 	EV_ACTOR_ADD,
99 	EV_ACTOR_TURN,			/**< turn an actor around */
100 	EV_ACTOR_MOVE,
101 	EV_ACTOR_REACTIONFIRECHANGE,
102 	EV_ACTOR_REACTIONFIREADDTARGET,
103 	EV_ACTOR_REACTIONFIREREMOVETARGET,
104 	EV_ACTOR_REACTIONFIRETARGETUPDATE,
105 
106 	EV_ACTOR_START_SHOOT,
107 	EV_ACTOR_SHOOT,
108 	EV_ACTOR_SHOOT_HIDDEN,
109 	EV_ACTOR_THROW,
110 	EV_ACTOR_END_SHOOT,
111 
112 	EV_ACTOR_DIE,
113 	EV_ACTOR_REVITALISED,
114 	EV_ACTOR_STATS,
115 	EV_ACTOR_STATECHANGE,	/**< set an actor to crouched or reaction fire */
116 	EV_ACTOR_RESERVATIONCHANGE,
117 	EV_ACTOR_WOUND,
118 
119 	EV_INV_ADD,
120 	EV_INV_DEL,
121 	EV_INV_AMMO,
122 	EV_INV_RELOAD,
123 	EV_INV_TRANSFER,
124 
125 	/* func_breakables */
126 	EV_MODEL_EXPLODE,		/**< delay the event execution by the impact time */
127 	EV_MODEL_EXPLODE_TRIGGERED,	/**< don't delay the model explode event */
128 
129 	EV_PARTICLE_APPEAR,
130 	EV_PARTICLE_SPAWN,
131 
132 	EV_SOUND,
133 
134 	EV_DOOR_OPEN,
135 	EV_DOOR_CLOSE,
136 	EV_CLIENT_ACTION,
137 	EV_RESET_CLIENT_ACTION,
138 
139 	EV_CAMERA_APPEAR,
140 
141 	EV_NUM_EVENTS
142 } event_t;
143 
144 typedef enum {
145 	ET_NULL,
146 	ET_ACTORSPAWN,
147 	ET_ACTOR,
148 	ET_ITEM,
149 	ET_BREAKABLE,
150 	ET_TRIGGER,
151 	ET_TRIGGER_HURT,
152 	ET_TRIGGER_TOUCH,
153 	ET_TRIGGER_RESCUE,
154 	ET_TRIGGER_NEXTMAP,
155 	ET_DOOR,
156 	ET_DOOR_SLIDING,
157 	ET_ROTATING,
158 	ET_ACTOR2x2SPAWN,
159 	ET_ACTOR2x2,
160 	ET_CIVILIANTARGET,
161 	ET_MISSION,
162 	ET_ACTORHIDDEN,
163 	ET_PARTICLE,
164 	ET_SOUND,
165 	ET_SOLID,
166 	ET_MESSAGE,
167 	ET_SMOKE,
168 	ET_FIRE,
169 	ET_SMOKESTUN,
170 	ET_CAMERA,
171 
172 	ET_MAX,
173 
174 	ENTITY_TYPE_ENSURE_32BIT = 0x7FFFFFFF
175 } entity_type_t;
176 
177 typedef enum {
178 	CAMERA_MOBILE,
179 	CAMERA_STATIONARY,
180 
181 	CAMERA_MAX,
182 
183 	CAMERA_ENSURE_32BIT = 0x7FFFFFFF
184 } camera_type_t;
185 
186 #define DOOR_ROTATION_ANGLE 90
187 
188 typedef enum {
189 	PA_NULL,
190 	PA_TURN,
191 	PA_MOVE,
192 	PA_STATE,
193 	PA_SHOOT,
194 	PA_USE,
195 	PA_INVMOVE,
196 	PA_REACT_SELECT,
197 	PA_RESERVE_STATE,
198 
199 	PA_NUM_EVENTS
200 } player_action_t;
201 
202 extern const char* pa_format[PA_NUM_EVENTS];
203 
204 /** @brief Available shoot types - also see the @c ST_ constants */
205 typedef int32_t shoot_types_t;
206 /**
207  * @brief The right hand should be used for shooting
208  * @note shoot_types_t value
209  */
210 #define ST_RIGHT 0
211 /**
212  * @brief The right hand reaction fire should be used for shooting
213  * @note shoot_types_t value
214  */
215 #define ST_RIGHT_REACTION 1
216 /**
217  * @brief The left hand should be used for shooting
218  * @note shoot_types_t value
219  */
220 #define ST_LEFT 2
221 /**
222  * @brief The left hand reaction fire should be used for shooting
223  * @note shoot_types_t value
224  */
225 #define ST_LEFT_REACTION 3
226 /**
227  * @brief The headgear slot item should be used when shooting/using the item in the slot
228  * @note shoot_types_t value
229  */
230 #define ST_HEADGEAR 4
231 /**
232  * @brief Amount of shoottypes available
233  * @note shoot_types_t value
234  */
235 #define ST_NUM_SHOOT_TYPES 5
236 
237 /** @brief Determine whether the selected shoot type is for reaction fire */
238 #define IS_SHOT_REACTION(x) ((x) == ST_RIGHT_REACTION || (x) == ST_LEFT_REACTION)
239 /** @brief Determine whether the selected shoot type is for the item in the left hand, either shooting or reaction fire */
240 #define IS_SHOT_LEFT(x)     ((x) == ST_LEFT || (x) == ST_LEFT_REACTION)
241 /** @brief Determine whether the selected shoot type is for the item in the right hand, either shooting or reaction fire */
242 #define IS_SHOT_RIGHT(x)    ((x) == ST_RIGHT || (x) == ST_RIGHT_REACTION)
243 /** @brief Determine whether the selected shoot type is for the item in the headgear slot */
244 #define IS_SHOT_HEADGEAR(x)    ((x) == ST_HEADGEAR)
245 
246 /* shoot flags */
247 #define SF_IMPACT	1	/**< impact on none actor objects */
248 #define SF_BODY		2	/**< impact on actors */
249 #define SF_BOUNCING	4	/**< item should bounce around (like grenades) */
250 #define SF_BOUNCED	8	/**< at least hit the ground once and can now start to bounce */
251 
252 /* is used in events where two edicts can be send, one actor and one receiver - but one of them can
253  * be nullptr, e.g. in a case where the actor is a trigger */
254 #define SKIP_LOCAL_ENTITY (-1)
255 
256 #define MAX_DEATH	3	/**< @sa STATE_DEAD */
257 
258 /* State flags - transfered as short (so max 16 bits please) */
259 /* public states */
260 #define STATE_PUBLIC		0x00FF	/**< mask to separate private flags from events */
261 #define STATE_DEAD			0x0003	/**< 0 alive, 1-3 different deaths @sa MAX_DEATH*/
262 #define STATE_CROUCHED		0x0004
263 #define STATE_PANIC			0x0008
264 
265 #define STATE_RAGE			0x0010	/**< pretty self-explaining */
266 #define STATE_INSANE		0x0030
267 #define STATE_STUN			0x0043	/**< stunned - includes death */
268 #define STATE_DAZED			0x0080	/**< dazed and unable to move */
269 
270 /* private states */
271 #define STATE_REACTION		0x0300	/**< reaction fire */
272 #define STATE_SHAKEN		0x0400	/**< forced reaction fire */
273 #define STATE_XVI			0x0800	/**< controlled by the other team */
274 
275 #define GRAVITY				500.0
276 
277 #define MAX_SKILL	100
278 #define MAX_MAXHP		255
279 #define MIN_TU				39
280 #define WEIGHT_LIGHT	0.2f
281 #define WEIGHT_HEAVY	0.5f
282 #define WEIGHT_NORMAL_PENALTY	0.3f
283 #define WEIGHT_HEAVY_PENALTY	0.6f
284 
285 #define GET_ENCUMBRANCE_PENALTY(weight, max)	(1.0f - ((weight) > (max) * WEIGHT_HEAVY ? WEIGHT_HEAVY_PENALTY : (weight) > (max) * WEIGHT_LIGHT ? WEIGHT_NORMAL_PENALTY : 0.0f))
286 /** @todo Skill-influence needs some balancing. */
287 #define GET_ACC( ab, sk )   ((1 - (((float)(ab) - 10) / (MAX_SKILL / 2) + ((float)(sk) - 10) / (MAX_SKILL / 2)) / 2))
288 #define GET_MORALE( ab )        (std::min((100 + (ab) * 150/MAX_SKILL), 255))
289 #define GET_TU( ab, md )		(MIN_TU * (md) + (ab) * 20 / MAX_SKILL)
290 
291 #define DOOR_OPEN_REVERSE 4
292 #define GET_SLIDING_DOOR_SHIFT_VECTOR(dir, speed, vecout) \
293 	do { const bool reverse = (dir) & DOOR_OPEN_REVERSE; VectorClear(vecout); vecout[dir & 3] = reverse ? -speed : speed; } while (0);
294 
295 /**
296  * config strings are a general means of communication from
297  * the server to all connected clients.
298  * Each config string can be at most MAX_QPATH characters. */
299 #define CS_NAME				0
300 #define CS_MAPTITLE			1		/**< display map title string - translated client side */
301 #define CS_MAXCLIENTS		2
302 #define CS_MAPCHECKSUM		3		/**< for catching cheater maps */
303 #define CS_MAXSOLDIERSPERTEAM	4	/**< max soldiers per team */
304 #define CS_MAXSOLDIERSPERPLAYER	5	/**< max soldiers per player when in teamplay mode */
305 #define CS_ENABLEMORALE		6		/**< enable the morale states in multiplayer */
306 #define CS_MAXTEAMS			7		/**< how many multiplayer teams for this map */
307 #define CS_PLAYERCOUNT		8		/**< amount of already connected players */
308 #define CS_VERSION			9		/**< what is the servers version */
309 #define CS_UFOCHECKSUM		10		/**< checksum of ufo files */
310 #define CS_OBJECTAMOUNT		11		/**< amount of defined objects in the script files */
311 #define CS_LIGHTMAP			12		/**< which lightmap to use */
312 #define CS_MAPZONE			13		/**< for terrain texture replacement */
313 #define CS_VICTORY_CONDITIONS	14	/**< a list of msgids seperated by , */
314 #define CS_ENTITYSTRING		15		/**< additional worldspawn settings */
315 #define CS_TILES			16
316 #define CS_POSITIONS		(CS_TILES+MAX_TILESTRINGS)
317 #define CS_MODELS			(CS_POSITIONS+MAX_TILESTRINGS)
318 #define CS_PLAYERNAMES		(CS_MODELS+MAX_MODELS)
319 #define CS_GENERAL			(CS_PLAYERNAMES+MAX_CLIENTS)
320 #define MAX_CONFIGSTRINGS	(CS_GENERAL+MAX_GENERAL)
321 
322 #define MAX_FORBIDDENLIST (MAX_EDICTS * 4)
323 
324 /** game types */
325 #define MAX_GAMETYPES 16
326 
327 #define MAX_CVARLISTINGAMETYPE 16
328 typedef struct cvarlist_s {
329 	char name[MAX_VAR];
330 	char value[MAX_VAR];
331 } cvarlist_t;
332 
333 typedef struct gametype_s {
334 	char id[MAX_VAR];	/**< script id */
335 	char name[MAX_VAR];	/**< translated menu name */
336 	cvarlist_t cvars[MAX_CVARLISTINGAMETYPE];
337 	int num_cvars;
338 } gametype_t;
339 
340 typedef struct mapDef_s {
341 	/* general */
342 	char* id;				/**< script file id */
343 	char* map;				/**< bsp or ump base filename (without extension and day or night char) */
344 	linkedList_t* params;	/**< in case of ump file, the assembly to use */
345 	char* description;		/**< the description to show in the menus */
346 	char* victoryCondition;	/**< the mission briefing victory condition */
347 	char* missionBriefing;	/**< the mission briefing description */
348 	char* size;				/**< small, medium, big */
349 	char* civTeam;			/**< the civilian team to use for this map - this can also be nullptr */
350 
351 	/* multiplayer */
352 	bool multiplayer;		/**< is this map multiplayer ready at all */
353 	int teams;				/**< multiplayer teams */
354 	linkedList_t* gameTypes;/**< gametype strings this map is useable for */
355 
356 
357 	/* singleplayer */
358 	bool campaign;			/**< available in campaign mode? */
359 	bool singleplayer;		/**< is this map available in singleplayer games? */
360 	bool nocunit;			/**< skip this map in cunit tests */
361 	int hwclass;			/**< skip this map everywhere if hwclass is higher than that of the pc */
362 	int maxAliens;			/**< Number of spawning points on the map */
363 	bool hurtAliens;		/**< hurt the aliens on spawning them - e.g. for ufocrash missions */
364 
365 	linkedList_t* terrains;		/**< terrain strings this map is useable for */
366 	linkedList_t* populations;	/**< population strings this map is useable for */
367 	linkedList_t* cultures;		/**< culture strings this map is useable for */
368 	bool storyRelated;			/**< Is this a mission story related? */
369 	int timesAlreadyUsed;		/**< Number of times the map has already been used */
370 	linkedList_t* ufos;			/**< Type of allowed UFOs on the map */
371 	linkedList_t* aircraft;		/**< Type of allowed aircraft on the map */
372 
373 	/** @note Don't end with ; - the trigger commands get the base index as
374 	 * parameter - see CP_ExecuteMissionTrigger - If you don't need the base index
375 	 * in your trigger command, you can seperate more commands with ; of course */
376 	char* onwin;		/**< trigger command after you've won a battle */
377 	char* onlose;		/**< trigger command after you've lost a battle */
378 } mapDef_t;
379 
380 #define MapDef_ForeachCondition(var, condition) \
381 	for (int var##__loopvar = 0; (var) = nullptr, var##__loopvar < csi.numMDs; var##__loopvar++) \
382 		if ((var) = &csi.mds[var##__loopvar], !(condition)) {} else
383 
384 #define MapDef_Foreach(var) MapDef_ForeachCondition(var, 1)
385 mapDef_t* Com_GetMapDefByIDX(int index);
386 mapDef_t* Com_GetMapDefinitionByID(const char* mapDefID);
387 
388 /**
389  * @brief The csi structure is the client-server-information structure
390  * which contains all the static data needed by the server and the client.
391  * @note Most of this comes from the script files
392  */
393 typedef struct csi_s {
394 	/** Object definitions */
395 	objDef_t ods[MAX_OBJDEFS];
396 	int numODs;
397 
398 	implantDef_t implants[MAX_IMPLANTS];
399 	int numImplants;
400 
401 	/** Inventory definitions */
402 	invDef_t ids[MAX_INVDEFS];
403 	int numIDs;
404 
405 	/** Damage type ids */
406 	int damNormal, damBlast, damFire;
407 	int damShock;	/**< Flashbang-type 'damage' (i.e. Blinding). */
408 
409 	/** Damage type ids */
410 	int damLaser, damPlasma, damParticle;
411 	int damStunGas;		/**< Stun gas attack (only effective against organic targets).
412 						 * @todo Maybe even make a differentiation between aliens/humans here? */
413 	int damStunElectro;	/**< Electro-Shock attack (effective against organic and robotic targets). */
414 	int damSmoke;		/**< smoke damage type that spawn smoke fields that blocks the visibility */
415 	int damIncendiary;	/**< incendiary damage type that spawns fire fields that will hurt actors */
416 
417 	/** Equipment definitions */
418 	equipDef_t eds[MAX_EQUIPDEFS];
419 	int numEDs;
420 
421 	/** Damage types */
422 	damageType_t dts[MAX_DAMAGETYPES];
423 	int numDTs;
424 
425 	/** team definitions */
426 	teamDef_t teamDef[MAX_TEAMDEFS];
427 	int numTeamDefs;
428 
429 	/** the current assigned teams for this mission
430 	 * @todo this does not belong here - this is only static data that is shared */
431 	const teamDef_t* alienTeams[MAX_TEAMS_PER_MISSION];
432 	const chrTemplate_t* alienChrTemplates[MAX_TEAMS_PER_MISSION];
433 	int numAlienTeams;
434 
435 	/** character templates */
436 	chrTemplate_t chrTemplates[MAX_CHARACTER_TEMPLATES];
437 	int numChrTemplates;
438 	linkedList_t* bodyTemplates;
439 
440 	linkedList_t* actorNames;
441 
442 	ugv_t ugvs[MAX_UGV];
443 	int numUGV;
444 
445 	gametype_t gts[MAX_GAMETYPES];
446 	int numGTs;
447 
448 	/** Map definitions */
449 	mapDef_t mds[MAX_MAPDEFS];
450 	int numMDs;
451 } csi_t;
452 
453 extern csi_t csi;
454 
455 /** @brief Reject messages that are send to the client from the game module */
456 #define REJ_PASSWORD_REQUIRED_OR_INCORRECT "Password required or incorrect."
457 #define BAD_RCON_PASSWORD "Bad rcon_password."
458 #define REJ_BANNED "Banned."
459 #define REJ_SERVER_FULL "Server is full."
460 #define REJ_SERVER_VERSION_MISMATCH "The server is running a different version of the game."
461 #define REJ_GAME_ALREADY_STARTED "Game has started already."
462 #define REJ_CONNECTION_REFUSED "Connection refused."
463 
464 #define SV_CMD_CONNECT "connect"
465 #define SV_CMD_RCON "rcon"
466 #define SV_CMD_STATUS "status"
467 #define SV_CMD_TEAMINFO "teaminfo"
468 #define SV_CMD_INFO "info"
469 #define SV_CMD_PRINT "print"
470 
471 /* unused */
472 #define SV_CMD_ACK "ack"
473 #define CL_CMD_PING "ping"
474 #define CL_CMD_ECHO "echo"
475 /* unused end */
476 
477 #define CL_PRECACHE "precache"
478 #define CL_SPAWNSOLDIERS "spawnsoldiers"
479 #define CL_STARTMATCH "startmatch"
480 
481 #define CL_CMD_COMMAND "cmd"
482 #define CL_CMD_CLIENT_CONNECT "client_connect"
483 
484 #define NET_STATE_NEW "new"
485 #define NET_STATE_BEGIN "begin"
486 #define NET_STATE_STARTMATCH "startmatch"
487 #define NET_STATE_DISCONNECT "disconnect"
488 #define NET_STATE_INFO "info"
489