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;
50 Cvar_RegisterVariable (&sv_maxvelocity);
51 Cvar_RegisterVariable (&sv_gravity);
52 Cvar_RegisterVariable (&sv_friction);
53 Cvar_RegisterVariable (&sv_edgefriction);
54 Cvar_RegisterVariable (&sv_stopspeed);
55 Cvar_RegisterVariable (&sv_maxspeed);
56 Cvar_RegisterVariable (&sv_accelerate);
57 Cvar_RegisterVariable (&sv_idealpitchscale);
58 Cvar_RegisterVariable (&sv_aim);
59 Cvar_RegisterVariable (&sv_nostep);
61 for (i=0 ; i<MAX_MODELS ; i++)
62 sprintf (localmodels[i], "*%i", i);
66 =============================================================================
70 =============================================================================
77 Make sure the event gets sent to all clients
80 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
84 if (sv.datagram.cursize > MAX_DATAGRAM-16)
86 MSG_WriteByte (&sv.datagram, svc_particle);
87 MSG_WriteCoord (&sv.datagram, org[0]);
88 MSG_WriteCoord (&sv.datagram, org[1]);
89 MSG_WriteCoord (&sv.datagram, org[2]);
97 MSG_WriteChar (&sv.datagram, v);
99 MSG_WriteByte (&sv.datagram, count);
100 MSG_WriteByte (&sv.datagram, color);
107 Each entity can have eight independant sound sources, like voice,
110 Channel 0 is an auto-allocate channel, the others override anything
111 allready running on that entity/channel pair.
113 An attenuation of 0 will play full volume everywhere in the level.
114 Larger attenuations will drop off. (max 4 attenuation)
118 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
126 if (volume < 0 || volume > 255)
127 Sys_Error ("SV_StartSound: volume = %i", volume);
129 if (attenuation < 0 || attenuation > 4)
130 Sys_Error ("SV_StartSound: attenuation = %f", attenuation);
132 if (channel < 0 || channel > 7)
133 Sys_Error ("SV_StartSound: channel = %i", channel);
135 if (sv.datagram.cursize > MAX_DATAGRAM-16)
138 // find precache number for sound
139 for (sound_num=1 ; sound_num<MAX_SOUNDS
140 && sv.sound_precache[sound_num] ; sound_num++)
141 if (!strcmp(sample, sv.sound_precache[sound_num]))
144 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
146 Con_Printf ("SV_StartSound: %s not precached\n", sample);
150 ent = NUM_FOR_EDICT(entity);
152 channel = (ent<<3) | channel;
155 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
156 field_mask |= SND_VOLUME;
157 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
158 field_mask |= SND_ATTENUATION;
160 // directed messages go only to the entity the are targeted on
161 MSG_WriteByte (&sv.datagram, svc_sound);
162 MSG_WriteByte (&sv.datagram, field_mask);
163 if (field_mask & SND_VOLUME)
164 MSG_WriteByte (&sv.datagram, volume);
165 if (field_mask & SND_ATTENUATION)
166 MSG_WriteByte (&sv.datagram, attenuation*64);
167 MSG_WriteShort (&sv.datagram, channel);
168 MSG_WriteByte (&sv.datagram, sound_num);
169 for (i=0 ; i<3 ; i++)
170 MSG_WriteCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
174 ==============================================================================
178 ==============================================================================
185 Sends the first message from the server to a connected client.
186 This will be sent on the initial connection and upon each server load.
189 void SV_SendServerinfo (client_t *client)
194 MSG_WriteByte (&client->message, svc_print);
196 sprintf (message, "%c\nDPNEHAHRA VERSION %4.2f SERVER (%i CRC)", 2, DP_VERSION, pr_crc);
198 sprintf (message, "%c\nDARKPLACES VERSION %4.2f SERVER (%i CRC)", 2, DP_VERSION, pr_crc);
200 MSG_WriteString (&client->message,message);
202 MSG_WriteByte (&client->message, svc_serverinfo);
203 MSG_WriteLong (&client->message, PROTOCOL_VERSION);
204 MSG_WriteByte (&client->message, svs.maxclients);
206 if (!coop.value && deathmatch.value)
207 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
209 MSG_WriteByte (&client->message, GAME_COOP);
211 sprintf (message, pr_strings+sv.edicts->v.message);
213 MSG_WriteString (&client->message,message);
215 for (s = sv.model_precache+1 ; *s ; s++)
216 MSG_WriteString (&client->message, *s);
217 MSG_WriteByte (&client->message, 0);
219 for (s = sv.sound_precache+1 ; *s ; s++)
220 MSG_WriteString (&client->message, *s);
221 MSG_WriteByte (&client->message, 0);
224 MSG_WriteByte (&client->message, svc_cdtrack);
225 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
226 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
229 MSG_WriteByte (&client->message, svc_setview);
230 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
232 MSG_WriteByte (&client->message, svc_signonnum);
233 MSG_WriteByte (&client->message, 1);
235 client->sendsignon = true;
236 client->spawned = false; // need prespawn, spawn, etc
243 Initializes a client_t for a new net connection. This will only be called
244 once for a player each game, not once for each level change.
247 void SV_ConnectClient (int clientnum)
252 struct qsocket_s *netconnection;
254 float spawn_parms[NUM_SPAWN_PARMS];
256 client = svs.clients + clientnum;
258 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
260 edictnum = clientnum+1;
262 ent = EDICT_NUM(edictnum);
264 // set up the client_t
265 netconnection = client->netconnection;
268 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
269 memset (client, 0, sizeof(*client));
270 client->netconnection = netconnection;
272 strcpy (client->name, "unconnected");
273 client->active = true;
274 client->spawned = false;
276 client->message.data = client->msgbuf;
277 client->message.maxsize = sizeof(client->msgbuf);
278 client->message.allowoverflow = true; // we can catch it
281 client->privileged = IsID(&client->netconnection->addr);
283 client->privileged = false;
287 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
290 // call the progs to get default spawn parms for the new client
291 PR_ExecuteProgram (pr_global_struct->SetNewParms);
292 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
293 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
296 SV_SendServerinfo (client);
302 SV_CheckForNewClients
306 void SV_CheckForNewClients (void)
308 struct qsocket_s *ret;
312 // check for new connections
316 ret = NET_CheckNewConnections ();
321 // init a new client structure
323 for (i=0 ; i<svs.maxclients ; i++)
324 if (!svs.clients[i].active)
326 if (i == svs.maxclients)
327 Sys_Error ("Host_CheckForNewClients: no free clients");
329 svs.clients[i].netconnection = ret;
330 SV_ConnectClient (i);
332 net_activeconnections++;
339 ===============================================================================
343 ===============================================================================
352 void SV_ClearDatagram (void)
354 SZ_Clear (&sv.datagram);
358 =============================================================================
360 The PVS must include a small area around the client to allow head bobbing
361 or other small motion on the client side. Otherwise, a bob might cause an
362 entity that should be visible to not show up, especially when the bob
365 =============================================================================
369 byte fatpvs[MAX_MAP_LEAFS/8];
371 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
380 // if this is a leaf, accumulate the pvs bits
381 if (node->contents < 0)
383 if (node->contents != CONTENTS_SOLID)
385 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
386 for (i=0 ; i<fatbytes ; i++)
393 d = DotProduct (org, plane->normal) - plane->dist;
395 node = node->children[0];
397 node = node->children[1];
400 SV_AddToFatPVS (org, node->children[0]);
401 node = node->children[1];
410 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
414 byte *SV_FatPVS (vec3_t org)
416 fatbytes = (sv.worldmodel->numleafs+31)>>3;
417 memset (fatpvs, 0, fatbytes);
418 SV_AddToFatPVS (org, sv.worldmodel->nodes);
422 //=============================================================================
427 SV_WriteEntitiesToClient
431 void SV_WriteEntitiesToClient (edict_t *clent, sizebuf_t *msg)
433 int e, i, clentnum, bits, alpha, glowcolor, glowsize, scale, colormod, modred, modgreen, modblue, dodelta, effects;
435 vec3_t org, origin, angles;
436 float movelerp, moveilerp;
439 entity_state_t *baseline; // LordHavoc: delta or startup baseline
441 // find the client's PVS
442 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
443 pvs = SV_FatPVS (org);
445 clentnum = NUM_FOR_EDICT(clent); // LordHavoc: for comparison purposes
446 // send over all entities (except the client) that touch the pvs
447 ent = NEXT_EDICT(sv.edicts);
448 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
451 if (ent != clent) // LordHavoc: always send player
453 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
455 if (val->edict != clentnum)
456 continue; // don't show to anyone else
458 bits |= U_VIEWMODEL; // show relative to the view
462 // LordHavoc: never draw something told not to display to this client
463 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
465 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
467 // ignore if not touching a PV leaf
468 for (i=0 ; i < ent->num_leafs ; i++)
469 if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
472 if (i == ent->num_leafs)
473 continue; // not visible
477 // don't send if flagged for NODRAW and there are no effects
483 effects = ent->v.effects;
485 if (val = GETEDICTFIELDVALUE(ent, eval_alpha))
486 if ((alpha = (int) (val->_float * 255.0)) == 0)
488 if (val = GETEDICTFIELDVALUE(ent, eval_renderamt)) // HalfLife support
489 alpha -= (int) val->_float;
490 if (alpha < 0) alpha = 0;
491 if (alpha > 255) alpha = 255;
493 if (val = GETEDICTFIELDVALUE(ent, eval_glow_size))
494 glowsize = (int) val->_float >> 3;
495 if (glowsize > 127) glowsize = 127;
496 if (glowsize < -128) glowsize = -128;
498 if (val = GETEDICTFIELDVALUE(ent, eval_scale))
499 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
500 if (scale < 0) scale = 0;
501 if (scale > 255) scale = 255;
503 if (val = GETEDICTFIELDVALUE(ent, eval_glow_trail))
504 if (val->_float != 0)
507 if (val = GETEDICTFIELDVALUE(ent, eval_glow_color))
508 if (val->_float != 0)
509 glowcolor = (int) val->_float;
511 if (val = GETEDICTFIELDVALUE(ent, eval_fullbright))
512 if (val->_float != 0)
513 effects |= EF_FULLBRIGHT;
515 if (val = GETEDICTFIELDVALUE(ent, eval_colormod))
516 if (val->vector[0] != 0 || val->vector[1] != 0 || val->vector[2] != 0)
518 modred = val->vector[0] * 8.0;if (modred < 0) modred = 0;if (modred > 7) modred = 7;
519 modgreen = val->vector[1] * 8.0;if (modgreen < 0) modgreen = 0;if (modgreen > 7) modgreen = 7;
520 modblue = val->vector[2] * 4.0;if (modblue < 0) modblue = 0;if (modblue > 3) modblue = 3;
521 colormod = (modred << 5) | (modgreen << 2) | modblue;
526 if (glowsize == 0 && bits == 0) // no effects
528 if (ent->v.modelindex && pr_strings[ent->v.model]) // model
530 if (sv.models[ (int)ent->v.modelindex ]->flags == 0 && (ent->v.effects == EF_NODRAW || scale <= 0 || alpha <= 0))
533 else // no model and no effects
538 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
540 Con_Printf ("packet overflow\n");
548 if ((int)ent->v.effects & EF_DELTA)
549 dodelta = cl.time < ent->nextfullupdate; // every half second a full update is forced
554 baseline = &ent->deltabaseline;
558 ent->nextfullupdate = cl.time + 0.5;
559 baseline = &ent->baseline;
563 bits |= U_LONGENTITY;
564 if (ent->v.movetype == MOVETYPE_STEP)
567 if (ent->v.movetype == MOVETYPE_STEP && ((int) ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM))) // monsters have smoothed walking/flying/swimming movement
569 if (!ent->steplerptime || ent->steplerptime > sv.time) // when the level just started...
571 ent->steplerptime = sv.time;
572 VectorCopy(ent->v.origin, ent->stepoldorigin);
573 VectorCopy(ent->v.angles, ent->stepoldangles);
574 VectorCopy(ent->v.origin, ent->steporigin);
575 VectorCopy(ent->v.angles, ent->stepangles);
577 VectorSubtract(ent->v.origin, ent->steporigin, origin);
578 VectorSubtract(ent->v.angles, ent->stepangles, angles);
579 if (DotProduct(origin, origin) >= 0.125 || DotProduct(angles, angles) >= 1.4)
581 // update lerp positions
582 ent->steplerptime = sv.time;
583 VectorCopy(ent->steporigin, ent->stepoldorigin);
584 VectorCopy(ent->stepangles, ent->stepoldangles);
585 VectorCopy(ent->v.origin, ent->steporigin);
586 VectorCopy(ent->v.angles, ent->stepangles);
588 movelerp = (sv.time - ent->steplerptime) * 10.0;
589 if (movelerp > 1) movelerp = 1;
590 moveilerp = 1 - movelerp;
591 origin[0] = ent->stepoldorigin[0] * moveilerp + ent->steporigin[0] * movelerp;
592 origin[1] = ent->stepoldorigin[1] * moveilerp + ent->steporigin[1] * movelerp;
593 origin[2] = ent->stepoldorigin[2] * moveilerp + ent->steporigin[2] * movelerp;
594 // choose shortest rotate (to avoid 'spin around' situations)
595 VectorSubtract(ent->stepangles, ent->stepoldangles, angles);
596 if (angles[0] < -180) angles[0] += 360;if (angles[0] >= 180) angles[0] -= 360;
597 if (angles[1] < -180) angles[1] += 360;if (angles[1] >= 180) angles[1] -= 360;
598 if (angles[2] < -180) angles[2] += 360;if (angles[2] >= 180) angles[2] -= 360;
599 angles[0] = angles[0] * movelerp + ent->stepoldangles[0];
600 angles[1] = angles[1] * movelerp + ent->stepoldangles[1];
601 angles[2] = angles[2] * movelerp + ent->stepoldangles[2];
603 else // copy as they are
605 VectorCopy(ent->v.origin, origin);
606 VectorCopy(ent->v.angles, angles);
607 if (ent->v.movetype == MOVETYPE_STEP) // monster, but airborn, update lerp info
609 // update lerp positions
610 ent->steplerptime = sv.time;
611 VectorCopy(ent->v.origin, ent->stepoldorigin);
612 VectorCopy(ent->v.angles, ent->stepoldangles);
613 VectorCopy(ent->v.origin, ent->steporigin);
614 VectorCopy(ent->v.angles, ent->stepangles);
618 // LordHavoc: old stuff, but rewritten to have more exact tolerances
619 if ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
620 if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
621 if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
622 if ((int)(angles[0]*(256.0/360.0)) != (int)(baseline->angles[0]*(256.0/360.0))) bits |= U_ANGLE1;
623 if ((int)(angles[1]*(256.0/360.0)) != (int)(baseline->angles[1]*(256.0/360.0))) bits |= U_ANGLE2;
624 if ((int)(angles[2]*(256.0/360.0)) != (int)(baseline->angles[2]*(256.0/360.0))) bits |= U_ANGLE3;
625 if (baseline->colormap != (int) ent->v.colormap) bits |= U_COLORMAP;
626 if (baseline->skin != (int) ent->v.skin) bits |= U_SKIN;
627 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
628 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
629 if (baseline->modelindex != (int) ent->v.modelindex) bits |= U_MODEL;
631 // LordHavoc: new stuff
632 if (baseline->alpha != alpha) bits |= U_ALPHA;
633 if (baseline->scale != scale) bits |= U_SCALE;
634 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
635 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
636 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
637 if (baseline->colormod != colormod) bits |= U_COLORMOD;
638 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
640 // update delta baseline
641 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
642 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
643 ent->deltabaseline.colormap = ent->v.colormap;
644 ent->deltabaseline.skin = ent->v.skin;
645 ent->deltabaseline.frame = ent->v.frame;
646 ent->deltabaseline.effects = ent->v.effects;
647 ent->deltabaseline.modelindex = ent->v.modelindex;
648 ent->deltabaseline.alpha = alpha;
649 ent->deltabaseline.scale = scale;
650 ent->deltabaseline.glowsize = glowsize;
651 ent->deltabaseline.glowcolor = glowcolor;
652 ent->deltabaseline.colormod = colormod;
655 if (bits >= 16777216)
663 MSG_WriteByte (msg, bits);
664 if (bits & U_MOREBITS)
665 MSG_WriteByte (msg, bits>>8);
666 // LordHavoc: extend bytes have to be written here due to delta compression
667 if (bits & U_EXTEND1)
668 MSG_WriteByte (msg, bits>>16);
669 if (bits & U_EXTEND2)
670 MSG_WriteByte (msg, bits>>24);
672 // LordHavoc: old stuff
673 if (bits & U_LONGENTITY)
674 MSG_WriteShort (msg,e);
676 MSG_WriteByte (msg,e);
677 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
678 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
679 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
680 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
681 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
682 if (bits & U_ORIGIN1) MSG_WriteCoord (msg, origin[0]);
683 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
684 if (bits & U_ORIGIN2) MSG_WriteCoord (msg, origin[1]);
685 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
686 if (bits & U_ORIGIN3) MSG_WriteCoord (msg, origin[2]);
687 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
689 // LordHavoc: new stuff
690 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
691 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
692 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
693 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
694 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
695 if (bits & U_COLORMOD) MSG_WriteByte(msg, colormod);
696 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
706 void SV_CleanupEnts (void)
711 ent = NEXT_EDICT(sv.edicts);
712 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
714 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
721 SV_WriteClientdataToMessage
725 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
734 // send a damage message
736 if (ent->v.dmg_take || ent->v.dmg_save)
738 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
739 MSG_WriteByte (msg, svc_damage);
740 MSG_WriteByte (msg, ent->v.dmg_save);
741 MSG_WriteByte (msg, ent->v.dmg_take);
742 for (i=0 ; i<3 ; i++)
743 MSG_WriteCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
750 // send the current viewpos offset from the view entity
752 SV_SetIdealPitch (); // how much to look up / down ideally
754 // a fixangle might get lost in a dropped packet. Oh well.
755 if ( ent->v.fixangle )
757 MSG_WriteByte (msg, svc_setangle);
758 for (i=0 ; i < 3 ; i++)
759 MSG_WriteAngle (msg, ent->v.angles[i] );
765 if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
766 bits |= SU_VIEWHEIGHT;
768 if (ent->v.idealpitch)
769 bits |= SU_IDEALPITCH;
771 // stuff the sigil bits into the high bits of items for sbar, or else
773 val = GETEDICTFIELDVALUE(ent, eval_items2);
776 items = (int)ent->v.items | ((int)val->_float << 23);
778 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
782 if ( (int)ent->v.flags & FL_ONGROUND)
785 if ( ent->v.waterlevel >= 2)
788 for (i=0 ; i<3 ; i++)
790 if (ent->v.punchangle[i])
791 bits |= (SU_PUNCH1<<i);
792 if (ent->v.velocity[i])
793 bits |= (SU_VELOCITY1<<i);
796 if (ent->v.weaponframe)
797 bits |= SU_WEAPONFRAME;
799 if (ent->v.armorvalue)
802 // if (ent->v.weapon)
807 MSG_WriteByte (msg, svc_clientdata);
808 MSG_WriteShort (msg, bits);
810 if (bits & SU_VIEWHEIGHT)
811 MSG_WriteChar (msg, ent->v.view_ofs[2]);
813 if (bits & SU_IDEALPITCH)
814 MSG_WriteChar (msg, ent->v.idealpitch);
816 for (i=0 ; i<3 ; i++)
818 if (bits & (SU_PUNCH1<<i))
819 MSG_WriteChar (msg, ent->v.punchangle[i]);
820 if (bits & (SU_VELOCITY1<<i))
821 MSG_WriteChar (msg, ent->v.velocity[i]/16);
824 // [always sent] if (bits & SU_ITEMS)
825 MSG_WriteLong (msg, items);
827 if (bits & SU_WEAPONFRAME)
828 MSG_WriteByte (msg, ent->v.weaponframe);
830 MSG_WriteByte (msg, ent->v.armorvalue);
831 if (bits & SU_WEAPON)
832 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
834 MSG_WriteShort (msg, ent->v.health);
835 MSG_WriteByte (msg, ent->v.currentammo);
836 MSG_WriteByte (msg, ent->v.ammo_shells);
837 MSG_WriteByte (msg, ent->v.ammo_nails);
838 MSG_WriteByte (msg, ent->v.ammo_rockets);
839 MSG_WriteByte (msg, ent->v.ammo_cells);
843 MSG_WriteByte (msg, ent->v.weapon);
849 if ( ((int)ent->v.weapon) & (1<<i) )
851 MSG_WriteByte (msg, i);
859 =======================
860 SV_SendClientDatagram
861 =======================
863 qboolean SV_SendClientDatagram (client_t *client)
865 byte buf[MAX_DATAGRAM];
869 msg.maxsize = sizeof(buf);
872 MSG_WriteByte (&msg, svc_time);
873 MSG_WriteFloat (&msg, sv.time);
875 // add the client specific data to the datagram
876 SV_WriteClientdataToMessage (client->edict, &msg);
878 SV_WriteEntitiesToClient (client->edict, &msg);
880 // copy the server datagram if there is space
881 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
882 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
885 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
887 SV_DropClient (true);// if the message couldn't send, kick off
895 =======================
896 SV_UpdateToReliableMessages
897 =======================
899 void SV_UpdateToReliableMessages (void)
904 // check for changes to be sent over the reliable streams
905 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
907 if (host_client->old_frags != host_client->edict->v.frags)
909 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
913 MSG_WriteByte (&client->message, svc_updatefrags);
914 MSG_WriteByte (&client->message, i);
915 MSG_WriteShort (&client->message, host_client->edict->v.frags);
918 host_client->old_frags = host_client->edict->v.frags;
922 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
926 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
929 SZ_Clear (&sv.reliable_datagram);
934 =======================
937 Send a nop message without trashing or sending the accumulated client
939 =======================
941 void SV_SendNop (client_t *client)
947 msg.maxsize = sizeof(buf);
950 MSG_WriteChar (&msg, svc_nop);
952 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
953 SV_DropClient (true); // if the message couldn't send, kick off
954 client->last_message = realtime;
958 =======================
959 SV_SendClientMessages
960 =======================
962 void SV_SendClientMessages (void)
966 // update frags, names, etc
967 SV_UpdateToReliableMessages ();
969 // build individual updates
970 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
972 if (!host_client->active)
975 if (host_client->spawned)
977 if (!SV_SendClientDatagram (host_client))
982 // the player isn't totally in the game yet
983 // send small keepalive messages if too much time has passed
984 // send a full message when the next signon stage has been requested
985 // some other message data (name changes, etc) may accumulate
986 // between signon stages
987 if (!host_client->sendsignon)
989 if (realtime - host_client->last_message > 5)
990 SV_SendNop (host_client);
991 continue; // don't send out non-signon messages
995 // check for an overflowed message. Should only happen
996 // on a very fucked up connection that backs up a lot, then
998 if (host_client->message.overflowed)
1000 SV_DropClient (true);
1001 host_client->message.overflowed = false;
1005 if (host_client->message.cursize || host_client->dropasap)
1007 if (!NET_CanSendMessage (host_client->netconnection))
1009 // I_Printf ("can't write\n");
1013 if (host_client->dropasap)
1014 SV_DropClient (false); // went to another level
1017 if (NET_SendMessage (host_client->netconnection
1018 , &host_client->message) == -1)
1019 SV_DropClient (true); // if the message couldn't send, kick off
1020 SZ_Clear (&host_client->message);
1021 host_client->last_message = realtime;
1022 host_client->sendsignon = false;
1028 // clear muzzle flashes
1034 ==============================================================================
1038 ==============================================================================
1047 int SV_ModelIndex (char *name)
1051 if (!name || !name[0])
1054 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1055 if (!strcmp(sv.model_precache[i], name))
1057 if (i==MAX_MODELS || !sv.model_precache[i])
1058 Sys_Error ("SV_ModelIndex: model %s not precached", name);
1068 void SV_CreateBaseline (void)
1074 for (entnum = 0; entnum < sv.num_edicts ; entnum++)
1076 // get the current server version
1077 svent = EDICT_NUM(entnum);
1080 if (entnum > svs.maxclients && !svent->v.modelindex)
1084 // create entity baseline
1086 VectorCopy (svent->v.origin, svent->baseline.origin);
1087 VectorCopy (svent->v.angles, svent->baseline.angles);
1088 svent->baseline.frame = svent->v.frame;
1089 svent->baseline.skin = svent->v.skin;
1090 if (entnum > 0 && entnum <= svs.maxclients)
1092 svent->baseline.colormap = entnum;
1093 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1097 svent->baseline.colormap = 0;
1098 svent->baseline.modelindex =
1099 SV_ModelIndex(pr_strings + svent->v.model);
1101 svent->baseline.alpha = 255;
1102 svent->baseline.scale = 16;
1103 svent->baseline.glowsize = 0;
1104 svent->baseline.glowcolor = 254;
1105 svent->baseline.colormod = 255;
1108 // add to the message
1110 MSG_WriteByte (&sv.signon,svc_spawnbaseline);
1111 MSG_WriteShort (&sv.signon,entnum);
1113 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1114 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1115 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1116 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1117 for (i=0 ; i<3 ; i++)
1119 MSG_WriteCoord(&sv.signon, svent->baseline.origin[i]);
1120 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1130 Tell all the clients that the server is changing levels
1133 void SV_SendReconnect (void)
1140 msg.maxsize = sizeof(data);
1142 MSG_WriteChar (&msg, svc_stufftext);
1143 MSG_WriteString (&msg, "reconnect\n");
1144 NET_SendToAll (&msg, 5);
1146 if (cls.state != ca_dedicated)
1147 Cmd_ExecuteString ("reconnect\n", src_command);
1155 Grabs the current state of each client for saving across the
1156 transition to another level
1159 void SV_SaveSpawnparms (void)
1163 svs.serverflags = pr_global_struct->serverflags;
1165 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1167 if (!host_client->active)
1170 // call the progs to get default spawn parms for the new client
1171 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1172 PR_ExecuteProgram (pr_global_struct->SetChangeParms);
1173 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1174 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1178 qboolean isworldmodel;
1184 This is called at the start of each level
1187 extern float scr_centertime_off;
1189 void SV_SpawnServer (char *server)
1194 // let's not have any servers with no name
1195 if (hostname.string[0] == 0)
1196 Cvar_Set ("hostname", "UNNAMED");
1197 scr_centertime_off = 0;
1199 Con_DPrintf ("SpawnServer: %s\n",server);
1200 svs.changelevel_issued = false; // now safe to issue another
1203 // tell all connected clients that we are going to a new level
1207 SV_SendReconnect ();
1211 // make cvars consistant
1214 Cvar_SetValue ("deathmatch", 0);
1215 current_skill = (int)(skill.value + 0.5);
1216 if (current_skill < 0)
1218 if (current_skill > 3)
1221 Cvar_SetValue ("skill", (float)current_skill);
1224 // set up the new server
1226 Host_ClearMemory ();
1228 memset (&sv, 0, sizeof(sv));
1230 strcpy (sv.name, server);
1232 // load progs to get entity field count
1235 // allocate server memory
1236 sv.max_edicts = MAX_EDICTS;
1238 sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1240 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1241 sv.datagram.cursize = 0;
1242 sv.datagram.data = sv.datagram_buf;
1244 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1245 sv.reliable_datagram.cursize = 0;
1246 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1248 sv.signon.maxsize = sizeof(sv.signon_buf);
1249 sv.signon.cursize = 0;
1250 sv.signon.data = sv.signon_buf;
1252 // leave slots at start for clients only
1253 sv.num_edicts = svs.maxclients+1;
1254 for (i=0 ; i<svs.maxclients ; i++)
1256 ent = EDICT_NUM(i+1);
1257 svs.clients[i].edict = ent;
1260 sv.state = ss_loading;
1265 strcpy (sv.name, server);
1266 sprintf (sv.modelname,"maps/%s.bsp", server);
1267 isworldmodel = true; // LordHavoc: only load submodels on the world model
1268 sv.worldmodel = Mod_ForName (sv.modelname, false);
1269 isworldmodel = false;
1272 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1276 sv.models[1] = sv.worldmodel;
1279 // clear world interaction links
1283 sv.sound_precache[0] = pr_strings;
1285 sv.model_precache[0] = pr_strings;
1286 sv.model_precache[1] = sv.modelname;
1287 for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1289 sv.model_precache[1+i] = localmodels[i];
1290 sv.models[i+1] = Mod_ForName (localmodels[i], false);
1294 // load the rest of the entities
1297 memset (&ent->v, 0, progs->entityfields * 4);
1299 ent->v.model = sv.worldmodel->name - pr_strings;
1300 ent->v.modelindex = 1; // world model
1301 ent->v.solid = SOLID_BSP;
1302 ent->v.movetype = MOVETYPE_PUSH;
1303 ent->v.angles[0] = ent->v.angles[1] = ent->v.angles[2] = 0;
1306 pr_global_struct->coop = coop.value;
1308 pr_global_struct->deathmatch = deathmatch.value;
1310 pr_global_struct->mapname = sv.name - pr_strings;
1312 // serverflags are for cross level information (sigils)
1313 pr_global_struct->serverflags = svs.serverflags;
1315 ED_LoadFromFile (sv.worldmodel->entities);
1316 // LordHavoc: clear world angles (to fix e3m3.bsp)
1317 sv.edicts->v.angles[0] = sv.edicts->v.angles[1] = sv.edicts->v.angles[2] = 0;
1321 // all setup is completed, any further precache statements are errors
1322 sv.state = ss_active;
1324 // run two frames to allow everything to settle
1325 host_frametime = 0.1;
1329 // create a baseline for more efficient communications
1330 SV_CreateBaseline ();
1332 // send serverinfo to all connected clients
1333 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1334 if (host_client->active)
1335 SV_SendServerinfo (host_client);
1337 Con_DPrintf ("Server spawned.\n");
1340 // LordHavoc: added light checking to the server
1341 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end);
1342 void SV_LightPoint (vec3_t color, vec3_t p)
1346 if (!sv.worldmodel->lightdata)
1348 color[0] = color[1] = color[2] = 255;
1354 end[2] = p[2] - 2048;
1356 color[0] = color[1] = color[2] = 0;
1357 RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);