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 cvar_t sv_pvscheckentities = {0, "sv_pvscheckentities", "1"};
25 cvar_t sv_vischeckentities = {0, "sv_vischeckentities", "0"}; // extremely accurate visibility checking, but too slow
26 cvar_t sv_reportvischeckentities = {0, "sv_reportvischeckentities", "0"};
27 int sv_vischeckentitycullcount = 0;
32 char localmodels[MAX_MODELS][5]; // inline model names for precache
34 //============================================================================
45 Cvar_RegisterVariable (&sv_maxvelocity);
46 Cvar_RegisterVariable (&sv_gravity);
47 Cvar_RegisterVariable (&sv_friction);
48 Cvar_RegisterVariable (&sv_edgefriction);
49 Cvar_RegisterVariable (&sv_stopspeed);
50 Cvar_RegisterVariable (&sv_maxspeed);
51 Cvar_RegisterVariable (&sv_accelerate);
52 Cvar_RegisterVariable (&sv_idealpitchscale);
53 Cvar_RegisterVariable (&sv_aim);
54 Cvar_RegisterVariable (&sv_nostep);
55 Cvar_RegisterVariable (&sv_predict);
56 Cvar_RegisterVariable (&sv_deltacompress);
57 Cvar_RegisterVariable (&sv_pvscheckentities);
58 Cvar_RegisterVariable (&sv_vischeckentities);
59 Cvar_RegisterVariable (&sv_reportvischeckentities);
61 for (i = 0;i < MAX_MODELS;i++)
62 sprintf (localmodels[i], "*%i", i);
66 =============================================================================
70 =============================================================================
77 Make sure the event gets sent to all clients
80 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
84 if (sv.datagram.cursize > MAX_DATAGRAM-16)
86 MSG_WriteByte (&sv.datagram, svc_particle);
87 MSG_WriteFloatCoord (&sv.datagram, org[0]);
88 MSG_WriteFloatCoord (&sv.datagram, org[1]);
89 MSG_WriteFloatCoord (&sv.datagram, org[2]);
97 MSG_WriteChar (&sv.datagram, v);
99 MSG_WriteByte (&sv.datagram, count);
100 MSG_WriteByte (&sv.datagram, color);
107 Make sure the event gets sent to all clients
110 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
112 if (sv.datagram.cursize > MAX_DATAGRAM-18)
114 if (modelindex >= 256 || startframe >= 256)
116 MSG_WriteByte (&sv.datagram, svc_effect2);
117 MSG_WriteFloatCoord (&sv.datagram, org[0]);
118 MSG_WriteFloatCoord (&sv.datagram, org[1]);
119 MSG_WriteFloatCoord (&sv.datagram, org[2]);
120 MSG_WriteShort (&sv.datagram, modelindex);
121 MSG_WriteShort (&sv.datagram, startframe);
122 MSG_WriteByte (&sv.datagram, framecount);
123 MSG_WriteByte (&sv.datagram, framerate);
127 MSG_WriteByte (&sv.datagram, svc_effect);
128 MSG_WriteFloatCoord (&sv.datagram, org[0]);
129 MSG_WriteFloatCoord (&sv.datagram, org[1]);
130 MSG_WriteFloatCoord (&sv.datagram, org[2]);
131 MSG_WriteByte (&sv.datagram, modelindex);
132 MSG_WriteByte (&sv.datagram, startframe);
133 MSG_WriteByte (&sv.datagram, framecount);
134 MSG_WriteByte (&sv.datagram, framerate);
142 Each entity can have eight independant sound sources, like voice,
145 Channel 0 is an auto-allocate channel, the others override anything
146 already running on that entity/channel pair.
148 An attenuation of 0 will play full volume everywhere in the level.
149 Larger attenuations will drop off. (max 4 attenuation)
153 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
161 if (volume < 0 || volume > 255)
162 Host_Error ("SV_StartSound: volume = %i", volume);
164 if (attenuation < 0 || attenuation > 4)
165 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
167 if (channel < 0 || channel > 7)
168 Host_Error ("SV_StartSound: channel = %i", channel);
170 if (sv.datagram.cursize > MAX_DATAGRAM-16)
173 // find precache number for sound
174 for (sound_num=1 ; sound_num<MAX_SOUNDS
175 && sv.sound_precache[sound_num] ; sound_num++)
176 if (!strcmp(sample, sv.sound_precache[sound_num]))
179 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
181 Con_Printf ("SV_StartSound: %s not precached\n", sample);
185 ent = NUM_FOR_EDICT(entity);
187 channel = (ent<<3) | channel;
190 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
191 field_mask |= SND_VOLUME;
192 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
193 field_mask |= SND_ATTENUATION;
195 // directed messages go only to the entity they are targeted on
196 if (sound_num >= 256)
197 MSG_WriteByte (&sv.datagram, svc_sound2);
199 MSG_WriteByte (&sv.datagram, svc_sound);
200 MSG_WriteByte (&sv.datagram, field_mask);
201 if (field_mask & SND_VOLUME)
202 MSG_WriteByte (&sv.datagram, volume);
203 if (field_mask & SND_ATTENUATION)
204 MSG_WriteByte (&sv.datagram, attenuation*64);
205 MSG_WriteShort (&sv.datagram, channel);
206 if (sound_num >= 256)
207 MSG_WriteShort (&sv.datagram, sound_num);
209 MSG_WriteByte (&sv.datagram, sound_num);
210 for (i=0 ; i<3 ; i++)
211 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
215 ==============================================================================
219 ==============================================================================
226 Sends the first message from the server to a connected client.
227 This will be sent on the initial connection and upon each server load.
230 void SV_SendServerinfo (client_t *client)
235 MSG_WriteByte (&client->message, svc_print);
236 sprintf (message, "\002\nServer: %s build %i (progs %i crc)", gamename, buildnumber, pr_crc);
237 MSG_WriteString (&client->message,message);
239 MSG_WriteByte (&client->message, svc_serverinfo);
240 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
241 MSG_WriteByte (&client->message, svs.maxclients);
243 if (!coop.value && deathmatch.value)
244 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
246 MSG_WriteByte (&client->message, GAME_COOP);
248 sprintf (message, pr_strings+sv.edicts->v.message);
250 MSG_WriteString (&client->message,message);
252 for (s = sv.model_precache+1 ; *s ; s++)
253 MSG_WriteString (&client->message, *s);
254 MSG_WriteByte (&client->message, 0);
256 for (s = sv.sound_precache+1 ; *s ; s++)
257 MSG_WriteString (&client->message, *s);
258 MSG_WriteByte (&client->message, 0);
261 MSG_WriteByte (&client->message, svc_cdtrack);
262 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
263 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
266 MSG_WriteByte (&client->message, svc_setview);
267 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
269 MSG_WriteByte (&client->message, svc_signonnum);
270 MSG_WriteByte (&client->message, 1);
272 client->sendsignon = true;
273 client->spawned = false; // need prespawn, spawn, etc
280 Initializes a client_t for a new net connection. This will only be called
281 once for a player each game, not once for each level change.
284 void SV_ConnectClient (int clientnum)
289 struct qsocket_s *netconnection;
291 float spawn_parms[NUM_SPAWN_PARMS];
293 client = svs.clients + clientnum;
295 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
297 edictnum = clientnum+1;
299 ent = EDICT_NUM(edictnum);
301 // set up the client_t
302 netconnection = client->netconnection;
305 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
306 memset (client, 0, sizeof(*client));
307 client->netconnection = netconnection;
309 strcpy (client->name, "unconnected");
310 client->active = true;
311 client->spawned = false;
313 client->message.data = client->msgbuf;
314 client->message.maxsize = sizeof(client->msgbuf);
315 client->message.allowoverflow = true; // we can catch it
318 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
321 // call the progs to get default spawn parms for the new client
322 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
323 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
324 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
327 SV_SendServerinfo (client);
333 SV_CheckForNewClients
337 void SV_CheckForNewClients (void)
339 struct qsocket_s *ret;
343 // check for new connections
347 ret = NET_CheckNewConnections ();
352 // init a new client structure
354 for (i=0 ; i<svs.maxclients ; i++)
355 if (!svs.clients[i].active)
357 if (i == svs.maxclients)
358 Sys_Error ("Host_CheckForNewClients: no free clients");
360 svs.clients[i].netconnection = ret;
361 SV_ConnectClient (i);
363 net_activeconnections++;
370 ===============================================================================
374 ===============================================================================
383 void SV_ClearDatagram (void)
385 SZ_Clear (&sv.datagram);
389 =============================================================================
391 The PVS must include a small area around the client to allow head bobbing
392 or other small motion on the client side. Otherwise, a bob might cause an
393 entity that should be visible to not show up, especially when the bob
396 =============================================================================
400 byte fatpvs[MAX_MAP_LEAFS/8];
402 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
411 // if this is a leaf, accumulate the pvs bits
412 if (node->contents < 0)
414 if (node->contents != CONTENTS_SOLID)
416 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
417 for (i=0 ; i<fatbytes ; i++)
424 d = DotProduct (org, plane->normal) - plane->dist;
426 node = node->children[0];
428 node = node->children[1];
431 SV_AddToFatPVS (org, node->children[0]);
432 node = node->children[1];
441 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
445 byte *SV_FatPVS (vec3_t org)
447 fatbytes = (sv.worldmodel->numleafs+31)>>3;
448 memset (fatpvs, 0, fatbytes);
449 SV_AddToFatPVS (org, sv.worldmodel->nodes);
453 //=============================================================================
456 int SV_BoxTouchingPVS (byte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
460 if (node->contents < 0)
463 if (node->contents == CONTENTS_SOLID)
465 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
466 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
469 // node - recurse down the BSP tree
470 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
473 node = node->children[0];
476 node = node->children[1];
479 if (node->children[0]->contents != CONTENTS_SOLID)
480 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
482 node = node->children[1];
492 SV_WriteEntitiesToClient
496 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
498 int e, i, clentnum, bits, alpha, glowcolor, glowsize, scale, colormod, effects;
500 vec3_t org, origin, angles, entmins, entmaxs;
501 float movelerp, moveilerp, nextfullupdate;
504 entity_state_t *baseline; // LordHavoc: delta or startup baseline
508 // find the client's PVS
509 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
510 pvs = SV_FatPVS (org);
513 MSG_WriteByte(msg, svc_playerposition);
514 MSG_WriteFloat(msg, org[0]);
515 MSG_WriteFloat(msg, org[1]);
516 MSG_WriteFloat(msg, org[2]);
519 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
520 // send all entities that touch the pvs
521 ent = NEXT_EDICT(sv.edicts);
522 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
526 // prevent delta compression against this frame (unless actually sent, which will restore this later)
527 nextfullupdate = client->nextfullupdate[e];
528 client->nextfullupdate[e] = -1;
530 if (ent != clent) // LordHavoc: always send player
532 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
534 if (val->edict != clentnum)
536 // don't show to anyone else
540 bits |= U_VIEWMODEL; // show relative to the view
544 // LordHavoc: never draw something told not to display to this client
545 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
547 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
554 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
555 glowsize = (int) val->_float >> 2;
556 if (glowsize > 255) glowsize = 255;
557 if (glowsize < 0) glowsize = 0;
559 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
560 if (val->_float != 0)
563 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
564 model = sv.models[(int)ent->v.modelindex];
568 if (ent != clent) // LordHavoc: always send player
569 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
573 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
575 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
577 ent->steplerptime = sv.time;
578 VectorCopy(ent->v.origin, ent->stepoldorigin);
579 VectorCopy(ent->v.angles, ent->stepoldangles);
580 VectorCopy(ent->v.origin, ent->steporigin);
581 VectorCopy(ent->v.angles, ent->stepangles);
583 VectorSubtract(ent->v.origin, ent->steporigin, origin);
584 VectorSubtract(ent->v.angles, ent->stepangles, angles);
585 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
587 // update lerp positions
588 ent->steplerptime = sv.time;
589 VectorCopy(ent->steporigin, ent->stepoldorigin);
590 VectorCopy(ent->stepangles, ent->stepoldangles);
591 VectorCopy(ent->v.origin, ent->steporigin);
592 VectorCopy(ent->v.angles, ent->stepangles);
594 movelerp = (sv.time - ent->steplerptime) * 10.0;
595 if (movelerp > 1) movelerp = 1;
596 moveilerp = 1 - movelerp;
597 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
598 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
599 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
600 // choose shortest rotate (to avoid 'spin around' situations)
601 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
602 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
603 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
604 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
605 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
606 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
607 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
608 //VectorMA(origin, host_client->latency, ent->v.velocity, origin);
610 else // copy as they are
612 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
614 // update lerp positions
615 ent->steplerptime = sv.time;
616 VectorCopy(ent->v.origin, ent->stepoldorigin);
617 VectorCopy(ent->v.angles, ent->stepoldangles);
618 VectorCopy(ent->v.origin, ent->steporigin);
619 VectorCopy(ent->v.angles, ent->stepangles);
622 VectorCopy(ent->v.angles, angles);
623 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
625 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
626 // LordHavoc: trace predicted movement to avoid putting things in walls
627 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
628 VectorCopy(trace.endpos, origin);
632 VectorCopy(ent->v.origin, origin);
636 // ent has survived every check so far, check if it is visible
637 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
639 // use the predicted origin
640 entmins[0] = origin[0] - 1.0f;
641 entmins[1] = origin[1] - 1.0f;
642 entmins[2] = origin[2] - 1.0f;
643 entmaxs[0] = origin[0] + 1.0f;
644 entmaxs[1] = origin[1] + 1.0f;
645 entmaxs[2] = origin[2] + 1.0f;
646 // using the model's bounding box to ensure things are visible regardless of their physics box
649 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
651 VectorAdd(entmins, model->rotatedmins, entmins);
652 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
654 else if (ent->v.angles[1])
656 VectorAdd(entmins, model->yawmins, entmins);
657 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
661 VectorAdd(entmins, model->normalmins, entmins);
662 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
666 // if not touching a visible leaf
667 if (sv_pvscheckentities.value && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
670 // or not visible through the portals
671 if (sv_vischeckentities.value && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
673 sv_vischeckentitycullcount++;
682 effects = ent->v.effects;
684 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
685 if (val->_float != 0)
686 alpha = (int) (val->_float * 255.0);
689 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
690 if (val->_float != 0)
691 alpha = (int) val->_float;
695 alpha = bound(0, alpha, 255);
697 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
698 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
699 if (scale < 0) scale = 0;
700 if (scale > 255) scale = 255;
702 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
703 if (val->_float != 0)
704 glowcolor = (int) val->_float;
706 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
707 if (val->_float != 0)
708 effects |= EF_FULLBRIGHT;
710 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
711 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
712 colormod = (bound(0, (int) (val->vector[0] * 8.0), 7) << 5) | (bound(0, (int) (val->vector[1] * 8.0), 7) << 2) | bound(0, (int) (val->vector[2] * 4.0), 3);
716 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
720 // don't send if flagged for NODRAW and there are no effects
721 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
724 else // no model and no effects
729 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
731 Con_Printf ("packet overflow\n");
732 // mark the rest of the entities so they can't be delta compressed against this frame
733 for (;e < sv.num_edicts;e++)
734 client->nextfullupdate[e] = -1;
738 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
739 bits = bits | U_EXTERIORMODEL;
742 baseline = &ent->baseline;
744 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.value)
746 // every half second a full update is forced
747 if (realtime < client->nextfullupdate[e])
750 baseline = &ent->deltabaseline;
753 nextfullupdate = realtime + 0.5f;
756 nextfullupdate = realtime + 0.5f;
758 // restore nextfullupdate since this is being sent for real
759 client->nextfullupdate[e] = nextfullupdate;
762 bits |= U_LONGENTITY;
764 if (ent->v.movetype == MOVETYPE_STEP)
767 // LordHavoc: old stuff, but rewritten to have more exact tolerances
768 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
769 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
770 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
771 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
772 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
773 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
774 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
775 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
776 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
777 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
778 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
779 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
780 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
781 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
783 // LordHavoc: new stuff
784 if (baseline->alpha != alpha) bits |= U_ALPHA;
785 if (baseline->scale != scale) bits |= U_SCALE;
786 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
787 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
788 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
789 if (baseline->colormod != colormod) bits |= U_COLORMOD;
790 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
791 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
793 // update delta baseline
794 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
795 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
796 ent->deltabaseline.colormap = ent->v.colormap;
797 ent->deltabaseline.skin = ent->v.skin;
798 ent->deltabaseline.frame = ent->v.frame;
799 ent->deltabaseline.effects = ent->v.effects;
800 ent->deltabaseline.modelindex = ent->v.modelindex;
801 ent->deltabaseline.alpha = alpha;
802 ent->deltabaseline.scale = scale;
803 ent->deltabaseline.glowsize = glowsize;
804 ent->deltabaseline.glowcolor = glowcolor;
805 ent->deltabaseline.colormod = colormod;
807 if (bits & (U_ALPHA | U_SCALE | U_EFFECTS2 | U_GLOWSIZE | U_GLOWCOLOR | U_COLORMOD | U_FRAME2 | U_MODEL2))
811 if (bits >= 16777216)
819 MSG_WriteByte (msg, bits);
820 if (bits & U_MOREBITS)
821 MSG_WriteByte (msg, bits>>8);
822 // LordHavoc: extend bytes have to be written here due to delta compression
823 if (bits & U_EXTEND1)
824 MSG_WriteByte (msg, bits>>16);
825 if (bits & U_EXTEND2)
826 MSG_WriteByte (msg, bits>>24);
828 // LordHavoc: old stuff
829 if (bits & U_LONGENTITY)
830 MSG_WriteShort (msg,e);
832 MSG_WriteByte (msg,e);
833 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
834 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
835 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
836 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
837 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
838 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
839 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
840 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
841 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
842 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
843 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
845 // LordHavoc: new stuff
846 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
847 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
848 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
849 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
850 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
851 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
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_reportvischeckentities.value)
857 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
858 sv_vischeckentitycullcount = 0;
867 void SV_CleanupEnts (void)
872 ent = NEXT_EDICT(sv.edicts);
873 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
875 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
882 SV_WriteClientdataToMessage
886 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
896 // send a damage message
898 if (ent->v.dmg_take || ent->v.dmg_save)
900 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
901 MSG_WriteByte (msg, svc_damage);
902 MSG_WriteByte (msg, ent->v.dmg_save);
903 MSG_WriteByte (msg, ent->v.dmg_take);
904 for (i=0 ; i<3 ; i++)
905 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
912 // send the current viewpos offset from the view entity
914 SV_SetIdealPitch (); // how much to look up / down ideally
916 // a fixangle might get lost in a dropped packet. Oh well.
917 if ( ent->v.fixangle )
919 MSG_WriteByte (msg, svc_setangle);
920 for (i=0 ; i < 3 ; i++)
921 MSG_WriteAngle (msg, ent->v.angles[i] );
927 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
928 bits |= SU_VIEWHEIGHT;
930 if (ent->v.idealpitch)
931 bits |= SU_IDEALPITCH;
933 // stuff the sigil bits into the high bits of items for sbar, or else
935 val = GETEDICTFIELDVALUE(ent, eval_items2);
938 items = (int)ent->v.items | ((int)val->_float << 23);
940 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
944 if ( (int)ent->v.flags & FL_ONGROUND)
947 if ( ent->v.waterlevel >= 2)
951 VectorClear(punchvector);
952 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
953 VectorCopy(val->vector, punchvector);
955 for (i=0 ; i<3 ; i++)
957 if (ent->v.punchangle[i])
958 bits |= (SU_PUNCH1<<i);
959 if (punchvector[i]) // dpprotocol
960 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
961 if (ent->v.velocity[i])
962 bits |= (SU_VELOCITY1<<i);
965 if (ent->v.weaponframe)
966 bits |= SU_WEAPONFRAME;
968 if (ent->v.armorvalue)
971 // if (ent->v.weapon)
976 if (bits >= 16777216)
981 MSG_WriteByte (msg, svc_clientdata);
982 MSG_WriteShort (msg, bits);
983 if (bits & SU_EXTEND1)
984 MSG_WriteByte(msg, bits >> 16);
985 if (bits & SU_EXTEND2)
986 MSG_WriteByte(msg, bits >> 24);
988 if (bits & SU_VIEWHEIGHT)
989 MSG_WriteChar (msg, ent->v.view_ofs[2]);
991 if (bits & SU_IDEALPITCH)
992 MSG_WriteChar (msg, ent->v.idealpitch);
994 for (i=0 ; i<3 ; i++)
996 if (bits & (SU_PUNCH1<<i))
997 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
998 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
999 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
1000 if (bits & (SU_VELOCITY1<<i))
1001 MSG_WriteChar (msg, ent->v.velocity[i]/16);
1004 // [always sent] if (bits & SU_ITEMS)
1005 MSG_WriteLong (msg, items);
1007 if (bits & SU_WEAPONFRAME)
1008 MSG_WriteByte (msg, ent->v.weaponframe);
1009 if (bits & SU_ARMOR)
1010 MSG_WriteByte (msg, ent->v.armorvalue);
1011 if (bits & SU_WEAPON)
1012 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1014 MSG_WriteShort (msg, ent->v.health);
1015 MSG_WriteByte (msg, ent->v.currentammo);
1016 MSG_WriteByte (msg, ent->v.ammo_shells);
1017 MSG_WriteByte (msg, ent->v.ammo_nails);
1018 MSG_WriteByte (msg, ent->v.ammo_rockets);
1019 MSG_WriteByte (msg, ent->v.ammo_cells);
1021 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1025 if ( ((int)ent->v.weapon) & (1<<i) )
1027 MSG_WriteByte (msg, i);
1034 MSG_WriteByte (msg, ent->v.weapon);
1039 =======================
1040 SV_SendClientDatagram
1041 =======================
1043 qboolean SV_SendClientDatagram (client_t *client)
1045 byte buf[MAX_DATAGRAM];
1049 msg.maxsize = sizeof(buf);
1052 MSG_WriteByte (&msg, svc_time);
1053 MSG_WriteFloat (&msg, sv.time);
1055 // add the client specific data to the datagram
1056 SV_WriteClientdataToMessage (client->edict, &msg);
1058 SV_WriteEntitiesToClient (client, client->edict, &msg);
1060 // copy the server datagram if there is space
1061 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1062 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1064 // send the datagram
1065 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1067 SV_DropClient (true);// if the message couldn't send, kick off
1075 =======================
1076 SV_UpdateToReliableMessages
1077 =======================
1079 void SV_UpdateToReliableMessages (void)
1084 // check for changes to be sent over the reliable streams
1085 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1087 if (host_client->old_frags != host_client->edict->v.frags)
1089 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1091 if (!client->active)
1093 MSG_WriteByte (&client->message, svc_updatefrags);
1094 MSG_WriteByte (&client->message, i);
1095 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1098 host_client->old_frags = host_client->edict->v.frags;
1102 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1104 if (!client->active)
1106 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1109 SZ_Clear (&sv.reliable_datagram);
1114 =======================
1117 Send a nop message without trashing or sending the accumulated client
1119 =======================
1121 void SV_SendNop (client_t *client)
1127 msg.maxsize = sizeof(buf);
1130 MSG_WriteChar (&msg, svc_nop);
1132 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1133 SV_DropClient (true); // if the message couldn't send, kick off
1134 client->last_message = realtime;
1138 =======================
1139 SV_SendClientMessages
1140 =======================
1142 void SV_SendClientMessages (void)
1146 // update frags, names, etc
1147 SV_UpdateToReliableMessages ();
1149 // build individual updates
1150 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1152 if (!host_client->active)
1155 if (host_client->spawned)
1157 if (!SV_SendClientDatagram (host_client))
1162 // the player isn't totally in the game yet
1163 // send small keepalive messages if too much time has passed
1164 // send a full message when the next signon stage has been requested
1165 // some other message data (name changes, etc) may accumulate
1166 // between signon stages
1167 if (!host_client->sendsignon)
1169 if (realtime - host_client->last_message > 5)
1170 SV_SendNop (host_client);
1171 continue; // don't send out non-signon messages
1175 // check for an overflowed message. Should only happen
1176 // on a very fucked up connection that backs up a lot, then
1178 if (host_client->message.overflowed)
1180 SV_DropClient (true);
1181 host_client->message.overflowed = false;
1185 if (host_client->message.cursize || host_client->dropasap)
1187 if (!NET_CanSendMessage (host_client->netconnection))
1189 // I_Printf ("can't write\n");
1193 if (host_client->dropasap)
1194 SV_DropClient (false); // went to another level
1197 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1198 SV_DropClient (true); // if the message couldn't send, kick off
1199 SZ_Clear (&host_client->message);
1200 host_client->last_message = realtime;
1201 host_client->sendsignon = false;
1207 // clear muzzle flashes
1213 ==============================================================================
1217 ==============================================================================
1226 int SV_ModelIndex (char *name)
1230 if (!name || !name[0])
1233 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1234 if (!strcmp(sv.model_precache[i], name))
1236 if (i==MAX_MODELS || !sv.model_precache[i])
1237 Host_Error ("SV_ModelIndex: model %s not precached", name);
1247 void SV_CreateBaseline (void)
1249 int i, entnum, large;
1252 // LordHavoc: clear *all* states (note just active ones)
1253 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1255 // get the current server version
1256 svent = EDICT_NUM(entnum);
1258 // LordHavoc: always clear state values, whether the entity is in use or not
1259 ClearStateToDefault(&svent->baseline);
1263 if (entnum > svs.maxclients && !svent->v.modelindex)
1267 // create entity baseline
1269 VectorCopy (svent->v.origin, svent->baseline.origin);
1270 VectorCopy (svent->v.angles, svent->baseline.angles);
1271 svent->baseline.frame = svent->v.frame;
1272 svent->baseline.skin = svent->v.skin;
1273 if (entnum > 0 && entnum <= svs.maxclients)
1275 svent->baseline.colormap = entnum;
1276 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1280 svent->baseline.colormap = 0;
1281 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1285 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1288 // add to the message
1291 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1293 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1294 MSG_WriteShort (&sv.signon, entnum);
1298 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1299 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1303 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1304 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1306 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1307 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1308 for (i=0 ; i<3 ; i++)
1310 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1311 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1321 Tell all the clients that the server is changing levels
1324 void SV_SendReconnect (void)
1331 msg.maxsize = sizeof(data);
1333 MSG_WriteChar (&msg, svc_stufftext);
1334 MSG_WriteString (&msg, "reconnect\n");
1335 NET_SendToAll (&msg, 5);
1337 if (cls.state != ca_dedicated)
1338 Cmd_ExecuteString ("reconnect\n", src_command);
1346 Grabs the current state of each client for saving across the
1347 transition to another level
1350 void SV_SaveSpawnparms (void)
1354 svs.serverflags = pr_global_struct->serverflags;
1356 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1358 if (!host_client->active)
1361 // call the progs to get default spawn parms for the new client
1362 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1363 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1364 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1365 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1369 qboolean isworldmodel;
1375 This is called at the start of each level
1378 extern float scr_centertime_off;
1380 void SV_SpawnServer (char *server)
1385 // let's not have any servers with no name
1386 if (hostname.string[0] == 0)
1387 Cvar_Set ("hostname", "UNNAMED");
1388 scr_centertime_off = 0;
1390 Con_DPrintf ("SpawnServer: %s\n",server);
1391 svs.changelevel_issued = false; // now safe to issue another
1394 // tell all connected clients that we are going to a new level
1398 SV_SendReconnect ();
1402 // make cvars consistant
1405 Cvar_SetValue ("deathmatch", 0);
1406 current_skill = (int)(skill.value + 0.5);
1407 if (current_skill < 0)
1409 if (current_skill > 3)
1412 Cvar_SetValue ("skill", (float)current_skill);
1415 // set up the new server
1417 Host_ClearMemory ();
1419 memset (&sv, 0, sizeof(sv));
1421 strcpy (sv.name, server);
1423 // load progs to get entity field count
1426 // allocate server memory
1427 sv.max_edicts = MAX_EDICTS;
1429 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1431 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1432 sv.datagram.cursize = 0;
1433 sv.datagram.data = sv.datagram_buf;
1435 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1436 sv.reliable_datagram.cursize = 0;
1437 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1439 sv.signon.maxsize = sizeof(sv.signon_buf);
1440 sv.signon.cursize = 0;
1441 sv.signon.data = sv.signon_buf;
1443 // leave slots at start for clients only
1444 sv.num_edicts = svs.maxclients+1;
1445 for (i=0 ; i<svs.maxclients ; i++)
1447 ent = EDICT_NUM(i+1);
1448 svs.clients[i].edict = ent;
1451 sv.state = ss_loading;
1456 strcpy (sv.name, server);
1457 sprintf (sv.modelname,"maps/%s.bsp", server);
1458 isworldmodel = true; // LordHavoc: only load submodels on the world model
1459 sv.worldmodel = Mod_ForName (sv.modelname, false);
1460 isworldmodel = false;
1463 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1467 sv.models[1] = sv.worldmodel;
1470 // clear world interaction links
1474 sv.sound_precache[0] = pr_strings;
1476 sv.model_precache[0] = pr_strings;
1477 sv.model_precache[1] = sv.modelname;
1478 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1480 sv.model_precache[1+i] = localmodels[i];
1481 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1485 // load the rest of the entities
1488 memset (&ent->v, 0, progs->entityfields * 4);
1490 ent->v.model = sv.worldmodel->name - pr_strings;
1491 ent->v.modelindex = 1; // world model
1492 ent->v.solid = SOLID_BSP;
1493 ent->v.movetype = MOVETYPE_PUSH;
1496 pr_global_struct->coop = coop.value;
1498 pr_global_struct->deathmatch = deathmatch.value;
1500 pr_global_struct->mapname = sv.name - pr_strings;
1502 // serverflags are for cross level information (sigils)
1503 pr_global_struct->serverflags = svs.serverflags;
1505 ED_LoadFromFile (sv.worldmodel->entities);
1506 // LordHavoc: clear world angles (to fix e3m3.bsp)
1507 VectorClear(sv.edicts->v.angles);
1511 // all setup is completed, any further precache statements are errors
1512 sv.state = ss_active;
1514 // run two frames to allow everything to settle
1515 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1519 // create a baseline for more efficient communications
1520 SV_CreateBaseline ();
1522 // send serverinfo to all connected clients
1523 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1524 if (host_client->active)
1525 SV_SendServerinfo (host_client);
1527 Con_DPrintf ("Server spawned.\n");