abortions and stuff

This commit is contained in:
griffi-gh 2024-05-02 16:50:46 +02:00
parent 64475022e3
commit 8c728f9650
12 changed files with 298 additions and 96 deletions

26
Cargo.lock generated
View file

@ -145,6 +145,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]]
name = "atomic"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d818003e740b63afc82337e3160717f4f63078720a810b7b903e70a5d1d2994"
dependencies = [
"bytemuck",
]
[[package]] [[package]]
name = "atomic-polyfill" name = "atomic-polyfill"
version = "1.0.3" version = "1.0.3"
@ -262,6 +271,20 @@ name = "bytemuck"
version = "1.15.0" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15" checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.60",
]
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -1150,6 +1173,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"android-activity", "android-activity",
"anyhow", "anyhow",
"atomic",
"flume", "flume",
"gilrs", "gilrs",
"glam", "glam",
@ -1217,8 +1241,10 @@ name = "kubi-shared"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"atomic",
"bincode", "bincode",
"bracket-noise", "bracket-noise",
"bytemuck",
"glam", "glam",
"hashbrown 0.14.3", "hashbrown 0.14.3",
"nohash-hasher", "nohash-hasher",

View file

@ -1,59 +1,61 @@
use shipyard::{Unique, AllStoragesView}; use shipyard::{Unique, AllStoragesView};
use flume::{unbounded, Sender, Receiver}; use flume::{unbounded, Sender, Receiver};
use glam::IVec3; use glam::IVec3;
use rayon::{ThreadPool, ThreadPoolBuilder}; use rayon::{ThreadPool, ThreadPoolBuilder};
use anyhow::Result; use anyhow::Result;
use kubi_shared::{ use kubi_shared::{
chunk::BlockData, chunk::BlockData,
worldgen::generate_world, worldgen::generate_world,
queue::QueuedBlock, queue::QueuedBlock,
}; };
pub enum ChunkTask { pub enum ChunkTask {
LoadChunk { LoadChunk {
position: IVec3, position: IVec3,
seed: u64, seed: u64,
} }
} }
pub enum ChunkTaskResponse { pub enum ChunkTaskResponse {
ChunkLoaded { ChunkLoaded {
chunk_position: IVec3, chunk_position: IVec3,
blocks: BlockData, blocks: BlockData,
queue: Vec<QueuedBlock> queue: Vec<QueuedBlock>
} }
} }
#[derive(Unique)] #[derive(Unique)]
pub struct ChunkTaskManager { pub struct ChunkTaskManager {
channel: (Sender<ChunkTaskResponse>, Receiver<ChunkTaskResponse>), channel: (Sender<ChunkTaskResponse>, Receiver<ChunkTaskResponse>),
pool: ThreadPool, pool: ThreadPool,
} }
impl ChunkTaskManager { impl ChunkTaskManager {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
Ok(Self { Ok(Self {
channel: unbounded(), channel: unbounded(),
pool: ThreadPoolBuilder::new().build()? pool: ThreadPoolBuilder::new().build()?
}) })
} }
pub fn spawn_task(&self, task: ChunkTask) { pub fn spawn_task(&self, task: ChunkTask) {
let sender = self.channel.0.clone(); let sender = self.channel.0.clone();
self.pool.spawn(move || { self.pool.spawn(move || {
sender.send(match task { sender.send(match task {
ChunkTask::LoadChunk { position: chunk_position, seed } => { ChunkTask::LoadChunk { position: chunk_position, seed } => {
let (blocks, queue) = generate_world(chunk_position, seed); let Some((blocks, queue)) = generate_world(chunk_position, seed, None) else {
ChunkTaskResponse::ChunkLoaded { chunk_position, blocks, queue } return
} };
}).unwrap() ChunkTaskResponse::ChunkLoaded { chunk_position, blocks, queue }
}) }
} }).unwrap()
pub fn receive(&self) -> Option<ChunkTaskResponse> { })
self.channel.1.try_recv().ok() }
} pub fn receive(&self) -> Option<ChunkTaskResponse> {
} self.channel.1.try_recv().ok()
}
pub fn init_chunk_task_manager( }
storages: AllStoragesView
) { pub fn init_chunk_task_manager(
storages.add_unique(ChunkTaskManager::new().expect("ChunkTaskManager Init failed")); storages: AllStoragesView
} ) {
storages.add_unique(ChunkTaskManager::new().expect("ChunkTaskManager Init failed"));
}

View file

@ -19,9 +19,10 @@ rand = { version = "0.8", default_features = false, features = ["std", "min_cons
rand_xoshiro = "0.6" rand_xoshiro = "0.6"
hashbrown = { version = "0.14", features = ["serde"] } hashbrown = { version = "0.14", features = ["serde"] }
nohash-hasher = "0.2" nohash-hasher = "0.2"
#bytemuck = { version = "1.14", features = ["derive"] } bytemuck = { version = "1.14", features = ["derive"] }
static_assertions = "1.1" static_assertions = "1.1"
nz = "0.3" nz = "0.3"
atomic = "0.6"
[features] [features]
default = [] default = []

View file

@ -26,9 +26,10 @@ pub enum BlockTexture {
Water, Water,
} }
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, EnumIter, TryFromPrimitive)] #[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq, EnumIter, TryFromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum Block { pub enum Block {
#[default]
Air, Air,
Marker, Marker,
Stone, Stone,

View file

@ -1,6 +1,8 @@
use std::sync::atomic::AtomicBool; use std::sync::Arc;
use atomic::Atomic;
use glam::{ivec3, IVec3}; use bytemuck::{CheckedBitPattern, NoUninit};
use glam::IVec3;
use static_assertions::const_assert;
use crate::{ use crate::{
block::Block, block::Block,
chunk::{BlockData, CHUNK_SIZE}, chunk::{BlockData, CHUNK_SIZE},
@ -8,6 +10,18 @@ use crate::{
}; };
mod _01_terrain; mod _01_terrain;
mod _02_water;
mod _03_caves;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, NoUninit, CheckedBitPattern)]
pub enum AbortState {
#[default]
Continue,
Abort,
Aborted,
}
const_assert!(Atomic::<AbortState>::is_lock_free());
trait WorldGenStep { trait WorldGenStep {
fn initialize(generator: &WorldGenerator) -> Self; fn initialize(generator: &WorldGenerator) -> Self;
@ -16,16 +30,29 @@ trait WorldGenStep {
macro_rules! run_steps { macro_rules! run_steps {
($gen: expr, $abort: expr, [$($step:ty),* $(,)?]) => { ($gen: expr, $abort: expr, [$($step:ty),* $(,)?]) => {
{ (||{
let _abort: AtomicBool = $abort.unwrap_or(AtomicBool::new(false)); let _abort: ::std::sync::Arc<::atomic::Atomic<$crate::worldgen::AbortState>> =
$abort.unwrap_or_else(|| ::std::sync::Arc::new(::atomic::Atomic::new($crate::worldgen::AbortState::Continue)));
let _chkabt = || _abort.compare_exchange(
$crate::worldgen::AbortState::Abort,
$crate::worldgen::AbortState::Aborted,
::atomic::Ordering::Relaxed,
::atomic::Ordering::Relaxed
).is_ok();
$({ $({
let _ensure_ref: &mut WorldGenerator = $gen; let _ensure_ref: &mut $crate::worldgen::WorldGenerator = $gen;
struct _Ensure0<T: WorldGenStep>(T); struct _Ensure0<T: $crate::worldgen::WorldGenStep>(T);
type _Ensure1 = _Ensure0<$step>; type _Ensure1 = _Ensure0<$step>;
let mut step: _Ensure1 = _Ensure0(<$step>::initialize(&*_ensure_ref)); let mut step: _Ensure1 = _Ensure0(<$step>::initialize(&*_ensure_ref));
if _chkabt() { return false }
step.0.generate(_ensure_ref); step.0.generate(_ensure_ref);
if _chkabt() { return false }
})* })*
}
true
})()
}; };
} }
@ -41,6 +68,32 @@ impl WorldGenerator {
self.chunk_position * CHUNK_SIZE as i32 self.chunk_position * CHUNK_SIZE as i32
} }
fn query(&self, position: IVec3) -> Block {
// let offset = self.offset();
// let event_pos = offset + position;
// if let Some(block) = self.queue.iter().find(|block| block.position == event_pos) {
// block.block_type
// } else {
// self.blocks[position.x as usize][position.y as usize][position.z as usize]
// }
self.blocks[position.x as usize][position.y as usize][position.z as usize]
}
fn place(&mut self, position: IVec3, block: Block) {
// let offset = self.offset();
// let event_pos = offset + position;
// self.queue.retain(|block: &QueuedBlock| {
// block.position != event_pos
// });
self.blocks[position.x as usize][position.y as usize][position.z as usize] = block;
}
fn place_if_empty(&mut self, position: IVec3, block: Block) {
if self.query(position) == Block::Air {
self.place(position, block);
}
}
fn place_or_queue(&mut self, position: IVec3, block: Block) { fn place_or_queue(&mut self, position: IVec3, block: Block) {
let offset = self.offset(); let offset = self.offset();
if position.to_array().iter().any(|&x| !(0..CHUNK_SIZE).contains(&(x as usize))) { if position.to_array().iter().any(|&x| !(0..CHUNK_SIZE).contains(&(x as usize))) {
@ -58,6 +111,21 @@ impl WorldGenerator {
} }
} }
fn global_position(&self, position: IVec3) -> IVec3 {
self.offset() + position
}
fn local_height(&self, height: i32) -> i32 {
let offset = self.chunk_position * CHUNK_SIZE as i32;
(height - offset.y).clamp(0, CHUNK_SIZE as i32)
}
fn local_y_position(&self, y: i32) -> Option<i32> {
let offset = self.chunk_position * CHUNK_SIZE as i32;
let position = y - offset.y;
(0..CHUNK_SIZE as i32).contains(&position).then_some(position)
}
pub fn new(chunk_position: IVec3, seed: u64) -> Self { pub fn new(chunk_position: IVec3, seed: u64) -> Self {
Self { Self {
seed, seed,
@ -67,14 +135,19 @@ impl WorldGenerator {
} }
} }
pub fn generate(mut self, abort: Option<AtomicBool>) -> (BlockData, Vec<QueuedBlock>) { /// Generate the chunk.
///
/// Will return `None` only if the generation was aborted.
pub fn generate(mut self, abort: Option<Arc<Atomic<AbortState>>>) -> Option<(BlockData, Vec<QueuedBlock>)> {
run_steps!(&mut self, abort, [ run_steps!(&mut self, abort, [
_01_terrain::TerrainStep _01_terrain::TerrainStep,
]); _02_water::WaterStep,
(self.blocks, self.queue) _03_caves::CaveStep,
]).then_some((self.blocks, self.queue))
} }
} }
pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) { pub fn generate_world(chunk_position: IVec3, seed: u64, abort: Option<Arc<Atomic<AbortState>>>) -> Option<(BlockData, Vec<QueuedBlock>)> {
WorldGenerator::new(chunk_position, seed).generate(None) //TODO: pass through None for abort
WorldGenerator::new(chunk_position, seed).generate(abort)
} }

View file

@ -6,21 +6,23 @@ use super::{WorldGenerator, WorldGenStep};
pub struct TerrainStep { pub struct TerrainStep {
noise: FastNoise, noise: FastNoise,
} }
impl WorldGenStep for TerrainStep { impl WorldGenStep for TerrainStep {
fn initialize(generator: &WorldGenerator) -> Self { fn initialize(generator: &WorldGenerator) -> Self {
let mut noise = FastNoise::seeded(generator.seed); let mut noise = FastNoise::seeded(generator.seed);
noise.set_fractal_type(FractalType::FBM); noise.set_fractal_type(FractalType::RigidMulti);
noise.set_fractal_octaves(4); noise.set_fractal_octaves(5);
noise.set_frequency(0.003); noise.set_frequency(0.003);
Self { noise } Self { noise }
} }
fn generate(&mut self, generator: &mut WorldGenerator) { fn generate(&mut self, gen: &mut WorldGenerator) {
for x in 0..CHUNK_SIZE as i32 { for x in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 {
let height = (self.noise.get_noise(x as f32, z as f32) * 8.0) as i32; let global_xz = gen.global_position(ivec3(x, 0, z));
for y in 0..height { let height = (self.noise.get_noise(global_xz.x as f32, global_xz.z as f32) * 32.0) as i32;
generator.place_or_queue(ivec3(x, y, z), Block::Stone); for y in 0..gen.local_height(height) {
gen.place(ivec3(x, y, z), Block::Stone);
} }
} }
} }

View file

@ -0,0 +1,19 @@
use bracket_noise::prelude::{FastNoise, FractalType};
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::{WorldGenerator, WorldGenStep};
pub struct WaterStep;
impl WorldGenStep for WaterStep {
fn initialize(_: &WorldGenerator) -> Self { Self }
fn generate(&mut self, gen: &mut WorldGenerator) {
for x in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 {
for y in 0..gen.local_height(0) {
gen.place_if_empty(ivec3(x, y, z), Block::Water);
}
}
}
}
}

View file

@ -0,0 +1,42 @@
use bracket_noise::prelude::{FastNoise, FractalType};
use glam::{ivec3, IVec3};
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::{WorldGenStep, WorldGenerator};
pub struct CaveStep {
a: FastNoise,
b: FastNoise,
}
impl WorldGenStep for CaveStep {
fn initialize(gen: &WorldGenerator) -> Self {
let mut a = FastNoise::seeded(gen.seed);
a.set_fractal_type(FractalType::FBM);
a.set_frequency(0.015);
let mut b = FastNoise::seeded(gen.seed.rotate_left(1) + 1);
b.set_fractal_type(FractalType::FBM);
b.set_frequency(0.015);
Self { a, b }
}
fn generate(&mut self, gen: &mut WorldGenerator) {
for x in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 {
for y in 0..CHUNK_SIZE as i32 {
let pos: IVec3 = ivec3(x, y, z);
if gen.query(pos) != Block::Stone { continue }
let gpos = gen.global_position(pos);
let noise_a = self.a.get_noise3d(gpos.x as f32, gpos.y as f32, gpos.z as f32);
let noise_b = self.b.get_noise3d(gpos.x as f32, gpos.y as f32, gpos.z as f32);
let noise_min = noise_a.min(noise_b);
if noise_min > 0.5 { return }
gen.place(ivec3(x, y, z), Block::Air);
}
}
}
}
}

View file

@ -37,6 +37,7 @@ static_assertions = "1.1"
tinyset = "0.4" tinyset = "0.4"
serde_json = { version = "1.0", optional = true } #only used for `generate_visualizer_data` serde_json = { version = "1.0", optional = true } #only used for `generate_visualizer_data`
rand = { version = "0.8", features = ["alloc", "small_rng"]} rand = { version = "0.8", features = ["alloc", "small_rng"]}
atomic = "0.6"
[target.'cfg(target_os = "android")'.dependencies] [target.'cfg(target_os = "android")'.dependencies]
android-activity = "^0.5.2" android-activity = "^0.5.2"

View file

@ -1,5 +1,8 @@
use std::sync::Arc;
use glam::IVec3; use glam::IVec3;
use atomic::Atomic;
use glium::{VertexBuffer, IndexBuffer}; use glium::{VertexBuffer, IndexBuffer};
use kubi_shared::worldgen::AbortState;
use crate::rendering::world::ChunkVertex; use crate::rendering::world::ChunkVertex;
pub use kubi_shared::chunk::{CHUNK_SIZE, BlockData}; pub use kubi_shared::chunk::{CHUNK_SIZE, BlockData};
@ -39,7 +42,7 @@ pub enum DesiredChunkState {
Nothing, Nothing,
Loaded, Loaded,
Rendered, Rendered,
ToUnload, Unloaded,
} }
impl DesiredChunkState { impl DesiredChunkState {
pub fn matches_current(self, current: CurrentChunkState) -> bool { pub fn matches_current(self, current: CurrentChunkState) -> bool {
@ -55,8 +58,10 @@ pub struct Chunk {
pub mesh_index: Option<usize>, pub mesh_index: Option<usize>,
pub current_state: CurrentChunkState, pub current_state: CurrentChunkState,
pub desired_state: DesiredChunkState, pub desired_state: DesiredChunkState,
pub abortion: Option<Arc<Atomic<AbortState>>>,
pub mesh_dirty: bool, pub mesh_dirty: bool,
} }
impl Chunk { impl Chunk {
pub fn new(position: IVec3) -> Self { pub fn new(position: IVec3) -> Self {
Self { Self {
@ -65,6 +70,7 @@ impl Chunk {
mesh_index: None, mesh_index: None,
current_state: Default::default(), current_state: Default::default(),
desired_state: Default::default(), desired_state: Default::default(),
abortion: None,
mesh_dirty: false, mesh_dirty: false,
} }
} }

View file

@ -1,6 +1,8 @@
use std::sync::Arc;
use atomic::{Atomic, Ordering};
use glam::{IVec3, ivec3}; use glam::{IVec3, ivec3};
use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
use kubi_shared::networking::messages::ClientToServerMessage; use kubi_shared::{networking::messages::ClientToServerMessage, worldgen::AbortState};
use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track}; use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track};
use uflow::SendMode; use uflow::SendMode;
use crate::{ use crate::{
@ -15,10 +17,10 @@ use super::{
ChunkStorage, ChunkMeshStorage, ChunkStorage, ChunkMeshStorage,
chunk::{Chunk, DesiredChunkState, CHUNK_SIZE, ChunkMesh, CurrentChunkState, ChunkData}, chunk::{Chunk, DesiredChunkState, CHUNK_SIZE, ChunkMesh, CurrentChunkState, ChunkData},
tasks::{ChunkTaskManager, ChunkTaskResponse, ChunkTask}, tasks::{ChunkTaskManager, ChunkTaskResponse, ChunkTask},
queue::BlockUpdateQueue queue::BlockUpdateQueue,
}; };
const MAX_CHUNK_OPS_INGAME: usize = 6; const MAX_CHUNK_OPS_INGAME: usize = 8;
const MAX_CHUNK_OPS: usize = 32; const MAX_CHUNK_OPS: usize = 32;
pub fn update_loaded_world_around_player() -> Workload { pub fn update_loaded_world_around_player() -> Workload {
@ -56,7 +58,7 @@ pub fn update_chunks_if_player_moved(
//Then, mark *ALL* chunks with ToUnload //Then, mark *ALL* chunks with ToUnload
for (_, chunk) in &mut vm_world.chunks { for (_, chunk) in &mut vm_world.chunks {
chunk.desired_state = DesiredChunkState::ToUnload; chunk.desired_state = DesiredChunkState::Unloaded;
} }
//Then mark chunks that are near to the player //Then mark chunks that are near to the player
@ -99,13 +101,27 @@ fn unload_downgrade_chunks(
//TODO refactor this //TODO refactor this
//TODO unsubscibe if in multiplayer //TODO unsubscibe if in multiplayer
vm_world.chunks.retain(|_, chunk| { vm_world.chunks.retain(|_, chunk| {
if chunk.desired_state == DesiredChunkState::ToUnload { if chunk.desired_state == DesiredChunkState::Unloaded {
if let Some(mesh_index) = chunk.mesh_index { if let Some(mesh_index) = chunk.mesh_index {
vm_meshes.remove(mesh_index).unwrap(); vm_meshes.remove(mesh_index).unwrap();
} }
if let Some(abortion) = &chunk.abortion {
let _ = abortion.compare_exchange(
AbortState::Continue, AbortState::Abort,
Ordering::Relaxed, Ordering::Relaxed
);
}
false false
} else { } else {
match chunk.desired_state { match chunk.desired_state {
DesiredChunkState::Nothing if matches!(chunk.current_state, CurrentChunkState::Loading) => {
if let Some(abortion) = &chunk.abortion {
let _ = abortion.compare_exchange(
AbortState::Continue, AbortState::Abort,
Ordering::Relaxed, Ordering::Relaxed
);
}
},
DesiredChunkState::Loaded if matches!(chunk.current_state, CurrentChunkState::Rendered | CurrentChunkState::CalculatingMesh | CurrentChunkState::RecalculatingMesh) => { DesiredChunkState::Loaded if matches!(chunk.current_state, CurrentChunkState::Rendered | CurrentChunkState::CalculatingMesh | CurrentChunkState::RecalculatingMesh) => {
if let Some(mesh_index) = chunk.mesh_index { if let Some(mesh_index) = chunk.mesh_index {
vm_meshes.remove(mesh_index).unwrap(); vm_meshes.remove(mesh_index).unwrap();
@ -134,6 +150,7 @@ fn start_required_tasks(
let chunk = world.chunks.get(&position).unwrap(); let chunk = world.chunks.get(&position).unwrap();
match chunk.desired_state { match chunk.desired_state {
DesiredChunkState::Loaded | DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Nothing => { DesiredChunkState::Loaded | DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Nothing => {
let mut abortion = None;
//start load task //start load task
if let Some(client) = &mut udp_client { if let Some(client) = &mut udp_client {
client.0.send( client.0.send(
@ -144,14 +161,18 @@ fn start_required_tasks(
SendMode::Reliable SendMode::Reliable
); );
} else { } else {
let atomic = Arc::new(Atomic::new(AbortState::Continue));
task_manager.spawn_task(ChunkTask::LoadChunk { task_manager.spawn_task(ChunkTask::LoadChunk {
seed: 0xbeef_face_dead_cafe, seed: 0xbeef_face_dead_cafe,
position position,
abortion: Some(Arc::clone(&atomic)),
}); });
abortion = Some(atomic);
} }
//Update chunk state //Update chunk state
let chunk = world.chunks.get_mut(&position).unwrap(); let chunk = world.chunks.get_mut(&position).unwrap();
chunk.current_state = CurrentChunkState::Loading; chunk.current_state = CurrentChunkState::Loading;
chunk.abortion = abortion;
// =========== // ===========
//log::trace!("Started loading chunk {position}"); //log::trace!("Started loading chunk {position}");
}, },
@ -173,6 +194,7 @@ fn start_required_tasks(
chunk.current_state = CurrentChunkState::CalculatingMesh; chunk.current_state = CurrentChunkState::CalculatingMesh;
} }
chunk.mesh_dirty = false; chunk.mesh_dirty = false;
chunk.abortion = None; //Can never abort at this point
// =========== // ===========
//log::trace!("Started generating mesh for chunk {position}"); //log::trace!("Started generating mesh for chunk {position}");
} }
@ -195,14 +217,15 @@ fn process_completed_tasks(
ChunkTaskResponse::LoadedChunk { position, chunk_data, mut queued } => { ChunkTaskResponse::LoadedChunk { position, chunk_data, mut queued } => {
//check if chunk exists //check if chunk exists
let Some(chunk) = world.chunks.get_mut(&position) else { let Some(chunk) = world.chunks.get_mut(&position) else {
//to compensate, actually push the ops counter back by one
log::warn!("blocks data discarded: chunk doesn't exist"); log::warn!("blocks data discarded: chunk doesn't exist");
return continue
}; };
//check if chunk still wants it //check if chunk still wants it
if !matches!(chunk.desired_state, DesiredChunkState::Loaded | DesiredChunkState::Rendered) { if !matches!(chunk.desired_state, DesiredChunkState::Loaded | DesiredChunkState::Rendered) {
log::warn!("block data discarded: state undesirable: {:?}", chunk.desired_state); log::warn!("block data discarded: state undesirable: {:?}", chunk.desired_state);
return continue
} }
//set the block data //set the block data
@ -228,13 +251,13 @@ fn process_completed_tasks(
//check if chunk exists //check if chunk exists
let Some(chunk) = world.chunks.get_mut(&position) else { let Some(chunk) = world.chunks.get_mut(&position) else {
log::warn!("mesh discarded: chunk doesn't exist"); log::warn!("mesh discarded: chunk doesn't exist");
return continue
}; };
//check if chunk still wants it //check if chunk still wants it
if chunk.desired_state != DesiredChunkState::Rendered { if chunk.desired_state != DesiredChunkState::Rendered {
log::warn!("mesh discarded: state undesirable: {:?}", chunk.desired_state); log::warn!("mesh discarded: state undesirable: {:?}", chunk.desired_state);
return continue
} }
//apply the mesh //apply the mesh

View file

@ -1,6 +1,8 @@
use std::sync::Arc;
use atomic::Atomic;
use flume::{Sender, Receiver}; use flume::{Sender, Receiver};
use glam::IVec3; use glam::IVec3;
use kubi_shared::queue::QueuedBlock; use kubi_shared::{queue::QueuedBlock, worldgen::AbortState};
use shipyard::Unique; use shipyard::Unique;
use rayon::{ThreadPool, ThreadPoolBuilder}; use rayon::{ThreadPool, ThreadPoolBuilder};
use super::{ use super::{
@ -13,7 +15,8 @@ use crate::rendering::world::ChunkVertex;
pub enum ChunkTask { pub enum ChunkTask {
LoadChunk { LoadChunk {
seed: u64, seed: u64,
position: IVec3 position: IVec3,
abortion: Option<Arc<Atomic<AbortState>>>,
}, },
GenerateMesh { GenerateMesh {
position: IVec3, position: IVec3,
@ -67,8 +70,11 @@ impl ChunkTaskManager {
trans_vertices, trans_indices, trans_vertices, trans_indices,
} }
}, },
ChunkTask::LoadChunk { position, seed } => { ChunkTask::LoadChunk { position, seed, abortion } => {
let (chunk_data, queued) = generate_world(position, seed); let Some((chunk_data, queued)) = generate_world(position, seed, abortion) else {
log::warn!("aborted operation");
return
};
ChunkTaskResponse::LoadedChunk { position, chunk_data, queued } ChunkTaskResponse::LoadedChunk { position, chunk_data, queued }
} }
}); });