This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/server/sv_send.c

565 lines
13 KiB
C
Raw Normal View History

2007-06-21 22:00:00 +02:00
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// sv_main.c -- server main program
#include "engine.h"
#include "server.h"
/*
=============================================================================
2007-07-28 22:00:00 +02:00
Msg redirection
2007-06-21 22:00:00 +02:00
=============================================================================
*/
char sv_outputbuf[SV_OUTPUTBUF_LENGTH];
void SV_FlushRedirect (int sv_redirected, char *outputbuf)
{
if (sv_redirected == RD_PACKET)
{
Netchan_OutOfBandPrint (NS_SERVER, net_from, "print\n%s", outputbuf);
}
else if (sv_redirected == RD_CLIENT)
{
MSG_WriteByte (&sv_client->netchan.message, svc_print);
MSG_WriteByte (&sv_client->netchan.message, PRINT_HIGH);
MSG_WriteString (&sv_client->netchan.message, outputbuf);
}
}
/*
=============================================================================
EVENT MESSAGES
=============================================================================
*/
/*
=================
SV_ClientPrintf
Sends text across to be displayed if the level passes
=================
*/
void SV_ClientPrintf (client_t *cl, int level, char *fmt, ...)
{
va_list argptr;
char string[1024];
if (level < cl->messagelevel)
return;
va_start (argptr,fmt);
vsprintf (string, fmt,argptr);
va_end (argptr);
MSG_WriteByte (&cl->netchan.message, svc_print);
MSG_WriteByte (&cl->netchan.message, level);
MSG_WriteString (&cl->netchan.message, string);
}
/*
=================
SV_BroadcastPrintf
Sends text to all active clients
=================
*/
void SV_BroadcastPrintf (int level, char *fmt, ...)
{
va_list argptr;
char string[2048];
client_t *cl;
int i;
va_start (argptr,fmt);
vsprintf (string, fmt,argptr);
va_end (argptr);
// echo to console
2007-09-06 22:00:00 +02:00
if (dedicated->value)
2007-06-21 22:00:00 +02:00
{
char copy[1024];
int i;
// mask off high bits
2007-09-06 22:00:00 +02:00
for (i=0 ; i<1023 && string[i] ; i++)
copy[i] = string[i]&127;
copy[i] = 0;
2007-07-28 22:00:00 +02:00
Msg ("%s", copy);
2007-06-21 22:00:00 +02:00
}
2007-09-06 22:00:00 +02:00
for (i=0, cl = svs.clients ; i<maxclients->value; i++, cl++)
2007-06-21 22:00:00 +02:00
{
if (level < cl->messagelevel)
continue;
if (cl->state != cs_spawned)
continue;
MSG_WriteByte (&cl->netchan.message, svc_print);
MSG_WriteByte (&cl->netchan.message, level);
MSG_WriteString (&cl->netchan.message, string);
}
}
/*
=================
SV_BroadcastCommand
Sends text to all active clients
=================
*/
void SV_BroadcastCommand (char *fmt, ...)
{
va_list argptr;
char string[1024];
2007-09-04 22:00:00 +02:00
if (!sv.state) return;
2007-06-21 22:00:00 +02:00
va_start (argptr,fmt);
vsprintf (string, fmt,argptr);
va_end (argptr);
2007-08-19 22:00:00 +02:00
MSG_Begin(svc_stufftext);
2007-06-21 22:00:00 +02:00
MSG_WriteString (&sv.multicast, string);
2007-08-19 22:00:00 +02:00
MSG_Send(MSG_ALL_R, NULL, NULL);
2007-06-21 22:00:00 +02:00
}
/*
=================
2007-08-19 22:00:00 +02:00
SV_Send
2007-06-21 22:00:00 +02:00
Sends the contents of sv.multicast to a subset of the clients,
then clears sv.multicast.
2007-08-19 22:00:00 +02:00
MULTICAST_ONE send to one client (ent can't be NULL)
2007-06-21 22:00:00 +02:00
MULTICAST_ALL same as broadcast (origin can be NULL)
MULTICAST_PVS send to clients potentially visible from org
MULTICAST_PHS send to clients potentially hearable from org
=================
*/
2007-09-06 22:00:00 +02:00
void _MSG_Send (msgtype_t to, vec3_t origin, edict_t *ent, const char *filename, int fileline)
2007-06-21 22:00:00 +02:00
{
2007-08-19 22:00:00 +02:00
byte *mask = NULL;
int leafnum = 0, cluster = 0;
int area1 = 0, area2 = 0;
2007-09-06 22:00:00 +02:00
int j, numclients = maxclients->value;
2007-08-19 22:00:00 +02:00
client_t *client, *current = svs.clients;
bool reliable = false;
/*if(origin == NULL || ent == NULL)
2007-06-21 22:00:00 +02:00
{
2007-08-19 22:00:00 +02:00
if(to == MSG_ONE || to == MSG_ONE_R)
Msg("MSG_Send: ent == NULL (called at %s:%i)\n", filename, fileline);
else Msg("MSG_Send: origin == NULL (called at %s:%i)\n", filename, fileline);
return;
}*/
2007-06-21 22:00:00 +02:00
// if doing a serverrecord, store everything
2007-08-19 22:00:00 +02:00
if (svs.demofile) SZ_Write (&svs.demo_multicast, sv.multicast.data, sv.multicast.cursize);
2007-06-21 22:00:00 +02:00
switch (to)
{
2007-07-21 22:00:00 +02:00
case MSG_ALL_R:
2007-06-21 22:00:00 +02:00
reliable = true; // intentional fallthrough
2007-07-21 22:00:00 +02:00
case MSG_ALL:
2007-08-19 22:00:00 +02:00
// nothing to sort
2007-06-21 22:00:00 +02:00
break;
2007-07-21 22:00:00 +02:00
case MSG_PHS_R:
2007-06-21 22:00:00 +02:00
reliable = true; // intentional fallthrough
2007-07-21 22:00:00 +02:00
case MSG_PHS:
2007-08-19 22:00:00 +02:00
if(origin == NULL) return;
2007-06-21 22:00:00 +02:00
leafnum = CM_PointLeafnum (origin);
cluster = CM_LeafCluster (leafnum);
mask = CM_ClusterPHS (cluster);
2007-08-19 22:00:00 +02:00
area1 = CM_LeafArea (leafnum);
2007-06-21 22:00:00 +02:00
break;
2007-07-21 22:00:00 +02:00
case MSG_PVS_R:
2007-06-21 22:00:00 +02:00
reliable = true; // intentional fallthrough
2007-07-21 22:00:00 +02:00
case MSG_PVS:
2007-08-19 22:00:00 +02:00
if(origin == NULL) return;
2007-06-21 22:00:00 +02:00
leafnum = CM_PointLeafnum (origin);
cluster = CM_LeafCluster (leafnum);
mask = CM_ClusterPVS (cluster);
2007-08-19 22:00:00 +02:00
area1 = CM_LeafArea (leafnum);
2007-06-21 22:00:00 +02:00
break;
2007-08-19 22:00:00 +02:00
case MSG_ONE_R:
reliable = true; // intentional fallthrough
2007-07-21 22:00:00 +02:00
case MSG_ONE:
2007-08-19 22:00:00 +02:00
if(ent == NULL) return;
2007-09-06 22:00:00 +02:00
j = NUM_FOR_EDICT(ent);
2007-08-19 22:00:00 +02:00
if (j < 1 || j > numclients) return;
current = svs.clients + (j - 1);
numclients = 1; // send to one
2007-07-21 22:00:00 +02:00
break;
2007-06-21 22:00:00 +02:00
default:
2007-08-19 22:00:00 +02:00
MsgWarn("MSG_Send: bad destination: %i (called at %s:%i)\n", to, filename, fileline);
return;
2007-06-21 22:00:00 +02:00
}
2007-08-19 22:00:00 +02:00
// send the data to all relevent clients (or once only)
for (j = 0, client = current; j < numclients; j++, client++)
2007-06-21 22:00:00 +02:00
{
2007-08-19 22:00:00 +02:00
if (client->state == cs_free || client->state == cs_zombie) continue;
if (client->state != cs_spawned && !reliable) continue;
2007-06-21 22:00:00 +02:00
if (mask)
{
area2 = CM_LeafArea (leafnum);
2007-08-19 22:00:00 +02:00
cluster = CM_LeafCluster (leafnum);
2007-09-06 22:00:00 +02:00
leafnum = CM_PointLeafnum (client->edict->s.origin);
2007-08-19 22:00:00 +02:00
if (!CM_AreasConnected (area1, area2)) continue;
2007-09-06 22:00:00 +02:00
if ( mask && (!(mask[cluster>>3] & (1<<(cluster&7))))) continue;
2007-06-21 22:00:00 +02:00
}
2007-08-19 22:00:00 +02:00
if (reliable) SZ_Write (&client->netchan.message, sv.multicast.data, sv.multicast.cursize);
else SZ_Write (&client->datagram, sv.multicast.data, sv.multicast.cursize);
2007-06-21 22:00:00 +02:00
}
SZ_Clear (&sv.multicast);
}
2007-08-19 22:00:00 +02:00
/*
=================
MSG_Begin
Misc helper function
=================
*/
void _MSG_Begin( int dest, const char *filename, int fileline )
{
_MSG_WriteChar (&sv.multicast, dest, filename, fileline );
}
2007-06-21 22:00:00 +02:00
/*
==================
SV_StartSound
Each entity can have eight independant sound sources, like voice,
weapon, feet, etc.
If cahnnel & 8, the sound will be sent to everyone, not just
things in the PHS.
FIXME: if entity isn't in PHS, they must be forced to be sent or
have the origin explicitly sent.
Channel 0 is an auto-allocate channel, the others override anything
already running on that entity/channel pair.
An attenuation of 0 will play full volume everywhere in the level.
Larger attenuations will drop off. (max 4 attenuation)
Timeofs can range from 0.0 to 0.1 to cause sounds to be started
later in the frame than they normally would.
If origin is NULL, the origin is determined from the entity origin
or the midpoint of the entity box for bmodels.
==================
*/
2007-09-06 22:00:00 +02:00
void SV_StartSound (vec3_t origin, edict_t *entity, int channel, int soundindex, float volume, float attenuation, float timeofs)
2007-06-21 22:00:00 +02:00
{
2007-08-19 22:00:00 +02:00
int i, ent, flags, sendchan;
2007-06-21 22:00:00 +02:00
vec3_t origin_v;
2007-08-19 22:00:00 +02:00
bool use_phs;
2007-06-21 22:00:00 +02:00
if (volume < 0 || volume > 1.0)
2007-08-19 22:00:00 +02:00
{
MsgWarn("SV_StartSound: volume = %f\n", volume);
volume = bound(0, volume, 1.0);
}
2007-06-21 22:00:00 +02:00
if (attenuation < 0 || attenuation > 4)
2007-08-19 22:00:00 +02:00
{
MsgWarn("SV_StartSound: attenuation = %f\n", attenuation);
attenuation = bound(0, volume, 4);
}
2007-06-21 22:00:00 +02:00
if (timeofs < 0 || timeofs > 0.255)
2007-08-19 22:00:00 +02:00
{
MsgWarn("SV_StartSound: timeofs = %f\n", timeofs);
timeofs = bound(0, timeofs, 0.255 );
}
2007-09-06 22:00:00 +02:00
ent = NUM_FOR_EDICT(entity);
2007-06-21 22:00:00 +02:00
2007-08-19 22:00:00 +02:00
if (channel & 8) // no PHS flag
2007-06-21 22:00:00 +02:00
{
use_phs = false;
channel &= 7;
}
2007-08-19 22:00:00 +02:00
else use_phs = true;
2007-06-21 22:00:00 +02:00
sendchan = (ent<<3) | (channel&7);
flags = 0;
2007-08-19 22:00:00 +02:00
if (volume != DEFAULT_SOUND_PACKET_VOLUME) flags |= SND_VOLUME;
if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION) flags |= SND_ATTENUATION;
2007-06-21 22:00:00 +02:00
// the client doesn't know that bmodels have weird origins
// the origin can also be explicitly set
2007-09-06 22:00:00 +02:00
if ( (entity->svflags & SVF_NOCLIENT) || (entity->solid == SOLID_BSP) || origin )
2007-06-21 22:00:00 +02:00
flags |= SND_POS;
// always send the entity number for channel overrides
flags |= SND_ENT;
2007-08-19 22:00:00 +02:00
if (timeofs) flags |= SND_OFFSET;
2007-06-21 22:00:00 +02:00
// use the entity origin unless it is a bmodel or explicitly specified
if (!origin)
{
origin = origin_v;
2007-09-06 22:00:00 +02:00
if (entity->solid == SOLID_BSP)
2007-06-21 22:00:00 +02:00
{
2007-08-19 22:00:00 +02:00
for (i = 0; i < 3; i++)
{
2007-09-06 22:00:00 +02:00
origin_v[i] = entity->s.origin[i]+0.5*(entity->mins[i]+entity->maxs[i]);
2007-08-19 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
}
else
{
2007-09-06 22:00:00 +02:00
VectorCopy (entity->s.origin, origin_v);
2007-06-21 22:00:00 +02:00
}
}
2007-08-19 22:00:00 +02:00
MSG_Begin(svc_sound);
2007-06-21 22:00:00 +02:00
MSG_WriteByte (&sv.multicast, flags);
MSG_WriteByte (&sv.multicast, soundindex);
2007-08-19 22:00:00 +02:00
if (flags & SND_VOLUME) MSG_WriteByte (&sv.multicast, volume*255);
if (flags & SND_ATTENUATION) MSG_WriteByte (&sv.multicast, attenuation*64);
if (flags & SND_OFFSET) MSG_WriteByte (&sv.multicast, timeofs*1000);
if (flags & SND_ENT) MSG_WriteShort (&sv.multicast, sendchan);
if (flags & SND_POS) MSG_WritePos (&sv.multicast, origin);
2007-06-21 22:00:00 +02:00
// if the sound doesn't attenuate,send it to everyone
// (global radio chatter, voiceovers, etc)
2007-08-19 22:00:00 +02:00
if (attenuation == ATTN_NONE) use_phs = false;
2007-06-21 22:00:00 +02:00
if (channel & CHAN_RELIABLE)
{
2007-08-19 22:00:00 +02:00
if(use_phs)
{
MSG_Send (MSG_PHS_R, origin, NULL);
}
else
{
MSG_Send(MSG_ALL_R, origin, NULL );
}
2007-06-21 22:00:00 +02:00
}
else
{
2007-08-19 22:00:00 +02:00
if(use_phs)
{
MSG_Send (MSG_PHS, origin, NULL);
}
else
{
MSG_Send (MSG_ALL, origin, NULL);
}
2007-06-21 22:00:00 +02:00
}
}
/*
===============================================================================
FRAME UPDATES
===============================================================================
*/
/*
=======================
SV_SendClientDatagram
=======================
*/
bool SV_SendClientDatagram (client_t *client)
{
byte msg_buf[MAX_MSGLEN];
2007-08-11 22:00:00 +02:00
sizebuf_t msg;
2007-06-21 22:00:00 +02:00
SV_BuildClientFrame (client);
SZ_Init (&msg, msg_buf, sizeof(msg_buf));
// send over all the relevant entity_state_t
// and the player_state_t
SV_WriteFrameToClient (client, &msg);
// copy the accumulated multicast datagram
// for this client out to the message
// it is necessary for this to be after the WriteEntities
// so that entity references will be current
if (client->datagram.overflowed)
2007-07-28 22:00:00 +02:00
Msg ("WARNING: datagram overflowed for %s\n", client->name);
2007-06-21 22:00:00 +02:00
else
SZ_Write (&msg, client->datagram.data, client->datagram.cursize);
SZ_Clear (&client->datagram);
if (msg.overflowed)
2007-09-06 22:00:00 +02:00
{ // must have room left for the packet header
2007-07-28 22:00:00 +02:00
Msg ("WARNING: msg overflowed for %s\n", client->name);
2007-06-21 22:00:00 +02:00
SZ_Clear (&msg);
}
// send the datagram
Netchan_Transmit (&client->netchan, msg.cursize, msg.data);
// record the size for rate estimation
client->message_size[sv.framenum % RATE_MESSAGES] = msg.cursize;
return true;
}
/*
==================
SV_DemoCompleted
==================
*/
void SV_DemoCompleted (void)
{
if (sv.demofile)
{
FS_Close (sv.demofile);
sv.demofile = NULL;
}
SV_Nextserver ();
}
/*
=======================
SV_RateDrop
Returns true if the client is over its current
bandwidth estimation and should not be sent another packet
=======================
*/
bool SV_RateDrop (client_t *c)
{
int total;
int i;
// never drop over the loopback
if (c->netchan.remote_address.type == NA_LOOPBACK)
return false;
total = 0;
for (i = 0 ; i < RATE_MESSAGES ; i++)
{
total += c->message_size[i];
}
if (total > c->rate)
{
c->surpressCount++;
c->message_size[sv.framenum % RATE_MESSAGES] = 0;
return true;
}
return false;
}
/*
=======================
SV_SendClientMessages
=======================
*/
void SV_SendClientMessages (void)
{
2007-09-06 22:00:00 +02:00
int i;
client_t *c;
int msglen;
2007-06-21 22:00:00 +02:00
byte msgbuf[MAX_MSGLEN];
msglen = 0;
// read the next demo message if needed
if (sv.state == ss_demo && sv.demofile)
{
2007-09-06 22:00:00 +02:00
if (sv_paused->value)
msglen = 0;
2007-06-21 22:00:00 +02:00
else
{
// get the next message
if(!FS_Read (sv.demofile, &msglen, 4))
{
SV_DemoCompleted ();
return;
}
msglen = LittleLong (msglen);
if (msglen == -1)
{
SV_DemoCompleted ();
return;
}
2007-09-10 22:00:00 +02:00
if (msglen > MAX_MSGLEN) Host_Error("SV_SendClientMessages: msglen > MAX_MSGLEN\n");
2007-06-21 22:00:00 +02:00
if(!FS_Read (sv.demofile, msgbuf, msglen))
{
SV_DemoCompleted ();
return;
}
}
}
// send a message to each connected client
2007-09-06 22:00:00 +02:00
for (i=0, c = svs.clients ; i<maxclients->value; i++, c++)
2007-06-21 22:00:00 +02:00
{
2007-09-06 22:00:00 +02:00
if (!c->state)
continue;
// if the reliable message overflowed,
// drop the client
if (c->netchan.message.overflowed)
2007-06-21 22:00:00 +02:00
{
2007-09-06 22:00:00 +02:00
SZ_Clear (&c->netchan.message);
SZ_Clear (&c->datagram);
SV_BroadcastPrintf (PRINT_HIGH, "%s overflowed\n", c->name);
SV_DropClient (c);
2007-06-21 22:00:00 +02:00
}
2007-09-06 22:00:00 +02:00
if (sv.state == ss_cinematic
|| sv.state == ss_demo
|| sv.state == ss_pic
)
Netchan_Transmit (&c->netchan, msglen, msgbuf);
else if (c->state == cs_spawned)
2007-06-21 22:00:00 +02:00
{
// don't overrun bandwidth
2007-09-06 22:00:00 +02:00
if (SV_RateDrop (c))
continue;
SV_SendClientDatagram (c);
2007-06-21 22:00:00 +02:00
}
else
{
2007-09-07 22:00:00 +02:00
// just update reliable if needed
2007-09-09 22:00:00 +02:00
if (c->netchan.message.cursize || host.realtime - c->netchan.last_sent > 1.0f )
2007-09-06 22:00:00 +02:00
Netchan_Transmit (&c->netchan, 0, NULL);
2007-06-21 22:00:00 +02:00
}
}
}