forked from JACoders/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsv_init.cpp
496 lines (379 loc) · 12 KB
/
sv_init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
===========================================================================
Copyright (C) 1999 - 2005, Id Software, Inc.
Copyright (C) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/
#include "../server/exe_headers.h"
#include "../client/snd_music.h" // didn't want to put this in snd_local because of rebuild times etc.
#include "server.h"
#if !defined (MINIHEAP_H_INC)
#include "../qcommon/MiniHeap.h"
#endif
void CM_CleanLeafCache(void);
extern void SV_FreeClient(client_t*);
CMiniHeap *G2VertSpaceServer = NULL;
/*
Ghoul2 Insert End
*/
/*
===============
SV_SetConfigstring
===============
*/
void SV_SetConfigstring (int index, const char *val) {
if ( index < 0 || index >= MAX_CONFIGSTRINGS ) {
Com_Error (ERR_DROP, "SV_SetConfigstring: bad index %i\n", index);
}
if ( !val ) {
val = "";
}
// don't bother broadcasting an update if no change
if ( !strcmp( val, sv.configstrings[ index ] ) ) {
return;
}
// change the string in sv
Z_Free( sv.configstrings[index] );
sv.configstrings[index] = CopyString( val );
// send it to all the clients if we aren't
// spawning a new server
if ( sv.state == SS_GAME ) {
SV_SendServerCommand( NULL, "cs %i \"%s\"\n", index, val );
}
}
/*
===============
SV_GetConfigstring
===============
*/
void SV_GetConfigstring( int index, char *buffer, int bufferSize ) {
if ( bufferSize < 1 ) {
Com_Error( ERR_DROP, "SV_GetConfigstring: bufferSize == %i", bufferSize );
}
if ( index < 0 || index >= MAX_CONFIGSTRINGS ) {
Com_Error (ERR_DROP, "SV_GetConfigstring: bad index %i\n", index);
}
if ( !sv.configstrings[index] ) {
buffer[0] = 0;
return;
}
Q_strncpyz( buffer, sv.configstrings[index], bufferSize );
}
/*
===============
SV_SetUserinfo
===============
*/
void SV_SetUserinfo( int index, const char *val ) {
if ( index < 0 || index >= 1 ) {
Com_Error (ERR_DROP, "SV_SetUserinfo: bad index %i\n", index);
}
if ( !val ) {
val = "";
}
Q_strncpyz( svs.clients[ index ].userinfo, val, sizeof( svs.clients[ index ].userinfo ) );
}
/*
===============
SV_GetUserinfo
===============
*/
void SV_GetUserinfo( int index, char *buffer, int bufferSize ) {
if ( bufferSize < 1 ) {
Com_Error( ERR_DROP, "SV_GetUserinfo: bufferSize == %i", bufferSize );
}
if ( index < 0 || index >= 1 ) {
Com_Error (ERR_DROP, "SV_GetUserinfo: bad index %i\n", index);
}
Q_strncpyz( buffer, svs.clients[ index ].userinfo, bufferSize );
}
/*
================
SV_CreateBaseline
Entity baselines are used to compress non-delta messages
to the clients -- only the fields that differ from the
baseline will be transmitted
================
*/
void SV_CreateBaseline( void ) {
gentity_t *svent;
int entnum;
for ( entnum = 0; entnum < ge->num_entities ; entnum++ ) {
svent = SV_GentityNum(entnum);
if (!svent->inuse) {
continue;
}
if (!svent->linked) {
continue;
}
svent->s.number = entnum;
//
// take current state as baseline
//
sv.svEntities[entnum].baseline = svent->s;
}
}
/*
===============
SV_Startup
Called when a game is about to begin
===============
*/
void SV_Startup( void ) {
if ( svs.initialized ) {
Com_Error( ERR_FATAL, "SV_Startup: svs.initialized" );
}
svs.clients = (struct client_s *) Z_Malloc ( sizeof(client_t) * 1, TAG_CLIENTS, qtrue );
svs.numSnapshotEntities = 2 * 4 * 64;
svs.initialized = qtrue;
Cvar_Set( "sv_running", "1" );
}
qboolean CM_SameMap(const char *server);
void Cvar_Defrag(void);
/*
================
SV_SpawnServer
Change the server to a new map, taking all connected
clients along with it.
================
*/
void SV_SpawnServer( const char *server, ForceReload_e eForceReload, qboolean bAllowScreenDissolve )
{
int i;
int checksum;
re.RegisterMedia_LevelLoadBegin( server, eForceReload, bAllowScreenDissolve );
Cvar_SetValue( "cl_paused", 0 );
Cvar_Set( "timescale", "1" );//jic we were skipping
// shut down the existing game if it is running
SV_ShutdownGameProgs(qtrue);
Com_Printf ("------ Server Initialization ------\n%s\n", com_version->string);
Com_Printf ("Server: %s\n",server);
// Moved up from below to help reduce fragmentation
if (svs.snapshotEntities)
{
Z_Free(svs.snapshotEntities);
svs.snapshotEntities = NULL;
}
// don't let sound stutter and dump all stuff on the hunk
CL_MapLoading();
if (!CM_SameMap(server))
{ //rww - only clear if not loading the same map
CM_ClearMap();
}
// Miniheap never changes sizes, so I just put it really early in mem.
G2VertSpaceServer->ResetHeap();
Hunk_Clear();
// wipe the entire per-level structure
// Also moved up, trying to do all freeing before new allocs
for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) {
if ( sv.configstrings[i] ) {
Z_Free( sv.configstrings[i] );
sv.configstrings[i] = NULL;
}
}
// Collect all the small allocations done by the cvar system
// This frees, then allocates. Make it the last thing before other
// allocations begin!
Cvar_Defrag();
/*
This is useful for debugging memory fragmentation. Please don't
remove it.
*/
// init client structures and svs.numSnapshotEntities
// This is moved down quite a bit, but should be safe. And keeps
// svs.clients right at the beginning of memory
if ( !Cvar_VariableIntegerValue("sv_running") ) {
SV_Startup();
}
// clear out those shaders, images and Models
/*R_InitImages();
R_InitShaders();
R_ModelInit();*/
re.SVModelInit();
// allocate the snapshot entities
svs.snapshotEntities = (entityState_t *) Z_Malloc (sizeof(entityState_t)*svs.numSnapshotEntities, TAG_CLIENTS, qtrue );
Music_SetLevelName(server);
// toggle the server bit so clients can detect that a
// server has changed
//!@ svs.snapFlagServerBit ^= SNAPFLAG_SERVERCOUNT;
// set nextmap to the same map, but it may be overriden
// by the game startup or another console command
Cvar_Set( "nextmap", va("map %s", server) );
memset (&sv, 0, sizeof(sv));
for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) {
sv.configstrings[i] = CopyString("");
}
sv.time = 1000;
re.G2API_SetTime(sv.time,G2T_SV_TIME);
CM_LoadMap( va("maps/%s.bsp", server), qfalse, &checksum, qfalse );
// set serverinfo visible name
Cvar_Set( "mapname", server );
Cvar_Set( "sv_mapChecksum", va("%i",checksum) );
// serverid should be different each time
sv.serverId = com_frameTime;
Cvar_Set( "sv_serverid", va("%i", sv.serverId ) );
// clear physics interaction links
SV_ClearWorld ();
// media configstring setting should be done during
// the loading stage, so connected clients don't have
// to load during actual gameplay
sv.state = SS_LOADING;
// load and spawn all other entities
SV_InitGameProgs();
// run a few frames to allow everything to settle
for ( i = 0 ;i < 4 ; i++ ) {
ge->RunFrame( sv.time );
sv.time += 100;
re.G2API_SetTime(sv.time,G2T_SV_TIME);
}
#ifndef JK2_MODE
ge->ConnectNavs(sv_mapname->string, sv_mapChecksum->integer);
#endif
// create a baseline for more efficient communications
SV_CreateBaseline ();
for (i=0 ; i<1 ; i++) {
// clear all time counters, because we have reset sv.time
svs.clients[i].lastPacketTime = 0;
svs.clients[i].lastConnectTime = 0;
// send the new gamestate to all connected clients
if (svs.clients[i].state >= CS_CONNECTED) {
char *denied;
// connect the client again
denied = ge->ClientConnect( i, qfalse, eNO/*qfalse*/ ); // firstTime = qfalse, qbFromSavedGame
if ( denied ) {
// this generally shouldn't happen, because the client
// was connected before the level change
SV_DropClient( &svs.clients[i], denied );
} else {
svs.clients[i].state = CS_CONNECTED;
// when we get the next packet from a connected client,
// the new gamestate will be sent
}
}
}
// run another frame to allow things to look at all connected clients
ge->RunFrame( sv.time );
sv.time += 100;
re.G2API_SetTime(sv.time,G2T_SV_TIME);
// save systeminfo and serverinfo strings
SV_SetConfigstring( CS_SYSTEMINFO, Cvar_InfoString( CVAR_SYSTEMINFO ) );
cvar_modifiedFlags &= ~CVAR_SYSTEMINFO;
SV_SetConfigstring( CS_SERVERINFO, Cvar_InfoString( CVAR_SERVERINFO ) );
cvar_modifiedFlags &= ~CVAR_SERVERINFO;
// any media configstring setting now should issue a warning
// and any configstring changes should be reliably transmitted
// to all clients
sv.state = SS_GAME;
Hunk_SetMark();
Z_Validate();
Z_Validate();
Z_Validate();
Com_Printf ("-----------------------------------\n");
}
#define G2_VERT_SPACE_SIZE 256
#define G2_MINIHEAP_SIZE G2_VERT_SPACE_SIZE*1024
/*
===============
SV_Init
Only called at main exe startup, not for each game
===============
*/
void SV_Init (void) {
SV_AddOperatorCommands ();
// serverinfo vars
Cvar_Get ("protocol", va("%i", PROTOCOL_VERSION), CVAR_SERVERINFO | CVAR_ROM);
sv_mapname = Cvar_Get ("mapname", "nomap", CVAR_SERVERINFO | CVAR_ROM);
// systeminfo
Cvar_Get ("helpUsObi", "0", CVAR_SYSTEMINFO );
sv_serverid = Cvar_Get ("sv_serverid", "0", CVAR_SYSTEMINFO | CVAR_ROM );
// server vars
sv_fps = Cvar_Get ("sv_fps", "20", CVAR_TEMP );
sv_timeout = Cvar_Get ("sv_timeout", "120", CVAR_TEMP );
sv_zombietime = Cvar_Get ("sv_zombietime", "2", CVAR_TEMP );
Cvar_Get ("nextmap", "", CVAR_TEMP );
sv_spawntarget = Cvar_Get ("spawntarget", "", 0 );
sv_reconnectlimit = Cvar_Get ("sv_reconnectlimit", "3", 0);
sv_showloss = Cvar_Get ("sv_showloss", "0", 0);
sv_killserver = Cvar_Get ("sv_killserver", "0", 0);
sv_mapChecksum = Cvar_Get ("sv_mapChecksum", "", CVAR_ROM);
sv_testsave = Cvar_Get ("sv_testsave", "0", 0);
sv_compress_saved_games = Cvar_Get ("sv_compress_saved_games", "1", 0);
// Only allocated once, no point in moving it around and fragmenting
// create a heap for Ghoul2 to use for game side model vertex transforms used in collision detection
{
static CMiniHeap singleton(G2_MINIHEAP_SIZE);
G2VertSpaceServer = &singleton;
}
}
/*
==================
SV_FinalMessage
Used by SV_Shutdown to send a final message to all
connected clients before the server goes down. The messages are sent immediately,
not just stuck on the outgoing message list, because the server is going
to totally exit after returning from this function.
==================
*/
void SV_FinalMessage( const char *message ) {
client_t *cl = svs.clients;
SV_SendServerCommand( NULL, "print \"%s\"", message );
SV_SendServerCommand( NULL, "disconnect" );
// send it twice, ignoring rate
if ( cl->state >= CS_CONNECTED ) {
SV_SendClientSnapshot( cl );
SV_SendClientSnapshot( cl );
}
}
/*
================
SV_Shutdown
Called when each game quits,
before Sys_Quit or Sys_Error
================
*/
void SV_Shutdown( const char *finalmsg ) {
int i;
if ( !com_sv_running || !com_sv_running->integer ) {
return;
}
//Com_Printf( "----- Server Shutdown -----\n" );
if ( svs.clients && !com_errorEntered ) {
SV_FinalMessage( finalmsg );
}
SV_RemoveOperatorCommands();
SV_ShutdownGameProgs(qfalse);
if (svs.snapshotEntities)
{
Z_Free(svs.snapshotEntities);
svs.snapshotEntities = NULL;
}
for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) {
if ( sv.configstrings[i] ) {
Z_Free( sv.configstrings[i] );
}
}
// free current level
memset( &sv, 0, sizeof( sv ) );
// free server static data
if ( svs.clients ) {
SV_FreeClient(svs.clients);
Z_Free( svs.clients );
}
memset( &svs, 0, sizeof( svs ) );
// Ensure we free any memory used by the leaf cache.
CM_CleanLeafCache();
Cvar_Set( "sv_running", "0" );
//Com_Printf( "---------------------------\n" );
}