/* ============= SV_EmitPacketEntities Writes a delta update of an entity_state_t list to the message-> ============= */ void SV_EmitPacketEntities( sv_client_t *cl, client_frame_t *to, sizebuf_t *msg ) { entity_state_t *oldent, *newent; int oldindex, newindex; int oldnum, newnum; int from_num_entities; client_frame_t *from; // this is the frame that we are going to delta update from if( cl->delta_sequence != -1 ) { from = &cl->frames[cl->delta_sequence & SV_UPDATE_MASK]; from_num_entities = from->num_entities; // the snapshot's entities may still have rolled off the buffer, though if( from->first_entity <= svs.next_client_entities - svs.num_client_entities ) { MsgDev( D_WARN, "%s: delta request from out of date entities.\n", cl->name ); from = NULL; from_num_entities = 0; BF_WriteByte( msg, svc_packetentities ); BF_WriteWord( msg, to->num_entities ); } else { BF_WriteByte( msg, svc_deltapacketentities ); BF_WriteWord( msg, to->num_entities ); BF_WriteByte( msg, cl->delta_sequence ); } } else { from = NULL; from_num_entities = 0; BF_WriteByte( msg, svc_packetentities ); BF_WriteWord( msg, to->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 = MAX_ENTNUMBER; } else { newent = &svs.packet_entities[(to->first_entity+newindex)%svs.num_client_entities]; newnum = newent->number; } if( oldindex >= from_num_entities ) { oldnum = MAX_ENTNUMBER; } else { oldent = &svs.packet_entities[(from->first_entity+oldindex)%svs.num_client_entities]; oldnum = oldent->number; } if( newnum == oldnum ) { // delta update from old position // because the force parm is false, this will not result // in any bytes being emited if the entity has not changed at all MSG_WriteDeltaEntity( oldent, newent, msg, false, SV_IsPlayerIndex( newent->number ), sv.time ); oldindex++; newindex++; continue; } if( newnum < oldnum ) { // this is a new entity, send it from the baseline MSG_WriteDeltaEntity( &svs.baselines[newnum], newent, msg, true, SV_IsPlayerIndex( newent->number ), sv.time ); newindex++; continue; } if( newnum > oldnum ) { qboolean force; if( EDICT_NUM( oldent->number )->free ) force = true; // entity completely removed from server else force = false; // just removed from delta-message // remove from message MSG_WriteDeltaEntity( oldent, NULL, msg, force, false, sv.time ); oldindex++; continue; } } BF_WriteWord( msg, 0 ); // end of packetentities }
/* ============= SV_EmitEvents ============= */ static void SV_EmitEvents( sv_client_t *cl, client_frame_t *to, sizebuf_t *msg ) { event_state_t *es; event_info_t *info; entity_state_t *state; event_args_t nullargs; int ev_count = 0; int count, ent_index; int i, j, ev; Q_memset( &nullargs, 0, sizeof( nullargs )); es = &cl->events; // count events for( ev = 0; ev < MAX_EVENT_QUEUE; ev++ ) { if( es->ei[ev].index ) ev_count++; } // nothing to send if( !ev_count ) return; // nothing to send if( ev_count >= 31 ) ev_count = 31; for( i = 0; i < MAX_EVENT_QUEUE; i++ ) { info = &es->ei[i]; if( info->index == 0 ) continue; ent_index = info->entity_index; for( j = 0; j < to->num_entities; j++ ) { state = &svs.packet_entities[(to->first_entity+j)%svs.num_client_entities]; if( state->number == ent_index ) break; } if( j >= to->num_entities ) { // couldn't find info->packet_index = to->num_entities; info->args.entindex = ent_index; } else { info->packet_index = j; info->args.ducking = 0; if(!( info->args.flags & FEVENT_ORIGIN )) VectorClear( info->args.origin ); if(!( info->args.flags & FEVENT_ANGLES )) VectorClear( info->args.angles ); VectorClear( info->args.velocity ); } } BF_WriteByte( msg, svc_event ); // create message BF_WriteUBitLong( msg, ev_count, 5 ); // up to MAX_EVENT_QUEUE events for( count = i = 0; i < MAX_EVENT_QUEUE; i++ ) { info = &es->ei[i]; if( info->index == 0 ) { info->packet_index = -1; info->entity_index = -1; continue; } // only send if there's room if( count < ev_count ) { BF_WriteUBitLong( msg, info->index, MAX_EVENT_BITS ); // 1024 events if( info->packet_index == -1 ) { BF_WriteOneBit( msg, 0 ); } else { BF_WriteOneBit( msg, 1 ); BF_WriteUBitLong( msg, info->packet_index, MAX_ENTITY_BITS ); if( !Q_memcmp( &nullargs, &info->args, sizeof( event_args_t ))) { BF_WriteOneBit( msg, 0 ); } else { BF_WriteOneBit( msg, 1 ); MSG_WriteDeltaEvent( msg, &nullargs, &info->args ); } } if( info->fire_time ) { BF_WriteOneBit( msg, 1 ); BF_WriteWord( msg, Q_rint( info->fire_time * 100.0f )); } else BF_WriteOneBit( msg, 0 ); } info->index = 0; info->packet_index = -1; info->entity_index = -1; count++; } }
/* ================== MSG_WriteDeltaEntity Writes part of a packetentities message, including the entity number. Can delta from either a baseline or a previous packet_entity If to is NULL, a remove entity update will be sent If force is not set, then nothing at all will be generated if the entity is identical, under the assumption that the in-order delta code will catch it. ================== */ void MSG_WriteDeltaEntity( entity_state_t *from, entity_state_t *to, sizebuf_t *msg, qboolean force, qboolean player, float timebase ) { delta_info_t *dt = NULL; delta_t *pField; int i, startBit; int numChanges = 0; if( to == NULL ) { int fRemoveType; if( from == NULL ) return; // a NULL to is a delta remove message BF_WriteWord( msg, from->number ); // fRemoveType: // 0 - keep alive, has delta-update // 1 - remove from delta message (but keep states) // 2 - completely remove from server if( force ) fRemoveType = 2; else fRemoveType = 1; BF_WriteUBitLong( msg, fRemoveType, 2 ); return; } startBit = msg->iCurBit; if( to->number < 0 || to->number >= GI->max_edicts ) { MsgDev( D_ERROR, "MSG_WriteDeltaEntity: Bad entity number: %i\n", to->number ); return; } BF_WriteWord( msg, to->number ); BF_WriteUBitLong( msg, 0, 2 ); // alive if( to->entityType != from->entityType ) { BF_WriteOneBit( msg, 1 ); BF_WriteUBitLong( msg, to->entityType, 2 ); } else BF_WriteOneBit( msg, 0 ); if( to->entityType == ENTITY_NORMAL ) { if( player ) { dt = Delta_FindStruct( "entity_state_player_t" ); } else { dt = Delta_FindStruct( "entity_state_t" ); } } else if( to->entityType == ENTITY_BEAM ) { dt = Delta_FindStruct( "custom_entity_state_t" ); } ASSERT( dt && dt->bInitialized ); pField = dt->pFields; ASSERT( pField ); // activate fields and call custom encode func Delta_CustomEncode( dt, from, to ); // process fields for( i = 0; i < dt->numFields; i++, pField++ ) { if( Delta_WriteField( msg, pField, from, to, timebase )) numChanges++; } // if we have no changes - kill the message if( !numChanges && !force ) BF_SeekToBit( msg, startBit ); }