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
27 char localmodels[MAX_MODELS][5]; // inline model names for precache
29 //============================================================================
39 extern cvar_t sv_maxvelocity;
40 extern cvar_t sv_gravity;
41 extern cvar_t sv_nostep;
42 extern cvar_t sv_friction;
43 extern cvar_t sv_edgefriction;
44 extern cvar_t sv_stopspeed;
45 extern cvar_t sv_maxspeed;
46 extern cvar_t sv_accelerate;
47 extern cvar_t sv_idealpitchscale;
49 extern cvar_t sv_predict;
51 Cvar_RegisterVariable (&sv_maxvelocity);
52 Cvar_RegisterVariable (&sv_gravity);
53 Cvar_RegisterVariable (&sv_friction);
54 Cvar_RegisterVariable (&sv_edgefriction);
55 Cvar_RegisterVariable (&sv_stopspeed);
56 Cvar_RegisterVariable (&sv_maxspeed);
57 Cvar_RegisterVariable (&sv_accelerate);
58 Cvar_RegisterVariable (&sv_idealpitchscale);
59 Cvar_RegisterVariable (&sv_aim);
60 Cvar_RegisterVariable (&sv_nostep);
61 Cvar_RegisterVariable (&sv_predict);
63 for (i=0 ; i<MAX_MODELS ; i++)
64 sprintf (localmodels[i], "*%i", i);
68 =============================================================================
72 =============================================================================
79 Make sure the event gets sent to all clients
82 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
86 if (sv.datagram.cursize > MAX_DATAGRAM-16)
88 MSG_WriteByte (&sv.datagram, svc_particle);
89 MSG_WriteFloatCoord (&sv.datagram, org[0]);
90 MSG_WriteFloatCoord (&sv.datagram, org[1]);
91 MSG_WriteFloatCoord (&sv.datagram, org[2]);
99 MSG_WriteChar (&sv.datagram, v);
101 MSG_WriteByte (&sv.datagram, count);
102 MSG_WriteByte (&sv.datagram, color);
109 Make sure the event gets sent to all clients
112 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
114 if (sv.datagram.cursize > MAX_DATAGRAM-18)
116 if (modelindex >= 256)
118 MSG_WriteByte (&sv.datagram, svc_effect2);
119 MSG_WriteFloatCoord (&sv.datagram, org[0]);
120 MSG_WriteFloatCoord (&sv.datagram, org[1]);
121 MSG_WriteFloatCoord (&sv.datagram, org[2]);
122 MSG_WriteShort (&sv.datagram, modelindex);
123 MSG_WriteByte (&sv.datagram, startframe);
124 MSG_WriteByte (&sv.datagram, framecount);
125 MSG_WriteByte (&sv.datagram, framerate);
129 MSG_WriteByte (&sv.datagram, svc_effect);
130 MSG_WriteFloatCoord (&sv.datagram, org[0]);
131 MSG_WriteFloatCoord (&sv.datagram, org[1]);
132 MSG_WriteFloatCoord (&sv.datagram, org[2]);
133 MSG_WriteByte (&sv.datagram, modelindex);
134 MSG_WriteByte (&sv.datagram, startframe);
135 MSG_WriteByte (&sv.datagram, framecount);
136 MSG_WriteByte (&sv.datagram, framerate);
144 Each entity can have eight independant sound sources, like voice,
147 Channel 0 is an auto-allocate channel, the others override anything
148 already running on that entity/channel pair.
150 An attenuation of 0 will play full volume everywhere in the level.
151 Larger attenuations will drop off. (max 4 attenuation)
155 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
163 if (volume < 0 || volume > 255)
164 Host_Error ("SV_StartSound: volume = %i", volume);
166 if (attenuation < 0 || attenuation > 4)
167 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
169 if (channel < 0 || channel > 7)
170 Host_Error ("SV_StartSound: channel = %i", channel);
172 if (sv.datagram.cursize > MAX_DATAGRAM-16)
175 // find precache number for sound
176 for (sound_num=1 ; sound_num<MAX_SOUNDS
177 && sv.sound_precache[sound_num] ; sound_num++)
178 if (!strcmp(sample, sv.sound_precache[sound_num]))
181 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
183 Con_Printf ("SV_StartSound: %s not precached\n", sample);
187 ent = NUM_FOR_EDICT(entity);
189 channel = (ent<<3) | channel;
192 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
193 field_mask |= SND_VOLUME;
194 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
195 field_mask |= SND_ATTENUATION;
197 // directed messages go only to the entity they are targeted on
198 if (sound_num >= 256)
199 MSG_WriteByte (&sv.datagram, svc_sound2);
201 MSG_WriteByte (&sv.datagram, svc_sound);
202 MSG_WriteByte (&sv.datagram, field_mask);
203 if (field_mask & SND_VOLUME)
204 MSG_WriteByte (&sv.datagram, volume);
205 if (field_mask & SND_ATTENUATION)
206 MSG_WriteByte (&sv.datagram, attenuation*64);
207 MSG_WriteShort (&sv.datagram, channel);
208 if (sound_num >= 256)
209 MSG_WriteShort (&sv.datagram, sound_num);
211 MSG_WriteByte (&sv.datagram, sound_num);
212 for (i=0 ; i<3 ; i++)
213 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
217 ==============================================================================
221 ==============================================================================
228 Sends the first message from the server to a connected client.
229 This will be sent on the initial connection and upon each server load.
232 void SV_SendServerinfo (client_t *client)
237 MSG_WriteByte (&client->message, svc_print);
238 sprintf (message, "%c\nDARKPLACES VERSION %4.2f BUILD %i SERVER (%i CRC)", 2, VERSION, buildnumber, pr_crc);
239 MSG_WriteString (&client->message,message);
241 MSG_WriteByte (&client->message, svc_serverinfo);
242 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
243 MSG_WriteByte (&client->message, svs.maxclients);
245 if (!coop.value && deathmatch.value)
246 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
248 MSG_WriteByte (&client->message, GAME_COOP);
250 sprintf (message, pr_strings+sv.edicts->v.message);
252 MSG_WriteString (&client->message,message);
254 for (s = sv.model_precache+1 ; *s ; s++)
255 MSG_WriteString (&client->message, *s);
256 MSG_WriteByte (&client->message, 0);
258 for (s = sv.sound_precache+1 ; *s ; s++)
259 MSG_WriteString (&client->message, *s);
260 MSG_WriteByte (&client->message, 0);
263 MSG_WriteByte (&client->message, svc_cdtrack);
264 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
265 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
268 MSG_WriteByte (&client->message, svc_setview);
269 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
271 MSG_WriteByte (&client->message, svc_signonnum);
272 MSG_WriteByte (&client->message, 1);
274 client->sendsignon = true;
275 client->spawned = false; // need prespawn, spawn, etc
282 Initializes a client_t for a new net connection. This will only be called
283 once for a player each game, not once for each level change.
286 void SV_ConnectClient (int clientnum)
291 struct qsocket_s *netconnection;
293 float spawn_parms[NUM_SPAWN_PARMS];
295 client = svs.clients + clientnum;
297 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
299 edictnum = clientnum+1;
301 ent = EDICT_NUM(edictnum);
303 // set up the client_t
304 netconnection = client->netconnection;
307 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
308 memset (client, 0, sizeof(*client));
309 client->netconnection = netconnection;
311 strcpy (client->name, "unconnected");
312 client->active = true;
313 client->spawned = false;
315 client->message.data = client->msgbuf;
316 client->message.maxsize = sizeof(client->msgbuf);
317 client->message.allowoverflow = true; // we can catch it
320 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
323 // call the progs to get default spawn parms for the new client
324 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
325 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
326 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
329 SV_SendServerinfo (client);
335 SV_CheckForNewClients
339 void SV_CheckForNewClients (void)
341 struct qsocket_s *ret;
345 // check for new connections
349 ret = NET_CheckNewConnections ();
354 // init a new client structure
356 for (i=0 ; i<svs.maxclients ; i++)
357 if (!svs.clients[i].active)
359 if (i == svs.maxclients)
360 Sys_Error ("Host_CheckForNewClients: no free clients");
362 svs.clients[i].netconnection = ret;
363 SV_ConnectClient (i);
365 net_activeconnections++;
372 ===============================================================================
376 ===============================================================================
385 void SV_ClearDatagram (void)
387 SZ_Clear (&sv.datagram);
391 =============================================================================
393 The PVS must include a small area around the client to allow head bobbing
394 or other small motion on the client side. Otherwise, a bob might cause an
395 entity that should be visible to not show up, especially when the bob
398 =============================================================================
402 byte fatpvs[MAX_MAP_LEAFS/8];
404 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
413 // if this is a leaf, accumulate the pvs bits
414 if (node->contents < 0)
416 if (node->contents != CONTENTS_SOLID)
418 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
419 for (i=0 ; i<fatbytes ; i++)
426 d = DotProduct (org, plane->normal) - plane->dist;
428 node = node->children[0];
430 node = node->children[1];
433 SV_AddToFatPVS (org, node->children[0]);
434 node = node->children[1];
443 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
447 byte *SV_FatPVS (vec3_t org)
449 fatbytes = (sv.worldmodel->numleafs+31)>>3;
450 memset (fatpvs, 0, fatbytes);
451 SV_AddToFatPVS (org, sv.worldmodel->nodes);
455 //=============================================================================
460 SV_WriteEntitiesToClient
464 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
466 int e, i, clentnum, bits, alpha, glowcolor, glowsize, scale, colormod, modred, modgreen, modblue, dodelta, effects;
468 vec3_t org, origin, angles;
469 float movelerp, moveilerp;
472 entity_state_t *baseline; // LordHavoc: delta or startup baseline
474 // find the client's PVS
475 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
476 pvs = SV_FatPVS (org);
480 MSG_WriteByte(msg, svc_playerposition);
481 MSG_WriteFloat(msg, org[0]);
482 MSG_WriteFloat(msg, org[1]);
483 MSG_WriteFloat(msg, org[2]);
486 MSG_WriteByte(msg, svc_entitiesbegin);
487 MSG_WriteShort(msg, 1);
489 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
490 // send over all entities (except the client) that touch the pvs
491 ent = NEXT_EDICT(sv.edicts);
492 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
495 if (ent != clent) // LordHavoc: always send player
497 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
499 if (val->edict != clentnum)
500 continue; // don't show to anyone else
502 bits |= U_VIEWMODEL; // show relative to the view
506 // LordHavoc: never draw something told not to display to this client
507 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
509 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
511 // ignore if not touching a PV leaf
512 for (i=0 ; i < ent->num_leafs ; i++)
513 if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
516 if (i == ent->num_leafs)
517 continue; // not visible
521 // don't send if flagged for NODRAW and there are no effects
527 effects = ent->v.effects;
529 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
530 if ((alpha = (int) (val->_float * 255.0)) == 0)
532 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)) && val->_float != 0) // HalfLife support
533 alpha = (int) val->_float;
534 if (alpha < 0) alpha = 0;
535 if (alpha > 255) alpha = 255;
537 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
538 glowsize = (int) val->_float >> 2;
539 if (glowsize > 255) glowsize = 255;
540 if (glowsize < 0) glowsize = 0;
542 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
543 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
544 if (scale < 0) scale = 0;
545 if (scale > 255) scale = 255;
547 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
548 if (val->_float != 0)
551 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
552 if (val->_float != 0)
553 glowcolor = (int) val->_float;
555 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
556 if (val->_float != 0)
557 effects |= EF_FULLBRIGHT;
559 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
560 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
562 modred = val->vector[0] * 8.0;if (modred < 0) modred = 0;if (modred > 7) modred = 7;
563 modgreen = val->vector[1] * 8.0;if (modgreen < 0) modgreen = 0;if (modgreen > 7) modgreen = 7;
564 modblue = val->vector[2] * 4.0;if (modblue < 0) modblue = 0;if (modblue > 3) modblue = 3;
565 colormod = (modred << 5) | (modgreen << 2) | modblue;
570 if (glowsize == 0 && bits == 0) // no effects
572 if (ent->v.modelindex && pr_strings[ent->v.model]) // model
574 if (sv.models[ (int)ent->v.modelindex ]->flags == 0 && (ent->v.effects == EF_NODRAW || scale <= 0 || alpha <= 0))
577 else // no model and no effects
582 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
584 Con_Printf ("packet overflow\n");
592 if ((int)ent->v.effects & EF_DELTA)
593 dodelta = realtime < client->nextfullupdate[e]; // every half second a full update is forced
598 baseline = &ent->deltabaseline;
602 client->nextfullupdate[e] = realtime + 0.5;
603 baseline = &ent->baseline;
607 bits |= U_LONGENTITY;
608 if (ent->v.movetype == MOVETYPE_STEP)
611 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
613 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
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);
621 VectorSubtract(ent->v.origin, ent->steporigin, origin);
622 VectorSubtract(ent->v.angles, ent->stepangles, angles);
623 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
625 // update lerp positions
626 ent->steplerptime = sv.time;
627 VectorCopy(ent->steporigin, ent->stepoldorigin);
628 VectorCopy(ent->stepangles, ent->stepoldangles);
629 VectorCopy(ent->v.origin, ent->steporigin);
630 VectorCopy(ent->v.angles, ent->stepangles);
632 movelerp = (sv.time - ent->steplerptime) * 10.0;
633 if (movelerp > 1) movelerp = 1;
634 moveilerp = 1 - movelerp;
635 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
636 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
637 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
638 // choose shortest rotate (to avoid 'spin around' situations)
639 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
640 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
641 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
642 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
643 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
644 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
645 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
646 VectorMA(origin, host_client->latency, ent->v.velocity, origin);
648 else // copy as they are
650 // VectorCopy(ent->v.origin, origin);
651 VectorCopy(ent->v.angles, angles);
652 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
653 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
655 // update lerp positions
656 ent->steplerptime = sv.time;
657 VectorCopy(ent->v.origin, ent->stepoldorigin);
658 VectorCopy(ent->v.angles, ent->stepoldangles);
659 VectorCopy(ent->v.origin, ent->steporigin);
660 VectorCopy(ent->v.angles, ent->stepangles);
664 // LordHavoc: old stuff, but rewritten to have more exact tolerances
665 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
666 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
667 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
668 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
669 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
670 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
671 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
672 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
673 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
674 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
675 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
676 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
677 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
678 if (baseline->modelindex != (byte) ent->v.modelindex) bits |= U_MODEL;
680 // LordHavoc: new stuff
681 if (baseline->alpha != alpha) bits |= U_ALPHA;
682 if (baseline->scale != scale) bits |= U_SCALE;
683 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
684 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
685 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
686 if (baseline->colormod != colormod) bits |= U_COLORMOD;
687 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
688 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
690 // update delta baseline
691 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
692 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
693 ent->deltabaseline.colormap = ent->v.colormap;
694 ent->deltabaseline.skin = ent->v.skin;
695 ent->deltabaseline.frame = ent->v.frame;
696 ent->deltabaseline.effects = ent->v.effects;
697 ent->deltabaseline.modelindex = ent->v.modelindex;
698 ent->deltabaseline.alpha = alpha;
699 ent->deltabaseline.scale = scale;
700 ent->deltabaseline.glowsize = glowsize;
701 ent->deltabaseline.glowcolor = glowcolor;
702 ent->deltabaseline.colormod = colormod;
705 if (bits >= 16777216)
713 MSG_WriteByte (msg, bits);
714 if (bits & U_MOREBITS)
715 MSG_WriteByte (msg, bits>>8);
716 // LordHavoc: extend bytes have to be written here due to delta compression
717 if (bits & U_EXTEND1)
718 MSG_WriteByte (msg, bits>>16);
719 if (bits & U_EXTEND2)
720 MSG_WriteByte (msg, bits>>24);
722 // LordHavoc: old stuff
723 if (bits & U_LONGENTITY)
724 MSG_WriteShort (msg,e);
726 MSG_WriteByte (msg,e);
727 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
728 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
729 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
730 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
731 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
732 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
733 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
734 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
735 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
736 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
737 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
739 // LordHavoc: new stuff
740 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
741 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
742 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
743 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
744 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
745 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
746 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
747 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
750 MSG_WriteByte(msg, svc_entitiesend);
751 MSG_WriteShort(msg, MAX_EDICTS);
760 void SV_CleanupEnts (void)
765 ent = NEXT_EDICT(sv.edicts);
766 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
768 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
775 SV_WriteClientdataToMessage
779 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
789 // send a damage message
791 if (ent->v.dmg_take || ent->v.dmg_save)
793 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
794 MSG_WriteByte (msg, svc_damage);
795 MSG_WriteByte (msg, ent->v.dmg_save);
796 MSG_WriteByte (msg, ent->v.dmg_take);
797 for (i=0 ; i<3 ; i++)
798 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
805 // send the current viewpos offset from the view entity
807 SV_SetIdealPitch (); // how much to look up / down ideally
809 // a fixangle might get lost in a dropped packet. Oh well.
810 if ( ent->v.fixangle )
812 MSG_WriteByte (msg, svc_setangle);
813 for (i=0 ; i < 3 ; i++)
814 MSG_WriteAngle (msg, ent->v.angles[i] );
820 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
821 bits |= SU_VIEWHEIGHT;
823 if (ent->v.idealpitch)
824 bits |= SU_IDEALPITCH;
826 // stuff the sigil bits into the high bits of items for sbar, or else
828 val = GETEDICTFIELDVALUE(ent, eval_items2);
831 items = (int)ent->v.items | ((int)val->_float << 23);
833 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
837 if ( (int)ent->v.flags & FL_ONGROUND)
840 if ( ent->v.waterlevel >= 2)
844 VectorClear(punchvector);
845 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
846 VectorCopy(val->vector, punchvector);
848 for (i=0 ; i<3 ; i++)
850 if (ent->v.punchangle[i])
851 bits |= (SU_PUNCH1<<i);
852 if (punchvector[i]) // dpprotocol
853 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
854 if (ent->v.velocity[i])
855 bits |= (SU_VELOCITY1<<i);
858 if (ent->v.weaponframe)
859 bits |= SU_WEAPONFRAME;
861 if (ent->v.armorvalue)
864 // if (ent->v.weapon)
869 if (bits >= 16777216)
874 MSG_WriteByte (msg, svc_clientdata);
875 MSG_WriteShort (msg, bits);
876 if (bits & SU_EXTEND1)
877 MSG_WriteByte(msg, bits >> 16);
878 if (bits & SU_EXTEND2)
879 MSG_WriteByte(msg, bits >> 24);
881 if (bits & SU_VIEWHEIGHT)
882 MSG_WriteChar (msg, ent->v.view_ofs[2]);
884 if (bits & SU_IDEALPITCH)
885 MSG_WriteChar (msg, ent->v.idealpitch);
887 for (i=0 ; i<3 ; i++)
889 if (bits & (SU_PUNCH1<<i))
890 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
891 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
892 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
893 if (bits & (SU_VELOCITY1<<i))
894 MSG_WriteChar (msg, ent->v.velocity[i]/16);
897 // [always sent] if (bits & SU_ITEMS)
898 MSG_WriteLong (msg, items);
900 if (bits & SU_WEAPONFRAME)
901 MSG_WriteByte (msg, ent->v.weaponframe);
903 MSG_WriteByte (msg, ent->v.armorvalue);
904 if (bits & SU_WEAPON)
905 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
907 MSG_WriteShort (msg, ent->v.health);
908 MSG_WriteByte (msg, ent->v.currentammo);
909 MSG_WriteByte (msg, ent->v.ammo_shells);
910 MSG_WriteByte (msg, ent->v.ammo_nails);
911 MSG_WriteByte (msg, ent->v.ammo_rockets);
912 MSG_WriteByte (msg, ent->v.ammo_cells);
916 MSG_WriteByte (msg, ent->v.weapon);
922 if ( ((int)ent->v.weapon) & (1<<i) )
924 MSG_WriteByte (msg, i);
932 =======================
933 SV_SendClientDatagram
934 =======================
936 qboolean SV_SendClientDatagram (client_t *client)
938 byte buf[MAX_DATAGRAM];
942 msg.maxsize = sizeof(buf);
945 MSG_WriteByte (&msg, svc_time);
946 MSG_WriteFloat (&msg, sv.time);
948 // add the client specific data to the datagram
949 SV_WriteClientdataToMessage (client->edict, &msg);
951 SV_WriteEntitiesToClient (client, client->edict, &msg);
953 // copy the server datagram if there is space
954 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
955 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
958 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
960 SV_DropClient (true);// if the message couldn't send, kick off
968 =======================
969 SV_UpdateToReliableMessages
970 =======================
972 void SV_UpdateToReliableMessages (void)
977 // check for changes to be sent over the reliable streams
978 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
980 if (host_client->old_frags != host_client->edict->v.frags)
982 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
986 MSG_WriteByte (&client->message, svc_updatefrags);
987 MSG_WriteByte (&client->message, i);
988 MSG_WriteShort (&client->message, host_client->edict->v.frags);
991 host_client->old_frags = host_client->edict->v.frags;
995 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
999 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1002 SZ_Clear (&sv.reliable_datagram);
1007 =======================
1010 Send a nop message without trashing or sending the accumulated client
1012 =======================
1014 void SV_SendNop (client_t *client)
1020 msg.maxsize = sizeof(buf);
1023 MSG_WriteChar (&msg, svc_nop);
1025 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1026 SV_DropClient (true); // if the message couldn't send, kick off
1027 client->last_message = realtime;
1031 =======================
1032 SV_SendClientMessages
1033 =======================
1035 void SV_SendClientMessages (void)
1039 // update frags, names, etc
1040 SV_UpdateToReliableMessages ();
1042 // build individual updates
1043 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1045 if (!host_client->active)
1048 if (host_client->spawned)
1050 if (!SV_SendClientDatagram (host_client))
1055 // the player isn't totally in the game yet
1056 // send small keepalive messages if too much time has passed
1057 // send a full message when the next signon stage has been requested
1058 // some other message data (name changes, etc) may accumulate
1059 // between signon stages
1060 if (!host_client->sendsignon)
1062 if (realtime - host_client->last_message > 5)
1063 SV_SendNop (host_client);
1064 continue; // don't send out non-signon messages
1068 // check for an overflowed message. Should only happen
1069 // on a very fucked up connection that backs up a lot, then
1071 if (host_client->message.overflowed)
1073 SV_DropClient (true);
1074 host_client->message.overflowed = false;
1078 if (host_client->message.cursize || host_client->dropasap)
1080 if (!NET_CanSendMessage (host_client->netconnection))
1082 // I_Printf ("can't write\n");
1086 if (host_client->dropasap)
1087 SV_DropClient (false); // went to another level
1090 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1091 SV_DropClient (true); // if the message couldn't send, kick off
1092 SZ_Clear (&host_client->message);
1093 host_client->last_message = realtime;
1094 host_client->sendsignon = false;
1100 // clear muzzle flashes
1106 ==============================================================================
1110 ==============================================================================
1119 int SV_ModelIndex (char *name)
1123 if (!name || !name[0])
1126 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1127 if (!strcmp(sv.model_precache[i], name))
1129 if (i==MAX_MODELS || !sv.model_precache[i])
1130 Host_Error ("SV_ModelIndex: model %s not precached", name);
1140 void SV_CreateBaseline (void)
1146 for (entnum = 0; entnum < sv.num_edicts ; entnum++)
1148 // get the current server version
1149 svent = EDICT_NUM(entnum);
1152 if (entnum > svs.maxclients && !svent->v.modelindex)
1156 // create entity baseline
1158 VectorCopy (svent->v.origin, svent->baseline.origin);
1159 VectorCopy (svent->v.angles, svent->baseline.angles);
1160 svent->baseline.frame = svent->v.frame;
1161 svent->baseline.skin = svent->v.skin;
1162 if (entnum > 0 && entnum <= svs.maxclients)
1164 svent->baseline.colormap = entnum;
1165 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1169 svent->baseline.colormap = 0;
1170 svent->baseline.modelindex =
1171 SV_ModelIndex(pr_strings + svent->v.model);
1173 svent->baseline.alpha = 255;
1174 svent->baseline.scale = 16;
1175 svent->baseline.glowsize = 0;
1176 svent->baseline.glowcolor = 254;
1177 svent->baseline.colormod = 255;
1180 // add to the message
1182 MSG_WriteByte (&sv.signon,svc_spawnbaseline);
1183 MSG_WriteShort (&sv.signon,entnum);
1185 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1186 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1187 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1188 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1189 for (i=0 ; i<3 ; i++)
1191 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1192 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1202 Tell all the clients that the server is changing levels
1205 void SV_SendReconnect (void)
1212 msg.maxsize = sizeof(data);
1214 MSG_WriteChar (&msg, svc_stufftext);
1215 MSG_WriteString (&msg, "reconnect\n");
1216 NET_SendToAll (&msg, 5);
1218 if (cls.state != ca_dedicated)
1219 Cmd_ExecuteString ("reconnect\n", src_command);
1227 Grabs the current state of each client for saving across the
1228 transition to another level
1231 void SV_SaveSpawnparms (void)
1235 svs.serverflags = pr_global_struct->serverflags;
1237 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1239 if (!host_client->active)
1242 // call the progs to get default spawn parms for the new client
1243 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1244 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1245 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1246 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1250 qboolean isworldmodel;
1256 This is called at the start of each level
1259 extern float scr_centertime_off;
1261 void SV_SpawnServer (char *server)
1266 // let's not have any servers with no name
1267 if (hostname.string[0] == 0)
1268 Cvar_Set ("hostname", "UNNAMED");
1269 scr_centertime_off = 0;
1271 Con_DPrintf ("SpawnServer: %s\n",server);
1272 svs.changelevel_issued = false; // now safe to issue another
1275 // tell all connected clients that we are going to a new level
1279 SV_SendReconnect ();
1283 // make cvars consistant
1286 Cvar_SetValue ("deathmatch", 0);
1287 current_skill = (int)(skill.value + 0.5);
1288 if (current_skill < 0)
1290 if (current_skill > 3)
1293 Cvar_SetValue ("skill", (float)current_skill);
1296 // set up the new server
1298 Host_ClearMemory ();
1300 memset (&sv, 0, sizeof(sv));
1302 strcpy (sv.name, server);
1304 // load progs to get entity field count
1307 // allocate server memory
1308 sv.max_edicts = MAX_EDICTS;
1310 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1312 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1313 sv.datagram.cursize = 0;
1314 sv.datagram.data = sv.datagram_buf;
1316 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1317 sv.reliable_datagram.cursize = 0;
1318 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1320 sv.signon.maxsize = sizeof(sv.signon_buf);
1321 sv.signon.cursize = 0;
1322 sv.signon.data = sv.signon_buf;
1324 // leave slots at start for clients only
1325 sv.num_edicts = svs.maxclients+1;
1326 for (i=0 ; i<svs.maxclients ; i++)
1328 ent = EDICT_NUM(i+1);
1329 svs.clients[i].edict = ent;
1332 sv.state = ss_loading;
1337 strcpy (sv.name, server);
1338 sprintf (sv.modelname,"maps/%s.bsp", server);
1339 isworldmodel = true; // LordHavoc: only load submodels on the world model
1340 sv.worldmodel = Mod_ForName (sv.modelname, false);
1341 isworldmodel = false;
1344 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1348 sv.models[1] = sv.worldmodel;
1351 // clear world interaction links
1355 sv.sound_precache[0] = pr_strings;
1357 sv.model_precache[0] = pr_strings;
1358 sv.model_precache[1] = sv.modelname;
1359 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1361 sv.model_precache[1+i] = localmodels[i];
1362 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1366 // load the rest of the entities
1369 memset (&ent->v, 0, progs->entityfields * 4);
1371 ent->v.model = sv.worldmodel->name - pr_strings;
1372 ent->v.modelindex = 1; // world model
1373 ent->v.solid = SOLID_BSP;
1374 ent->v.movetype = MOVETYPE_PUSH;
1375 ent->v.angles[0] = ent->v.angles[1] = ent->v.angles[2] = 0;
1378 pr_global_struct->coop = coop.value;
1380 pr_global_struct->deathmatch = deathmatch.value;
1382 pr_global_struct->mapname = sv.name - pr_strings;
1384 // serverflags are for cross level information (sigils)
1385 pr_global_struct->serverflags = svs.serverflags;
1387 ED_LoadFromFile (sv.worldmodel->entities);
1388 // LordHavoc: clear world angles (to fix e3m3.bsp)
1389 sv.edicts->v.angles[0] = sv.edicts->v.angles[1] = sv.edicts->v.angles[2] = 0;
1393 // all setup is completed, any further precache statements are errors
1394 sv.state = ss_active;
1396 // run two frames to allow everything to settle
1397 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1401 // create a baseline for more efficient communications
1402 SV_CreateBaseline ();
1404 // send serverinfo to all connected clients
1405 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1406 if (host_client->active)
1407 SV_SendServerinfo (host_client);
1409 Con_DPrintf ("Server spawned.\n");