From c1588ef66e844c067112ea880a54b4de9ec5a062 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 25 Feb 2023 17:32:15 -0600 Subject: Fix system order ambiguities (#74) * start fixing stuff where systems run in the wrong order * fix ordering ambiguity * add debugging guide * some fixes * fix panic for swarms * fix some warnings --- azalea-world/src/entity/info.rs | 65 ++++++++++++++++++----------------------- azalea-world/src/world.rs | 43 +++++++++++++++++---------- 2 files changed, 56 insertions(+), 52 deletions(-) (limited to 'azalea-world/src') diff --git a/azalea-world/src/entity/info.rs b/azalea-world/src/entity/info.rs index 48636c5b..8615ef81 100644 --- a/azalea-world/src/entity/info.rs +++ b/azalea-world/src/entity/info.rs @@ -36,52 +36,45 @@ use super::Local; pub struct EntityPlugin; impl Plugin for EntityPlugin { fn build(&self, app: &mut App) { - app.add_system_set( - SystemSet::new() - .after("tick") - .after("packet") - .with_system(update_entity_chunk_positions) - .with_system(remove_despawned_entities_from_indexes) - .with_system(update_bounding_box) - .with_system(add_dead) - .with_system( - add_updates_received - .after("deduplicate_entities") - .after("deduplicate_local_entities") - .label("add_updates_received"), - ) - .with_system( - update_uuid_index - .label("update_uuid_index") - .after("deduplicate_local_entities") - .after("deduplicate_entities"), - ) - .with_system(debug_detect_updates_received_on_local_entities) - .with_system( - update_entity_by_id_index - .label("update_entity_by_id_index") - .after("deduplicate_entities"), - ) - .with_system(debug_new_entity), + // entities get added pre-update + // added to indexes during update (done by this plugin) + // modified during update + // despawned post-update (done by this plugin) + app.add_system_set_to_stage( + CoreStage::PreUpdate, + SystemSet::new().with_system(remove_despawned_entities_from_indexes), ) .add_system_set_to_stage( CoreStage::PostUpdate, SystemSet::new() .with_system(deduplicate_entities.label("deduplicate_entities")) - .with_system( - deduplicate_local_entities - .label("deduplicate_local_entities") - .before("update_uuid_index") - .before("update_entity_by_id_index"), - ), + .with_system(deduplicate_local_entities.label("deduplicate_entities")), + ) + .add_system_set( + SystemSet::new() + .with_system(update_entity_chunk_positions) + .with_system(update_uuid_index.label("update_indexes")) + .with_system(update_entity_by_id_index.label("update_indexes")), + ) + .add_system_set( + SystemSet::new() + .with_system(add_updates_received.label("add_updates_received")) + .with_system(debug_new_entity) + .with_system(debug_detect_updates_received_on_local_entities) + .with_system(add_dead) + .with_system(update_bounding_box), ) .init_resource::(); } } -fn debug_new_entity(query: Query>) { - for entity in query.iter() { - debug!("new entity: {:?}", entity); +fn debug_new_entity(query: Query<(Entity, Option<&Local>), Added>) { + for (entity, local) in query.iter() { + if local.is_some() { + debug!("new local entity: {:?}", entity); + } else { + debug!("new entity: {:?}", entity); + } } } diff --git a/azalea-world/src/world.rs b/azalea-world/src/world.rs index 53a1f377..8f1b2179 100644 --- a/azalea-world/src/world.rs +++ b/azalea-world/src/world.rs @@ -82,8 +82,8 @@ pub fn deduplicate_entities( } commands.entity(new_entity).despawn(); info!( - "Entity with id {id:?} / {new_entity:?} already existed in the world, merging it with {old_entity:?}" - ); + "Entity with id {id:?} / {new_entity:?} already existed in the world, merging it with {old_entity:?}" + ); break; } } else { @@ -127,15 +127,20 @@ pub fn deduplicate_local_entities( pub fn update_uuid_index( mut entity_infos: ResMut, - query: Query<(Entity, &EntityUuid), Changed>, + query: Query<(Entity, &EntityUuid, Option<&Local>), Changed>, ) { - for (entity, &uuid) in query.iter() { + for (entity, &uuid, local) in query.iter() { // only add it if it doesn't already exist in // entity_infos.entity_by_uuid - // if entity_infos.entity_by_uuid.contains_key(&uuid) { - // warn!("Entity with UUID {uuid:?} already existed in the world, not adding - // to index (ecs id: {entity:?})", uuid=*uuid); continue; - // } + if local.is_none() { + if let Some(old_entity) = entity_infos.entity_by_uuid.get(&uuid) { + debug!( + "Entity with UUID {uuid:?} already existed in the world, not adding to + index (old ecs id: {old_entity:?} / new ecs id: {entity:?})" + ); + continue; + } + } entity_infos.entity_by_uuid.insert(*uuid, entity); } } @@ -208,18 +213,24 @@ impl Default for PartialWorld { /// System to keep the entity_by_id index up-to-date. pub fn update_entity_by_id_index( - mut query: Query<(Entity, &MinecraftEntityId, &WorldName), Changed>, + mut query: Query< + (Entity, &MinecraftEntityId, &WorldName, Option<&Local>), + Changed, + >, world_container: Res, ) { - for (entity, id, world_name) in query.iter_mut() { + for (entity, id, world_name, local) in query.iter_mut() { let world_lock = world_container.get(world_name).unwrap(); let mut world = world_lock.write(); - // if let Some(old_entity) = world.entity_by_id.get(id) { - // warn!( - // "Entity with ID {id:?} already existed in the world, not adding to - // index (old ecs id: {old_entity:?} / new ecs id: {entity:?})" ); - // continue; - // } + if local.is_none() { + if let Some(old_entity) = world.entity_by_id.get(id) { + debug!( + "Entity with ID {id:?} already existed in the world, not adding to + index (old ecs id: {old_entity:?} / new ecs id: {entity:?})" + ); + continue; + } + } world.entity_by_id.insert(*id, entity); debug!("Added {entity:?} to {world_name:?} with {id:?}."); } -- cgit v1.2.3