📄 sv_snapshot.c
字号:
// entities can be flagged to be sent to a given mask of clients
if ( ent->r.svFlags & SVF_CLIENTMASK ) {
if (frame->ps.clientNum >= 32)
Com_Error( ERR_DROP, "SVF_CLIENTMASK: cientNum > 32\n" );
if (~ent->r.singleClient & (1 << frame->ps.clientNum))
continue;
}
svEnt = SV_SvEntityForGentity( ent );
// don't double add an entity through portals
if ( svEnt->snapshotCounter == sv.snapshotCounter ) {
continue;
}
// broadcast entities are always sent
if ( ent->r.svFlags & SVF_BROADCAST ) {
SV_AddEntToSnapshot( svEnt, ent, eNums );
continue;
}
// ignore if not touching a PV leaf
// check area
if ( !CM_AreasConnected( clientarea, svEnt->areanum ) ) {
// doors can legally straddle two areas, so
// we may need to check another one
if ( !CM_AreasConnected( clientarea, svEnt->areanum2 ) ) {
continue; // blocked by a door
}
}
bitvector = clientpvs;
// check individual leafs
if ( !svEnt->numClusters ) {
continue;
}
l = 0;
for ( i=0 ; i < svEnt->numClusters ; i++ ) {
l = svEnt->clusternums[i];
if ( bitvector[l >> 3] & (1 << (l&7) ) ) {
break;
}
}
// if we haven't found it to be visible,
// check overflow clusters that coudln't be stored
if ( i == svEnt->numClusters ) {
if ( svEnt->lastCluster ) {
for ( ; l <= svEnt->lastCluster ; l++ ) {
if ( bitvector[l >> 3] & (1 << (l&7) ) ) {
break;
}
}
if ( l == svEnt->lastCluster ) {
continue; // not visible
}
} else {
continue;
}
}
// add it
SV_AddEntToSnapshot( svEnt, ent, eNums );
// if its a portal entity, add everything visible from its camera position
if ( ent->r.svFlags & SVF_PORTAL ) {
if ( ent->s.generic1 ) {
vec3_t dir;
VectorSubtract(ent->s.origin, origin, dir);
if ( VectorLengthSquared(dir) > (float) ent->s.generic1 * ent->s.generic1 ) {
continue;
}
}
SV_AddEntitiesVisibleFromPoint( ent->s.origin2, frame, eNums, qtrue );
}
}
}
/*
=============
SV_BuildClientSnapshot
Decides which entities are going to be visible to the client, and
copies off the playerstate and areabits.
This properly handles multiple recursive portals, but the render
currently doesn't.
For viewing through other player's eyes, clent can be something other than client->gentity
=============
*/
static void SV_BuildClientSnapshot( client_t *client ) {
vec3_t org;
clientSnapshot_t *frame;
snapshotEntityNumbers_t entityNumbers;
int i;
sharedEntity_t *ent;
entityState_t *state;
svEntity_t *svEnt;
sharedEntity_t *clent;
int clientNum;
playerState_t *ps;
// bump the counter used to prevent double adding
sv.snapshotCounter++;
// this is the frame we are creating
frame = &client->frames[ client->netchan.outgoingSequence & PACKET_MASK ];
// clear everything in this snapshot
entityNumbers.numSnapshotEntities = 0;
Com_Memset( frame->areabits, 0, sizeof( frame->areabits ) );
// https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=62
frame->num_entities = 0;
clent = client->gentity;
if ( !clent || client->state == CS_ZOMBIE ) {
return;
}
// grab the current playerState_t
ps = SV_GameClientNum( client - svs.clients );
frame->ps = *ps;
// never send client's own entity, because it can
// be regenerated from the playerstate
clientNum = frame->ps.clientNum;
if ( clientNum < 0 || clientNum >= MAX_GENTITIES ) {
Com_Error( ERR_DROP, "SV_SvEntityForGentity: bad gEnt" );
}
svEnt = &sv.svEntities[ clientNum ];
svEnt->snapshotCounter = sv.snapshotCounter;
// find the client's viewpoint
VectorCopy( ps->origin, org );
org[2] += ps->viewheight;
// add all the entities directly visible to the eye, which
// may include portal entities that merge other viewpoints
SV_AddEntitiesVisibleFromPoint( org, frame, &entityNumbers, qfalse );
// if there were portals visible, there may be out of order entities
// in the list which will need to be resorted for the delta compression
// to work correctly. This also catches the error condition
// of an entity being included twice.
qsort( entityNumbers.snapshotEntities, entityNumbers.numSnapshotEntities,
sizeof( entityNumbers.snapshotEntities[0] ), SV_QsortEntityNumbers );
// now that all viewpoint's areabits have been OR'd together, invert
// all of them to make it a mask vector, which is what the renderer wants
for ( i = 0 ; i < MAX_MAP_AREA_BYTES/4 ; i++ ) {
((int *)frame->areabits)[i] = ((int *)frame->areabits)[i] ^ -1;
}
// copy the entity states out
frame->num_entities = 0;
frame->first_entity = svs.nextSnapshotEntities;
for ( i = 0 ; i < entityNumbers.numSnapshotEntities ; i++ ) {
ent = SV_GentityNum(entityNumbers.snapshotEntities[i]);
state = &svs.snapshotEntities[svs.nextSnapshotEntities % svs.numSnapshotEntities];
*state = ent->s;
svs.nextSnapshotEntities++;
// this should never hit, map should always be restarted first in SV_Frame
if ( svs.nextSnapshotEntities >= 0x7FFFFFFE ) {
Com_Error(ERR_FATAL, "svs.nextSnapshotEntities wrapped");
}
frame->num_entities++;
}
}
/*
====================
SV_RateMsec
Return the number of msec a given size message is supposed
to take to clear, based on the current rate
====================
*/
#define HEADER_RATE_BYTES 48 // include our header, IP header, and some overhead
static int SV_RateMsec( client_t *client, int messageSize ) {
int rate;
int rateMsec;
// individual messages will never be larger than fragment size
if ( messageSize > 1500 ) {
messageSize = 1500;
}
rate = client->rate;
if ( sv_maxRate->integer ) {
if ( sv_maxRate->integer < 1000 ) {
Cvar_Set( "sv_MaxRate", "1000" );
}
if ( sv_maxRate->integer < rate ) {
rate = sv_maxRate->integer;
}
}
rateMsec = ( messageSize + HEADER_RATE_BYTES ) * 1000 / rate;
return rateMsec;
}
/*
=======================
SV_SendMessageToClient
Called by SV_SendClientSnapshot and SV_SendClientGameState
=======================
*/
void SV_SendMessageToClient( msg_t *msg, client_t *client ) {
int rateMsec;
// record information about the message
client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSize = msg->cursize;
client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSent = svs.time;
client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageAcked = -1;
// send the datagram
SV_Netchan_Transmit( client, msg ); //msg->cursize, msg->data );
// set nextSnapshotTime based on rate and requested number of updates
// local clients get snapshots every frame
// TTimo - https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=491
// added sv_lanForceRate check
if ( client->netchan.remoteAddress.type == NA_LOOPBACK || (sv_lanForceRate->integer && Sys_IsLANAddress (client->netchan.remoteAddress)) ) {
client->nextSnapshotTime = svs.time - 1;
return;
}
// normal rate / snapshotMsec calculation
rateMsec = SV_RateMsec( client, msg->cursize );
if ( rateMsec < client->snapshotMsec ) {
// never send more packets than this, no matter what the rate is at
rateMsec = client->snapshotMsec;
client->rateDelayed = qfalse;
} else {
client->rateDelayed = qtrue;
}
client->nextSnapshotTime = svs.time + rateMsec;
// don't pile up empty snapshots while connecting
if ( client->state != CS_ACTIVE ) {
// a gigantic connection message may have already put the nextSnapshotTime
// more than a second away, so don't shorten it
// do shorten if client is downloading
if ( !*client->downloadName && client->nextSnapshotTime < svs.time + 1000 ) {
client->nextSnapshotTime = svs.time + 1000;
}
}
}
/*
=======================
SV_SendClientSnapshot
Also called by SV_FinalMessage
=======================
*/
void SV_SendClientSnapshot( client_t *client ) {
byte msg_buf[MAX_MSGLEN];
msg_t msg;
// build the snapshot
SV_BuildClientSnapshot( client );
// bots need to have their snapshots build, but
// the query them directly without needing to be sent
if ( client->gentity && client->gentity->r.svFlags & SVF_BOT ) {
return;
}
MSG_Init (&msg, msg_buf, sizeof(msg_buf));
msg.allowoverflow = qtrue;
// NOTE, MRE: all server->client messages now acknowledge
// let the client know which reliable clientCommands we have received
MSG_WriteLong( &msg, client->lastClientCommand );
// (re)send any reliable server commands
SV_UpdateServerCommandsToClient( client, &msg );
// send over all the relevant entityState_t
// and the playerState_t
SV_WriteSnapshotToClient( client, &msg );
// Add any download data if the client is downloading
SV_WriteDownloadToClient( client, &msg );
// check for overflow
if ( msg.overflowed ) {
Com_Printf ("WARNING: msg overflowed for %s\n", client->name);
MSG_Clear (&msg);
}
SV_SendMessageToClient( &msg, client );
}
/*
=======================
SV_SendClientMessages
=======================
*/
void SV_SendClientMessages( void ) {
int i;
client_t *c;
// send a message to each connected client
for (i=0, c = svs.clients ; i < sv_maxclients->integer ; i++, c++) {
if (!c->state) {
continue; // not connected
}
if ( svs.time < c->nextSnapshotTime ) {
continue; // not time yet
}
// send additional message fragments if the last message
// was too large to send at once
if ( c->netchan.unsentFragments ) {
c->nextSnapshotTime = svs.time +
SV_RateMsec( c, c->netchan.unsentLength - c->netchan.unsentFragmentStart );
SV_Netchan_TransmitNextFragment( c );
continue;
}
// generate and send a new message
SV_SendClientSnapshot( c );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -