1 /**
2  * @file
3  * @brief Main server include file.
4  */
5 
6 /*
7 All original material Copyright (C) 2002-2013 UFO: Alien Invasion.
8 
9 Original file from Quake 2 v3.21: quake2-2.31/server/server.h
10 Copyright (C) 1997-2001 Id Software, Inc.
11 
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 
21 See the GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 
27 */
28 
29 #pragma once
30 
31 #include "../common/common.h"
32 #include "../common/http.h"
33 #include "../shared/infostring.h"
34 #include "../game/game.h"
35 #include <SDL_thread.h>
36 
37 extern memPool_t* sv_genericPool;
38 
39 typedef struct sv_edict_s {
40 	struct worldSector_s* worldSector;	/**< the sector this edict is linked into */
41 	struct sv_edict_s* nextEntityInWorldSector;
42 	bool linked;		/**< linked into the world */
43 	edict_t* ent;
44 } sv_edict_t;
45 
46 /** @brief static mesh models (none-animated) can have a server side flag set to be clipped for pathfinding */
47 typedef struct sv_model_s {
48 	AABB aabb;			/**< the mins and maxs of the model bounding box */
49 	int frame;			/**< the frame the mins and maxs were calculated for */
50 	char* name;			/**< the model path (relative to base/ */
51 } sv_model_t;
52 
53 /**
54  * @brief To avoid linearly searching through lists of entities during environment testing,
55  * the world is carved up with an evenly spaced, axially aligned bsp tree.
56  */
57 typedef struct worldSector_s {
58 	int axis;					/**< -1 = leaf node */
59 	float dist;
60 	struct worldSector_s* children[2];
61 	sv_edict_t* entities;
62 } worldSector_t;
63 
64 #define	AREA_NODES	32
65 
66 typedef struct pending_event_s {
67 	/** this is true when there was an event - and false if the event reached the end */
68 	bool pending;
69 	/** player mask of the current event */
70 	int playerMask;
71 	int type;
72 	int entnum;
73 	dbuffer* buf;
74 } pending_event_t;
75 
76 typedef struct serverInstanceStatic_s {
77 	bool initialized;			/**< sv_init has completed */
78 	int realtime;				/**< always increasing, no clamping, etc */
79 	struct datagram_socket* netDatagramSocket;
80 	struct client_s* clients;	/**< [sv_maxclients->value]; */
81 	int lastHeartbeat;			/**< time where the last heartbeat was send to the master server
82 								 * Set to a huge negative value to send immmediately */
83 	int lastPing;
84 	bool abandon;				/**< shutdown server when all clients disconnect and don't accept new connections */
85 	bool killserver;			/**< will initiate shutdown once abandon is set */
86 	SDL_mutex *serverMutex;
87 	SDL_cond *gameFrameCond;	/**< the signal that the game frame threads waits for */
88 	SDL_Thread *gameThread;
89 #ifndef HARD_LINKED_GAME
90 	void* gameLibrary;
91 #endif
92 	game_export_t* ge;
93 } serverInstanceStatic_t;
94 
95 typedef enum server_state_e {
96 	ss_dead,					/**< no map loaded */
97 	ss_restart,					/**< clients should reconnect, the server switched the map */
98 	ss_loading,					/**< spawning level edicts */
99 	ss_game,					/**< actively running */
100 	ss_game_shutdown			/**< tell the game lib to end */
101 } server_state_t;
102 
103 /**
104  * @brief Struct that is only valid for one map. It's deleted on every map load.
105  */
106 typedef struct serverInstanceGame_s {
107 	server_state_t state;		/**< precache commands are only valid during load */
108 
109 	char name[MAX_QPATH];		/**< map name */
110 	char assembly[MAX_QPATH];	/**< random map assembly name */
111 	struct cBspModel_s* models[MAX_MODELS];
112 
113 	bool endgame;
114 	bool spawned;				/**< set when the actors have spawned - no further connections are allowed in this case */
115 	bool started;				/**< set when the match has started */
116 
117 	char configstrings[MAX_CONFIGSTRINGS][MAX_TOKEN_CHARS];
118 
119 	dbuffer* messageBuffer;		/**< the message buffer that was received from the client. */
120 	pending_event_t pendingEvent;
121 	pending_event_t eventQueue[64];
122 	int eventQueuePos;
123 
124 	mapData_t mapData;
125 
126 	mapTiles_t mapTiles;
127 
128 	sv_model_t svModels[MAX_MOD_KNOWN];
129 	unsigned int numSVModels;
130 	sv_edict_t edicts[MAX_EDICTS];
131 	worldSector_t worldSectors[AREA_NODES];
132 	unsigned int numWorldSectors;
133 
134 	memPool_t* gameSysPool;		/**< the mempool for the game lib */
135 } serverInstanceGame_t;
136 
137 #define PLAYER_NUM(n) ((player_t*)((byte*)svs.ge->players + svs.ge->player_size * (n)))
138 
139 typedef enum {
140 	cs_free,					/**< can be reused for a new connection */
141 	cs_connected,				/**< has been assigned to a client_t, but not in game yet */
142 	cs_spawning,				/**< received new, not begin yet */
143 	cs_began,					/**< began was received, client side rendering is active - in this stage the player is an spectator
144 								 * and still has to spawn his soldiers */
145 	cs_spawned					/**< client is fully in game and soldiers were spawned */
146 } client_state_t;
147 
148 /**
149  * a client can leave the server in one of four ways:
150  * @li dropping properly by quitting or disconnecting
151  * @li timing out if no valid messages are received
152  * @li getting kicked off by the server operator
153  * @li a program error, like an overflowed reliable buffer
154  */
155 typedef struct client_s {
156 	client_state_t state;
157 	char userinfo[MAX_INFO_STRING];
158 	player_t* player;			/**< game client structure */
159 	char name[32];				/**< extracted from userinfo, high bits masked */
160 	int messagelevel;			/**< for filtering printed messages */
161 	int lastmessage;			/**< when packet was last received */
162 	char peername[256];
163 	struct net_stream *stream;
164 } client_t;
165 
166 extern serverInstanceStatic_t svs;	/**< persistant server instance info */
167 extern serverInstanceGame_t* sv;	/**< server data per game/map */
168 
169 extern cvar_t* sv_mapname;
170 extern cvar_t* sv_rma;
171 /** @brief display a character graphic of the tiles placed when RMA2 reaches a dead end. */
172 extern cvar_t* sv_rmadisplaythemap;
173 extern cvar_t* sv_public;			/**< should heartbeats be sent? (only for public servers) */
174 extern cvar_t* sv_dumpmapassembly;
175 extern cvar_t* sv_threads;			/**< run the game lib threaded */
176 
177 /* sv_main.c */
178 void SV_DropClient(client_t* drop, const char* message);
179 int SV_CountPlayers(void);
180 void SV_InitOperatorCommands(void);
181 void SV_UserinfoChanged(client_t* cl);
182 void SV_ReadPacket(struct net_stream *s);
183 char* SV_GetConfigString(int index);
184 int SV_GetConfigStringInteger(int index);
185 char* SV_SetConfigString(int index, ...);
186 /* ensure that always two parameters are used */
187 #define SV_SetConfigString(index, value) SV_SetConfigString(index, value)
188 client_t* SV_GetNextClient(client_t* lastClient);
189 client_t* SV_GetClient(int index);
190 
191 /* sv_mapcycle.c */
192 void SV_MapcycleInit(void);
193 void SV_NextMapcycle(void);
194 void SV_MapcycleClear(void);
195 
196 /* sv_init.c */
197 void SV_Map(bool day, const char* levelstring, const char* assembly);
198 
199 void SV_Multicast(int mask, const dbuffer &msg);
200 void SV_ClientCommand(client_t* client, const char* fmt, ...) __attribute__((format(__printf__,2,3)));
201 void SV_ClientPrintf(client_t* cl, int level, const char* fmt, ...) __attribute__((format(__printf__,3,4)));
202 void SV_BroadcastPrintf(int level, const char* fmt, ...) __attribute__((format(__printf__,2,3)));
203 
204 /* sv_user.c */
205 void SV_ExecuteClientMessage(client_t* cl, int cmd, dbuffer* msg);
206 void SV_SetClientState(client_t* client, client_state_t state);
207 
208 /* sv_ccmds.c */
209 void SV_SetMaster_f(void);
210 void SV_Heartbeat_f(void);
211 bool SV_CheckMap(const char* map, const char* assembly);
212 
213 /* sv_game.c */
214 int SV_RunGameFrameThread(void* data);
215 void SV_RunGameFrame(void);
216 void SV_InitGameProgs(void);
217 void SV_ShutdownGameProgs(void);
218 
219 /*============================================================ */
220 
221 void SV_ClearWorld(void);
222 
223 void SV_UnlinkEdict(edict_t* ent);
224 void SV_LinkEdict(edict_t* ent);
225 int SV_AreaEdicts(const vec3_t mins, const vec3_t maxs, edict_t** list, int maxcount);
226 
227 /*=================================================================== */
228 
229 /* returns the CONTENTS_* value from the world at the given point. */
230 int SV_PointContents(const vec3_t p);
231 const char* SV_GetFootstepSound(const char* texture);
232 float SV_GetBounceFraction(const char* texture);
233 bool SV_LoadModelAABB(const char* model, int frame, AABB& aabb);
234 trace_t SV_Trace(const vec3_t start, const AABB &box, const vec3_t end, const edict_t* passedict, int contentmask);
235 server_state_t SV_GetServerState(void);
236 void SV_SetServerState(server_state_t);
237