2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_main.c -- server main program
24 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "0"}; // fast but loose
25 static cvar_t sv_cullentities_portal = {0, "sv_cullentities_portal", "0"}; // extremely accurate visibility checking, but too slow
26 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "1"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
27 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
32 static char localmodels[MAX_MODELS][5]; // inline model names for precache
34 static mempool_t *sv_edicts_mempool = NULL;
36 //============================================================================
47 Cvar_RegisterVariable (&sv_maxvelocity);
48 Cvar_RegisterVariable (&sv_gravity);
49 Cvar_RegisterVariable (&sv_friction);
50 Cvar_RegisterVariable (&sv_edgefriction);
51 Cvar_RegisterVariable (&sv_stopspeed);
52 Cvar_RegisterVariable (&sv_maxspeed);
53 Cvar_RegisterVariable (&sv_accelerate);
54 Cvar_RegisterVariable (&sv_idealpitchscale);
55 Cvar_RegisterVariable (&sv_aim);
56 Cvar_RegisterVariable (&sv_nostep);
57 Cvar_RegisterVariable (&sv_predict);
58 Cvar_RegisterVariable (&sv_deltacompress);
59 Cvar_RegisterVariable (&sv_cullentities_pvs);
60 Cvar_RegisterVariable (&sv_cullentities_portal);
61 Cvar_RegisterVariable (&sv_cullentities_trace);
62 Cvar_RegisterVariable (&sv_cullentities_stats);
64 for (i = 0;i < MAX_MODELS;i++)
65 sprintf (localmodels[i], "*%i", i);
67 sv_edicts_mempool = Mem_AllocPool("edicts");
71 =============================================================================
75 =============================================================================
82 Make sure the event gets sent to all clients
85 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
89 if (sv.datagram.cursize > MAX_DATAGRAM-16)
91 MSG_WriteByte (&sv.datagram, svc_particle);
92 MSG_WriteFloatCoord (&sv.datagram, org[0]);
93 MSG_WriteFloatCoord (&sv.datagram, org[1]);
94 MSG_WriteFloatCoord (&sv.datagram, org[2]);
102 MSG_WriteChar (&sv.datagram, v);
104 MSG_WriteByte (&sv.datagram, count);
105 MSG_WriteByte (&sv.datagram, color);
112 Make sure the event gets sent to all clients
115 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
117 if (sv.datagram.cursize > MAX_DATAGRAM-18)
119 if (modelindex >= 256 || startframe >= 256)
121 MSG_WriteByte (&sv.datagram, svc_effect2);
122 MSG_WriteFloatCoord (&sv.datagram, org[0]);
123 MSG_WriteFloatCoord (&sv.datagram, org[1]);
124 MSG_WriteFloatCoord (&sv.datagram, org[2]);
125 MSG_WriteShort (&sv.datagram, modelindex);
126 MSG_WriteShort (&sv.datagram, startframe);
127 MSG_WriteByte (&sv.datagram, framecount);
128 MSG_WriteByte (&sv.datagram, framerate);
132 MSG_WriteByte (&sv.datagram, svc_effect);
133 MSG_WriteFloatCoord (&sv.datagram, org[0]);
134 MSG_WriteFloatCoord (&sv.datagram, org[1]);
135 MSG_WriteFloatCoord (&sv.datagram, org[2]);
136 MSG_WriteByte (&sv.datagram, modelindex);
137 MSG_WriteByte (&sv.datagram, startframe);
138 MSG_WriteByte (&sv.datagram, framecount);
139 MSG_WriteByte (&sv.datagram, framerate);
147 Each entity can have eight independant sound sources, like voice,
150 Channel 0 is an auto-allocate channel, the others override anything
151 already running on that entity/channel pair.
153 An attenuation of 0 will play full volume everywhere in the level.
154 Larger attenuations will drop off. (max 4 attenuation)
158 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
166 if (volume < 0 || volume > 255)
167 Host_Error ("SV_StartSound: volume = %i", volume);
169 if (attenuation < 0 || attenuation > 4)
170 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
172 if (channel < 0 || channel > 7)
173 Host_Error ("SV_StartSound: channel = %i", channel);
175 if (sv.datagram.cursize > MAX_DATAGRAM-16)
178 // find precache number for sound
179 for (sound_num=1 ; sound_num<MAX_SOUNDS
180 && sv.sound_precache[sound_num] ; sound_num++)
181 if (!strcmp(sample, sv.sound_precache[sound_num]))
184 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
186 Con_Printf ("SV_StartSound: %s not precached\n", sample);
190 ent = NUM_FOR_EDICT(entity);
192 channel = (ent<<3) | channel;
195 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
196 field_mask |= SND_VOLUME;
197 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
198 field_mask |= SND_ATTENUATION;
200 // directed messages go only to the entity they are targeted on
201 if (sound_num >= 256)
202 MSG_WriteByte (&sv.datagram, svc_sound2);
204 MSG_WriteByte (&sv.datagram, svc_sound);
205 MSG_WriteByte (&sv.datagram, field_mask);
206 if (field_mask & SND_VOLUME)
207 MSG_WriteByte (&sv.datagram, volume);
208 if (field_mask & SND_ATTENUATION)
209 MSG_WriteByte (&sv.datagram, attenuation*64);
210 MSG_WriteShort (&sv.datagram, channel);
211 if (sound_num >= 256)
212 MSG_WriteShort (&sv.datagram, sound_num);
214 MSG_WriteByte (&sv.datagram, sound_num);
215 for (i=0 ; i<3 ; i++)
216 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
220 ==============================================================================
224 ==============================================================================
231 Sends the first message from the server to a connected client.
232 This will be sent on the initial connection and upon each server load.
235 void SV_SendServerinfo (client_t *client)
240 MSG_WriteByte (&client->message, svc_print);
241 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
242 MSG_WriteString (&client->message,message);
244 MSG_WriteByte (&client->message, svc_serverinfo);
245 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
246 MSG_WriteByte (&client->message, svs.maxclients);
248 if (!coop.integer && deathmatch.integer)
249 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
251 MSG_WriteByte (&client->message, GAME_COOP);
253 sprintf (message, pr_strings+sv.edicts->v.message);
255 MSG_WriteString (&client->message,message);
257 for (s = sv.model_precache+1 ; *s ; s++)
258 MSG_WriteString (&client->message, *s);
259 MSG_WriteByte (&client->message, 0);
261 for (s = sv.sound_precache+1 ; *s ; s++)
262 MSG_WriteString (&client->message, *s);
263 MSG_WriteByte (&client->message, 0);
266 MSG_WriteByte (&client->message, svc_cdtrack);
267 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
268 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
271 MSG_WriteByte (&client->message, svc_setview);
272 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
274 MSG_WriteByte (&client->message, svc_signonnum);
275 MSG_WriteByte (&client->message, 1);
277 client->sendsignon = true;
278 client->spawned = false; // need prespawn, spawn, etc
285 Initializes a client_t for a new net connection. This will only be called
286 once for a player each game, not once for each level change.
289 void SV_ConnectClient (int clientnum)
294 struct qsocket_s *netconnection;
296 float spawn_parms[NUM_SPAWN_PARMS];
298 client = svs.clients + clientnum;
300 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
302 edictnum = clientnum+1;
304 ent = EDICT_NUM(edictnum);
306 // set up the client_t
307 netconnection = client->netconnection;
310 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
311 memset (client, 0, sizeof(*client));
312 client->netconnection = netconnection;
314 strcpy (client->name, "unconnected");
315 client->active = true;
316 client->spawned = false;
318 client->message.data = client->msgbuf;
319 client->message.maxsize = sizeof(client->msgbuf);
320 client->message.allowoverflow = true; // we can catch it
323 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
326 // call the progs to get default spawn parms for the new client
327 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
328 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
329 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
332 SV_SendServerinfo (client);
338 SV_CheckForNewClients
342 void SV_CheckForNewClients (void)
344 struct qsocket_s *ret;
348 // check for new connections
352 ret = NET_CheckNewConnections ();
357 // init a new client structure
359 for (i=0 ; i<svs.maxclients ; i++)
360 if (!svs.clients[i].active)
362 if (i == svs.maxclients)
363 Sys_Error ("Host_CheckForNewClients: no free clients");
365 svs.clients[i].netconnection = ret;
366 SV_ConnectClient (i);
368 net_activeconnections++;
375 ===============================================================================
379 ===============================================================================
388 void SV_ClearDatagram (void)
390 SZ_Clear (&sv.datagram);
394 =============================================================================
396 The PVS must include a small area around the client to allow head bobbing
397 or other small motion on the client side. Otherwise, a bob might cause an
398 entity that should be visible to not show up, especially when the bob
401 =============================================================================
405 byte fatpvs[MAX_MAP_LEAFS/8];
407 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
416 // if this is a leaf, accumulate the pvs bits
417 if (node->contents < 0)
419 if (node->contents != CONTENTS_SOLID)
421 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
422 for (i=0 ; i<fatbytes ; i++)
429 d = DotProduct (org, plane->normal) - plane->dist;
431 node = node->children[0];
433 node = node->children[1];
436 SV_AddToFatPVS (org, node->children[0]);
437 node = node->children[1];
446 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
450 byte *SV_FatPVS (vec3_t org)
452 fatbytes = (sv.worldmodel->numleafs+31)>>3;
453 memset (fatpvs, 0, fatbytes);
454 SV_AddToFatPVS (org, sv.worldmodel->nodes);
458 //=============================================================================
461 int SV_BoxTouchingPVS (byte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
465 if (node->contents < 0)
468 if (node->contents == CONTENTS_SOLID)
470 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
471 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
474 // node - recurse down the BSP tree
475 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
478 node = node->children[0];
481 node = node->children[1];
484 if (node->children[0]->contents != CONTENTS_SOLID)
485 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
487 node = node->children[1];
497 SV_WriteEntitiesToClient
501 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
503 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
504 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
506 vec3_t org, origin, angles, entmins, entmaxs, testorigin;
507 float nextfullupdate;
510 entity_state_t *baseline; // LordHavoc: delta or startup baseline
514 Mod_CheckLoaded(sv.worldmodel);
516 // find the client's PVS
517 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
518 pvs = SV_FatPVS (org);
521 MSG_WriteByte(msg, svc_playerposition);
522 MSG_WriteFloat(msg, org[0]);
523 MSG_WriteFloat(msg, org[1]);
524 MSG_WriteFloat(msg, org[2]);
533 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
534 // send all entities that touch the pvs
535 ent = NEXT_EDICT(sv.edicts);
536 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
540 // prevent delta compression against this frame (unless actually sent, which will restore this later)
541 nextfullupdate = client->nextfullupdate[e];
542 client->nextfullupdate[e] = -1;
544 if (ent != clent) // LordHavoc: always send player
546 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
548 if (val->edict != clentnum)
550 // don't show to anyone else
554 bits |= U_VIEWMODEL; // show relative to the view
558 // LordHavoc: never draw something told not to display to this client
559 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
561 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
568 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
569 glowsize = (int) val->_float >> 2;
570 if (glowsize > 255) glowsize = 255;
571 if (glowsize < 0) glowsize = 0;
573 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
574 if (val->_float != 0)
577 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
579 model = sv.models[(int)ent->v.modelindex];
580 Mod_CheckLoaded(model);
585 if (ent != clent) // LordHavoc: always send player
586 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
590 VectorCopy(ent->v.angles, angles);
591 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
593 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
594 // LordHavoc: trace predicted movement to avoid putting things in walls
595 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
596 VectorCopy(trace.endpos, origin);
600 VectorCopy(ent->v.origin, origin);
603 // ent has survived every check so far, check if it is visible
604 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
606 // use the predicted origin
607 entmins[0] = origin[0] - 1.0f;
608 entmins[1] = origin[1] - 1.0f;
609 entmins[2] = origin[2] - 1.0f;
610 entmaxs[0] = origin[0] + 1.0f;
611 entmaxs[1] = origin[1] + 1.0f;
612 entmaxs[2] = origin[2] + 1.0f;
613 // using the model's bounding box to ensure things are visible regardless of their physics box
616 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
618 VectorAdd(entmins, model->rotatedmins, entmins);
619 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
621 else if (ent->v.angles[1])
623 VectorAdd(entmins, model->yawmins, entmins);
624 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
628 VectorAdd(entmins, model->normalmins, entmins);
629 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
635 // if not touching a visible leaf
636 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
642 // or not visible through the portals
643 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
649 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
650 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
652 // LordHavoc: test random offsets, to maximize chance of detection
653 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
654 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
655 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
657 memset (&trace, 0, sizeof(trace_t));
659 trace.allsolid = true;
660 VectorCopy(testorigin, trace.endpos);
662 VectorCopy(org, RecursiveHullCheckInfo.start);
663 VectorSubtract(testorigin, org, RecursiveHullCheckInfo.dist);
664 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
665 RecursiveHullCheckInfo.trace = &trace;
666 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, org, testorigin);
668 if (trace.fraction == 1)
669 client->lastvisible[e] = realtime;
672 if (realtime - client->lastvisible[e] >= 1)
685 effects = ent->v.effects;
687 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
688 if (val->_float != 0)
689 alpha = (int) (val->_float * 255.0);
692 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
693 if (val->_float != 0)
694 alpha = (int) val->_float;
698 alpha = bound(0, alpha, 255);
700 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
701 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
702 if (scale < 0) scale = 0;
703 if (scale > 255) scale = 255;
705 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
706 if (val->_float != 0)
707 glowcolor = (int) val->_float;
709 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
710 if (val->_float != 0)
711 effects |= EF_FULLBRIGHT;
715 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
719 // don't send if flagged for NODRAW and there are no effects
720 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
723 else // no model and no effects
728 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
730 Con_Printf ("packet overflow\n");
731 // mark the rest of the entities so they can't be delta compressed against this frame
732 for (;e < sv.num_edicts;e++)
734 client->nextfullupdate[e] = -1;
735 client->lastvisible[e] = -1;
740 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
741 bits = bits | U_EXTERIORMODEL;
744 baseline = &ent->baseline;
746 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.integer)
748 // every half second a full update is forced
749 if (realtime < client->nextfullupdate[e])
752 baseline = &ent->deltabaseline;
755 nextfullupdate = realtime + 0.5f;
758 nextfullupdate = realtime + 0.5f;
760 // restore nextfullupdate since this is being sent for real
761 client->nextfullupdate[e] = nextfullupdate;
764 bits |= U_LONGENTITY;
766 if (ent->v.movetype == MOVETYPE_STEP)
769 // LordHavoc: old stuff, but rewritten to have more exact tolerances
770 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
771 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
772 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
773 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
774 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
775 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
776 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
777 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
778 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
779 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
780 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
781 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
782 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
783 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
785 // LordHavoc: new stuff
786 if (baseline->alpha != alpha) bits |= U_ALPHA;
787 if (baseline->scale != scale) bits |= U_SCALE;
788 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
789 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
790 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
791 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
792 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
794 // update delta baseline
795 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
796 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
797 ent->deltabaseline.colormap = ent->v.colormap;
798 ent->deltabaseline.skin = ent->v.skin;
799 ent->deltabaseline.frame = ent->v.frame;
800 ent->deltabaseline.effects = ent->v.effects;
801 ent->deltabaseline.modelindex = ent->v.modelindex;
802 ent->deltabaseline.alpha = alpha;
803 ent->deltabaseline.scale = scale;
804 ent->deltabaseline.glowsize = glowsize;
805 ent->deltabaseline.glowcolor = glowcolor;
808 if (bits >= 16777216)
816 MSG_WriteByte (msg, bits);
817 if (bits & U_MOREBITS)
818 MSG_WriteByte (msg, bits>>8);
819 // LordHavoc: extend bytes have to be written here due to delta compression
820 if (bits & U_EXTEND1)
821 MSG_WriteByte (msg, bits>>16);
822 if (bits & U_EXTEND2)
823 MSG_WriteByte (msg, bits>>24);
825 // LordHavoc: old stuff
826 if (bits & U_LONGENTITY)
827 MSG_WriteShort (msg,e);
829 MSG_WriteByte (msg,e);
830 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
831 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
832 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
833 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
834 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
835 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
836 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
837 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
838 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
839 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
840 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
842 // LordHavoc: new stuff
843 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
844 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
845 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
846 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
847 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
848 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
849 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
852 if (sv_cullentities_stats.integer)
853 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);
862 void SV_CleanupEnts (void)
867 ent = NEXT_EDICT(sv.edicts);
868 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
869 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
874 SV_WriteClientdataToMessage
878 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
888 // send a damage message
890 if (ent->v.dmg_take || ent->v.dmg_save)
892 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
893 MSG_WriteByte (msg, svc_damage);
894 MSG_WriteByte (msg, ent->v.dmg_save);
895 MSG_WriteByte (msg, ent->v.dmg_take);
896 for (i=0 ; i<3 ; i++)
897 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
904 // send the current viewpos offset from the view entity
906 SV_SetIdealPitch (); // how much to look up / down ideally
908 // a fixangle might get lost in a dropped packet. Oh well.
909 if ( ent->v.fixangle )
911 MSG_WriteByte (msg, svc_setangle);
912 for (i=0 ; i < 3 ; i++)
913 MSG_WriteAngle (msg, ent->v.angles[i] );
919 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
920 bits |= SU_VIEWHEIGHT;
922 if (ent->v.idealpitch)
923 bits |= SU_IDEALPITCH;
925 // stuff the sigil bits into the high bits of items for sbar, or else
927 val = GETEDICTFIELDVALUE(ent, eval_items2);
930 items = (int)ent->v.items | ((int)val->_float << 23);
932 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
936 if ( (int)ent->v.flags & FL_ONGROUND)
939 if ( ent->v.waterlevel >= 2)
943 VectorClear(punchvector);
944 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
945 VectorCopy(val->vector, punchvector);
947 for (i=0 ; i<3 ; i++)
949 if (ent->v.punchangle[i])
950 bits |= (SU_PUNCH1<<i);
951 if (punchvector[i]) // dpprotocol
952 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
953 if (ent->v.velocity[i])
954 bits |= (SU_VELOCITY1<<i);
957 if (ent->v.weaponframe)
958 bits |= SU_WEAPONFRAME;
960 if (ent->v.armorvalue)
963 // if (ent->v.weapon)
968 if (bits >= 16777216)
973 MSG_WriteByte (msg, svc_clientdata);
974 MSG_WriteShort (msg, bits);
975 if (bits & SU_EXTEND1)
976 MSG_WriteByte(msg, bits >> 16);
977 if (bits & SU_EXTEND2)
978 MSG_WriteByte(msg, bits >> 24);
980 if (bits & SU_VIEWHEIGHT)
981 MSG_WriteChar (msg, ent->v.view_ofs[2]);
983 if (bits & SU_IDEALPITCH)
984 MSG_WriteChar (msg, ent->v.idealpitch);
986 for (i=0 ; i<3 ; i++)
988 if (bits & (SU_PUNCH1<<i))
989 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
990 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
991 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
992 if (bits & (SU_VELOCITY1<<i))
993 MSG_WriteChar (msg, ent->v.velocity[i]/16);
996 // [always sent] if (bits & SU_ITEMS)
997 MSG_WriteLong (msg, items);
999 if (bits & SU_WEAPONFRAME)
1000 MSG_WriteByte (msg, ent->v.weaponframe);
1001 if (bits & SU_ARMOR)
1002 MSG_WriteByte (msg, ent->v.armorvalue);
1003 if (bits & SU_WEAPON)
1004 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1006 MSG_WriteShort (msg, ent->v.health);
1007 MSG_WriteByte (msg, ent->v.currentammo);
1008 MSG_WriteByte (msg, ent->v.ammo_shells);
1009 MSG_WriteByte (msg, ent->v.ammo_nails);
1010 MSG_WriteByte (msg, ent->v.ammo_rockets);
1011 MSG_WriteByte (msg, ent->v.ammo_cells);
1013 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1017 if ( ((int)ent->v.weapon) & (1<<i) )
1019 MSG_WriteByte (msg, i);
1026 MSG_WriteByte (msg, ent->v.weapon);
1031 =======================
1032 SV_SendClientDatagram
1033 =======================
1035 qboolean SV_SendClientDatagram (client_t *client)
1037 byte buf[MAX_DATAGRAM];
1041 msg.maxsize = sizeof(buf);
1044 MSG_WriteByte (&msg, svc_time);
1045 MSG_WriteFloat (&msg, sv.time);
1047 // add the client specific data to the datagram
1048 SV_WriteClientdataToMessage (client->edict, &msg);
1050 SV_WriteEntitiesToClient (client, client->edict, &msg);
1052 // copy the server datagram if there is space
1053 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1054 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1056 // send the datagram
1057 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1059 SV_DropClient (true);// if the message couldn't send, kick off
1067 =======================
1068 SV_UpdateToReliableMessages
1069 =======================
1071 void SV_UpdateToReliableMessages (void)
1076 // check for changes to be sent over the reliable streams
1077 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1079 if (host_client->old_frags != host_client->edict->v.frags)
1081 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1083 if (!client->active)
1085 MSG_WriteByte (&client->message, svc_updatefrags);
1086 MSG_WriteByte (&client->message, i);
1087 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1090 host_client->old_frags = host_client->edict->v.frags;
1094 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1096 if (!client->active)
1098 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1101 SZ_Clear (&sv.reliable_datagram);
1106 =======================
1109 Send a nop message without trashing or sending the accumulated client
1111 =======================
1113 void SV_SendNop (client_t *client)
1119 msg.maxsize = sizeof(buf);
1122 MSG_WriteChar (&msg, svc_nop);
1124 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1125 SV_DropClient (true); // if the message couldn't send, kick off
1126 client->last_message = realtime;
1130 =======================
1131 SV_SendClientMessages
1132 =======================
1134 void SV_SendClientMessages (void)
1138 // update frags, names, etc
1139 SV_UpdateToReliableMessages ();
1141 // build individual updates
1142 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1144 if (!host_client->active)
1147 if (host_client->spawned)
1149 if (!SV_SendClientDatagram (host_client))
1154 // the player isn't totally in the game yet
1155 // send small keepalive messages if too much time has passed
1156 // send a full message when the next signon stage has been requested
1157 // some other message data (name changes, etc) may accumulate
1158 // between signon stages
1159 if (!host_client->sendsignon)
1161 if (realtime - host_client->last_message > 5)
1162 SV_SendNop (host_client);
1163 continue; // don't send out non-signon messages
1167 // check for an overflowed message. Should only happen
1168 // on a very fucked up connection that backs up a lot, then
1170 if (host_client->message.overflowed)
1172 SV_DropClient (true);
1173 host_client->message.overflowed = false;
1177 if (host_client->message.cursize || host_client->dropasap)
1179 if (!NET_CanSendMessage (host_client->netconnection))
1181 // I_Printf ("can't write\n");
1185 if (host_client->dropasap)
1186 SV_DropClient (false); // went to another level
1189 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1190 SV_DropClient (true); // if the message couldn't send, kick off
1191 SZ_Clear (&host_client->message);
1192 host_client->last_message = realtime;
1193 host_client->sendsignon = false;
1199 // clear muzzle flashes
1205 ==============================================================================
1209 ==============================================================================
1218 int SV_ModelIndex (char *name)
1222 if (!name || !name[0])
1225 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1226 if (!strcmp(sv.model_precache[i], name))
1228 if (i==MAX_MODELS || !sv.model_precache[i])
1229 Host_Error ("SV_ModelIndex: model %s not precached", name);
1239 void SV_CreateBaseline (void)
1241 int i, entnum, large;
1244 // LordHavoc: clear *all* states (note just active ones)
1245 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1247 // get the current server version
1248 svent = EDICT_NUM(entnum);
1250 // LordHavoc: always clear state values, whether the entity is in use or not
1251 ClearStateToDefault(&svent->baseline);
1255 if (entnum > svs.maxclients && !svent->v.modelindex)
1258 // create entity baseline
1259 VectorCopy (svent->v.origin, svent->baseline.origin);
1260 VectorCopy (svent->v.angles, svent->baseline.angles);
1261 svent->baseline.frame = svent->v.frame;
1262 svent->baseline.skin = svent->v.skin;
1263 if (entnum > 0 && entnum <= svs.maxclients)
1265 svent->baseline.colormap = entnum;
1266 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1270 svent->baseline.colormap = 0;
1271 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1275 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1278 // add to the message
1280 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1282 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1283 MSG_WriteShort (&sv.signon, entnum);
1287 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1288 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1292 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1293 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1295 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1296 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1297 for (i=0 ; i<3 ; i++)
1299 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1300 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1310 Tell all the clients that the server is changing levels
1313 void SV_SendReconnect (void)
1320 msg.maxsize = sizeof(data);
1322 MSG_WriteChar (&msg, svc_stufftext);
1323 MSG_WriteString (&msg, "reconnect\n");
1324 NET_SendToAll (&msg, 5);
1326 if (cls.state != ca_dedicated)
1327 Cmd_ExecuteString ("reconnect\n", src_command);
1335 Grabs the current state of each client for saving across the
1336 transition to another level
1339 void SV_SaveSpawnparms (void)
1343 svs.serverflags = pr_global_struct->serverflags;
1345 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1347 if (!host_client->active)
1350 // call the progs to get default spawn parms for the new client
1351 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1352 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1353 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1354 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1362 This is called at the start of each level
1365 extern float scr_centertime_off;
1367 void SV_SpawnServer (char *server)
1372 // let's not have any servers with no name
1373 if (hostname.string[0] == 0)
1374 Cvar_Set ("hostname", "UNNAMED");
1375 scr_centertime_off = 0;
1377 Con_DPrintf ("SpawnServer: %s\n",server);
1378 svs.changelevel_issued = false; // now safe to issue another
1381 // tell all connected clients that we are going to a new level
1384 SV_SendReconnect ();
1387 // make cvars consistant
1390 Cvar_SetValue ("deathmatch", 0);
1391 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1393 Cvar_SetValue ("skill", (float)current_skill);
1396 // set up the new server
1398 Host_ClearMemory ();
1400 memset (&sv, 0, sizeof(sv));
1402 strcpy (sv.name, server);
1404 // load progs to get entity field count
1407 // allocate server memory
1408 sv.max_edicts = MAX_EDICTS;
1410 // clear the edict memory pool
1411 Mem_EmptyPool(sv_edicts_mempool);
1412 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1414 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1415 sv.datagram.cursize = 0;
1416 sv.datagram.data = sv.datagram_buf;
1418 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1419 sv.reliable_datagram.cursize = 0;
1420 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1422 sv.signon.maxsize = sizeof(sv.signon_buf);
1423 sv.signon.cursize = 0;
1424 sv.signon.data = sv.signon_buf;
1426 // leave slots at start for clients only
1427 sv.num_edicts = svs.maxclients+1;
1428 for (i=0 ; i<svs.maxclients ; i++)
1430 ent = EDICT_NUM(i+1);
1431 svs.clients[i].edict = ent;
1434 sv.state = ss_loading;
1441 strcpy (sv.name, server);
1442 sprintf (sv.modelname,"maps/%s.bsp", server);
1443 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1446 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1450 sv.models[1] = sv.worldmodel;
1453 // clear world interaction links
1457 sv.sound_precache[0] = pr_strings;
1459 sv.model_precache[0] = pr_strings;
1460 sv.model_precache[1] = sv.modelname;
1461 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1463 sv.model_precache[i+1] = localmodels[i];
1464 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1468 // load the rest of the entities
1471 memset (&ent->v, 0, progs->entityfields * 4);
1473 ent->v.model = sv.worldmodel->name - pr_strings;
1474 ent->v.modelindex = 1; // world model
1475 ent->v.solid = SOLID_BSP;
1476 ent->v.movetype = MOVETYPE_PUSH;
1479 pr_global_struct->coop = coop.integer;
1481 pr_global_struct->deathmatch = deathmatch.integer;
1483 pr_global_struct->mapname = sv.name - pr_strings;
1485 // serverflags are for cross level information (sigils)
1486 pr_global_struct->serverflags = svs.serverflags;
1488 ED_LoadFromFile (sv.worldmodel->entities);
1489 // LordHavoc: clear world angles (to fix e3m3.bsp)
1490 VectorClear(sv.edicts->v.angles);
1494 // all setup is completed, any further precache statements are errors
1495 sv.state = ss_active;
1497 // run two frames to allow everything to settle
1498 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1504 // create a baseline for more efficient communications
1505 SV_CreateBaseline ();
1507 // send serverinfo to all connected clients
1508 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1509 if (host_client->active)
1510 SV_SendServerinfo (host_client);
1512 Con_DPrintf ("Server spawned.\n");