]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_main.c
made id_pointer fields of rcachearrayrequest_t const (because they'll never be used...
[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 #include "portals.h"
24
25 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "0"}; // fast but loose
26 static cvar_t sv_cullentities_portal = {0, "sv_cullentities_portal", "0"}; // extremely accurate visibility checking, but too slow
27 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "1"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
28 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
29 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
30
31 server_t                sv;
32 server_static_t svs;
33
34 static char localmodels[MAX_MODELS][5];                 // inline model names for precache
35
36 mempool_t *sv_edicts_mempool = NULL;
37
38 //============================================================================
39
40 extern void SV_Phys_Init (void);
41 extern void SV_World_Init (void);
42
43 /*
44 ===============
45 SV_Init
46 ===============
47 */
48 void SV_Init (void)
49 {
50         int i;
51
52         Cvar_RegisterVariable (&sv_maxvelocity);
53         Cvar_RegisterVariable (&sv_gravity);
54         Cvar_RegisterVariable (&sv_friction);
55         Cvar_RegisterVariable (&sv_edgefriction);
56         Cvar_RegisterVariable (&sv_stopspeed);
57         Cvar_RegisterVariable (&sv_maxspeed);
58         Cvar_RegisterVariable (&sv_accelerate);
59         Cvar_RegisterVariable (&sv_idealpitchscale);
60         Cvar_RegisterVariable (&sv_aim);
61         Cvar_RegisterVariable (&sv_nostep);
62         Cvar_RegisterVariable (&sv_predict);
63         Cvar_RegisterVariable (&sv_deltacompress);
64         Cvar_RegisterVariable (&sv_cullentities_pvs);
65         Cvar_RegisterVariable (&sv_cullentities_portal);
66         Cvar_RegisterVariable (&sv_cullentities_trace);
67         Cvar_RegisterVariable (&sv_cullentities_stats);
68         Cvar_RegisterVariable (&sv_entpatch);
69
70         SV_Phys_Init();
71         SV_World_Init();
72
73         for (i = 0;i < MAX_MODELS;i++)
74                 sprintf (localmodels[i], "*%i", i);
75
76         sv_edicts_mempool = Mem_AllocPool("edicts");
77 }
78
79 /*
80 =============================================================================
81
82 EVENT MESSAGES
83
84 =============================================================================
85 */
86
87 /*
88 ==================
89 SV_StartParticle
90
91 Make sure the event gets sent to all clients
92 ==================
93 */
94 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
95 {
96         int             i, v;
97
98         if (sv.datagram.cursize > MAX_DATAGRAM-16)
99                 return;
100         MSG_WriteByte (&sv.datagram, svc_particle);
101         MSG_WriteDPCoord (&sv.datagram, org[0]);
102         MSG_WriteDPCoord (&sv.datagram, org[1]);
103         MSG_WriteDPCoord (&sv.datagram, org[2]);
104         for (i=0 ; i<3 ; i++)
105         {
106                 v = dir[i]*16;
107                 if (v > 127)
108                         v = 127;
109                 else if (v < -128)
110                         v = -128;
111                 MSG_WriteChar (&sv.datagram, v);
112         }
113         MSG_WriteByte (&sv.datagram, count);
114         MSG_WriteByte (&sv.datagram, color);
115 }
116
117 /*
118 ==================
119 SV_StartEffect
120
121 Make sure the event gets sent to all clients
122 ==================
123 */
124 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
125 {
126         if (sv.datagram.cursize > MAX_DATAGRAM-18)
127                 return;
128         if (modelindex >= 256 || startframe >= 256)
129         {
130                 MSG_WriteByte (&sv.datagram, svc_effect2);
131                 MSG_WriteDPCoord (&sv.datagram, org[0]);
132                 MSG_WriteDPCoord (&sv.datagram, org[1]);
133                 MSG_WriteDPCoord (&sv.datagram, org[2]);
134                 MSG_WriteShort (&sv.datagram, modelindex);
135                 MSG_WriteShort (&sv.datagram, startframe);
136                 MSG_WriteByte (&sv.datagram, framecount);
137                 MSG_WriteByte (&sv.datagram, framerate);
138         }
139         else
140         {
141                 MSG_WriteByte (&sv.datagram, svc_effect);
142                 MSG_WriteDPCoord (&sv.datagram, org[0]);
143                 MSG_WriteDPCoord (&sv.datagram, org[1]);
144                 MSG_WriteDPCoord (&sv.datagram, org[2]);
145                 MSG_WriteByte (&sv.datagram, modelindex);
146                 MSG_WriteByte (&sv.datagram, startframe);
147                 MSG_WriteByte (&sv.datagram, framecount);
148                 MSG_WriteByte (&sv.datagram, framerate);
149         }
150 }
151
152 /*
153 ==================
154 SV_StartSound
155
156 Each entity can have eight independant sound sources, like voice,
157 weapon, feet, etc.
158
159 Channel 0 is an auto-allocate channel, the others override anything
160 already running on that entity/channel pair.
161
162 An attenuation of 0 will play full volume everywhere in the level.
163 Larger attenuations will drop off.  (max 4 attenuation)
164
165 ==================
166 */
167 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
168     float attenuation)
169 {
170     int         sound_num;
171     int field_mask;
172     int                 i;
173         int                     ent;
174
175         if (volume < 0 || volume > 255)
176                 Host_Error ("SV_StartSound: volume = %i", volume);
177
178         if (attenuation < 0 || attenuation > 4)
179                 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
180
181         if (channel < 0 || channel > 7)
182                 Host_Error ("SV_StartSound: channel = %i", channel);
183
184         if (sv.datagram.cursize > MAX_DATAGRAM-16)
185                 return;
186
187 // find precache number for sound
188     for (sound_num=1 ; sound_num<MAX_SOUNDS
189         && sv.sound_precache[sound_num] ; sound_num++)
190         if (!strcmp(sample, sv.sound_precache[sound_num]))
191             break;
192
193     if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
194     {
195         Con_Printf ("SV_StartSound: %s not precached\n", sample);
196         return;
197     }
198
199         ent = NUM_FOR_EDICT(entity);
200
201         field_mask = 0;
202         if (volume != DEFAULT_SOUND_PACKET_VOLUME)
203                 field_mask |= SND_VOLUME;
204         if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
205                 field_mask |= SND_ATTENUATION;
206         if (ent >= 8192)
207                 field_mask |= SND_LARGEENTITY;
208         if (sound_num >= 256 || channel >= 8)
209                 field_mask |= SND_LARGESOUND;
210
211 // directed messages go only to the entity they are targeted on
212         MSG_WriteByte (&sv.datagram, svc_sound);
213         MSG_WriteByte (&sv.datagram, field_mask);
214         if (field_mask & SND_VOLUME)
215                 MSG_WriteByte (&sv.datagram, volume);
216         if (field_mask & SND_ATTENUATION)
217                 MSG_WriteByte (&sv.datagram, attenuation*64);
218         if (field_mask & SND_LARGEENTITY)
219         {
220                 MSG_WriteShort (&sv.datagram, ent);
221                 MSG_WriteByte (&sv.datagram, channel);
222         }
223         else
224                 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
225         if (field_mask & SND_LARGESOUND)
226                 MSG_WriteShort (&sv.datagram, sound_num);
227         else
228                 MSG_WriteByte (&sv.datagram, sound_num);
229         for (i = 0;i < 3;i++)
230                 MSG_WriteDPCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]));
231 }
232
233 /*
234 ==============================================================================
235
236 CLIENT SPAWNING
237
238 ==============================================================================
239 */
240
241 /*
242 ================
243 SV_SendServerinfo
244
245 Sends the first message from the server to a connected client.
246 This will be sent on the initial connection and upon each server load.
247 ================
248 */
249 void SV_SendServerinfo (client_t *client)
250 {
251         char                    **s;
252         char                    message[2048];
253
254         // LordHavoc: clear entityframe tracking
255         client->entityframenumber = 0;
256         EntityFrame_ClearDatabase(&client->entitydatabase);
257
258         MSG_WriteByte (&client->message, svc_print);
259         sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
260         MSG_WriteString (&client->message,message);
261
262         MSG_WriteByte (&client->message, svc_serverinfo);
263         MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
264         MSG_WriteByte (&client->message, svs.maxclients);
265
266         if (!coop.integer && deathmatch.integer)
267                 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
268         else
269                 MSG_WriteByte (&client->message, GAME_COOP);
270
271         sprintf (message, PR_GetString(sv.edicts->v->message));
272
273         MSG_WriteString (&client->message,message);
274
275         for (s = sv.model_precache+1 ; *s ; s++)
276                 MSG_WriteString (&client->message, *s);
277         MSG_WriteByte (&client->message, 0);
278
279         for (s = sv.sound_precache+1 ; *s ; s++)
280                 MSG_WriteString (&client->message, *s);
281         MSG_WriteByte (&client->message, 0);
282
283 // send music
284         MSG_WriteByte (&client->message, svc_cdtrack);
285         MSG_WriteByte (&client->message, sv.edicts->v->sounds);
286         MSG_WriteByte (&client->message, sv.edicts->v->sounds);
287
288 // set view
289         MSG_WriteByte (&client->message, svc_setview);
290         MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
291
292         MSG_WriteByte (&client->message, svc_signonnum);
293         MSG_WriteByte (&client->message, 1);
294
295         client->sendsignon = true;
296         client->spawned = false;                // need prespawn, spawn, etc
297 }
298
299 /*
300 ================
301 SV_ConnectClient
302
303 Initializes a client_t for a new net connection.  This will only be called
304 once for a player each game, not once for each level change.
305 ================
306 */
307 void SV_ConnectClient (int clientnum)
308 {
309         client_t                *client;
310         struct qsocket_s *netconnection;
311         int                             i;
312         float                   spawn_parms[NUM_SPAWN_PARMS];
313
314         client = svs.clients + clientnum;
315
316         Con_DPrintf ("Client %s connected\n", client->netconnection->address);
317
318 // set up the client_t
319         netconnection = client->netconnection;
320
321         if (sv.loadgame)
322                 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
323         memset (client, 0, sizeof(*client));
324         client->netconnection = netconnection;
325
326         strcpy (client->name, "unconnected");
327         client->active = true;
328         client->spawned = false;
329         client->edict = EDICT_NUM(clientnum+1);
330         client->message.data = client->msgbuf;
331         client->message.maxsize = sizeof(client->msgbuf);
332         client->message.allowoverflow = true;           // we can catch it
333
334         if (sv.loadgame)
335                 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
336         else
337         {
338         // call the progs to get default spawn parms for the new client
339                 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
340                 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
341                         client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
342         }
343
344 #if NOROUTINGFIX
345         SV_SendServerinfo (client);
346 #else
347         // send serverinfo on first nop
348         client->waitingforconnect = true;
349         client->sendsignon = true;
350         client->spawned = false;                // need prespawn, spawn, etc
351 #endif
352 }
353
354
355 /*
356 ===================
357 SV_CheckForNewClients
358
359 ===================
360 */
361 void SV_CheckForNewClients (void)
362 {
363         struct qsocket_s        *ret;
364         int                             i;
365
366 //
367 // check for new connections
368 //
369         while (1)
370         {
371                 ret = NET_CheckNewConnections ();
372                 if (!ret)
373                         break;
374
375         //
376         // init a new client structure
377         //
378                 for (i=0 ; i<svs.maxclients ; i++)
379                         if (!svs.clients[i].active)
380                                 break;
381                 if (i == svs.maxclients)
382                         Sys_Error ("Host_CheckForNewClients: no free clients");
383
384                 svs.clients[i].netconnection = ret;
385                 SV_ConnectClient (i);
386
387                 net_activeconnections++;
388                 NET_Heartbeat (1);
389         }
390 }
391
392
393
394 /*
395 ===============================================================================
396
397 FRAME UPDATES
398
399 ===============================================================================
400 */
401
402 /*
403 ==================
404 SV_ClearDatagram
405
406 ==================
407 */
408 void SV_ClearDatagram (void)
409 {
410         SZ_Clear (&sv.datagram);
411 }
412
413 /*
414 =============================================================================
415
416 The PVS must include a small area around the client to allow head bobbing
417 or other small motion on the client side.  Otherwise, a bob might cause an
418 entity that should be visible to not show up, especially when the bob
419 crosses a waterline.
420
421 =============================================================================
422 */
423
424 int             fatbytes;
425 qbyte   fatpvs[MAX_MAP_LEAFS/8];
426
427 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
428 {
429         int             i;
430         qbyte   *pvs;
431         mplane_t        *plane;
432         float   d;
433
434         while (1)
435         {
436         // if this is a leaf, accumulate the pvs bits
437                 if (node->contents < 0)
438                 {
439                         if (node->contents != CONTENTS_SOLID)
440                         {
441                                 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
442                                 for (i=0 ; i<fatbytes ; i++)
443                                         fatpvs[i] |= pvs[i];
444                         }
445                         return;
446                 }
447
448                 plane = node->plane;
449                 d = DotProduct (org, plane->normal) - plane->dist;
450                 if (d > 8)
451                         node = node->children[0];
452                 else if (d < -8)
453                         node = node->children[1];
454                 else
455                 {       // go down both
456                         SV_AddToFatPVS (org, node->children[0]);
457                         node = node->children[1];
458                 }
459         }
460 }
461
462 /*
463 =============
464 SV_FatPVS
465
466 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
467 given point.
468 =============
469 */
470 qbyte *SV_FatPVS (vec3_t org)
471 {
472         fatbytes = (sv.worldmodel->numleafs+31)>>3;
473         memset (fatpvs, 0, fatbytes);
474         SV_AddToFatPVS (org, sv.worldmodel->nodes);
475         return fatpvs;
476 }
477
478 //=============================================================================
479
480
481 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
482 {
483         int leafnum;
484 loc0:
485         if (node->contents < 0)
486         {
487                 // leaf
488                 if (node->contents == CONTENTS_SOLID)
489                         return false;
490                 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
491                 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
492         }
493
494         // node - recurse down the BSP tree
495         switch (BoxOnPlaneSide(mins, maxs, node->plane))
496         {
497         case 1: // front
498                 node = node->children[0];
499                 goto loc0;
500         case 2: // back
501                 node = node->children[1];
502                 goto loc0;
503         default: // crossing
504                 if (node->children[0]->contents != CONTENTS_SOLID)
505                         if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
506                                 return true;
507                 node = node->children[1];
508                 goto loc0;
509         }
510         // never reached
511         return false;
512 }
513
514
515 /*
516 =============
517 SV_WriteEntitiesToClient
518
519 =============
520 */
521 #ifdef QUAKEENTITIES
522 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
523 {
524         int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
525         int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
526         qbyte *pvs;
527         vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
528         float nextfullupdate, alphaf;
529         edict_t *ent;
530         eval_t *val;
531         entity_state_t *baseline; // LordHavoc: delta or startup baseline
532         trace_t trace;
533         model_t *model;
534
535         Mod_CheckLoaded(sv.worldmodel);
536
537 // find the client's PVS
538         VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
539         pvs = SV_FatPVS (testeye);
540
541         culled_pvs = 0;
542         culled_portal = 0;
543         culled_trace = 0;
544         visibleentities = 0;
545         totalentities = 0;
546
547         clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
548         // send all entities that touch the pvs
549         ent = NEXT_EDICT(sv.edicts);
550         for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
551         {
552                 bits = 0;
553
554                 // prevent delta compression against this frame (unless actually sent, which will restore this later)
555                 nextfullupdate = client->nextfullupdate[e];
556                 client->nextfullupdate[e] = -1;
557
558                 if (ent != clent) // LordHavoc: always send player
559                 {
560                         if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
561                         {
562                                 if (val->edict != clentnum)
563                                 {
564                                         // don't show to anyone else
565                                         continue;
566                                 }
567                                 else
568                                         bits |= U_VIEWMODEL; // show relative to the view
569                         }
570                         else
571                         {
572                                 // LordHavoc: never draw something told not to display to this client
573                                 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
574                                         continue;
575                                 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
576                                         continue;
577                         }
578                 }
579
580                 glowsize = 0;
581
582                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
583                         glowsize = (int) val->_float >> 2;
584                 if (glowsize > 255) glowsize = 255;
585                 if (glowsize < 0) glowsize = 0;
586
587                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
588                 if (val->_float != 0)
589                         bits |= U_GLOWTRAIL;
590
591                 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
592                 {
593                         model = sv.models[(int)ent->v->modelindex];
594                         Mod_CheckLoaded(model);
595                 }
596                 else
597                 {
598                         model = NULL;
599                         if (ent != clent) // LordHavoc: always send player
600                                 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
601                                         continue;
602                 }
603
604                 VectorCopy(ent->v->angles, angles);
605                 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f)
606                 {
607                         VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
608                         // LordHavoc: trace predicted movement to avoid putting things in walls
609                         trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
610                         VectorCopy(trace.endpos, origin);
611                 }
612                 else
613                 {
614                         VectorCopy(ent->v->origin, origin);
615                 }
616
617                 // ent has survived every check so far, check if it is visible
618                 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
619                 {
620                         // use the predicted origin
621                         entmins[0] = origin[0] - 1.0f;
622                         entmins[1] = origin[1] - 1.0f;
623                         entmins[2] = origin[2] - 1.0f;
624                         entmaxs[0] = origin[0] + 1.0f;
625                         entmaxs[1] = origin[1] + 1.0f;
626                         entmaxs[2] = origin[2] + 1.0f;
627                         // using the model's bounding box to ensure things are visible regardless of their physics box
628                         if (model)
629                         {
630                                 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
631                                 {
632                                         VectorAdd(entmins, model->rotatedmins, entmins);
633                                         VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
634                                 }
635                                 else if (ent->v->angles[1])
636                                 {
637                                         VectorAdd(entmins, model->yawmins, entmins);
638                                         VectorAdd(entmaxs, model->yawmaxs, entmaxs);
639                                 }
640                                 else
641                                 {
642                                         VectorAdd(entmins, model->normalmins, entmins);
643                                         VectorAdd(entmaxs, model->normalmaxs, entmaxs);
644                                 }
645                         }
646
647                         totalentities++;
648
649                         // if not touching a visible leaf
650                         if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
651                         {
652                                 culled_pvs++;
653                                 continue;
654                         }
655
656                         // or not visible through the portals
657                         if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
658                         {
659                                 culled_portal++;
660                                 continue;
661                         }
662
663                         // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
664                         if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
665                         {
666                                 // LordHavoc: test random offsets, to maximize chance of detection
667                                 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
668                                 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
669                                 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
670
671                                 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
672
673                                 if (trace.fraction == 1)
674                                         client->visibletime[e] = realtime + 1;
675                                 else
676                                 {
677                                         //test nearest point on bbox
678                                         testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
679                                         testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
680                                         testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
681
682                                         Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
683
684                                         if (trace.fraction == 1)
685                                                 client->visibletime[e] = realtime + 1;
686                                         else if (realtime > client->visibletime[e])
687                                         {
688                                                 culled_trace++;
689                                                 continue;
690                                         }
691                                 }
692                         }
693                         visibleentities++;
694                 }
695
696                 alphaf = 255.0f;
697                 scale = 16;
698                 glowcolor = 254;
699                 effects = ent->v->effects;
700
701                 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
702                 if (val->_float != 0)
703                         alphaf = val->_float * 255.0f;
704
705                 // HalfLife support
706                 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
707                 if (val->_float != 0)
708                         alphaf = val->_float;
709
710                 if (alphaf == 0.0f)
711                         alphaf = 255.0f;
712                 alpha = bound(0, alphaf, 255);
713
714                 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
715                 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
716                 if (scale < 0) scale = 0;
717                 if (scale > 255) scale = 255;
718
719                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
720                 if (val->_float != 0)
721                         glowcolor = (int) val->_float;
722
723                 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
724                 if (val->_float != 0)
725                         effects |= EF_FULLBRIGHT;
726
727                 if (ent != clent)
728                 {
729                         if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
730                         {
731                                 if (model) // model
732                                 {
733                                         // don't send if flagged for NODRAW and there are no effects
734                                         if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
735                                                 continue;
736                                 }
737                                 else // no model and no effects
738                                         continue;
739                         }
740                 }
741
742                 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
743                 {
744                         Con_Printf ("packet overflow\n");
745                         // mark the rest of the entities so they can't be delta compressed against this frame
746                         for (;e < sv.num_edicts;e++)
747                         {
748                                 client->nextfullupdate[e] = -1;
749                                 client->visibletime[e] = -1;
750                         }
751                         return;
752                 }
753
754                 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
755                         bits = bits | U_EXTERIORMODEL;
756
757 // send an update
758                 baseline = &ent->e->baseline;
759
760                 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
761                 {
762                         // every half second a full update is forced
763                         if (realtime < client->nextfullupdate[e])
764                         {
765                                 bits |= U_DELTA;
766                                 baseline = &ent->e->deltabaseline;
767                         }
768                         else
769                                 nextfullupdate = realtime + 0.5f;
770                 }
771                 else
772                         nextfullupdate = realtime + 0.5f;
773
774                 // restore nextfullupdate since this is being sent for real
775                 client->nextfullupdate[e] = nextfullupdate;
776
777                 if (e >= 256)
778                         bits |= U_LONGENTITY;
779
780                 if (ent->v->movetype == MOVETYPE_STEP)
781                         bits |= U_STEP;
782
783                 // LordHavoc: old stuff, but rewritten to have more exact tolerances
784                 if (origin[0] != baseline->origin[0])                                                                                   bits |= U_ORIGIN1;
785                 if (origin[1] != baseline->origin[1])                                                                                   bits |= U_ORIGIN2;
786                 if (origin[2] != baseline->origin[2])                                                                                   bits |= U_ORIGIN3;
787                 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
788                 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
789                 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
790                 if (baseline->colormap != (qbyte) ent->v->colormap)                                                             bits |= U_COLORMAP;
791                 if (baseline->skin != (qbyte) ent->v->skin)                                                                             bits |= U_SKIN;
792                 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF))                               bits |= U_FRAME;
793                 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF))                   bits |= U_EFFECTS;
794                 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF))             bits |= U_MODEL;
795
796                 // LordHavoc: new stuff
797                 if (baseline->alpha != alpha)                                                                                                   bits |= U_ALPHA;
798                 if (baseline->scale != scale)                                                                                                   bits |= U_SCALE;
799                 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00))             bits |= U_EFFECTS2;
800                 if (baseline->glowsize != glowsize)                                                                                             bits |= U_GLOWSIZE;
801                 if (baseline->glowcolor != glowcolor)                                                                                   bits |= U_GLOWCOLOR;
802                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00))                 bits |= U_FRAME2;
803                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00))            bits |= U_MODEL2;
804
805                 // update delta baseline
806                 VectorCopy(ent->v->origin, ent->e->deltabaseline.origin);
807                 VectorCopy(ent->v->angles, ent->e->deltabaseline.angles);
808                 ent->e->deltabaseline.colormap = ent->v->colormap;
809                 ent->e->deltabaseline.skin = ent->v->skin;
810                 ent->e->deltabaseline.frame = ent->v->frame;
811                 ent->e->deltabaseline.effects = ent->v->effects;
812                 ent->e->deltabaseline.modelindex = ent->v->modelindex;
813                 ent->e->deltabaseline.alpha = alpha;
814                 ent->e->deltabaseline.scale = scale;
815                 ent->e->deltabaseline.glowsize = glowsize;
816                 ent->e->deltabaseline.glowcolor = glowcolor;
817
818                 // write the message
819                 if (bits >= 16777216)
820                         bits |= U_EXTEND2;
821                 if (bits >= 65536)
822                         bits |= U_EXTEND1;
823                 if (bits >= 256)
824                         bits |= U_MOREBITS;
825                 bits |= U_SIGNAL;
826
827                 MSG_WriteByte (msg, bits);
828                 if (bits & U_MOREBITS)
829                         MSG_WriteByte (msg, bits>>8);
830                 // LordHavoc: extend bytes have to be written here due to delta compression
831                 if (bits & U_EXTEND1)
832                         MSG_WriteByte (msg, bits>>16);
833                 if (bits & U_EXTEND2)
834                         MSG_WriteByte (msg, bits>>24);
835
836                 // LordHavoc: old stuff
837                 if (bits & U_LONGENTITY)
838                         MSG_WriteShort (msg,e);
839                 else
840                         MSG_WriteByte (msg,e);
841                 if (bits & U_MODEL)             MSG_WriteByte (msg,     ent->v->modelindex);
842                 if (bits & U_FRAME)             MSG_WriteByte (msg, ent->v->frame);
843                 if (bits & U_COLORMAP)  MSG_WriteByte (msg, ent->v->colormap);
844                 if (bits & U_SKIN)              MSG_WriteByte (msg, ent->v->skin);
845                 if (bits & U_EFFECTS)   MSG_WriteByte (msg, ent->v->effects);
846                 if (bits & U_ORIGIN1)   MSG_WriteDPCoord (msg, origin[0]);
847                 if (bits & U_ANGLE1)    MSG_WriteAngle(msg, angles[0]);
848                 if (bits & U_ORIGIN2)   MSG_WriteDPCoord (msg, origin[1]);
849                 if (bits & U_ANGLE2)    MSG_WriteAngle(msg, angles[1]);
850                 if (bits & U_ORIGIN3)   MSG_WriteDPCoord (msg, origin[2]);
851                 if (bits & U_ANGLE3)    MSG_WriteAngle(msg, angles[2]);
852
853                 // LordHavoc: new stuff
854                 if (bits & U_ALPHA)             MSG_WriteByte(msg, alpha);
855                 if (bits & U_SCALE)             MSG_WriteByte(msg, scale);
856                 if (bits & U_EFFECTS2)  MSG_WriteByte(msg, (int)ent->v->effects >> 8);
857                 if (bits & U_GLOWSIZE)  MSG_WriteByte(msg, glowsize);
858                 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
859                 if (bits & U_FRAME2)    MSG_WriteByte(msg, (int)ent->v->frame >> 8);
860                 if (bits & U_MODEL2)    MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
861         }
862
863         if (sv_cullentities_stats.integer)
864                 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d portal %d trace\n", client->name, totalentities, visibleentities, culled_pvs + culled_portal + culled_trace, culled_pvs, culled_portal, culled_trace);
865 }
866 #else
867 static entity_frame_t sv_writeentitiestoclient_entityframe;
868 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
869 {
870         int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
871         int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
872         float alphaf, lightsize;
873         qbyte *pvs;
874         vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
875         edict_t *ent;
876         eval_t *val;
877         trace_t trace;
878         model_t *model;
879         entity_state_t *s;
880
881         if (client->sendsignon)
882                 return;
883
884         Mod_CheckLoaded(sv.worldmodel);
885
886 // find the client's PVS
887         // the real place being tested from
888         VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
889         pvs = SV_FatPVS (testeye);
890
891         // the place being reported (to consider the fact the client still
892         // applies the view_ofs[2], so we have to only send the fractional part
893         // of view_ofs[2], undoing what the client will redo)
894         VectorCopy (testeye, testorigin);
895         e = (int) clent->v->view_ofs[2] & 255;
896         if (e >= 128)
897                 e -= 256;
898         testorigin[2] -= (float) e;
899         EntityFrame_Clear(&sv_writeentitiestoclient_entityframe, testorigin);
900
901         culled_pvs = 0;
902         culled_portal = 0;
903         culled_trace = 0;
904         visibleentities = 0;
905         totalentities = 0;
906
907         clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
908         // send all entities that touch the pvs
909         ent = NEXT_EDICT(sv.edicts);
910         for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
911         {
912                 if (ent->e->free)
913                         continue;
914                 flags = 0;
915
916                 if (ent != clent) // LordHavoc: always send player
917                 {
918                         if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
919                         {
920                                 if (val->edict == clentnum)
921                                         flags |= RENDER_VIEWMODEL; // show relative to the view
922                                 else
923                                 {
924                                         // don't show to anyone else
925                                         continue;
926                                 }
927                         }
928                         else
929                         {
930                                 // LordHavoc: never draw something told not to display to this client
931                                 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
932                                         continue;
933                                 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
934                                         continue;
935                         }
936                 }
937
938                 glowsize = 0;
939                 effects = ent->v->effects;
940                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
941                         glowsize = (int) val->_float >> 2;
942                 glowsize = bound(0, glowsize, 255);
943
944                 lightsize = 0;
945                 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
946                 {
947                         if (effects & EF_BRIGHTFIELD)
948                                 lightsize = max(lightsize, 80);
949                         if (effects & EF_MUZZLEFLASH)
950                                 lightsize = max(lightsize, 100);
951                         if (effects & EF_BRIGHTLIGHT)
952                                 lightsize = max(lightsize, 400);
953                         if (effects & EF_DIMLIGHT)
954                                 lightsize = max(lightsize, 200);
955                         if (effects & EF_RED)
956                                 lightsize = max(lightsize, 200);
957                         if (effects & EF_BLUE)
958                                 lightsize = max(lightsize, 200);
959                         if (effects & EF_FLAME)
960                                 lightsize = max(lightsize, 250);
961                         if (effects & EF_STARDUST)
962                                 lightsize = max(lightsize, 100);
963                 }
964                 if (glowsize)
965                         lightsize = max(lightsize, glowsize << 2);
966
967                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
968                 if (val->_float != 0)
969                 {
970                         flags |= RENDER_GLOWTRAIL;
971                         lightsize = max(lightsize, 100);
972                 }
973
974                 modelindex = 0;
975                 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
976                 {
977                         modelindex = ent->v->modelindex;
978                         model = sv.models[(int)ent->v->modelindex];
979                         Mod_CheckLoaded(model);
980                 }
981                 else
982                 {
983                         model = NULL;
984                         if (ent != clent) // LordHavoc: always send player
985                                 if (lightsize == 0) // no effects
986                                         continue;
987                 }
988
989                 VectorCopy(ent->v->angles, angles);
990                 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f && host_client->latency >= 0.01f)
991                 {
992                         VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
993                         // LordHavoc: trace predicted movement to avoid putting things in walls
994                         trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
995                         VectorCopy(trace.endpos, origin);
996                 }
997                 else
998                 {
999                         VectorCopy(ent->v->origin, origin);
1000                 }
1001
1002                 // ent has survived every check so far, check if it is visible
1003                 // always send embedded brush models, they don't generate much traffic
1004                 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
1005                 {
1006                         // use the predicted origin
1007                         entmins[0] = origin[0] - 1.0f;
1008                         entmins[1] = origin[1] - 1.0f;
1009                         entmins[2] = origin[2] - 1.0f;
1010                         entmaxs[0] = origin[0] + 1.0f;
1011                         entmaxs[1] = origin[1] + 1.0f;
1012                         entmaxs[2] = origin[2] + 1.0f;
1013                         // using the model's bounding box to ensure things are visible regardless of their physics box
1014                         if (model)
1015                         {
1016                                 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
1017                                 {
1018                                         VectorAdd(entmins, model->rotatedmins, entmins);
1019                                         VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
1020                                 }
1021                                 else if (ent->v->angles[1])
1022                                 {
1023                                         VectorAdd(entmins, model->yawmins, entmins);
1024                                         VectorAdd(entmaxs, model->yawmaxs, entmaxs);
1025                                 }
1026                                 else
1027                                 {
1028                                         VectorAdd(entmins, model->normalmins, entmins);
1029                                         VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1030                                 }
1031                         }
1032                         lightmins[0] = min(entmins[0], origin[0] - lightsize);
1033                         lightmins[1] = min(entmins[1], origin[1] - lightsize);
1034                         lightmins[2] = min(entmins[2], origin[2] - lightsize);
1035                         lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
1036                         lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
1037                         lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
1038
1039                         totalentities++;
1040
1041                         // if not touching a visible leaf
1042                         if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->nodes))
1043                         {
1044                                 culled_pvs++;
1045                                 continue;
1046                         }
1047
1048                         // or not visible through the portals
1049                         if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
1050                         {
1051                                 culled_portal++;
1052                                 continue;
1053                         }
1054
1055                         if (sv_cullentities_trace.integer)
1056                         {
1057                                 // LordHavoc: test center first
1058                                 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
1059                                 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
1060                                 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
1061                                 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1062                                 if (trace.fraction == 1)
1063                                         client->visibletime[e] = realtime + 1;
1064                                 else
1065                                 {
1066                                         // LordHavoc: test random offsets, to maximize chance of detection
1067                                         testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1068                                         testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1069                                         testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1070                                         Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1071                                         if (trace.fraction == 1)
1072                                                 client->visibletime[e] = realtime + 1;
1073                                         else
1074                                         {
1075                                                 if (lightsize)
1076                                                 {
1077                                                         // LordHavoc: test random offsets, to maximize chance of detection
1078                                                         testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1079                                                         testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1080                                                         testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1081                                                         Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1082                                                         if (trace.fraction == 1)
1083                                                                 client->visibletime[e] = realtime + 1;
1084                                                         else
1085                                                         {
1086                                                                 if (realtime > client->visibletime[e])
1087                                                                 {
1088                                                                         culled_trace++;
1089                                                                         continue;
1090                                                                 }
1091                                                         }
1092                                                 }
1093                                                 else
1094                                                 {
1095                                                         if (realtime > client->visibletime[e])
1096                                                         {
1097                                                                 culled_trace++;
1098                                                                 continue;
1099                                                         }
1100                                                 }
1101                                         }
1102                                 }
1103                         }
1104                         visibleentities++;
1105                 }
1106
1107                 alphaf = 255.0f;
1108                 scale = 16;
1109                 glowcolor = 254;
1110                 effects = ent->v->effects;
1111
1112                 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1113                 if (val->_float != 0)
1114                         alphaf = val->_float * 255.0;
1115
1116                 // HalfLife support
1117                 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1118                 if (val->_float != 0)
1119                         alphaf = val->_float;
1120
1121                 if (alphaf == 0.0f)
1122                         alphaf = 255.0f;
1123                 alpha = bound(0, alphaf, 255);
1124
1125                 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1126                 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1127                 if (scale < 0) scale = 0;
1128                 if (scale > 255) scale = 255;
1129
1130                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1131                 if (val->_float != 0)
1132                         glowcolor = (int) val->_float;
1133
1134                 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1135                 if (val->_float != 0)
1136                         effects |= EF_FULLBRIGHT;
1137
1138                 if (ent != clent)
1139                 {
1140                         if (lightsize == 0) // no effects
1141                         {
1142                                 if (model) // model
1143                                 {
1144                                         // don't send if flagged for NODRAW and there are no effects
1145                                         if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1146                                                 continue;
1147                                 }
1148                                 else // no model and no effects
1149                                         continue;
1150                         }
1151                 }
1152
1153                 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1154                         flags |= RENDER_EXTERIORMODEL;
1155
1156                 if (ent->v->movetype == MOVETYPE_STEP)
1157                         flags |= RENDER_STEP;
1158                 // don't send an entity if it's coordinates would wrap around
1159                 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1160                         flags |= RENDER_LOWPRECISION;
1161
1162                 s = EntityFrame_NewEntity(&sv_writeentitiestoclient_entityframe, e);
1163                 // if we run out of space, abort
1164                 if (!s)
1165                         break;
1166                 VectorCopy(origin, s->origin);
1167                 VectorCopy(angles, s->angles);
1168                 if (ent->v->colormap >= 1024)
1169                         flags |= RENDER_COLORMAPPED;
1170                 s->colormap = ent->v->colormap;
1171                 s->skin = ent->v->skin;
1172                 s->frame = ent->v->frame;
1173                 s->modelindex = modelindex;
1174                 s->effects = effects;
1175                 s->alpha = alpha;
1176                 s->scale = scale;
1177                 s->glowsize = glowsize;
1178                 s->glowcolor = glowcolor;
1179                 s->flags = flags;
1180         }
1181         sv_writeentitiestoclient_entityframe.framenum = ++client->entityframenumber;
1182         EntityFrame_Write(&client->entitydatabase, &sv_writeentitiestoclient_entityframe, msg);
1183
1184         if (sv_cullentities_stats.integer)
1185                 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d portal %d trace\n", client->name, totalentities, visibleentities, culled_pvs + culled_portal + culled_trace, culled_pvs, culled_portal, culled_trace);
1186 }
1187 #endif
1188
1189 /*
1190 =============
1191 SV_CleanupEnts
1192
1193 =============
1194 */
1195 void SV_CleanupEnts (void)
1196 {
1197         int             e;
1198         edict_t *ent;
1199
1200         ent = NEXT_EDICT(sv.edicts);
1201         for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1202                 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1203 }
1204
1205 /*
1206 ==================
1207 SV_WriteClientdataToMessage
1208
1209 ==================
1210 */
1211 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1212 {
1213         int             bits;
1214         int             i;
1215         edict_t *other;
1216         int             items;
1217         eval_t  *val;
1218         vec3_t  punchvector;
1219         qbyte   viewzoom;
1220
1221 //
1222 // send a damage message
1223 //
1224         if (ent->v->dmg_take || ent->v->dmg_save)
1225         {
1226                 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1227                 MSG_WriteByte (msg, svc_damage);
1228                 MSG_WriteByte (msg, ent->v->dmg_save);
1229                 MSG_WriteByte (msg, ent->v->dmg_take);
1230                 for (i=0 ; i<3 ; i++)
1231                         MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1232
1233                 ent->v->dmg_take = 0;
1234                 ent->v->dmg_save = 0;
1235         }
1236
1237 //
1238 // send the current viewpos offset from the view entity
1239 //
1240         SV_SetIdealPitch ();            // how much to look up / down ideally
1241
1242 // a fixangle might get lost in a dropped packet.  Oh well.
1243         if ( ent->v->fixangle )
1244         {
1245                 MSG_WriteByte (msg, svc_setangle);
1246                 for (i=0 ; i < 3 ; i++)
1247                         MSG_WriteAngle (msg, ent->v->angles[i] );
1248                 ent->v->fixangle = 0;
1249         }
1250
1251         bits = 0;
1252
1253         if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1254                 bits |= SU_VIEWHEIGHT;
1255
1256         if (ent->v->idealpitch)
1257                 bits |= SU_IDEALPITCH;
1258
1259 // stuff the sigil bits into the high bits of items for sbar, or else
1260 // mix in items2
1261         val = GETEDICTFIELDVALUE(ent, eval_items2);
1262
1263         if (val)
1264                 items = (int)ent->v->items | ((int)val->_float << 23);
1265         else
1266                 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1267
1268         bits |= SU_ITEMS;
1269
1270         if ( (int)ent->v->flags & FL_ONGROUND)
1271                 bits |= SU_ONGROUND;
1272
1273         if ( ent->v->waterlevel >= 2)
1274                 bits |= SU_INWATER;
1275
1276         // dpprotocol
1277         VectorClear(punchvector);
1278         if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1279                 VectorCopy(val->vector, punchvector);
1280
1281         i = 255;
1282         if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1283         {
1284                 i = val->_float * 255.0f;
1285                 if (i == 0)
1286                         i = 255;
1287                 else
1288                         i = bound(0, i, 255);
1289         }
1290         viewzoom = i;
1291
1292         if (viewzoom != 255)
1293                 bits |= SU_VIEWZOOM;
1294
1295         for (i=0 ; i<3 ; i++)
1296         {
1297                 if (ent->v->punchangle[i])
1298                         bits |= (SU_PUNCH1<<i);
1299                 if (punchvector[i]) // dpprotocol
1300                         bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1301                 if (ent->v->velocity[i])
1302                         bits |= (SU_VELOCITY1<<i);
1303         }
1304
1305         if (ent->v->weaponframe)
1306                 bits |= SU_WEAPONFRAME;
1307
1308         if (ent->v->armorvalue)
1309                 bits |= SU_ARMOR;
1310
1311         bits |= SU_WEAPON;
1312
1313         if (bits >= 65536)
1314                 bits |= SU_EXTEND1;
1315         if (bits >= 16777216)
1316                 bits |= SU_EXTEND2;
1317
1318 // send the data
1319
1320         MSG_WriteByte (msg, svc_clientdata);
1321         MSG_WriteShort (msg, bits);
1322         if (bits & SU_EXTEND1)
1323                 MSG_WriteByte(msg, bits >> 16);
1324         if (bits & SU_EXTEND2)
1325                 MSG_WriteByte(msg, bits >> 24);
1326
1327         if (bits & SU_VIEWHEIGHT)
1328                 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1329
1330         if (bits & SU_IDEALPITCH)
1331                 MSG_WriteChar (msg, ent->v->idealpitch);
1332
1333         for (i=0 ; i<3 ; i++)
1334         {
1335                 if (bits & (SU_PUNCH1<<i))
1336                         MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1337                 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1338                         MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1339                 if (bits & (SU_VELOCITY1<<i))
1340                         MSG_WriteChar (msg, ent->v->velocity[i]/16);
1341         }
1342
1343 // [always sent]        if (bits & SU_ITEMS)
1344         MSG_WriteLong (msg, items);
1345
1346         if (bits & SU_WEAPONFRAME)
1347                 MSG_WriteByte (msg, ent->v->weaponframe);
1348         if (bits & SU_ARMOR)
1349                 MSG_WriteByte (msg, ent->v->armorvalue);
1350         if (bits & SU_WEAPON)
1351                 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1352
1353         MSG_WriteShort (msg, ent->v->health);
1354         MSG_WriteByte (msg, ent->v->currentammo);
1355         MSG_WriteByte (msg, ent->v->ammo_shells);
1356         MSG_WriteByte (msg, ent->v->ammo_nails);
1357         MSG_WriteByte (msg, ent->v->ammo_rockets);
1358         MSG_WriteByte (msg, ent->v->ammo_cells);
1359
1360         if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1361         {
1362                 for(i=0;i<32;i++)
1363                 {
1364                         if ( ((int)ent->v->weapon) & (1<<i) )
1365                         {
1366                                 MSG_WriteByte (msg, i);
1367                                 break;
1368                         }
1369                 }
1370         }
1371         else
1372         {
1373                 MSG_WriteByte (msg, ent->v->weapon);
1374         }
1375
1376         if (bits & SU_VIEWZOOM)
1377                 MSG_WriteByte (msg, viewzoom);
1378 }
1379
1380 /*
1381 =======================
1382 SV_SendClientDatagram
1383 =======================
1384 */
1385 static qbyte sv_sendclientdatagram_buf[MAX_DATAGRAM]; // FIXME?
1386 qboolean SV_SendClientDatagram (client_t *client)
1387 {
1388         sizebuf_t       msg;
1389
1390         msg.data = sv_sendclientdatagram_buf;
1391         msg.maxsize = sizeof(sv_sendclientdatagram_buf);
1392         msg.cursize = 0;
1393
1394         MSG_WriteByte (&msg, svc_time);
1395         MSG_WriteFloat (&msg, sv.time);
1396
1397         if (client->spawned)
1398         {
1399                 // add the client specific data to the datagram
1400                 SV_WriteClientdataToMessage (client->edict, &msg);
1401
1402                 SV_WriteEntitiesToClient (client, client->edict, &msg);
1403
1404                 // copy the server datagram if there is space
1405                 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1406                         SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1407         }
1408
1409 // send the datagram
1410         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1411         {
1412                 SV_DropClient (true);// if the message couldn't send, kick off
1413                 return false;
1414         }
1415
1416         return true;
1417 }
1418
1419 /*
1420 =======================
1421 SV_UpdateToReliableMessages
1422 =======================
1423 */
1424 void SV_UpdateToReliableMessages (void)
1425 {
1426         int i, j;
1427         client_t *client;
1428
1429 // check for changes to be sent over the reliable streams
1430         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1431         {
1432                 sv_player = host_client->edict;
1433                 if (host_client->old_frags != sv_player->v->frags)
1434                 {
1435                         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1436                         {
1437                                 if (!client->active || !client->spawned)
1438                                         continue;
1439                                 MSG_WriteByte (&client->message, svc_updatefrags);
1440                                 MSG_WriteByte (&client->message, i);
1441                                 MSG_WriteShort (&client->message, sv_player->v->frags);
1442                         }
1443
1444                         host_client->old_frags = sv_player->v->frags;
1445                 }
1446         }
1447
1448         for (j=0, host_client = svs.clients ; j<svs.maxclients ; j++, host_client++)
1449         {
1450                 if (!host_client->active)
1451                         continue;
1452                 SZ_Write (&host_client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1453         }
1454
1455         SZ_Clear (&sv.reliable_datagram);
1456 }
1457
1458
1459 /*
1460 =======================
1461 SV_SendNop
1462
1463 Send a nop message without trashing or sending the accumulated client
1464 message buffer
1465 =======================
1466 */
1467 void SV_SendNop (client_t *client)
1468 {
1469         sizebuf_t       msg;
1470         qbyte           buf[4];
1471
1472         msg.data = buf;
1473         msg.maxsize = sizeof(buf);
1474         msg.cursize = 0;
1475
1476         MSG_WriteChar (&msg, svc_nop);
1477
1478         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1479                 SV_DropClient (true);   // if the message couldn't send, kick off
1480         client->last_message = realtime;
1481 }
1482
1483 /*
1484 =======================
1485 SV_SendClientMessages
1486 =======================
1487 */
1488 void SV_SendClientMessages (void)
1489 {
1490         int                     i;
1491
1492 // update frags, names, etc
1493         SV_UpdateToReliableMessages ();
1494
1495 // build individual updates
1496         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1497         {
1498                 if (!host_client->active)
1499                         continue;
1500
1501 #ifndef NOROUTINGFIX
1502                 if (host_client->sendserverinfo)
1503                 {
1504                         host_client->sendserverinfo = false;
1505                         SV_SendServerinfo (host_client);
1506                 }
1507 #endif
1508
1509                 if (host_client->spawned)
1510                 {
1511                         if (!SV_SendClientDatagram (host_client))
1512                                 continue;
1513                 }
1514                 else
1515                 {
1516                 // the player isn't totally in the game yet
1517                 // send small keepalive messages if too much time has passed
1518                 // send a full message when the next signon stage has been requested
1519                 // some other message data (name changes, etc) may accumulate
1520                 // between signon stages
1521                         if (!host_client->sendsignon)
1522                         {
1523                                 if (realtime - host_client->last_message > 5)
1524                                         SV_SendNop (host_client);
1525                                 continue;       // don't send out non-signon messages
1526                         }
1527                 }
1528
1529                 // check for an overflowed message.  Should only happen
1530                 // on a very fucked up connection that backs up a lot, then
1531                 // changes level
1532                 if (host_client->message.overflowed)
1533                 {
1534                         SV_DropClient (true); // overflowed
1535                         host_client->message.overflowed = false;
1536                         continue;
1537                 }
1538
1539                 if (host_client->message.cursize || host_client->dropasap)
1540                 {
1541                         if (!NET_CanSendMessage (host_client->netconnection))
1542                                 continue;
1543
1544                         if (host_client->dropasap)
1545                                 SV_DropClient (false);  // went to another level
1546                         else
1547                         {
1548                                 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1549                                         SV_DropClient (true);   // if the message couldn't send, kick off
1550                                 SZ_Clear (&host_client->message);
1551                                 host_client->last_message = realtime;
1552                                 host_client->sendsignon = false;
1553                         }
1554                 }
1555         }
1556
1557
1558 // clear muzzle flashes
1559         SV_CleanupEnts ();
1560 }
1561
1562
1563 /*
1564 ==============================================================================
1565
1566 SERVER SPAWNING
1567
1568 ==============================================================================
1569 */
1570
1571 /*
1572 ================
1573 SV_ModelIndex
1574
1575 ================
1576 */
1577 int SV_ModelIndex (const char *name)
1578 {
1579         int i;
1580
1581         if (!name || !name[0])
1582                 return 0;
1583
1584         for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1585                 if (!strcmp(sv.model_precache[i], name))
1586                         return i;
1587         if (i==MAX_MODELS || !sv.model_precache[i])
1588                 Host_Error ("SV_ModelIndex: model %s not precached", name);
1589         return i;
1590 }
1591
1592 #ifdef SV_QUAKEENTITIES
1593 /*
1594 ================
1595 SV_CreateBaseline
1596
1597 ================
1598 */
1599 void SV_CreateBaseline (void)
1600 {
1601         int i, entnum, large;
1602         edict_t *svent;
1603
1604         // LordHavoc: clear *all* states (note just active ones)
1605         for (entnum = 0;entnum < sv.max_edicts;entnum++)
1606         {
1607                 // get the current server version
1608                 svent = EDICT_NUM(entnum);
1609
1610                 // LordHavoc: always clear state values, whether the entity is in use or not
1611                 ClearStateToDefault(&svent->e->baseline);
1612
1613                 if (svent->e->free)
1614                         continue;
1615                 if (entnum > svs.maxclients && !svent->v->modelindex)
1616                         continue;
1617
1618                 // create entity baseline
1619                 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1620                 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1621                 svent->e->baseline.frame = svent->v->frame;
1622                 svent->e->baseline.skin = svent->v->skin;
1623                 if (entnum > 0 && entnum <= svs.maxclients)
1624                 {
1625                         svent->e->baseline.colormap = entnum;
1626                         svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1627                 }
1628                 else
1629                 {
1630                         svent->e->baseline.colormap = 0;
1631                         svent->e->baseline.modelindex = svent->v->modelindex;
1632                 }
1633
1634                 large = false;
1635                 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1636                         large = true;
1637
1638                 // add to the message
1639                 if (large)
1640                         MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1641                 else
1642                         MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1643                 MSG_WriteShort (&sv.signon, entnum);
1644
1645                 if (large)
1646                 {
1647                         MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1648                         MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1649                 }
1650                 else
1651                 {
1652                         MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1653                         MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1654                 }
1655                 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1656                 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1657                 for (i=0 ; i<3 ; i++)
1658                 {
1659                         MSG_WriteDPCoord(&sv.signon, svent->e->baseline.origin[i]);
1660                         MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i]);
1661                 }
1662         }
1663 }
1664 #endif
1665
1666
1667 /*
1668 ================
1669 SV_SendReconnect
1670
1671 Tell all the clients that the server is changing levels
1672 ================
1673 */
1674 void SV_SendReconnect (void)
1675 {
1676         char    data[128];
1677         sizebuf_t       msg;
1678
1679         msg.data = data;
1680         msg.cursize = 0;
1681         msg.maxsize = sizeof(data);
1682
1683         MSG_WriteChar (&msg, svc_stufftext);
1684         MSG_WriteString (&msg, "reconnect\n");
1685         NET_SendToAll (&msg, 5);
1686
1687         if (cls.state != ca_dedicated)
1688                 Cmd_ExecuteString ("reconnect\n", src_command);
1689 }
1690
1691
1692 /*
1693 ================
1694 SV_SaveSpawnparms
1695
1696 Grabs the current state of each client for saving across the
1697 transition to another level
1698 ================
1699 */
1700 void SV_SaveSpawnparms (void)
1701 {
1702         int             i, j;
1703
1704         svs.serverflags = pr_global_struct->serverflags;
1705
1706         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1707         {
1708                 if (!host_client->active)
1709                         continue;
1710
1711         // call the progs to get default spawn parms for the new client
1712                 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1713                 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1714                 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1715                         host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1716         }
1717 }
1718
1719 void SV_IncreaseEdicts(void)
1720 {
1721         int i;
1722         edict_t *ent;
1723         int oldmax_edicts = sv.max_edicts;
1724         void *oldedictsengineprivate = sv.edictsengineprivate;
1725         void *oldedictsfields = sv.edictsfields;
1726         void *oldmoved_edicts = sv.moved_edicts;
1727
1728         // links don't survive the transition, so unlink everything
1729         for (i = 0;i < sv.max_edicts;i++)
1730                 SV_UnlinkEdict (sv.edicts + i);
1731         SV_ClearWorld();
1732
1733         sv.max_edicts   = min(sv.max_edicts + 32, MAX_EDICTS);
1734         sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1735         sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1736         sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1737
1738         memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1739         memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1740
1741         for (i = 0;i < sv.max_edicts;i++)
1742         {
1743                 ent = sv.edicts + i;
1744                 ent->e = sv.edictsengineprivate + i;
1745                 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1746                 // link every entity except world
1747                 if (i > 0)
1748                         SV_LinkEdict(ent, false);
1749         }
1750
1751         Mem_Free(oldedictsengineprivate);
1752         Mem_Free(oldedictsfields);
1753         Mem_Free(oldmoved_edicts);
1754 }
1755
1756 /*
1757 ================
1758 SV_SpawnServer
1759
1760 This is called at the start of each level
1761 ================
1762 */
1763 extern float            scr_centertime_off;
1764
1765 void SV_SpawnServer (const char *server)
1766 {
1767         edict_t *ent;
1768         int i;
1769         qbyte *entities;
1770
1771         // let's not have any servers with no name
1772         if (hostname.string[0] == 0)
1773                 Cvar_Set ("hostname", "UNNAMED");
1774         scr_centertime_off = 0;
1775
1776         Con_DPrintf ("SpawnServer: %s\n",server);
1777         svs.changelevel_issued = false;         // now safe to issue another
1778
1779 //
1780 // tell all connected clients that we are going to a new level
1781 //
1782         if (sv.active)
1783                 SV_SendReconnect ();
1784
1785 //
1786 // make cvars consistant
1787 //
1788         if (coop.integer)
1789                 Cvar_SetValue ("deathmatch", 0);
1790         current_skill = bound(0, (int)(skill.value + 0.5), 3);
1791
1792         Cvar_SetValue ("skill", (float)current_skill);
1793
1794 //
1795 // set up the new server
1796 //
1797         Host_ClearMemory ();
1798
1799         memset (&sv, 0, sizeof(sv));
1800
1801         strcpy (sv.name, server);
1802
1803 // load progs to get entity field count
1804         PR_LoadProgs ();
1805
1806 // allocate server memory
1807         // start out with just enough room for clients and a reasonable estimate of entities
1808         sv.max_edicts = ((svs.maxclients + 128) + 31) & ~31;
1809         sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1810
1811         // clear the edict memory pool
1812         Mem_EmptyPool(sv_edicts_mempool);
1813         // edict_t structures (hidden from progs)
1814         sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1815         // engine private structures (hidden from progs)
1816         sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1817         // progs fields, often accessed by server
1818         sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1819         // used by PushMove to move back pushed entities
1820         sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1821         for (i = 0;i < sv.max_edicts;i++)
1822         {
1823                 ent = sv.edicts + i;
1824                 ent->e = sv.edictsengineprivate + i;
1825                 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1826         }
1827
1828         sv.datagram.maxsize = sizeof(sv.datagram_buf);
1829         sv.datagram.cursize = 0;
1830         sv.datagram.data = sv.datagram_buf;
1831
1832         sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1833         sv.reliable_datagram.cursize = 0;
1834         sv.reliable_datagram.data = sv.reliable_datagram_buf;
1835
1836         sv.signon.maxsize = sizeof(sv.signon_buf);
1837         sv.signon.cursize = 0;
1838         sv.signon.data = sv.signon_buf;
1839
1840 // leave slots at start for clients only
1841         sv.num_edicts = svs.maxclients+1;
1842         for (i=0 ; i<svs.maxclients ; i++)
1843                 svs.clients[i].edict = EDICT_NUM(i+1);
1844
1845         sv.state = ss_loading;
1846         sv.paused = false;
1847
1848         sv.time = 1.0;
1849
1850         Mod_ClearUsed();
1851
1852         strcpy (sv.name, server);
1853         sprintf (sv.modelname,"maps/%s.bsp", server);
1854         sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1855         if (!sv.worldmodel)
1856         {
1857                 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1858                 sv.active = false;
1859                 return;
1860         }
1861         sv.models[1] = sv.worldmodel;
1862
1863 //
1864 // clear world interaction links
1865 //
1866         SV_ClearWorld ();
1867
1868         sv.sound_precache[0] = "";
1869
1870         sv.model_precache[0] = "";
1871         sv.model_precache[1] = sv.modelname;
1872         for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1873         {
1874                 sv.model_precache[i+1] = localmodels[i];
1875                 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1876         }
1877
1878 //
1879 // load the rest of the entities
1880 //
1881         ent = EDICT_NUM(0);
1882         memset (ent->v, 0, progs->entityfields * 4);
1883         ent->e->free = false;
1884         ent->v->model = PR_SetString(sv.modelname);
1885         ent->v->modelindex = 1;         // world model
1886         ent->v->solid = SOLID_BSP;
1887         ent->v->movetype = MOVETYPE_PUSH;
1888
1889         if (coop.integer)
1890                 pr_global_struct->coop = coop.integer;
1891         else
1892                 pr_global_struct->deathmatch = deathmatch.integer;
1893
1894         pr_global_struct->mapname = PR_SetString(sv.name);
1895
1896 // serverflags are for cross level information (sigils)
1897         pr_global_struct->serverflags = svs.serverflags;
1898
1899         // load replacement entity file if found
1900         entities = NULL;
1901         if (sv_entpatch.integer)
1902                 entities = FS_LoadFile(va("maps/%s.ent", sv.name), true);
1903         if (entities)
1904         {
1905                 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1906                 ED_LoadFromFile (entities);
1907                 Mem_Free(entities);
1908         }
1909         else
1910                 ED_LoadFromFile (sv.worldmodel->entities);
1911
1912
1913         // LordHavoc: clear world angles (to fix e3m3.bsp)
1914         VectorClear(sv.edicts->v->angles);
1915
1916         sv.active = true;
1917
1918 // all setup is completed, any further precache statements are errors
1919         sv.state = ss_active;
1920
1921 // run two frames to allow everything to settle
1922         sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1923         SV_Physics ();
1924         sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1925         SV_Physics ();
1926
1927         Mod_PurgeUnused();
1928
1929 #ifdef QUAKEENTITIES
1930 // create a baseline for more efficient communications
1931         SV_CreateBaseline ();
1932 #endif
1933
1934 // send serverinfo to all connected clients
1935         for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1936                 if (host_client->active)
1937                         SV_SendServerinfo (host_client);
1938
1939         Con_DPrintf ("Server spawned.\n");
1940         NET_Heartbeat (2);
1941 }
1942