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 //============================================================================
38 extern void SV_Phys_Init (void);
39 extern void SV_World_Init (void);
50 Cvar_RegisterVariable (&sv_maxvelocity);
51 Cvar_RegisterVariable (&sv_gravity);
52 Cvar_RegisterVariable (&sv_friction);
53 Cvar_RegisterVariable (&sv_edgefriction);
54 Cvar_RegisterVariable (&sv_stopspeed);
55 Cvar_RegisterVariable (&sv_maxspeed);
56 Cvar_RegisterVariable (&sv_accelerate);
57 Cvar_RegisterVariable (&sv_idealpitchscale);
58 Cvar_RegisterVariable (&sv_aim);
59 Cvar_RegisterVariable (&sv_nostep);
60 Cvar_RegisterVariable (&sv_predict);
61 Cvar_RegisterVariable (&sv_deltacompress);
62 Cvar_RegisterVariable (&sv_cullentities_pvs);
63 Cvar_RegisterVariable (&sv_cullentities_portal);
64 Cvar_RegisterVariable (&sv_cullentities_trace);
65 Cvar_RegisterVariable (&sv_cullentities_stats);
70 for (i = 0;i < MAX_MODELS;i++)
71 sprintf (localmodels[i], "*%i", i);
73 sv_edicts_mempool = Mem_AllocPool("edicts");
77 =============================================================================
81 =============================================================================
88 Make sure the event gets sent to all clients
91 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
95 if (sv.datagram.cursize > MAX_DATAGRAM-16)
97 MSG_WriteByte (&sv.datagram, svc_particle);
98 MSG_WriteDPCoord (&sv.datagram, org[0]);
99 MSG_WriteDPCoord (&sv.datagram, org[1]);
100 MSG_WriteDPCoord (&sv.datagram, org[2]);
101 for (i=0 ; i<3 ; i++)
108 MSG_WriteChar (&sv.datagram, v);
110 MSG_WriteByte (&sv.datagram, count);
111 MSG_WriteByte (&sv.datagram, color);
118 Make sure the event gets sent to all clients
121 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
123 if (sv.datagram.cursize > MAX_DATAGRAM-18)
125 if (modelindex >= 256 || startframe >= 256)
127 MSG_WriteByte (&sv.datagram, svc_effect2);
128 MSG_WriteDPCoord (&sv.datagram, org[0]);
129 MSG_WriteDPCoord (&sv.datagram, org[1]);
130 MSG_WriteDPCoord (&sv.datagram, org[2]);
131 MSG_WriteShort (&sv.datagram, modelindex);
132 MSG_WriteShort (&sv.datagram, startframe);
133 MSG_WriteByte (&sv.datagram, framecount);
134 MSG_WriteByte (&sv.datagram, framerate);
138 MSG_WriteByte (&sv.datagram, svc_effect);
139 MSG_WriteDPCoord (&sv.datagram, org[0]);
140 MSG_WriteDPCoord (&sv.datagram, org[1]);
141 MSG_WriteDPCoord (&sv.datagram, org[2]);
142 MSG_WriteByte (&sv.datagram, modelindex);
143 MSG_WriteByte (&sv.datagram, startframe);
144 MSG_WriteByte (&sv.datagram, framecount);
145 MSG_WriteByte (&sv.datagram, framerate);
153 Each entity can have eight independant sound sources, like voice,
156 Channel 0 is an auto-allocate channel, the others override anything
157 already running on that entity/channel pair.
159 An attenuation of 0 will play full volume everywhere in the level.
160 Larger attenuations will drop off. (max 4 attenuation)
164 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
172 if (volume < 0 || volume > 255)
173 Host_Error ("SV_StartSound: volume = %i", volume);
175 if (attenuation < 0 || attenuation > 4)
176 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
178 if (channel < 0 || channel > 7)
179 Host_Error ("SV_StartSound: channel = %i", channel);
181 if (sv.datagram.cursize > MAX_DATAGRAM-16)
184 // find precache number for sound
185 for (sound_num=1 ; sound_num<MAX_SOUNDS
186 && sv.sound_precache[sound_num] ; sound_num++)
187 if (!strcmp(sample, sv.sound_precache[sound_num]))
190 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
192 Con_Printf ("SV_StartSound: %s not precached\n", sample);
196 ent = NUM_FOR_EDICT(entity);
199 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
200 field_mask |= SND_VOLUME;
201 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
202 field_mask |= SND_ATTENUATION;
204 field_mask |= SND_LARGEENTITY;
205 if (sound_num >= 256 || channel >= 8)
206 field_mask |= SND_LARGESOUND;
208 // directed messages go only to the entity they are targeted on
209 MSG_WriteByte (&sv.datagram, svc_sound);
210 MSG_WriteByte (&sv.datagram, field_mask);
211 if (field_mask & SND_VOLUME)
212 MSG_WriteByte (&sv.datagram, volume);
213 if (field_mask & SND_ATTENUATION)
214 MSG_WriteByte (&sv.datagram, attenuation*64);
215 if (field_mask & SND_LARGEENTITY)
217 MSG_WriteShort (&sv.datagram, ent);
218 MSG_WriteByte (&sv.datagram, channel);
221 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
222 if (field_mask & SND_LARGESOUND)
223 MSG_WriteShort (&sv.datagram, sound_num);
225 MSG_WriteByte (&sv.datagram, sound_num);
226 for (i = 0;i < 3;i++)
227 MSG_WriteDPCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]));
231 ==============================================================================
235 ==============================================================================
242 Sends the first message from the server to a connected client.
243 This will be sent on the initial connection and upon each server load.
246 void SV_SendServerinfo (client_t *client)
251 // LordHavoc: clear entityframe tracking
252 client->entityframenumber = 0;
253 EntityFrame_ClearDatabase(&client->entitydatabase);
255 MSG_WriteByte (&client->message, svc_print);
256 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
257 MSG_WriteString (&client->message,message);
259 MSG_WriteByte (&client->message, svc_serverinfo);
260 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
261 MSG_WriteByte (&client->message, svs.maxclients);
263 if (!coop.integer && deathmatch.integer)
264 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
266 MSG_WriteByte (&client->message, GAME_COOP);
268 sprintf (message, PR_GetString(sv.edicts->v->message));
270 MSG_WriteString (&client->message,message);
272 for (s = sv.model_precache+1 ; *s ; s++)
273 MSG_WriteString (&client->message, *s);
274 MSG_WriteByte (&client->message, 0);
276 for (s = sv.sound_precache+1 ; *s ; s++)
277 MSG_WriteString (&client->message, *s);
278 MSG_WriteByte (&client->message, 0);
281 MSG_WriteByte (&client->message, svc_cdtrack);
282 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
283 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
286 MSG_WriteByte (&client->message, svc_setview);
287 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
289 MSG_WriteByte (&client->message, svc_signonnum);
290 MSG_WriteByte (&client->message, 1);
292 client->sendsignon = true;
293 client->spawned = false; // need prespawn, spawn, etc
300 Initializes a client_t for a new net connection. This will only be called
301 once for a player each game, not once for each level change.
304 void SV_ConnectClient (int clientnum)
309 struct qsocket_s *netconnection;
311 float spawn_parms[NUM_SPAWN_PARMS];
313 client = svs.clients + clientnum;
315 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
317 edictnum = clientnum+1;
319 ent = EDICT_NUM(edictnum);
321 // set up the client_t
322 netconnection = client->netconnection;
325 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
326 memset (client, 0, sizeof(*client));
327 client->netconnection = netconnection;
329 strcpy (client->name, "unconnected");
330 client->active = true;
331 client->spawned = false;
333 client->message.data = client->msgbuf;
334 client->message.maxsize = sizeof(client->msgbuf);
335 client->message.allowoverflow = true; // we can catch it
338 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
341 // call the progs to get default spawn parms for the new client
342 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
343 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
344 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
348 SV_SendServerinfo (client);
350 // send serverinfo on first nop
351 client->waitingforconnect = true;
352 client->sendsignon = true;
353 client->spawned = false; // need prespawn, spawn, etc
360 SV_CheckForNewClients
364 void SV_CheckForNewClients (void)
366 struct qsocket_s *ret;
370 // check for new connections
374 ret = NET_CheckNewConnections ();
379 // init a new client structure
381 for (i=0 ; i<svs.maxclients ; i++)
382 if (!svs.clients[i].active)
384 if (i == svs.maxclients)
385 Sys_Error ("Host_CheckForNewClients: no free clients");
387 svs.clients[i].netconnection = ret;
388 SV_ConnectClient (i);
390 net_activeconnections++;
398 ===============================================================================
402 ===============================================================================
411 void SV_ClearDatagram (void)
413 SZ_Clear (&sv.datagram);
417 =============================================================================
419 The PVS must include a small area around the client to allow head bobbing
420 or other small motion on the client side. Otherwise, a bob might cause an
421 entity that should be visible to not show up, especially when the bob
424 =============================================================================
428 qbyte fatpvs[MAX_MAP_LEAFS/8];
430 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
439 // if this is a leaf, accumulate the pvs bits
440 if (node->contents < 0)
442 if (node->contents != CONTENTS_SOLID)
444 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
445 for (i=0 ; i<fatbytes ; i++)
452 d = DotProduct (org, plane->normal) - plane->dist;
454 node = node->children[0];
456 node = node->children[1];
459 SV_AddToFatPVS (org, node->children[0]);
460 node = node->children[1];
469 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
473 qbyte *SV_FatPVS (vec3_t org)
475 fatbytes = (sv.worldmodel->numleafs+31)>>3;
476 memset (fatpvs, 0, fatbytes);
477 SV_AddToFatPVS (org, sv.worldmodel->nodes);
481 //=============================================================================
484 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
488 if (node->contents < 0)
491 if (node->contents == CONTENTS_SOLID)
493 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
494 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
497 // node - recurse down the BSP tree
498 switch (BoxOnPlaneSide(mins, maxs, node->plane))
501 node = node->children[0];
504 node = node->children[1];
507 if (node->children[0]->contents != CONTENTS_SOLID)
508 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
510 node = node->children[1];
520 SV_WriteEntitiesToClient
525 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
527 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
528 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
530 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
531 float nextfullupdate, alphaf;
534 entity_state_t *baseline; // LordHavoc: delta or startup baseline
538 Mod_CheckLoaded(sv.worldmodel);
540 // find the client's PVS
541 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
542 pvs = SV_FatPVS (testeye);
550 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
551 // send all entities that touch the pvs
552 ent = NEXT_EDICT(sv.edicts);
553 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
557 // prevent delta compression against this frame (unless actually sent, which will restore this later)
558 nextfullupdate = client->nextfullupdate[e];
559 client->nextfullupdate[e] = -1;
561 if (ent != clent) // LordHavoc: always send player
563 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
565 if (val->edict != clentnum)
567 // don't show to anyone else
571 bits |= U_VIEWMODEL; // show relative to the view
575 // LordHavoc: never draw something told not to display to this client
576 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
578 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
585 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
586 glowsize = (int) val->_float >> 2;
587 if (glowsize > 255) glowsize = 255;
588 if (glowsize < 0) glowsize = 0;
590 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
591 if (val->_float != 0)
594 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
596 model = sv.models[(int)ent->v->modelindex];
597 Mod_CheckLoaded(model);
602 if (ent != clent) // LordHavoc: always send player
603 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
607 VectorCopy(ent->v->angles, angles);
608 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f)
610 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
611 // LordHavoc: trace predicted movement to avoid putting things in walls
612 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
613 VectorCopy(trace.endpos, origin);
617 VectorCopy(ent->v->origin, origin);
620 // ent has survived every check so far, check if it is visible
621 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
623 // use the predicted origin
624 entmins[0] = origin[0] - 1.0f;
625 entmins[1] = origin[1] - 1.0f;
626 entmins[2] = origin[2] - 1.0f;
627 entmaxs[0] = origin[0] + 1.0f;
628 entmaxs[1] = origin[1] + 1.0f;
629 entmaxs[2] = origin[2] + 1.0f;
630 // using the model's bounding box to ensure things are visible regardless of their physics box
633 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
635 VectorAdd(entmins, model->rotatedmins, entmins);
636 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
638 else if (ent->v->angles[1])
640 VectorAdd(entmins, model->yawmins, entmins);
641 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
645 VectorAdd(entmins, model->normalmins, entmins);
646 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
652 // if not touching a visible leaf
653 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
659 // or not visible through the portals
660 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
666 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
667 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
669 // LordHavoc: test random offsets, to maximize chance of detection
670 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
671 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
672 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
674 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
676 if (trace.fraction == 1)
677 client->visibletime[e] = realtime + 1;
680 //test nearest point on bbox
681 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
682 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
683 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
685 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
687 if (trace.fraction == 1)
688 client->visibletime[e] = realtime + 1;
689 else if (realtime > client->visibletime[e])
702 effects = ent->v->effects;
704 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
705 if (val->_float != 0)
706 alphaf = val->_float * 255.0f;
709 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
710 if (val->_float != 0)
711 alphaf = val->_float;
715 alpha = bound(0, alphaf, 255);
717 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
718 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
719 if (scale < 0) scale = 0;
720 if (scale > 255) scale = 255;
722 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
723 if (val->_float != 0)
724 glowcolor = (int) val->_float;
726 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
727 if (val->_float != 0)
728 effects |= EF_FULLBRIGHT;
732 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
736 // don't send if flagged for NODRAW and there are no effects
737 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
740 else // no model and no effects
745 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
747 Con_Printf ("packet overflow\n");
748 // mark the rest of the entities so they can't be delta compressed against this frame
749 for (;e < sv.num_edicts;e++)
751 client->nextfullupdate[e] = -1;
752 client->visibletime[e] = -1;
757 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
758 bits = bits | U_EXTERIORMODEL;
761 baseline = &ent->baseline;
763 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
765 // every half second a full update is forced
766 if (realtime < client->nextfullupdate[e])
769 baseline = &ent->deltabaseline;
772 nextfullupdate = realtime + 0.5f;
775 nextfullupdate = realtime + 0.5f;
777 // restore nextfullupdate since this is being sent for real
778 client->nextfullupdate[e] = nextfullupdate;
781 bits |= U_LONGENTITY;
783 if (ent->v->movetype == MOVETYPE_STEP)
786 // LordHavoc: old stuff, but rewritten to have more exact tolerances
787 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
788 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
789 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
790 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
791 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
792 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
793 if (baseline->colormap != (qbyte) ent->v->colormap) bits |= U_COLORMAP;
794 if (baseline->skin != (qbyte) ent->v->skin) bits |= U_SKIN;
795 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF)) bits |= U_FRAME;
796 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF)) bits |= U_EFFECTS;
797 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF)) bits |= U_MODEL;
799 // LordHavoc: new stuff
800 if (baseline->alpha != alpha) bits |= U_ALPHA;
801 if (baseline->scale != scale) bits |= U_SCALE;
802 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00)) bits |= U_EFFECTS2;
803 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
804 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
805 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00)) bits |= U_FRAME2;
806 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00)) bits |= U_MODEL2;
808 // update delta baseline
809 VectorCopy(ent->v->origin, ent->deltabaseline.origin);
810 VectorCopy(ent->v->angles, ent->deltabaseline.angles);
811 ent->deltabaseline.colormap = ent->v->colormap;
812 ent->deltabaseline.skin = ent->v->skin;
813 ent->deltabaseline.frame = ent->v->frame;
814 ent->deltabaseline.effects = ent->v->effects;
815 ent->deltabaseline.modelindex = ent->v->modelindex;
816 ent->deltabaseline.alpha = alpha;
817 ent->deltabaseline.scale = scale;
818 ent->deltabaseline.glowsize = glowsize;
819 ent->deltabaseline.glowcolor = glowcolor;
822 if (bits >= 16777216)
830 MSG_WriteByte (msg, bits);
831 if (bits & U_MOREBITS)
832 MSG_WriteByte (msg, bits>>8);
833 // LordHavoc: extend bytes have to be written here due to delta compression
834 if (bits & U_EXTEND1)
835 MSG_WriteByte (msg, bits>>16);
836 if (bits & U_EXTEND2)
837 MSG_WriteByte (msg, bits>>24);
839 // LordHavoc: old stuff
840 if (bits & U_LONGENTITY)
841 MSG_WriteShort (msg,e);
843 MSG_WriteByte (msg,e);
844 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v->modelindex);
845 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v->frame);
846 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v->colormap);
847 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v->skin);
848 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v->effects);
849 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
850 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
851 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
852 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
853 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
854 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
856 // LordHavoc: new stuff
857 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
858 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
859 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v->effects >> 8);
860 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
861 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
862 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v->frame >> 8);
863 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
866 if (sv_cullentities_stats.integer)
867 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);
870 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
872 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
873 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
874 float alphaf, lightsize;
876 vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
881 entity_frame_t entityframe;
884 if (client->sendsignon)
887 Mod_CheckLoaded(sv.worldmodel);
889 // find the client's PVS
890 // the real place being tested from
891 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
892 pvs = SV_FatPVS (testeye);
894 // the place being reported (to consider the fact the client still
895 // applies the view_ofs[2], so we have to only send the fractional part
896 // of view_ofs[2], undoing what the client will redo)
897 VectorCopy (testeye, testorigin);
898 e = (int) clent->v->view_ofs[2] & 255;
901 testorigin[2] -= (float) e;
902 EntityFrame_Clear(&entityframe, testorigin);
910 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
911 // send all entities that touch the pvs
912 ent = NEXT_EDICT(sv.edicts);
913 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
919 if (ent != clent) // LordHavoc: always send player
921 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
923 if (val->edict == clentnum)
924 flags |= RENDER_VIEWMODEL; // show relative to the view
927 // don't show to anyone else
933 // LordHavoc: never draw something told not to display to this client
934 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
936 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
942 effects = ent->v->effects;
943 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
944 glowsize = (int) val->_float >> 2;
945 glowsize = bound(0, glowsize, 255);
948 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
950 if (effects & EF_BRIGHTFIELD)
951 lightsize = max(lightsize, 80);
952 if (effects & EF_MUZZLEFLASH)
953 lightsize = max(lightsize, 100);
954 if (effects & EF_BRIGHTLIGHT)
955 lightsize = max(lightsize, 400);
956 if (effects & EF_DIMLIGHT)
957 lightsize = max(lightsize, 200);
958 if (effects & EF_RED)
959 lightsize = max(lightsize, 200);
960 if (effects & EF_BLUE)
961 lightsize = max(lightsize, 200);
962 if (effects & EF_FLAME)
963 lightsize = max(lightsize, 250);
964 if (effects & EF_STARDUST)
965 lightsize = max(lightsize, 100);
968 lightsize = max(lightsize, glowsize << 2);
970 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
971 if (val->_float != 0)
973 flags |= RENDER_GLOWTRAIL;
974 lightsize = max(lightsize, 100);
978 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
980 modelindex = ent->v->modelindex;
981 model = sv.models[(int)ent->v->modelindex];
982 Mod_CheckLoaded(model);
987 if (ent != clent) // LordHavoc: always send player
988 if (lightsize == 0) // no effects
992 VectorCopy(ent->v->angles, angles);
993 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f && host_client->latency >= 0.01f)
995 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
996 // LordHavoc: trace predicted movement to avoid putting things in walls
997 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
998 VectorCopy(trace.endpos, origin);
1002 VectorCopy(ent->v->origin, origin);
1005 // ent has survived every check so far, check if it is visible
1006 // always send embedded brush models, they don't generate much traffic
1007 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
1009 // use the predicted origin
1010 entmins[0] = origin[0] - 1.0f;
1011 entmins[1] = origin[1] - 1.0f;
1012 entmins[2] = origin[2] - 1.0f;
1013 entmaxs[0] = origin[0] + 1.0f;
1014 entmaxs[1] = origin[1] + 1.0f;
1015 entmaxs[2] = origin[2] + 1.0f;
1016 // using the model's bounding box to ensure things are visible regardless of their physics box
1019 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
1021 VectorAdd(entmins, model->rotatedmins, entmins);
1022 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
1024 else if (ent->v->angles[1])
1026 VectorAdd(entmins, model->yawmins, entmins);
1027 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
1031 VectorAdd(entmins, model->normalmins, entmins);
1032 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1035 lightmins[0] = min(entmins[0], origin[0] - lightsize);
1036 lightmins[1] = min(entmins[1], origin[1] - lightsize);
1037 lightmins[2] = min(entmins[2], origin[2] - lightsize);
1038 lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
1039 lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
1040 lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
1044 // if not touching a visible leaf
1045 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->nodes))
1051 // or not visible through the portals
1052 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
1058 if (sv_cullentities_trace.integer)
1060 // LordHavoc: test center first
1061 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
1062 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
1063 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
1064 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1065 if (trace.fraction == 1)
1066 client->visibletime[e] = realtime + 1;
1069 // LordHavoc: test random offsets, to maximize chance of detection
1070 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1071 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1072 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1073 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1074 if (trace.fraction == 1)
1075 client->visibletime[e] = realtime + 1;
1080 // LordHavoc: test random offsets, to maximize chance of detection
1081 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1082 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1083 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1084 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1085 if (trace.fraction == 1)
1086 client->visibletime[e] = realtime + 1;
1089 if (realtime > client->visibletime[e])
1098 if (realtime > client->visibletime[e])
1113 effects = ent->v->effects;
1115 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1116 if (val->_float != 0)
1117 alphaf = val->_float * 255.0;
1120 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1121 if (val->_float != 0)
1122 alphaf = val->_float;
1126 alpha = bound(0, alphaf, 255);
1128 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1129 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1130 if (scale < 0) scale = 0;
1131 if (scale > 255) scale = 255;
1133 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1134 if (val->_float != 0)
1135 glowcolor = (int) val->_float;
1137 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1138 if (val->_float != 0)
1139 effects |= EF_FULLBRIGHT;
1143 if (lightsize == 0) // no effects
1147 // don't send if flagged for NODRAW and there are no effects
1148 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1151 else // no model and no effects
1156 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1157 flags |= RENDER_EXTERIORMODEL;
1159 if (ent->v->movetype == MOVETYPE_STEP)
1160 flags |= RENDER_STEP;
1161 // don't send an entity if it's coordinates would wrap around
1162 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1163 flags |= RENDER_LOWPRECISION;
1165 s = EntityFrame_NewEntity(&entityframe, e);
1166 // if we run out of space, abort
1169 VectorCopy(origin, s->origin);
1170 VectorCopy(angles, s->angles);
1171 if (ent->v->colormap >= 1024)
1172 flags |= RENDER_COLORMAPPED;
1173 s->colormap = ent->v->colormap;
1174 s->skin = ent->v->skin;
1175 s->frame = ent->v->frame;
1176 s->modelindex = modelindex;
1177 s->effects = effects;
1180 s->glowsize = glowsize;
1181 s->glowcolor = glowcolor;
1184 entityframe.framenum = ++client->entityframenumber;
1185 EntityFrame_Write(&client->entitydatabase, &entityframe, msg);
1187 if (sv_cullentities_stats.integer)
1188 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);
1198 void SV_CleanupEnts (void)
1203 ent = NEXT_EDICT(sv.edicts);
1204 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1205 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1210 SV_WriteClientdataToMessage
1214 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1225 // send a damage message
1227 if (ent->v->dmg_take || ent->v->dmg_save)
1229 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1230 MSG_WriteByte (msg, svc_damage);
1231 MSG_WriteByte (msg, ent->v->dmg_save);
1232 MSG_WriteByte (msg, ent->v->dmg_take);
1233 for (i=0 ; i<3 ; i++)
1234 MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1236 ent->v->dmg_take = 0;
1237 ent->v->dmg_save = 0;
1241 // send the current viewpos offset from the view entity
1243 SV_SetIdealPitch (); // how much to look up / down ideally
1245 // a fixangle might get lost in a dropped packet. Oh well.
1246 if ( ent->v->fixangle )
1248 MSG_WriteByte (msg, svc_setangle);
1249 for (i=0 ; i < 3 ; i++)
1250 MSG_WriteAngle (msg, ent->v->angles[i] );
1251 ent->v->fixangle = 0;
1256 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1257 bits |= SU_VIEWHEIGHT;
1259 if (ent->v->idealpitch)
1260 bits |= SU_IDEALPITCH;
1262 // stuff the sigil bits into the high bits of items for sbar, or else
1264 val = GETEDICTFIELDVALUE(ent, eval_items2);
1267 items = (int)ent->v->items | ((int)val->_float << 23);
1269 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1273 if ( (int)ent->v->flags & FL_ONGROUND)
1274 bits |= SU_ONGROUND;
1276 if ( ent->v->waterlevel >= 2)
1280 VectorClear(punchvector);
1281 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1282 VectorCopy(val->vector, punchvector);
1285 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1287 i = val->_float * 255.0f;
1291 i = bound(0, i, 255);
1295 if (viewzoom != 255)
1296 bits |= SU_VIEWZOOM;
1298 for (i=0 ; i<3 ; i++)
1300 if (ent->v->punchangle[i])
1301 bits |= (SU_PUNCH1<<i);
1302 if (punchvector[i]) // dpprotocol
1303 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1304 if (ent->v->velocity[i])
1305 bits |= (SU_VELOCITY1<<i);
1308 if (ent->v->weaponframe)
1309 bits |= SU_WEAPONFRAME;
1311 if (ent->v->armorvalue)
1318 if (bits >= 16777216)
1323 MSG_WriteByte (msg, svc_clientdata);
1324 MSG_WriteShort (msg, bits);
1325 if (bits & SU_EXTEND1)
1326 MSG_WriteByte(msg, bits >> 16);
1327 if (bits & SU_EXTEND2)
1328 MSG_WriteByte(msg, bits >> 24);
1330 if (bits & SU_VIEWHEIGHT)
1331 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1333 if (bits & SU_IDEALPITCH)
1334 MSG_WriteChar (msg, ent->v->idealpitch);
1336 for (i=0 ; i<3 ; i++)
1338 if (bits & (SU_PUNCH1<<i))
1339 MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1340 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1341 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1342 if (bits & (SU_VELOCITY1<<i))
1343 MSG_WriteChar (msg, ent->v->velocity[i]/16);
1346 // [always sent] if (bits & SU_ITEMS)
1347 MSG_WriteLong (msg, items);
1349 if (bits & SU_WEAPONFRAME)
1350 MSG_WriteByte (msg, ent->v->weaponframe);
1351 if (bits & SU_ARMOR)
1352 MSG_WriteByte (msg, ent->v->armorvalue);
1353 if (bits & SU_WEAPON)
1354 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1356 MSG_WriteShort (msg, ent->v->health);
1357 MSG_WriteByte (msg, ent->v->currentammo);
1358 MSG_WriteByte (msg, ent->v->ammo_shells);
1359 MSG_WriteByte (msg, ent->v->ammo_nails);
1360 MSG_WriteByte (msg, ent->v->ammo_rockets);
1361 MSG_WriteByte (msg, ent->v->ammo_cells);
1363 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1367 if ( ((int)ent->v->weapon) & (1<<i) )
1369 MSG_WriteByte (msg, i);
1376 MSG_WriteByte (msg, ent->v->weapon);
1379 if (bits & SU_VIEWZOOM)
1380 MSG_WriteByte (msg, viewzoom);
1384 =======================
1385 SV_SendClientDatagram
1386 =======================
1388 qboolean SV_SendClientDatagram (client_t *client)
1390 qbyte buf[MAX_DATAGRAM];
1394 msg.maxsize = sizeof(buf);
1397 MSG_WriteByte (&msg, svc_time);
1398 MSG_WriteFloat (&msg, sv.time);
1400 if (client->spawned)
1402 // add the client specific data to the datagram
1403 SV_WriteClientdataToMessage (client->edict, &msg);
1405 SV_WriteEntitiesToClient (client, client->edict, &msg);
1407 // copy the server datagram if there is space
1408 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1409 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1412 // send the datagram
1413 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1415 SV_DropClient (true);// if the message couldn't send, kick off
1423 =======================
1424 SV_UpdateToReliableMessages
1425 =======================
1427 void SV_UpdateToReliableMessages (void)
1432 // check for changes to be sent over the reliable streams
1433 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1435 if (host_client->old_frags != host_client->edict->v->frags)
1437 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1439 if (!client->active || !client->spawned)
1441 MSG_WriteByte (&client->message, svc_updatefrags);
1442 MSG_WriteByte (&client->message, i);
1443 MSG_WriteShort (&client->message, host_client->edict->v->frags);
1446 host_client->old_frags = host_client->edict->v->frags;
1450 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1452 if (!client->active)
1454 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1457 SZ_Clear (&sv.reliable_datagram);
1462 =======================
1465 Send a nop message without trashing or sending the accumulated client
1467 =======================
1469 void SV_SendNop (client_t *client)
1475 msg.maxsize = sizeof(buf);
1478 MSG_WriteChar (&msg, svc_nop);
1480 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1481 SV_DropClient (true); // if the message couldn't send, kick off
1482 client->last_message = realtime;
1486 =======================
1487 SV_SendClientMessages
1488 =======================
1490 void SV_SendClientMessages (void)
1494 // update frags, names, etc
1495 SV_UpdateToReliableMessages ();
1497 // build individual updates
1498 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1500 if (!host_client->active)
1503 #ifndef NOROUTINGFIX
1504 if (host_client->sendserverinfo)
1506 host_client->sendserverinfo = false;
1507 SV_SendServerinfo (host_client);
1511 if (host_client->spawned)
1513 if (!SV_SendClientDatagram (host_client))
1518 // the player isn't totally in the game yet
1519 // send small keepalive messages if too much time has passed
1520 // send a full message when the next signon stage has been requested
1521 // some other message data (name changes, etc) may accumulate
1522 // between signon stages
1523 if (!host_client->sendsignon)
1525 if (realtime - host_client->last_message > 5)
1526 SV_SendNop (host_client);
1527 continue; // don't send out non-signon messages
1531 // check for an overflowed message. Should only happen
1532 // on a very fucked up connection that backs up a lot, then
1534 if (host_client->message.overflowed)
1536 SV_DropClient (true);
1537 host_client->message.overflowed = false;
1541 if (host_client->message.cursize || host_client->dropasap)
1543 if (!NET_CanSendMessage (host_client->netconnection))
1546 if (host_client->dropasap)
1547 SV_DropClient (false); // went to another level
1550 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1551 SV_DropClient (true); // if the message couldn't send, kick off
1552 SZ_Clear (&host_client->message);
1553 host_client->last_message = realtime;
1554 host_client->sendsignon = false;
1560 // clear muzzle flashes
1566 ==============================================================================
1570 ==============================================================================
1579 int SV_ModelIndex (const char *name)
1583 if (!name || !name[0])
1586 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1587 if (!strcmp(sv.model_precache[i], name))
1589 if (i==MAX_MODELS || !sv.model_precache[i])
1590 Host_Error ("SV_ModelIndex: model %s not precached", name);
1594 #ifdef SV_QUAKEENTITIES
1601 void SV_CreateBaseline (void)
1603 int i, entnum, large;
1606 // LordHavoc: clear *all* states (note just active ones)
1607 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1609 // get the current server version
1610 svent = EDICT_NUM(entnum);
1612 // LordHavoc: always clear state values, whether the entity is in use or not
1613 ClearStateToDefault(&svent->baseline);
1617 if (entnum > svs.maxclients && !svent->v->modelindex)
1620 // create entity baseline
1621 VectorCopy (svent->v->origin, svent->baseline.origin);
1622 VectorCopy (svent->v->angles, svent->baseline.angles);
1623 svent->baseline.frame = svent->v->frame;
1624 svent->baseline.skin = svent->v->skin;
1625 if (entnum > 0 && entnum <= svs.maxclients)
1627 svent->baseline.colormap = entnum;
1628 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1632 svent->baseline.colormap = 0;
1633 svent->baseline.modelindex = svent->v->modelindex;
1637 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1640 // add to the message
1642 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1644 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1645 MSG_WriteShort (&sv.signon, entnum);
1649 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1650 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1654 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1655 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1657 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1658 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1659 for (i=0 ; i<3 ; i++)
1661 MSG_WriteDPCoord(&sv.signon, svent->baseline.origin[i]);
1662 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1673 Tell all the clients that the server is changing levels
1676 void SV_SendReconnect (void)
1683 msg.maxsize = sizeof(data);
1685 MSG_WriteChar (&msg, svc_stufftext);
1686 MSG_WriteString (&msg, "reconnect\n");
1687 NET_SendToAll (&msg, 5);
1689 if (cls.state != ca_dedicated)
1690 Cmd_ExecuteString ("reconnect\n", src_command);
1698 Grabs the current state of each client for saving across the
1699 transition to another level
1702 void SV_SaveSpawnparms (void)
1706 svs.serverflags = pr_global_struct->serverflags;
1708 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1710 if (!host_client->active)
1713 // call the progs to get default spawn parms for the new client
1714 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1715 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1716 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1717 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1725 This is called at the start of each level
1728 extern float scr_centertime_off;
1730 void SV_SpawnServer (const char *server)
1735 // let's not have any servers with no name
1736 if (hostname.string[0] == 0)
1737 Cvar_Set ("hostname", "UNNAMED");
1738 scr_centertime_off = 0;
1740 Con_DPrintf ("SpawnServer: %s\n",server);
1741 svs.changelevel_issued = false; // now safe to issue another
1744 // tell all connected clients that we are going to a new level
1747 SV_SendReconnect ();
1750 // make cvars consistant
1753 Cvar_SetValue ("deathmatch", 0);
1754 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1756 Cvar_SetValue ("skill", (float)current_skill);
1759 // set up the new server
1761 Host_ClearMemory ();
1763 memset (&sv, 0, sizeof(sv));
1765 strcpy (sv.name, server);
1767 // load progs to get entity field count
1770 // allocate server memory
1771 sv.max_edicts = MAX_EDICTS;
1773 // clear the edict memory pool
1774 Mem_EmptyPool(sv_edicts_mempool);
1775 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t));
1776 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1777 sv.edictstable = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1778 for (i = 0;i < MAX_EDICTS;i++)
1780 ent = sv.edicts + i;
1781 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1782 sv.edictstable[i] = ent;
1785 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1786 sv.datagram.cursize = 0;
1787 sv.datagram.data = sv.datagram_buf;
1789 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1790 sv.reliable_datagram.cursize = 0;
1791 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1793 sv.signon.maxsize = sizeof(sv.signon_buf);
1794 sv.signon.cursize = 0;
1795 sv.signon.data = sv.signon_buf;
1797 // leave slots at start for clients only
1798 sv.num_edicts = svs.maxclients+1;
1799 for (i=0 ; i<svs.maxclients ; i++)
1801 ent = EDICT_NUM(i+1);
1802 svs.clients[i].edict = ent;
1805 sv.state = ss_loading;
1812 strcpy (sv.name, server);
1813 sprintf (sv.modelname,"maps/%s.bsp", server);
1814 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1817 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1821 sv.models[1] = sv.worldmodel;
1824 // clear world interaction links
1828 sv.sound_precache[0] = "";
1830 sv.model_precache[0] = "";
1831 sv.model_precache[1] = sv.modelname;
1832 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1834 sv.model_precache[i+1] = localmodels[i];
1835 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1839 // load the rest of the entities
1842 memset (ent->v, 0, progs->entityfields * 4);
1844 ent->v->model = PR_SetString(sv.modelname);
1845 ent->v->modelindex = 1; // world model
1846 ent->v->solid = SOLID_BSP;
1847 ent->v->movetype = MOVETYPE_PUSH;
1850 pr_global_struct->coop = coop.integer;
1852 pr_global_struct->deathmatch = deathmatch.integer;
1854 pr_global_struct->mapname = PR_SetString(sv.name);
1856 // serverflags are for cross level information (sigils)
1857 pr_global_struct->serverflags = svs.serverflags;
1859 ED_LoadFromFile (sv.worldmodel->entities);
1860 // LordHavoc: clear world angles (to fix e3m3.bsp)
1861 VectorClear(sv.edicts->v->angles);
1865 // all setup is completed, any further precache statements are errors
1866 sv.state = ss_active;
1868 // run two frames to allow everything to settle
1869 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1871 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1876 #ifdef QUAKEENTITIES
1877 // create a baseline for more efficient communications
1878 SV_CreateBaseline ();
1881 // send serverinfo to all connected clients
1882 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1883 if (host_client->active)
1884 SV_SendServerinfo (host_client);
1886 Con_DPrintf ("Server spawned.\n");