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