better? baselines

This commit is contained in:
Robin Appelman 2022-04-10 22:52:27 +02:00
commit 57ee813af7
2 changed files with 47 additions and 22 deletions

View file

@ -15,7 +15,7 @@ pub struct ActiveEntities {
impl ActiveEntities {
pub fn handle_message(&mut self, msg: &PacketEntitiesMessage, state: &ParserState) {
for entity in &msg.entities {
if entity.update_type == UpdateType::Delete || entity.update_type == UpdateType::Leave {
if entity.update_type == UpdateType::Delete {
self.remove_entity(entity.entity_index);
} else {
self.handle_entity(entity, state);
@ -27,11 +27,20 @@ impl ActiveEntities {
if msg.updated_base_line {
let old_index = msg.base_line as usize;
let new_index = 1 - old_index;
self.baselines.swap(0, 1);
self.baselines[new_index] = self.baselines[old_index].clone();
for entity in &msg.entities {
if entity.update_type == UpdateType::Enter {
self.baselines[new_index].insert(entity.entity_index, entity.clone());
let entity = match self.baselines[old_index].get(&entity.entity_index) {
Some(baseline) if baseline.server_class == entity.server_class => {
let mut baseline = baseline.clone();
baseline.apply_update(&entity.props);
baseline
}
_ => entity.clone(),
};
self.baselines[new_index].insert(entity.entity_index, entity);
}
}
}
@ -47,39 +56,51 @@ impl ActiveEntities {
self.entities
.entry(entity.entity_index)
.and_modify(|existing| {
if existing.serial_number != entity.serial_number {
if existing.serial_number != entity.serial_number
&& existing.server_class != entity.server_class
{
// todo: do baselines need to be cleanup up or updated here?
*existing = entity.clone();
existing.update_type = UpdateType::Enter;
} else {
assert_eq!(
debug_assert_eq!(
state.server_classes[usize::from(existing.server_class)],
state.server_classes[usize::from(entity.server_class)]
);
for prop in &entity.props {
match existing
.props
.iter_mut()
.find(|existing| existing.index == prop.index)
{
Some(existing) => existing.value = prop.value.clone(),
None => existing.props.push(prop.clone()),
}
if existing.serial_number != entity.serial_number {
existing.serial_number = entity.serial_number;
existing.update_type = UpdateType::Enter;
}
existing.apply_update(&entity.props);
}
})
.or_insert_with(|| entity.clone());
}
pub fn encode(self) -> impl IntoIterator<Item = PacketEntitiesMessage> {
pub fn encode(mut self) -> impl IntoIterator<Item = PacketEntitiesMessage> {
let mut baselines = [
encode_entities(self.baselines[0].clone().into_values().collect::<Vec<_>>()),
encode_entities(self.baselines[1].clone().into_values().collect::<Vec<_>>()),
];
for entity in self.entities.values_mut() {
entity.update_type = UpdateType::Enter;
}
let entities = encode_entities(self.entities.into_values().collect::<Vec<_>>());
baselines[0].updated_base_line = true;
baselines[0].base_line = 1; //the baseline that is updated is the other one
baselines[1].base_line = 0;
for ent in baselines[0]
.entities
.iter()
.chain(baselines[1].entities.iter())
.chain(entities.entities.iter())
{
if ent.entity_index == 650 {
// dbg!(ent.update_type, &ent.props);
}
}
baselines[1].updated_base_line = true;

View file

@ -47,6 +47,7 @@ fn test_reparse_with_analyser<A: BorrowMessageHandler + Default, F: Fn(&A::Outpu
(original_ticks.next().unwrap(), cut_ticks.next().unwrap())
{
assert_eq!(original_tick.tick, cut_tick.tick + 30000);
// dbg!(original_tick.tick);
let original_state = &original_tick.state;
let cut_state = &cut_tick.state;
@ -105,10 +106,13 @@ fn test_reparse_game_state() {
})
}
// #[test]
// fn test_reparse_entities() {
// test_reparse_with_analyser::<EntityDumper, _>(|original_state, cut_state| {
// assert_eq!(original_state.len(), cut_state.len());
// panic!();
// })
// }
#[test]
fn test_reparse_entities() {
test_reparse_with_analyser::<EntityDumper, _>(|original_state, cut_state| {
assert_eq!(original_state.len(), cut_state.len());
for (original_entity, cut_entity) in original_state.iter().zip(cut_state) {
assert_eq!(original_entity.0, cut_entity.0);
assert_eq!(original_entity.1, cut_entity.1);
}
})
}