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);
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)
307 struct qsocket_s *netconnection;
309 float spawn_parms[NUM_SPAWN_PARMS];
311 client = svs.clients + clientnum;
313 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
315 // set up the client_t
316 netconnection = client->netconnection;
319 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
320 memset (client, 0, sizeof(*client));
321 client->netconnection = netconnection;
323 strcpy (client->name, "unconnected");
324 client->active = true;
325 client->spawned = false;
326 client->edict = EDICT_NUM(clientnum+1);
327 client->message.data = client->msgbuf;
328 client->message.maxsize = sizeof(client->msgbuf);
329 client->message.allowoverflow = true; // we can catch it
332 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
335 // call the progs to get default spawn parms for the new client
336 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
337 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
338 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
341 client->spawned = false; // need prespawn, spawn, etc
342 client->waitingforconnect = true;
343 client->sendserverinfo = false;
344 client->sendsignon = true;
350 SV_CheckForNewClients
354 void SV_CheckForNewClients (void)
356 struct qsocket_s *ret;
360 // check for new connections
364 ret = NET_CheckNewConnections ();
369 // init a new client structure
371 for (i=0 ; i<svs.maxclients ; i++)
372 if (!svs.clients[i].active)
374 if (i == svs.maxclients)
375 Sys_Error ("Host_CheckForNewClients: no free clients");
377 svs.clients[i].netconnection = ret;
378 SV_ConnectClient (i);
380 net_activeconnections++;
388 ===============================================================================
392 ===============================================================================
401 void SV_ClearDatagram (void)
403 SZ_Clear (&sv.datagram);
407 =============================================================================
409 The PVS must include a small area around the client to allow head bobbing
410 or other small motion on the client side. Otherwise, a bob might cause an
411 entity that should be visible to not show up, especially when the bob
414 =============================================================================
418 qbyte fatpvs[MAX_MAP_LEAFS/8];
420 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
429 // if this is a leaf, accumulate the pvs bits
430 if (node->contents < 0)
432 if (node->contents != CONTENTS_SOLID)
434 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
435 for (i=0 ; i<fatbytes ; i++)
442 d = DotProduct (org, plane->normal) - plane->dist;
444 node = node->children[0];
446 node = node->children[1];
449 SV_AddToFatPVS (org, node->children[0]);
450 node = node->children[1];
459 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
463 qbyte *SV_FatPVS (vec3_t org)
465 fatbytes = (sv.worldmodel->numleafs+31)>>3;
466 memset (fatpvs, 0, fatbytes);
467 SV_AddToFatPVS (org, sv.worldmodel->nodes);
471 //=============================================================================
474 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
478 if (node->contents < 0)
481 if (node->contents == CONTENTS_SOLID)
483 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
484 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
487 // node - recurse down the BSP tree
488 switch (BoxOnPlaneSide(mins, maxs, node->plane))
491 node = node->children[0];
494 node = node->children[1];
497 if (node->children[0]->contents != CONTENTS_SOLID)
498 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
500 node = node->children[1];
510 SV_WriteEntitiesToClient
515 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
517 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
518 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
520 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
521 float nextfullupdate, alphaf;
524 entity_state_t *baseline; // LordHavoc: delta or startup baseline
528 Mod_CheckLoaded(sv.worldmodel);
530 // find the client's PVS
531 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
532 pvs = SV_FatPVS (testeye);
540 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
541 // send all entities that touch the pvs
542 ent = NEXT_EDICT(sv.edicts);
543 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
547 // prevent delta compression against this frame (unless actually sent, which will restore this later)
548 nextfullupdate = client->nextfullupdate[e];
549 client->nextfullupdate[e] = -1;
551 if (ent != clent) // LordHavoc: always send player
553 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
555 if (val->edict != clentnum)
557 // don't show to anyone else
561 bits |= U_VIEWMODEL; // show relative to the view
565 // LordHavoc: never draw something told not to display to this client
566 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
568 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
575 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
576 glowsize = (int) val->_float >> 2;
577 if (glowsize > 255) glowsize = 255;
578 if (glowsize < 0) glowsize = 0;
580 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
581 if (val->_float != 0)
584 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
586 model = sv.models[(int)ent->v->modelindex];
587 Mod_CheckLoaded(model);
592 if (ent != clent) // LordHavoc: always send player
593 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
597 VectorCopy(ent->v->angles, angles);
598 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f)
600 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
601 // LordHavoc: trace predicted movement to avoid putting things in walls
602 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
603 VectorCopy(trace.endpos, origin);
607 VectorCopy(ent->v->origin, origin);
610 // ent has survived every check so far, check if it is visible
611 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
613 // use the predicted origin
614 entmins[0] = origin[0] - 1.0f;
615 entmins[1] = origin[1] - 1.0f;
616 entmins[2] = origin[2] - 1.0f;
617 entmaxs[0] = origin[0] + 1.0f;
618 entmaxs[1] = origin[1] + 1.0f;
619 entmaxs[2] = origin[2] + 1.0f;
620 // using the model's bounding box to ensure things are visible regardless of their physics box
623 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
625 VectorAdd(entmins, model->rotatedmins, entmins);
626 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
628 else if (ent->v->angles[1])
630 VectorAdd(entmins, model->yawmins, entmins);
631 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
635 VectorAdd(entmins, model->normalmins, entmins);
636 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
642 // if not touching a visible leaf
643 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
649 // or not visible through the portals
650 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
656 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
657 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
659 // LordHavoc: test random offsets, to maximize chance of detection
660 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
661 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
662 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
664 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
666 if (trace.fraction == 1)
667 client->visibletime[e] = realtime + 1;
670 //test nearest point on bbox
671 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
672 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
673 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
675 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
677 if (trace.fraction == 1)
678 client->visibletime[e] = realtime + 1;
679 else if (realtime > client->visibletime[e])
692 effects = ent->v->effects;
694 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
695 if (val->_float != 0)
696 alphaf = val->_float * 255.0f;
699 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
700 if (val->_float != 0)
701 alphaf = val->_float;
705 alpha = bound(0, alphaf, 255);
707 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
708 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
709 if (scale < 0) scale = 0;
710 if (scale > 255) scale = 255;
712 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
713 if (val->_float != 0)
714 glowcolor = (int) val->_float;
716 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
717 if (val->_float != 0)
718 effects |= EF_FULLBRIGHT;
722 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
726 // don't send if flagged for NODRAW and there are no effects
727 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
730 else // no model and no effects
735 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
737 Con_Printf ("packet overflow\n");
738 // mark the rest of the entities so they can't be delta compressed against this frame
739 for (;e < sv.num_edicts;e++)
741 client->nextfullupdate[e] = -1;
742 client->visibletime[e] = -1;
747 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
748 bits = bits | U_EXTERIORMODEL;
751 baseline = &ent->e->baseline;
753 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
755 // every half second a full update is forced
756 if (realtime < client->nextfullupdate[e])
759 baseline = &ent->e->deltabaseline;
762 nextfullupdate = realtime + 0.5f;
765 nextfullupdate = realtime + 0.5f;
767 // restore nextfullupdate since this is being sent for real
768 client->nextfullupdate[e] = nextfullupdate;
771 bits |= U_LONGENTITY;
773 if (ent->v->movetype == MOVETYPE_STEP)
776 // LordHavoc: old stuff, but rewritten to have more exact tolerances
777 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
778 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
779 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
780 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
781 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
782 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
783 if (baseline->colormap != (qbyte) ent->v->colormap) bits |= U_COLORMAP;
784 if (baseline->skin != (qbyte) ent->v->skin) bits |= U_SKIN;
785 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF)) bits |= U_FRAME;
786 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF)) bits |= U_EFFECTS;
787 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF)) bits |= U_MODEL;
789 // LordHavoc: new stuff
790 if (baseline->alpha != alpha) bits |= U_ALPHA;
791 if (baseline->scale != scale) bits |= U_SCALE;
792 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00)) bits |= U_EFFECTS2;
793 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
794 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
795 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00)) bits |= U_FRAME2;
796 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00)) bits |= U_MODEL2;
798 // update delta baseline
799 VectorCopy(ent->v->origin, ent->e->deltabaseline.origin);
800 VectorCopy(ent->v->angles, ent->e->deltabaseline.angles);
801 ent->e->deltabaseline.colormap = ent->v->colormap;
802 ent->e->deltabaseline.skin = ent->v->skin;
803 ent->e->deltabaseline.frame = ent->v->frame;
804 ent->e->deltabaseline.effects = ent->v->effects;
805 ent->e->deltabaseline.modelindex = ent->v->modelindex;
806 ent->e->deltabaseline.alpha = alpha;
807 ent->e->deltabaseline.scale = scale;
808 ent->e->deltabaseline.glowsize = glowsize;
809 ent->e->deltabaseline.glowcolor = glowcolor;
812 if (bits >= 16777216)
820 MSG_WriteByte (msg, bits);
821 if (bits & U_MOREBITS)
822 MSG_WriteByte (msg, bits>>8);
823 // LordHavoc: extend bytes have to be written here due to delta compression
824 if (bits & U_EXTEND1)
825 MSG_WriteByte (msg, bits>>16);
826 if (bits & U_EXTEND2)
827 MSG_WriteByte (msg, bits>>24);
829 // LordHavoc: old stuff
830 if (bits & U_LONGENTITY)
831 MSG_WriteShort (msg,e);
833 MSG_WriteByte (msg,e);
834 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v->modelindex);
835 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v->frame);
836 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v->colormap);
837 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v->skin);
838 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v->effects);
839 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
840 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
841 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
842 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
843 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
844 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
846 // LordHavoc: new stuff
847 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
848 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
849 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v->effects >> 8);
850 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
851 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
852 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v->frame >> 8);
853 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
856 if (sv_cullentities_stats.integer)
857 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);
860 static entity_frame_t sv_writeentitiestoclient_entityframe;
861 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
863 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
864 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
865 float alphaf, lightsize;
867 vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
874 if (client->sendsignon)
877 Mod_CheckLoaded(sv.worldmodel);
879 // find the client's PVS
880 // the real place being tested from
881 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
882 pvs = SV_FatPVS (testeye);
884 // the place being reported (to consider the fact the client still
885 // applies the view_ofs[2], so we have to only send the fractional part
886 // of view_ofs[2], undoing what the client will redo)
887 VectorCopy (testeye, testorigin);
888 e = (int) clent->v->view_ofs[2] & 255;
891 testorigin[2] -= (float) e;
892 EntityFrame_Clear(&sv_writeentitiestoclient_entityframe, testorigin);
900 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
901 // send all entities that touch the pvs
902 ent = NEXT_EDICT(sv.edicts);
903 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
909 if (ent != clent) // LordHavoc: always send player
911 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
913 if (val->edict == clentnum)
914 flags |= RENDER_VIEWMODEL; // show relative to the view
917 // don't show to anyone else
923 // LordHavoc: never draw something told not to display to this client
924 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
926 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
932 effects = ent->v->effects;
933 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
934 glowsize = (int) val->_float >> 2;
935 glowsize = bound(0, glowsize, 255);
938 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
940 if (effects & EF_BRIGHTFIELD)
941 lightsize = max(lightsize, 80);
942 if (effects & EF_MUZZLEFLASH)
943 lightsize = max(lightsize, 100);
944 if (effects & EF_BRIGHTLIGHT)
945 lightsize = max(lightsize, 400);
946 if (effects & EF_DIMLIGHT)
947 lightsize = max(lightsize, 200);
948 if (effects & EF_RED)
949 lightsize = max(lightsize, 200);
950 if (effects & EF_BLUE)
951 lightsize = max(lightsize, 200);
952 if (effects & EF_FLAME)
953 lightsize = max(lightsize, 250);
954 if (effects & EF_STARDUST)
955 lightsize = max(lightsize, 100);
958 lightsize = max(lightsize, glowsize << 2);
960 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
961 if (val->_float != 0)
963 flags |= RENDER_GLOWTRAIL;
964 lightsize = max(lightsize, 100);
968 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
970 modelindex = ent->v->modelindex;
971 model = sv.models[(int)ent->v->modelindex];
972 Mod_CheckLoaded(model);
977 if (ent != clent) // LordHavoc: always send player
978 if (lightsize == 0) // no effects
982 VectorCopy(ent->v->angles, angles);
983 if (DotProduct(ent->v->velocity, ent->v->velocity) >= 1.0f && host_client->latency >= 0.01f)
985 VectorMA(ent->v->origin, host_client->latency, ent->v->velocity, origin);
986 // LordHavoc: trace predicted movement to avoid putting things in walls
987 trace = SV_Move (ent->v->origin, ent->v->mins, ent->v->maxs, origin, MOVE_NORMAL, ent);
988 VectorCopy(trace.endpos, origin);
992 VectorCopy(ent->v->origin, origin);
995 // ent has survived every check so far, check if it is visible
996 // always send embedded brush models, they don't generate much traffic
997 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
999 // use the predicted origin
1000 entmins[0] = origin[0] - 1.0f;
1001 entmins[1] = origin[1] - 1.0f;
1002 entmins[2] = origin[2] - 1.0f;
1003 entmaxs[0] = origin[0] + 1.0f;
1004 entmaxs[1] = origin[1] + 1.0f;
1005 entmaxs[2] = origin[2] + 1.0f;
1006 // using the model's bounding box to ensure things are visible regardless of their physics box
1009 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
1011 VectorAdd(entmins, model->rotatedmins, entmins);
1012 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
1014 else if (ent->v->angles[1])
1016 VectorAdd(entmins, model->yawmins, entmins);
1017 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
1021 VectorAdd(entmins, model->normalmins, entmins);
1022 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1025 lightmins[0] = min(entmins[0], origin[0] - lightsize);
1026 lightmins[1] = min(entmins[1], origin[1] - lightsize);
1027 lightmins[2] = min(entmins[2], origin[2] - lightsize);
1028 lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
1029 lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
1030 lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
1034 // if not touching a visible leaf
1035 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->nodes))
1041 // or not visible through the portals
1042 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
1048 if (sv_cullentities_trace.integer)
1050 // LordHavoc: test center first
1051 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
1052 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
1053 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
1054 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1055 if (trace.fraction == 1)
1056 client->visibletime[e] = realtime + 1;
1059 // LordHavoc: test random offsets, to maximize chance of detection
1060 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1061 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1062 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1063 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1064 if (trace.fraction == 1)
1065 client->visibletime[e] = realtime + 1;
1070 // LordHavoc: test random offsets, to maximize chance of detection
1071 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1072 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1073 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1074 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1075 if (trace.fraction == 1)
1076 client->visibletime[e] = realtime + 1;
1079 if (realtime > client->visibletime[e])
1088 if (realtime > client->visibletime[e])
1103 effects = ent->v->effects;
1105 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1106 if (val->_float != 0)
1107 alphaf = val->_float * 255.0;
1110 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1111 if (val->_float != 0)
1112 alphaf = val->_float;
1116 alpha = bound(0, alphaf, 255);
1118 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1119 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1120 if (scale < 0) scale = 0;
1121 if (scale > 255) scale = 255;
1123 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1124 if (val->_float != 0)
1125 glowcolor = (int) val->_float;
1127 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1128 if (val->_float != 0)
1129 effects |= EF_FULLBRIGHT;
1133 if (lightsize == 0) // no effects
1137 // don't send if flagged for NODRAW and there are no effects
1138 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1141 else // no model and no effects
1146 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1147 flags |= RENDER_EXTERIORMODEL;
1149 if (ent->v->movetype == MOVETYPE_STEP)
1150 flags |= RENDER_STEP;
1151 // don't send an entity if it's coordinates would wrap around
1152 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1153 flags |= RENDER_LOWPRECISION;
1155 s = EntityFrame_NewEntity(&sv_writeentitiestoclient_entityframe, e);
1156 // if we run out of space, abort
1159 VectorCopy(origin, s->origin);
1160 VectorCopy(angles, s->angles);
1161 if (ent->v->colormap >= 1024)
1162 flags |= RENDER_COLORMAPPED;
1163 s->colormap = ent->v->colormap;
1164 s->skin = ent->v->skin;
1165 s->frame = ent->v->frame;
1166 s->modelindex = modelindex;
1167 s->effects = effects;
1170 s->glowsize = glowsize;
1171 s->glowcolor = glowcolor;
1174 sv_writeentitiestoclient_entityframe.framenum = ++client->entityframenumber;
1175 EntityFrame_Write(&client->entitydatabase, &sv_writeentitiestoclient_entityframe, msg);
1177 if (sv_cullentities_stats.integer)
1178 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);
1188 void SV_CleanupEnts (void)
1193 ent = NEXT_EDICT(sv.edicts);
1194 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1195 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1200 SV_WriteClientdataToMessage
1204 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1215 // send a damage message
1217 if (ent->v->dmg_take || ent->v->dmg_save)
1219 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1220 MSG_WriteByte (msg, svc_damage);
1221 MSG_WriteByte (msg, ent->v->dmg_save);
1222 MSG_WriteByte (msg, ent->v->dmg_take);
1223 for (i=0 ; i<3 ; i++)
1224 MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1226 ent->v->dmg_take = 0;
1227 ent->v->dmg_save = 0;
1231 // send the current viewpos offset from the view entity
1233 SV_SetIdealPitch (); // how much to look up / down ideally
1235 // a fixangle might get lost in a dropped packet. Oh well.
1236 if ( ent->v->fixangle )
1238 MSG_WriteByte (msg, svc_setangle);
1239 for (i=0 ; i < 3 ; i++)
1240 MSG_WriteAngle (msg, ent->v->angles[i] );
1241 ent->v->fixangle = 0;
1246 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1247 bits |= SU_VIEWHEIGHT;
1249 if (ent->v->idealpitch)
1250 bits |= SU_IDEALPITCH;
1252 // stuff the sigil bits into the high bits of items for sbar, or else
1254 val = GETEDICTFIELDVALUE(ent, eval_items2);
1257 items = (int)ent->v->items | ((int)val->_float << 23);
1259 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1263 if ( (int)ent->v->flags & FL_ONGROUND)
1264 bits |= SU_ONGROUND;
1266 if ( ent->v->waterlevel >= 2)
1270 VectorClear(punchvector);
1271 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1272 VectorCopy(val->vector, punchvector);
1275 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1277 i = val->_float * 255.0f;
1281 i = bound(0, i, 255);
1285 if (viewzoom != 255)
1286 bits |= SU_VIEWZOOM;
1288 for (i=0 ; i<3 ; i++)
1290 if (ent->v->punchangle[i])
1291 bits |= (SU_PUNCH1<<i);
1292 if (punchvector[i]) // dpprotocol
1293 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1294 if (ent->v->velocity[i])
1295 bits |= (SU_VELOCITY1<<i);
1298 if (ent->v->weaponframe)
1299 bits |= SU_WEAPONFRAME;
1301 if (ent->v->armorvalue)
1308 if (bits >= 16777216)
1313 MSG_WriteByte (msg, svc_clientdata);
1314 MSG_WriteShort (msg, bits);
1315 if (bits & SU_EXTEND1)
1316 MSG_WriteByte(msg, bits >> 16);
1317 if (bits & SU_EXTEND2)
1318 MSG_WriteByte(msg, bits >> 24);
1320 if (bits & SU_VIEWHEIGHT)
1321 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1323 if (bits & SU_IDEALPITCH)
1324 MSG_WriteChar (msg, ent->v->idealpitch);
1326 for (i=0 ; i<3 ; i++)
1328 if (bits & (SU_PUNCH1<<i))
1329 MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1330 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1331 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1332 if (bits & (SU_VELOCITY1<<i))
1333 MSG_WriteChar (msg, ent->v->velocity[i]/16);
1336 // [always sent] if (bits & SU_ITEMS)
1337 MSG_WriteLong (msg, items);
1339 if (bits & SU_WEAPONFRAME)
1340 MSG_WriteByte (msg, ent->v->weaponframe);
1341 if (bits & SU_ARMOR)
1342 MSG_WriteByte (msg, ent->v->armorvalue);
1343 if (bits & SU_WEAPON)
1344 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1346 MSG_WriteShort (msg, ent->v->health);
1347 MSG_WriteByte (msg, ent->v->currentammo);
1348 MSG_WriteByte (msg, ent->v->ammo_shells);
1349 MSG_WriteByte (msg, ent->v->ammo_nails);
1350 MSG_WriteByte (msg, ent->v->ammo_rockets);
1351 MSG_WriteByte (msg, ent->v->ammo_cells);
1353 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1357 if ( ((int)ent->v->weapon) & (1<<i) )
1359 MSG_WriteByte (msg, i);
1366 MSG_WriteByte (msg, ent->v->weapon);
1369 if (bits & SU_VIEWZOOM)
1370 MSG_WriteByte (msg, viewzoom);
1374 =======================
1375 SV_SendClientDatagram
1376 =======================
1378 static qbyte sv_sendclientdatagram_buf[MAX_DATAGRAM]; // FIXME?
1379 qboolean SV_SendClientDatagram (client_t *client)
1383 msg.data = sv_sendclientdatagram_buf;
1384 msg.maxsize = sizeof(sv_sendclientdatagram_buf);
1387 MSG_WriteByte (&msg, svc_time);
1388 MSG_WriteFloat (&msg, sv.time);
1390 if (client->spawned)
1392 // add the client specific data to the datagram
1393 SV_WriteClientdataToMessage (client->edict, &msg);
1395 SV_WriteEntitiesToClient (client, client->edict, &msg);
1397 // copy the server datagram if there is space
1398 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1399 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1402 // send the datagram
1403 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1405 SV_DropClient (true);// if the message couldn't send, kick off
1413 =======================
1414 SV_UpdateToReliableMessages
1415 =======================
1417 void SV_UpdateToReliableMessages (void)
1422 // check for changes to be sent over the reliable streams
1423 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1425 sv_player = host_client->edict;
1426 if (host_client->old_frags != sv_player->v->frags)
1428 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1430 if (!client->active || !client->spawned)
1432 MSG_WriteByte (&client->message, svc_updatefrags);
1433 MSG_WriteByte (&client->message, i);
1434 MSG_WriteShort (&client->message, sv_player->v->frags);
1437 host_client->old_frags = sv_player->v->frags;
1441 for (j=0, host_client = svs.clients ; j<svs.maxclients ; j++, host_client++)
1443 if (!host_client->active)
1445 SZ_Write (&host_client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1448 SZ_Clear (&sv.reliable_datagram);
1453 =======================
1456 Send a nop message without trashing or sending the accumulated client
1458 =======================
1460 void SV_SendNop (client_t *client)
1466 msg.maxsize = sizeof(buf);
1469 MSG_WriteChar (&msg, svc_nop);
1471 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1472 SV_DropClient (true); // if the message couldn't send, kick off
1473 client->last_message = realtime;
1477 =======================
1478 SV_SendClientMessages
1479 =======================
1481 void SV_SendClientMessages (void)
1485 // update frags, names, etc
1486 SV_UpdateToReliableMessages ();
1488 // build individual updates
1489 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1491 if (!host_client->active)
1494 if (host_client->spawned)
1496 if (!SV_SendClientDatagram (host_client))
1501 // the player isn't totally in the game yet
1502 // send small keepalive messages if too much time has passed
1503 // send a full message when the next signon stage has been requested
1504 // some other message data (name changes, etc) may accumulate
1505 // between signon stages
1506 if (host_client->sendserverinfo)
1508 Con_DPrintf("SV_SendClientMessages: sending server info to new client\n");
1509 SV_SendServerinfo (host_client);
1510 host_client->sendserverinfo = false;
1512 if (!host_client->sendsignon)
1514 if (realtime - host_client->last_message > 5)
1515 SV_SendNop (host_client);
1516 continue; // don't send out non-signon messages
1520 // check for an overflowed message. Should only happen
1521 // on a very fucked up connection that backs up a lot, then
1523 if (host_client->message.overflowed)
1525 SV_DropClient (true); // overflowed
1526 host_client->message.overflowed = false;
1530 if (host_client->message.cursize || host_client->dropasap)
1532 if (!NET_CanSendMessage (host_client->netconnection))
1535 if (host_client->dropasap)
1536 SV_DropClient (false); // went to another level
1539 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1540 SV_DropClient (true); // if the message couldn't send, kick off
1541 SZ_Clear (&host_client->message);
1542 host_client->last_message = realtime;
1543 host_client->sendsignon = false;
1549 // clear muzzle flashes
1555 ==============================================================================
1559 ==============================================================================
1568 int SV_ModelIndex (const char *name)
1572 if (!name || !name[0])
1575 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1576 if (!strcmp(sv.model_precache[i], name))
1578 if (i==MAX_MODELS || !sv.model_precache[i])
1579 Host_Error ("SV_ModelIndex: model %s not precached", name);
1583 #ifdef SV_QUAKEENTITIES
1590 void SV_CreateBaseline (void)
1592 int i, entnum, large;
1595 // LordHavoc: clear *all* states (note just active ones)
1596 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1598 // get the current server version
1599 svent = EDICT_NUM(entnum);
1601 // LordHavoc: always clear state values, whether the entity is in use or not
1602 ClearStateToDefault(&svent->e->baseline);
1606 if (entnum > svs.maxclients && !svent->v->modelindex)
1609 // create entity baseline
1610 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1611 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1612 svent->e->baseline.frame = svent->v->frame;
1613 svent->e->baseline.skin = svent->v->skin;
1614 if (entnum > 0 && entnum <= svs.maxclients)
1616 svent->e->baseline.colormap = entnum;
1617 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1621 svent->e->baseline.colormap = 0;
1622 svent->e->baseline.modelindex = svent->v->modelindex;
1626 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1629 // add to the message
1631 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1633 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1634 MSG_WriteShort (&sv.signon, entnum);
1638 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1639 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1643 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1644 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1646 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1647 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1648 for (i=0 ; i<3 ; i++)
1650 MSG_WriteDPCoord(&sv.signon, svent->e->baseline.origin[i]);
1651 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i]);
1662 Tell all the clients that the server is changing levels
1665 void SV_SendReconnect (void)
1672 msg.maxsize = sizeof(data);
1674 MSG_WriteChar (&msg, svc_stufftext);
1675 MSG_WriteString (&msg, "reconnect\n");
1676 NET_SendToAll (&msg, 5);
1678 if (cls.state != ca_dedicated)
1679 Cmd_ExecuteString ("reconnect\n", src_command);
1687 Grabs the current state of each client for saving across the
1688 transition to another level
1691 void SV_SaveSpawnparms (void)
1695 svs.serverflags = pr_global_struct->serverflags;
1697 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1699 if (!host_client->active)
1702 // call the progs to get default spawn parms for the new client
1703 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1704 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1705 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1706 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1710 void SV_IncreaseEdicts(void)
1714 int oldmax_edicts = sv.max_edicts;
1715 void *oldedictsengineprivate = sv.edictsengineprivate;
1716 void *oldedictsfields = sv.edictsfields;
1717 void *oldmoved_edicts = sv.moved_edicts;
1719 // links don't survive the transition, so unlink everything
1720 for (i = 0;i < sv.max_edicts;i++)
1721 SV_UnlinkEdict (sv.edicts + i);
1724 sv.max_edicts = min(sv.max_edicts + 32, MAX_EDICTS);
1725 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1726 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1727 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1729 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1730 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1732 for (i = 0;i < sv.max_edicts;i++)
1734 ent = sv.edicts + i;
1735 ent->e = sv.edictsengineprivate + i;
1736 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1737 // link every entity except world
1739 SV_LinkEdict(ent, false);
1742 Mem_Free(oldedictsengineprivate);
1743 Mem_Free(oldedictsfields);
1744 Mem_Free(oldmoved_edicts);
1751 This is called at the start of each level
1754 extern float scr_centertime_off;
1756 void SV_SpawnServer (const char *server)
1762 // let's not have any servers with no name
1763 if (hostname.string[0] == 0)
1764 Cvar_Set ("hostname", "UNNAMED");
1765 scr_centertime_off = 0;
1767 Con_DPrintf ("SpawnServer: %s\n",server);
1768 svs.changelevel_issued = false; // now safe to issue another
1771 // tell all connected clients that we are going to a new level
1774 SV_SendReconnect ();
1777 // make cvars consistant
1780 Cvar_SetValue ("deathmatch", 0);
1781 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1783 Cvar_SetValue ("skill", (float)current_skill);
1786 // set up the new server
1788 Host_ClearMemory ();
1790 memset (&sv, 0, sizeof(sv));
1792 strcpy (sv.name, server);
1794 // load progs to get entity field count
1797 // allocate server memory
1798 // start out with just enough room for clients and a reasonable estimate of entities
1799 sv.max_edicts = ((svs.maxclients + 128) + 31) & ~31;
1800 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1802 // clear the edict memory pool
1803 Mem_EmptyPool(sv_edicts_mempool);
1804 // edict_t structures (hidden from progs)
1805 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1806 // engine private structures (hidden from progs)
1807 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1808 // progs fields, often accessed by server
1809 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1810 // used by PushMove to move back pushed entities
1811 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1812 for (i = 0;i < sv.max_edicts;i++)
1814 ent = sv.edicts + i;
1815 ent->e = sv.edictsengineprivate + i;
1816 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1819 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1820 sv.datagram.cursize = 0;
1821 sv.datagram.data = sv.datagram_buf;
1823 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1824 sv.reliable_datagram.cursize = 0;
1825 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1827 sv.signon.maxsize = sizeof(sv.signon_buf);
1828 sv.signon.cursize = 0;
1829 sv.signon.data = sv.signon_buf;
1831 // leave slots at start for clients only
1832 sv.num_edicts = svs.maxclients+1;
1833 for (i=0 ; i<svs.maxclients ; i++)
1834 svs.clients[i].edict = EDICT_NUM(i+1);
1836 sv.state = ss_loading;
1843 strcpy (sv.name, server);
1844 sprintf (sv.modelname,"maps/%s.bsp", server);
1845 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1848 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1852 sv.models[1] = sv.worldmodel;
1855 // clear world interaction links
1859 sv.sound_precache[0] = "";
1861 sv.model_precache[0] = "";
1862 sv.model_precache[1] = sv.modelname;
1863 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1865 sv.model_precache[i+1] = localmodels[i];
1866 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1870 // load the rest of the entities
1873 memset (ent->v, 0, progs->entityfields * 4);
1874 ent->e->free = false;
1875 ent->v->model = PR_SetString(sv.modelname);
1876 ent->v->modelindex = 1; // world model
1877 ent->v->solid = SOLID_BSP;
1878 ent->v->movetype = MOVETYPE_PUSH;
1881 pr_global_struct->coop = coop.integer;
1883 pr_global_struct->deathmatch = deathmatch.integer;
1885 pr_global_struct->mapname = PR_SetString(sv.name);
1887 // serverflags are for cross level information (sigils)
1888 pr_global_struct->serverflags = svs.serverflags;
1890 // load replacement entity file if found
1892 if (sv_entpatch.integer)
1893 entities = FS_LoadFile(va("maps/%s.ent", sv.name), true);
1896 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1897 ED_LoadFromFile (entities);
1901 ED_LoadFromFile (sv.worldmodel->entities);
1904 // LordHavoc: clear world angles (to fix e3m3.bsp)
1905 VectorClear(sv.edicts->v->angles);
1909 // all setup is completed, any further precache statements are errors
1910 sv.state = ss_active;
1912 // run two frames to allow everything to settle
1913 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1915 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1920 #ifdef QUAKEENTITIES
1921 // create a baseline for more efficient communications
1922 SV_CreateBaseline ();
1925 // send serverinfo to all connected clients
1926 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1927 if (host_client->active)
1928 SV_SendServerinfo (host_client);
1930 Con_DPrintf ("Server spawned.\n");