Skip to content

Commit

Permalink
Lods now update based on your position
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Sep 5, 2023
1 parent 2092e02 commit caa2d50
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 54 deletions.
8 changes: 7 additions & 1 deletion cosmos_client/src/structure/planet/lod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ fn listen_for_new_lods(
if let Ok(mut cur_lod) = cur_lod {
delta_lod.apply_changes(&mut cur_lod);
} else {
ecmds.insert(delta_lod.create_lod());
let created = delta_lod.clone().create_lod();
if matches!(created, Lod::None) {
println!("From:");
// remove above clone when you remove this
println!("{delta_lod:?}");
}
ecmds.insert(created);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cosmos_core/src/structure/lod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl LodDelta {
LodDelta::None => Lod::None,
LodDelta::Single(chunk) => Lod::Single(chunk, true),
LodDelta::NoChange => {
// or forcibly capture the backtrace regardless of environment variable configuration
println!("Error backtrace: {}", Backtrace::force_capture());
// Forcibly capture the backtrace regardless of environment variable configuration
println!("Error backtrace: \n{}", Backtrace::force_capture());

// panic!("Cannot have no change with no lod given!");
Lod::None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ fn recurse<T: Component + Default + Clone, S: BiosphereGenerationStrategy + 'sta
match generating_lod {
GeneratingLod::NeedsGenerated => {
*generating_lod = GeneratingLod::BeingGenerated;
println!("Generating LOD ({scale})!");
generate::<T, S>(
generating_lod,
(structure_x, structure_y, structure_z),
Expand Down
70 changes: 19 additions & 51 deletions cosmos_server/src/structure/planet/lods/generate_lods.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::time::Duration;

use bevy::{
prelude::{
in_state, warn, App, BuildChildren, Children, Commands, Component, DespawnRecursiveExt, Entity, Event, EventWriter,
GlobalTransform, IntoSystemConfigs, Quat, Query, Res, Update, With,
},
time::common_conditions::on_timer,
use bevy::prelude::{
in_state, warn, App, BuildChildren, Children, Commands, Component, DespawnRecursiveExt, Entity, GlobalTransform, IntoSystemConfigs,
Quat, Query, Res, Update, With,
};
use cosmos_core::{
block::Block,
Expand All @@ -14,7 +9,7 @@ use cosmos_core::{
registry::Registry,
structure::{
chunk::CHUNK_DIMENSIONS,
coordinates::{BlockCoordinate, ChunkCoordinate, CoordinateType, UnboundChunkCoordinate, UnboundCoordinateType},
coordinates::{BlockCoordinate, CoordinateType, UnboundChunkCoordinate, UnboundCoordinateType},
lod::{Lod, LodDelta},
lod_chunk::LodChunk,
planet::Planet,
Expand Down Expand Up @@ -80,22 +75,6 @@ struct LodGenerationRequest {
// task: Task<Lod>,
}

#[derive(Event)]
pub struct GenerateLodRequest {
pub starting_chunk: ChunkCoordinate,
pub structure_entity: Entity,

pub block_interval: CoordinateType,

pub lod_chunk: LodChunk,
}

// fn generate_lod(mut query: Query<&mut PlayerGeneratingLod>, blocks: Res<Registry<Block>>) {
// for mut generating_lod in query.iter_mut() {
// recurse(&mut generating_lod.generating_lod, &blocks);
// }
// }

fn check_done(generating_lod: &GeneratingLod) -> bool {
match generating_lod {
GeneratingLod::Children(children) => children.iter().all(check_done),
Expand Down Expand Up @@ -177,7 +156,6 @@ fn check_done_generating(
fn create_generating_lod(
structure_entity: Entity,
blocks: &Registry<Block>,
event_writer: &mut EventWriter<GenerateLodRequest>,
request: &LodRequest,
(min_block_range_inclusive, max_block_range_exclusive): (BlockCoordinate, BlockCoordinate),
) -> GeneratingLod {
Expand All @@ -190,14 +168,6 @@ fn create_generating_lod(
&& max_block_range_exclusive.x - min_block_range_inclusive.x
== max_block_range_exclusive.z - min_block_range_inclusive.z
);
let interval = (max_block_range_exclusive.x - min_block_range_inclusive.x + 1) / CHUNK_DIMENSIONS;

event_writer.send(GenerateLodRequest {
starting_chunk: ChunkCoordinate::for_block_coordinate(min_block_range_inclusive),
structure_entity: structure_entity,
block_interval: interval,
lod_chunk: LodChunk::new(),
});

GeneratingLod::NeedsGenerated
}
Expand All @@ -215,56 +185,48 @@ fn create_generating_lod(
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[0],
((min.x, min.y, min.z).into(), (max.x - dx, max.y - dy, max.z - dz).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[1],
((min.x, min.y, min.z + dz).into(), (max.x - dx, max.y - dy, max.z).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[2],
((min.x + dx, min.y, min.z + dz).into(), (max.x, max.y - dy, max.z).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[3],
((min.x + dx, min.y, min.z).into(), (max.x, max.y - dy, max.z - dz).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[4],
((min.x, min.y + dy, min.z).into(), (max.x - dx, max.y, max.z - dz).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[5],
((min.x, min.y + dy, min.z + dz).into(), (max.x - dx, max.y, max.z).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[6],
((min.x + dx, min.y + dy, min.z + dz).into(), (max.x, max.y, max.z).into()),
),
create_generating_lod(
structure_entity,
blocks,
event_writer,
&child_requests[7],
((min.x + dx, min.y + dy, min.z).into(), (max.x, max.y, max.z - dz).into()),
),
Expand All @@ -276,7 +238,6 @@ fn create_generating_lod(
fn poll_generating(
mut commands: Commands,
blocks: Res<Registry<Block>>,
mut event_writer: EventWriter<GenerateLodRequest>,
structure_query: Query<&Structure>,
query: Query<(Entity, &LodGenerationRequest)>,
) {
Expand All @@ -288,7 +249,6 @@ fn poll_generating(
let generating_lod = create_generating_lod(
lod_request.structure_entity,
&blocks,
&mut event_writer,
&lod_request.request,
(BlockCoordinate::new(0, 0, 0), structure.block_dimensions()),
);
Expand Down Expand Up @@ -329,7 +289,7 @@ fn create_lod_request(
} else {
let s4 = scale as UnboundCoordinateType / 4;

LodRequest::Multi(Box::new([
let children = [
create_lod_request(
scale / 2,
render_distance,
Expand Down Expand Up @@ -410,7 +370,13 @@ fn create_lod_request(
_ => None,
},
),
]))
];

if children.iter().all(|x| matches!(x, LodRequest::Same)) {
LodRequest::Same
} else {
LodRequest::Multi(Box::new(children))
}
}
}

Expand Down Expand Up @@ -461,6 +427,11 @@ fn generate_player_lods(

let request = create_lod_request(scale, render_distance, rel_coords - middle_chunk, true, current_lod);

// Same lod, don't generate
if matches!(request, LodRequest::Same) {
continue;
}

let request_entity = commands
.spawn(LodGenerationRequest {
player_entity,
Expand All @@ -477,11 +448,8 @@ pub(super) fn register(app: &mut App) {
app.add_systems(
Update,
(
generate_player_lods
.run_if(in_state(GameState::Playing))
.run_if(on_timer(Duration::from_millis(10000))),
generate_player_lods.run_if(in_state(GameState::Playing)),
(poll_generating, check_done_generating).run_if(in_state(GameState::Playing)),
),
)
.add_event::<GenerateLodRequest>();
);
}

0 comments on commit caa2d50

Please sign in to comment.