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 //============================================================================
40 Cvar_RegisterVariable (&sv_maxvelocity);
41 Cvar_RegisterVariable (&sv_gravity);
42 Cvar_RegisterVariable (&sv_friction);
43 Cvar_RegisterVariable (&sv_edgefriction);
44 Cvar_RegisterVariable (&sv_stopspeed);
45 Cvar_RegisterVariable (&sv_maxspeed);
46 Cvar_RegisterVariable (&sv_accelerate);
47 Cvar_RegisterVariable (&sv_idealpitchscale);
48 Cvar_RegisterVariable (&sv_aim);
49 Cvar_RegisterVariable (&sv_nostep);
50 Cvar_RegisterVariable (&sv_predict);
51 Cvar_RegisterVariable (&sv_deltacompress);
53 for (i = 0;i < MAX_MODELS;i++)
54 sprintf (localmodels[i], "*%i", i);
58 =============================================================================
62 =============================================================================
69 Make sure the event gets sent to all clients
72 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
76 if (sv.datagram.cursize > MAX_DATAGRAM-16)
78 MSG_WriteByte (&sv.datagram, svc_particle);
79 MSG_WriteFloatCoord (&sv.datagram, org[0]);
80 MSG_WriteFloatCoord (&sv.datagram, org[1]);
81 MSG_WriteFloatCoord (&sv.datagram, org[2]);
89 MSG_WriteChar (&sv.datagram, v);
91 MSG_WriteByte (&sv.datagram, count);
92 MSG_WriteByte (&sv.datagram, color);
99 Make sure the event gets sent to all clients
102 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
104 if (sv.datagram.cursize > MAX_DATAGRAM-18)
106 if (modelindex >= 256 || startframe >= 256)
108 MSG_WriteByte (&sv.datagram, svc_effect2);
109 MSG_WriteFloatCoord (&sv.datagram, org[0]);
110 MSG_WriteFloatCoord (&sv.datagram, org[1]);
111 MSG_WriteFloatCoord (&sv.datagram, org[2]);
112 MSG_WriteShort (&sv.datagram, modelindex);
113 MSG_WriteShort (&sv.datagram, startframe);
114 MSG_WriteByte (&sv.datagram, framecount);
115 MSG_WriteByte (&sv.datagram, framerate);
119 MSG_WriteByte (&sv.datagram, svc_effect);
120 MSG_WriteFloatCoord (&sv.datagram, org[0]);
121 MSG_WriteFloatCoord (&sv.datagram, org[1]);
122 MSG_WriteFloatCoord (&sv.datagram, org[2]);
123 MSG_WriteByte (&sv.datagram, modelindex);
124 MSG_WriteByte (&sv.datagram, startframe);
125 MSG_WriteByte (&sv.datagram, framecount);
126 MSG_WriteByte (&sv.datagram, framerate);
134 Each entity can have eight independant sound sources, like voice,
137 Channel 0 is an auto-allocate channel, the others override anything
138 already running on that entity/channel pair.
140 An attenuation of 0 will play full volume everywhere in the level.
141 Larger attenuations will drop off. (max 4 attenuation)
145 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
153 if (volume < 0 || volume > 255)
154 Host_Error ("SV_StartSound: volume = %i", volume);
156 if (attenuation < 0 || attenuation > 4)
157 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
159 if (channel < 0 || channel > 7)
160 Host_Error ("SV_StartSound: channel = %i", channel);
162 if (sv.datagram.cursize > MAX_DATAGRAM-16)
165 // find precache number for sound
166 for (sound_num=1 ; sound_num<MAX_SOUNDS
167 && sv.sound_precache[sound_num] ; sound_num++)
168 if (!strcmp(sample, sv.sound_precache[sound_num]))
171 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
173 Con_Printf ("SV_StartSound: %s not precached\n", sample);
177 ent = NUM_FOR_EDICT(entity);
179 channel = (ent<<3) | channel;
182 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
183 field_mask |= SND_VOLUME;
184 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
185 field_mask |= SND_ATTENUATION;
187 // directed messages go only to the entity they are targeted on
188 if (sound_num >= 256)
189 MSG_WriteByte (&sv.datagram, svc_sound2);
191 MSG_WriteByte (&sv.datagram, svc_sound);
192 MSG_WriteByte (&sv.datagram, field_mask);
193 if (field_mask & SND_VOLUME)
194 MSG_WriteByte (&sv.datagram, volume);
195 if (field_mask & SND_ATTENUATION)
196 MSG_WriteByte (&sv.datagram, attenuation*64);
197 MSG_WriteShort (&sv.datagram, channel);
198 if (sound_num >= 256)
199 MSG_WriteShort (&sv.datagram, sound_num);
201 MSG_WriteByte (&sv.datagram, sound_num);
202 for (i=0 ; i<3 ; i++)
203 MSG_WriteFloatCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
207 ==============================================================================
211 ==============================================================================
218 Sends the first message from the server to a connected client.
219 This will be sent on the initial connection and upon each server load.
222 void SV_SendServerinfo (client_t *client)
227 MSG_WriteByte (&client->message, svc_print);
228 sprintf (message, "%c\nDARKPLACES VERSION %4.2f BUILD %i SERVER (%i CRC)", 2, VERSION, buildnumber, pr_crc);
229 MSG_WriteString (&client->message,message);
231 MSG_WriteByte (&client->message, svc_serverinfo);
232 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION);
233 MSG_WriteByte (&client->message, svs.maxclients);
235 if (!coop.value && deathmatch.value)
236 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
238 MSG_WriteByte (&client->message, GAME_COOP);
240 sprintf (message, pr_strings+sv.edicts->v.message);
242 MSG_WriteString (&client->message,message);
244 for (s = sv.model_precache+1 ; *s ; s++)
245 MSG_WriteString (&client->message, *s);
246 MSG_WriteByte (&client->message, 0);
248 for (s = sv.sound_precache+1 ; *s ; s++)
249 MSG_WriteString (&client->message, *s);
250 MSG_WriteByte (&client->message, 0);
253 MSG_WriteByte (&client->message, svc_cdtrack);
254 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
255 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
258 MSG_WriteByte (&client->message, svc_setview);
259 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
261 MSG_WriteByte (&client->message, svc_signonnum);
262 MSG_WriteByte (&client->message, 1);
264 client->sendsignon = true;
265 client->spawned = false; // need prespawn, spawn, etc
272 Initializes a client_t for a new net connection. This will only be called
273 once for a player each game, not once for each level change.
276 void SV_ConnectClient (int clientnum)
281 struct qsocket_s *netconnection;
283 float spawn_parms[NUM_SPAWN_PARMS];
285 client = svs.clients + clientnum;
287 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
289 edictnum = clientnum+1;
291 ent = EDICT_NUM(edictnum);
293 // set up the client_t
294 netconnection = client->netconnection;
297 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
298 memset (client, 0, sizeof(*client));
299 client->netconnection = netconnection;
301 strcpy (client->name, "unconnected");
302 client->active = true;
303 client->spawned = false;
305 client->message.data = client->msgbuf;
306 client->message.maxsize = sizeof(client->msgbuf);
307 client->message.allowoverflow = true; // we can catch it
310 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
313 // call the progs to get default spawn parms for the new client
314 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
315 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
316 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
319 SV_SendServerinfo (client);
325 SV_CheckForNewClients
329 void SV_CheckForNewClients (void)
331 struct qsocket_s *ret;
335 // check for new connections
339 ret = NET_CheckNewConnections ();
344 // init a new client structure
346 for (i=0 ; i<svs.maxclients ; i++)
347 if (!svs.clients[i].active)
349 if (i == svs.maxclients)
350 Sys_Error ("Host_CheckForNewClients: no free clients");
352 svs.clients[i].netconnection = ret;
353 SV_ConnectClient (i);
355 net_activeconnections++;
362 ===============================================================================
366 ===============================================================================
375 void SV_ClearDatagram (void)
377 SZ_Clear (&sv.datagram);
381 =============================================================================
383 The PVS must include a small area around the client to allow head bobbing
384 or other small motion on the client side. Otherwise, a bob might cause an
385 entity that should be visible to not show up, especially when the bob
388 =============================================================================
392 byte fatpvs[MAX_MAP_LEAFS/8];
394 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
403 // if this is a leaf, accumulate the pvs bits
404 if (node->contents < 0)
406 if (node->contents != CONTENTS_SOLID)
408 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
409 for (i=0 ; i<fatbytes ; i++)
416 d = DotProduct (org, plane->normal) - plane->dist;
418 node = node->children[0];
420 node = node->children[1];
423 SV_AddToFatPVS (org, node->children[0]);
424 node = node->children[1];
433 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
437 byte *SV_FatPVS (vec3_t org)
439 fatbytes = (sv.worldmodel->numleafs+31)>>3;
440 memset (fatpvs, 0, fatbytes);
441 SV_AddToFatPVS (org, sv.worldmodel->nodes);
445 //=============================================================================
450 SV_WriteEntitiesToClient
454 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
456 int e, i, clentnum, bits, alpha, glowcolor, glowsize, scale, colormod, effects;
458 vec3_t org, origin, angles;
459 float movelerp, moveilerp, nextfullupdate;
462 entity_state_t *baseline; // LordHavoc: delta or startup baseline
464 // find the client's PVS
465 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
466 pvs = SV_FatPVS (org);
470 MSG_WriteByte(msg, svc_playerposition);
471 MSG_WriteFloat(msg, org[0]);
472 MSG_WriteFloat(msg, org[1]);
473 MSG_WriteFloat(msg, org[2]);
477 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
478 // send over all entities (except the client) that touch the pvs
479 ent = NEXT_EDICT(sv.edicts);
480 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
484 // prevent delta compression against this frame (unless actually sent, which will restore this later)
485 nextfullupdate = client->nextfullupdate[e];
486 client->nextfullupdate[e] = -1;
488 if (ent != clent) // LordHavoc: always send player
490 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
492 if (val->edict != clentnum)
494 // don't show to anyone else
498 bits |= U_VIEWMODEL; // show relative to the view
502 // LordHavoc: never draw something told not to display to this client
503 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
505 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
507 // ignore if not touching a PV leaf
508 for (i = 0;i < ent->num_leafs;i++)
509 if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
512 if (i == ent->num_leafs)
520 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
521 bits = bits | U_EXTERIORMODEL;
523 // don't send if flagged for NODRAW and there are no effects
529 effects = ent->v.effects;
531 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
532 if (val->_float != 0)
533 alpha = (int) (val->_float * 255.0);
536 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
537 if (val->_float != 0)
538 alpha = (int) val->_float;
542 alpha = bound(0, alpha, 255);
544 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
545 glowsize = (int) val->_float >> 2;
546 if (glowsize > 255) glowsize = 255;
547 if (glowsize < 0) glowsize = 0;
549 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
550 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
551 if (scale < 0) scale = 0;
552 if (scale > 255) scale = 255;
554 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
555 if (val->_float != 0)
558 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
559 if (val->_float != 0)
560 glowcolor = (int) val->_float;
562 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
563 if (val->_float != 0)
564 effects |= EF_FULLBRIGHT;
566 if ((val = GETEDICTFIELDVALUE(ent, eval_colormod)))
567 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
568 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);
572 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
574 if (ent->v.modelindex && pr_strings[ent->v.model]) // model
576 if (sv.models[ (int)ent->v.modelindex ]->flags == 0 && (ent->v.effects == EF_NODRAW || scale <= 0 || alpha <= 0))
579 else // no model and no effects
584 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
586 Con_Printf ("packet overflow\n");
587 // mark the rest of the entities so they can't be delta compressed against this frame
588 for (;e < sv.num_edicts;e++)
589 client->nextfullupdate[e] = -1;
594 baseline = &ent->baseline;
596 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.value)
598 // every half second a full update is forced
599 if (realtime < client->nextfullupdate[e])
602 baseline = &ent->deltabaseline;
605 nextfullupdate = realtime + 0.5f;
608 nextfullupdate = realtime + 0.5f;
610 // restore nextfullupdate since this is being sent
611 client->nextfullupdate[e] = nextfullupdate;
614 bits |= U_LONGENTITY;
616 if (ent->v.movetype == MOVETYPE_STEP)
619 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
621 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
623 ent->steplerptime = sv.time;
624 VectorCopy(ent->v.origin, ent->stepoldorigin);
625 VectorCopy(ent->v.angles, ent->stepoldangles);
626 VectorCopy(ent->v.origin, ent->steporigin);
627 VectorCopy(ent->v.angles, ent->stepangles);
629 VectorSubtract(ent->v.origin, ent->steporigin, origin);
630 VectorSubtract(ent->v.angles, ent->stepangles, angles);
631 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
633 // update lerp positions
634 ent->steplerptime = sv.time;
635 VectorCopy(ent->steporigin, ent->stepoldorigin);
636 VectorCopy(ent->stepangles, ent->stepoldangles);
637 VectorCopy(ent->v.origin, ent->steporigin);
638 VectorCopy(ent->v.angles, ent->stepangles);
640 movelerp = (sv.time - ent->steplerptime) * 10.0;
641 if (movelerp > 1) movelerp = 1;
642 moveilerp = 1 - movelerp;
643 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
644 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
645 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
646 // choose shortest rotate (to avoid 'spin around' situations)
647 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
648 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
649 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
650 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
651 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
652 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
653 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
654 VectorMA(origin, host_client->latency, ent->v.velocity, origin);
656 else // copy as they are
658 // VectorCopy(ent->v.origin, origin);
659 VectorCopy(ent->v.angles, angles);
660 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
661 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
663 // update lerp positions
664 ent->steplerptime = sv.time;
665 VectorCopy(ent->v.origin, ent->stepoldorigin);
666 VectorCopy(ent->v.angles, ent->stepoldangles);
667 VectorCopy(ent->v.origin, ent->steporigin);
668 VectorCopy(ent->v.angles, ent->stepangles);
672 // LordHavoc: old stuff, but rewritten to have more exact tolerances
673 // if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
674 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
675 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
676 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
677 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
678 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
679 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
680 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
681 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
682 if (baseline->colormap != (byte) ent->v.colormap) bits |= U_COLORMAP;
683 if (baseline->skin != (byte) ent->v.skin) bits |= U_SKIN;
684 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
685 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
686 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
688 // LordHavoc: new stuff
689 if (baseline->alpha != alpha) bits |= U_ALPHA;
690 if (baseline->scale != scale) bits |= U_SCALE;
691 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
692 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
693 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
694 if (baseline->colormod != colormod) bits |= U_COLORMOD;
695 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
696 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
698 // update delta baseline
699 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
700 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
701 ent->deltabaseline.colormap = ent->v.colormap;
702 ent->deltabaseline.skin = ent->v.skin;
703 ent->deltabaseline.frame = ent->v.frame;
704 ent->deltabaseline.effects = ent->v.effects;
705 ent->deltabaseline.modelindex = ent->v.modelindex;
706 ent->deltabaseline.alpha = alpha;
707 ent->deltabaseline.scale = scale;
708 ent->deltabaseline.glowsize = glowsize;
709 ent->deltabaseline.glowcolor = glowcolor;
710 ent->deltabaseline.colormod = colormod;
712 if (bits & (U_ALPHA | U_SCALE | U_EFFECTS2 | U_GLOWSIZE | U_GLOWCOLOR | U_COLORMOD | U_FRAME2 | U_MODEL2))
716 if (bits >= 16777216)
724 MSG_WriteByte (msg, bits);
725 if (bits & U_MOREBITS)
726 MSG_WriteByte (msg, bits>>8);
727 // LordHavoc: extend bytes have to be written here due to delta compression
728 if (bits & U_EXTEND1)
729 MSG_WriteByte (msg, bits>>16);
730 if (bits & U_EXTEND2)
731 MSG_WriteByte (msg, bits>>24);
733 // LordHavoc: old stuff
734 if (bits & U_LONGENTITY)
735 MSG_WriteShort (msg,e);
737 MSG_WriteByte (msg,e);
738 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
739 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
740 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
741 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
742 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
743 if (bits & U_ORIGIN1) MSG_WriteFloatCoord (msg, origin[0]);
744 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
745 if (bits & U_ORIGIN2) MSG_WriteFloatCoord (msg, origin[1]);
746 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
747 if (bits & U_ORIGIN3) MSG_WriteFloatCoord (msg, origin[2]);
748 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
750 // LordHavoc: new stuff
751 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
752 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
753 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
754 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
755 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
756 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
757 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
758 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
768 void SV_CleanupEnts (void)
773 ent = NEXT_EDICT(sv.edicts);
774 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
776 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
783 SV_WriteClientdataToMessage
787 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
797 // send a damage message
799 if (ent->v.dmg_take || ent->v.dmg_save)
801 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
802 MSG_WriteByte (msg, svc_damage);
803 MSG_WriteByte (msg, ent->v.dmg_save);
804 MSG_WriteByte (msg, ent->v.dmg_take);
805 for (i=0 ; i<3 ; i++)
806 MSG_WriteFloatCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
813 // send the current viewpos offset from the view entity
815 SV_SetIdealPitch (); // how much to look up / down ideally
817 // a fixangle might get lost in a dropped packet. Oh well.
818 if ( ent->v.fixangle )
820 MSG_WriteByte (msg, svc_setangle);
821 for (i=0 ; i < 3 ; i++)
822 MSG_WriteAngle (msg, ent->v.angles[i] );
828 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
829 bits |= SU_VIEWHEIGHT;
831 if (ent->v.idealpitch)
832 bits |= SU_IDEALPITCH;
834 // stuff the sigil bits into the high bits of items for sbar, or else
836 val = GETEDICTFIELDVALUE(ent, eval_items2);
839 items = (int)ent->v.items | ((int)val->_float << 23);
841 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
845 if ( (int)ent->v.flags & FL_ONGROUND)
848 if ( ent->v.waterlevel >= 2)
852 VectorClear(punchvector);
853 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
854 VectorCopy(val->vector, punchvector);
856 for (i=0 ; i<3 ; i++)
858 if (ent->v.punchangle[i])
859 bits |= (SU_PUNCH1<<i);
860 if (punchvector[i]) // dpprotocol
861 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
862 if (ent->v.velocity[i])
863 bits |= (SU_VELOCITY1<<i);
866 if (ent->v.weaponframe)
867 bits |= SU_WEAPONFRAME;
869 if (ent->v.armorvalue)
872 // if (ent->v.weapon)
877 if (bits >= 16777216)
882 MSG_WriteByte (msg, svc_clientdata);
883 MSG_WriteShort (msg, bits);
884 if (bits & SU_EXTEND1)
885 MSG_WriteByte(msg, bits >> 16);
886 if (bits & SU_EXTEND2)
887 MSG_WriteByte(msg, bits >> 24);
889 if (bits & SU_VIEWHEIGHT)
890 MSG_WriteChar (msg, ent->v.view_ofs[2]);
892 if (bits & SU_IDEALPITCH)
893 MSG_WriteChar (msg, ent->v.idealpitch);
895 for (i=0 ; i<3 ; i++)
897 if (bits & (SU_PUNCH1<<i))
898 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
899 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
900 MSG_WriteFloatCoord(msg, punchvector[i]); // dpprotocol
901 if (bits & (SU_VELOCITY1<<i))
902 MSG_WriteChar (msg, ent->v.velocity[i]/16);
905 // [always sent] if (bits & SU_ITEMS)
906 MSG_WriteLong (msg, items);
908 if (bits & SU_WEAPONFRAME)
909 MSG_WriteByte (msg, ent->v.weaponframe);
911 MSG_WriteByte (msg, ent->v.armorvalue);
912 if (bits & SU_WEAPON)
913 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
915 MSG_WriteShort (msg, ent->v.health);
916 MSG_WriteByte (msg, ent->v.currentammo);
917 MSG_WriteByte (msg, ent->v.ammo_shells);
918 MSG_WriteByte (msg, ent->v.ammo_nails);
919 MSG_WriteByte (msg, ent->v.ammo_rockets);
920 MSG_WriteByte (msg, ent->v.ammo_cells);
924 MSG_WriteByte (msg, ent->v.weapon);
930 if ( ((int)ent->v.weapon) & (1<<i) )
932 MSG_WriteByte (msg, i);
940 =======================
941 SV_SendClientDatagram
942 =======================
944 qboolean SV_SendClientDatagram (client_t *client)
946 byte buf[MAX_DATAGRAM];
950 msg.maxsize = sizeof(buf);
953 MSG_WriteByte (&msg, svc_time);
954 MSG_WriteFloat (&msg, sv.time);
956 // add the client specific data to the datagram
957 SV_WriteClientdataToMessage (client->edict, &msg);
959 SV_WriteEntitiesToClient (client, client->edict, &msg);
961 // copy the server datagram if there is space
962 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
963 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
966 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
968 SV_DropClient (true);// if the message couldn't send, kick off
976 =======================
977 SV_UpdateToReliableMessages
978 =======================
980 void SV_UpdateToReliableMessages (void)
985 // check for changes to be sent over the reliable streams
986 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
988 if (host_client->old_frags != host_client->edict->v.frags)
990 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
994 MSG_WriteByte (&client->message, svc_updatefrags);
995 MSG_WriteByte (&client->message, i);
996 MSG_WriteShort (&client->message, host_client->edict->v.frags);
999 host_client->old_frags = host_client->edict->v.frags;
1003 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1005 if (!client->active)
1007 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1010 SZ_Clear (&sv.reliable_datagram);
1015 =======================
1018 Send a nop message without trashing or sending the accumulated client
1020 =======================
1022 void SV_SendNop (client_t *client)
1028 msg.maxsize = sizeof(buf);
1031 MSG_WriteChar (&msg, svc_nop);
1033 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1034 SV_DropClient (true); // if the message couldn't send, kick off
1035 client->last_message = realtime;
1039 =======================
1040 SV_SendClientMessages
1041 =======================
1043 void SV_SendClientMessages (void)
1047 // update frags, names, etc
1048 SV_UpdateToReliableMessages ();
1050 // build individual updates
1051 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1053 if (!host_client->active)
1056 if (host_client->spawned)
1058 if (!SV_SendClientDatagram (host_client))
1063 // the player isn't totally in the game yet
1064 // send small keepalive messages if too much time has passed
1065 // send a full message when the next signon stage has been requested
1066 // some other message data (name changes, etc) may accumulate
1067 // between signon stages
1068 if (!host_client->sendsignon)
1070 if (realtime - host_client->last_message > 5)
1071 SV_SendNop (host_client);
1072 continue; // don't send out non-signon messages
1076 // check for an overflowed message. Should only happen
1077 // on a very fucked up connection that backs up a lot, then
1079 if (host_client->message.overflowed)
1081 SV_DropClient (true);
1082 host_client->message.overflowed = false;
1086 if (host_client->message.cursize || host_client->dropasap)
1088 if (!NET_CanSendMessage (host_client->netconnection))
1090 // I_Printf ("can't write\n");
1094 if (host_client->dropasap)
1095 SV_DropClient (false); // went to another level
1098 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1099 SV_DropClient (true); // if the message couldn't send, kick off
1100 SZ_Clear (&host_client->message);
1101 host_client->last_message = realtime;
1102 host_client->sendsignon = false;
1108 // clear muzzle flashes
1114 ==============================================================================
1118 ==============================================================================
1127 int SV_ModelIndex (char *name)
1131 if (!name || !name[0])
1134 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1135 if (!strcmp(sv.model_precache[i], name))
1137 if (i==MAX_MODELS || !sv.model_precache[i])
1138 Host_Error ("SV_ModelIndex: model %s not precached", name);
1148 void SV_CreateBaseline (void)
1150 int i, entnum, large;
1153 // LordHavoc: clear *all* states (note just active ones)
1154 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1156 // get the current server version
1157 svent = EDICT_NUM(entnum);
1159 // LordHavoc: always clear state values, whether the entity is in use or not
1160 ClearStateToDefault(&svent->baseline);
1164 if (entnum > svs.maxclients && !svent->v.modelindex)
1168 // create entity baseline
1170 VectorCopy (svent->v.origin, svent->baseline.origin);
1171 VectorCopy (svent->v.angles, svent->baseline.angles);
1172 svent->baseline.frame = svent->v.frame;
1173 svent->baseline.skin = svent->v.skin;
1174 if (entnum > 0 && entnum <= svs.maxclients)
1176 svent->baseline.colormap = entnum;
1177 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1181 svent->baseline.colormap = 0;
1182 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1186 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1189 // add to the message
1192 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1194 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1195 MSG_WriteShort (&sv.signon, entnum);
1199 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1200 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1204 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1205 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1207 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1208 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1209 for (i=0 ; i<3 ; i++)
1211 MSG_WriteFloatCoord(&sv.signon, svent->baseline.origin[i]);
1212 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1222 Tell all the clients that the server is changing levels
1225 void SV_SendReconnect (void)
1232 msg.maxsize = sizeof(data);
1234 MSG_WriteChar (&msg, svc_stufftext);
1235 MSG_WriteString (&msg, "reconnect\n");
1236 NET_SendToAll (&msg, 5);
1238 if (cls.state != ca_dedicated)
1239 Cmd_ExecuteString ("reconnect\n", src_command);
1247 Grabs the current state of each client for saving across the
1248 transition to another level
1251 void SV_SaveSpawnparms (void)
1255 svs.serverflags = pr_global_struct->serverflags;
1257 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1259 if (!host_client->active)
1262 // call the progs to get default spawn parms for the new client
1263 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1264 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1265 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1266 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1270 qboolean isworldmodel;
1276 This is called at the start of each level
1279 extern float scr_centertime_off;
1281 void SV_SpawnServer (char *server)
1286 // let's not have any servers with no name
1287 if (hostname.string[0] == 0)
1288 Cvar_Set ("hostname", "UNNAMED");
1289 scr_centertime_off = 0;
1291 Con_DPrintf ("SpawnServer: %s\n",server);
1292 svs.changelevel_issued = false; // now safe to issue another
1295 // tell all connected clients that we are going to a new level
1299 SV_SendReconnect ();
1303 // make cvars consistant
1306 Cvar_SetValue ("deathmatch", 0);
1307 current_skill = (int)(skill.value + 0.5);
1308 if (current_skill < 0)
1310 if (current_skill > 3)
1313 Cvar_SetValue ("skill", (float)current_skill);
1316 // set up the new server
1318 Host_ClearMemory ();
1320 memset (&sv, 0, sizeof(sv));
1322 strcpy (sv.name, server);
1324 // load progs to get entity field count
1327 // allocate server memory
1328 sv.max_edicts = MAX_EDICTS;
1330 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1332 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1333 sv.datagram.cursize = 0;
1334 sv.datagram.data = sv.datagram_buf;
1336 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1337 sv.reliable_datagram.cursize = 0;
1338 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1340 sv.signon.maxsize = sizeof(sv.signon_buf);
1341 sv.signon.cursize = 0;
1342 sv.signon.data = sv.signon_buf;
1344 // leave slots at start for clients only
1345 sv.num_edicts = svs.maxclients+1;
1346 for (i=0 ; i<svs.maxclients ; i++)
1348 ent = EDICT_NUM(i+1);
1349 svs.clients[i].edict = ent;
1352 sv.state = ss_loading;
1357 strcpy (sv.name, server);
1358 sprintf (sv.modelname,"maps/%s.bsp", server);
1359 isworldmodel = true; // LordHavoc: only load submodels on the world model
1360 sv.worldmodel = Mod_ForName (sv.modelname, false);
1361 isworldmodel = false;
1364 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1368 sv.models[1] = sv.worldmodel;
1371 // clear world interaction links
1375 sv.sound_precache[0] = pr_strings;
1377 sv.model_precache[0] = pr_strings;
1378 sv.model_precache[1] = sv.modelname;
1379 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1381 sv.model_precache[1+i] = localmodels[i];
1382 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1386 // load the rest of the entities
1389 memset (&ent->v, 0, progs->entityfields * 4);
1391 ent->v.model = sv.worldmodel->name - pr_strings;
1392 ent->v.modelindex = 1; // world model
1393 ent->v.solid = SOLID_BSP;
1394 ent->v.movetype = MOVETYPE_PUSH;
1395 ent->v.angles[0] = ent->v.angles[1] = ent->v.angles[2] = 0;
1398 pr_global_struct->coop = coop.value;
1400 pr_global_struct->deathmatch = deathmatch.value;
1402 pr_global_struct->mapname = sv.name - pr_strings;
1404 // serverflags are for cross level information (sigils)
1405 pr_global_struct->serverflags = svs.serverflags;
1407 ED_LoadFromFile (sv.worldmodel->entities);
1408 // LordHavoc: clear world angles (to fix e3m3.bsp)
1409 sv.edicts->v.angles[0] = sv.edicts->v.angles[1] = sv.edicts->v.angles[2] = 0;
1413 // all setup is completed, any further precache statements are errors
1414 sv.state = ss_active;
1416 // run two frames to allow everything to settle
1417 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1421 // create a baseline for more efficient communications
1422 SV_CreateBaseline ();
1424 // send serverinfo to all connected clients
1425 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1426 if (host_client->active)
1427 SV_SendServerinfo (host_client);
1429 Con_DPrintf ("Server spawned.\n");