2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // sv_main.c -- server main program
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"};
32 static char localmodels[MAX_MODELS][5]; // inline model names for precache
34 static mempool_t *sv_edicts_mempool = NULL;
36 //============================================================================
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);
64 for (i = 0;i < MAX_MODELS;i++)
65 sprintf (localmodels[i], "*%i", i);
67 sv_edicts_mempool = Mem_AllocPool("edicts");
71 =============================================================================
75 =============================================================================
82 Make sure the event gets sent to all clients
85 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
89 if (sv.datagram.cursize > MAX_DATAGRAM-16)
91 MSG_WriteByte (&sv.datagram, svc_particle);
92 MSG_WriteDPCoord (&sv.datagram, org[0]);
93 MSG_WriteDPCoord (&sv.datagram, org[1]);
94 MSG_WriteDPCoord (&sv.datagram, org[2]);
102 MSG_WriteChar (&sv.datagram, v);
104 MSG_WriteByte (&sv.datagram, count);
105 MSG_WriteByte (&sv.datagram, color);
112 Make sure the event gets sent to all clients
115 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
117 if (sv.datagram.cursize > MAX_DATAGRAM-18)
119 if (modelindex >= 256 || startframe >= 256)
121 MSG_WriteByte (&sv.datagram, svc_effect2);
122 MSG_WriteDPCoord (&sv.datagram, org[0]);
123 MSG_WriteDPCoord (&sv.datagram, org[1]);
124 MSG_WriteDPCoord (&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);
132 MSG_WriteByte (&sv.datagram, svc_effect);
133 MSG_WriteDPCoord (&sv.datagram, org[0]);
134 MSG_WriteDPCoord (&sv.datagram, org[1]);
135 MSG_WriteDPCoord (&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);
147 Each entity can have eight independant sound sources, like voice,
150 Channel 0 is an auto-allocate channel, the others override anything
151 already running on that entity/channel pair.
153 An attenuation of 0 will play full volume everywhere in the level.
154 Larger attenuations will drop off. (max 4 attenuation)
158 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
166 if (volume < 0 || volume > 255)
167 Host_Error ("SV_StartSound: volume = %i", volume);
169 if (attenuation < 0 || attenuation > 4)
170 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
172 if (channel < 0 || channel > 7)
173 Host_Error ("SV_StartSound: channel = %i", channel);
175 if (sv.datagram.cursize > MAX_DATAGRAM-16)
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]))
184 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
186 Con_Printf ("SV_StartSound: %s not precached\n", sample);
190 ent = NUM_FOR_EDICT(entity);
192 channel = (ent<<3) | channel;
195 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
196 field_mask |= SND_VOLUME;
197 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
198 field_mask |= SND_ATTENUATION;
200 // directed messages go only to the entity they are targeted on
201 if (sound_num >= 256)
202 MSG_WriteByte (&sv.datagram, svc_sound2);
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);
214 MSG_WriteByte (&sv.datagram, sound_num);
215 for (i=0 ; i<3 ; i++)
216 MSG_WriteDPCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
220 ==============================================================================
224 ==============================================================================
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.
235 void SV_SendServerinfo (client_t *client)
240 // LordHavoc: clear entityframe tracking
241 client->entityframenumber = 0;
242 EntityFrame_ClearDatabase(&client->entitydatabase);
244 MSG_WriteByte (&client->message, svc_print);
245 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
246 MSG_WriteString (&client->message,message);
248 MSG_WriteByte (&client->message, svc_serverinfo);
249 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
250 MSG_WriteByte (&client->message, svs.maxclients);
252 if (!coop.integer && deathmatch.integer)
253 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
255 MSG_WriteByte (&client->message, GAME_COOP);
257 sprintf (message, pr_strings+sv.edicts->v.message);
259 MSG_WriteString (&client->message,message);
261 for (s = sv.model_precache+1 ; *s ; s++)
262 MSG_WriteString (&client->message, *s);
263 MSG_WriteByte (&client->message, 0);
265 for (s = sv.sound_precache+1 ; *s ; s++)
266 MSG_WriteString (&client->message, *s);
267 MSG_WriteByte (&client->message, 0);
270 MSG_WriteByte (&client->message, svc_cdtrack);
271 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
272 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
275 MSG_WriteByte (&client->message, svc_setview);
276 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
278 MSG_WriteByte (&client->message, svc_signonnum);
279 MSG_WriteByte (&client->message, 1);
281 client->sendsignon = true;
282 client->spawned = false; // need prespawn, spawn, etc
289 Initializes a client_t for a new net connection. This will only be called
290 once for a player each game, not once for each level change.
293 void SV_ConnectClient (int clientnum)
298 struct qsocket_s *netconnection;
300 float spawn_parms[NUM_SPAWN_PARMS];
302 client = svs.clients + clientnum;
304 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
306 edictnum = clientnum+1;
308 ent = EDICT_NUM(edictnum);
310 // set up the client_t
311 netconnection = client->netconnection;
314 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
315 memset (client, 0, sizeof(*client));
316 client->netconnection = netconnection;
318 strcpy (client->name, "unconnected");
319 client->active = true;
320 client->spawned = false;
322 client->message.data = client->msgbuf;
323 client->message.maxsize = sizeof(client->msgbuf);
324 client->message.allowoverflow = true; // we can catch it
327 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
330 // call the progs to get default spawn parms for the new client
331 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
332 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
333 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
337 SV_SendServerinfo (client);
339 // send serverinfo on first nop
340 client->waitingforconnect = true;
341 client->sendsignon = true;
342 client->spawned = false; // need prespawn, spawn, etc
349 SV_CheckForNewClients
353 void SV_CheckForNewClients (void)
355 struct qsocket_s *ret;
359 // check for new connections
363 ret = NET_CheckNewConnections ();
368 // init a new client structure
370 for (i=0 ; i<svs.maxclients ; i++)
371 if (!svs.clients[i].active)
373 if (i == svs.maxclients)
374 Sys_Error ("Host_CheckForNewClients: no free clients");
376 svs.clients[i].netconnection = ret;
377 SV_ConnectClient (i);
379 net_activeconnections++;
386 ===============================================================================
390 ===============================================================================
399 void SV_ClearDatagram (void)
401 SZ_Clear (&sv.datagram);
405 =============================================================================
407 The PVS must include a small area around the client to allow head bobbing
408 or other small motion on the client side. Otherwise, a bob might cause an
409 entity that should be visible to not show up, especially when the bob
412 =============================================================================
416 qbyte fatpvs[MAX_MAP_LEAFS/8];
418 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
427 // if this is a leaf, accumulate the pvs bits
428 if (node->contents < 0)
430 if (node->contents != CONTENTS_SOLID)
432 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
433 for (i=0 ; i<fatbytes ; i++)
440 d = DotProduct (org, plane->normal) - plane->dist;
442 node = node->children[0];
444 node = node->children[1];
447 SV_AddToFatPVS (org, node->children[0]);
448 node = node->children[1];
457 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
461 qbyte *SV_FatPVS (vec3_t org)
463 fatbytes = (sv.worldmodel->numleafs+31)>>3;
464 memset (fatpvs, 0, fatbytes);
465 SV_AddToFatPVS (org, sv.worldmodel->nodes);
469 //=============================================================================
472 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
476 if (node->contents < 0)
479 if (node->contents == CONTENTS_SOLID)
481 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
482 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
485 // node - recurse down the BSP tree
486 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
489 node = node->children[0];
492 node = node->children[1];
495 if (node->children[0]->contents != CONTENTS_SOLID)
496 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
498 node = node->children[1];
508 SV_WriteEntitiesToClient
513 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
515 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
516 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
518 vec3_t org, origin, angles, entmins, entmaxs;
519 float nextfullupdate;
522 entity_state_t *baseline; // LordHavoc: delta or startup baseline
526 double testorigin[3];
528 Mod_CheckLoaded(sv.worldmodel);
530 // find the client's PVS
531 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
532 VectorCopy (org, testeye);
533 pvs = SV_FatPVS (org);
536 MSG_WriteByte(msg, svc_playerposition);
537 MSG_WriteFloat(msg, org[0]);
538 MSG_WriteFloat(msg, org[1]);
539 MSG_WriteFloat(msg, org[2]);
548 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
549 // send all entities that touch the pvs
550 ent = NEXT_EDICT(sv.edicts);
551 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
555 // prevent delta compression against this frame (unless actually sent, which will restore this later)
556 nextfullupdate = client->nextfullupdate[e];
557 client->nextfullupdate[e] = -1;
559 if (ent != clent) // LordHavoc: always send player
561 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
563 if (val->edict != clentnum)
565 // don't show to anyone else
569 bits |= U_VIEWMODEL; // show relative to the view
573 // LordHavoc: never draw something told not to display to this client
574 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
576 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
583 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
584 glowsize = (int) val->_float >> 2;
585 if (glowsize > 255) glowsize = 255;
586 if (glowsize < 0) glowsize = 0;
588 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
589 if (val->_float != 0)
592 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
594 model = sv.models[(int)ent->v.modelindex];
595 Mod_CheckLoaded(model);
600 if (ent != clent) // LordHavoc: always send player
601 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
605 VectorCopy(ent->v.angles, angles);
606 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
608 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
609 // LordHavoc: trace predicted movement to avoid putting things in walls
610 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
611 VectorCopy(trace.endpos, origin);
615 VectorCopy(ent->v.origin, origin);
618 // ent has survived every check so far, check if it is visible
619 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
621 // use the predicted origin
622 entmins[0] = origin[0] - 1.0f;
623 entmins[1] = origin[1] - 1.0f;
624 entmins[2] = origin[2] - 1.0f;
625 entmaxs[0] = origin[0] + 1.0f;
626 entmaxs[1] = origin[1] + 1.0f;
627 entmaxs[2] = origin[2] + 1.0f;
628 // using the model's bounding box to ensure things are visible regardless of their physics box
631 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
633 VectorAdd(entmins, model->rotatedmins, entmins);
634 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
636 else if (ent->v.angles[1])
638 VectorAdd(entmins, model->yawmins, entmins);
639 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
643 VectorAdd(entmins, model->normalmins, entmins);
644 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
650 // if not touching a visible leaf
651 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
657 // or not visible through the portals
658 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
664 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
665 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
667 // LordHavoc: test random offsets, to maximize chance of detection
668 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
669 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
670 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
672 memset (&trace, 0, sizeof(trace_t));
674 trace.allsolid = true;
675 VectorCopy(testorigin, trace.endpos);
677 VectorCopy(org, RecursiveHullCheckInfo.start);
678 VectorSubtract(testorigin, testeye, RecursiveHullCheckInfo.dist);
679 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
680 RecursiveHullCheckInfo.trace = &trace;
681 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, testeye, testorigin);
683 if (trace.fraction == 1)
684 client->visibletime[e] = realtime + 1;
687 //test nearest point on bbox
688 testorigin[0] = bound(entmins[0], org[0], entmaxs[0]);
689 testorigin[1] = bound(entmins[1], org[1], entmaxs[1]);
690 testorigin[2] = bound(entmins[2], org[2], entmaxs[2]);
692 memset (&trace, 0, sizeof(trace_t));
694 trace.allsolid = true;
695 VectorCopy(testorigin, trace.endpos);
697 VectorCopy(org, RecursiveHullCheckInfo.start);
698 VectorSubtract(testorigin, testeye, RecursiveHullCheckInfo.dist);
699 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
700 RecursiveHullCheckInfo.trace = &trace;
701 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, testeye, testorigin);
703 if (trace.fraction == 1)
704 client->visibletime[e] = realtime + 1;
705 else if (realtime > client->visibletime[e])
718 effects = ent->v.effects;
720 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
721 if (val->_float != 0)
722 alpha = (int) (val->_float * 255.0);
725 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
726 if (val->_float != 0)
727 alpha = (int) val->_float;
731 alpha = bound(0, alpha, 255);
733 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
734 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
735 if (scale < 0) scale = 0;
736 if (scale > 255) scale = 255;
738 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
739 if (val->_float != 0)
740 glowcolor = (int) val->_float;
742 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
743 if (val->_float != 0)
744 effects |= EF_FULLBRIGHT;
748 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
752 // don't send if flagged for NODRAW and there are no effects
753 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
756 else // no model and no effects
761 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
763 Con_Printf ("packet overflow\n");
764 // mark the rest of the entities so they can't be delta compressed against this frame
765 for (;e < sv.num_edicts;e++)
767 client->nextfullupdate[e] = -1;
768 client->visibletime[e] = -1;
773 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
774 bits = bits | U_EXTERIORMODEL;
777 baseline = &ent->baseline;
779 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.integer)
781 // every half second a full update is forced
782 if (realtime < client->nextfullupdate[e])
785 baseline = &ent->deltabaseline;
788 nextfullupdate = realtime + 0.5f;
791 nextfullupdate = realtime + 0.5f;
793 // restore nextfullupdate since this is being sent for real
794 client->nextfullupdate[e] = nextfullupdate;
797 bits |= U_LONGENTITY;
799 if (ent->v.movetype == MOVETYPE_STEP)
802 // LordHavoc: old stuff, but rewritten to have more exact tolerances
803 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
804 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
805 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
806 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
807 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
808 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
809 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
810 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
811 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
812 if (baseline->colormap != (qbyte) ent->v.colormap) bits |= U_COLORMAP;
813 if (baseline->skin != (qbyte) ent->v.skin) bits |= U_SKIN;
814 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
815 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
816 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
818 // LordHavoc: new stuff
819 if (baseline->alpha != alpha) bits |= U_ALPHA;
820 if (baseline->scale != scale) bits |= U_SCALE;
821 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
822 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
823 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
824 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
825 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
827 // update delta baseline
828 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
829 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
830 ent->deltabaseline.colormap = ent->v.colormap;
831 ent->deltabaseline.skin = ent->v.skin;
832 ent->deltabaseline.frame = ent->v.frame;
833 ent->deltabaseline.effects = ent->v.effects;
834 ent->deltabaseline.modelindex = ent->v.modelindex;
835 ent->deltabaseline.alpha = alpha;
836 ent->deltabaseline.scale = scale;
837 ent->deltabaseline.glowsize = glowsize;
838 ent->deltabaseline.glowcolor = glowcolor;
841 if (bits >= 16777216)
849 MSG_WriteByte (msg, bits);
850 if (bits & U_MOREBITS)
851 MSG_WriteByte (msg, bits>>8);
852 // LordHavoc: extend bytes have to be written here due to delta compression
853 if (bits & U_EXTEND1)
854 MSG_WriteByte (msg, bits>>16);
855 if (bits & U_EXTEND2)
856 MSG_WriteByte (msg, bits>>24);
858 // LordHavoc: old stuff
859 if (bits & U_LONGENTITY)
860 MSG_WriteShort (msg,e);
862 MSG_WriteByte (msg,e);
863 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
864 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
865 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
866 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
867 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
868 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
869 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
870 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
871 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
872 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
873 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
875 // LordHavoc: new stuff
876 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
877 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
878 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
879 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
880 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
881 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
882 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
885 if (sv_cullentities_stats.integer)
886 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);
889 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
891 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects;
892 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
894 vec3_t org, origin, angles, entmins, entmaxs;
900 double testorigin[3];
901 entity_frame_t entityframe;
904 if (client->sendsignon)
907 Mod_CheckLoaded(sv.worldmodel);
909 // find the client's PVS
910 VectorAdd (clent->v.origin, clent->v.view_ofs, testeye);
911 VectorCopy (testeye, org);
912 pvs = SV_FatPVS (org);
913 EntityFrame_Clear(&entityframe, org);
921 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
922 // send all entities that touch the pvs
923 ent = NEXT_EDICT(sv.edicts);
924 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
928 if (ent != clent) // LordHavoc: always send player
930 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
932 if (val->edict == clentnum)
933 flags |= RENDER_VIEWMODEL; // show relative to the view
936 // don't show to anyone else
942 // LordHavoc: never draw something told not to display to this client
943 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
945 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
952 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
953 glowsize = (int) val->_float >> 2;
954 glowsize = bound(0, glowsize, 255);
956 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
957 if (val->_float != 0)
958 flags |= RENDER_GLOWTRAIL;
960 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
962 model = sv.models[(int)ent->v.modelindex];
963 Mod_CheckLoaded(model);
968 if (ent != clent) // LordHavoc: always send player
969 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
973 VectorCopy(ent->v.angles, angles);
974 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f && host_client->latency >= 0.01f)
976 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
977 // LordHavoc: trace predicted movement to avoid putting things in walls
978 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
979 VectorCopy(trace.endpos, origin);
983 VectorCopy(ent->v.origin, origin);
986 // ent has survived every check so far, check if it is visible
987 // always send embedded brush models, they don't generate much traffic
988 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
990 // use the predicted origin
991 entmins[0] = origin[0] - 1.0f;
992 entmins[1] = origin[1] - 1.0f;
993 entmins[2] = origin[2] - 1.0f;
994 entmaxs[0] = origin[0] + 1.0f;
995 entmaxs[1] = origin[1] + 1.0f;
996 entmaxs[2] = origin[2] + 1.0f;
997 // using the model's bounding box to ensure things are visible regardless of their physics box
1000 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
1002 VectorAdd(entmins, model->rotatedmins, entmins);
1003 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
1005 else if (ent->v.angles[1])
1007 VectorAdd(entmins, model->yawmins, entmins);
1008 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
1012 VectorAdd(entmins, model->normalmins, entmins);
1013 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1019 // if not touching a visible leaf
1020 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
1026 // or not visible through the portals
1027 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
1033 if (sv_cullentities_trace.integer)
1035 // LordHavoc: test random offsets, to maximize chance of detection
1036 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1037 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1038 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1040 memset (&trace, 0, sizeof(trace_t));
1042 trace.allsolid = true;
1043 VectorCopy(testorigin, trace.endpos);
1045 VectorCopy(org, RecursiveHullCheckInfo.start);
1046 VectorSubtract(testorigin, testeye, RecursiveHullCheckInfo.dist);
1047 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
1048 RecursiveHullCheckInfo.trace = &trace;
1049 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, testeye, testorigin);
1051 if (trace.fraction == 1)
1052 client->visibletime[e] = realtime + 1;
1055 if (realtime > client->visibletime[e])
1068 effects = ent->v.effects;
1070 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1071 if (val->_float != 0)
1072 alpha = (int) (val->_float * 255.0);
1075 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1076 if (val->_float != 0)
1077 alpha = (int) val->_float;
1081 alpha = bound(0, alpha, 255);
1083 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1084 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1085 if (scale < 0) scale = 0;
1086 if (scale > 255) scale = 255;
1088 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1089 if (val->_float != 0)
1090 glowcolor = (int) val->_float;
1092 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1093 if (val->_float != 0)
1094 effects |= EF_FULLBRIGHT;
1098 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
1102 // don't send if flagged for NODRAW and there are no effects
1103 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1106 else // no model and no effects
1111 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1112 flags |= RENDER_EXTERIORMODEL;
1114 if (ent->v.movetype == MOVETYPE_STEP)
1115 flags |= RENDER_STEP;
1116 // don't send an entity if it's coordinates would wrap around
1117 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1118 flags |= RENDER_LOWPRECISION;
1120 s = EntityFrame_NewEntity(&entityframe, e);
1121 // if we run out of space, abort
1124 VectorCopy(origin, s->origin);
1125 VectorCopy(angles, s->angles);
1126 s->colormap = ent->v.colormap;
1127 s->skin = ent->v.skin;
1128 s->frame = ent->v.frame;
1129 s->modelindex = ent->v.modelindex;
1130 s->effects = effects;
1133 s->glowsize = glowsize;
1134 s->glowcolor = glowcolor;
1137 entityframe.framenum = ++client->entityframenumber;
1138 EntityFrame_Write(&client->entitydatabase, &entityframe, msg);
1140 if (sv_cullentities_stats.integer)
1141 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);
1151 void SV_CleanupEnts (void)
1156 ent = NEXT_EDICT(sv.edicts);
1157 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1158 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
1163 SV_WriteClientdataToMessage
1167 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1178 // send a damage message
1180 if (ent->v.dmg_take || ent->v.dmg_save)
1182 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
1183 MSG_WriteByte (msg, svc_damage);
1184 MSG_WriteByte (msg, ent->v.dmg_save);
1185 MSG_WriteByte (msg, ent->v.dmg_take);
1186 for (i=0 ; i<3 ; i++)
1187 MSG_WriteDPCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
1189 ent->v.dmg_take = 0;
1190 ent->v.dmg_save = 0;
1194 // send the current viewpos offset from the view entity
1196 SV_SetIdealPitch (); // how much to look up / down ideally
1198 // a fixangle might get lost in a dropped packet. Oh well.
1199 if ( ent->v.fixangle )
1201 MSG_WriteByte (msg, svc_setangle);
1202 for (i=0 ; i < 3 ; i++)
1203 MSG_WriteAngle (msg, ent->v.angles[i] );
1204 ent->v.fixangle = 0;
1209 //if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
1210 bits |= SU_VIEWHEIGHT;
1212 if (ent->v.idealpitch)
1213 bits |= SU_IDEALPITCH;
1215 // stuff the sigil bits into the high bits of items for sbar, or else
1217 val = GETEDICTFIELDVALUE(ent, eval_items2);
1220 items = (int)ent->v.items | ((int)val->_float << 23);
1222 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
1226 if ( (int)ent->v.flags & FL_ONGROUND)
1227 bits |= SU_ONGROUND;
1229 if ( ent->v.waterlevel >= 2)
1233 VectorClear(punchvector);
1234 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1235 VectorCopy(val->vector, punchvector);
1238 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1240 i = val->_float * 255.0f;
1244 i = bound(0, i, 255);
1248 if (viewzoom != 255)
1249 bits |= SU_VIEWZOOM;
1251 for (i=0 ; i<3 ; i++)
1253 if (ent->v.punchangle[i])
1254 bits |= (SU_PUNCH1<<i);
1255 if (punchvector[i]) // dpprotocol
1256 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1257 if (ent->v.velocity[i])
1258 bits |= (SU_VELOCITY1<<i);
1261 if (ent->v.weaponframe)
1262 bits |= SU_WEAPONFRAME;
1264 if (ent->v.armorvalue)
1267 // if (ent->v.weapon)
1272 if (bits >= 16777216)
1277 MSG_WriteByte (msg, svc_clientdata);
1278 MSG_WriteShort (msg, bits);
1279 if (bits & SU_EXTEND1)
1280 MSG_WriteByte(msg, bits >> 16);
1281 if (bits & SU_EXTEND2)
1282 MSG_WriteByte(msg, bits >> 24);
1284 if (bits & SU_VIEWHEIGHT)
1285 //MSG_WriteChar (msg, ent->v.view_ofs[2]);
1286 MSG_WriteChar (msg, 0);
1288 if (bits & SU_IDEALPITCH)
1289 MSG_WriteChar (msg, ent->v.idealpitch);
1291 for (i=0 ; i<3 ; i++)
1293 if (bits & (SU_PUNCH1<<i))
1294 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
1295 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1296 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1297 if (bits & (SU_VELOCITY1<<i))
1298 MSG_WriteChar (msg, ent->v.velocity[i]/16);
1301 // [always sent] if (bits & SU_ITEMS)
1302 MSG_WriteLong (msg, items);
1304 if (bits & SU_WEAPONFRAME)
1305 MSG_WriteByte (msg, ent->v.weaponframe);
1306 if (bits & SU_ARMOR)
1307 MSG_WriteByte (msg, ent->v.armorvalue);
1308 if (bits & SU_WEAPON)
1309 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1311 MSG_WriteShort (msg, ent->v.health);
1312 MSG_WriteByte (msg, ent->v.currentammo);
1313 MSG_WriteByte (msg, ent->v.ammo_shells);
1314 MSG_WriteByte (msg, ent->v.ammo_nails);
1315 MSG_WriteByte (msg, ent->v.ammo_rockets);
1316 MSG_WriteByte (msg, ent->v.ammo_cells);
1318 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1322 if ( ((int)ent->v.weapon) & (1<<i) )
1324 MSG_WriteByte (msg, i);
1331 MSG_WriteByte (msg, ent->v.weapon);
1334 if (bits & SU_VIEWZOOM)
1335 MSG_WriteByte (msg, viewzoom);
1339 =======================
1340 SV_SendClientDatagram
1341 =======================
1343 qboolean SV_SendClientDatagram (client_t *client)
1345 qbyte buf[MAX_DATAGRAM];
1349 msg.maxsize = sizeof(buf);
1352 MSG_WriteByte (&msg, svc_time);
1353 MSG_WriteFloat (&msg, sv.time);
1355 if (client->spawned)
1357 // add the client specific data to the datagram
1358 SV_WriteClientdataToMessage (client->edict, &msg);
1360 SV_WriteEntitiesToClient (client, client->edict, &msg);
1362 // copy the server datagram if there is space
1363 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1364 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1367 // send the datagram
1368 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1370 SV_DropClient (true);// if the message couldn't send, kick off
1378 =======================
1379 SV_UpdateToReliableMessages
1380 =======================
1382 void SV_UpdateToReliableMessages (void)
1387 // check for changes to be sent over the reliable streams
1388 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1390 if (host_client->old_frags != host_client->edict->v.frags)
1392 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1394 if (!client->active || !client->spawned)
1396 MSG_WriteByte (&client->message, svc_updatefrags);
1397 MSG_WriteByte (&client->message, i);
1398 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1401 host_client->old_frags = host_client->edict->v.frags;
1405 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1407 if (!client->active)
1409 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1412 SZ_Clear (&sv.reliable_datagram);
1417 =======================
1420 Send a nop message without trashing or sending the accumulated client
1422 =======================
1424 void SV_SendNop (client_t *client)
1430 msg.maxsize = sizeof(buf);
1433 MSG_WriteChar (&msg, svc_nop);
1435 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1436 SV_DropClient (true); // if the message couldn't send, kick off
1437 client->last_message = realtime;
1441 =======================
1442 SV_SendClientMessages
1443 =======================
1445 void SV_SendClientMessages (void)
1449 // update frags, names, etc
1450 SV_UpdateToReliableMessages ();
1452 // build individual updates
1453 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1455 if (!host_client->active)
1458 #ifndef NOROUTINGFIX
1459 if (host_client->sendserverinfo)
1461 host_client->sendserverinfo = false;
1462 SV_SendServerinfo (host_client);
1466 if (host_client->spawned)
1468 if (!SV_SendClientDatagram (host_client))
1473 // the player isn't totally in the game yet
1474 // send small keepalive messages if too much time has passed
1475 // send a full message when the next signon stage has been requested
1476 // some other message data (name changes, etc) may accumulate
1477 // between signon stages
1478 if (!host_client->sendsignon)
1480 if (realtime - host_client->last_message > 5)
1481 SV_SendNop (host_client);
1482 continue; // don't send out non-signon messages
1486 // check for an overflowed message. Should only happen
1487 // on a very fucked up connection that backs up a lot, then
1489 if (host_client->message.overflowed)
1491 SV_DropClient (true);
1492 host_client->message.overflowed = false;
1496 if (host_client->message.cursize || host_client->dropasap)
1498 if (!NET_CanSendMessage (host_client->netconnection))
1500 // I_Printf ("can't write\n");
1504 if (host_client->dropasap)
1505 SV_DropClient (false); // went to another level
1508 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1509 SV_DropClient (true); // if the message couldn't send, kick off
1510 SZ_Clear (&host_client->message);
1511 host_client->last_message = realtime;
1512 host_client->sendsignon = false;
1518 // clear muzzle flashes
1524 ==============================================================================
1528 ==============================================================================
1537 int SV_ModelIndex (char *name)
1541 if (!name || !name[0])
1544 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1545 if (!strcmp(sv.model_precache[i], name))
1547 if (i==MAX_MODELS || !sv.model_precache[i])
1548 Host_Error ("SV_ModelIndex: model %s not precached", name);
1552 #ifdef SV_QUAKEENTITIES
1559 void SV_CreateBaseline (void)
1561 int i, entnum, large;
1564 // LordHavoc: clear *all* states (note just active ones)
1565 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1567 // get the current server version
1568 svent = EDICT_NUM(entnum);
1570 // LordHavoc: always clear state values, whether the entity is in use or not
1571 ClearStateToDefault(&svent->baseline);
1575 if (entnum > svs.maxclients && !svent->v.modelindex)
1578 // create entity baseline
1579 VectorCopy (svent->v.origin, svent->baseline.origin);
1580 VectorCopy (svent->v.angles, svent->baseline.angles);
1581 svent->baseline.frame = svent->v.frame;
1582 svent->baseline.skin = svent->v.skin;
1583 if (entnum > 0 && entnum <= svs.maxclients)
1585 svent->baseline.colormap = entnum;
1586 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1590 svent->baseline.colormap = 0;
1591 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1595 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1598 // add to the message
1600 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1602 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1603 MSG_WriteShort (&sv.signon, entnum);
1607 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1608 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1612 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1613 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1615 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1616 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1617 for (i=0 ; i<3 ; i++)
1619 MSG_WriteDPCoord(&sv.signon, svent->baseline.origin[i]);
1620 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1631 Tell all the clients that the server is changing levels
1634 void SV_SendReconnect (void)
1641 msg.maxsize = sizeof(data);
1643 MSG_WriteChar (&msg, svc_stufftext);
1644 MSG_WriteString (&msg, "reconnect\n");
1645 NET_SendToAll (&msg, 5);
1647 if (cls.state != ca_dedicated)
1648 Cmd_ExecuteString ("reconnect\n", src_command);
1656 Grabs the current state of each client for saving across the
1657 transition to another level
1660 void SV_SaveSpawnparms (void)
1664 svs.serverflags = pr_global_struct->serverflags;
1666 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1668 if (!host_client->active)
1671 // call the progs to get default spawn parms for the new client
1672 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1673 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1674 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1675 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1683 This is called at the start of each level
1686 extern float scr_centertime_off;
1688 void SV_SpawnServer (char *server)
1693 // let's not have any servers with no name
1694 if (hostname.string[0] == 0)
1695 Cvar_Set ("hostname", "UNNAMED");
1696 scr_centertime_off = 0;
1698 Con_DPrintf ("SpawnServer: %s\n",server);
1699 svs.changelevel_issued = false; // now safe to issue another
1702 // tell all connected clients that we are going to a new level
1705 SV_SendReconnect ();
1708 // make cvars consistant
1711 Cvar_SetValue ("deathmatch", 0);
1712 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1714 Cvar_SetValue ("skill", (float)current_skill);
1717 // set up the new server
1719 Host_ClearMemory ();
1721 memset (&sv, 0, sizeof(sv));
1723 strcpy (sv.name, server);
1725 // load progs to get entity field count
1728 // allocate server memory
1729 sv.max_edicts = MAX_EDICTS;
1731 // clear the edict memory pool
1732 Mem_EmptyPool(sv_edicts_mempool);
1733 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1735 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1736 sv.datagram.cursize = 0;
1737 sv.datagram.data = sv.datagram_buf;
1739 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1740 sv.reliable_datagram.cursize = 0;
1741 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1743 sv.signon.maxsize = sizeof(sv.signon_buf);
1744 sv.signon.cursize = 0;
1745 sv.signon.data = sv.signon_buf;
1747 // leave slots at start for clients only
1748 sv.num_edicts = svs.maxclients+1;
1749 for (i=0 ; i<svs.maxclients ; i++)
1751 ent = EDICT_NUM(i+1);
1752 svs.clients[i].edict = ent;
1755 sv.state = ss_loading;
1762 strcpy (sv.name, server);
1763 sprintf (sv.modelname,"maps/%s.bsp", server);
1764 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1767 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1771 sv.models[1] = sv.worldmodel;
1774 // clear world interaction links
1778 sv.sound_precache[0] = pr_strings;
1780 sv.model_precache[0] = pr_strings;
1781 sv.model_precache[1] = sv.modelname;
1782 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1784 sv.model_precache[i+1] = localmodels[i];
1785 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1789 // load the rest of the entities
1792 memset (&ent->v, 0, progs->entityfields * 4);
1794 ent->v.model = sv.worldmodel->name - pr_strings;
1795 ent->v.modelindex = 1; // world model
1796 ent->v.solid = SOLID_BSP;
1797 ent->v.movetype = MOVETYPE_PUSH;
1800 pr_global_struct->coop = coop.integer;
1802 pr_global_struct->deathmatch = deathmatch.integer;
1804 pr_global_struct->mapname = sv.name - pr_strings;
1806 // serverflags are for cross level information (sigils)
1807 pr_global_struct->serverflags = svs.serverflags;
1809 ED_LoadFromFile (sv.worldmodel->entities);
1810 // LordHavoc: clear world angles (to fix e3m3.bsp)
1811 VectorClear(sv.edicts->v.angles);
1815 // all setup is completed, any further precache statements are errors
1816 sv.state = ss_active;
1818 // run two frames to allow everything to settle
1819 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1821 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1826 #ifdef QUAKEENTITIES
1827 // create a baseline for more efficient communications
1828 SV_CreateBaseline ();
1831 // send serverinfo to all connected clients
1832 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1833 if (host_client->active)
1834 SV_SendServerinfo (host_client);
1836 Con_DPrintf ("Server spawned.\n");