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