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