]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_main.c
Updated DSP file. Fixed a bunch of warnings with MSVC6
[divverent/darkplaces.git] / sv_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // sv_main.c -- server main program
21
22 #include "quakedef.h"
23
24 static cvar_t sv_pvscheckentities = {0, "sv_pvscheckentities", "1"};
25 static cvar_t sv_vischeckentities = {0, "sv_vischeckentities", "0"}; // extremely accurate visibility checking, but too slow
26 static cvar_t sv_reportvischeckentities = {0, "sv_reportvischeckentities", "0"};
27 static int sv_vischeckentitycullcount = 0;
28
29 server_t                sv;
30 server_static_t svs;
31
32 static char localmodels[MAX_MODELS][5];                 // inline model names for precache
33
34 static mempool_t *sv_edicts_mempool = NULL;
35
36 //============================================================================
37
38 /*
39 ===============
40 SV_Init
41 ===============
42 */
43 void SV_Init (void)
44 {
45         int i;
46
47         Cvar_RegisterVariable (&sv_maxvelocity);
48         Cvar_RegisterVariable (&sv_gravity);
49         Cvar_RegisterVariable (&sv_friction);
50         Cvar_RegisterVariable (&sv_edgefriction);
51         Cvar_RegisterVariable (&sv_stopspeed);
52         Cvar_RegisterVariable (&sv_maxspeed);
53         Cvar_RegisterVariable (&sv_accelerate);
54         Cvar_RegisterVariable (&sv_idealpitchscale);
55         Cvar_RegisterVariable (&sv_aim);
56         Cvar_RegisterVariable (&sv_nostep);
57         Cvar_RegisterVariable (&sv_predict);
58         Cvar_RegisterVariable (&sv_deltacompress);
59         Cvar_RegisterVariable (&sv_pvscheckentities);
60         Cvar_RegisterVariable (&sv_vischeckentities);
61         Cvar_RegisterVariable (&sv_reportvischeckentities);
62
63         for (i = 0;i < MAX_MODELS;i++)
64                 sprintf (localmodels[i], "*%i", i);
65
66         sv_edicts_mempool = Mem_AllocPool("edicts");
67 }
68
69 /*
70 =============================================================================
71
72 EVENT MESSAGES
73
74 =============================================================================
75 */
76
77 /*
78 ==================
79 SV_StartParticle
80
81 Make sure the event gets sent to all clients
82 ==================
83 */
84 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
85 {
86         int             i, v;
87
88         if (sv.datagram.cursize > MAX_DATAGRAM-16)
89                 return;
90         MSG_WriteByte (&sv.datagram, svc_particle);
91         MSG_WriteFloatCoord (&sv.datagram, org[0]);
92         MSG_WriteFloatCoord (&sv.datagram, org[1]);
93         MSG_WriteFloatCoord (&sv.datagram, org[2]);
94         for (i=0 ; i<3 ; i++)
95         {
96                 v = dir[i]*16;
97                 if (v > 127)
98                         v = 127;
99                 else if (v < -128)
100                         v = -128;
101                 MSG_WriteChar (&sv.datagram, v);
102         }
103         MSG_WriteByte (&sv.datagram, count);
104         MSG_WriteByte (&sv.datagram, color);
105 }
106
107 /*
108 ==================
109 SV_StartEffect
110
111 Make sure the event gets sent to all clients
112 ==================
113 */
114 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
115 {
116         if (sv.datagram.cursize > MAX_DATAGRAM-18)
117                 return;
118         if (modelindex >= 256 || startframe >= 256)
119         {
120                 MSG_WriteByte (&sv.datagram, svc_effect2);
121                 MSG_WriteFloatCoord (&sv.datagram, org[0]);
122                 MSG_WriteFloatCoord (&sv.datagram, org[1]);
123                 MSG_WriteFloatCoord (&sv.datagram, org[2]);
124                 MSG_WriteShort (&sv.datagram, modelindex);
125                 MSG_WriteShort (&sv.datagram, startframe);
126                 MSG_WriteByte (&sv.datagram, framecount);
127                 MSG_WriteByte (&sv.datagram, framerate);
128         }
129         else
130         {
131                 MSG_WriteByte (&sv.datagram, svc_effect);
132                 MSG_WriteFloatCoord (&sv.datagram, org[0]);
133                 MSG_WriteFloatCoord (&sv.datagram, org[1]);
134                 MSG_WriteFloatCoord (&sv.datagram, org[2]);
135                 MSG_WriteByte (&sv.datagram, modelindex);
136                 MSG_WriteByte (&sv.datagram, startframe);
137                 MSG_WriteByte (&sv.datagram, framecount);
138                 MSG_WriteByte (&sv.datagram, framerate);
139         }
140 }
141
142 /*
143 ==================
144 SV_StartSound
145
146 Each entity can have eight independant sound sources, like voice,
147 weapon, feet, etc.
148
149 Channel 0 is an auto-allocate channel, the others override anything
150 already running on that entity/channel pair.
151
152 An attenuation of 0 will play full volume everywhere in the level.
153 Larger attenuations will drop off.  (max 4 attenuation)
154
155 ==================
156 */
157 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
158     float attenuation)
159 {
160     int         sound_num;
161     int field_mask;
162     int                 i;
163         int                     ent;
164
165         if (volume < 0 || volume > 255)
166                 Host_Error ("SV_StartSound: volume = %i", volume);
167
168         if (attenuation < 0 || attenuation > 4)
169                 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
170
171         if (channel < 0 || channel > 7)
172                 Host_Error ("SV_StartSound: channel = %i", channel);
173
174         if (sv.datagram.cursize > MAX_DATAGRAM-16)
175                 return;
176
177 // find precache number for sound
178     for (sound_num=1 ; sound_num<MAX_SOUNDS
179         && sv.sound_precache[sound_num] ; sound_num++)
180         if (!strcmp(sample, sv.sound_precache[sound_num]))
181             break;
182
183     if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
184     {
185         Con_Printf ("SV_StartSound: %s not precached\n", sample);
186         return;
187     }
188
189         ent = NUM_FOR_EDICT(entity);
190
191         channel = (ent<<3) | channel;
192
193         field_mask = 0;
194         if (volume != DEFAULT_SOUND_PACKET_VOLUME)
195                 field_mask |= SND_VOLUME;
196         if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
197                 field_mask |= SND_ATTENUATION;
198
199 // directed messages go only to the entity they are targeted on
200         if (sound_num >= 256)
201                 MSG_WriteByte (&sv.datagram, svc_sound2);
202         else
203                 MSG_WriteByte (&sv.datagram, svc_sound);
204         MSG_WriteByte (&sv.datagram, field_mask);
205         if (field_mask & SND_VOLUME)
206                 MSG_WriteByte (&sv.datagram, volume);
207         if (field_mask & SND_ATTENUATION)
208                 MSG_WriteByte (&sv.datagram, attenuation*64);
209         MSG_WriteShort (&sv.datagram, channel);
210         if (sound_num >= 256)
211                 MSG_WriteShort (&sv.datagram, sound_num);
212         else
213                 MSG_WriteByte (&sv.datagram, sound_num);
214         for (i=0 ; i<3 ; i++)
215                 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
216 }
217
218 /*
219 ==============================================================================
220
221 CLIENT SPAWNING
222
223 ==============================================================================
224 */
225
226 /*
227 ================
228 SV_SendServerinfo
229
230 Sends the first message from the server to a connected client.
231 This will be sent on the initial connection and upon each server load.
232 ================
233 */
234 void SV_SendServerinfo (client_t *client)
235 {
236         char                    **s;
237         char                    message[2048];
238
239         MSG_WriteByte (&client->message, svc_print);
240         sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
241         MSG_WriteString (&client->message,message);
242
243         MSG_WriteByte (&client->message, svc_serverinfo);
244         MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
245         MSG_WriteByte (&client->message, svs.maxclients);
246
247         if (!coop.integer && deathmatch.integer)
248                 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
249         else
250                 MSG_WriteByte (&client->message, GAME_COOP);
251
252         sprintf (message, pr_strings+sv.edicts->v.message);
253
254         MSG_WriteString (&client->message,message);
255
256         for (s = sv.model_precache+1 ; *s ; s++)
257                 MSG_WriteString (&client->message, *s);
258         MSG_WriteByte (&client->message, 0);
259
260         for (s = sv.sound_precache+1 ; *s ; s++)
261                 MSG_WriteString (&client->message, *s);
262         MSG_WriteByte (&client->message, 0);
263
264 // send music
265         MSG_WriteByte (&client->message, svc_cdtrack);
266         MSG_WriteByte (&client->message, sv.edicts->v.sounds);
267         MSG_WriteByte (&client->message, sv.edicts->v.sounds);
268
269 // set view
270         MSG_WriteByte (&client->message, svc_setview);
271         MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
272
273         MSG_WriteByte (&client->message, svc_signonnum);
274         MSG_WriteByte (&client->message, 1);
275
276         client->sendsignon = true;
277         client->spawned = false;                // need prespawn, spawn, etc
278 }
279
280 /*
281 ================
282 SV_ConnectClient
283
284 Initializes a client_t for a new net connection.  This will only be called
285 once for a player each game, not once for each level change.
286 ================
287 */
288 void SV_ConnectClient (int clientnum)
289 {
290         edict_t                 *ent;
291         client_t                *client;
292         int                             edictnum;
293         struct qsocket_s *netconnection;
294         int                             i;
295         float                   spawn_parms[NUM_SPAWN_PARMS];
296
297         client = svs.clients + clientnum;
298
299         Con_DPrintf ("Client %s connected\n", client->netconnection->address);
300
301         edictnum = clientnum+1;
302
303         ent = EDICT_NUM(edictnum);
304
305 // set up the client_t
306         netconnection = client->netconnection;
307
308         if (sv.loadgame)
309                 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
310         memset (client, 0, sizeof(*client));
311         client->netconnection = netconnection;
312
313         strcpy (client->name, "unconnected");
314         client->active = true;
315         client->spawned = false;
316         client->edict = ent;
317         client->message.data = client->msgbuf;
318         client->message.maxsize = sizeof(client->msgbuf);
319         client->message.allowoverflow = true;           // we can catch it
320
321         if (sv.loadgame)
322                 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
323         else
324         {
325         // call the progs to get default spawn parms for the new client
326                 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
327                 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
328                         client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
329         }
330
331         SV_SendServerinfo (client);
332 }
333
334
335 /*
336 ===================
337 SV_CheckForNewClients
338
339 ===================
340 */
341 void SV_CheckForNewClients (void)
342 {
343         struct qsocket_s        *ret;
344         int                             i;
345
346 //
347 // check for new connections
348 //
349         while (1)
350         {
351                 ret = NET_CheckNewConnections ();
352                 if (!ret)
353                         break;
354
355         //
356         // init a new client structure
357         //
358                 for (i=0 ; i<svs.maxclients ; i++)
359                         if (!svs.clients[i].active)
360                                 break;
361                 if (i == svs.maxclients)
362                         Sys_Error ("Host_CheckForNewClients: no free clients");
363
364                 svs.clients[i].netconnection = ret;
365                 SV_ConnectClient (i);
366
367                 net_activeconnections++;
368         }
369 }
370
371
372
373 /*
374 ===============================================================================
375
376 FRAME UPDATES
377
378 ===============================================================================
379 */
380
381 /*
382 ==================
383 SV_ClearDatagram
384
385 ==================
386 */
387 void SV_ClearDatagram (void)
388 {
389         SZ_Clear (&sv.datagram);
390 }
391
392 /*
393 =============================================================================
394
395 The PVS must include a small area around the client to allow head bobbing
396 or other small motion on the client side.  Otherwise, a bob might cause an
397 entity that should be visible to not show up, especially when the bob
398 crosses a waterline.
399
400 =============================================================================
401 */
402
403 int             fatbytes;
404 byte    fatpvs[MAX_MAP_LEAFS/8];
405
406 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
407 {
408         int             i;
409         byte    *pvs;
410         mplane_t        *plane;
411         float   d;
412
413         while (1)
414         {
415         // if this is a leaf, accumulate the pvs bits
416                 if (node->contents < 0)
417                 {
418                         if (node->contents != CONTENTS_SOLID)
419                         {
420                                 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
421                                 for (i=0 ; i<fatbytes ; i++)
422                                         fatpvs[i] |= pvs[i];
423                         }
424                         return;
425                 }
426
427                 plane = node->plane;
428                 d = DotProduct (org, plane->normal) - plane->dist;
429                 if (d > 8)
430                         node = node->children[0];
431                 else if (d < -8)
432                         node = node->children[1];
433                 else
434                 {       // go down both
435                         SV_AddToFatPVS (org, node->children[0]);
436                         node = node->children[1];
437                 }
438         }
439 }
440
441 /*
442 =============
443 SV_FatPVS
444
445 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
446 given point.
447 =============
448 */
449 byte *SV_FatPVS (vec3_t org)
450 {
451         fatbytes = (sv.worldmodel->numleafs+31)>>3;
452         memset (fatpvs, 0, fatbytes);
453         SV_AddToFatPVS (org, sv.worldmodel->nodes);
454         return fatpvs;
455 }
456
457 //=============================================================================
458
459
460 int SV_BoxTouchingPVS (byte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
461 {
462         int leafnum;
463 loc0:
464         if (node->contents < 0)
465         {
466                 // leaf
467                 if (node->contents == CONTENTS_SOLID)
468                         return false;
469                 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
470                 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
471         }
472
473         // node - recurse down the BSP tree
474         switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
475         {
476         case 1: // front
477                 node = node->children[0];
478                 goto loc0;
479         case 2: // back
480                 node = node->children[1];
481                 goto loc0;
482         default: // crossing
483                 if (node->children[0]->contents != CONTENTS_SOLID)
484                         if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
485                                 return true;
486                 node = node->children[1];
487                 goto loc0;
488         }
489         // never reached
490         return false;
491 }
492
493
494 /*
495 =============
496 SV_WriteEntitiesToClient
497
498 =============
499 */
500 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
501 {
502         int             e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
503         byte    *pvs;
504         vec3_t  org, origin, angles, entmins, entmaxs;
505         float   nextfullupdate;
506         edict_t *ent;
507         eval_t  *val;
508         entity_state_t  *baseline; // LordHavoc: delta or startup baseline
509         trace_t trace;
510         model_t *model;
511
512         Mod_CheckLoaded(sv.worldmodel);
513
514 // find the client's PVS
515         VectorAdd (clent->v.origin, clent->v.view_ofs, org);
516         pvs = SV_FatPVS (org);
517         /*
518         // dp protocol
519         MSG_WriteByte(msg, svc_playerposition);
520         MSG_WriteFloat(msg, org[0]);
521         MSG_WriteFloat(msg, org[1]);
522         MSG_WriteFloat(msg, org[2]);
523         */
524
525         clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
526         // send all entities that touch the pvs
527         ent = NEXT_EDICT(sv.edicts);
528         for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
529         {
530                 bits = 0;
531
532                 // prevent delta compression against this frame (unless actually sent, which will restore this later)
533                 nextfullupdate = client->nextfullupdate[e];
534                 client->nextfullupdate[e] = -1;
535
536                 if (ent != clent) // LordHavoc: always send player
537                 {
538                         if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
539                         {
540                                 if (val->edict != clentnum)
541                                 {
542                                         // don't show to anyone else
543                                         continue;
544                                 }
545                                 else
546                                         bits |= U_VIEWMODEL; // show relative to the view
547                         }
548                         else
549                         {
550                                 // LordHavoc: never draw something told not to display to this client
551                                 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
552                                         continue;
553                                 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
554                                         continue;
555                         }
556                 }
557
558                 glowsize = 0;
559
560                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
561                         glowsize = (int) val->_float >> 2;
562                 if (glowsize > 255) glowsize = 255;
563                 if (glowsize < 0) glowsize = 0;
564
565                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
566                 if (val->_float != 0)
567                         bits |= U_GLOWTRAIL;
568
569                 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
570                 {
571                         model = sv.models[(int)ent->v.modelindex];
572                         Mod_CheckLoaded(model);
573                 }
574                 else
575                 {
576                         model = NULL;
577                         if (ent != clent) // LordHavoc: always send player
578                                 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
579                                         continue;
580                 }
581
582                 VectorCopy(ent->v.angles, angles);
583                 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
584                 {
585                         VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
586                         // LordHavoc: trace predicted movement to avoid putting things in walls
587                         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
588                         VectorCopy(trace.endpos, origin);
589                 }
590                 else
591                 {
592                         VectorCopy(ent->v.origin, origin);
593                 }
594
595                 // ent has survived every check so far, check if it is visible
596                 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
597                 {
598                         // use the predicted origin
599                         entmins[0] = origin[0] - 1.0f;
600                         entmins[1] = origin[1] - 1.0f;
601                         entmins[2] = origin[2] - 1.0f;
602                         entmaxs[0] = origin[0] + 1.0f;
603                         entmaxs[1] = origin[1] + 1.0f;
604                         entmaxs[2] = origin[2] + 1.0f;
605                         // using the model's bounding box to ensure things are visible regardless of their physics box
606                         if (model)
607                         {
608                                 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
609                                 {
610                                         VectorAdd(entmins, model->rotatedmins, entmins);
611                                         VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
612                                 }
613                                 else if (ent->v.angles[1])
614                                 {
615                                         VectorAdd(entmins, model->yawmins, entmins);
616                                         VectorAdd(entmaxs, model->yawmaxs, entmaxs);
617                                 }
618                                 else
619                                 {
620                                         VectorAdd(entmins, model->normalmins, entmins);
621                                         VectorAdd(entmaxs, model->normalmaxs, entmaxs);
622                                 }
623                         }
624
625                         // if not touching a visible leaf
626                         if (sv_pvscheckentities.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
627                                 continue;
628
629                         // or not visible through the portals
630                         if (sv_vischeckentities.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
631                         {
632                                 sv_vischeckentitycullcount++;
633                                 continue;
634                         }
635                 }
636
637                 alpha = 255;
638                 scale = 16;
639                 glowcolor = 254;
640                 effects = ent->v.effects;
641
642                 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
643                 if (val->_float != 0)
644                         alpha = (int) (val->_float * 255.0);
645
646                 // HalfLife support
647                 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
648                 if (val->_float != 0)
649                         alpha = (int) val->_float;
650
651                 if (alpha == 0)
652                         alpha = 255;
653                 alpha = bound(0, alpha, 255);
654
655                 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
656                 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
657                 if (scale < 0) scale = 0;
658                 if (scale > 255) scale = 255;
659
660                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
661                 if (val->_float != 0)
662                         glowcolor = (int) val->_float;
663
664                 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
665                 if (val->_float != 0)
666                         effects |= EF_FULLBRIGHT;
667
668                 if (ent != clent)
669                 {
670                         if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
671                         {
672                                 if (model) // model
673                                 {
674                                         // don't send if flagged for NODRAW and there are no effects
675                                         if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
676                                                 continue;
677                                 }
678                                 else // no model and no effects
679                                         continue;
680                         }
681                 }
682
683                 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
684                 {
685                         Con_Printf ("packet overflow\n");
686                         // mark the rest of the entities so they can't be delta compressed against this frame
687                         for (;e < sv.num_edicts;e++)
688                                 client->nextfullupdate[e] = -1;
689                         return;
690                 }
691
692                 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
693                         bits = bits | U_EXTERIORMODEL;
694
695 // send an update
696                 baseline = &ent->baseline;
697
698                 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.integer)
699                 {
700                         // every half second a full update is forced
701                         if (realtime < client->nextfullupdate[e])
702                         {
703                                 bits |= U_DELTA;
704                                 baseline = &ent->deltabaseline;
705                         }
706                         else
707                                 nextfullupdate = realtime + 0.5f;
708                 }
709                 else
710                         nextfullupdate = realtime + 0.5f;
711
712                 // restore nextfullupdate since this is being sent for real
713                 client->nextfullupdate[e] = nextfullupdate;
714
715                 if (e >= 256)
716                         bits |= U_LONGENTITY;
717
718                 if (ent->v.movetype == MOVETYPE_STEP)
719                         bits |= U_STEP;
720
721                 // LordHavoc: old stuff, but rewritten to have more exact tolerances
722 //              if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0))                                             bits |= U_ORIGIN1;
723 //              if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0))                                             bits |= U_ORIGIN2;
724 //              if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0))                                             bits |= U_ORIGIN3;
725                 if (origin[0] != baseline->origin[0])                                                                                   bits |= U_ORIGIN1;
726                 if (origin[1] != baseline->origin[1])                                                                                   bits |= U_ORIGIN2;
727                 if (origin[2] != baseline->origin[2])                                                                                   bits |= U_ORIGIN3;
728                 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
729                 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
730                 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
731                 if (baseline->colormap != (byte) ent->v.colormap)                                                               bits |= U_COLORMAP;
732                 if (baseline->skin != (byte) ent->v.skin)                                                                               bits |= U_SKIN;
733                 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF))                                bits |= U_FRAME;
734                 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF))                    bits |= U_EFFECTS;
735                 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF))              bits |= U_MODEL;
736
737                 // LordHavoc: new stuff
738                 if (baseline->alpha != alpha)                                                                                                   bits |= U_ALPHA;
739                 if (baseline->scale != scale)                                                                                                   bits |= U_SCALE;
740                 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00))              bits |= U_EFFECTS2;
741                 if (baseline->glowsize != glowsize)                                                                                             bits |= U_GLOWSIZE;
742                 if (baseline->glowcolor != glowcolor)                                                                                   bits |= U_GLOWCOLOR;
743                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00))                  bits |= U_FRAME2;
744                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00))             bits |= U_MODEL2;
745
746                 // update delta baseline
747                 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
748                 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
749                 ent->deltabaseline.colormap = ent->v.colormap;
750                 ent->deltabaseline.skin = ent->v.skin;
751                 ent->deltabaseline.frame = ent->v.frame;
752                 ent->deltabaseline.effects = ent->v.effects;
753                 ent->deltabaseline.modelindex = ent->v.modelindex;
754                 ent->deltabaseline.alpha = alpha;
755                 ent->deltabaseline.scale = scale;
756                 ent->deltabaseline.glowsize = glowsize;
757                 ent->deltabaseline.glowcolor = glowcolor;
758
759                 // write the message
760                 if (bits >= 16777216)
761                         bits |= U_EXTEND2;
762                 if (bits >= 65536)
763                         bits |= U_EXTEND1;
764                 if (bits >= 256)
765                         bits |= U_MOREBITS;
766                 bits |= U_SIGNAL;
767
768                 MSG_WriteByte (msg, bits);
769                 if (bits & U_MOREBITS)
770                         MSG_WriteByte (msg, bits>>8);
771                 // LordHavoc: extend bytes have to be written here due to delta compression
772                 if (bits & U_EXTEND1)
773                         MSG_WriteByte (msg, bits>>16);
774                 if (bits & U_EXTEND2)
775                         MSG_WriteByte (msg, bits>>24);
776
777                 // LordHavoc: old stuff
778                 if (bits & U_LONGENTITY)
779                         MSG_WriteShort (msg,e);
780                 else
781                         MSG_WriteByte (msg,e);
782                 if (bits & U_MODEL)             MSG_WriteByte (msg,     ent->v.modelindex);
783                 if (bits & U_FRAME)             MSG_WriteByte (msg, ent->v.frame);
784                 if (bits & U_COLORMAP)  MSG_WriteByte (msg, ent->v.colormap);
785                 if (bits & U_SKIN)              MSG_WriteByte (msg, ent->v.skin);
786                 if (bits & U_EFFECTS)   MSG_WriteByte (msg, ent->v.effects);
787                 if (bits & U_ORIGIN1)   MSG_WriteFloatCoord (msg, origin[0]);
788                 if (bits & U_ANGLE1)    MSG_WriteAngle(msg, angles[0]);
789                 if (bits & U_ORIGIN2)   MSG_WriteFloatCoord (msg, origin[1]);
790                 if (bits & U_ANGLE2)    MSG_WriteAngle(msg, angles[1]);
791                 if (bits & U_ORIGIN3)   MSG_WriteFloatCoord (msg, origin[2]);
792                 if (bits & U_ANGLE3)    MSG_WriteAngle(msg, angles[2]);
793
794                 // LordHavoc: new stuff
795                 if (bits & U_ALPHA)             MSG_WriteByte(msg, alpha);
796                 if (bits & U_SCALE)             MSG_WriteByte(msg, scale);
797                 if (bits & U_EFFECTS2)  MSG_WriteByte(msg, (int)ent->v.effects >> 8);
798                 if (bits & U_GLOWSIZE)  MSG_WriteByte(msg, glowsize);
799                 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
800                 if (bits & U_FRAME2)    MSG_WriteByte(msg, (int)ent->v.frame >> 8);
801                 if (bits & U_MODEL2)    MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
802         }
803
804         if (sv_reportvischeckentities.integer)
805                 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
806         sv_vischeckentitycullcount = 0;
807 }
808
809 /*
810 =============
811 SV_CleanupEnts
812
813 =============
814 */
815 void SV_CleanupEnts (void)
816 {
817         int             e;
818         edict_t *ent;
819
820         ent = NEXT_EDICT(sv.edicts);
821         for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
822                 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
823 }
824
825 /*
826 ==================
827 SV_WriteClientdataToMessage
828
829 ==================
830 */
831 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
832 {
833         int             bits;
834         int             i;
835         edict_t *other;
836         int             items;
837         eval_t  *val;
838         vec3_t  punchvector;
839
840 //
841 // send a damage message
842 //
843         if (ent->v.dmg_take || ent->v.dmg_save)
844         {
845                 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
846                 MSG_WriteByte (msg, svc_damage);
847                 MSG_WriteByte (msg, ent->v.dmg_save);
848                 MSG_WriteByte (msg, ent->v.dmg_take);
849                 for (i=0 ; i<3 ; i++)
850                         MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
851
852                 ent->v.dmg_take = 0;
853                 ent->v.dmg_save = 0;
854         }
855
856 //
857 // send the current viewpos offset from the view entity
858 //
859         SV_SetIdealPitch ();            // how much to look up / down ideally
860
861 // a fixangle might get lost in a dropped packet.  Oh well.
862         if ( ent->v.fixangle )
863         {
864                 MSG_WriteByte (msg, svc_setangle);
865                 for (i=0 ; i < 3 ; i++)
866                         MSG_WriteAngle (msg, ent->v.angles[i] );
867                 ent->v.fixangle = 0;
868         }
869
870         bits = 0;
871
872         if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
873                 bits |= SU_VIEWHEIGHT;
874
875         if (ent->v.idealpitch)
876                 bits |= SU_IDEALPITCH;
877
878 // stuff the sigil bits into the high bits of items for sbar, or else
879 // mix in items2
880         val = GETEDICTFIELDVALUE(ent, eval_items2);
881
882         if (val)
883                 items = (int)ent->v.items | ((int)val->_float << 23);
884         else
885                 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
886
887         bits |= SU_ITEMS;
888
889         if ( (int)ent->v.flags & FL_ONGROUND)
890                 bits |= SU_ONGROUND;
891
892         if ( ent->v.waterlevel >= 2)
893                 bits |= SU_INWATER;
894
895         // dpprotocol
896         VectorClear(punchvector);
897         if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
898                 VectorCopy(val->vector, punchvector);
899
900         for (i=0 ; i<3 ; i++)
901         {
902                 if (ent->v.punchangle[i])
903                         bits |= (SU_PUNCH1<<i);
904                 if (punchvector[i]) // dpprotocol
905                         bits |= (SU_PUNCHVEC1<<i); // dpprotocol
906                 if (ent->v.velocity[i])
907                         bits |= (SU_VELOCITY1<<i);
908         }
909
910         if (ent->v.weaponframe)
911                 bits |= SU_WEAPONFRAME;
912
913         if (ent->v.armorvalue)
914                 bits |= SU_ARMOR;
915
916 //      if (ent->v.weapon)
917                 bits |= SU_WEAPON;
918
919         if (bits >= 65536)
920                 bits |= SU_EXTEND1;
921         if (bits >= 16777216)
922                 bits |= SU_EXTEND2;
923
924 // send the data
925
926         MSG_WriteByte (msg, svc_clientdata);
927         MSG_WriteShort (msg, bits);
928         if (bits & SU_EXTEND1)
929                 MSG_WriteByte(msg, bits >> 16);
930         if (bits & SU_EXTEND2)
931                 MSG_WriteByte(msg, bits >> 24);
932
933         if (bits & SU_VIEWHEIGHT)
934                 MSG_WriteChar (msg, ent->v.view_ofs[2]);
935
936         if (bits & SU_IDEALPITCH)
937                 MSG_WriteChar (msg, ent->v.idealpitch);
938
939         for (i=0 ; i<3 ; i++)
940         {
941                 if (bits & (SU_PUNCH1<<i))
942                         MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
943                 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
944                         MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
945                 if (bits & (SU_VELOCITY1<<i))
946                         MSG_WriteChar (msg, ent->v.velocity[i]/16);
947         }
948
949 // [always sent]        if (bits & SU_ITEMS)
950         MSG_WriteLong (msg, items);
951
952         if (bits & SU_WEAPONFRAME)
953                 MSG_WriteByte (msg, ent->v.weaponframe);
954         if (bits & SU_ARMOR)
955                 MSG_WriteByte (msg, ent->v.armorvalue);
956         if (bits & SU_WEAPON)
957                 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
958
959         MSG_WriteShort (msg, ent->v.health);
960         MSG_WriteByte (msg, ent->v.currentammo);
961         MSG_WriteByte (msg, ent->v.ammo_shells);
962         MSG_WriteByte (msg, ent->v.ammo_nails);
963         MSG_WriteByte (msg, ent->v.ammo_rockets);
964         MSG_WriteByte (msg, ent->v.ammo_cells);
965
966         if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
967         {
968                 for(i=0;i<32;i++)
969                 {
970                         if ( ((int)ent->v.weapon) & (1<<i) )
971                         {
972                                 MSG_WriteByte (msg, i);
973                                 break;
974                         }
975                 }
976         }
977         else
978         {
979                 MSG_WriteByte (msg, ent->v.weapon);
980         }
981 }
982
983 /*
984 =======================
985 SV_SendClientDatagram
986 =======================
987 */
988 qboolean SV_SendClientDatagram (client_t *client)
989 {
990         byte            buf[MAX_DATAGRAM];
991         sizebuf_t       msg;
992
993         msg.data = buf;
994         msg.maxsize = sizeof(buf);
995         msg.cursize = 0;
996
997         MSG_WriteByte (&msg, svc_time);
998         MSG_WriteFloat (&msg, sv.time);
999
1000 // add the client specific data to the datagram
1001         SV_WriteClientdataToMessage (client->edict, &msg);
1002
1003         SV_WriteEntitiesToClient (client, client->edict, &msg);
1004
1005 // copy the server datagram if there is space
1006         if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1007                 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1008
1009 // send the datagram
1010         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1011         {
1012                 SV_DropClient (true);// if the message couldn't send, kick off
1013                 return false;
1014         }
1015
1016         return true;
1017 }
1018
1019 /*
1020 =======================
1021 SV_UpdateToReliableMessages
1022 =======================
1023 */
1024 void SV_UpdateToReliableMessages (void)
1025 {
1026         int                     i, j;
1027         client_t *client;
1028
1029 // check for changes to be sent over the reliable streams
1030         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1031         {
1032                 if (host_client->old_frags != host_client->edict->v.frags)
1033                 {
1034                         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1035                         {
1036                                 if (!client->active)
1037                                         continue;
1038                                 MSG_WriteByte (&client->message, svc_updatefrags);
1039                                 MSG_WriteByte (&client->message, i);
1040                                 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1041                         }
1042
1043                         host_client->old_frags = host_client->edict->v.frags;
1044                 }
1045         }
1046
1047         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1048         {
1049                 if (!client->active)
1050                         continue;
1051                 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1052         }
1053
1054         SZ_Clear (&sv.reliable_datagram);
1055 }
1056
1057
1058 /*
1059 =======================
1060 SV_SendNop
1061
1062 Send a nop message without trashing or sending the accumulated client
1063 message buffer
1064 =======================
1065 */
1066 void SV_SendNop (client_t *client)
1067 {
1068         sizebuf_t       msg;
1069         byte            buf[4];
1070
1071         msg.data = buf;
1072         msg.maxsize = sizeof(buf);
1073         msg.cursize = 0;
1074
1075         MSG_WriteChar (&msg, svc_nop);
1076
1077         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1078                 SV_DropClient (true);   // if the message couldn't send, kick off
1079         client->last_message = realtime;
1080 }
1081
1082 /*
1083 =======================
1084 SV_SendClientMessages
1085 =======================
1086 */
1087 void SV_SendClientMessages (void)
1088 {
1089         int                     i;
1090
1091 // update frags, names, etc
1092         SV_UpdateToReliableMessages ();
1093
1094 // build individual updates
1095         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1096         {
1097                 if (!host_client->active)
1098                         continue;
1099
1100                 if (host_client->spawned)
1101                 {
1102                         if (!SV_SendClientDatagram (host_client))
1103                                 continue;
1104                 }
1105                 else
1106                 {
1107                 // the player isn't totally in the game yet
1108                 // send small keepalive messages if too much time has passed
1109                 // send a full message when the next signon stage has been requested
1110                 // some other message data (name changes, etc) may accumulate
1111                 // between signon stages
1112                         if (!host_client->sendsignon)
1113                         {
1114                                 if (realtime - host_client->last_message > 5)
1115                                         SV_SendNop (host_client);
1116                                 continue;       // don't send out non-signon messages
1117                         }
1118                 }
1119
1120                 // check for an overflowed message.  Should only happen
1121                 // on a very fucked up connection that backs up a lot, then
1122                 // changes level
1123                 if (host_client->message.overflowed)
1124                 {
1125                         SV_DropClient (true);
1126                         host_client->message.overflowed = false;
1127                         continue;
1128                 }
1129
1130                 if (host_client->message.cursize || host_client->dropasap)
1131                 {
1132                         if (!NET_CanSendMessage (host_client->netconnection))
1133                         {
1134 //                              I_Printf ("can't write\n");
1135                                 continue;
1136                         }
1137
1138                         if (host_client->dropasap)
1139                                 SV_DropClient (false);  // went to another level
1140                         else
1141                         {
1142                                 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1143                                         SV_DropClient (true);   // if the message couldn't send, kick off
1144                                 SZ_Clear (&host_client->message);
1145                                 host_client->last_message = realtime;
1146                                 host_client->sendsignon = false;
1147                         }
1148                 }
1149         }
1150
1151
1152 // clear muzzle flashes
1153         SV_CleanupEnts ();
1154 }
1155
1156
1157 /*
1158 ==============================================================================
1159
1160 SERVER SPAWNING
1161
1162 ==============================================================================
1163 */
1164
1165 /*
1166 ================
1167 SV_ModelIndex
1168
1169 ================
1170 */
1171 int SV_ModelIndex (char *name)
1172 {
1173         int             i;
1174
1175         if (!name || !name[0])
1176                 return 0;
1177
1178         for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1179                 if (!strcmp(sv.model_precache[i], name))
1180                         return i;
1181         if (i==MAX_MODELS || !sv.model_precache[i])
1182                 Host_Error ("SV_ModelIndex: model %s not precached", name);
1183         return i;
1184 }
1185
1186 /*
1187 ================
1188 SV_CreateBaseline
1189
1190 ================
1191 */
1192 void SV_CreateBaseline (void)
1193 {
1194         int i, entnum, large;
1195         edict_t *svent;
1196
1197         // LordHavoc: clear *all* states (note just active ones)
1198         for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1199         {
1200                 // get the current server version
1201                 svent = EDICT_NUM(entnum);
1202
1203                 // LordHavoc: always clear state values, whether the entity is in use or not
1204                 ClearStateToDefault(&svent->baseline);
1205
1206                 if (svent->free)
1207                         continue;
1208                 if (entnum > svs.maxclients && !svent->v.modelindex)
1209                         continue;
1210
1211                 // create entity baseline
1212                 VectorCopy (svent->v.origin, svent->baseline.origin);
1213                 VectorCopy (svent->v.angles, svent->baseline.angles);
1214                 svent->baseline.frame = svent->v.frame;
1215                 svent->baseline.skin = svent->v.skin;
1216                 if (entnum > 0 && entnum <= svs.maxclients)
1217                 {
1218                         svent->baseline.colormap = entnum;
1219                         svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1220                 }
1221                 else
1222                 {
1223                         svent->baseline.colormap = 0;
1224                         svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1225                 }
1226
1227                 large = false;
1228                 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1229                         large = true;
1230
1231                 // add to the message
1232                 if (large)
1233                         MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1234                 else
1235                         MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1236                 MSG_WriteShort (&sv.signon, entnum);
1237
1238                 if (large)
1239                 {
1240                         MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1241                         MSG_WriteShort (&sv.signon, svent->baseline.frame);
1242                 }
1243                 else
1244                 {
1245                         MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1246                         MSG_WriteByte (&sv.signon, svent->baseline.frame);
1247                 }
1248                 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1249                 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1250                 for (i=0 ; i<3 ; i++)
1251                 {
1252                         MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1253                         MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1254                 }
1255         }
1256 }
1257
1258
1259 /*
1260 ================
1261 SV_SendReconnect
1262
1263 Tell all the clients that the server is changing levels
1264 ================
1265 */
1266 void SV_SendReconnect (void)
1267 {
1268         char    data[128];
1269         sizebuf_t       msg;
1270
1271         msg.data = data;
1272         msg.cursize = 0;
1273         msg.maxsize = sizeof(data);
1274
1275         MSG_WriteChar (&msg, svc_stufftext);
1276         MSG_WriteString (&msg, "reconnect\n");
1277         NET_SendToAll (&msg, 5);
1278         
1279         if (cls.state != ca_dedicated)
1280                 Cmd_ExecuteString ("reconnect\n", src_command);
1281 }
1282
1283
1284 /*
1285 ================
1286 SV_SaveSpawnparms
1287
1288 Grabs the current state of each client for saving across the
1289 transition to another level
1290 ================
1291 */
1292 void SV_SaveSpawnparms (void)
1293 {
1294         int             i, j;
1295
1296         svs.serverflags = pr_global_struct->serverflags;
1297
1298         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1299         {
1300                 if (!host_client->active)
1301                         continue;
1302
1303         // call the progs to get default spawn parms for the new client
1304                 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1305                 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1306                 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1307                         host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1308         }
1309 }
1310
1311 /*
1312 ================
1313 SV_SpawnServer
1314
1315 This is called at the start of each level
1316 ================
1317 */
1318 extern float            scr_centertime_off;
1319
1320 void SV_SpawnServer (char *server)
1321 {
1322         edict_t         *ent;
1323         int                     i;
1324
1325         // let's not have any servers with no name
1326         if (hostname.string[0] == 0)
1327                 Cvar_Set ("hostname", "UNNAMED");
1328         scr_centertime_off = 0;
1329
1330         Con_DPrintf ("SpawnServer: %s\n",server);
1331         svs.changelevel_issued = false;         // now safe to issue another
1332
1333 //
1334 // tell all connected clients that we are going to a new level
1335 //
1336         if (sv.active)
1337                 SV_SendReconnect ();
1338
1339 //
1340 // make cvars consistant
1341 //
1342         if (coop.integer)
1343                 Cvar_SetValue ("deathmatch", 0);
1344         current_skill = bound(0, (int)(skill.value + 0.5), 3);
1345
1346         Cvar_SetValue ("skill", (float)current_skill);
1347
1348 //
1349 // set up the new server
1350 //
1351         Host_ClearMemory ();
1352
1353         memset (&sv, 0, sizeof(sv));
1354
1355         strcpy (sv.name, server);
1356
1357 // load progs to get entity field count
1358         PR_LoadProgs ();
1359
1360 // allocate server memory
1361         sv.max_edicts = MAX_EDICTS;
1362
1363         // clear the edict memory pool
1364         Mem_EmptyPool(sv_edicts_mempool);
1365         sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1366
1367         sv.datagram.maxsize = sizeof(sv.datagram_buf);
1368         sv.datagram.cursize = 0;
1369         sv.datagram.data = sv.datagram_buf;
1370
1371         sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1372         sv.reliable_datagram.cursize = 0;
1373         sv.reliable_datagram.data = sv.reliable_datagram_buf;
1374
1375         sv.signon.maxsize = sizeof(sv.signon_buf);
1376         sv.signon.cursize = 0;
1377         sv.signon.data = sv.signon_buf;
1378
1379 // leave slots at start for clients only
1380         sv.num_edicts = svs.maxclients+1;
1381         for (i=0 ; i<svs.maxclients ; i++)
1382         {
1383                 ent = EDICT_NUM(i+1);
1384                 svs.clients[i].edict = ent;
1385         }
1386
1387         sv.state = ss_loading;
1388         sv.paused = false;
1389
1390         sv.time = 1.0;
1391
1392         Mod_ClearUsed();
1393
1394         strcpy (sv.name, server);
1395         sprintf (sv.modelname,"maps/%s.bsp", server);
1396         sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1397         if (!sv.worldmodel)
1398         {
1399                 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1400                 sv.active = false;
1401                 return;
1402         }
1403         sv.models[1] = sv.worldmodel;
1404
1405 //
1406 // clear world interaction links
1407 //
1408         SV_ClearWorld ();
1409
1410         sv.sound_precache[0] = pr_strings;
1411
1412         sv.model_precache[0] = pr_strings;
1413         sv.model_precache[1] = sv.modelname;
1414         for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1415         {
1416                 sv.model_precache[i+1] = localmodels[i];
1417                 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1418         }
1419
1420 //
1421 // load the rest of the entities
1422 //
1423         ent = EDICT_NUM(0);
1424         memset (&ent->v, 0, progs->entityfields * 4);
1425         ent->free = false;
1426         ent->v.model = sv.worldmodel->name - pr_strings;
1427         ent->v.modelindex = 1;          // world model
1428         ent->v.solid = SOLID_BSP;
1429         ent->v.movetype = MOVETYPE_PUSH;
1430
1431         if (coop.integer)
1432                 pr_global_struct->coop = coop.integer;
1433         else
1434                 pr_global_struct->deathmatch = deathmatch.integer;
1435
1436         pr_global_struct->mapname = sv.name - pr_strings;
1437
1438 // serverflags are for cross level information (sigils)
1439         pr_global_struct->serverflags = svs.serverflags;
1440         
1441         ED_LoadFromFile (sv.worldmodel->entities);
1442         // LordHavoc: clear world angles (to fix e3m3.bsp)
1443         VectorClear(sv.edicts->v.angles);
1444
1445         sv.active = true;
1446
1447 // all setup is completed, any further precache statements are errors
1448         sv.state = ss_active;
1449
1450 // run two frames to allow everything to settle
1451         sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1452         SV_Physics ();
1453         SV_Physics ();
1454
1455         Mod_PurgeUnused();
1456
1457 // create a baseline for more efficient communications
1458         SV_CreateBaseline ();
1459
1460 // send serverinfo to all connected clients
1461         for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1462                 if (host_client->active)
1463                         SV_SendServerinfo (host_client);
1464
1465         Con_DPrintf ("Server spawned.\n");
1466 }