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_pvscheckentities = {0, "sv_pvscheckentities", "1"};
25 static cvar_t sv_vischeckentities = {0, "sv_vischeckentities", "0"}; // extremely accurate visibility checking, but too slow
26 static cvar_t sv_reportvischeckentities = {0, "sv_reportvischeckentities", "0"};
27 static int sv_vischeckentitycullcount = 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_pvscheckentities);
60 Cvar_RegisterVariable (&sv_vischeckentities);
61 Cvar_RegisterVariable (&sv_reportvischeckentities);
63 for (i = 0;i < MAX_MODELS;i++)
64 sprintf (localmodels[i], "*%i", i);
66 sv_edicts_mempool = Mem_AllocPool("edicts");
70 =============================================================================
74 =============================================================================
81 Make sure the event gets sent to all clients
84 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
88 if (sv.datagram.cursize > MAX_DATAGRAM-16)
90 MSG_WriteByte (&sv.datagram, svc_particle);
91 MSG_WriteFloatCoord (&sv.datagram, org[0]);
92 MSG_WriteFloatCoord (&sv.datagram, org[1]);
93 MSG_WriteFloatCoord (&sv.datagram, org[2]);
101 MSG_WriteChar (&sv.datagram, v);
103 MSG_WriteByte (&sv.datagram, count);
104 MSG_WriteByte (&sv.datagram, color);
111 Make sure the event gets sent to all clients
114 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
116 if (sv.datagram.cursize > MAX_DATAGRAM-18)
118 if (modelindex >= 256 || startframe >= 256)
120 MSG_WriteByte (&sv.datagram, svc_effect2);
121 MSG_WriteFloatCoord (&sv.datagram, org[0]);
122 MSG_WriteFloatCoord (&sv.datagram, org[1]);
123 MSG_WriteFloatCoord (&sv.datagram, org[2]);
124 MSG_WriteShort (&sv.datagram, modelindex);
125 MSG_WriteShort (&sv.datagram, startframe);
126 MSG_WriteByte (&sv.datagram, framecount);
127 MSG_WriteByte (&sv.datagram, framerate);
131 MSG_WriteByte (&sv.datagram, svc_effect);
132 MSG_WriteFloatCoord (&sv.datagram, org[0]);
133 MSG_WriteFloatCoord (&sv.datagram, org[1]);
134 MSG_WriteFloatCoord (&sv.datagram, org[2]);
135 MSG_WriteByte (&sv.datagram, modelindex);
136 MSG_WriteByte (&sv.datagram, startframe);
137 MSG_WriteByte (&sv.datagram, framecount);
138 MSG_WriteByte (&sv.datagram, framerate);
146 Each entity can have eight independant sound sources, like voice,
149 Channel 0 is an auto-allocate channel, the others override anything
150 already running on that entity/channel pair.
152 An attenuation of 0 will play full volume everywhere in the level.
153 Larger attenuations will drop off. (max 4 attenuation)
157 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
165 if (volume < 0 || volume > 255)
166 Host_Error ("SV_StartSound: volume = %i", volume);
168 if (attenuation < 0 || attenuation > 4)
169 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
171 if (channel < 0 || channel > 7)
172 Host_Error ("SV_StartSound: channel = %i", channel);
174 if (sv.datagram.cursize > MAX_DATAGRAM-16)
177 // find precache number for sound
178 for (sound_num=1 ; sound_num<MAX_SOUNDS
179 && sv.sound_precache[sound_num] ; sound_num++)
180 if (!strcmp(sample, sv.sound_precache[sound_num]))
183 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
185 Con_Printf ("SV_StartSound: %s not precached\n", sample);
189 ent = NUM_FOR_EDICT(entity);
191 channel = (ent<<3) | channel;
194 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
195 field_mask |= SND_VOLUME;
196 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
197 field_mask |= SND_ATTENUATION;
199 // directed messages go only to the entity they are targeted on
200 if (sound_num >= 256)
201 MSG_WriteByte (&sv.datagram, svc_sound2);
203 MSG_WriteByte (&sv.datagram, svc_sound);
204 MSG_WriteByte (&sv.datagram, field_mask);
205 if (field_mask & SND_VOLUME)
206 MSG_WriteByte (&sv.datagram, volume);
207 if (field_mask & SND_ATTENUATION)
208 MSG_WriteByte (&sv.datagram, attenuation*64);
209 MSG_WriteShort (&sv.datagram, channel);
210 if (sound_num >= 256)
211 MSG_WriteShort (&sv.datagram, sound_num);
213 MSG_WriteByte (&sv.datagram, sound_num);
214 for (i=0 ; i<3 ; i++)
215 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
219 ==============================================================================
223 ==============================================================================
230 Sends the first message from the server to a connected client.
231 This will be sent on the initial connection and upon each server load.
234 void SV_SendServerinfo (client_t *client)
239 MSG_WriteByte (&client->message, svc_print);
240 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
241 MSG_WriteString (&client->message,message);
243 MSG_WriteByte (&client->message, svc_serverinfo);
244 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
245 MSG_WriteByte (&client->message, svs.maxclients);
247 if (!coop.integer && deathmatch.integer)
248 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
250 MSG_WriteByte (&client->message, GAME_COOP);
252 sprintf (message, pr_strings+sv.edicts->v.message);
254 MSG_WriteString (&client->message,message);
256 for (s = sv.model_precache+1 ; *s ; s++)
257 MSG_WriteString (&client->message, *s);
258 MSG_WriteByte (&client->message, 0);
260 for (s = sv.sound_precache+1 ; *s ; s++)
261 MSG_WriteString (&client->message, *s);
262 MSG_WriteByte (&client->message, 0);
265 MSG_WriteByte (&client->message, svc_cdtrack);
266 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
267 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
270 MSG_WriteByte (&client->message, svc_setview);
271 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
273 MSG_WriteByte (&client->message, svc_signonnum);
274 MSG_WriteByte (&client->message, 1);
276 client->sendsignon = true;
277 client->spawned = false; // need prespawn, spawn, etc
284 Initializes a client_t for a new net connection. This will only be called
285 once for a player each game, not once for each level change.
288 void SV_ConnectClient (int clientnum)
293 struct qsocket_s *netconnection;
295 float spawn_parms[NUM_SPAWN_PARMS];
297 client = svs.clients + clientnum;
299 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
301 edictnum = clientnum+1;
303 ent = EDICT_NUM(edictnum);
305 // set up the client_t
306 netconnection = client->netconnection;
309 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
310 memset (client, 0, sizeof(*client));
311 client->netconnection = netconnection;
313 strcpy (client->name, "unconnected");
314 client->active = true;
315 client->spawned = false;
317 client->message.data = client->msgbuf;
318 client->message.maxsize = sizeof(client->msgbuf);
319 client->message.allowoverflow = true; // we can catch it
322 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
325 // call the progs to get default spawn parms for the new client
326 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
327 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
328 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
331 SV_SendServerinfo (client);
337 SV_CheckForNewClients
341 void SV_CheckForNewClients (void)
343 struct qsocket_s *ret;
347 // check for new connections
351 ret = NET_CheckNewConnections ();
356 // init a new client structure
358 for (i=0 ; i<svs.maxclients ; i++)
359 if (!svs.clients[i].active)
361 if (i == svs.maxclients)
362 Sys_Error ("Host_CheckForNewClients: no free clients");
364 svs.clients[i].netconnection = ret;
365 SV_ConnectClient (i);
367 net_activeconnections++;
374 ===============================================================================
378 ===============================================================================
387 void SV_ClearDatagram (void)
389 SZ_Clear (&sv.datagram);
393 =============================================================================
395 The PVS must include a small area around the client to allow head bobbing
396 or other small motion on the client side. Otherwise, a bob might cause an
397 entity that should be visible to not show up, especially when the bob
400 =============================================================================
404 byte fatpvs[MAX_MAP_LEAFS/8];
406 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
415 // if this is a leaf, accumulate the pvs bits
416 if (node->contents < 0)
418 if (node->contents != CONTENTS_SOLID)
420 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
421 for (i=0 ; i<fatbytes ; i++)
428 d = DotProduct (org, plane->normal) - plane->dist;
430 node = node->children[0];
432 node = node->children[1];
435 SV_AddToFatPVS (org, node->children[0]);
436 node = node->children[1];
445 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
449 byte *SV_FatPVS (vec3_t org)
451 fatbytes = (sv.worldmodel->numleafs+31)>>3;
452 memset (fatpvs, 0, fatbytes);
453 SV_AddToFatPVS (org, sv.worldmodel->nodes);
457 //=============================================================================
460 int SV_BoxTouchingPVS (byte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
464 if (node->contents < 0)
467 if (node->contents == CONTENTS_SOLID)
469 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
470 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
473 // node - recurse down the BSP tree
474 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
477 node = node->children[0];
480 node = node->children[1];
483 if (node->children[0]->contents != CONTENTS_SOLID)
484 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
486 node = node->children[1];
496 SV_WriteEntitiesToClient
500 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
502 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
504 vec3_t org, origin, angles, entmins, entmaxs;
505 float nextfullupdate;
508 entity_state_t *baseline; // LordHavoc: delta or startup baseline
512 Mod_CheckLoaded(sv.worldmodel);
514 // find the client's PVS
515 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
516 pvs = SV_FatPVS (org);
519 MSG_WriteByte(msg, svc_playerposition);
520 MSG_WriteFloat(msg, org[0]);
521 MSG_WriteFloat(msg, org[1]);
522 MSG_WriteFloat(msg, org[2]);
525 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
526 // send all entities that touch the pvs
527 ent = NEXT_EDICT(sv.edicts);
528 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
532 // prevent delta compression against this frame (unless actually sent, which will restore this later)
533 nextfullupdate = client->nextfullupdate[e];
534 client->nextfullupdate[e] = -1;
536 if (ent != clent) // LordHavoc: always send player
538 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
540 if (val->edict != clentnum)
542 // don't show to anyone else
546 bits |= U_VIEWMODEL; // show relative to the view
550 // LordHavoc: never draw something told not to display to this client
551 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
553 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
560 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
561 glowsize = (int) val->_float >> 2;
562 if (glowsize > 255) glowsize = 255;
563 if (glowsize < 0) glowsize = 0;
565 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
566 if (val->_float != 0)
569 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
571 model = sv.models[(int)ent->v.modelindex];
572 Mod_CheckLoaded(model);
577 if (ent != clent) // LordHavoc: always send player
578 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
582 VectorCopy(ent->v.angles, angles);
583 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
585 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
586 // LordHavoc: trace predicted movement to avoid putting things in walls
587 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
588 VectorCopy(trace.endpos, origin);
592 VectorCopy(ent->v.origin, origin);
595 // ent has survived every check so far, check if it is visible
596 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
598 // use the predicted origin
599 entmins[0] = origin[0] - 1.0f;
600 entmins[1] = origin[1] - 1.0f;
601 entmins[2] = origin[2] - 1.0f;
602 entmaxs[0] = origin[0] + 1.0f;
603 entmaxs[1] = origin[1] + 1.0f;
604 entmaxs[2] = origin[2] + 1.0f;
605 // using the model's bounding box to ensure things are visible regardless of their physics box
608 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
610 VectorAdd(entmins, model->rotatedmins, entmins);
611 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
613 else if (ent->v.angles[1])
615 VectorAdd(entmins, model->yawmins, entmins);
616 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
620 VectorAdd(entmins, model->normalmins, entmins);
621 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
625 // if not touching a visible leaf
626 if (sv_pvscheckentities.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
629 // or not visible through the portals
630 if (sv_vischeckentities.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
632 sv_vischeckentitycullcount++;
640 effects = ent->v.effects;
642 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
643 if (val->_float != 0)
644 alpha = (int) (val->_float * 255.0);
647 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
648 if (val->_float != 0)
649 alpha = (int) val->_float;
653 alpha = bound(0, alpha, 255);
655 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
656 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
657 if (scale < 0) scale = 0;
658 if (scale > 255) scale = 255;
660 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
661 if (val->_float != 0)
662 glowcolor = (int) val->_float;
664 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
665 if (val->_float != 0)
666 effects |= EF_FULLBRIGHT;
670 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
674 // don't send if flagged for NODRAW and there are no effects
675 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
678 else // no model and no effects
683 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
685 Con_Printf ("packet overflow\n");
686 // mark the rest of the entities so they can't be delta compressed against this frame
687 for (;e < sv.num_edicts;e++)
688 client->nextfullupdate[e] = -1;
692 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
693 bits = bits | U_EXTERIORMODEL;
696 baseline = &ent->baseline;
698 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.integer)
700 // every half second a full update is forced
701 if (realtime < client->nextfullupdate[e])
704 baseline = &ent->deltabaseline;
707 nextfullupdate = realtime + 0.5f;
710 nextfullupdate = realtime + 0.5f;
712 // restore nextfullupdate since this is being sent for real
713 client->nextfullupdate[e] = nextfullupdate;
716 bits |= U_LONGENTITY;
718 if (ent->v.movetype == MOVETYPE_STEP)
721 // LordHavoc: old stuff, but rewritten to have more exact tolerances
722 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
723 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
724 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
725 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
726 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
727 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
728 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
729 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
730 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
731 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
732 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
733 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
734 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
735 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
737 // LordHavoc: new stuff
738 if (baseline->alpha != alpha) bits |= U_ALPHA;
739 if (baseline->scale != scale) bits |= U_SCALE;
740 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
741 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
742 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
743 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
744 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
746 // update delta baseline
747 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
748 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
749 ent->deltabaseline.colormap = ent->v.colormap;
750 ent->deltabaseline.skin = ent->v.skin;
751 ent->deltabaseline.frame = ent->v.frame;
752 ent->deltabaseline.effects = ent->v.effects;
753 ent->deltabaseline.modelindex = ent->v.modelindex;
754 ent->deltabaseline.alpha = alpha;
755 ent->deltabaseline.scale = scale;
756 ent->deltabaseline.glowsize = glowsize;
757 ent->deltabaseline.glowcolor = glowcolor;
760 if (bits >= 16777216)
768 MSG_WriteByte (msg, bits);
769 if (bits & U_MOREBITS)
770 MSG_WriteByte (msg, bits>>8);
771 // LordHavoc: extend bytes have to be written here due to delta compression
772 if (bits & U_EXTEND1)
773 MSG_WriteByte (msg, bits>>16);
774 if (bits & U_EXTEND2)
775 MSG_WriteByte (msg, bits>>24);
777 // LordHavoc: old stuff
778 if (bits & U_LONGENTITY)
779 MSG_WriteShort (msg,e);
781 MSG_WriteByte (msg,e);
782 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
783 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
784 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
785 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
786 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
787 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
788 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
789 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
790 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
791 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
792 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
794 // LordHavoc: new stuff
795 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
796 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
797 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
798 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
799 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
800 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
801 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
804 if (sv_reportvischeckentities.integer)
805 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
806 sv_vischeckentitycullcount = 0;
815 void SV_CleanupEnts (void)
820 ent = NEXT_EDICT(sv.edicts);
821 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
822 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
827 SV_WriteClientdataToMessage
831 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
841 // send a damage message
843 if (ent->v.dmg_take || ent->v.dmg_save)
845 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
846 MSG_WriteByte (msg, svc_damage);
847 MSG_WriteByte (msg, ent->v.dmg_save);
848 MSG_WriteByte (msg, ent->v.dmg_take);
849 for (i=0 ; i<3 ; i++)
850 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
857 // send the current viewpos offset from the view entity
859 SV_SetIdealPitch (); // how much to look up / down ideally
861 // a fixangle might get lost in a dropped packet. Oh well.
862 if ( ent->v.fixangle )
864 MSG_WriteByte (msg, svc_setangle);
865 for (i=0 ; i < 3 ; i++)
866 MSG_WriteAngle (msg, ent->v.angles[i] );
872 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
873 bits |= SU_VIEWHEIGHT;
875 if (ent->v.idealpitch)
876 bits |= SU_IDEALPITCH;
878 // stuff the sigil bits into the high bits of items for sbar, or else
880 val = GETEDICTFIELDVALUE(ent, eval_items2);
883 items = (int)ent->v.items | ((int)val->_float << 23);
885 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
889 if ( (int)ent->v.flags & FL_ONGROUND)
892 if ( ent->v.waterlevel >= 2)
896 VectorClear(punchvector);
897 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
898 VectorCopy(val->vector, punchvector);
900 for (i=0 ; i<3 ; i++)
902 if (ent->v.punchangle[i])
903 bits |= (SU_PUNCH1<<i);
904 if (punchvector[i]) // dpprotocol
905 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
906 if (ent->v.velocity[i])
907 bits |= (SU_VELOCITY1<<i);
910 if (ent->v.weaponframe)
911 bits |= SU_WEAPONFRAME;
913 if (ent->v.armorvalue)
916 // if (ent->v.weapon)
921 if (bits >= 16777216)
926 MSG_WriteByte (msg, svc_clientdata);
927 MSG_WriteShort (msg, bits);
928 if (bits & SU_EXTEND1)
929 MSG_WriteByte(msg, bits >> 16);
930 if (bits & SU_EXTEND2)
931 MSG_WriteByte(msg, bits >> 24);
933 if (bits & SU_VIEWHEIGHT)
934 MSG_WriteChar (msg, ent->v.view_ofs[2]);
936 if (bits & SU_IDEALPITCH)
937 MSG_WriteChar (msg, ent->v.idealpitch);
939 for (i=0 ; i<3 ; i++)
941 if (bits & (SU_PUNCH1<<i))
942 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
943 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
944 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
945 if (bits & (SU_VELOCITY1<<i))
946 MSG_WriteChar (msg, ent->v.velocity[i]/16);
949 // [always sent] if (bits & SU_ITEMS)
950 MSG_WriteLong (msg, items);
952 if (bits & SU_WEAPONFRAME)
953 MSG_WriteByte (msg, ent->v.weaponframe);
955 MSG_WriteByte (msg, ent->v.armorvalue);
956 if (bits & SU_WEAPON)
957 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
959 MSG_WriteShort (msg, ent->v.health);
960 MSG_WriteByte (msg, ent->v.currentammo);
961 MSG_WriteByte (msg, ent->v.ammo_shells);
962 MSG_WriteByte (msg, ent->v.ammo_nails);
963 MSG_WriteByte (msg, ent->v.ammo_rockets);
964 MSG_WriteByte (msg, ent->v.ammo_cells);
966 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
970 if ( ((int)ent->v.weapon) & (1<<i) )
972 MSG_WriteByte (msg, i);
979 MSG_WriteByte (msg, ent->v.weapon);
984 =======================
985 SV_SendClientDatagram
986 =======================
988 qboolean SV_SendClientDatagram (client_t *client)
990 byte buf[MAX_DATAGRAM];
994 msg.maxsize = sizeof(buf);
997 MSG_WriteByte (&msg, svc_time);
998 MSG_WriteFloat (&msg, sv.time);
1000 // add the client specific data to the datagram
1001 SV_WriteClientdataToMessage (client->edict, &msg);
1003 SV_WriteEntitiesToClient (client, client->edict, &msg);
1005 // copy the server datagram if there is space
1006 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1007 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1009 // send the datagram
1010 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1012 SV_DropClient (true);// if the message couldn't send, kick off
1020 =======================
1021 SV_UpdateToReliableMessages
1022 =======================
1024 void SV_UpdateToReliableMessages (void)
1029 // check for changes to be sent over the reliable streams
1030 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1032 if (host_client->old_frags != host_client->edict->v.frags)
1034 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1036 if (!client->active)
1038 MSG_WriteByte (&client->message, svc_updatefrags);
1039 MSG_WriteByte (&client->message, i);
1040 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1043 host_client->old_frags = host_client->edict->v.frags;
1047 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1049 if (!client->active)
1051 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1054 SZ_Clear (&sv.reliable_datagram);
1059 =======================
1062 Send a nop message without trashing or sending the accumulated client
1064 =======================
1066 void SV_SendNop (client_t *client)
1072 msg.maxsize = sizeof(buf);
1075 MSG_WriteChar (&msg, svc_nop);
1077 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1078 SV_DropClient (true); // if the message couldn't send, kick off
1079 client->last_message = realtime;
1083 =======================
1084 SV_SendClientMessages
1085 =======================
1087 void SV_SendClientMessages (void)
1091 // update frags, names, etc
1092 SV_UpdateToReliableMessages ();
1094 // build individual updates
1095 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1097 if (!host_client->active)
1100 if (host_client->spawned)
1102 if (!SV_SendClientDatagram (host_client))
1107 // the player isn't totally in the game yet
1108 // send small keepalive messages if too much time has passed
1109 // send a full message when the next signon stage has been requested
1110 // some other message data (name changes, etc) may accumulate
1111 // between signon stages
1112 if (!host_client->sendsignon)
1114 if (realtime - host_client->last_message > 5)
1115 SV_SendNop (host_client);
1116 continue; // don't send out non-signon messages
1120 // check for an overflowed message. Should only happen
1121 // on a very fucked up connection that backs up a lot, then
1123 if (host_client->message.overflowed)
1125 SV_DropClient (true);
1126 host_client->message.overflowed = false;
1130 if (host_client->message.cursize || host_client->dropasap)
1132 if (!NET_CanSendMessage (host_client->netconnection))
1134 // I_Printf ("can't write\n");
1138 if (host_client->dropasap)
1139 SV_DropClient (false); // went to another level
1142 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1143 SV_DropClient (true); // if the message couldn't send, kick off
1144 SZ_Clear (&host_client->message);
1145 host_client->last_message = realtime;
1146 host_client->sendsignon = false;
1152 // clear muzzle flashes
1158 ==============================================================================
1162 ==============================================================================
1171 int SV_ModelIndex (char *name)
1175 if (!name || !name[0])
1178 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1179 if (!strcmp(sv.model_precache[i], name))
1181 if (i==MAX_MODELS || !sv.model_precache[i])
1182 Host_Error ("SV_ModelIndex: model %s not precached", name);
1192 void SV_CreateBaseline (void)
1194 int i, entnum, large;
1197 // LordHavoc: clear *all* states (note just active ones)
1198 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1200 // get the current server version
1201 svent = EDICT_NUM(entnum);
1203 // LordHavoc: always clear state values, whether the entity is in use or not
1204 ClearStateToDefault(&svent->baseline);
1208 if (entnum > svs.maxclients && !svent->v.modelindex)
1211 // create entity baseline
1212 VectorCopy (svent->v.origin, svent->baseline.origin);
1213 VectorCopy (svent->v.angles, svent->baseline.angles);
1214 svent->baseline.frame = svent->v.frame;
1215 svent->baseline.skin = svent->v.skin;
1216 if (entnum > 0 && entnum <= svs.maxclients)
1218 svent->baseline.colormap = entnum;
1219 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1223 svent->baseline.colormap = 0;
1224 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1228 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1231 // add to the message
1233 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1235 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1236 MSG_WriteShort (&sv.signon, entnum);
1240 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1241 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1245 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1246 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1248 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1249 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1250 for (i=0 ; i<3 ; i++)
1252 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1253 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1263 Tell all the clients that the server is changing levels
1266 void SV_SendReconnect (void)
1273 msg.maxsize = sizeof(data);
1275 MSG_WriteChar (&msg, svc_stufftext);
1276 MSG_WriteString (&msg, "reconnect\n");
1277 NET_SendToAll (&msg, 5);
1279 if (cls.state != ca_dedicated)
1280 Cmd_ExecuteString ("reconnect\n", src_command);
1288 Grabs the current state of each client for saving across the
1289 transition to another level
1292 void SV_SaveSpawnparms (void)
1296 svs.serverflags = pr_global_struct->serverflags;
1298 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1300 if (!host_client->active)
1303 // call the progs to get default spawn parms for the new client
1304 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1305 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1306 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1307 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1315 This is called at the start of each level
1318 extern float scr_centertime_off;
1320 void SV_SpawnServer (char *server)
1325 // let's not have any servers with no name
1326 if (hostname.string[0] == 0)
1327 Cvar_Set ("hostname", "UNNAMED");
1328 scr_centertime_off = 0;
1330 Con_DPrintf ("SpawnServer: %s\n",server);
1331 svs.changelevel_issued = false; // now safe to issue another
1334 // tell all connected clients that we are going to a new level
1337 SV_SendReconnect ();
1340 // make cvars consistant
1343 Cvar_SetValue ("deathmatch", 0);
1344 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1346 Cvar_SetValue ("skill", (float)current_skill);
1349 // set up the new server
1351 Host_ClearMemory ();
1353 memset (&sv, 0, sizeof(sv));
1355 strcpy (sv.name, server);
1357 // load progs to get entity field count
1360 // allocate server memory
1361 sv.max_edicts = MAX_EDICTS;
1363 // clear the edict memory pool
1364 Mem_EmptyPool(sv_edicts_mempool);
1365 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1367 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1368 sv.datagram.cursize = 0;
1369 sv.datagram.data = sv.datagram_buf;
1371 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1372 sv.reliable_datagram.cursize = 0;
1373 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1375 sv.signon.maxsize = sizeof(sv.signon_buf);
1376 sv.signon.cursize = 0;
1377 sv.signon.data = sv.signon_buf;
1379 // leave slots at start for clients only
1380 sv.num_edicts = svs.maxclients+1;
1381 for (i=0 ; i<svs.maxclients ; i++)
1383 ent = EDICT_NUM(i+1);
1384 svs.clients[i].edict = ent;
1387 sv.state = ss_loading;
1394 strcpy (sv.name, server);
1395 sprintf (sv.modelname,"maps/%s.bsp", server);
1396 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1399 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1403 sv.models[1] = sv.worldmodel;
1406 // clear world interaction links
1410 sv.sound_precache[0] = pr_strings;
1412 sv.model_precache[0] = pr_strings;
1413 sv.model_precache[1] = sv.modelname;
1414 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1416 sv.model_precache[i+1] = localmodels[i];
1417 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1421 // load the rest of the entities
1424 memset (&ent->v, 0, progs->entityfields * 4);
1426 ent->v.model = sv.worldmodel->name - pr_strings;
1427 ent->v.modelindex = 1; // world model
1428 ent->v.solid = SOLID_BSP;
1429 ent->v.movetype = MOVETYPE_PUSH;
1432 pr_global_struct->coop = coop.integer;
1434 pr_global_struct->deathmatch = deathmatch.integer;
1436 pr_global_struct->mapname = sv.name - pr_strings;
1438 // serverflags are for cross level information (sigils)
1439 pr_global_struct->serverflags = svs.serverflags;
1441 ED_LoadFromFile (sv.worldmodel->entities);
1442 // LordHavoc: clear world angles (to fix e3m3.bsp)
1443 VectorClear(sv.edicts->v.angles);
1447 // all setup is completed, any further precache statements are errors
1448 sv.state = ss_active;
1450 // run two frames to allow everything to settle
1451 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1457 // create a baseline for more efficient communications
1458 SV_CreateBaseline ();
1460 // send serverinfo to all connected clients
1461 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1462 if (host_client->active)
1463 SV_SendServerinfo (host_client);
1465 Con_DPrintf ("Server spawned.\n");