]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_main.c
forgot to add this
[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", "0"}; // extremely accurate visibility checking, but too slow
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, "\002\nServer: %s build %i (progs %i crc)", gamename, 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         model_t *model;
507
508 // find the client's PVS
509         VectorAdd (clent->v.origin, clent->v.view_ofs, org);
510         pvs = SV_FatPVS (org);
511         /*
512         // dp protocol
513         MSG_WriteByte(msg, svc_playerposition);
514         MSG_WriteFloat(msg, org[0]);
515         MSG_WriteFloat(msg, org[1]);
516         MSG_WriteFloat(msg, org[2]);
517         */
518
519         clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
520         // send all entities that touch the pvs
521         ent = NEXT_EDICT(sv.edicts);
522         for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
523         {
524                 bits = 0;
525
526                 // prevent delta compression against this frame (unless actually sent, which will restore this later)
527                 nextfullupdate = client->nextfullupdate[e];
528                 client->nextfullupdate[e] = -1;
529
530                 if (ent != clent) // LordHavoc: always send player
531                 {
532                         if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
533                         {
534                                 if (val->edict != clentnum)
535                                 {
536                                         // don't show to anyone else
537                                         continue;
538                                 }
539                                 else
540                                         bits |= U_VIEWMODEL; // show relative to the view
541                         }
542                         else
543                         {
544                                 // LordHavoc: never draw something told not to display to this client
545                                 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
546                                         continue;
547                                 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
548                                         continue;
549                         }
550                 }
551
552                 glowsize = 0;
553
554                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
555                         glowsize = (int) val->_float >> 2;
556                 if (glowsize > 255) glowsize = 255;
557                 if (glowsize < 0) glowsize = 0;
558
559                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
560                 if (val->_float != 0)
561                         bits |= U_GLOWTRAIL;
562
563                 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
564                         model = sv.models[(int)ent->v.modelindex];
565                 else
566                 {
567                         model = NULL;
568                         if (ent != clent) // LordHavoc: always send player
569                                 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
570                                         continue;
571                 }
572
573                 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
574                 {
575                         if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
576                         {
577                                 ent->steplerptime = sv.time;
578                                 VectorCopy(ent->v.origin, ent->stepoldorigin);
579                                 VectorCopy(ent->v.angles, ent->stepoldangles);
580                                 VectorCopy(ent->v.origin, ent->steporigin);
581                                 VectorCopy(ent->v.angles, ent->stepangles);
582                         }
583                         VectorSubtract(ent->v.origin, ent->steporigin, origin);
584                         VectorSubtract(ent->v.angles, ent->stepangles, angles);
585                         if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
586                         {
587                                 // update lerp positions
588                                 ent->steplerptime = sv.time;
589                                 VectorCopy(ent->steporigin, ent->stepoldorigin);
590                                 VectorCopy(ent->stepangles, ent->stepoldangles);
591                                 VectorCopy(ent->v.origin, ent->steporigin);
592                                 VectorCopy(ent->v.angles, ent->stepangles);
593                         }
594                         movelerp = (sv.time - ent->steplerptime) * 10.0;
595                         if (movelerp > 1) movelerp = 1;
596                         moveilerp = 1 - movelerp;
597                         origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
598                         origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
599                         origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
600                         // choose shortest rotate (to avoid 'spin around' situations)
601                         VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
602                         if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
603                         if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
604                         if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
605                         angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
606                         angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
607                         angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
608                         //VectorMA(origin, host_client->latency, ent->v.velocity, origin);
609                 }
610                 else // copy as they are
611                 {
612                         if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
613                         {
614                                 // update lerp positions
615                                 ent->steplerptime = sv.time;
616                                 VectorCopy(ent->v.origin, ent->stepoldorigin);
617                                 VectorCopy(ent->v.angles, ent->stepoldangles);
618                                 VectorCopy(ent->v.origin, ent->steporigin);
619                                 VectorCopy(ent->v.angles, ent->stepangles);
620                         }
621
622                         VectorCopy(ent->v.angles, angles);
623                         if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
624                         {
625                                 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
626                                 // LordHavoc: trace predicted movement to avoid putting things in walls
627                                 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
628                                 VectorCopy(trace.endpos, origin);
629                         }
630                         else
631                         {
632                                 VectorCopy(ent->v.origin, origin);
633                         }
634                 }
635
636                 // ent has survived every check so far, check if it is visible
637                 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
638                 {
639                         // use the predicted origin
640                         entmins[0] = origin[0] - 1.0f;
641                         entmins[1] = origin[1] - 1.0f;
642                         entmins[2] = origin[2] - 1.0f;
643                         entmaxs[0] = origin[0] + 1.0f;
644                         entmaxs[1] = origin[1] + 1.0f;
645                         entmaxs[2] = origin[2] + 1.0f;
646                         // using the model's bounding box to ensure things are visible regardless of their physics box
647                         if (model)
648                         {
649                                 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
650                                 {
651                                         VectorAdd(entmins, model->rotatedmins, entmins);
652                                         VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
653                                 }
654                                 else if (ent->v.angles[1])
655                                 {
656                                         VectorAdd(entmins, model->yawmins, entmins);
657                                         VectorAdd(entmaxs, model->yawmaxs, entmaxs);
658                                 }
659                                 else
660                                 {
661                                         VectorAdd(entmins, model->normalmins, entmins);
662                                         VectorAdd(entmaxs, model->normalmaxs, entmaxs);
663                                 }
664                         }
665
666                         // if not touching a visible leaf
667                         if (sv_pvscheckentities.value && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
668                                 continue;
669
670                         // or not visible through the portals
671                         if (sv_vischeckentities.value && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
672                         {
673                                 sv_vischeckentitycullcount++;
674                                 continue;
675                         }
676                 }
677
678                 alpha = 255;
679                 scale = 16;
680                 glowcolor = 254;
681                 colormod = 255;
682                 effects = ent->v.effects;
683
684                 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
685                 if (val->_float != 0)
686                         alpha = (int) (val->_float * 255.0);
687
688                 // HalfLife support
689                 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
690                 if (val->_float != 0)
691                         alpha = (int) val->_float;
692
693                 if (alpha == 0)
694                         alpha = 255;
695                 alpha = bound(0, alpha, 255);
696
697                 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
698                 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
699                 if (scale < 0) scale = 0;
700                 if (scale > 255) scale = 255;
701
702                 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
703                 if (val->_float != 0)
704                         glowcolor = (int) val->_float;
705
706                 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
707                 if (val->_float != 0)
708                         effects |= EF_FULLBRIGHT;
709
710                 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
711                 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
712                         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);
713
714                 if (ent != clent)
715                 {
716                         if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
717                         {
718                                 if (model) // model
719                                 {
720                                         // don't send if flagged for NODRAW and there are no effects
721                                         if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
722                                                 continue;
723                                 }
724                                 else // no model and no effects
725                                         continue;
726                         }
727                 }
728
729                 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
730                 {
731                         Con_Printf ("packet overflow\n");
732                         // mark the rest of the entities so they can't be delta compressed against this frame
733                         for (;e < sv.num_edicts;e++)
734                                 client->nextfullupdate[e] = -1;
735                         return;
736                 }
737
738                 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
739                         bits = bits | U_EXTERIORMODEL;
740
741 // send an update
742                 baseline = &ent->baseline;
743
744                 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.value)
745                 {
746                         // every half second a full update is forced
747                         if (realtime < client->nextfullupdate[e])
748                         {
749                                 bits |= U_DELTA;
750                                 baseline = &ent->deltabaseline;
751                         }
752                         else
753                                 nextfullupdate = realtime + 0.5f;
754                 }
755                 else
756                         nextfullupdate = realtime + 0.5f;
757
758                 // restore nextfullupdate since this is being sent for real
759                 client->nextfullupdate[e] = nextfullupdate;
760
761                 if (e >= 256)
762                         bits |= U_LONGENTITY;
763
764                 if (ent->v.movetype == MOVETYPE_STEP)
765                         bits |= U_STEP;
766
767                 // LordHavoc: old stuff, but rewritten to have more exact tolerances
768 //              if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0))                                             bits |= U_ORIGIN1;
769 //              if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0))                                             bits |= U_ORIGIN2;
770 //              if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0))                                             bits |= U_ORIGIN3;
771                 if (origin[0] != baseline->origin[0])                                                                                   bits |= U_ORIGIN1;
772                 if (origin[1] != baseline->origin[1])                                                                                   bits |= U_ORIGIN2;
773                 if (origin[2] != baseline->origin[2])                                                                                   bits |= U_ORIGIN3;
774                 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
775                 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
776                 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
777                 if (baseline->colormap != (byte) ent->v.colormap)                                                               bits |= U_COLORMAP;
778                 if (baseline->skin != (byte) ent->v.skin)                                                                               bits |= U_SKIN;
779                 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF))                                bits |= U_FRAME;
780                 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF))                    bits |= U_EFFECTS;
781                 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF))              bits |= U_MODEL;
782
783                 // LordHavoc: new stuff
784                 if (baseline->alpha != alpha)                                                                                                   bits |= U_ALPHA;
785                 if (baseline->scale != scale)                                                                                                   bits |= U_SCALE;
786                 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00))              bits |= U_EFFECTS2;
787                 if (baseline->glowsize != glowsize)                                                                                             bits |= U_GLOWSIZE;
788                 if (baseline->glowcolor != glowcolor)                                                                                   bits |= U_GLOWCOLOR;
789                 if (baseline->colormod != colormod)                                                                                             bits |= U_COLORMOD;
790                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00))                  bits |= U_FRAME2;
791                 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00))             bits |= U_MODEL2;
792
793                 // update delta baseline
794                 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
795                 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
796                 ent->deltabaseline.colormap = ent->v.colormap;
797                 ent->deltabaseline.skin = ent->v.skin;
798                 ent->deltabaseline.frame = ent->v.frame;
799                 ent->deltabaseline.effects = ent->v.effects;
800                 ent->deltabaseline.modelindex = ent->v.modelindex;
801                 ent->deltabaseline.alpha = alpha;
802                 ent->deltabaseline.scale = scale;
803                 ent->deltabaseline.glowsize = glowsize;
804                 ent->deltabaseline.glowcolor = glowcolor;
805                 ent->deltabaseline.colormod = colormod;
806
807                 if (bits & (U_ALPHA | U_SCALE | U_EFFECTS2 | U_GLOWSIZE | U_GLOWCOLOR | U_COLORMOD | U_FRAME2 | U_MODEL2))
808                         i = -1;
809
810                 // write the message
811                 if (bits >= 16777216)
812                         bits |= U_EXTEND2;
813                 if (bits >= 65536)
814                         bits |= U_EXTEND1;
815                 if (bits >= 256)
816                         bits |= U_MOREBITS;
817                 bits |= U_SIGNAL;
818
819                 MSG_WriteByte (msg, bits);
820                 if (bits & U_MOREBITS)
821                         MSG_WriteByte (msg, bits>>8);
822                 // LordHavoc: extend bytes have to be written here due to delta compression
823                 if (bits & U_EXTEND1)
824                         MSG_WriteByte (msg, bits>>16);
825                 if (bits & U_EXTEND2)
826                         MSG_WriteByte (msg, bits>>24);
827
828                 // LordHavoc: old stuff
829                 if (bits & U_LONGENTITY)
830                         MSG_WriteShort (msg,e);
831                 else
832                         MSG_WriteByte (msg,e);
833                 if (bits & U_MODEL)             MSG_WriteByte (msg,     ent->v.modelindex);
834                 if (bits & U_FRAME)             MSG_WriteByte (msg, ent->v.frame);
835                 if (bits & U_COLORMAP)  MSG_WriteByte (msg, ent->v.colormap);
836                 if (bits & U_SKIN)              MSG_WriteByte (msg, ent->v.skin);
837                 if (bits & U_EFFECTS)   MSG_WriteByte (msg, ent->v.effects);
838                 if (bits & U_ORIGIN1)   MSG_WriteFloatCoord (msg, origin[0]);
839                 if (bits & U_ANGLE1)    MSG_WriteAngle(msg, angles[0]);
840                 if (bits & U_ORIGIN2)   MSG_WriteFloatCoord (msg, origin[1]);
841                 if (bits & U_ANGLE2)    MSG_WriteAngle(msg, angles[1]);
842                 if (bits & U_ORIGIN3)   MSG_WriteFloatCoord (msg, origin[2]);
843                 if (bits & U_ANGLE3)    MSG_WriteAngle(msg, angles[2]);
844
845                 // LordHavoc: new stuff
846                 if (bits & U_ALPHA)             MSG_WriteByte(msg, alpha);
847                 if (bits & U_SCALE)             MSG_WriteByte(msg, scale);
848                 if (bits & U_EFFECTS2)  MSG_WriteByte(msg, (int)ent->v.effects >> 8);
849                 if (bits & U_GLOWSIZE)  MSG_WriteByte(msg, glowsize);
850                 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
851                 if (bits & U_COLORMOD)  MSG_WriteByte(msg, colormod);
852                 if (bits & U_FRAME2)    MSG_WriteByte(msg, (int)ent->v.frame >> 8);
853                 if (bits & U_MODEL2)    MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
854         }
855
856         if (sv_reportvischeckentities.value)
857                 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
858         sv_vischeckentitycullcount = 0;
859 }
860
861 /*
862 =============
863 SV_CleanupEnts
864
865 =============
866 */
867 void SV_CleanupEnts (void)
868 {
869         int             e;
870         edict_t *ent;
871
872         ent = NEXT_EDICT(sv.edicts);
873         for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
874         {
875                 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
876         }
877
878 }
879
880 /*
881 ==================
882 SV_WriteClientdataToMessage
883
884 ==================
885 */
886 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
887 {
888         int             bits;
889         int             i;
890         edict_t *other;
891         int             items;
892         eval_t  *val;
893         vec3_t  punchvector;
894
895 //
896 // send a damage message
897 //
898         if (ent->v.dmg_take || ent->v.dmg_save)
899         {
900                 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
901                 MSG_WriteByte (msg, svc_damage);
902                 MSG_WriteByte (msg, ent->v.dmg_save);
903                 MSG_WriteByte (msg, ent->v.dmg_take);
904                 for (i=0 ; i<3 ; i++)
905                         MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
906
907                 ent->v.dmg_take = 0;
908                 ent->v.dmg_save = 0;
909         }
910
911 //
912 // send the current viewpos offset from the view entity
913 //
914         SV_SetIdealPitch ();            // how much to look up / down ideally
915
916 // a fixangle might get lost in a dropped packet.  Oh well.
917         if ( ent->v.fixangle )
918         {
919                 MSG_WriteByte (msg, svc_setangle);
920                 for (i=0 ; i < 3 ; i++)
921                         MSG_WriteAngle (msg, ent->v.angles[i] );
922                 ent->v.fixangle = 0;
923         }
924
925         bits = 0;
926
927         if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
928                 bits |= SU_VIEWHEIGHT;
929
930         if (ent->v.idealpitch)
931                 bits |= SU_IDEALPITCH;
932
933 // stuff the sigil bits into the high bits of items for sbar, or else
934 // mix in items2
935         val = GETEDICTFIELDVALUE(ent, eval_items2);
936
937         if (val)
938                 items = (int)ent->v.items | ((int)val->_float << 23);
939         else
940                 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
941
942         bits |= SU_ITEMS;
943         
944         if ( (int)ent->v.flags & FL_ONGROUND)
945                 bits |= SU_ONGROUND;
946         
947         if ( ent->v.waterlevel >= 2)
948                 bits |= SU_INWATER;
949         
950         // dpprotocol
951         VectorClear(punchvector);
952         if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
953                 VectorCopy(val->vector, punchvector);
954
955         for (i=0 ; i<3 ; i++)
956         {
957                 if (ent->v.punchangle[i])
958                         bits |= (SU_PUNCH1<<i);
959                 if (punchvector[i]) // dpprotocol
960                         bits |= (SU_PUNCHVEC1<<i); // dpprotocol
961                 if (ent->v.velocity[i])
962                         bits |= (SU_VELOCITY1<<i);
963         }
964
965         if (ent->v.weaponframe)
966                 bits |= SU_WEAPONFRAME;
967
968         if (ent->v.armorvalue)
969                 bits |= SU_ARMOR;
970
971 //      if (ent->v.weapon)
972                 bits |= SU_WEAPON;
973
974         if (bits >= 65536)
975                 bits |= SU_EXTEND1;
976         if (bits >= 16777216)
977                 bits |= SU_EXTEND2;
978
979 // send the data
980
981         MSG_WriteByte (msg, svc_clientdata);
982         MSG_WriteShort (msg, bits);
983         if (bits & SU_EXTEND1)
984                 MSG_WriteByte(msg, bits >> 16);
985         if (bits & SU_EXTEND2)
986                 MSG_WriteByte(msg, bits >> 24);
987
988         if (bits & SU_VIEWHEIGHT)
989                 MSG_WriteChar (msg, ent->v.view_ofs[2]);
990
991         if (bits & SU_IDEALPITCH)
992                 MSG_WriteChar (msg, ent->v.idealpitch);
993
994         for (i=0 ; i<3 ; i++)
995         {
996                 if (bits & (SU_PUNCH1<<i))
997                         MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
998                 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
999                         MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
1000                 if (bits & (SU_VELOCITY1<<i))
1001                         MSG_WriteChar (msg, ent->v.velocity[i]/16);
1002         }
1003
1004 // [always sent]        if (bits & SU_ITEMS)
1005         MSG_WriteLong (msg, items);
1006
1007         if (bits & SU_WEAPONFRAME)
1008                 MSG_WriteByte (msg, ent->v.weaponframe);
1009         if (bits & SU_ARMOR)
1010                 MSG_WriteByte (msg, ent->v.armorvalue);
1011         if (bits & SU_WEAPON)
1012                 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1013         
1014         MSG_WriteShort (msg, ent->v.health);
1015         MSG_WriteByte (msg, ent->v.currentammo);
1016         MSG_WriteByte (msg, ent->v.ammo_shells);
1017         MSG_WriteByte (msg, ent->v.ammo_nails);
1018         MSG_WriteByte (msg, ent->v.ammo_rockets);
1019         MSG_WriteByte (msg, ent->v.ammo_cells);
1020
1021         if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1022         {
1023                 for(i=0;i<32;i++)
1024                 {
1025                         if ( ((int)ent->v.weapon) & (1<<i) )
1026                         {
1027                                 MSG_WriteByte (msg, i);
1028                                 break;
1029                         }
1030                 }
1031         }
1032         else
1033         {
1034                 MSG_WriteByte (msg, ent->v.weapon);
1035         }
1036 }
1037
1038 /*
1039 =======================
1040 SV_SendClientDatagram
1041 =======================
1042 */
1043 qboolean SV_SendClientDatagram (client_t *client)
1044 {
1045         byte            buf[MAX_DATAGRAM];
1046         sizebuf_t       msg;
1047         
1048         msg.data = buf;
1049         msg.maxsize = sizeof(buf);
1050         msg.cursize = 0;
1051
1052         MSG_WriteByte (&msg, svc_time);
1053         MSG_WriteFloat (&msg, sv.time);
1054
1055 // add the client specific data to the datagram
1056         SV_WriteClientdataToMessage (client->edict, &msg);
1057
1058         SV_WriteEntitiesToClient (client, client->edict, &msg);
1059
1060 // copy the server datagram if there is space
1061         if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1062                 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1063
1064 // send the datagram
1065         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1066         {
1067                 SV_DropClient (true);// if the message couldn't send, kick off
1068                 return false;
1069         }
1070
1071         return true;
1072 }
1073
1074 /*
1075 =======================
1076 SV_UpdateToReliableMessages
1077 =======================
1078 */
1079 void SV_UpdateToReliableMessages (void)
1080 {
1081         int                     i, j;
1082         client_t *client;
1083
1084 // check for changes to be sent over the reliable streams
1085         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1086         {
1087                 if (host_client->old_frags != host_client->edict->v.frags)
1088                 {
1089                         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1090                         {
1091                                 if (!client->active)
1092                                         continue;
1093                                 MSG_WriteByte (&client->message, svc_updatefrags);
1094                                 MSG_WriteByte (&client->message, i);
1095                                 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1096                         }
1097
1098                         host_client->old_frags = host_client->edict->v.frags;
1099                 }
1100         }
1101         
1102         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1103         {
1104                 if (!client->active)
1105                         continue;
1106                 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1107         }
1108
1109         SZ_Clear (&sv.reliable_datagram);
1110 }
1111
1112
1113 /*
1114 =======================
1115 SV_SendNop
1116
1117 Send a nop message without trashing or sending the accumulated client
1118 message buffer
1119 =======================
1120 */
1121 void SV_SendNop (client_t *client)
1122 {
1123         sizebuf_t       msg;
1124         byte            buf[4];
1125         
1126         msg.data = buf;
1127         msg.maxsize = sizeof(buf);
1128         msg.cursize = 0;
1129
1130         MSG_WriteChar (&msg, svc_nop);
1131
1132         if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1133                 SV_DropClient (true);   // if the message couldn't send, kick off
1134         client->last_message = realtime;
1135 }
1136
1137 /*
1138 =======================
1139 SV_SendClientMessages
1140 =======================
1141 */
1142 void SV_SendClientMessages (void)
1143 {
1144         int                     i;
1145         
1146 // update frags, names, etc
1147         SV_UpdateToReliableMessages ();
1148
1149 // build individual updates
1150         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1151         {
1152                 if (!host_client->active)
1153                         continue;
1154
1155                 if (host_client->spawned)
1156                 {
1157                         if (!SV_SendClientDatagram (host_client))
1158                                 continue;
1159                 }
1160                 else
1161                 {
1162                 // the player isn't totally in the game yet
1163                 // send small keepalive messages if too much time has passed
1164                 // send a full message when the next signon stage has been requested
1165                 // some other message data (name changes, etc) may accumulate 
1166                 // between signon stages
1167                         if (!host_client->sendsignon)
1168                         {
1169                                 if (realtime - host_client->last_message > 5)
1170                                         SV_SendNop (host_client);
1171                                 continue;       // don't send out non-signon messages
1172                         }
1173                 }
1174
1175                 // check for an overflowed message.  Should only happen
1176                 // on a very fucked up connection that backs up a lot, then
1177                 // changes level
1178                 if (host_client->message.overflowed)
1179                 {
1180                         SV_DropClient (true);
1181                         host_client->message.overflowed = false;
1182                         continue;
1183                 }
1184                         
1185                 if (host_client->message.cursize || host_client->dropasap)
1186                 {
1187                         if (!NET_CanSendMessage (host_client->netconnection))
1188                         {
1189 //                              I_Printf ("can't write\n");
1190                                 continue;
1191                         }
1192
1193                         if (host_client->dropasap)
1194                                 SV_DropClient (false);  // went to another level
1195                         else
1196                         {
1197                                 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1198                                         SV_DropClient (true);   // if the message couldn't send, kick off
1199                                 SZ_Clear (&host_client->message);
1200                                 host_client->last_message = realtime;
1201                                 host_client->sendsignon = false;
1202                         }
1203                 }
1204         }
1205         
1206         
1207 // clear muzzle flashes
1208         SV_CleanupEnts ();
1209 }
1210
1211
1212 /*
1213 ==============================================================================
1214
1215 SERVER SPAWNING
1216
1217 ==============================================================================
1218 */
1219
1220 /*
1221 ================
1222 SV_ModelIndex
1223
1224 ================
1225 */
1226 int SV_ModelIndex (char *name)
1227 {
1228         int             i;
1229         
1230         if (!name || !name[0])
1231                 return 0;
1232
1233         for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1234                 if (!strcmp(sv.model_precache[i], name))
1235                         return i;
1236         if (i==MAX_MODELS || !sv.model_precache[i])
1237                 Host_Error ("SV_ModelIndex: model %s not precached", name);
1238         return i;
1239 }
1240
1241 /*
1242 ================
1243 SV_CreateBaseline
1244
1245 ================
1246 */
1247 void SV_CreateBaseline (void)
1248 {
1249         int i, entnum, large;
1250         edict_t *svent;
1251
1252         // LordHavoc: clear *all* states (note just active ones)
1253         for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1254         {
1255                 // get the current server version
1256                 svent = EDICT_NUM(entnum);
1257
1258                 // LordHavoc: always clear state values, whether the entity is in use or not
1259                 ClearStateToDefault(&svent->baseline);
1260
1261                 if (svent->free)
1262                         continue;
1263                 if (entnum > svs.maxclients && !svent->v.modelindex)
1264                         continue;
1265
1266         //
1267                 // create entity baseline
1268         //
1269                 VectorCopy (svent->v.origin, svent->baseline.origin);
1270                 VectorCopy (svent->v.angles, svent->baseline.angles);
1271                 svent->baseline.frame = svent->v.frame;
1272                 svent->baseline.skin = svent->v.skin;
1273                 if (entnum > 0 && entnum <= svs.maxclients)
1274                 {
1275                         svent->baseline.colormap = entnum;
1276                         svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1277                 }
1278                 else
1279                 {
1280                         svent->baseline.colormap = 0;
1281                         svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1282                 }
1283
1284                 large = false;
1285                 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1286                         large = true;
1287         //
1288                 // add to the message
1289         //
1290                 if (large)
1291                         MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1292                 else
1293                         MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1294                 MSG_WriteShort (&sv.signon, entnum);
1295
1296                 if (large)
1297                 {
1298                         MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1299                         MSG_WriteShort (&sv.signon, svent->baseline.frame);
1300                 }
1301                 else
1302                 {
1303                         MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1304                         MSG_WriteByte (&sv.signon, svent->baseline.frame);
1305                 }
1306                 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1307                 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1308                 for (i=0 ; i<3 ; i++)
1309                 {
1310                         MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1311                         MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1312                 }
1313         }
1314 }
1315
1316
1317 /*
1318 ================
1319 SV_SendReconnect
1320
1321 Tell all the clients that the server is changing levels
1322 ================
1323 */
1324 void SV_SendReconnect (void)
1325 {
1326         char    data[128];
1327         sizebuf_t       msg;
1328
1329         msg.data = data;
1330         msg.cursize = 0;
1331         msg.maxsize = sizeof(data);
1332
1333         MSG_WriteChar (&msg, svc_stufftext);
1334         MSG_WriteString (&msg, "reconnect\n");
1335         NET_SendToAll (&msg, 5);
1336         
1337         if (cls.state != ca_dedicated)
1338                 Cmd_ExecuteString ("reconnect\n", src_command);
1339 }
1340
1341
1342 /*
1343 ================
1344 SV_SaveSpawnparms
1345
1346 Grabs the current state of each client for saving across the
1347 transition to another level
1348 ================
1349 */
1350 void SV_SaveSpawnparms (void)
1351 {
1352         int             i, j;
1353
1354         svs.serverflags = pr_global_struct->serverflags;
1355
1356         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1357         {
1358                 if (!host_client->active)
1359                         continue;
1360
1361         // call the progs to get default spawn parms for the new client
1362                 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1363                 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1364                 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1365                         host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1366         }
1367 }
1368
1369 qboolean isworldmodel;
1370
1371 /*
1372 ================
1373 SV_SpawnServer
1374
1375 This is called at the start of each level
1376 ================
1377 */
1378 extern float            scr_centertime_off;
1379
1380 void SV_SpawnServer (char *server)
1381 {
1382         edict_t         *ent;
1383         int                     i;
1384
1385         // let's not have any servers with no name
1386         if (hostname.string[0] == 0)
1387                 Cvar_Set ("hostname", "UNNAMED");
1388         scr_centertime_off = 0;
1389
1390         Con_DPrintf ("SpawnServer: %s\n",server);
1391         svs.changelevel_issued = false;         // now safe to issue another
1392
1393 //
1394 // tell all connected clients that we are going to a new level
1395 //
1396         if (sv.active)
1397         {
1398                 SV_SendReconnect ();
1399         }
1400
1401 //
1402 // make cvars consistant
1403 //
1404         if (coop.value)
1405                 Cvar_SetValue ("deathmatch", 0);
1406         current_skill = (int)(skill.value + 0.5);
1407         if (current_skill < 0)
1408                 current_skill = 0;
1409         if (current_skill > 3)
1410                 current_skill = 3;
1411
1412         Cvar_SetValue ("skill", (float)current_skill);
1413         
1414 //
1415 // set up the new server
1416 //
1417         Host_ClearMemory ();
1418
1419         memset (&sv, 0, sizeof(sv));
1420
1421         strcpy (sv.name, server);
1422
1423 // load progs to get entity field count
1424         PR_LoadProgs ();
1425
1426 // allocate server memory
1427         sv.max_edicts = MAX_EDICTS;
1428         
1429         sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1430
1431         sv.datagram.maxsize = sizeof(sv.datagram_buf);
1432         sv.datagram.cursize = 0;
1433         sv.datagram.data = sv.datagram_buf;
1434         
1435         sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1436         sv.reliable_datagram.cursize = 0;
1437         sv.reliable_datagram.data = sv.reliable_datagram_buf;
1438         
1439         sv.signon.maxsize = sizeof(sv.signon_buf);
1440         sv.signon.cursize = 0;
1441         sv.signon.data = sv.signon_buf;
1442
1443 // leave slots at start for clients only
1444         sv.num_edicts = svs.maxclients+1;
1445         for (i=0 ; i<svs.maxclients ; i++)
1446         {
1447                 ent = EDICT_NUM(i+1);
1448                 svs.clients[i].edict = ent;
1449         }
1450         
1451         sv.state = ss_loading;
1452         sv.paused = false;
1453
1454         sv.time = 1.0;
1455         
1456         strcpy (sv.name, server);
1457         sprintf (sv.modelname,"maps/%s.bsp", server);
1458         isworldmodel = true; // LordHavoc: only load submodels on the world model
1459         sv.worldmodel = Mod_ForName (sv.modelname, false);
1460         isworldmodel = false;
1461         if (!sv.worldmodel)
1462         {
1463                 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1464                 sv.active = false;
1465                 return;
1466         }
1467         sv.models[1] = sv.worldmodel;
1468
1469 //
1470 // clear world interaction links
1471 //
1472         SV_ClearWorld ();
1473         
1474         sv.sound_precache[0] = pr_strings;
1475
1476         sv.model_precache[0] = pr_strings;
1477         sv.model_precache[1] = sv.modelname;
1478         for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1479         {
1480                 sv.model_precache[1+i] = localmodels[i];
1481                 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1482         }
1483
1484 //
1485 // load the rest of the entities
1486 //      
1487         ent = EDICT_NUM(0);
1488         memset (&ent->v, 0, progs->entityfields * 4);
1489         ent->free = false;
1490         ent->v.model = sv.worldmodel->name - pr_strings;
1491         ent->v.modelindex = 1;          // world model
1492         ent->v.solid = SOLID_BSP;
1493         ent->v.movetype = MOVETYPE_PUSH;
1494
1495         if (coop.value)
1496                 pr_global_struct->coop = coop.value;
1497         else
1498                 pr_global_struct->deathmatch = deathmatch.value;
1499
1500         pr_global_struct->mapname = sv.name - pr_strings;
1501
1502 // serverflags are for cross level information (sigils)
1503         pr_global_struct->serverflags = svs.serverflags;
1504         
1505         ED_LoadFromFile (sv.worldmodel->entities);
1506         // LordHavoc: clear world angles (to fix e3m3.bsp)
1507         VectorClear(sv.edicts->v.angles);
1508
1509         sv.active = true;
1510
1511 // all setup is completed, any further precache statements are errors
1512         sv.state = ss_active;
1513
1514 // run two frames to allow everything to settle
1515         sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1516         SV_Physics ();
1517         SV_Physics ();
1518
1519 // create a baseline for more efficient communications
1520         SV_CreateBaseline ();
1521
1522 // send serverinfo to all connected clients
1523         for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1524                 if (host_client->active)
1525                         SV_SendServerinfo (host_client);
1526
1527         Con_DPrintf ("Server spawned.\n");
1528 }