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