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