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