]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_main.c
Quick hack to fix the calls to "open" on Win32
[divverent/darkplaces.git] / sv_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // sv_main.c -- server main program
21
22 #include "quakedef.h"
23
24 // select which protocol to host, by name
25 // this is named the same as PROTOCOL_DARKPLACES5 for example, minus the PROTOCOL_ prefix
26 cvar_t sv_protocolname = {0, "sv_protocolname", "DARKPLACES5"};
27 cvar_t sv_ratelimitlocalplayer = {0, "sv_ratelimitlocalplayer", "0"};
28 cvar_t sv_maxrate = {CVAR_SAVE | CVAR_NOTIFY, "sv_maxrate", "10000"};
29
30 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "1"}; // fast but loose
31 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "0"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
32 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
33 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
34
35 extern cvar_t sys_ticrate;
36
37 cvar_t sv_gameplayfix_grenadebouncedownslopes = {0, "sv_gameplayfix_grenadebouncedownslopes", "1"};
38 cvar_t sv_gameplayfix_noairborncorpse = {0, "sv_gameplayfix_noairborncorpse", "1"};
39 cvar_t sv_gameplayfix_stepdown = {0, "sv_gameplayfix_stepdown", "1"};
40 cvar_t sv_gameplayfix_stepwhilejumping = {0, "sv_gameplayfix_stepwhilejumping", "1"};
41 cvar_t sv_gameplayfix_swiminbmodels = {0, "sv_gameplayfix_swiminbmodels", "1"};
42
43 server_t sv;
44 server_static_t svs;
45
46 static char localmodels[MAX_MODELS][5];                 // inline model names for precache
47
48 mempool_t *sv_edicts_mempool = NULL;
49
50 //============================================================================
51
52 extern void SV_Phys_Init (void);
53 extern void SV_World_Init (void);
54 static void SV_SaveEntFile_f(void);
55
56 /*
57 ===============
58 SV_Init
59 ===============
60 */
61 void SV_Init (void)
62 {
63         int i;
64
65         Cmd_AddCommand("sv_saveentfile", SV_SaveEntFile_f);
66         Cvar_RegisterVariable (&sv_maxvelocity);
67         Cvar_RegisterVariable (&sv_gravity);
68         Cvar_RegisterVariable (&sv_friction);
69         Cvar_RegisterVariable (&sv_edgefriction);
70         Cvar_RegisterVariable (&sv_stopspeed);
71         Cvar_RegisterVariable (&sv_maxspeed);
72         Cvar_RegisterVariable (&sv_accelerate);
73         Cvar_RegisterVariable (&sv_idealpitchscale);
74         Cvar_RegisterVariable (&sv_aim);
75         Cvar_RegisterVariable (&sv_nostep);
76         Cvar_RegisterVariable (&sv_deltacompress);
77         Cvar_RegisterVariable (&sv_cullentities_pvs);
78         Cvar_RegisterVariable (&sv_cullentities_trace);
79         Cvar_RegisterVariable (&sv_cullentities_stats);
80         Cvar_RegisterVariable (&sv_entpatch);
81         Cvar_RegisterVariable (&sv_gameplayfix_grenadebouncedownslopes);
82         Cvar_RegisterVariable (&sv_gameplayfix_noairborncorpse);
83         Cvar_RegisterVariable (&sv_gameplayfix_stepdown);
84         Cvar_RegisterVariable (&sv_gameplayfix_stepwhilejumping);
85         Cvar_RegisterVariable (&sv_gameplayfix_swiminbmodels);
86         Cvar_RegisterVariable (&sv_protocolname);
87         Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
88         Cvar_RegisterVariable (&sv_maxrate);
89
90         SV_Phys_Init();
91         SV_World_Init();
92
93         for (i = 0;i < MAX_MODELS;i++)
94                 sprintf (localmodels[i], "*%i", i);
95
96         sv_edicts_mempool = Mem_AllocPool("server edicts", 0, NULL);
97 }
98
99 static void SV_SaveEntFile_f(void)
100 {
101         char basename[MAX_QPATH];
102         if (!sv.active || !sv.worldmodel)
103         {
104                 Con_Print("Not running a server\n");
105                 return;
106         }
107         FS_StripExtension(sv.worldmodel->name, basename, sizeof(basename));
108         FS_WriteFile(va("%s.ent", basename), sv.worldmodel->brush.entities, strlen(sv.worldmodel->brush.entities));
109 }
110
111 /*
112 =============================================================================
113
114 EVENT MESSAGES
115
116 =============================================================================
117 */
118
119 /*
120 ==================
121 SV_StartParticle
122
123 Make sure the event gets sent to all clients
124 ==================
125 */
126 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
127 {
128         int             i, v;
129
130         if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
131                 return;
132         MSG_WriteByte (&sv.datagram, svc_particle);
133         MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
134         MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
135         MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
136         for (i=0 ; i<3 ; i++)
137         {
138                 v = dir[i]*16;
139                 if (v > 127)
140                         v = 127;
141                 else if (v < -128)
142                         v = -128;
143                 MSG_WriteChar (&sv.datagram, v);
144         }
145         MSG_WriteByte (&sv.datagram, count);
146         MSG_WriteByte (&sv.datagram, color);
147 }
148
149 /*
150 ==================
151 SV_StartEffect
152
153 Make sure the event gets sent to all clients
154 ==================
155 */
156 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
157 {
158         if (modelindex >= 256 || startframe >= 256)
159         {
160                 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
161                         return;
162                 MSG_WriteByte (&sv.datagram, svc_effect2);
163                 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
164                 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
165                 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
166                 MSG_WriteShort (&sv.datagram, modelindex);
167                 MSG_WriteShort (&sv.datagram, startframe);
168                 MSG_WriteByte (&sv.datagram, framecount);
169                 MSG_WriteByte (&sv.datagram, framerate);
170         }
171         else
172         {
173                 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
174                         return;
175                 MSG_WriteByte (&sv.datagram, svc_effect);
176                 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
177                 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
178                 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
179                 MSG_WriteByte (&sv.datagram, modelindex);
180                 MSG_WriteByte (&sv.datagram, startframe);
181                 MSG_WriteByte (&sv.datagram, framecount);
182                 MSG_WriteByte (&sv.datagram, framerate);
183         }
184 }
185
186 /*
187 ==================
188 SV_StartSound
189
190 Each entity can have eight independant sound sources, like voice,
191 weapon, feet, etc.
192
193 Channel 0 is an auto-allocate channel, the others override anything
194 already running on that entity/channel pair.
195
196 An attenuation of 0 will play full volume everywhere in the level.
197 Larger attenuations will drop off.  (max 4 attenuation)
198
199 ==================
200 */
201 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume, float attenuation)
202 {
203         int sound_num, field_mask, i, ent;
204
205         if (volume < 0 || volume > 255)
206                 Host_Error ("SV_StartSound: volume = %i", volume);
207
208         if (attenuation < 0 || attenuation > 4)
209                 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
210
211         if (channel < 0 || channel > 7)
212                 Host_Error ("SV_StartSound: channel = %i", channel);
213
214         if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
215                 return;
216
217 // find precache number for sound
218         for (sound_num=1 ; sound_num<MAX_SOUNDS && sv.sound_precache[sound_num] ; sound_num++)
219                 if (!strcmp(sample, sv.sound_precache[sound_num]))
220                         break;
221
222         if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
223         {
224                 Con_Printf("SV_StartSound: %s not precached\n", sample);
225                 return;
226         }
227
228         ent = NUM_FOR_EDICT(entity);
229
230         field_mask = 0;
231         if (volume != DEFAULT_SOUND_PACKET_VOLUME)
232                 field_mask |= SND_VOLUME;
233         if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
234                 field_mask |= SND_ATTENUATION;
235         if (ent >= 8192)
236                 field_mask |= SND_LARGEENTITY;
237         if (sound_num >= 256 || channel >= 8)
238                 field_mask |= SND_LARGESOUND;
239
240 // directed messages go only to the entity they are targeted on
241         MSG_WriteByte (&sv.datagram, svc_sound);
242         MSG_WriteByte (&sv.datagram, field_mask);
243         if (field_mask & SND_VOLUME)
244                 MSG_WriteByte (&sv.datagram, volume);
245         if (field_mask & SND_ATTENUATION)
246                 MSG_WriteByte (&sv.datagram, attenuation*64);
247         if (field_mask & SND_LARGEENTITY)
248         {
249                 MSG_WriteShort (&sv.datagram, ent);
250                 MSG_WriteByte (&sv.datagram, channel);
251         }
252         else
253                 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
254         if (field_mask & SND_LARGESOUND)
255                 MSG_WriteShort (&sv.datagram, sound_num);
256         else
257                 MSG_WriteByte (&sv.datagram, sound_num);
258         for (i = 0;i < 3;i++)
259                 MSG_WriteCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]), sv.protocol);
260 }
261
262 /*
263 ==============================================================================
264
265 CLIENT SPAWNING
266
267 ==============================================================================
268 */
269
270 /*
271 ================
272 SV_SendServerinfo
273
274 Sends the first message from the server to a connected client.
275 This will be sent on the initial connection and upon each server load.
276 ================
277 */
278 void SV_SendServerinfo (client_t *client)
279 {
280         char                    **s;
281         char                    message[128];
282
283         // edicts get reallocated on level changes, so we need to update it here
284         client->edict = EDICT_NUM((client - svs.clients) + 1);
285
286         // if client is a botclient coming from a level change, we need to set up
287         // client info that normally requires networking
288         if (!client->netconnection)
289         {
290                 int i;
291
292                 // set up the edict
293                 ED_ClearEdict(client->edict);
294
295                 // copy spawn parms out of the client_t
296                 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
297                         (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
298
299                 // call the spawn function
300                 pr_global_struct->time = sv.time;
301                 pr_global_struct->self = EDICT_TO_PROG(client->edict);
302                 PR_ExecuteProgram (pr_global_struct->ClientConnect, "QC function ClientConnect is missing");
303                 PR_ExecuteProgram (pr_global_struct->PutClientInServer, "QC function PutClientInServer is missing");
304                 host_client->spawned = true;
305                 return;
306         }
307
308         // LordHavoc: clear entityframe tracking
309
310         if (client->entitydatabase)
311                 EntityFrame_FreeDatabase(client->entitydatabase);
312         if (client->entitydatabase4)
313                 EntityFrame4_FreeDatabase(client->entitydatabase4);
314         if (client->entitydatabase5)
315                 EntityFrame5_FreeDatabase(client->entitydatabase5);
316
317         if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
318                 client->entitydatabase = EntityFrame_AllocDatabase(sv_clients_mempool);
319         if (sv.protocol == PROTOCOL_DARKPLACES4)
320                 client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool);
321         if (sv.protocol == PROTOCOL_DARKPLACES5)
322                 client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_clients_mempool);
323
324         MSG_WriteByte (&client->message, svc_print);
325         snprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
326         MSG_WriteString (&client->message,message);
327
328         MSG_WriteByte (&client->message, svc_serverinfo);
329         MSG_WriteLong (&client->message, sv.protocol);
330         MSG_WriteByte (&client->message, svs.maxclients);
331
332         if (!coop.integer && deathmatch.integer)
333                 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
334         else
335                 MSG_WriteByte (&client->message, GAME_COOP);
336
337         MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
338
339         for (s = sv.model_precache+1 ; *s ; s++)
340                 MSG_WriteString (&client->message, *s);
341         MSG_WriteByte (&client->message, 0);
342
343         for (s = sv.sound_precache+1 ; *s ; s++)
344                 MSG_WriteString (&client->message, *s);
345         MSG_WriteByte (&client->message, 0);
346
347 // send music
348         MSG_WriteByte (&client->message, svc_cdtrack);
349         MSG_WriteByte (&client->message, sv.edicts->v->sounds);
350         MSG_WriteByte (&client->message, sv.edicts->v->sounds);
351
352 // set view
353         MSG_WriteByte (&client->message, svc_setview);
354         MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
355
356         MSG_WriteByte (&client->message, svc_signonnum);
357         MSG_WriteByte (&client->message, 1);
358
359         client->sendsignon = true;
360         client->spawned = false;                // need prespawn, spawn, etc
361 }
362
363 /*
364 ================
365 SV_ConnectClient
366
367 Initializes a client_t for a new net connection.  This will only be called
368 once for a player each game, not once for each level change.
369 ================
370 */
371 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
372 {
373         client_t                *client;
374         int                             i;
375         float                   spawn_parms[NUM_SPAWN_PARMS];
376
377         client = svs.clients + clientnum;
378
379 // set up the client_t
380         if (sv.loadgame)
381                 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
382         memset (client, 0, sizeof(*client));
383         client->active = true;
384         client->netconnection = netconnection;
385
386         Con_DPrintf("Client %s connected\n", client->netconnection ? client->netconnection->address : "botclient");
387
388         strcpy(client->name, "unconnected");
389         strcpy(client->old_name, "unconnected");
390         client->spawned = false;
391         client->edict = EDICT_NUM(clientnum+1);
392         client->message.data = client->msgbuf;
393         client->message.maxsize = sizeof(client->msgbuf);
394         client->message.allowoverflow = true;           // we can catch it
395         // updated by receiving "rate" command from client
396         client->rate = NET_MINRATE;
397         // no limits for local player
398         if (client->netconnection && LHNETADDRESS_GetAddressType(&client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP)
399                 client->rate = 1000000000;
400         client->connecttime = realtime;
401
402         if (sv.loadgame)
403                 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
404         else
405         {
406                 // call the progs to get default spawn parms for the new client
407                 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
408                 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
409                         client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
410         }
411
412         // don't call SendServerinfo for a fresh botclient because its fields have
413         // not been set up by the qc yet
414         if (client->netconnection)
415                 SV_SendServerinfo (client);
416         else
417                 client->spawned = true;
418 }
419
420
421 /*
422 ===============================================================================
423
424 FRAME UPDATES
425
426 ===============================================================================
427 */
428
429 /*
430 ==================
431 SV_ClearDatagram
432
433 ==================
434 */
435 void SV_ClearDatagram (void)
436 {
437         SZ_Clear (&sv.datagram);
438 }
439
440 /*
441 =============================================================================
442
443 The PVS must include a small area around the client to allow head bobbing
444 or other small motion on the client side.  Otherwise, a bob might cause an
445 entity that should be visible to not show up, especially when the bob
446 crosses a waterline.
447
448 =============================================================================
449 */
450
451 int sv_writeentitiestoclient_pvsbytes;
452 qbyte sv_writeentitiestoclient_pvs[MAX_MAP_LEAFS/8];
453
454 static int numsendentities;
455 static entity_state_t sendentities[MAX_EDICTS];
456 static entity_state_t *sendentitiesindex[MAX_EDICTS];
457
458 void SV_PrepareEntitiesForSending(void)
459 {
460         int e, i;
461         float f;
462         edict_t *ent;
463         entity_state_t cs;
464         // send all entities that touch the pvs
465         numsendentities = 0;
466         sendentitiesindex[0] = NULL;
467         for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
468         {
469                 sendentitiesindex[e] = NULL;
470                 if (ent->e->free)
471                         continue;
472
473                 cs = defaultstate;
474                 cs.active = true;
475                 cs.number = e;
476                 VectorCopy(ent->v->origin, cs.origin);
477                 VectorCopy(ent->v->angles, cs.angles);
478                 cs.flags = 0;
479                 cs.effects = (int)ent->v->effects;
480                 cs.colormap = (qbyte)ent->v->colormap;
481                 cs.skin = (qbyte)ent->v->skin;
482                 cs.frame = (qbyte)ent->v->frame;
483                 cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
484                 cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
485                 cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
486                 cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
487                 cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
488                 cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
489                 i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
490                 cs.glowsize = (qbyte)bound(0, i, 255);
491                 if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
492                         cs.flags |= RENDER_GLOWTRAIL;
493
494                 cs.modelindex = 0;
495                 i = (int)ent->v->modelindex;
496                 if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
497                         cs.modelindex = i;
498
499                 cs.alpha = 255;
500                 f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
501                 if (f)
502                 {
503                         i = (int)f;
504                         cs.alpha = (qbyte)bound(0, i, 255);
505                 }
506                 // halflife
507                 f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
508                 if (f)
509                 {
510                         i = (int)f;
511                         cs.alpha = (qbyte)bound(0, i, 255);
512                 }
513
514                 cs.scale = 16;
515                 f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
516                 if (f)
517                 {
518                         i = (int)f;
519                         cs.scale = (qbyte)bound(0, i, 255);
520                 }
521
522                 cs.glowcolor = 254;
523                 f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
524                 if (f)
525                         cs.glowcolor = (int)f;
526
527                 if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
528                         cs.effects |= EF_FULLBRIGHT;
529
530                 if (ent->v->movetype == MOVETYPE_STEP)
531                         cs.flags |= RENDER_STEP;
532                 if ((cs.effects & EF_LOWPRECISION) && cs.origin[0] >= -32768 && cs.origin[1] >= -32768 && cs.origin[2] >= -32768 && cs.origin[0] <= 32767 && cs.origin[1] <= 32767 && cs.origin[2] <= 32767)
533                         cs.flags |= RENDER_LOWPRECISION;
534                 if (ent->v->colormap >= 1024)
535                         cs.flags |= RENDER_COLORMAPPED;
536                 if (cs.viewmodelforclient)
537                         cs.flags |= RENDER_VIEWMODEL; // show relative to the view
538
539                 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[0]*256;
540                 cs.light[0] = (unsigned short)bound(0, f, 65535);
541                 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[1]*256;
542                 cs.light[1] = (unsigned short)bound(0, f, 65535);
543                 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[2]*256;
544                 cs.light[2] = (unsigned short)bound(0, f, 65535);
545                 f = GETEDICTFIELDVALUE(ent, eval_light_lev)->_float;
546                 cs.light[3] = (unsigned short)bound(0, f, 65535);
547                 cs.lightstyle = (qbyte)GETEDICTFIELDVALUE(ent, eval_style)->_float;
548                 cs.lightpflags = (qbyte)GETEDICTFIELDVALUE(ent, eval_pflags)->_float;
549
550                 if (gamemode == GAME_TENEBRAE)
551                 {
552                         // tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
553                         if (cs.effects & 16)
554                         {
555                                 cs.effects &= ~16;
556                                 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
557                         }
558                         // tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
559                         if (cs.effects & 32)
560                         {
561                                 cs.effects &= ~32;
562                                 cs.light[0] = 0.2;
563                                 cs.light[1] = 1;
564                                 cs.light[2] = 0.2;
565                                 cs.light[3] = 200;
566                                 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
567                         }
568                 }
569
570                 cs.specialvisibilityradius = 0;
571                 if (cs.lightpflags & PFLAGS_FULLDYNAMIC)
572                         cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.light[3]);
573                 if (cs.glowsize)
574                         cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
575                 if (cs.flags & RENDER_GLOWTRAIL)
576                         cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
577                 if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
578                 {
579                         if (cs.effects & EF_BRIGHTFIELD)
580                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
581                         if (cs.effects & EF_MUZZLEFLASH)
582                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
583                         if (cs.effects & EF_BRIGHTLIGHT)
584                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
585                         if (cs.effects & EF_DIMLIGHT)
586                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
587                         if (cs.effects & EF_RED)
588                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
589                         if (cs.effects & EF_BLUE)
590                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
591                         if (cs.effects & EF_FLAME)
592                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
593                         if (cs.effects & EF_STARDUST)
594                                 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
595                 }
596
597                 if (numsendentities >= MAX_EDICTS)
598                         continue;
599                 // we can omit invisible entities with no effects that are not clients
600                 // LordHavoc: this could kill tags attached to an invisible entity, I
601                 // just hope we never have to support that case
602                 if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
603                         continue;
604                 sendentitiesindex[e] = sendentities + numsendentities;
605                 sendentities[numsendentities++] = cs;
606         }
607 }
608
609 static int sententitiesmark = 0;
610 static int sententities[MAX_EDICTS];
611 static int sententitiesconsideration[MAX_EDICTS];
612 static int sv_writeentitiestoclient_culled_pvs;
613 static int sv_writeentitiestoclient_culled_trace;
614 static int sv_writeentitiestoclient_visibleentities;
615 static int sv_writeentitiestoclient_totalentities;
616 //static entity_frame_t sv_writeentitiestoclient_entityframe;
617 static int sv_writeentitiestoclient_clentnum;
618 static vec3_t sv_writeentitiestoclient_testeye;
619 static client_t *sv_writeentitiestoclient_client;
620
621 void SV_MarkWriteEntityStateToClient(entity_state_t *s)
622 {
623         int isbmodel;
624         vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
625         model_t *model;
626         trace_t trace;
627         if (sententitiesconsideration[s->number] == sententitiesmark)
628                 return;
629         sententitiesconsideration[s->number] = sententitiesmark;
630         // viewmodels don't have visibility checking
631         if (s->viewmodelforclient)
632         {
633                 if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
634                         return;
635         }
636         // never reject player
637         else if (s->number != sv_writeentitiestoclient_clentnum)
638         {
639                 // check various rejection conditions
640                 if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
641                         return;
642                 if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
643                         return;
644                 if (s->effects & EF_NODRAW)
645                         return;
646                 // LordHavoc: only send entities with a model or important effects
647                 if (!s->modelindex && s->specialvisibilityradius == 0)
648                         return;
649                 if (s->tagentity)
650                 {
651                         // tag attached entities simply check their parent
652                         if (!sendentitiesindex[s->tagentity])
653                                 return;
654                         SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
655                         if (sententities[s->tagentity] != sententitiesmark)
656                                 return;
657                 }
658                 // always send world submodels, they don't generate much traffic
659                 // except in PROTOCOL_QUAKE where they hog bandwidth like crazy
660                 else if ((!(isbmodel = (model = sv.models[s->modelindex]) != NULL && model->name[0] == '*') && !(s->effects & EF_NODEPTHTEST)) || sv.protocol == PROTOCOL_QUAKE)
661                 {
662                         Mod_CheckLoaded(model);
663                         // entity has survived every check so far, check if visible
664                         // enlarged box to account for prediction (not that there is
665                         // any currently, but still helps the 'run into a room and
666                         // watch items pop up' problem)
667                         entmins[0] = s->origin[0] - 32.0f;
668                         entmins[1] = s->origin[1] - 32.0f;
669                         entmins[2] = s->origin[2] - 32.0f;
670                         entmaxs[0] = s->origin[0] + 32.0f;
671                         entmaxs[1] = s->origin[1] + 32.0f;
672                         entmaxs[2] = s->origin[2] + 32.0f;
673                         // using the model's bounding box to ensure things are visible regardless of their physics box
674                         if (model)
675                         {
676                                 if (s->angles[0] || s->angles[2]) // pitch and roll
677                                 {
678                                         VectorAdd(entmins, model->rotatedmins, entmins);
679                                         VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
680                                 }
681                                 else if (s->angles[1])
682                                 {
683                                         VectorAdd(entmins, model->yawmins, entmins);
684                                         VectorAdd(entmaxs, model->yawmaxs, entmaxs);
685                                 }
686                                 else
687                                 {
688                                         VectorAdd(entmins, model->normalmins, entmins);
689                                         VectorAdd(entmaxs, model->normalmaxs, entmaxs);
690                                 }
691                         }
692                         lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
693                         lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
694                         lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
695                         lightmaxs[0] = max(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
696                         lightmaxs[1] = max(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
697                         lightmaxs[2] = max(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
698                         sv_writeentitiestoclient_totalentities++;
699                         // if not touching a visible leaf
700                         if (sv_cullentities_pvs.integer && sv_writeentitiestoclient_pvsbytes && sv.worldmodel && sv.worldmodel->brush.BoxTouchingPVS && !sv.worldmodel->brush.BoxTouchingPVS(sv.worldmodel, sv_writeentitiestoclient_pvs, lightmins, lightmaxs))
701                         {
702                                 sv_writeentitiestoclient_culled_pvs++;
703                                 return;
704                         }
705                         // or not seen by random tracelines
706                         if (sv_cullentities_trace.integer && !isbmodel)
707                         {
708                                 // LordHavoc: test center first
709                                 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
710                                 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
711                                 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
712                                 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
713                                 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
714                                         sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
715                                 else
716                                 {
717                                         // LordHavoc: test random offsets, to maximize chance of detection
718                                         testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
719                                         testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
720                                         testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
721                                         sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
722                                         if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
723                                                 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
724                                         else
725                                         {
726                                                 if (s->specialvisibilityradius)
727                                                 {
728                                                         // LordHavoc: test random offsets, to maximize chance of detection
729                                                         testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
730                                                         testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
731                                                         testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
732                                                         sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
733                                                         if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
734                                                                 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
735                                                 }
736                                         }
737                                 }
738                                 if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
739                                 {
740                                         sv_writeentitiestoclient_culled_trace++;
741                                         return;
742                                 }
743                         }
744                         sv_writeentitiestoclient_visibleentities++;
745                 }
746         }
747         // this just marks it for sending
748         // FIXME: it would be more efficient to send here, but the entity
749         // compressor isn't that flexible
750         sententities[s->number] = sententitiesmark;
751 }
752
753 entity_state_t sendstates[MAX_EDICTS]; 
754
755 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg)
756 {
757         int i, numsendstates;
758         entity_state_t *s;
759
760         // if there isn't enough space to accomplish anything, skip it
761         if (msg->cursize + 25 > msg->maxsize)
762                 return;
763
764         sv_writeentitiestoclient_client = client;
765
766         sv_writeentitiestoclient_culled_pvs = 0;
767         sv_writeentitiestoclient_culled_trace = 0;
768         sv_writeentitiestoclient_visibleentities = 0;
769         sv_writeentitiestoclient_totalentities = 0;
770
771         Mod_CheckLoaded(sv.worldmodel);
772
773 // find the client's PVS
774         // the real place being tested from
775         VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
776         sv_writeentitiestoclient_pvsbytes = 0;
777         if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
778                 sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
779
780         sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
781
782         sententitiesmark++;
783
784         for (i = 0;i < numsendentities;i++)
785                 SV_MarkWriteEntityStateToClient(sendentities + i);
786
787         numsendstates = 0;
788         for (i = 0;i < numsendentities;i++)
789         {
790                 if (sententities[sendentities[i].number] == sententitiesmark)
791                 {
792                         s = &sendstates[numsendstates++];
793                         *s = sendentities[i];
794                         if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
795                                 s->flags |= RENDER_EXTERIORMODEL;
796                 }
797         }
798
799         if (sv_cullentities_stats.integer)
800                 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d trace\n", client->name, sv_writeentitiestoclient_totalentities, sv_writeentitiestoclient_visibleentities, sv_writeentitiestoclient_culled_pvs + sv_writeentitiestoclient_culled_trace, sv_writeentitiestoclient_culled_pvs, sv_writeentitiestoclient_culled_trace);
801
802         if (client->entitydatabase5)
803                 EntityFrame5_WriteFrame(msg, client->entitydatabase5, numsendstates, sendstates, client - svs.clients + 1);
804         else if (client->entitydatabase4)
805                 EntityFrame4_WriteFrame(msg, client->entitydatabase4, numsendstates, sendstates);
806         else if (client->entitydatabase)
807                 EntityFrame_WriteFrame(msg, client->entitydatabase, numsendstates, sendstates, client - svs.clients + 1);
808         else
809                 EntityFrameQuake_WriteFrame(msg, numsendstates, sendstates);
810 }
811
812 /*
813 =============
814 SV_CleanupEnts
815
816 =============
817 */
818 void SV_CleanupEnts (void)
819 {
820         int             e;
821         edict_t *ent;
822
823         ent = NEXT_EDICT(sv.edicts);
824         for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
825                 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
826 }
827
828 /*
829 ==================
830 SV_WriteClientdataToMessage
831
832 ==================
833 */
834 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
835 {
836         int             bits;
837         int             i;
838         edict_t *other;
839         int             items;
840         eval_t  *val;
841         vec3_t  punchvector;
842         qbyte   viewzoom;
843
844 //
845 // send a damage message
846 //
847         if (ent->v->dmg_take || ent->v->dmg_save)
848         {
849                 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
850                 MSG_WriteByte (msg, svc_damage);
851                 MSG_WriteByte (msg, ent->v->dmg_save);
852                 MSG_WriteByte (msg, ent->v->dmg_take);
853                 for (i=0 ; i<3 ; i++)
854                         MSG_WriteCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]), sv.protocol);
855
856                 ent->v->dmg_take = 0;
857                 ent->v->dmg_save = 0;
858         }
859
860 //
861 // send the current viewpos offset from the view entity
862 //
863         SV_SetIdealPitch ();            // how much to look up / down ideally
864
865 // a fixangle might get lost in a dropped packet.  Oh well.
866         if ( ent->v->fixangle )
867         {
868                 MSG_WriteByte (msg, svc_setangle);
869                 for (i=0 ; i < 3 ; i++)
870                         MSG_WriteAngle (msg, ent->v->angles[i], sv.protocol);
871                 ent->v->fixangle = 0;
872         }
873
874         bits = 0;
875
876         if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
877                 bits |= SU_VIEWHEIGHT;
878
879         if (ent->v->idealpitch)
880                 bits |= SU_IDEALPITCH;
881
882 // stuff the sigil bits into the high bits of items for sbar, or else
883 // mix in items2
884         val = GETEDICTFIELDVALUE(ent, eval_items2);
885
886         if (val)
887                 items = (int)ent->v->items | ((int)val->_float << 23);
888         else
889                 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
890
891         bits |= SU_ITEMS;
892
893         if ( (int)ent->v->flags & FL_ONGROUND)
894                 bits |= SU_ONGROUND;
895
896         if ( ent->v->waterlevel >= 2)
897                 bits |= SU_INWATER;
898
899         // PROTOCOL_DARKPLACES
900         VectorClear(punchvector);
901         if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
902                 VectorCopy(val->vector, punchvector);
903
904         i = 255;
905         if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
906         {
907                 i = val->_float * 255.0f;
908                 if (i == 0)
909                         i = 255;
910                 else
911                         i = bound(0, i, 65535);
912         }
913         viewzoom = i;
914
915         // FIXME: which protocols support this?  does PROTOCOL_DARKPLACES3 support viewzoom?
916         if (sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
917                 if (viewzoom != 255)
918                         bits |= SU_VIEWZOOM;
919
920         for (i=0 ; i<3 ; i++)
921         {
922                 if (ent->v->punchangle[i])
923                         bits |= (SU_PUNCH1<<i);
924                 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
925                         if (punchvector[i])
926                                 bits |= (SU_PUNCHVEC1<<i);
927                 if (ent->v->velocity[i])
928                         bits |= (SU_VELOCITY1<<i);
929         }
930
931         if (ent->v->weaponframe)
932                 bits |= SU_WEAPONFRAME;
933
934         if (ent->v->armorvalue)
935                 bits |= SU_ARMOR;
936
937         bits |= SU_WEAPON;
938
939         if (bits >= 65536)
940                 bits |= SU_EXTEND1;
941         if (bits >= 16777216)
942                 bits |= SU_EXTEND2;
943
944 // send the data
945
946         MSG_WriteByte (msg, svc_clientdata);
947         MSG_WriteShort (msg, bits);
948         if (bits & SU_EXTEND1)
949                 MSG_WriteByte(msg, bits >> 16);
950         if (bits & SU_EXTEND2)
951                 MSG_WriteByte(msg, bits >> 24);
952
953         if (bits & SU_VIEWHEIGHT)
954                 MSG_WriteChar (msg, ent->v->view_ofs[2]);
955
956         if (bits & SU_IDEALPITCH)
957                 MSG_WriteChar (msg, ent->v->idealpitch);
958
959         for (i=0 ; i<3 ; i++)
960         {
961                 if (bits & (SU_PUNCH1<<i))
962                 {
963                         if (sv.protocol == PROTOCOL_QUAKE)
964                                 MSG_WriteChar(msg, ent->v->punchangle[i]);
965                         else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
966                                 MSG_WriteAngle16i(msg, ent->v->punchangle[i]);
967                 }
968                 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
969                 {
970                         if (bits & (SU_PUNCHVEC1<<i))
971                         {
972                                 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
973                                         MSG_WriteCoord16i(msg, punchvector[i]);
974                                 else if (sv.protocol == PROTOCOL_DARKPLACES5)
975                                         MSG_WriteCoord32f(msg, punchvector[i]);
976                         }
977                 }
978                 if (bits & (SU_VELOCITY1<<i))
979                 {
980                         if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
981                                 MSG_WriteChar(msg, ent->v->velocity[i] * (1.0f / 16.0f));
982                         else if (sv.protocol == PROTOCOL_DARKPLACES5)
983                                 MSG_WriteCoord32f(msg, ent->v->velocity[i]);
984                 }
985         }
986
987 // [always sent]        if (bits & SU_ITEMS)
988         MSG_WriteLong (msg, items);
989
990         if (sv.protocol == PROTOCOL_DARKPLACES5)
991         {
992                 if (bits & SU_WEAPONFRAME)
993                         MSG_WriteShort (msg, ent->v->weaponframe);
994                 if (bits & SU_ARMOR)
995                         MSG_WriteShort (msg, ent->v->armorvalue);
996                 if (bits & SU_WEAPON)
997                 {
998                         i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
999                         if (i < 0)
1000                         {
1001                                 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
1002                                 i = 0;
1003                         }
1004                         MSG_WriteShort (msg, i);
1005                 }
1006
1007                 MSG_WriteShort (msg, ent->v->health);
1008                 MSG_WriteShort (msg, ent->v->currentammo);
1009                 MSG_WriteShort (msg, ent->v->ammo_shells);
1010                 MSG_WriteShort (msg, ent->v->ammo_nails);
1011                 MSG_WriteShort (msg, ent->v->ammo_rockets);
1012                 MSG_WriteShort (msg, ent->v->ammo_cells);
1013
1014                 MSG_WriteShort (msg, ent->v->weapon);
1015         
1016                 if (bits & SU_VIEWZOOM)
1017                         MSG_WriteShort (msg, viewzoom);
1018         }
1019         else
1020         {
1021                 if (bits & SU_WEAPONFRAME)
1022                         MSG_WriteByte (msg, ent->v->weaponframe);
1023                 if (bits & SU_ARMOR)
1024                         MSG_WriteByte (msg, ent->v->armorvalue);
1025                 if (bits & SU_WEAPON)
1026                 {
1027                         i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
1028                         if (i < 0)
1029                         {
1030                                 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
1031                                 i = 0;
1032                         }
1033                         MSG_WriteByte (msg, i);
1034                 }
1035
1036                 MSG_WriteShort (msg, ent->v->health);
1037                 MSG_WriteByte (msg, ent->v->currentammo);
1038                 MSG_WriteByte (msg, ent->v->ammo_shells);
1039                 MSG_WriteByte (msg, ent->v->ammo_nails);
1040                 MSG_WriteByte (msg, ent->v->ammo_rockets);
1041                 MSG_WriteByte (msg, ent->v->ammo_cells);
1042
1043                 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1044                 {
1045                         for(i=0;i<32;i++)
1046                         {
1047                                 if ( ((int)ent->v->weapon) & (1<<i) )
1048                                 {
1049                                         MSG_WriteByte (msg, i);
1050                                         break;
1051                                 }
1052                         }
1053                 }
1054                 else
1055                 {
1056                         MSG_WriteByte (msg, ent->v->weapon);
1057                 }
1058         
1059                 if (bits & SU_VIEWZOOM)
1060                 {
1061                         if (sv.protocol == PROTOCOL_DARKPLACES4)
1062                         {
1063                                 viewzoom = min(viewzoom, 255);
1064                                 MSG_WriteByte (msg, viewzoom);
1065                         }
1066                         else if (sv.protocol == PROTOCOL_DARKPLACES5)
1067                                 MSG_WriteShort (msg, viewzoom);
1068                 }
1069         }
1070 }
1071
1072 /*
1073 =======================
1074 SV_SendClientDatagram
1075 =======================
1076 */
1077 static qbyte sv_sendclientdatagram_buf[NET_MAXMESSAGE]; // FIXME?
1078 qboolean SV_SendClientDatagram (client_t *client)
1079 {
1080         int rate, maxrate, maxsize, maxsize2;
1081         sizebuf_t msg;
1082
1083         if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
1084         {
1085                 // for good singleplayer, send huge packets
1086                 maxsize = sizeof(sv_sendclientdatagram_buf);
1087                 maxsize2 = sizeof(sv_sendclientdatagram_buf);
1088         }
1089         else if (sv.protocol == PROTOCOL_DARKPLACES5)
1090         {
1091                 // PROTOCOL_DARKPLACES5 supports packet size limiting of updates
1092                 maxrate = bound(NET_MINRATE, sv_maxrate.integer, NET_MAXRATE);
1093                 if (sv_maxrate.integer != maxrate)
1094                         Cvar_SetValueQuick(&sv_maxrate, maxrate);
1095
1096                 rate = bound(NET_MINRATE, client->rate, maxrate);
1097                 rate = (int)(client->rate * sys_ticrate.value);
1098                 maxsize = bound(100, rate, 1400);
1099                 maxsize2 = 1400;
1100         }
1101         else
1102         {
1103                 // no rate limiting support on older protocols because dp protocols
1104                 // 1-4 kick the client off if they overflow, and quake protocol shows
1105                 // less than the full entity set if rate limited
1106                 maxsize = 1400;
1107                 maxsize2 = 1400;
1108         }
1109
1110         msg.data = sv_sendclientdatagram_buf;
1111         msg.maxsize = maxsize;
1112         msg.cursize = 0;
1113
1114         MSG_WriteByte (&msg, svc_time);
1115         MSG_WriteFloat (&msg, sv.time);
1116
1117         // add the client specific data to the datagram
1118         SV_WriteClientdataToMessage (client->edict, &msg);
1119
1120         SV_WriteEntitiesToClient (client, client->edict, &msg);
1121
1122         // expand packet size to allow effects to go over the rate limit
1123         // (dropping them is FAR too ugly)
1124         msg.maxsize = maxsize2;
1125
1126         // copy the server datagram if there is space
1127         // FIXME: put in delayed queue of effects to send
1128         if (sv.datagram.cursize > 0 && msg.cursize + sv.datagram.cursize <= msg.maxsize)
1129                 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1130
1131 // send the datagram
1132         if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1133         {
1134                 SV_DropClient (true);// if the message couldn't send, kick off
1135                 return false;
1136         }
1137
1138         return true;
1139 }
1140
1141 /*
1142 =======================
1143 SV_UpdateToReliableMessages
1144 =======================
1145 */
1146 void SV_UpdateToReliableMessages (void)
1147 {
1148         int i, j;
1149         client_t *client;
1150         eval_t *val;
1151         char *name;
1152
1153 // check for changes to be sent over the reliable streams
1154         for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1155         {
1156                 // update the host_client fields we care about according to the entity fields
1157                 host_client->edict = EDICT_NUM(i+1);
1158
1159                 // DP_SV_CLIENTNAME
1160                 name = PR_GetString(host_client->edict->v->netname);
1161                 if (name == NULL)
1162                         name = "";
1163                 // always point the string back at host_client->name to keep it safe
1164                 strlcpy (host_client->name, name, sizeof (host_client->name));
1165                 host_client->edict->v->netname = PR_SetString(host_client->name);
1166                 if (strcmp(host_client->old_name, host_client->name))
1167                 {
1168                         if (host_client->spawned)
1169                                 SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
1170                         strcpy(host_client->old_name, host_client->name);
1171                         // send notification to all clients
1172                         MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
1173                         MSG_WriteByte (&sv.reliable_datagram, i);
1174                         MSG_WriteString (&sv.reliable_datagram, host_client->name);
1175                 }
1176
1177                 // DP_SV_CLIENTCOLORS
1178                 // this is always found (since it's added by the progs loader)
1179                 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_clientcolors)))
1180                         host_client->colors = (int)val->_float;
1181                 if (host_client->old_colors != host_client->colors)
1182                 {
1183                         host_client->old_colors = host_client->colors;
1184                         // send notification to all clients
1185                         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1186                         MSG_WriteByte (&sv.reliable_datagram, i);
1187                         MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1188                 }
1189
1190                 // frags
1191                 host_client->frags = (int)host_client->edict->v->frags;
1192                 if (host_client->old_frags != host_client->frags)
1193                 {
1194                         host_client->old_frags = host_client->frags;
1195                         // send notification to all clients
1196                         MSG_WriteByte (&sv.reliable_datagram, svc_updatefrags);
1197                         MSG_WriteByte (&sv.reliable_datagram, i);
1198                         MSG_WriteShort (&sv.reliable_datagram, host_client->frags);
1199                 }
1200         }
1201
1202         for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
1203                 if (client->netconnection)
1204                         SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1205
1206         SZ_Clear (&sv.reliable_datagram);
1207 }
1208
1209
1210 /*
1211 =======================
1212 SV_SendNop
1213
1214 Send a nop message without trashing or sending the accumulated client
1215 message buffer
1216 =======================
1217 */
1218 void SV_SendNop (client_t *client)
1219 {
1220         sizebuf_t       msg;
1221         qbyte           buf[4];
1222
1223         msg.data = buf;
1224         msg.maxsize = sizeof(buf);
1225         msg.cursize = 0;
1226
1227         MSG_WriteChar (&msg, svc_nop);
1228
1229         if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1230                 SV_DropClient (true);   // if the message couldn't send, kick off
1231         client->last_message = realtime;
1232 }
1233
1234 /*
1235 =======================
1236 SV_SendClientMessages
1237 =======================
1238 */
1239 void SV_SendClientMessages (void)
1240 {
1241         int i, prepared = false;
1242
1243 // update frags, names, etc
1244         SV_UpdateToReliableMessages();
1245
1246 // build individual updates
1247         for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1248         {
1249                 if (!host_client->active)
1250                         continue;
1251                 if (!host_client->netconnection)
1252                 {
1253                         SZ_Clear(&host_client->message);
1254                         continue;
1255                 }
1256
1257                 if (host_client->deadsocket || host_client->message.overflowed)
1258                 {
1259                         SV_DropClient (true);   // if the message couldn't send, kick off
1260                         continue;
1261                 }
1262
1263                 if (host_client->spawned)
1264                 {
1265                         if (!prepared)
1266                         {
1267                                 prepared = true;
1268                                 // only prepare entities once per frame
1269                                 SV_PrepareEntitiesForSending();
1270                         }
1271                         if (!SV_SendClientDatagram (host_client))
1272                                 continue;
1273                 }
1274                 else
1275                 {
1276                 // the player isn't totally in the game yet
1277                 // send small keepalive messages if too much time has passed
1278                 // send a full message when the next signon stage has been requested
1279                 // some other message data (name changes, etc) may accumulate
1280                 // between signon stages
1281                         if (!host_client->sendsignon)
1282                         {
1283                                 if (realtime - host_client->last_message > 5)
1284                                         SV_SendNop (host_client);
1285                                 continue;       // don't send out non-signon messages
1286                         }
1287                 }
1288
1289                 if (host_client->message.cursize || host_client->dropasap)
1290                 {
1291                         if (!NetConn_CanSendMessage (host_client->netconnection))
1292                                 continue;
1293
1294                         if (host_client->dropasap)
1295                                 SV_DropClient (false);  // went to another level
1296                         else
1297                         {
1298                                 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1299                                         SV_DropClient (true);   // if the message couldn't send, kick off
1300                                 SZ_Clear (&host_client->message);
1301                                 host_client->last_message = realtime;
1302                                 host_client->sendsignon = false;
1303                         }
1304                 }
1305         }
1306
1307 // clear muzzle flashes
1308         SV_CleanupEnts();
1309 }
1310
1311
1312 /*
1313 ==============================================================================
1314
1315 SERVER SPAWNING
1316
1317 ==============================================================================
1318 */
1319
1320 /*
1321 ================
1322 SV_ModelIndex
1323
1324 ================
1325 */
1326 int SV_ModelIndex (const char *name)
1327 {
1328         int i;
1329
1330         if (!name || !name[0])
1331                 return 0;
1332
1333         for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1334                 if (!strcmp(sv.model_precache[i], name))
1335                         return i;
1336         if (i==MAX_MODELS || !sv.model_precache[i])
1337         {
1338                 Con_DPrintf ("SV_ModelIndex: model %s not precached", name);
1339                 return -1;
1340         }
1341         return i;
1342 }
1343
1344 /*
1345 ================
1346 SV_CreateBaseline
1347
1348 ================
1349 */
1350 void SV_CreateBaseline (void)
1351 {
1352         int i, entnum, large;
1353         edict_t *svent;
1354
1355         // LordHavoc: clear *all* states (note just active ones)
1356         for (entnum = 0;entnum < sv.max_edicts;entnum++)
1357         {
1358                 // get the current server version
1359                 svent = EDICT_NUM(entnum);
1360
1361                 // LordHavoc: always clear state values, whether the entity is in use or not
1362                 svent->e->baseline = defaultstate;
1363
1364                 if (svent->e->free)
1365                         continue;
1366                 if (entnum > svs.maxclients && !svent->v->modelindex)
1367                         continue;
1368
1369                 // create entity baseline
1370                 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1371                 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1372                 svent->e->baseline.frame = svent->v->frame;
1373                 svent->e->baseline.skin = svent->v->skin;
1374                 if (entnum > 0 && entnum <= svs.maxclients)
1375                 {
1376                         svent->e->baseline.colormap = entnum;
1377                         i = SV_ModelIndex("progs/player.mdl");
1378                         if (i < 0)
1379                                 i = 0;
1380                         svent->e->baseline.modelindex = i;
1381                 }
1382                 else
1383                 {
1384                         svent->e->baseline.colormap = 0;
1385                         svent->e->baseline.modelindex = svent->v->modelindex;
1386                 }
1387
1388                 large = false;
1389                 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1390                         large = true;
1391
1392                 // add to the message
1393                 if (large)
1394                         MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1395                 else
1396                         MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1397                 MSG_WriteShort (&sv.signon, entnum);
1398
1399                 if (large)
1400                 {
1401                         MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1402                         MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1403                 }
1404                 else
1405                 {
1406                         MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1407                         MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1408                 }
1409                 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1410                 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1411                 for (i=0 ; i<3 ; i++)
1412                 {
1413                         MSG_WriteCoord(&sv.signon, svent->e->baseline.origin[i], sv.protocol);
1414                         MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i], sv.protocol);
1415                 }
1416         }
1417 }
1418
1419
1420 /*
1421 ================
1422 SV_SendReconnect
1423
1424 Tell all the clients that the server is changing levels
1425 ================
1426 */
1427 void SV_SendReconnect (void)
1428 {
1429         char    data[128];
1430         sizebuf_t       msg;
1431
1432         msg.data = data;
1433         msg.cursize = 0;
1434         msg.maxsize = sizeof(data);
1435
1436         MSG_WriteChar (&msg, svc_stufftext);
1437         MSG_WriteString (&msg, "reconnect\n");
1438         NetConn_SendToAll (&msg, 5);
1439
1440         if (cls.state != ca_dedicated)
1441                 Cmd_ExecuteString ("reconnect\n", src_command);
1442 }
1443
1444
1445 /*
1446 ================
1447 SV_SaveSpawnparms
1448
1449 Grabs the current state of each client for saving across the
1450 transition to another level
1451 ================
1452 */
1453 void SV_SaveSpawnparms (void)
1454 {
1455         int             i, j;
1456
1457         svs.serverflags = pr_global_struct->serverflags;
1458
1459         for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1460         {
1461                 if (!host_client->active)
1462                         continue;
1463
1464         // call the progs to get default spawn parms for the new client
1465                 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1466                 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1467                 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1468                         host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1469         }
1470 }
1471
1472 void SV_IncreaseEdicts(void)
1473 {
1474         int i;
1475         edict_t *ent;
1476         int oldmax_edicts = sv.max_edicts;
1477         void *oldedictsengineprivate = sv.edictsengineprivate;
1478         void *oldedictsfields = sv.edictsfields;
1479         void *oldmoved_edicts = sv.moved_edicts;
1480
1481         if (sv.max_edicts >= MAX_EDICTS)
1482                 return;
1483
1484         // links don't survive the transition, so unlink everything
1485         for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1486         {
1487                 if (!ent->e->free)
1488                         SV_UnlinkEdict(sv.edicts + i);
1489                 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1490         }
1491         SV_ClearWorld();
1492
1493         sv.max_edicts   = min(sv.max_edicts + 256, MAX_EDICTS);
1494         sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1495         sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1496         sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1497
1498         memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1499         memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1500
1501         for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1502         {
1503                 ent->e = sv.edictsengineprivate + i;
1504                 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1505                 // link every entity except world
1506                 if (!ent->e->free)
1507                         SV_LinkEdict(ent, false);
1508         }
1509
1510         Mem_Free(oldedictsengineprivate);
1511         Mem_Free(oldedictsfields);
1512         Mem_Free(oldmoved_edicts);
1513 }
1514
1515 /*
1516 ================
1517 SV_SpawnServer
1518
1519 This is called at the start of each level
1520 ================
1521 */
1522 extern float            scr_centertime_off;
1523
1524 void SV_SpawnServer (const char *server)
1525 {
1526         edict_t *ent;
1527         int i;
1528         qbyte *entities;
1529         model_t *worldmodel;
1530         char modelname[sizeof(sv.modelname)];
1531
1532         Con_DPrintf("SpawnServer: %s\n", server);
1533
1534         snprintf (modelname, sizeof(modelname), "maps/%s.bsp", server);
1535         worldmodel = Mod_ForName(modelname, false, true, true);
1536         if (!worldmodel || !worldmodel->TraceBox)
1537         {
1538                 Con_Printf("Couldn't load map %s\n", modelname);
1539                 return;
1540         }
1541
1542         // let's not have any servers with no name
1543         if (hostname.string[0] == 0)
1544                 Cvar_Set ("hostname", "UNNAMED");
1545         scr_centertime_off = 0;
1546
1547         svs.changelevel_issued = false;         // now safe to issue another
1548
1549 //
1550 // tell all connected clients that we are going to a new level
1551 //
1552         if (sv.active)
1553                 SV_SendReconnect();
1554         else
1555         {
1556                 // make sure cvars have been checked before opening the ports
1557                 NetConn_ServerFrame();
1558                 NetConn_OpenServerPorts(true);
1559         }
1560
1561 //
1562 // make cvars consistant
1563 //
1564         if (coop.integer)
1565                 Cvar_SetValue ("deathmatch", 0);
1566         current_skill = bound(0, (int)(skill.value + 0.5), 3);
1567
1568         Cvar_SetValue ("skill", (float)current_skill);
1569
1570 //
1571 // set up the new server
1572 //
1573         Host_ClearMemory ();
1574
1575         memset (&sv, 0, sizeof(sv));
1576
1577         strlcpy (sv.name, server, sizeof (sv.name));
1578
1579         // FIXME: cvar
1580         if (!strcasecmp(sv_protocolname.string, "QUAKE"))
1581         {
1582                 sv.protocol = PROTOCOL_QUAKE;
1583                 sv.netquakecompatible = true;
1584         }
1585         else if (!strcasecmp(sv_protocolname.string, "QUAKEDP"))
1586         {
1587                 sv.protocol = PROTOCOL_QUAKE;
1588                 sv.netquakecompatible = false;
1589         }
1590         else if (!strcasecmp(sv_protocolname.string, "DARKPLACES1"))
1591         {
1592                 sv.protocol = PROTOCOL_DARKPLACES1;
1593                 sv.netquakecompatible = false;
1594         }
1595         else if (!strcasecmp(sv_protocolname.string, "DARKPLACES2"))
1596         {
1597                 sv.protocol = PROTOCOL_DARKPLACES2;
1598                 sv.netquakecompatible = false;
1599         }
1600         else if (!strcasecmp(sv_protocolname.string, "DARKPLACES3"))
1601         {
1602                 sv.protocol = PROTOCOL_DARKPLACES3;
1603                 sv.netquakecompatible = false;
1604         }
1605         else if (!strcasecmp(sv_protocolname.string, "DARKPLACES4"))
1606         {
1607                 sv.protocol = PROTOCOL_DARKPLACES4;
1608                 sv.netquakecompatible = false;
1609         }
1610         else if (!strcasecmp(sv_protocolname.string, "DARKPLACES5"))
1611         {
1612                 sv.protocol = PROTOCOL_DARKPLACES5;
1613                 sv.netquakecompatible = false;
1614         }
1615         else
1616         {
1617                 sv.protocol = PROTOCOL_DARKPLACES5;
1618                 sv.netquakecompatible = false;
1619                 Con_Printf("Unknown sv_protocolname \"%s\", valid values are QUAKE, QUAKEDP, DARKPLACES1, DARKPLACES2, DARKPLACES3, DARKPLACES4, DARKPLACES5, falling back to DARKPLACES5 protocol\n", sv_protocolname.string);
1620         }
1621
1622 // load progs to get entity field count
1623         PR_LoadProgs ();
1624
1625 // allocate server memory
1626         // start out with just enough room for clients and a reasonable estimate of entities
1627         sv.max_edicts = max(svs.maxclients + 1, 512);
1628         sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1629
1630         // clear the edict memory pool
1631         Mem_EmptyPool(sv_edicts_mempool);
1632         // edict_t structures (hidden from progs)
1633         sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1634         // engine private structures (hidden from progs)
1635         sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1636         // progs fields, often accessed by server
1637         sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1638         // used by PushMove to move back pushed entities
1639         sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1640         for (i = 0;i < sv.max_edicts;i++)
1641         {
1642                 ent = sv.edicts + i;
1643                 ent->e = sv.edictsengineprivate + i;
1644                 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1645         }
1646
1647         // fix up client->edict pointers for returning clients right away...
1648         for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1649                 host_client->edict = EDICT_NUM(i + 1);
1650
1651         sv.datagram.maxsize = sizeof(sv.datagram_buf);
1652         sv.datagram.cursize = 0;
1653         sv.datagram.data = sv.datagram_buf;
1654
1655         sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1656         sv.reliable_datagram.cursize = 0;
1657         sv.reliable_datagram.data = sv.reliable_datagram_buf;
1658
1659         sv.signon.maxsize = sizeof(sv.signon_buf);
1660         sv.signon.cursize = 0;
1661         sv.signon.data = sv.signon_buf;
1662
1663 // leave slots at start for clients only
1664         sv.num_edicts = svs.maxclients+1;
1665
1666         sv.state = ss_loading;
1667         sv.paused = false;
1668
1669         sv.time = 1.0;
1670
1671         Mod_ClearUsed();
1672         worldmodel->used = true;
1673
1674         strlcpy (sv.name, server, sizeof (sv.name));
1675         strcpy(sv.modelname, modelname);
1676         sv.worldmodel = worldmodel;
1677         sv.models[1] = sv.worldmodel;
1678
1679 //
1680 // clear world interaction links
1681 //
1682         SV_ClearWorld ();
1683
1684         sv.sound_precache[0] = "";
1685
1686         sv.model_precache[0] = "";
1687         sv.model_precache[1] = sv.modelname;
1688         for (i = 1;i < sv.worldmodel->brush.numsubmodels;i++)
1689         {
1690                 sv.model_precache[i+1] = localmodels[i];
1691                 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1692         }
1693
1694 //
1695 // load the rest of the entities
1696 //
1697         ent = EDICT_NUM(0);
1698         memset (ent->v, 0, progs->entityfields * 4);
1699         ent->e->free = false;
1700         ent->v->model = PR_SetString(sv.modelname);
1701         ent->v->modelindex = 1;         // world model
1702         ent->v->solid = SOLID_BSP;
1703         ent->v->movetype = MOVETYPE_PUSH;
1704
1705         if (coop.value)
1706                 pr_global_struct->coop = coop.integer;
1707         else
1708                 pr_global_struct->deathmatch = deathmatch.integer;
1709
1710         pr_global_struct->mapname = PR_SetString(sv.name);
1711
1712 // serverflags are for cross level information (sigils)
1713         pr_global_struct->serverflags = svs.serverflags;
1714
1715         // load replacement entity file if found
1716         entities = NULL;
1717         if (sv_entpatch.integer)
1718                 entities = FS_LoadFile(va("maps/%s.ent", sv.name), tempmempool, true);
1719         if (entities)
1720         {
1721                 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1722                 ED_LoadFromFile (entities);
1723                 Mem_Free(entities);
1724         }
1725         else
1726                 ED_LoadFromFile (sv.worldmodel->brush.entities);
1727
1728
1729         // LordHavoc: clear world angles (to fix e3m3.bsp)
1730         VectorClear(sv.edicts->v->angles);
1731
1732         sv.active = true;
1733
1734 // all setup is completed, any further precache statements are errors
1735         sv.state = ss_active;
1736
1737 // run two frames to allow everything to settle
1738         for (i = 0;i < 2;i++)
1739         {
1740                 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1741                 SV_Physics ();
1742         }
1743
1744         Mod_PurgeUnused();
1745
1746 // create a baseline for more efficient communications
1747         if (sv.protocol == PROTOCOL_QUAKE)
1748                 SV_CreateBaseline ();
1749
1750 // send serverinfo to all connected clients
1751         // (note this also handles botclients coming back from a level change)
1752         for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1753                 if (host_client->active)
1754                         SV_SendServerinfo(host_client);
1755
1756         Con_DPrint("Server spawned.\n");
1757         NetConn_Heartbeat (2);
1758 }
1759