📄 sv_snapshot.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "server.h"
/*
=============================================================================
Delta encode a client frame onto the network channel
A normal server packet will look like:
4 sequence number (high bit set if an oversize fragment)
<optional reliable commands>
1 svc_snapshot
4 last client reliable command
4 serverTime
1 lastframe for delta compression
1 snapFlags
1 areaBytes
<areabytes>
<playerstate>
<packetentities>
=============================================================================
*/
/*
=============
SV_EmitPacketEntities
Writes a delta update of an entityState_t list to the message.
=============
*/
static void SV_EmitPacketEntities( clientSnapshot_t *from, clientSnapshot_t *to, msg_t *msg ) {
entityState_t *oldent, *newent;
int oldindex, newindex;
int oldnum, newnum;
int from_num_entities;
// generate the delta update
if ( !from ) {
from_num_entities = 0;
} else {
from_num_entities = from->num_entities;
}
newent = NULL;
oldent = NULL;
newindex = 0;
oldindex = 0;
while ( newindex < to->num_entities || oldindex < from_num_entities ) {
if ( newindex >= to->num_entities ) {
newnum = 9999;
} else {
newent = &svs.snapshotEntities[(to->first_entity+newindex) % svs.numSnapshotEntities];
newnum = newent->number;
}
if ( oldindex >= from_num_entities ) {
oldnum = 9999;
} else {
oldent = &svs.snapshotEntities[(from->first_entity+oldindex) % svs.numSnapshotEntities];
oldnum = oldent->number;
}
if ( newnum == oldnum ) {
// delta update from old position
// because the force parm is qfalse, this will not result
// in any bytes being emited if the entity has not changed at all
MSG_WriteDeltaEntity (msg, oldent, newent, qfalse );
oldindex++;
newindex++;
continue;
}
if ( newnum < oldnum ) {
// this is a new entity, send it from the baseline
MSG_WriteDeltaEntity (msg, &sv.svEntities[newnum].baseline, newent, qtrue );
newindex++;
continue;
}
if ( newnum > oldnum ) {
// the old entity isn't present in the new message
MSG_WriteDeltaEntity (msg, oldent, NULL, qtrue );
oldindex++;
continue;
}
}
MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); // end of packetentities
}
/*
==================
SV_WriteSnapshotToClient
==================
*/
static void SV_WriteSnapshotToClient( client_t *client, msg_t *msg ) {
clientSnapshot_t *frame, *oldframe;
int lastframe;
int i;
int snapFlags;
// this is the snapshot we are creating
frame = &client->frames[ client->netchan.outgoingSequence & PACKET_MASK ];
// try to use a previous frame as the source for delta compressing the snapshot
if ( client->deltaMessage <= 0 || client->state != CS_ACTIVE ) {
// client is asking for a retransmit
oldframe = NULL;
lastframe = 0;
} else if ( client->netchan.outgoingSequence - client->deltaMessage
>= (PACKET_BACKUP - 3) ) {
// client hasn't gotten a good message through in a long time
Com_DPrintf ("%s: Delta request from out of date packet.\n", client->name);
oldframe = NULL;
lastframe = 0;
} else {
// we have a valid snapshot to delta from
oldframe = &client->frames[ client->deltaMessage & PACKET_MASK ];
lastframe = client->netchan.outgoingSequence - client->deltaMessage;
// the snapshot's entities may still have rolled off the buffer, though
if ( oldframe->first_entity <= svs.nextSnapshotEntities - svs.numSnapshotEntities ) {
Com_DPrintf ("%s: Delta request from out of date entities.\n", client->name);
oldframe = NULL;
lastframe = 0;
}
}
MSG_WriteByte (msg, svc_snapshot);
// NOTE, MRE: now sent at the start of every message from server to client
// let the client know which reliable clientCommands we have received
//MSG_WriteLong( msg, client->lastClientCommand );
// send over the current server time so the client can drift
// its view of time to try to match
MSG_WriteLong (msg, svs.time);
// what we are delta'ing from
MSG_WriteByte (msg, lastframe);
snapFlags = svs.snapFlagServerBit;
if ( client->rateDelayed ) {
snapFlags |= SNAPFLAG_RATE_DELAYED;
}
if ( client->state != CS_ACTIVE ) {
snapFlags |= SNAPFLAG_NOT_ACTIVE;
}
MSG_WriteByte (msg, snapFlags);
// send over the areabits
MSG_WriteByte (msg, frame->areabytes);
MSG_WriteData (msg, frame->areabits, frame->areabytes);
// delta encode the playerstate
if ( oldframe ) {
MSG_WriteDeltaPlayerstate( msg, &oldframe->ps, &frame->ps );
} else {
MSG_WriteDeltaPlayerstate( msg, NULL, &frame->ps );
}
// delta encode the entities
SV_EmitPacketEntities (oldframe, frame, msg);
// padding for rate debugging
if ( sv_padPackets->integer ) {
for ( i = 0 ; i < sv_padPackets->integer ; i++ ) {
MSG_WriteByte (msg, svc_nop);
}
}
}
/*
==================
SV_UpdateServerCommandsToClient
(re)send all server commands the client hasn't acknowledged yet
==================
*/
void SV_UpdateServerCommandsToClient( client_t *client, msg_t *msg ) {
int i;
// write any unacknowledged serverCommands
for ( i = client->reliableAcknowledge + 1 ; i <= client->reliableSequence ; i++ ) {
MSG_WriteByte( msg, svc_serverCommand );
MSG_WriteLong( msg, i );
MSG_WriteString( msg, client->reliableCommands[ i & (MAX_RELIABLE_COMMANDS-1) ] );
}
client->reliableSent = client->reliableSequence;
}
/*
=============================================================================
Build a client snapshot structure
=============================================================================
*/
#define MAX_SNAPSHOT_ENTITIES 1024
typedef struct {
int numSnapshotEntities;
int snapshotEntities[MAX_SNAPSHOT_ENTITIES];
} snapshotEntityNumbers_t;
/*
=======================
SV_QsortEntityNumbers
=======================
*/
static int QDECL SV_QsortEntityNumbers( const void *a, const void *b ) {
int *ea, *eb;
ea = (int *)a;
eb = (int *)b;
if ( *ea == *eb ) {
Com_Error( ERR_DROP, "SV_QsortEntityStates: duplicated entity" );
}
if ( *ea < *eb ) {
return -1;
}
return 1;
}
/*
===============
SV_AddEntToSnapshot
===============
*/
static void SV_AddEntToSnapshot( svEntity_t *svEnt, sharedEntity_t *gEnt, snapshotEntityNumbers_t *eNums ) {
// if we have already added this entity to this snapshot, don't add again
if ( svEnt->snapshotCounter == sv.snapshotCounter ) {
return;
}
svEnt->snapshotCounter = sv.snapshotCounter;
// if we are full, silently discard entities
if ( eNums->numSnapshotEntities == MAX_SNAPSHOT_ENTITIES ) {
return;
}
eNums->snapshotEntities[ eNums->numSnapshotEntities ] = gEnt->s.number;
eNums->numSnapshotEntities++;
}
/*
===============
SV_AddEntitiesVisibleFromPoint
===============
*/
static void SV_AddEntitiesVisibleFromPoint( vec3_t origin, clientSnapshot_t *frame,
snapshotEntityNumbers_t *eNums, qboolean portal ) {
int e, i;
sharedEntity_t *ent;
svEntity_t *svEnt;
int l;
int clientarea, clientcluster;
int leafnum;
int c_fullsend;
byte *clientpvs;
byte *bitvector;
// during an error shutdown message we may need to transmit
// the shutdown message after the server has shutdown, so
// specfically check for it
if ( !sv.state ) {
return;
}
leafnum = CM_PointLeafnum (origin);
clientarea = CM_LeafArea (leafnum);
clientcluster = CM_LeafCluster (leafnum);
// calculate the visible areas
frame->areabytes = CM_WriteAreaBits( frame->areabits, clientarea );
clientpvs = CM_ClusterPVS (clientcluster);
c_fullsend = 0;
for ( e = 0 ; e < sv.num_entities ; e++ ) {
ent = SV_GentityNum(e);
// never send entities that aren't linked in
if ( !ent->r.linked ) {
continue;
}
if (ent->s.number != e) {
Com_DPrintf ("FIXING ENT->S.NUMBER!!!\n");
ent->s.number = e;
}
// entities can be flagged to explicitly not be sent to the client
if ( ent->r.svFlags & SVF_NOCLIENT ) {
continue;
}
// entities can be flagged to be sent to only one client
if ( ent->r.svFlags & SVF_SINGLECLIENT ) {
if ( ent->r.singleClient != frame->ps.clientNum ) {
continue;
}
}
// entities can be flagged to be sent to everyone but one client
if ( ent->r.svFlags & SVF_NOTSINGLECLIENT ) {
if ( ent->r.singleClient == frame->ps.clientNum ) {
continue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -