Skip to content

Commit

Permalink
Fixed asyncification
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Sep 10, 2023
1 parent 75a27f5 commit 7e940e6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1097,7 +1097,7 @@ fn recurse<T: Component + Default + Clone, S: BiosphereGenerationStrategy + 'sta
}

pub(crate) fn start_generating_lods<T: Component + Default + Clone, S: BiosphereGenerationStrategy + 'static>(
query: Query<(Entity, &LodNeedsGeneratedForPlayer)>,
query: Query<(Entity, &LodNeedsGeneratedForPlayer), With<T>>,
is_biosphere: Query<(&Structure, &Location), With<T>>,
noise_generator: Res<ReadOnlyNoise>,
block_ranges: Res<BlockLayers<T>>,
Expand All @@ -1111,6 +1111,8 @@ pub(crate) fn start_generating_lods<T: Component + Default + Clone, S: Biosphere
return;
};

let (player_entity, structure_entity) = (generating_lod.player_entity, generating_lod.structure_entity);

let task_pool = AsyncComputeTaskPool::get();

let structure_coords = location.absolute_coords_f64();
Expand All @@ -1137,7 +1139,11 @@ pub(crate) fn start_generating_lods<T: Component + Default + Clone, S: Biosphere
generating_lod
});

currently_generating.push(task);
currently_generating.push(AsyncGeneratingLod {
task,
player_entity,
structure_entity,
});
}
}

Expand Down
3 changes: 2 additions & 1 deletion cosmos_server/src/structure/planet/biosphere/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use self::biosphere_generation::{
generate_planet, notify_when_done_generating_terrain, start_generating_lods, BiosphereGenerationStrategy, GenerateChunkFeaturesEvent,
};

use super::generation::planet_generator::check_needs_generated_system;
use super::{generation::planet_generator::check_needs_generated_system, lods::generate_lods::generate_player_lods};

pub mod biosphere_generation;
pub mod generation_tools;
Expand Down Expand Up @@ -152,6 +152,7 @@ pub fn register_biosphere<
(
generate_planet::<T, E, S>,
notify_when_done_generating_terrain::<T>,
generate_player_lods::<T>,
start_generating_lods::<T, S>,
check_needs_generated_system::<E, T>,
)
Expand Down
42 changes: 28 additions & 14 deletions cosmos_server/src/structure/planet/lods/generate_lods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ pub struct LodNeedsGeneratedForPlayer {
pub current_lod: Option<Lod>,
}

#[derive(Debug)]
pub struct AsyncGeneratingLod {
pub player_entity: Entity,
pub structure_entity: Entity,
pub task: Task<LodNeedsGeneratedForPlayer>,
}

#[derive(Debug, Resource, Deref, DerefMut, Default)]
pub(crate) struct GeneratingLods(pub Vec<Task<LodNeedsGeneratedForPlayer>>);
pub(crate) struct GeneratingLods(pub Vec<AsyncGeneratingLod>);

#[derive(Debug, Clone)]
/// Represents a reduced-detail version of a planet undergoing generation
Expand Down Expand Up @@ -74,7 +81,7 @@ pub enum GeneratingLod {
}

#[derive(Debug, Component)]
struct LodGenerationRequest {
pub(crate) struct LodGenerationRequest {
request: LodRequest,
structure_entity: Entity,
player_entity: Entity,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -392,7 +399,7 @@ fn create_lod_request(
}
}

fn generate_player_lods(
pub(crate) fn generate_player_lods<T: Component + Default>(
mut commands: Commands,
any_generation_requests: Query<(), With<LodGenerationRequest>>,
generating_lods: Query<&LodNeedsGeneratedForPlayer>,
Expand All @@ -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!!!");
};
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)),),
);
}

0 comments on commit 7e940e6

Please sign in to comment.