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