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", "1"};
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, "%c\nDARKPLACES VERSION %4.2f BUILD %i SERVER (%i CRC)", 2, VERSION, 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
507 // find the client's PVS
508 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
509 pvs = SV_FatPVS (org);
512 MSG_WriteByte(msg, svc_playerposition);
513 MSG_WriteFloat(msg, org[0]);
514 MSG_WriteFloat(msg, org[1]);
515 MSG_WriteFloat(msg, org[2]);
518 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
519 // send over all entities (except the client) that touch the pvs
520 ent = NEXT_EDICT(sv.edicts);
521 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
525 // prevent delta compression against this frame (unless actually sent, which will restore this later)
526 nextfullupdate = client->nextfullupdate[e];
527 client->nextfullupdate[e] = -1;
529 if (ent != clent) // LordHavoc: always send player
531 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
533 if (val->edict != clentnum)
535 // don't show to anyone else
539 bits |= U_VIEWMODEL; // show relative to the view
543 // LordHavoc: never draw something told not to display to this client
544 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
546 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
553 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
554 glowsize = (int) val->_float >> 2;
555 if (glowsize > 255) glowsize = 255;
556 if (glowsize < 0) glowsize = 0;
558 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
559 if (val->_float != 0)
562 if (ent->v.modelindex == 0 || pr_strings[ent->v.model] == 0) // no model
563 if (ent != clent) // LordHavoc: always send player
564 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
567 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
569 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
571 ent->steplerptime = sv.time;
572 VectorCopy(ent->v.origin, ent->stepoldorigin);
573 VectorCopy(ent->v.angles, ent->stepoldangles);
574 VectorCopy(ent->v.origin, ent->steporigin);
575 VectorCopy(ent->v.angles, ent->stepangles);
577 VectorSubtract(ent->v.origin, ent->steporigin, origin);
578 VectorSubtract(ent->v.angles, ent->stepangles, angles);
579 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
581 // update lerp positions
582 ent->steplerptime = sv.time;
583 VectorCopy(ent->steporigin, ent->stepoldorigin);
584 VectorCopy(ent->stepangles, ent->stepoldangles);
585 VectorCopy(ent->v.origin, ent->steporigin);
586 VectorCopy(ent->v.angles, ent->stepangles);
588 movelerp = (sv.time - ent->steplerptime) * 10.0;
589 if (movelerp > 1) movelerp = 1;
590 moveilerp = 1 - movelerp;
591 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
592 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
593 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
594 // choose shortest rotate (to avoid 'spin around' situations)
595 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
596 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
597 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
598 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
599 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
600 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
601 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
602 //VectorMA(origin, host_client->latency, ent->v.velocity, origin);
604 else // copy as they are
606 VectorCopy(ent->v.angles, angles);
607 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
609 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
610 // LordHavoc: trace predicted movement to avoid putting things in walls
611 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
612 VectorCopy(trace.endpos, origin);
616 VectorCopy(ent->v.origin, origin);
618 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
620 // update lerp positions
621 ent->steplerptime = sv.time;
622 VectorCopy(ent->v.origin, ent->stepoldorigin);
623 VectorCopy(ent->v.angles, ent->stepoldangles);
624 VectorCopy(ent->v.origin, ent->steporigin);
625 VectorCopy(ent->v.angles, ent->stepangles);
629 // ent has survived every check so far, check if it is visible
630 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
632 // use the predicted origin
633 entmins[0] = ent->v.mins[0] + origin[0] - 1.0f;
634 entmins[1] = ent->v.mins[1] + origin[1] - 1.0f;
635 entmins[2] = ent->v.mins[2] + origin[2] - 1.0f;
636 entmaxs[0] = ent->v.maxs[0] + origin[0] + 1.0f;
637 entmaxs[1] = ent->v.maxs[1] + origin[1] + 1.0f;
638 entmaxs[2] = ent->v.maxs[2] + origin[2] + 1.0f;
640 // if not touching a visible leaf
641 if (sv_pvscheckentities.value && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
644 // or not visible through the portals
645 if (sv_vischeckentities.value && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
647 sv_vischeckentitycullcount++;
656 effects = ent->v.effects;
658 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
659 if (val->_float != 0)
660 alpha = (int) (val->_float * 255.0);
663 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
664 if (val->_float != 0)
665 alpha = (int) val->_float;
669 alpha = bound(0, alpha, 255);
671 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
672 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
673 if (scale < 0) scale = 0;
674 if (scale > 255) scale = 255;
676 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
677 if (val->_float != 0)
678 glowcolor = (int) val->_float;
680 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
681 if (val->_float != 0)
682 effects |= EF_FULLBRIGHT;
684 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
685 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
686 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);
690 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
692 if (ent->v.modelindex && pr_strings[ent->v.model]) // model
694 // don't send if flagged for NODRAW and there are no effects
695 if (sv.models[(int)ent->v.modelindex]->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
698 else // no model and no effects
703 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
705 Con_Printf ("packet overflow\n");
706 // mark the rest of the entities so they can't be delta compressed against this frame
707 for (;e < sv.num_edicts;e++)
708 client->nextfullupdate[e] = -1;
712 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
713 bits = bits | U_EXTERIORMODEL;
716 baseline = &ent->baseline;
718 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.value)
720 // every half second a full update is forced
721 if (realtime < client->nextfullupdate[e])
724 baseline = &ent->deltabaseline;
727 nextfullupdate = realtime + 0.5f;
730 nextfullupdate = realtime + 0.5f;
732 // restore nextfullupdate since this is being sent for real
733 client->nextfullupdate[e] = nextfullupdate;
736 bits |= U_LONGENTITY;
738 if (ent->v.movetype == MOVETYPE_STEP)
741 // LordHavoc: old stuff, but rewritten to have more exact tolerances
742 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
743 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
744 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
745 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
746 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
747 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
748 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
749 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
750 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
751 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
752 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
753 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
754 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
755 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
757 // LordHavoc: new stuff
758 if (baseline->alpha != alpha) bits |= U_ALPHA;
759 if (baseline->scale != scale) bits |= U_SCALE;
760 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
761 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
762 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
763 if (baseline->colormod != colormod) bits |= U_COLORMOD;
764 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
765 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
767 // update delta baseline
768 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
769 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
770 ent->deltabaseline.colormap = ent->v.colormap;
771 ent->deltabaseline.skin = ent->v.skin;
772 ent->deltabaseline.frame = ent->v.frame;
773 ent->deltabaseline.effects = ent->v.effects;
774 ent->deltabaseline.modelindex = ent->v.modelindex;
775 ent->deltabaseline.alpha = alpha;
776 ent->deltabaseline.scale = scale;
777 ent->deltabaseline.glowsize = glowsize;
778 ent->deltabaseline.glowcolor = glowcolor;
779 ent->deltabaseline.colormod = colormod;
781 if (bits & (U_ALPHA | U_SCALE | U_EFFECTS2 | U_GLOWSIZE | U_GLOWCOLOR | U_COLORMOD | U_FRAME2 | U_MODEL2))
785 if (bits >= 16777216)
793 MSG_WriteByte (msg, bits);
794 if (bits & U_MOREBITS)
795 MSG_WriteByte (msg, bits>>8);
796 // LordHavoc: extend bytes have to be written here due to delta compression
797 if (bits & U_EXTEND1)
798 MSG_WriteByte (msg, bits>>16);
799 if (bits & U_EXTEND2)
800 MSG_WriteByte (msg, bits>>24);
802 // LordHavoc: old stuff
803 if (bits & U_LONGENTITY)
804 MSG_WriteShort (msg,e);
806 MSG_WriteByte (msg,e);
807 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
808 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
809 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
810 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
811 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
812 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
813 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
814 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
815 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
816 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
817 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
819 // LordHavoc: new stuff
820 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
821 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
822 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
823 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
824 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
825 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
826 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
827 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
830 if (sv_reportvischeckentities.value)
831 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
832 sv_vischeckentitycullcount = 0;
841 void SV_CleanupEnts (void)
846 ent = NEXT_EDICT(sv.edicts);
847 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
849 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
856 SV_WriteClientdataToMessage
860 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
870 // send a damage message
872 if (ent->v.dmg_take || ent->v.dmg_save)
874 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
875 MSG_WriteByte (msg, svc_damage);
876 MSG_WriteByte (msg, ent->v.dmg_save);
877 MSG_WriteByte (msg, ent->v.dmg_take);
878 for (i=0 ; i<3 ; i++)
879 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
886 // send the current viewpos offset from the view entity
888 SV_SetIdealPitch (); // how much to look up / down ideally
890 // a fixangle might get lost in a dropped packet. Oh well.
891 if ( ent->v.fixangle )
893 MSG_WriteByte (msg, svc_setangle);
894 for (i=0 ; i < 3 ; i++)
895 MSG_WriteAngle (msg, ent->v.angles[i] );
901 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
902 bits |= SU_VIEWHEIGHT;
904 if (ent->v.idealpitch)
905 bits |= SU_IDEALPITCH;
907 // stuff the sigil bits into the high bits of items for sbar, or else
909 val = GETEDICTFIELDVALUE(ent, eval_items2);
912 items = (int)ent->v.items | ((int)val->_float << 23);
914 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
918 if ( (int)ent->v.flags & FL_ONGROUND)
921 if ( ent->v.waterlevel >= 2)
925 VectorClear(punchvector);
926 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
927 VectorCopy(val->vector, punchvector);
929 for (i=0 ; i<3 ; i++)
931 if (ent->v.punchangle[i])
932 bits |= (SU_PUNCH1<<i);
933 if (punchvector[i]) // dpprotocol
934 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
935 if (ent->v.velocity[i])
936 bits |= (SU_VELOCITY1<<i);
939 if (ent->v.weaponframe)
940 bits |= SU_WEAPONFRAME;
942 if (ent->v.armorvalue)
945 // if (ent->v.weapon)
950 if (bits >= 16777216)
955 MSG_WriteByte (msg, svc_clientdata);
956 MSG_WriteShort (msg, bits);
957 if (bits & SU_EXTEND1)
958 MSG_WriteByte(msg, bits >> 16);
959 if (bits & SU_EXTEND2)
960 MSG_WriteByte(msg, bits >> 24);
962 if (bits & SU_VIEWHEIGHT)
963 MSG_WriteChar (msg, ent->v.view_ofs[2]);
965 if (bits & SU_IDEALPITCH)
966 MSG_WriteChar (msg, ent->v.idealpitch);
968 for (i=0 ; i<3 ; i++)
970 if (bits & (SU_PUNCH1<<i))
971 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
972 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
973 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
974 if (bits & (SU_VELOCITY1<<i))
975 MSG_WriteChar (msg, ent->v.velocity[i]/16);
978 // [always sent] if (bits & SU_ITEMS)
979 MSG_WriteLong (msg, items);
981 if (bits & SU_WEAPONFRAME)
982 MSG_WriteByte (msg, ent->v.weaponframe);
984 MSG_WriteByte (msg, ent->v.armorvalue);
985 if (bits & SU_WEAPON)
986 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
988 MSG_WriteShort (msg, ent->v.health);
989 MSG_WriteByte (msg, ent->v.currentammo);
990 MSG_WriteByte (msg, ent->v.ammo_shells);
991 MSG_WriteByte (msg, ent->v.ammo_nails);
992 MSG_WriteByte (msg, ent->v.ammo_rockets);
993 MSG_WriteByte (msg, ent->v.ammo_cells);
997 MSG_WriteByte (msg, ent->v.weapon);
1003 if ( ((int)ent->v.weapon) & (1<<i) )
1005 MSG_WriteByte (msg, i);
1013 =======================
1014 SV_SendClientDatagram
1015 =======================
1017 qboolean SV_SendClientDatagram (client_t *client)
1019 byte buf[MAX_DATAGRAM];
1023 msg.maxsize = sizeof(buf);
1026 MSG_WriteByte (&msg, svc_time);
1027 MSG_WriteFloat (&msg, sv.time);
1029 // add the client specific data to the datagram
1030 SV_WriteClientdataToMessage (client->edict, &msg);
1032 SV_WriteEntitiesToClient (client, client->edict, &msg);
1034 // copy the server datagram if there is space
1035 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1036 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1038 // send the datagram
1039 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1041 SV_DropClient (true);// if the message couldn't send, kick off
1049 =======================
1050 SV_UpdateToReliableMessages
1051 =======================
1053 void SV_UpdateToReliableMessages (void)
1058 // check for changes to be sent over the reliable streams
1059 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1061 if (host_client->old_frags != host_client->edict->v.frags)
1063 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1065 if (!client->active)
1067 MSG_WriteByte (&client->message, svc_updatefrags);
1068 MSG_WriteByte (&client->message, i);
1069 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1072 host_client->old_frags = host_client->edict->v.frags;
1076 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1078 if (!client->active)
1080 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1083 SZ_Clear (&sv.reliable_datagram);
1088 =======================
1091 Send a nop message without trashing or sending the accumulated client
1093 =======================
1095 void SV_SendNop (client_t *client)
1101 msg.maxsize = sizeof(buf);
1104 MSG_WriteChar (&msg, svc_nop);
1106 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1107 SV_DropClient (true); // if the message couldn't send, kick off
1108 client->last_message = realtime;
1112 =======================
1113 SV_SendClientMessages
1114 =======================
1116 void SV_SendClientMessages (void)
1120 // update frags, names, etc
1121 SV_UpdateToReliableMessages ();
1123 // build individual updates
1124 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1126 if (!host_client->active)
1129 if (host_client->spawned)
1131 if (!SV_SendClientDatagram (host_client))
1136 // the player isn't totally in the game yet
1137 // send small keepalive messages if too much time has passed
1138 // send a full message when the next signon stage has been requested
1139 // some other message data (name changes, etc) may accumulate
1140 // between signon stages
1141 if (!host_client->sendsignon)
1143 if (realtime - host_client->last_message > 5)
1144 SV_SendNop (host_client);
1145 continue; // don't send out non-signon messages
1149 // check for an overflowed message. Should only happen
1150 // on a very fucked up connection that backs up a lot, then
1152 if (host_client->message.overflowed)
1154 SV_DropClient (true);
1155 host_client->message.overflowed = false;
1159 if (host_client->message.cursize || host_client->dropasap)
1161 if (!NET_CanSendMessage (host_client->netconnection))
1163 // I_Printf ("can't write\n");
1167 if (host_client->dropasap)
1168 SV_DropClient (false); // went to another level
1171 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1172 SV_DropClient (true); // if the message couldn't send, kick off
1173 SZ_Clear (&host_client->message);
1174 host_client->last_message = realtime;
1175 host_client->sendsignon = false;
1181 // clear muzzle flashes
1187 ==============================================================================
1191 ==============================================================================
1200 int SV_ModelIndex (char *name)
1204 if (!name || !name[0])
1207 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1208 if (!strcmp(sv.model_precache[i], name))
1210 if (i==MAX_MODELS || !sv.model_precache[i])
1211 Host_Error ("SV_ModelIndex: model %s not precached", name);
1221 void SV_CreateBaseline (void)
1223 int i, entnum, large;
1226 // LordHavoc: clear *all* states (note just active ones)
1227 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1229 // get the current server version
1230 svent = EDICT_NUM(entnum);
1232 // LordHavoc: always clear state values, whether the entity is in use or not
1233 ClearStateToDefault(&svent->baseline);
1237 if (entnum > svs.maxclients && !svent->v.modelindex)
1241 // create entity baseline
1243 VectorCopy (svent->v.origin, svent->baseline.origin);
1244 VectorCopy (svent->v.angles, svent->baseline.angles);
1245 svent->baseline.frame = svent->v.frame;
1246 svent->baseline.skin = svent->v.skin;
1247 if (entnum > 0 && entnum <= svs.maxclients)
1249 svent->baseline.colormap = entnum;
1250 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1254 svent->baseline.colormap = 0;
1255 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1259 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1262 // add to the message
1265 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1267 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1268 MSG_WriteShort (&sv.signon, entnum);
1272 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1273 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1277 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1278 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1280 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1281 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1282 for (i=0 ; i<3 ; i++)
1284 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1285 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1295 Tell all the clients that the server is changing levels
1298 void SV_SendReconnect (void)
1305 msg.maxsize = sizeof(data);
1307 MSG_WriteChar (&msg, svc_stufftext);
1308 MSG_WriteString (&msg, "reconnect\n");
1309 NET_SendToAll (&msg, 5);
1311 if (cls.state != ca_dedicated)
1312 Cmd_ExecuteString ("reconnect\n", src_command);
1320 Grabs the current state of each client for saving across the
1321 transition to another level
1324 void SV_SaveSpawnparms (void)
1328 svs.serverflags = pr_global_struct->serverflags;
1330 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1332 if (!host_client->active)
1335 // call the progs to get default spawn parms for the new client
1336 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1337 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1338 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1339 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1343 qboolean isworldmodel;
1349 This is called at the start of each level
1352 extern float scr_centertime_off;
1354 void SV_SpawnServer (char *server)
1359 // let's not have any servers with no name
1360 if (hostname.string[0] == 0)
1361 Cvar_Set ("hostname", "UNNAMED");
1362 scr_centertime_off = 0;
1364 Con_DPrintf ("SpawnServer: %s\n",server);
1365 svs.changelevel_issued = false; // now safe to issue another
1368 // tell all connected clients that we are going to a new level
1372 SV_SendReconnect ();
1376 // make cvars consistant
1379 Cvar_SetValue ("deathmatch", 0);
1380 current_skill = (int)(skill.value + 0.5);
1381 if (current_skill < 0)
1383 if (current_skill > 3)
1386 Cvar_SetValue ("skill", (float)current_skill);
1389 // set up the new server
1391 Host_ClearMemory ();
1393 memset (&sv, 0, sizeof(sv));
1395 strcpy (sv.name, server);
1397 // load progs to get entity field count
1400 // allocate server memory
1401 sv.max_edicts = MAX_EDICTS;
1403 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1405 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1406 sv.datagram.cursize = 0;
1407 sv.datagram.data = sv.datagram_buf;
1409 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1410 sv.reliable_datagram.cursize = 0;
1411 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1413 sv.signon.maxsize = sizeof(sv.signon_buf);
1414 sv.signon.cursize = 0;
1415 sv.signon.data = sv.signon_buf;
1417 // leave slots at start for clients only
1418 sv.num_edicts = svs.maxclients+1;
1419 for (i=0 ; i<svs.maxclients ; i++)
1421 ent = EDICT_NUM(i+1);
1422 svs.clients[i].edict = ent;
1425 sv.state = ss_loading;
1430 strcpy (sv.name, server);
1431 sprintf (sv.modelname,"maps/%s.bsp", server);
1432 isworldmodel = true; // LordHavoc: only load submodels on the world model
1433 sv.worldmodel = Mod_ForName (sv.modelname, false);
1434 isworldmodel = false;
1437 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1441 sv.models[1] = sv.worldmodel;
1444 // clear world interaction links
1448 sv.sound_precache[0] = pr_strings;
1450 sv.model_precache[0] = pr_strings;
1451 sv.model_precache[1] = sv.modelname;
1452 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1454 sv.model_precache[1+i] = localmodels[i];
1455 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1459 // load the rest of the entities
1462 memset (&ent->v, 0, progs->entityfields * 4);
1464 ent->v.model = sv.worldmodel->name - pr_strings;
1465 ent->v.modelindex = 1; // world model
1466 ent->v.solid = SOLID_BSP;
1467 ent->v.movetype = MOVETYPE_PUSH;
1470 pr_global_struct->coop = coop.value;
1472 pr_global_struct->deathmatch = deathmatch.value;
1474 pr_global_struct->mapname = sv.name - pr_strings;
1476 // serverflags are for cross level information (sigils)
1477 pr_global_struct->serverflags = svs.serverflags;
1479 ED_LoadFromFile (sv.worldmodel->entities);
1480 // LordHavoc: clear world angles (to fix e3m3.bsp)
1481 VectorClear(sv.edicts->v.angles);
1485 // all setup is completed, any further precache statements are errors
1486 sv.state = ss_active;
1488 // run two frames to allow everything to settle
1489 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1493 // create a baseline for more efficient communications
1494 SV_CreateBaseline ();
1496 // send serverinfo to all connected clients
1497 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1498 if (host_client->active)
1499 SV_SendServerinfo (host_client);
1501 Con_DPrintf ("Server spawned.\n");