diff --git a/cosmos_server/src/structure/planet/biosphere/biosphere_generation.rs b/cosmos_server/src/structure/planet/biosphere/biosphere_generation.rs index 0d951b808..6a974dd6c 100644 --- a/cosmos_server/src/structure/planet/biosphere/biosphere_generation.rs +++ b/cosmos_server/src/structure/planet/biosphere/biosphere_generation.rs @@ -26,7 +26,7 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, Parall use crate::{ init::init_world::{Noise, ReadOnlyNoise}, - structure::planet::lods::generate_lods::{GeneratingLod, GeneratingLods, LodNeedsGeneratedForPlayer}, + structure::planet::lods::generate_lods::{AsyncGeneratingLod, GeneratingLod, GeneratingLods, LodNeedsGeneratedForPlayer}, }; use super::{GeneratingChunk, GeneratingChunks, TGenerateChunkEvent}; @@ -1097,7 +1097,7 @@ fn recurse( - query: Query<(Entity, &LodNeedsGeneratedForPlayer)>, + query: Query<(Entity, &LodNeedsGeneratedForPlayer), With>, is_biosphere: Query<(&Structure, &Location), With>, noise_generator: Res, block_ranges: Res>, @@ -1111,6 +1111,8 @@ pub(crate) fn start_generating_lods, notify_when_done_generating_terrain::, + generate_player_lods::, start_generating_lods::, check_needs_generated_system::, ) diff --git a/cosmos_server/src/structure/planet/lods/generate_lods.rs b/cosmos_server/src/structure/planet/lods/generate_lods.rs index f8dffb3e0..21f9726a3 100644 --- a/cosmos_server/src/structure/planet/lods/generate_lods.rs +++ b/cosmos_server/src/structure/planet/lods/generate_lods.rs @@ -41,8 +41,15 @@ pub struct LodNeedsGeneratedForPlayer { pub current_lod: Option, } +#[derive(Debug)] +pub struct AsyncGeneratingLod { + pub player_entity: Entity, + pub structure_entity: Entity, + pub task: Task, +} + #[derive(Debug, Resource, Deref, DerefMut, Default)] -pub(crate) struct GeneratingLods(pub Vec>); +pub(crate) struct GeneratingLods(pub Vec); #[derive(Debug, Clone)] /// Represents a reduced-detail version of a planet undergoing generation @@ -74,7 +81,7 @@ pub enum GeneratingLod { } #[derive(Debug, Component)] -struct LodGenerationRequest { +pub(crate) struct LodGenerationRequest { request: LodRequest, structure_entity: Entity, player_entity: Entity, @@ -125,7 +132,7 @@ fn check_done_generating( swap(&mut todo, &mut generating_lods.0); for mut task in todo { - if let Some(generated_lod) = future::block_on(future::poll_once(&mut task)) { + if let Some(generated_lod) = future::block_on(future::poll_once(&mut task.task)) { if check_done(&generated_lod.generating_lod) { let current_lod = children_query .get(generated_lod.structure_entity) @@ -392,7 +399,7 @@ fn create_lod_request( } } -fn generate_player_lods( +pub(crate) fn generate_player_lods( mut commands: Commands, any_generation_requests: Query<(), With>, generating_lods: Query<&LodNeedsGeneratedForPlayer>, @@ -415,6 +422,13 @@ fn generate_player_lods( continue; } + if generating_lods + .iter() + .any(|x| x.player_entity == player_entity || x.structure_entity == structure_ent) + { + continue; + } + let Structure::Dynamic(ds) = structure else { panic!("Planet was a non-dynamic!!!"); }; @@ -453,12 +467,15 @@ fn generate_player_lods( } let request_entity = commands - .spawn(LodGenerationRequest { - player_entity, - structure_entity: structure_ent, - request, - current_lod: current_lod.cloned(), - }) + .spawn(( + LodGenerationRequest { + player_entity, + structure_entity: structure_ent, + request, + current_lod: current_lod.cloned(), + }, + T::default(), + )) .id(); commands.entity(structure_ent).add_child(request_entity); } @@ -468,9 +485,6 @@ fn generate_player_lods( pub(super) fn register(app: &mut App) { app.add_systems( Update, - ( - generate_player_lods.run_if(in_state(GameState::Playing)), - (start_generating_lods, check_done_generating).run_if(in_state(GameState::Playing)), - ), + ((start_generating_lods, check_done_generating).run_if(in_state(GameState::Playing)),), ); }