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