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