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_vischeckentities = {0, "sv_vischeckentities", "1"};
25 cvar_t sv_reportvischeckentities = {0, "sv_reportvischeckentities", "0"};
26 int sv_vischeckentitycullcount = 0;
31 char localmodels[MAX_MODELS][5]; // inline model names for precache
33 //============================================================================
44 Cvar_RegisterVariable (&sv_maxvelocity);
45 Cvar_RegisterVariable (&sv_gravity);
46 Cvar_RegisterVariable (&sv_friction);
47 Cvar_RegisterVariable (&sv_edgefriction);
48 Cvar_RegisterVariable (&sv_stopspeed);
49 Cvar_RegisterVariable (&sv_maxspeed);
50 Cvar_RegisterVariable (&sv_accelerate);
51 Cvar_RegisterVariable (&sv_idealpitchscale);
52 Cvar_RegisterVariable (&sv_aim);
53 Cvar_RegisterVariable (&sv_nostep);
54 Cvar_RegisterVariable (&sv_predict);
55 Cvar_RegisterVariable (&sv_deltacompress);
56 Cvar_RegisterVariable (&sv_vischeckentities);
57 Cvar_RegisterVariable (&sv_reportvischeckentities);
59 for (i = 0;i < MAX_MODELS;i++)
60 sprintf (localmodels[i], "*%i", i);
64 =============================================================================
68 =============================================================================
75 Make sure the event gets sent to all clients
78 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
82 if (sv.datagram.cursize > MAX_DATAGRAM-16)
84 MSG_WriteByte (&sv.datagram, svc_particle);
85 MSG_WriteFloatCoord (&sv.datagram, org[0]);
86 MSG_WriteFloatCoord (&sv.datagram, org[1]);
87 MSG_WriteFloatCoord (&sv.datagram, org[2]);
95 MSG_WriteChar (&sv.datagram, v);
97 MSG_WriteByte (&sv.datagram, count);
98 MSG_WriteByte (&sv.datagram, color);
105 Make sure the event gets sent to all clients
108 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
110 if (sv.datagram.cursize > MAX_DATAGRAM-18)
112 if (modelindex >= 256 || startframe >= 256)
114 MSG_WriteByte (&sv.datagram, svc_effect2);
115 MSG_WriteFloatCoord (&sv.datagram, org[0]);
116 MSG_WriteFloatCoord (&sv.datagram, org[1]);
117 MSG_WriteFloatCoord (&sv.datagram, org[2]);
118 MSG_WriteShort (&sv.datagram, modelindex);
119 MSG_WriteShort (&sv.datagram, startframe);
120 MSG_WriteByte (&sv.datagram, framecount);
121 MSG_WriteByte (&sv.datagram, framerate);
125 MSG_WriteByte (&sv.datagram, svc_effect);
126 MSG_WriteFloatCoord (&sv.datagram, org[0]);
127 MSG_WriteFloatCoord (&sv.datagram, org[1]);
128 MSG_WriteFloatCoord (&sv.datagram, org[2]);
129 MSG_WriteByte (&sv.datagram, modelindex);
130 MSG_WriteByte (&sv.datagram, startframe);
131 MSG_WriteByte (&sv.datagram, framecount);
132 MSG_WriteByte (&sv.datagram, framerate);
140 Each entity can have eight independant sound sources, like voice,
143 Channel 0 is an auto-allocate channel, the others override anything
144 already running on that entity/channel pair.
146 An attenuation of 0 will play full volume everywhere in the level.
147 Larger attenuations will drop off. (max 4 attenuation)
151 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
159 if (volume < 0 || volume > 255)
160 Host_Error ("SV_StartSound: volume = %i", volume);
162 if (attenuation < 0 || attenuation > 4)
163 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
165 if (channel < 0 || channel > 7)
166 Host_Error ("SV_StartSound: channel = %i", channel);
168 if (sv.datagram.cursize > MAX_DATAGRAM-16)
171 // find precache number for sound
172 for (sound_num=1 ; sound_num<MAX_SOUNDS
173 && sv.sound_precache[sound_num] ; sound_num++)
174 if (!strcmp(sample, sv.sound_precache[sound_num]))
177 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
179 Con_Printf ("SV_StartSound: %s not precached\n", sample);
183 ent = NUM_FOR_EDICT(entity);
185 channel = (ent<<3) | channel;
188 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
189 field_mask |= SND_VOLUME;
190 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
191 field_mask |= SND_ATTENUATION;
193 // directed messages go only to the entity they are targeted on
194 if (sound_num >= 256)
195 MSG_WriteByte (&sv.datagram, svc_sound2);
197 MSG_WriteByte (&sv.datagram, svc_sound);
198 MSG_WriteByte (&sv.datagram, field_mask);
199 if (field_mask & SND_VOLUME)
200 MSG_WriteByte (&sv.datagram, volume);
201 if (field_mask & SND_ATTENUATION)
202 MSG_WriteByte (&sv.datagram, attenuation*64);
203 MSG_WriteShort (&sv.datagram, channel);
204 if (sound_num >= 256)
205 MSG_WriteShort (&sv.datagram, sound_num);
207 MSG_WriteByte (&sv.datagram, sound_num);
208 for (i=0 ; i<3 ; i++)
209 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
213 ==============================================================================
217 ==============================================================================
224 Sends the first message from the server to a connected client.
225 This will be sent on the initial connection and upon each server load.
228 void SV_SendServerinfo (client_t *client)
233 MSG_WriteByte (&client->message, svc_print);
234 sprintf (message, "%c\nDARKPLACES VERSION %4.2f BUILD %i SERVER (%i CRC)", 2, VERSION, buildnumber, pr_crc);
235 MSG_WriteString (&client->message,message);
237 MSG_WriteByte (&client->message, svc_serverinfo);
238 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
239 MSG_WriteByte (&client->message, svs.maxclients);
241 if (!coop.value && deathmatch.value)
242 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
244 MSG_WriteByte (&client->message, GAME_COOP);
246 sprintf (message, pr_strings+sv.edicts->v.message);
248 MSG_WriteString (&client->message,message);
250 for (s = sv.model_precache+1 ; *s ; s++)
251 MSG_WriteString (&client->message, *s);
252 MSG_WriteByte (&client->message, 0);
254 for (s = sv.sound_precache+1 ; *s ; s++)
255 MSG_WriteString (&client->message, *s);
256 MSG_WriteByte (&client->message, 0);
259 MSG_WriteByte (&client->message, svc_cdtrack);
260 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
261 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
264 MSG_WriteByte (&client->message, svc_setview);
265 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
267 MSG_WriteByte (&client->message, svc_signonnum);
268 MSG_WriteByte (&client->message, 1);
270 client->sendsignon = true;
271 client->spawned = false; // need prespawn, spawn, etc
278 Initializes a client_t for a new net connection. This will only be called
279 once for a player each game, not once for each level change.
282 void SV_ConnectClient (int clientnum)
287 struct qsocket_s *netconnection;
289 float spawn_parms[NUM_SPAWN_PARMS];
291 client = svs.clients + clientnum;
293 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
295 edictnum = clientnum+1;
297 ent = EDICT_NUM(edictnum);
299 // set up the client_t
300 netconnection = client->netconnection;
303 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
304 memset (client, 0, sizeof(*client));
305 client->netconnection = netconnection;
307 strcpy (client->name, "unconnected");
308 client->active = true;
309 client->spawned = false;
311 client->message.data = client->msgbuf;
312 client->message.maxsize = sizeof(client->msgbuf);
313 client->message.allowoverflow = true; // we can catch it
316 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
319 // call the progs to get default spawn parms for the new client
320 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
321 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
322 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
325 SV_SendServerinfo (client);
331 SV_CheckForNewClients
335 void SV_CheckForNewClients (void)
337 struct qsocket_s *ret;
341 // check for new connections
345 ret = NET_CheckNewConnections ();
350 // init a new client structure
352 for (i=0 ; i<svs.maxclients ; i++)
353 if (!svs.clients[i].active)
355 if (i == svs.maxclients)
356 Sys_Error ("Host_CheckForNewClients: no free clients");
358 svs.clients[i].netconnection = ret;
359 SV_ConnectClient (i);
361 net_activeconnections++;
368 ===============================================================================
372 ===============================================================================
381 void SV_ClearDatagram (void)
383 SZ_Clear (&sv.datagram);
387 =============================================================================
389 The PVS must include a small area around the client to allow head bobbing
390 or other small motion on the client side. Otherwise, a bob might cause an
391 entity that should be visible to not show up, especially when the bob
394 =============================================================================
398 byte fatpvs[MAX_MAP_LEAFS/8];
400 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
409 // if this is a leaf, accumulate the pvs bits
410 if (node->contents < 0)
412 if (node->contents != CONTENTS_SOLID)
414 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
415 for (i=0 ; i<fatbytes ; i++)
422 d = DotProduct (org, plane->normal) - plane->dist;
424 node = node->children[0];
426 node = node->children[1];
429 SV_AddToFatPVS (org, node->children[0]);
430 node = node->children[1];
439 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
443 byte *SV_FatPVS (vec3_t org)
445 fatbytes = (sv.worldmodel->numleafs+31)>>3;
446 memset (fatpvs, 0, fatbytes);
447 SV_AddToFatPVS (org, sv.worldmodel->nodes);
451 //=============================================================================
456 SV_WriteEntitiesToClient
460 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
462 int e, i, clentnum, bits, alpha, glowcolor, glowsize, scale, colormod, effects;
464 vec3_t org, origin, angles, entmins, entmaxs;
465 float movelerp, moveilerp, nextfullupdate;
468 entity_state_t *baseline; // LordHavoc: delta or startup baseline
470 // find the client's PVS
471 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
472 pvs = SV_FatPVS (org);
476 MSG_WriteByte(msg, svc_playerposition);
477 MSG_WriteFloat(msg, org[0]);
478 MSG_WriteFloat(msg, org[1]);
479 MSG_WriteFloat(msg, org[2]);
483 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
484 // send over all entities (except the client) that touch the pvs
485 ent = NEXT_EDICT(sv.edicts);
486 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
490 // prevent delta compression against this frame (unless actually sent, which will restore this later)
491 nextfullupdate = client->nextfullupdate[e];
492 client->nextfullupdate[e] = -1;
494 if (ent != clent) // LordHavoc: always send player
496 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
498 if (val->edict != clentnum)
500 // don't show to anyone else
504 bits |= U_VIEWMODEL; // show relative to the view
508 // LordHavoc: never draw something told not to display to this client
509 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
511 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
513 // ignore if not touching a PV leaf
514 for (i = 0;i < ent->num_leafs;i++)
515 if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
518 if (i == ent->num_leafs)
524 VectorAdd(ent->v.mins, ent->v.origin, entmins);
525 VectorAdd(ent->v.maxs, ent->v.origin, entmaxs);
526 entmins[0] -= 1.0f;entmins[1] -= 1.0f;entmins[2] -= 1.0f;
527 entmaxs[0] += 1.0f;entmaxs[1] += 1.0f;entmaxs[2] += 1.0f;
528 if (!Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
530 sv_vischeckentitycullcount++;
536 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
537 bits = bits | U_EXTERIORMODEL;
539 // don't send if flagged for NODRAW and there are no effects
545 effects = ent->v.effects;
547 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
548 if (val->_float != 0)
549 alpha = (int) (val->_float * 255.0);
552 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
553 if (val->_float != 0)
554 alpha = (int) val->_float;
558 alpha = bound(0, alpha, 255);
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_scale)))
566 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
567 if (scale < 0) scale = 0;
568 if (scale > 255) scale = 255;
570 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
571 if (val->_float != 0)
574 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
575 if (val->_float != 0)
576 glowcolor = (int) val->_float;
578 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
579 if (val->_float != 0)
580 effects |= EF_FULLBRIGHT;
582 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
583 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
584 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);
588 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
590 if (ent->v.modelindex && pr_strings[ent->v.model]) // model
592 if (sv.models[ (int)ent->v.modelindex ]->flags == 0 && (ent->v.effects == EF_NODRAW || scale <= 0 || alpha <= 0))
595 else // no model and no effects
600 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
602 Con_Printf ("packet overflow\n");
603 // mark the rest of the entities so they can't be delta compressed against this frame
604 for (;e < sv.num_edicts;e++)
605 client->nextfullupdate[e] = -1;
610 baseline = &ent->baseline;
612 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.value)
614 // every half second a full update is forced
615 if (realtime < client->nextfullupdate[e])
618 baseline = &ent->deltabaseline;
621 nextfullupdate = realtime + 0.5f;
624 nextfullupdate = realtime + 0.5f;
626 // restore nextfullupdate since this is being sent
627 client->nextfullupdate[e] = nextfullupdate;
630 bits |= U_LONGENTITY;
632 if (ent->v.movetype == MOVETYPE_STEP)
635 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
637 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
639 ent->steplerptime = sv.time;
640 VectorCopy(ent->v.origin, ent->stepoldorigin);
641 VectorCopy(ent->v.angles, ent->stepoldangles);
642 VectorCopy(ent->v.origin, ent->steporigin);
643 VectorCopy(ent->v.angles, ent->stepangles);
645 VectorSubtract(ent->v.origin, ent->steporigin, origin);
646 VectorSubtract(ent->v.angles, ent->stepangles, angles);
647 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
649 // update lerp positions
650 ent->steplerptime = sv.time;
651 VectorCopy(ent->steporigin, ent->stepoldorigin);
652 VectorCopy(ent->stepangles, ent->stepoldangles);
653 VectorCopy(ent->v.origin, ent->steporigin);
654 VectorCopy(ent->v.angles, ent->stepangles);
656 movelerp = (sv.time - ent->steplerptime) * 10.0;
657 if (movelerp > 1) movelerp = 1;
658 moveilerp = 1 - movelerp;
659 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
660 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
661 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
662 // choose shortest rotate (to avoid 'spin around' situations)
663 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
664 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
665 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
666 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
667 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
668 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
669 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
670 VectorMA(origin, host_client->latency, ent->v.velocity, origin);
672 else // copy as they are
674 // VectorCopy(ent->v.origin, origin);
675 VectorCopy(ent->v.angles, angles);
676 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
677 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
679 // update lerp positions
680 ent->steplerptime = sv.time;
681 VectorCopy(ent->v.origin, ent->stepoldorigin);
682 VectorCopy(ent->v.angles, ent->stepoldangles);
683 VectorCopy(ent->v.origin, ent->steporigin);
684 VectorCopy(ent->v.angles, ent->stepangles);
688 // LordHavoc: old stuff, but rewritten to have more exact tolerances
689 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
690 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
691 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
692 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
693 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
694 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
695 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
696 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
697 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
698 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
699 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
700 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
701 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
702 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
704 // LordHavoc: new stuff
705 if (baseline->alpha != alpha) bits |= U_ALPHA;
706 if (baseline->scale != scale) bits |= U_SCALE;
707 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
708 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
709 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
710 if (baseline->colormod != colormod) bits |= U_COLORMOD;
711 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
712 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
714 // update delta baseline
715 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
716 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
717 ent->deltabaseline.colormap = ent->v.colormap;
718 ent->deltabaseline.skin = ent->v.skin;
719 ent->deltabaseline.frame = ent->v.frame;
720 ent->deltabaseline.effects = ent->v.effects;
721 ent->deltabaseline.modelindex = ent->v.modelindex;
722 ent->deltabaseline.alpha = alpha;
723 ent->deltabaseline.scale = scale;
724 ent->deltabaseline.glowsize = glowsize;
725 ent->deltabaseline.glowcolor = glowcolor;
726 ent->deltabaseline.colormod = colormod;
728 if (bits & (U_ALPHA | U_SCALE | U_EFFECTS2 | U_GLOWSIZE | U_GLOWCOLOR | U_COLORMOD | U_FRAME2 | U_MODEL2))
732 if (bits >= 16777216)
740 MSG_WriteByte (msg, bits);
741 if (bits & U_MOREBITS)
742 MSG_WriteByte (msg, bits>>8);
743 // LordHavoc: extend bytes have to be written here due to delta compression
744 if (bits & U_EXTEND1)
745 MSG_WriteByte (msg, bits>>16);
746 if (bits & U_EXTEND2)
747 MSG_WriteByte (msg, bits>>24);
749 // LordHavoc: old stuff
750 if (bits & U_LONGENTITY)
751 MSG_WriteShort (msg,e);
753 MSG_WriteByte (msg,e);
754 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
755 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
756 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
757 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
758 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
759 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
760 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
761 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
762 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
763 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
764 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
766 // LordHavoc: new stuff
767 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
768 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
769 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
770 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
771 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
772 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
773 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
774 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
777 if (sv_reportvischeckentities.value)
778 Con_Printf("sv_vischeck culled entities: %d\n", sv_vischeckentitycullcount);
779 sv_vischeckentitycullcount = 0;
788 void SV_CleanupEnts (void)
793 ent = NEXT_EDICT(sv.edicts);
794 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
796 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
803 SV_WriteClientdataToMessage
807 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
817 // send a damage message
819 if (ent->v.dmg_take || ent->v.dmg_save)
821 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
822 MSG_WriteByte (msg, svc_damage);
823 MSG_WriteByte (msg, ent->v.dmg_save);
824 MSG_WriteByte (msg, ent->v.dmg_take);
825 for (i=0 ; i<3 ; i++)
826 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
833 // send the current viewpos offset from the view entity
835 SV_SetIdealPitch (); // how much to look up / down ideally
837 // a fixangle might get lost in a dropped packet. Oh well.
838 if ( ent->v.fixangle )
840 MSG_WriteByte (msg, svc_setangle);
841 for (i=0 ; i < 3 ; i++)
842 MSG_WriteAngle (msg, ent->v.angles[i] );
848 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
849 bits |= SU_VIEWHEIGHT;
851 if (ent->v.idealpitch)
852 bits |= SU_IDEALPITCH;
854 // stuff the sigil bits into the high bits of items for sbar, or else
856 val = GETEDICTFIELDVALUE(ent, eval_items2);
859 items = (int)ent->v.items | ((int)val->_float << 23);
861 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
865 if ( (int)ent->v.flags & FL_ONGROUND)
868 if ( ent->v.waterlevel >= 2)
872 VectorClear(punchvector);
873 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
874 VectorCopy(val->vector, punchvector);
876 for (i=0 ; i<3 ; i++)
878 if (ent->v.punchangle[i])
879 bits |= (SU_PUNCH1<<i);
880 if (punchvector[i]) // dpprotocol
881 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
882 if (ent->v.velocity[i])
883 bits |= (SU_VELOCITY1<<i);
886 if (ent->v.weaponframe)
887 bits |= SU_WEAPONFRAME;
889 if (ent->v.armorvalue)
892 // if (ent->v.weapon)
897 if (bits >= 16777216)
902 MSG_WriteByte (msg, svc_clientdata);
903 MSG_WriteShort (msg, bits);
904 if (bits & SU_EXTEND1)
905 MSG_WriteByte(msg, bits >> 16);
906 if (bits & SU_EXTEND2)
907 MSG_WriteByte(msg, bits >> 24);
909 if (bits & SU_VIEWHEIGHT)
910 MSG_WriteChar (msg, ent->v.view_ofs[2]);
912 if (bits & SU_IDEALPITCH)
913 MSG_WriteChar (msg, ent->v.idealpitch);
915 for (i=0 ; i<3 ; i++)
917 if (bits & (SU_PUNCH1<<i))
918 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
919 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
920 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
921 if (bits & (SU_VELOCITY1<<i))
922 MSG_WriteChar (msg, ent->v.velocity[i]/16);
925 // [always sent] if (bits & SU_ITEMS)
926 MSG_WriteLong (msg, items);
928 if (bits & SU_WEAPONFRAME)
929 MSG_WriteByte (msg, ent->v.weaponframe);
931 MSG_WriteByte (msg, ent->v.armorvalue);
932 if (bits & SU_WEAPON)
933 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
935 MSG_WriteShort (msg, ent->v.health);
936 MSG_WriteByte (msg, ent->v.currentammo);
937 MSG_WriteByte (msg, ent->v.ammo_shells);
938 MSG_WriteByte (msg, ent->v.ammo_nails);
939 MSG_WriteByte (msg, ent->v.ammo_rockets);
940 MSG_WriteByte (msg, ent->v.ammo_cells);
944 MSG_WriteByte (msg, ent->v.weapon);
950 if ( ((int)ent->v.weapon) & (1<<i) )
952 MSG_WriteByte (msg, i);
960 =======================
961 SV_SendClientDatagram
962 =======================
964 qboolean SV_SendClientDatagram (client_t *client)
966 byte buf[MAX_DATAGRAM];
970 msg.maxsize = sizeof(buf);
973 MSG_WriteByte (&msg, svc_time);
974 MSG_WriteFloat (&msg, sv.time);
976 // add the client specific data to the datagram
977 SV_WriteClientdataToMessage (client->edict, &msg);
979 SV_WriteEntitiesToClient (client, client->edict, &msg);
981 // copy the server datagram if there is space
982 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
983 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
986 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
988 SV_DropClient (true);// if the message couldn't send, kick off
996 =======================
997 SV_UpdateToReliableMessages
998 =======================
1000 void SV_UpdateToReliableMessages (void)
1005 // check for changes to be sent over the reliable streams
1006 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1008 if (host_client->old_frags != host_client->edict->v.frags)
1010 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1012 if (!client->active)
1014 MSG_WriteByte (&client->message, svc_updatefrags);
1015 MSG_WriteByte (&client->message, i);
1016 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1019 host_client->old_frags = host_client->edict->v.frags;
1023 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1025 if (!client->active)
1027 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1030 SZ_Clear (&sv.reliable_datagram);
1035 =======================
1038 Send a nop message without trashing or sending the accumulated client
1040 =======================
1042 void SV_SendNop (client_t *client)
1048 msg.maxsize = sizeof(buf);
1051 MSG_WriteChar (&msg, svc_nop);
1053 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1054 SV_DropClient (true); // if the message couldn't send, kick off
1055 client->last_message = realtime;
1059 =======================
1060 SV_SendClientMessages
1061 =======================
1063 void SV_SendClientMessages (void)
1067 // update frags, names, etc
1068 SV_UpdateToReliableMessages ();
1070 // build individual updates
1071 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1073 if (!host_client->active)
1076 if (host_client->spawned)
1078 if (!SV_SendClientDatagram (host_client))
1083 // the player isn't totally in the game yet
1084 // send small keepalive messages if too much time has passed
1085 // send a full message when the next signon stage has been requested
1086 // some other message data (name changes, etc) may accumulate
1087 // between signon stages
1088 if (!host_client->sendsignon)
1090 if (realtime - host_client->last_message > 5)
1091 SV_SendNop (host_client);
1092 continue; // don't send out non-signon messages
1096 // check for an overflowed message. Should only happen
1097 // on a very fucked up connection that backs up a lot, then
1099 if (host_client->message.overflowed)
1101 SV_DropClient (true);
1102 host_client->message.overflowed = false;
1106 if (host_client->message.cursize || host_client->dropasap)
1108 if (!NET_CanSendMessage (host_client->netconnection))
1110 // I_Printf ("can't write\n");
1114 if (host_client->dropasap)
1115 SV_DropClient (false); // went to another level
1118 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1119 SV_DropClient (true); // if the message couldn't send, kick off
1120 SZ_Clear (&host_client->message);
1121 host_client->last_message = realtime;
1122 host_client->sendsignon = false;
1128 // clear muzzle flashes
1134 ==============================================================================
1138 ==============================================================================
1147 int SV_ModelIndex (char *name)
1151 if (!name || !name[0])
1154 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1155 if (!strcmp(sv.model_precache[i], name))
1157 if (i==MAX_MODELS || !sv.model_precache[i])
1158 Host_Error ("SV_ModelIndex: model %s not precached", name);
1168 void SV_CreateBaseline (void)
1170 int i, entnum, large;
1173 // LordHavoc: clear *all* states (note just active ones)
1174 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1176 // get the current server version
1177 svent = EDICT_NUM(entnum);
1179 // LordHavoc: always clear state values, whether the entity is in use or not
1180 ClearStateToDefault(&svent->baseline);
1184 if (entnum > svs.maxclients && !svent->v.modelindex)
1188 // create entity baseline
1190 VectorCopy (svent->v.origin, svent->baseline.origin);
1191 VectorCopy (svent->v.angles, svent->baseline.angles);
1192 svent->baseline.frame = svent->v.frame;
1193 svent->baseline.skin = svent->v.skin;
1194 if (entnum > 0 && entnum <= svs.maxclients)
1196 svent->baseline.colormap = entnum;
1197 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1201 svent->baseline.colormap = 0;
1202 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1206 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1209 // add to the message
1212 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1214 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1215 MSG_WriteShort (&sv.signon, entnum);
1219 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1220 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1224 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1225 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1227 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1228 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1229 for (i=0 ; i<3 ; i++)
1231 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1232 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1242 Tell all the clients that the server is changing levels
1245 void SV_SendReconnect (void)
1252 msg.maxsize = sizeof(data);
1254 MSG_WriteChar (&msg, svc_stufftext);
1255 MSG_WriteString (&msg, "reconnect\n");
1256 NET_SendToAll (&msg, 5);
1258 if (cls.state != ca_dedicated)
1259 Cmd_ExecuteString ("reconnect\n", src_command);
1267 Grabs the current state of each client for saving across the
1268 transition to another level
1271 void SV_SaveSpawnparms (void)
1275 svs.serverflags = pr_global_struct->serverflags;
1277 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1279 if (!host_client->active)
1282 // call the progs to get default spawn parms for the new client
1283 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1284 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1285 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1286 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1290 qboolean isworldmodel;
1296 This is called at the start of each level
1299 extern float scr_centertime_off;
1301 void SV_SpawnServer (char *server)
1306 // let's not have any servers with no name
1307 if (hostname.string[0] == 0)
1308 Cvar_Set ("hostname", "UNNAMED");
1309 scr_centertime_off = 0;
1311 Con_DPrintf ("SpawnServer: %s\n",server);
1312 svs.changelevel_issued = false; // now safe to issue another
1315 // tell all connected clients that we are going to a new level
1319 SV_SendReconnect ();
1323 // make cvars consistant
1326 Cvar_SetValue ("deathmatch", 0);
1327 current_skill = (int)(skill.value + 0.5);
1328 if (current_skill < 0)
1330 if (current_skill > 3)
1333 Cvar_SetValue ("skill", (float)current_skill);
1336 // set up the new server
1338 Host_ClearMemory ();
1340 memset (&sv, 0, sizeof(sv));
1342 strcpy (sv.name, server);
1344 // load progs to get entity field count
1347 // allocate server memory
1348 sv.max_edicts = MAX_EDICTS;
1350 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1352 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1353 sv.datagram.cursize = 0;
1354 sv.datagram.data = sv.datagram_buf;
1356 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1357 sv.reliable_datagram.cursize = 0;
1358 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1360 sv.signon.maxsize = sizeof(sv.signon_buf);
1361 sv.signon.cursize = 0;
1362 sv.signon.data = sv.signon_buf;
1364 // leave slots at start for clients only
1365 sv.num_edicts = svs.maxclients+1;
1366 for (i=0 ; i<svs.maxclients ; i++)
1368 ent = EDICT_NUM(i+1);
1369 svs.clients[i].edict = ent;
1372 sv.state = ss_loading;
1377 strcpy (sv.name, server);
1378 sprintf (sv.modelname,"maps/%s.bsp", server);
1379 isworldmodel = true; // LordHavoc: only load submodels on the world model
1380 sv.worldmodel = Mod_ForName (sv.modelname, false);
1381 isworldmodel = false;
1384 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1388 sv.models[1] = sv.worldmodel;
1391 // clear world interaction links
1395 sv.sound_precache[0] = pr_strings;
1397 sv.model_precache[0] = pr_strings;
1398 sv.model_precache[1] = sv.modelname;
1399 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1401 sv.model_precache[1+i] = localmodels[i];
1402 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1406 // load the rest of the entities
1409 memset (&ent->v, 0, progs->entityfields * 4);
1411 ent->v.model = sv.worldmodel->name - pr_strings;
1412 ent->v.modelindex = 1; // world model
1413 ent->v.solid = SOLID_BSP;
1414 ent->v.movetype = MOVETYPE_PUSH;
1417 pr_global_struct->coop = coop.value;
1419 pr_global_struct->deathmatch = deathmatch.value;
1421 pr_global_struct->mapname = sv.name - pr_strings;
1423 // serverflags are for cross level information (sigils)
1424 pr_global_struct->serverflags = svs.serverflags;
1426 ED_LoadFromFile (sv.worldmodel->entities);
1427 // LordHavoc: clear world angles (to fix e3m3.bsp)
1428 VectorClear(sv.edicts->v.angles);
1432 // all setup is completed, any further precache statements are errors
1433 sv.state = ss_active;
1435 // run two frames to allow everything to settle
1436 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1440 // create a baseline for more efficient communications
1441 SV_CreateBaseline ();
1443 // send serverinfo to all connected clients
1444 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1445 if (host_client->active)
1446 SV_SendServerinfo (host_client);
1448 Con_DPrintf ("Server spawned.\n");