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
25 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "0"}; // fast but loose
26 static cvar_t sv_cullentities_portal = {0, "sv_cullentities_portal", "0"}; // extremely accurate visibility checking, but too slow
27 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
28 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
29 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
34 static char localmodels[MAX_MODELS][5]; // inline model names for precache
36 mempool_t *sv_edicts_mempool = NULL;
38 //============================================================================
40 extern void SV_Phys_Init (void);
41 extern void SV_World_Init (void);
52 Cvar_RegisterVariable (&sv_maxvelocity);
53 Cvar_RegisterVariable (&sv_gravity);
54 Cvar_RegisterVariable (&sv_friction);
55 Cvar_RegisterVariable (&sv_edgefriction);
56 Cvar_RegisterVariable (&sv_stopspeed);
57 Cvar_RegisterVariable (&sv_maxspeed);
58 Cvar_RegisterVariable (&sv_accelerate);
59 Cvar_RegisterVariable (&sv_idealpitchscale);
60 Cvar_RegisterVariable (&sv_aim);
61 Cvar_RegisterVariable (&sv_nostep);
62 Cvar_RegisterVariable (&sv_predict);
63 Cvar_RegisterVariable (&sv_deltacompress);
64 Cvar_RegisterVariable (&sv_cullentities_pvs);
65 Cvar_RegisterVariable (&sv_cullentities_portal);
66 Cvar_RegisterVariable (&sv_cullentities_trace);
67 Cvar_RegisterVariable (&sv_cullentities_stats);
68 Cvar_RegisterVariable (&sv_entpatch);
73 for (i = 0;i < MAX_MODELS;i++)
74 sprintf (localmodels[i], "*%i", i);
76 sv_edicts_mempool = Mem_AllocPool("edicts");
80 =============================================================================
84 =============================================================================
91 Make sure the event gets sent to all clients
94 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
98 if (sv.datagram.cursize > MAX_DATAGRAM-16)
100 MSG_WriteByte (&sv.datagram, svc_particle);
101 MSG_WriteDPCoord (&sv.datagram, org[0]);
102 MSG_WriteDPCoord (&sv.datagram, org[1]);
103 MSG_WriteDPCoord (&sv.datagram, org[2]);
104 for (i=0 ; i<3 ; i++)
111 MSG_WriteChar (&sv.datagram, v);
113 MSG_WriteByte (&sv.datagram, count);
114 MSG_WriteByte (&sv.datagram, color);
121 Make sure the event gets sent to all clients
124 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
126 if (sv.datagram.cursize > MAX_DATAGRAM-18)
128 if (modelindex >= 256 || startframe >= 256)
130 MSG_WriteByte (&sv.datagram, svc_effect2);
131 MSG_WriteDPCoord (&sv.datagram, org[0]);
132 MSG_WriteDPCoord (&sv.datagram, org[1]);
133 MSG_WriteDPCoord (&sv.datagram, org[2]);
134 MSG_WriteShort (&sv.datagram, modelindex);
135 MSG_WriteShort (&sv.datagram, startframe);
136 MSG_WriteByte (&sv.datagram, framecount);
137 MSG_WriteByte (&sv.datagram, framerate);
141 MSG_WriteByte (&sv.datagram, svc_effect);
142 MSG_WriteDPCoord (&sv.datagram, org[0]);
143 MSG_WriteDPCoord (&sv.datagram, org[1]);
144 MSG_WriteDPCoord (&sv.datagram, org[2]);
145 MSG_WriteByte (&sv.datagram, modelindex);
146 MSG_WriteByte (&sv.datagram, startframe);
147 MSG_WriteByte (&sv.datagram, framecount);
148 MSG_WriteByte (&sv.datagram, framerate);
156 Each entity can have eight independant sound sources, like voice,
159 Channel 0 is an auto-allocate channel, the others override anything
160 already running on that entity/channel pair.
162 An attenuation of 0 will play full volume everywhere in the level.
163 Larger attenuations will drop off. (max 4 attenuation)
167 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
175 if (volume < 0 || volume > 255)
176 Host_Error ("SV_StartSound: volume = %i", volume);
178 if (attenuation < 0 || attenuation > 4)
179 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
181 if (channel < 0 || channel > 7)
182 Host_Error ("SV_StartSound: channel = %i", channel);
184 if (sv.datagram.cursize > MAX_DATAGRAM-16)
187 // find precache number for sound
188 for (sound_num=1 ; sound_num<MAX_SOUNDS
189 && sv.sound_precache[sound_num] ; sound_num++)
190 if (!strcmp(sample, sv.sound_precache[sound_num]))
193 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
195 Con_Printf ("SV_StartSound: %s not precached\n", sample);
199 ent = NUM_FOR_EDICT(entity);
202 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
203 field_mask |= SND_VOLUME;
204 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
205 field_mask |= SND_ATTENUATION;
207 field_mask |= SND_LARGEENTITY;
208 if (sound_num >= 256 || channel >= 8)
209 field_mask |= SND_LARGESOUND;
211 // directed messages go only to the entity they are targeted on
212 MSG_WriteByte (&sv.datagram, svc_sound);
213 MSG_WriteByte (&sv.datagram, field_mask);
214 if (field_mask & SND_VOLUME)
215 MSG_WriteByte (&sv.datagram, volume);
216 if (field_mask & SND_ATTENUATION)
217 MSG_WriteByte (&sv.datagram, attenuation*64);
218 if (field_mask & SND_LARGEENTITY)
220 MSG_WriteShort (&sv.datagram, ent);
221 MSG_WriteByte (&sv.datagram, channel);
224 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
225 if (field_mask & SND_LARGESOUND)
226 MSG_WriteShort (&sv.datagram, sound_num);
228 MSG_WriteByte (&sv.datagram, sound_num);
229 for (i = 0;i < 3;i++)
230 MSG_WriteDPCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]));
234 ==============================================================================
238 ==============================================================================
245 Sends the first message from the server to a connected client.
246 This will be sent on the initial connection and upon each server load.
249 void SV_SendServerinfo (client_t *client)
254 // LordHavoc: clear entityframe tracking
255 client->entityframenumber = 0;
256 EntityFrame_ClearDatabase(&client->entitydatabase);
258 MSG_WriteByte (&client->message, svc_print);
259 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
260 MSG_WriteString (&client->message,message);
262 MSG_WriteByte (&client->message, svc_serverinfo);
263 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
264 MSG_WriteByte (&client->message, svs.maxclients);
266 if (!coop.integer && deathmatch.integer)
267 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
269 MSG_WriteByte (&client->message, GAME_COOP);
271 sprintf (message, PR_GetString(sv.edicts->v->message));
273 MSG_WriteString (&client->message,message);
275 for (s = sv.model_precache+1 ; *s ; s++)
276 MSG_WriteString (&client->message, *s);
277 MSG_WriteByte (&client->message, 0);
279 for (s = sv.sound_precache+1 ; *s ; s++)
280 MSG_WriteString (&client->message, *s);
281 MSG_WriteByte (&client->message, 0);
284 MSG_WriteByte (&client->message, svc_cdtrack);
285 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
286 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
289 MSG_WriteByte (&client->message, svc_setview);
290 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
292 MSG_WriteByte (&client->message, svc_signonnum);
293 MSG_WriteByte (&client->message, 1);
295 client->sendsignon = true;
296 client->spawned = false; // need prespawn, spawn, etc
303 Initializes a client_t for a new net connection. This will only be called
304 once for a player each game, not once for each level change.
307 void SV_ConnectClient (int clientnum)
310 struct qsocket_s *netconnection;
312 float spawn_parms[NUM_SPAWN_PARMS];
314 client = svs.clients + clientnum;
316 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
318 // set up the client_t
319 netconnection = client->netconnection;
322 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
323 memset (client, 0, sizeof(*client));
324 client->netconnection = netconnection;
326 strcpy (client->name, "unconnected");
327 client->active = true;
328 client->spawned = false;
329 client->edict = EDICT_NUM(clientnum+1);
330 client->message.data = client->msgbuf;
331 client->message.maxsize = sizeof(client->msgbuf);
332 client->message.allowoverflow = true; // we can catch it
335 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
338 // call the progs to get default spawn parms for the new client
339 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
340 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
341 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
345 SV_SendServerinfo (client);
347 // send serverinfo on first nop
348 client->waitingforconnect = true;
349 client->sendsignon = true;
350 client->spawned = false; // need prespawn, spawn, etc
357 SV_CheckForNewClients
361 void SV_CheckForNewClients (void)
363 struct qsocket_s *ret;
367 // check for new connections
371 ret = NET_CheckNewConnections ();
376 // init a new client structure
378 for (i=0 ; i<svs.maxclients ; i++)
379 if (!svs.clients[i].active)
381 if (i == svs.maxclients)
382 Sys_Error ("Host_CheckForNewClients: no free clients");
384 svs.clients[i].netconnection = ret;
385 SV_ConnectClient (i);
387 net_activeconnections++;
395 ===============================================================================
399 ===============================================================================
408 void SV_ClearDatagram (void)
410 SZ_Clear (&sv.datagram);
414 =============================================================================
416 The PVS must include a small area around the client to allow head bobbing
417 or other small motion on the client side. Otherwise, a bob might cause an
418 entity that should be visible to not show up, especially when the bob
421 =============================================================================
425 qbyte fatpvs[MAX_MAP_LEAFS/8];
427 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
436 // if this is a leaf, accumulate the pvs bits
437 if (node->contents < 0)
439 if (node->contents != CONTENTS_SOLID)
441 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
442 for (i=0 ; i<fatbytes ; i++)
449 d = DotProduct (org, plane->normal) - plane->dist;
451 node = node->children[0];
453 node = node->children[1];
456 SV_AddToFatPVS (org, node->children[0]);
457 node = node->children[1];
466 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
470 qbyte *SV_FatPVS (vec3_t org)
472 fatbytes = (sv.worldmodel->numleafs+31)>>3;
473 memset (fatpvs, 0, fatbytes);
474 SV_AddToFatPVS (org, sv.worldmodel->nodes);
478 //=============================================================================
481 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
485 if (node->contents < 0)
488 if (node->contents == CONTENTS_SOLID)
490 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
491 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
494 // node - recurse down the BSP tree
495 switch (BoxOnPlaneSide(mins, maxs, node->plane))
498 node = node->children[0];
501 node = node->children[1];
504 if (node->children[0]->contents != CONTENTS_SOLID)
505 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
507 node = node->children[1];
517 SV_WriteEntitiesToClient
522 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
524 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
525 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
527 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
528 float nextfullupdate, alphaf;
531 entity_state_t *baseline; // LordHavoc: delta or startup baseline
535 Mod_CheckLoaded(sv.worldmodel);
537 // find the client's PVS
538 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
539 pvs = SV_FatPVS (testeye);
547 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
548 // send all entities that touch the pvs
549 ent = NEXT_EDICT(sv.edicts);
550 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
554 // prevent delta compression against this frame (unless actually sent, which will restore this later)
555 nextfullupdate = client->nextfullupdate[e];
556 client->nextfullupdate[e] = -1;
558 if (ent != clent) // LordHavoc: always send player
560 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
562 if (val->edict != clentnum)
564 // don't show to anyone else
568 bits |= U_VIEWMODEL; // show relative to the view
572 // LordHavoc: never draw something told not to display to this client
573 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
575 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
582 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
583 glowsize = (int) val->_float >> 2;
584 if (glowsize > 255) glowsize = 255;
585 if (glowsize < 0) glowsize = 0;
587 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
588 if (val->_float != 0)
591 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
593 model = sv.models[(int)ent->v->modelindex];
594 Mod_CheckLoaded(model);
599 if (ent != clent) // LordHavoc: always send player
600 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
604 VectorCopy(ent->v->angles, angles);
605 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f)
607 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
608 // LordHavoc: trace predicted movement to avoid putting things in walls
609 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
610 VectorCopy(trace.endpos, origin);
614 VectorCopy(ent->v->origin, origin);
617 // ent has survived every check so far, check if it is visible
618 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
620 // use the predicted origin
621 entmins[0] = origin[0] - 1.0f;
622 entmins[1] = origin[1] - 1.0f;
623 entmins[2] = origin[2] - 1.0f;
624 entmaxs[0] = origin[0] + 1.0f;
625 entmaxs[1] = origin[1] + 1.0f;
626 entmaxs[2] = origin[2] + 1.0f;
627 // using the model's bounding box to ensure things are visible regardless of their physics box
630 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
632 VectorAdd(entmins, model->rotatedmins, entmins);
633 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
635 else if (ent->v->angles[1])
637 VectorAdd(entmins, model->yawmins, entmins);
638 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
642 VectorAdd(entmins, model->normalmins, entmins);
643 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
649 // if not touching a visible leaf
650 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
656 // or not visible through the portals
657 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
663 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
664 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
666 // LordHavoc: test random offsets, to maximize chance of detection
667 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
668 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
669 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
671 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
673 if (trace.fraction == 1)
674 client->visibletime[e] = realtime + 1;
677 //test nearest point on bbox
678 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
679 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
680 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
682 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
684 if (trace.fraction == 1)
685 client->visibletime[e] = realtime + 1;
686 else if (realtime > client->visibletime[e])
699 effects = ent->v->effects;
701 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
702 if (val->_float != 0)
703 alphaf = val->_float * 255.0f;
706 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
707 if (val->_float != 0)
708 alphaf = val->_float;
712 alpha = bound(0, alphaf, 255);
714 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
715 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
716 if (scale < 0) scale = 0;
717 if (scale > 255) scale = 255;
719 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
720 if (val->_float != 0)
721 glowcolor = (int) val->_float;
723 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
724 if (val->_float != 0)
725 effects |= EF_FULLBRIGHT;
729 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
733 // don't send if flagged for NODRAW and there are no effects
734 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
737 else // no model and no effects
742 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
744 Con_Printf ("packet overflow\n");
745 // mark the rest of the entities so they can't be delta compressed against this frame
746 for (;e < sv.num_edicts;e++)
748 client->nextfullupdate[e] = -1;
749 client->visibletime[e] = -1;
754 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
755 bits = bits | U_EXTERIORMODEL;
758 baseline = &ent->e->baseline;
760 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
762 // every half second a full update is forced
763 if (realtime < client->nextfullupdate[e])
766 baseline = &ent->e->deltabaseline;
769 nextfullupdate = realtime + 0.5f;
772 nextfullupdate = realtime + 0.5f;
774 // restore nextfullupdate since this is being sent for real
775 client->nextfullupdate[e] = nextfullupdate;
778 bits |= U_LONGENTITY;
780 if (ent->v->movetype == MOVETYPE_STEP)
783 // LordHavoc: old stuff, but rewritten to have more exact tolerances
784 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
785 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
786 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
787 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
788 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
789 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
790 if (baseline->colormap != (qbyte) ent->v->colormap) bits |= U_COLORMAP;
791 if (baseline->skin != (qbyte) ent->v->skin) bits |= U_SKIN;
792 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF)) bits |= U_FRAME;
793 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF)) bits |= U_EFFECTS;
794 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF)) bits |= U_MODEL;
796 // LordHavoc: new stuff
797 if (baseline->alpha != alpha) bits |= U_ALPHA;
798 if (baseline->scale != scale) bits |= U_SCALE;
799 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00)) bits |= U_EFFECTS2;
800 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
801 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
802 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00)) bits |= U_FRAME2;
803 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00)) bits |= U_MODEL2;
805 // update delta baseline
806 VectorCopy(ent->v->origin, ent->e->deltabaseline.origin);
807 VectorCopy(ent->v->angles, ent->e->deltabaseline.angles);
808 ent->e->deltabaseline.colormap = ent->v->colormap;
809 ent->e->deltabaseline.skin = ent->v->skin;
810 ent->e->deltabaseline.frame = ent->v->frame;
811 ent->e->deltabaseline.effects = ent->v->effects;
812 ent->e->deltabaseline.modelindex = ent->v->modelindex;
813 ent->e->deltabaseline.alpha = alpha;
814 ent->e->deltabaseline.scale = scale;
815 ent->e->deltabaseline.glowsize = glowsize;
816 ent->e->deltabaseline.glowcolor = glowcolor;
819 if (bits >= 16777216)
827 MSG_WriteByte (msg, bits);
828 if (bits & U_MOREBITS)
829 MSG_WriteByte (msg, bits>>8);
830 // LordHavoc: extend bytes have to be written here due to delta compression
831 if (bits & U_EXTEND1)
832 MSG_WriteByte (msg, bits>>16);
833 if (bits & U_EXTEND2)
834 MSG_WriteByte (msg, bits>>24);
836 // LordHavoc: old stuff
837 if (bits & U_LONGENTITY)
838 MSG_WriteShort (msg,e);
840 MSG_WriteByte (msg,e);
841 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v->modelindex);
842 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v->frame);
843 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v->colormap);
844 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v->skin);
845 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v->effects);
846 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
847 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
848 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
849 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
850 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
851 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
853 // LordHavoc: new stuff
854 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
855 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
856 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v->effects >> 8);
857 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
858 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
859 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v->frame >> 8);
860 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
863 if (sv_cullentities_stats.integer)
864 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);
867 static entity_frame_t sv_writeentitiestoclient_entityframe;
868 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
870 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
871 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
872 float alphaf, lightsize;
874 vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
881 if (client->sendsignon)
884 Mod_CheckLoaded(sv.worldmodel);
886 // find the client's PVS
887 // the real place being tested from
888 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
889 pvs = SV_FatPVS (testeye);
891 // the place being reported (to consider the fact the client still
892 // applies the view_ofs[2], so we have to only send the fractional part
893 // of view_ofs[2], undoing what the client will redo)
894 VectorCopy (testeye, testorigin);
895 e = (int) clent->v->view_ofs[2] & 255;
898 testorigin[2] -= (float) e;
899 EntityFrame_Clear(&sv_writeentitiestoclient_entityframe, testorigin);
907 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
908 // send all entities that touch the pvs
909 ent = NEXT_EDICT(sv.edicts);
910 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
916 if (ent != clent) // LordHavoc: always send player
918 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
920 if (val->edict == clentnum)
921 flags |= RENDER_VIEWMODEL; // show relative to the view
924 // don't show to anyone else
930 // LordHavoc: never draw something told not to display to this client
931 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
933 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
939 effects = ent->v->effects;
940 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
941 glowsize = (int) val->_float >> 2;
942 glowsize = bound(0, glowsize, 255);
945 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
947 if (effects & EF_BRIGHTFIELD)
948 lightsize = max(lightsize, 80);
949 if (effects & EF_MUZZLEFLASH)
950 lightsize = max(lightsize, 100);
951 if (effects & EF_BRIGHTLIGHT)
952 lightsize = max(lightsize, 400);
953 if (effects & EF_DIMLIGHT)
954 lightsize = max(lightsize, 200);
955 if (effects & EF_RED)
956 lightsize = max(lightsize, 200);
957 if (effects & EF_BLUE)
958 lightsize = max(lightsize, 200);
959 if (effects & EF_FLAME)
960 lightsize = max(lightsize, 250);
961 if (effects & EF_STARDUST)
962 lightsize = max(lightsize, 100);
965 lightsize = max(lightsize, glowsize << 2);
967 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
968 if (val->_float != 0)
970 flags |= RENDER_GLOWTRAIL;
971 lightsize = max(lightsize, 100);
975 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
977 modelindex = ent->v->modelindex;
978 model = sv.models[(int)ent->v->modelindex];
979 Mod_CheckLoaded(model);
984 if (ent != clent) // LordHavoc: always send player
985 if (lightsize == 0) // no effects
989 VectorCopy(ent->v->angles, angles);
990 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f && host_client->latency >= 0.01f)
992 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
993 // LordHavoc: trace predicted movement to avoid putting things in walls
994 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
995 VectorCopy(trace.endpos, origin);
999 VectorCopy(ent->v->origin, origin);
1002 // ent has survived every check so far, check if it is visible
1003 // always send embedded brush models, they don't generate much traffic
1004 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
1006 // use the predicted origin
1007 entmins[0] = origin[0] - 1.0f;
1008 entmins[1] = origin[1] - 1.0f;
1009 entmins[2] = origin[2] - 1.0f;
1010 entmaxs[0] = origin[0] + 1.0f;
1011 entmaxs[1] = origin[1] + 1.0f;
1012 entmaxs[2] = origin[2] + 1.0f;
1013 // using the model's bounding box to ensure things are visible regardless of their physics box
1016 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
1018 VectorAdd(entmins, model->rotatedmins, entmins);
1019 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
1021 else if (ent->v->angles[1])
1023 VectorAdd(entmins, model->yawmins, entmins);
1024 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
1028 VectorAdd(entmins, model->normalmins, entmins);
1029 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1032 lightmins[0] = min(entmins[0], origin[0] - lightsize);
1033 lightmins[1] = min(entmins[1], origin[1] - lightsize);
1034 lightmins[2] = min(entmins[2], origin[2] - lightsize);
1035 lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
1036 lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
1037 lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
1041 // if not touching a visible leaf
1042 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->nodes))
1048 // or not visible through the portals
1049 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
1055 if (sv_cullentities_trace.integer)
1057 // LordHavoc: test center first
1058 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
1059 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
1060 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
1061 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1062 if (trace.fraction == 1)
1063 client->visibletime[e] = realtime + 1;
1066 // LordHavoc: test random offsets, to maximize chance of detection
1067 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1068 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1069 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1070 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1071 if (trace.fraction == 1)
1072 client->visibletime[e] = realtime + 1;
1077 // LordHavoc: test random offsets, to maximize chance of detection
1078 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1079 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1080 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1081 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1082 if (trace.fraction == 1)
1083 client->visibletime[e] = realtime + 1;
1086 if (realtime > client->visibletime[e])
1095 if (realtime > client->visibletime[e])
1110 effects = ent->v->effects;
1112 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1113 if (val->_float != 0)
1114 alphaf = val->_float * 255.0;
1117 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1118 if (val->_float != 0)
1119 alphaf = val->_float;
1123 alpha = bound(0, alphaf, 255);
1125 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1126 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1127 if (scale < 0) scale = 0;
1128 if (scale > 255) scale = 255;
1130 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1131 if (val->_float != 0)
1132 glowcolor = (int) val->_float;
1134 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1135 if (val->_float != 0)
1136 effects |= EF_FULLBRIGHT;
1140 if (lightsize == 0) // no effects
1144 // don't send if flagged for NODRAW and there are no effects
1145 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1148 else // no model and no effects
1153 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1154 flags |= RENDER_EXTERIORMODEL;
1156 if (ent->v->movetype == MOVETYPE_STEP)
1157 flags |= RENDER_STEP;
1158 // don't send an entity if it's coordinates would wrap around
1159 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1160 flags |= RENDER_LOWPRECISION;
1162 s = EntityFrame_NewEntity(&sv_writeentitiestoclient_entityframe, e);
1163 // if we run out of space, abort
1166 VectorCopy(origin, s->origin);
1167 VectorCopy(angles, s->angles);
1168 if (ent->v->colormap >= 1024)
1169 flags |= RENDER_COLORMAPPED;
1170 s->colormap = ent->v->colormap;
1171 s->skin = ent->v->skin;
1172 s->frame = ent->v->frame;
1173 s->modelindex = modelindex;
1174 s->effects = effects;
1177 s->glowsize = glowsize;
1178 s->glowcolor = glowcolor;
1181 sv_writeentitiestoclient_entityframe.framenum = ++client->entityframenumber;
1182 EntityFrame_Write(&client->entitydatabase, &sv_writeentitiestoclient_entityframe, msg);
1184 if (sv_cullentities_stats.integer)
1185 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);
1195 void SV_CleanupEnts (void)
1200 ent = NEXT_EDICT(sv.edicts);
1201 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1202 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1207 SV_WriteClientdataToMessage
1211 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1222 // send a damage message
1224 if (ent->v->dmg_take || ent->v->dmg_save)
1226 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1227 MSG_WriteByte (msg, svc_damage);
1228 MSG_WriteByte (msg, ent->v->dmg_save);
1229 MSG_WriteByte (msg, ent->v->dmg_take);
1230 for (i=0 ; i<3 ; i++)
1231 MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1233 ent->v->dmg_take = 0;
1234 ent->v->dmg_save = 0;
1238 // send the current viewpos offset from the view entity
1240 SV_SetIdealPitch (); // how much to look up / down ideally
1242 // a fixangle might get lost in a dropped packet. Oh well.
1243 if ( ent->v->fixangle )
1245 MSG_WriteByte (msg, svc_setangle);
1246 for (i=0 ; i < 3 ; i++)
1247 MSG_WriteAngle (msg, ent->v->angles[i] );
1248 ent->v->fixangle = 0;
1253 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1254 bits |= SU_VIEWHEIGHT;
1256 if (ent->v->idealpitch)
1257 bits |= SU_IDEALPITCH;
1259 // stuff the sigil bits into the high bits of items for sbar, or else
1261 val = GETEDICTFIELDVALUE(ent, eval_items2);
1264 items = (int)ent->v->items | ((int)val->_float << 23);
1266 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1270 if ( (int)ent->v->flags & FL_ONGROUND)
1271 bits |= SU_ONGROUND;
1273 if ( ent->v->waterlevel >= 2)
1277 VectorClear(punchvector);
1278 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1279 VectorCopy(val->vector, punchvector);
1282 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1284 i = val->_float * 255.0f;
1288 i = bound(0, i, 255);
1292 if (viewzoom != 255)
1293 bits |= SU_VIEWZOOM;
1295 for (i=0 ; i<3 ; i++)
1297 if (ent->v->punchangle[i])
1298 bits |= (SU_PUNCH1<<i);
1299 if (punchvector[i]) // dpprotocol
1300 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1301 if (ent->v->velocity[i])
1302 bits |= (SU_VELOCITY1<<i);
1305 if (ent->v->weaponframe)
1306 bits |= SU_WEAPONFRAME;
1308 if (ent->v->armorvalue)
1315 if (bits >= 16777216)
1320 MSG_WriteByte (msg, svc_clientdata);
1321 MSG_WriteShort (msg, bits);
1322 if (bits & SU_EXTEND1)
1323 MSG_WriteByte(msg, bits >> 16);
1324 if (bits & SU_EXTEND2)
1325 MSG_WriteByte(msg, bits >> 24);
1327 if (bits & SU_VIEWHEIGHT)
1328 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1330 if (bits & SU_IDEALPITCH)
1331 MSG_WriteChar (msg, ent->v->idealpitch);
1333 for (i=0 ; i<3 ; i++)
1335 if (bits & (SU_PUNCH1<<i))
1336 MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1337 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1338 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1339 if (bits & (SU_VELOCITY1<<i))
1340 MSG_WriteChar (msg, ent->v->velocity[i]/16);
1343 // [always sent] if (bits & SU_ITEMS)
1344 MSG_WriteLong (msg, items);
1346 if (bits & SU_WEAPONFRAME)
1347 MSG_WriteByte (msg, ent->v->weaponframe);
1348 if (bits & SU_ARMOR)
1349 MSG_WriteByte (msg, ent->v->armorvalue);
1350 if (bits & SU_WEAPON)
1351 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1353 MSG_WriteShort (msg, ent->v->health);
1354 MSG_WriteByte (msg, ent->v->currentammo);
1355 MSG_WriteByte (msg, ent->v->ammo_shells);
1356 MSG_WriteByte (msg, ent->v->ammo_nails);
1357 MSG_WriteByte (msg, ent->v->ammo_rockets);
1358 MSG_WriteByte (msg, ent->v->ammo_cells);
1360 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1364 if ( ((int)ent->v->weapon) & (1<<i) )
1366 MSG_WriteByte (msg, i);
1373 MSG_WriteByte (msg, ent->v->weapon);
1376 if (bits & SU_VIEWZOOM)
1377 MSG_WriteByte (msg, viewzoom);
1381 =======================
1382 SV_SendClientDatagram
1383 =======================
1385 static qbyte sv_sendclientdatagram_buf[MAX_DATAGRAM]; // FIXME?
1386 qboolean SV_SendClientDatagram (client_t *client)
1390 msg.data = sv_sendclientdatagram_buf;
1391 msg.maxsize = sizeof(sv_sendclientdatagram_buf);
1394 MSG_WriteByte (&msg, svc_time);
1395 MSG_WriteFloat (&msg, sv.time);
1397 if (client->spawned)
1399 // add the client specific data to the datagram
1400 SV_WriteClientdataToMessage (client->edict, &msg);
1402 SV_WriteEntitiesToClient (client, client->edict, &msg);
1404 // copy the server datagram if there is space
1405 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1406 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1409 // send the datagram
1410 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1412 SV_DropClient (true);// if the message couldn't send, kick off
1420 =======================
1421 SV_UpdateToReliableMessages
1422 =======================
1424 void SV_UpdateToReliableMessages (void)
1429 // check for changes to be sent over the reliable streams
1430 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1432 sv_player = host_client->edict;
1433 if (host_client->old_frags != sv_player->v->frags)
1435 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1437 if (!client->active || !client->spawned)
1439 MSG_WriteByte (&client->message, svc_updatefrags);
1440 MSG_WriteByte (&client->message, i);
1441 MSG_WriteShort (&client->message, sv_player->v->frags);
1444 host_client->old_frags = sv_player->v->frags;
1448 for (j=0, host_client = svs.clients ; j<svs.maxclients ; j++, host_client++)
1450 if (!host_client->active)
1452 SZ_Write (&host_client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1455 SZ_Clear (&sv.reliable_datagram);
1460 =======================
1463 Send a nop message without trashing or sending the accumulated client
1465 =======================
1467 void SV_SendNop (client_t *client)
1473 msg.maxsize = sizeof(buf);
1476 MSG_WriteChar (&msg, svc_nop);
1478 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1479 SV_DropClient (true); // if the message couldn't send, kick off
1480 client->last_message = realtime;
1484 =======================
1485 SV_SendClientMessages
1486 =======================
1488 void SV_SendClientMessages (void)
1492 // update frags, names, etc
1493 SV_UpdateToReliableMessages ();
1495 // build individual updates
1496 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1498 if (!host_client->active)
1501 #ifndef NOROUTINGFIX
1502 if (host_client->sendserverinfo)
1504 host_client->sendserverinfo = false;
1505 SV_SendServerinfo (host_client);
1509 if (host_client->spawned)
1511 if (!SV_SendClientDatagram (host_client))
1516 // the player isn't totally in the game yet
1517 // send small keepalive messages if too much time has passed
1518 // send a full message when the next signon stage has been requested
1519 // some other message data (name changes, etc) may accumulate
1520 // between signon stages
1521 if (!host_client->sendsignon)
1523 if (realtime - host_client->last_message > 5)
1524 SV_SendNop (host_client);
1525 continue; // don't send out non-signon messages
1529 // check for an overflowed message. Should only happen
1530 // on a very fucked up connection that backs up a lot, then
1532 if (host_client->message.overflowed)
1534 SV_DropClient (true); // overflowed
1535 host_client->message.overflowed = false;
1539 if (host_client->message.cursize || host_client->dropasap)
1541 if (!NET_CanSendMessage (host_client->netconnection))
1544 if (host_client->dropasap)
1545 SV_DropClient (false); // went to another level
1548 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1549 SV_DropClient (true); // if the message couldn't send, kick off
1550 SZ_Clear (&host_client->message);
1551 host_client->last_message = realtime;
1552 host_client->sendsignon = false;
1558 // clear muzzle flashes
1564 ==============================================================================
1568 ==============================================================================
1577 int SV_ModelIndex (const char *name)
1581 if (!name || !name[0])
1584 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1585 if (!strcmp(sv.model_precache[i], name))
1587 if (i==MAX_MODELS || !sv.model_precache[i])
1588 Host_Error ("SV_ModelIndex: model %s not precached", name);
1592 #ifdef SV_QUAKEENTITIES
1599 void SV_CreateBaseline (void)
1601 int i, entnum, large;
1604 // LordHavoc: clear *all* states (note just active ones)
1605 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1607 // get the current server version
1608 svent = EDICT_NUM(entnum);
1610 // LordHavoc: always clear state values, whether the entity is in use or not
1611 ClearStateToDefault(&svent->e->baseline);
1615 if (entnum > svs.maxclients && !svent->v->modelindex)
1618 // create entity baseline
1619 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1620 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1621 svent->e->baseline.frame = svent->v->frame;
1622 svent->e->baseline.skin = svent->v->skin;
1623 if (entnum > 0 && entnum <= svs.maxclients)
1625 svent->e->baseline.colormap = entnum;
1626 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1630 svent->e->baseline.colormap = 0;
1631 svent->e->baseline.modelindex = svent->v->modelindex;
1635 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1638 // add to the message
1640 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1642 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1643 MSG_WriteShort (&sv.signon, entnum);
1647 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1648 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1652 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1653 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1655 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1656 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1657 for (i=0 ; i<3 ; i++)
1659 MSG_WriteDPCoord(&sv.signon, svent->e->baseline.origin[i]);
1660 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i]);
1671 Tell all the clients that the server is changing levels
1674 void SV_SendReconnect (void)
1681 msg.maxsize = sizeof(data);
1683 MSG_WriteChar (&msg, svc_stufftext);
1684 MSG_WriteString (&msg, "reconnect\n");
1685 NET_SendToAll (&msg, 5);
1687 if (cls.state != ca_dedicated)
1688 Cmd_ExecuteString ("reconnect\n", src_command);
1696 Grabs the current state of each client for saving across the
1697 transition to another level
1700 void SV_SaveSpawnparms (void)
1704 svs.serverflags = pr_global_struct->serverflags;
1706 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1708 if (!host_client->active)
1711 // call the progs to get default spawn parms for the new client
1712 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1713 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1714 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1715 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1719 void SV_IncreaseEdicts(void)
1723 int oldmax_edicts = sv.max_edicts;
1724 void *oldedictsengineprivate = sv.edictsengineprivate;
1725 void *oldedictsfields = sv.edictsfields;
1726 void *oldmoved_edicts = sv.moved_edicts;
1728 // links don't survive the transition, so unlink everything
1729 for (i = 0;i < sv.max_edicts;i++)
1730 SV_UnlinkEdict (sv.edicts + i);
1733 sv.max_edicts = min(sv.max_edicts + 32, MAX_EDICTS);
1734 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1735 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1736 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1738 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1739 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1741 for (i = 0;i < sv.max_edicts;i++)
1743 ent = sv.edicts + i;
1744 ent->e = sv.edictsengineprivate + i;
1745 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1746 // link every entity except world
1748 SV_LinkEdict(ent, false);
1751 Mem_Free(oldedictsengineprivate);
1752 Mem_Free(oldedictsfields);
1753 Mem_Free(oldmoved_edicts);
1760 This is called at the start of each level
1763 extern float scr_centertime_off;
1765 void SV_SpawnServer (const char *server)
1771 // let's not have any servers with no name
1772 if (hostname.string[0] == 0)
1773 Cvar_Set ("hostname", "UNNAMED");
1774 scr_centertime_off = 0;
1776 Con_DPrintf ("SpawnServer: %s\n",server);
1777 svs.changelevel_issued = false; // now safe to issue another
1780 // tell all connected clients that we are going to a new level
1783 SV_SendReconnect ();
1786 // make cvars consistant
1789 Cvar_SetValue ("deathmatch", 0);
1790 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1792 Cvar_SetValue ("skill", (float)current_skill);
1795 // set up the new server
1797 Host_ClearMemory ();
1799 memset (&sv, 0, sizeof(sv));
1801 strcpy (sv.name, server);
1803 // load progs to get entity field count
1806 // allocate server memory
1807 // start out with just enough room for clients and a reasonable estimate of entities
1808 sv.max_edicts = ((svs.maxclients + 128) + 31) & ~31;
1809 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1811 // clear the edict memory pool
1812 Mem_EmptyPool(sv_edicts_mempool);
1813 // edict_t structures (hidden from progs)
1814 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1815 // engine private structures (hidden from progs)
1816 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1817 // progs fields, often accessed by server
1818 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1819 // used by PushMove to move back pushed entities
1820 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1821 for (i = 0;i < sv.max_edicts;i++)
1823 ent = sv.edicts + i;
1824 ent->e = sv.edictsengineprivate + i;
1825 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1828 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1829 sv.datagram.cursize = 0;
1830 sv.datagram.data = sv.datagram_buf;
1832 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1833 sv.reliable_datagram.cursize = 0;
1834 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1836 sv.signon.maxsize = sizeof(sv.signon_buf);
1837 sv.signon.cursize = 0;
1838 sv.signon.data = sv.signon_buf;
1840 // leave slots at start for clients only
1841 sv.num_edicts = svs.maxclients+1;
1842 for (i=0 ; i<svs.maxclients ; i++)
1843 svs.clients[i].edict = EDICT_NUM(i+1);
1845 sv.state = ss_loading;
1852 strcpy (sv.name, server);
1853 sprintf (sv.modelname,"maps/%s.bsp", server);
1854 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1857 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1861 sv.models[1] = sv.worldmodel;
1864 // clear world interaction links
1868 sv.sound_precache[0] = "";
1870 sv.model_precache[0] = "";
1871 sv.model_precache[1] = sv.modelname;
1872 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1874 sv.model_precache[i+1] = localmodels[i];
1875 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1879 // load the rest of the entities
1882 memset (ent->v, 0, progs->entityfields * 4);
1883 ent->e->free = false;
1884 ent->v->model = PR_SetString(sv.modelname);
1885 ent->v->modelindex = 1; // world model
1886 ent->v->solid = SOLID_BSP;
1887 ent->v->movetype = MOVETYPE_PUSH;
1890 pr_global_struct->coop = coop.integer;
1892 pr_global_struct->deathmatch = deathmatch.integer;
1894 pr_global_struct->mapname = PR_SetString(sv.name);
1896 // serverflags are for cross level information (sigils)
1897 pr_global_struct->serverflags = svs.serverflags;
1899 // load replacement entity file if found
1901 if (sv_entpatch.integer)
1902 entities = FS_LoadFile(va("maps/%s.ent", sv.name), true);
1905 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1906 ED_LoadFromFile (entities);
1910 ED_LoadFromFile (sv.worldmodel->entities);
1913 // LordHavoc: clear world angles (to fix e3m3.bsp)
1914 VectorClear(sv.edicts->v->angles);
1918 // all setup is completed, any further precache statements are errors
1919 sv.state = ss_active;
1921 // run two frames to allow everything to settle
1922 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1924 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1929 #ifdef QUAKEENTITIES
1930 // create a baseline for more efficient communications
1931 SV_CreateBaseline ();
1934 // send serverinfo to all connected clients
1935 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1936 if (host_client->active)
1937 SV_SendServerinfo (host_client);
1939 Con_DPrintf ("Server spawned.\n");