Compare commits

..

11 commits

Author SHA1 Message Date
griffi-gh ec592951bc add lints 2024-05-03 01:48:12 +02:00
griffi-gh 656f124549 "fix" some warnings 2024-05-03 01:39:47 +02:00
griffi-gh 00b8a253bb
Merge pull request #19 from griffi-gh/rewrite-wgen
Rewrite world generation
2024-05-03 01:23:34 +02:00
griffi-gh 875f131fe1 move stuff, forests! 2024-05-03 01:21:56 +02:00
griffi-gh 91326ce2dc decoration and layers 2024-05-03 00:27:43 +02:00
griffi-gh d620ba3840 add opt lvl 2024-05-02 22:27:49 +02:00
griffi-gh 73695dcd5a check before first step 2024-05-02 18:16:29 +02:00
griffi-gh 8790454b23 switch libs 2024-05-02 18:15:18 +02:00
griffi-gh 650ea55a14 disable borked cave generation 2024-05-02 16:51:03 +02:00
griffi-gh 8c728f9650 abortions and stuff 2024-05-02 16:50:46 +02:00
griffi-gh 64475022e3 rewrite worldgen 2024-05-02 12:42:14 +02:00
29 changed files with 732 additions and 537 deletions

70
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"
@ -227,30 +236,6 @@ dependencies = [
"objc2", "objc2",
] ]
[[package]]
name = "bracket-noise"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0b7443d0990c69db7a83f376f0101d684c20a911698e5f932bffbda2c8b08dd"
dependencies = [
"bracket-random",
]
[[package]]
name = "bracket-random"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "437be61484077b1ddb57002ce3c96b7d03cbf500b5d15157ee7e67e22332c39b"
dependencies = [
"getrandom",
"js-sys",
"lazy_static",
"rand",
"rand_xorshift",
"regex",
"wasm-bindgen",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.16.0" version = "3.16.0"
@ -262,6 +247,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"
@ -610,6 +609,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "fastnoise-lite"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e5f3c3cc9081e5d0e18bcd50e80cd33cba47fc22f88a9da9c33ecd1c87ea5c0"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "fdeflate" name = "fdeflate"
version = "0.3.4" version = "0.3.4"
@ -1150,6 +1158,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"android-activity", "android-activity",
"anyhow", "anyhow",
"atomic",
"flume", "flume",
"gilrs", "gilrs",
"glam", "glam",
@ -1217,8 +1226,10 @@ name = "kubi-shared"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"atomic",
"bincode", "bincode",
"bracket-noise", "bytemuck",
"fastnoise-lite",
"glam", "glam",
"hashbrown 0.14.3", "hashbrown 0.14.3",
"nohash-hasher", "nohash-hasher",
@ -1662,15 +1673,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rand_xorshift"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f"
dependencies = [
"rand_core",
]
[[package]] [[package]]
name = "rand_xoshiro" name = "rand_xoshiro"
version = "0.6.0" version = "0.6.0"

View file

@ -27,6 +27,9 @@ opt-level = 3
[profile.dev.package.bracket-noise] [profile.dev.package.bracket-noise]
opt-level = 3 opt-level = 3
[profile.dev.package.fastnoise-lite]
opt-level = 3
[profile.dev.package.rayon] [profile.dev.package.rayon]
opt-level = 3 opt-level = 3

View file

@ -1,5 +1,5 @@
use glam::Mat4; use glam::Mat4;
use shipyard::{AllStoragesView, AllStoragesViewMut, Component, EntitiesViewMut, EntityId, Get, IntoIter, NonSendSync, Remove, Unique, UniqueView, UniqueViewMut, View, ViewMut}; use shipyard::{AllStoragesView, AllStoragesViewMut, Component, EntityId, Get, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View, ViewMut};
use hashbrown::HashMap; use hashbrown::HashMap;
use uflow::{server::Event, SendMode}; use uflow::{server::Event, SendMode};
use std::net::SocketAddr; use std::net::SocketAddr;
@ -95,7 +95,8 @@ pub fn on_client_disconnect(
for event in &events.0 { for event in &events.0 {
if let Event::Disconnect(addr) = event { if let Event::Disconnect(addr) = event {
let net_client = server.0.client(addr).unwrap(); //XXX: do sth with this:
//let net_client = server.0.client(addr).unwrap();
let Some(&entity_id) = addr_map.0.get(addr) else { let Some(&entity_id) = addr_map.0.get(addr) else {
log::error!("Disconnected client not authenticated, moving on"); log::error!("Disconnected client not authenticated, moving on");
continue; continue;

View file

@ -102,7 +102,7 @@ fn process_chunk_requests(
).unwrap(); ).unwrap();
} }
} else { } else {
let mut chunk = Chunk::new(chunk_position); let mut chunk = Chunk::new();
chunk.state = ChunkState::Loading; chunk.state = ChunkState::Loading;
chunk.subscriptions.insert(message.client_id); chunk.subscriptions.insert(message.client_id);
chunk_manager.chunks.insert(chunk_position, chunk); chunk_manager.chunks.insert(chunk_position, chunk);

View file

@ -1,4 +1,3 @@
use glam::IVec3;
use hashbrown::HashSet; use hashbrown::HashSet;
use nohash_hasher::BuildNoHashHasher; use nohash_hasher::BuildNoHashHasher;
use kubi_shared::{ use kubi_shared::{
@ -14,15 +13,13 @@ pub enum ChunkState {
} }
pub struct Chunk { pub struct Chunk {
pub position: IVec3,
pub state: ChunkState, pub state: ChunkState,
pub blocks: Option<BlockData>, pub blocks: Option<BlockData>,
pub subscriptions: HashSet<ClientId, BuildNoHashHasher<ClientId>>, pub subscriptions: HashSet<ClientId, BuildNoHashHasher<ClientId>>,
} }
impl Chunk { impl Chunk {
pub fn new(position: IVec3) -> Self { pub fn new() -> Self {
Self { Self {
position,
state: ChunkState::Nothing, state: ChunkState::Nothing,
blocks: None, blocks: None,
subscriptions: HashSet::with_capacity_and_hasher(4, BuildNoHashHasher::default()), subscriptions: HashSet::with_capacity_and_hasher(4, BuildNoHashHasher::default()),

View file

@ -41,7 +41,9 @@ impl ChunkTaskManager {
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 {
return
};
ChunkTaskResponse::ChunkLoaded { chunk_position, blocks, queue } ChunkTaskResponse::ChunkLoaded { chunk_position, blocks, queue }
} }
}).unwrap() }).unwrap()

View file

@ -14,14 +14,15 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
serde_with = "3.4" serde_with = "3.4"
bincode = "1.3" bincode = "1.3"
anyhow = "1.0" anyhow = "1.0"
bracket-noise = "0.8" fastnoise-lite = { version = "1.1", features = ["std", "f64"] }
rand = { version = "0.8", default_features = false, features = ["std", "min_const_gen"] } rand = { version = "0.8", default_features = false, features = ["std", "min_const_gen"] }
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

@ -17,19 +17,20 @@ impl Health {
} }
} }
} }
impl PartialEq for Health {
fn eq(&self, other: &Self) -> bool { // impl PartialEq for Health {
self.current == other.current // fn eq(&self, other: &Self) -> bool {
} // self.current == other.current
} // }
impl Eq for Health {} // }
impl PartialOrd for Health { // impl Eq for Health {}
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { // impl PartialOrd for Health {
self.current.partial_cmp(&other.current) // fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
} // self.current.partial_cmp(&other.current)
} // }
impl Ord for Health { // }
fn cmp(&self, other: &Self) -> std::cmp::Ordering { // impl Ord for Health {
self.current.cmp(&other.current) // fn cmp(&self, other: &Self) -> std::cmp::Ordering {
} // self.current.cmp(&other.current)
} // }
// }

View file

@ -1,342 +1,199 @@
use bracket_noise::prelude::*; use std::sync::Arc;
use rand::prelude::*; use atomic::Atomic;
use glam::{IVec3, ivec3, Vec3Swizzles, IVec2}; use bytemuck::{CheckedBitPattern, NoUninit};
use rand_xoshiro::Xoshiro256StarStar; use glam::IVec3;
use static_assertions::const_assert;
use crate::{ use crate::{
chunk::{BlockData, CHUNK_SIZE},
block::Block, block::Block,
chunk::{BlockData, CHUNK_SIZE},
queue::QueuedBlock, queue::QueuedBlock,
}; };
fn mountain_ramp(mut x: f32) -> f32 { pub mod steps;
x *= 2.0; pub mod structures;
if x < 0.4 {
0.5 * x #[repr(u8)]
} else if x < 0.55 { #[derive(Clone, Copy, Debug, Default, NoUninit, CheckedBitPattern)]
4. * (x - 0.4) + 0.2 pub enum AbortState {
} else { #[default]
0.4444 * (x - 0.55) + 0.8 Continue,
Abort,
Aborted,
}
const_assert!(Atomic::<AbortState>::is_lock_free());
pub struct SeedThingy {
pseed: u64,
iseed: i32,
iter: u8,
}
impl SeedThingy {
pub fn new(seed: u64) -> Self {
Self {
pseed: seed,
iseed: (seed & 0x7fffffffu64) as i32,
iter: 0,
}
}
pub fn next_seed(&mut self) -> i32 {
self.iter += 1;
self.iseed = (
self.pseed
.rotate_left((3 * self.iter) as _)
& 0x7fffffff
) as i32;
self.iseed
} }
} }
trait WorldGenStep {
fn local_height(height: i32, chunk_position: IVec3) -> usize { fn initialize(generator: &WorldGenerator) -> Self;
let offset = chunk_position * CHUNK_SIZE as i32; fn generate(&mut self, generator: &mut WorldGenerator);
(height - offset.y).clamp(0, CHUNK_SIZE as i32) as usize
} }
fn local_y_position(height: i32, chunk_position: IVec3) -> Option<usize> { macro_rules! run_steps {
let offset = chunk_position * CHUNK_SIZE as i32; ($gen: expr, $abort: expr, [$($step:ty),* $(,)?]) => {
let position = height - offset.y; (||{
(0..CHUNK_SIZE as i32).contains(&position).then_some(position as usize) 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();
if _chkabt() { return false }
$({
let _ensure_ref: &mut $crate::worldgen::WorldGenerator = $gen;
struct _Ensure0<T: $crate::worldgen::WorldGenStep>(T);
type _Ensure1 = _Ensure0<$step>;
let mut step: _Ensure1 = _Ensure0(<$step>::initialize(&*_ensure_ref));
if _chkabt() { return false }
step.0.generate(_ensure_ref);
if _chkabt() { return false }
})*
true
})()
};
} }
#[derive(Default)]
pub struct WorldGeneratorData {
pub master_height_map: Option<Vec<Vec<i32>>>,
}
pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) { pub struct WorldGenerator {
let offset = chunk_position * CHUNK_SIZE as i32; seed: u64,
let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]); chunk_position: IVec3,
let mut queue = Vec::with_capacity(0); blocks: BlockData,
queue: Vec<QueuedBlock>,
pub data: WorldGeneratorData,
}
let mut smart_place = |blocks: &mut BlockData, position: IVec3, block: Block| { impl WorldGenerator {
fn offset(&self) -> IVec3 {
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) {
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))) {
let event_pos = offset + position; let event_pos = offset + position;
queue.retain(|block: &QueuedBlock| { self.queue.retain(|block: &QueuedBlock| {
block.position != event_pos block.position != event_pos
}); });
queue.push(QueuedBlock { self.queue.push(QueuedBlock {
position: event_pos, position: event_pos,
block_type: block, block_type: block,
soft: true soft: true
}); });
} else { } else {
blocks[position.x as usize][position.y as usize][position.z as usize] = block; self.blocks[position.x as usize][position.y as usize][position.z as usize] = block;
}
};
//STICK
if chunk_position.x == 0 && chunk_position.y == 5 {
for z in 0..CHUNK_SIZE {
blocks[0][0][z] = Block::Stone;
}
}
//
let mut height_noise = FastNoise::seeded(seed);
height_noise.set_fractal_type(FractalType::FBM);
height_noise.set_fractal_octaves(4);
height_noise.set_frequency(0.003);
let mut elevation_noise = FastNoise::seeded(seed.rotate_left(1));
elevation_noise.set_fractal_type(FractalType::FBM);
elevation_noise.set_fractal_octaves(1);
elevation_noise.set_frequency(0.001);
let mut cave_noise_a = FastNoise::seeded(seed.rotate_left(2));
cave_noise_a.set_fractal_type(FractalType::FBM);
cave_noise_a.set_fractal_octaves(2);
cave_noise_a.set_frequency(0.01);
let mut cave_noise_b = FastNoise::seeded(seed.rotate_left(3));
cave_noise_b.set_fractal_type(FractalType::FBM);
cave_noise_b.set_fractal_octaves(3);
cave_noise_b.set_frequency(0.015);
let mut cave_noise_holes = FastNoise::seeded(seed.rotate_left(4));
cave_noise_holes.set_fractal_type(FractalType::FBM);
cave_noise_holes.set_fractal_octaves(2);
cave_noise_holes.set_frequency(0.005);
let mut ravine_nose_line = FastNoise::seeded(seed.rotate_left(5));
ravine_nose_line.set_fractal_type(FractalType::Billow);
ravine_nose_line.set_fractal_octaves(2);
ravine_nose_line.set_frequency(0.005);
let mut ravine_noise_location = FastNoise::seeded(seed.rotate_left(6));
ravine_noise_location.set_fractal_type(FractalType::FBM);
ravine_noise_location.set_fractal_octaves(1);
ravine_noise_location.set_frequency(0.005);
let mut river_noise = FastNoise::seeded(seed.rotate_left(7));
river_noise.set_fractal_type(FractalType::Billow);
river_noise.set_fractal_octaves(2);
river_noise.set_frequency(0.5 * 0.005);
let mut rng = Xoshiro256StarStar::seed_from_u64(
seed
^ (chunk_position.x as u32 as u64)
^ ((chunk_position.z as u32 as u64) << 32)
);
let rng_map_a: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
let rng_map_b: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
//Generate height map
let mut within_heightmap = false;
let mut deco_heightmap = [[None; CHUNK_SIZE]; CHUNK_SIZE];
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let (noise_x, noise_y) = ((offset.x + x as i32) as f32, (offset.z + z as i32) as f32);
//sample noises (that are needed right now)
let raw_heightmap_value = height_noise.get_noise(noise_x, noise_y);
let raw_elevation_value = elevation_noise.get_noise(noise_x, noise_y);
let raw_ravine_location_value = ravine_noise_location.get_noise(noise_x, noise_y);
//compute height
let mut is_surface = true;
let mut river_fill_height = None;
let height = {
let local_elevation = raw_elevation_value.powi(4).sqrt();
let mut height = (mountain_ramp(raw_heightmap_value) * local_elevation * 100.) as i32;
//Flatten valleys
if height < 0 {
height /= 2;
}
//Generate rivers
{
let river_width = (height as f32 / -5.).clamp(0.5, 1.);
let river_value = river_noise.get_noise(noise_x, noise_y);
if ((-0.00625 * river_width)..(0.00625 * river_width)).contains(&(river_value.powi(2))) {
is_surface = false;
river_fill_height = Some(height - 1);
//river_fill_height = Some(-3);
height -= (river_width * 15. * ((0.00625 * river_width) - river_value.powi(2)) * (1. / (0.00625 * river_width))).round() as i32;
}
}
//Generate ravines
if height < 0 && raw_ravine_location_value > 0.4 {
let raw_ravine_value = ravine_nose_line.get_noise(noise_x, noise_y);
if (-0.0125..0.0125).contains(&(raw_ravine_value.powi(2))) {
is_surface = false;
height -= (100. * (0.0125 - raw_ravine_value.powi(2)) * (1. / 0.0125)).round() as i32;
}
}
height
};
//add to heightmap
if is_surface {
deco_heightmap[x][z] = Some(height);
//place dirt
for y in 0..local_height(height, chunk_position) {
blocks[x][y][z] = Block::Dirt;
within_heightmap = true;
}
//place stone
for y in 0..local_height(height - 5 - (raw_heightmap_value * 5.) as i32, chunk_position) {
blocks[x][y][z] = Block::Stone;
within_heightmap = true;
}
//place grass
if let Some(y) = local_y_position(height, chunk_position) {
blocks[x][y][z] = Block::Grass;
within_heightmap = true;
}
} else if let Some(river_fill_height) = river_fill_height {
//Place water
for y in 0..local_height(river_fill_height, chunk_position) {
blocks[x][y][z] = Block::Water;
within_heightmap = true;
}
//Place stone
for y in 0..local_height(height, chunk_position) {
blocks[x][y][z] = Block::Stone;
within_heightmap = true;
}
//Place dirt
if let Some(y) = local_y_position(height, chunk_position) {
blocks[x][y][z] = Block::Dirt;
within_heightmap = true;
}
} else {
//Place stone
for y in 0..local_height(height, chunk_position) {
blocks[x][y][z] = Block::Stone;
within_heightmap = true;
}
}
} }
} }
//Carve out caves fn global_position(&self, position: IVec3) -> IVec3 {
if within_heightmap { self.offset() + position
for z in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for x in 0..CHUNK_SIZE {
if blocks[x][y][z] != Block::Stone { continue }
let cave_size = ((offset.y + y as i32) as f32 / -100.).clamp(0., 1.);
let inv_cave_size = 1. - cave_size;
if cave_size < 0.1 { continue }
let position = ivec3(x as i32, y as i32, z as i32) + offset;
let is_cave = || {
let raw_cavemap_value_a = cave_noise_a.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
let raw_cavemap_value_b = cave_noise_b.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
((cave_size * -0.15)..=(cave_size * 0.15)).contains(&raw_cavemap_value_a) &&
((cave_size * -0.15)..=(cave_size * 0.15)).contains(&raw_cavemap_value_b)
};
let is_hole_cave = || {
let raw_cavemap_value_holes = cave_noise_holes.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
((0.9 + (0.1 * inv_cave_size))..=1.0).contains(&raw_cavemap_value_holes.abs())
};
if is_cave() || is_hole_cave() {
blocks[x][y][z] = Block::Air;
if deco_heightmap[x][z] == Some(y as i32 + offset.y) {
deco_heightmap[x][z] = None
} }
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)
} }
/// crude hash of self.seed and x
fn seeded_hash(&self, x: impl std::hash::Hash) -> u64 {
//use std::hash to hash the seed and x
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
x.hash(&mut hasher);
self.seed.hash(&mut hasher);
hasher.finish()
} }
pub fn new(chunk_position: IVec3, seed: u64) -> Self {
Self {
seed,
chunk_position,
blocks: Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]),
queue: Vec::with_capacity(0),
data: Default::default(),
} }
} }
//Add decorations /// Generate the chunk.
for x in 0..CHUNK_SIZE { ///
for z in 0..CHUNK_SIZE { /// Will return `None` only if the generation was aborted.
//get height pub fn generate(mut self, abort: Option<Arc<Atomic<AbortState>>>) -> Option<(BlockData, Vec<QueuedBlock>)> {
let Some(height) = deco_heightmap[x][z] else { continue }; run_steps!(&mut self, abort, [
//check for air steps::_01_terrain::TerrainStep,
// if blocks[x][local_y][z] == Block::Air { steps::_02_water::WaterStep,
// continue steps::_03_caves::CaveStep,
// } steps::_04_layers::LayersStep,
//place tall grass steps::_05_decorate::DecorateStep,
if rng_map_a[x][z] < 0.03 { steps::_06_trees::TreesStep,
if let Some(y) = local_y_position(height + 1, chunk_position) { ]).then_some((self.blocks, self.queue))
blocks[x][y][z] = Block::TallGrass;
} }
} }
//place trees!
if rng_map_a[x][z] < 0.001 { pub fn generate_world(chunk_position: IVec3, seed: u64, abort: Option<Arc<Atomic<AbortState>>>) -> Option<(BlockData, Vec<QueuedBlock>)> {
//Replace grass with dirt under the tree //TODO: pass through None for abort
if let Some(y) = local_y_position(height, chunk_position) { WorldGenerator::new(chunk_position, seed).generate(abort)
blocks[x][y][z] = Block::Dirt;
}
//Place wood (no smart_place needed here!)
let tree_height = 4 + (rng_map_b[x][z] * 3.).round() as i32;
for tree_y in 0..tree_height {
if let Some(y) = local_y_position(height + 1 + tree_y, chunk_position) {
blocks[x][y][z] = Block::Wood;
}
}
let tree_height = 4 + (rng_map_b[x][z] * 3.).round() as i32;
//Place leaf blocks
if let Some(y) = local_y_position(height + 1, chunk_position) {
let tree_pos = ivec3(x as i32, y as i32, z as i32);
// Place wood (smart_place)
// for tree_y in 0..tree_height {
// smart_place(&mut blocks, tree_pos + tree_y * IVec3::Y, Block::Wood);
// }
// Part that wraps around the tree
{
let tree_leaf_height = tree_height - 3;
let leaf_width = 2;
for tree_y in tree_leaf_height..tree_height {
for tree_x in (-leaf_width)..=leaf_width {
for tree_z in (-leaf_width)..=leaf_width {
let tree_offset = ivec3(tree_x, tree_y, tree_z);
if tree_offset.xz() == IVec2::ZERO { continue }
smart_place(&mut blocks, tree_pos + tree_offset, Block::Leaf);
}
}
}
}
//part above the tree
{
let leaf_above_height = 2;
let leaf_width = 1;
for tree_y in tree_height..(tree_height + leaf_above_height) {
for tree_x in (-leaf_width)..=leaf_width {
for tree_z in (-leaf_width)..=leaf_width {
let tree_offset = ivec3(tree_x, tree_y, tree_z);
smart_place(&mut blocks, tree_pos + tree_offset, Block::Leaf);
}
}
}
}
}
}
}
}
(blocks, queue)
// let mut cave_noise = FastNoise::seeded(seed);
// cave_noise.set_fractal_type(FractalType::FBM);
// cave_noise.set_frequency(0.1);
// let mut dirt_noise = FastNoise::seeded(seed.rotate_left(1));
// dirt_noise.set_fractal_type(FractalType::FBM);
// dirt_noise.set_frequency(0.1);
//
// if chunk_position.y >= 0 {
// if chunk_position.y == 0 {
// for x in 0..CHUNK_SIZE {
// for z in 0..CHUNK_SIZE {
// blocks[x][0][z] = Block::Dirt;
// blocks[x][1][z] = Block::Grass;
// }
// }
// }
// } else {
// for x in 0..CHUNK_SIZE {
// for y in 0..CHUNK_SIZE {
// for z in 0..CHUNK_SIZE {
// let position = ivec3(x as i32, y as i32, z as i32) + offset;
// let v_cave_noise = cave_noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32) * (-position.y as f32 - 10.0).clamp(0., 1.);
// let v_dirt_noise = dirt_noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32) * (-position.y as f32).clamp(0., 1.);
// if v_cave_noise > 0.5 {
// blocks[x][y][z] = Block::Stone;
// } else if v_dirt_noise > 0.5 {
// blocks[x][y][z] = Block::Dirt;
// }
// }
// }
// }
// }
// blocks
} }

View file

@ -0,0 +1,6 @@
pub mod _01_terrain;
pub mod _02_water;
pub mod _03_caves;
pub mod _04_layers;
pub mod _05_decorate;
pub mod _06_trees;

View file

@ -0,0 +1,34 @@
use fastnoise_lite::{FastNoiseLite, FractalType};
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::super::{SeedThingy, WorldGenStep, WorldGenerator};
pub struct TerrainStep {
noise: FastNoiseLite,
}
impl WorldGenStep for TerrainStep {
fn initialize(generator: &WorldGenerator) -> Self {
let mut seeder = SeedThingy::new(generator.seed);
let mut noise = FastNoiseLite::with_seed(seeder.next_seed());
noise.set_fractal_type(Some(FractalType::FBm));
noise.set_fractal_octaves(Some(4));
noise.set_frequency(Some(0.003));
Self { noise }
}
fn generate(&mut self, gen: &mut WorldGenerator) {
let mut height_map = vec![vec![0; CHUNK_SIZE]; CHUNK_SIZE];
for x in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 {
let global_xz = gen.global_position(ivec3(x, 0, z));
let height = (self.noise.get_noise_2d(global_xz.x as f64, global_xz.z as f64) * 32.0) as i32;
height_map[x as usize][z as usize] = height;
for y in 0..gen.local_height(height) {
gen.place(ivec3(x, y, z), Block::Stone);
}
}
}
gen.data.master_height_map = Some(height_map);
}
}

View file

@ -0,0 +1,20 @@
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::super::{WorldGenerator, WorldGenStep};
pub const WATER_LEVEL: i32 = 0;
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(WATER_LEVEL) {
gen.place_if_empty(ivec3(x, y, z), Block::Water);
}
}
}
}
}

View file

@ -0,0 +1,49 @@
use fastnoise_lite::{FastNoiseLite, FractalType};
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::super::{SeedThingy, WorldGenStep, WorldGenerator};
pub struct CaveStep {
a: FastNoiseLite,
b: FastNoiseLite,
}
impl WorldGenStep for CaveStep {
fn initialize(gen: &WorldGenerator) -> Self {
let mut seeder = SeedThingy::new(gen.seed);
let mut a = FastNoiseLite::with_seed(seeder.next_seed());
a.set_fractal_type(Some(FractalType::FBm));
a.set_fractal_octaves(Some(2));
let mut b = FastNoiseLite::with_seed(seeder.next_seed());
b.set_fractal_type(Some(FractalType::FBm));
b.set_fractal_octaves(Some(2));
Self { a, b }
}
fn generate(&mut self, gen: &mut WorldGenerator) {
for x in 0..CHUNK_SIZE as i32 {
for y in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 {
let cave_size = ((gen.offset().y + y - 50) as f64 / -200.).clamp(0., 1.) as f32;
let inv_cave_size = 1. - cave_size;
if cave_size < 0.1 { continue }
let pos = ivec3(x, y, z);
if gen.query(pos) != Block::Stone { continue }
let pos_global = gen.global_position(pos);
let noise_a = self.a.get_noise_3d(pos_global.x as f64, pos_global.y as f64, pos_global.z as f64) * 0.5 + 0.5;
let noise_b = self.b.get_noise_3d(pos_global.x as f64, pos_global.y as f64, pos_global.z as f64) * 0.5 + 0.5;
if noise_a.min(noise_b) > (0.6 + 0.4 * inv_cave_size) {
gen.place(pos, Block::Air);
}
//TODO
}
}
}
}
}

View file

@ -0,0 +1,36 @@
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::{
_02_water::WATER_LEVEL,
super::{WorldGenStep, WorldGenerator}
};
pub struct LayersStep;
impl WorldGenStep for LayersStep {
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 {
let terrain_height = gen.data.master_height_map.as_ref().unwrap()[x as usize][z as usize];
// Dirt layer height, naturally gets thinner as height gets deeper
let mut dirt_layer_height = (((terrain_height as f32 + 15.) / 20.).clamp(0., 1.) * 8.).round() as i32;
dirt_layer_height -= (gen.seeded_hash((x, z, 1)) & 1) as i32; //+ (gen.seeded_hash((x, z, 0xbau8)) & 1) as i32;
// Place dirt layer
for y in gen.local_height(terrain_height - dirt_layer_height)..gen.local_height(terrain_height) {
gen.place(ivec3(x, y, z), Block::Dirt);
}
// If above water level, place grass
if terrain_height >= WATER_LEVEL {
if let Some(local_y) = gen.local_y_position(terrain_height - 1) {
gen.place(ivec3(x, local_y, z), Block::Grass);
}
}
}
}
}
}

View file

@ -0,0 +1,31 @@
use glam::ivec3;
use crate::{block::Block, chunk::CHUNK_SIZE};
use super::{
_02_water::WATER_LEVEL,
super::{WorldGenStep, WorldGenerator},
};
pub struct DecorateStep;
impl WorldGenStep for DecorateStep {
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 {
let global_xz = gen.global_position(ivec3(x, 0, z));
let terrain_height = gen.data.master_height_map.as_ref().unwrap()[x as usize][z as usize];
//Place tall grass
if terrain_height >= WATER_LEVEL {
if let Some(local_y) = gen.local_y_position(terrain_height) {
if (gen.seeded_hash((global_xz.x, global_xz.z)) & 0xf) == 0xf {
gen.place_if_empty(ivec3(x, local_y, z), Block::TallGrass);
}
}
}
}
}
}
}

View file

@ -0,0 +1,44 @@
use fastnoise_lite::{FastNoiseLite, NoiseType};
use glam::ivec3;
use crate::{chunk::CHUNK_SIZE, worldgen::SeedThingy};
use super::_02_water::WATER_LEVEL;
use crate::worldgen::{
WorldGenStep, WorldGenerator,
structures::{Structure, TreeStructure},
};
pub struct TreesStep {
density_noise: FastNoiseLite,
}
impl WorldGenStep for TreesStep {
fn initialize(gen: &WorldGenerator) -> Self {
let mut seeder = SeedThingy::new(gen.seed.rotate_left(5));
let mut density_noise = FastNoiseLite::with_seed(seeder.next_seed());
density_noise.set_noise_type(Some(NoiseType::OpenSimplex2));
density_noise.set_frequency(Some(0.008));
Self { density_noise }
}
fn generate(&mut self, gen: &mut WorldGenerator) {
for x in 0..CHUNK_SIZE as i32 {
for z in 0..CHUNK_SIZE as i32 {
let terrain_height = gen.data.master_height_map.as_ref().unwrap()[x as usize][z as usize];
if terrain_height < WATER_LEVEL { continue }
let global_xz = gen.global_position(ivec3(x, 0, z));
let mut density = self.density_noise.get_noise_2d(global_xz.x as f64, global_xz.z as f64) * 0.5 + 0.5;
density = density.powi(3);
if gen.seeded_hash((global_xz.x, global_xz.z, 0xfef)) & 0xff >= (density * 7.).round() as u64 {
continue
}
let tree = TreeStructure::default();
if let Some(local_y) = gen.local_y_position(terrain_height) {
tree.place(gen, ivec3(x, local_y, z));
}
}
}
}
}

View file

@ -0,0 +1,9 @@
use glam::IVec3;
use super::WorldGenerator;
mod tree;
pub use tree::TreeStructure;
pub trait Structure {
fn place(&self, gen: &mut WorldGenerator, root_pos: IVec3);
}

View file

@ -0,0 +1,58 @@
use glam::IVec3;
use super::Structure;
use crate::{block::Block, worldgen::WorldGenerator};
#[derive(Clone, Copy, Debug)]
pub struct TreeStructure {
pub height: i32,
}
impl Default for TreeStructure {
fn default() -> Self {
Self { height: 5 }
}
}
impl Structure for TreeStructure {
fn place(&self, gen: &mut WorldGenerator, root: IVec3) {
//check the block below the tree, if it's grass, replace it with dirt
//XXX: This won't work if root.y == 0
if root.y != 0 && gen.query(root - IVec3::Y) == Block::Grass {
gen.place(root - IVec3::Y, Block::Dirt);
}
//Tree stem
for y in root.y..root.y + self.height {
gen.place_or_queue(IVec3::new(root.x, y, root.z), Block::Wood);
}
//Tree leaves
//Try to create the following shape:
//(a 5x2x5 cube that wraps around the stem with a 3x1x3 cube on top)
// xxx
// xx|xx
// xx|xx
// |
for y in 0..=4_i32 {
for x in -2..=2_i32 {
for z in -2..=2_i32 {
//Do not overwrite the stem
if y < 3 && x == 0 && z == 0 {
continue
}
// Cut off the corners of the top layer
if y >= 3 && (x.abs() > 1 || z.abs() > 1) {
continue
}
let position = IVec3::new(
root.x + x,
root.y + self.height - 3 + y,
root.z + z
);
gen.place_or_queue(position, Block::Leaf);
}
}
}
}
}

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,11 +1,11 @@
use shipyard::{AllStoragesView, UniqueViewMut}; use shipyard::{AllStoragesView, UniqueViewMut};
use std::{env, net::SocketAddr, fs::OpenOptions, path::{Path, PathBuf}, str::FromStr, sync::{Arc, RwLock}}; use std::{env, net::SocketAddr, fs::OpenOptions, path::Path};
use anyhow::Result; use anyhow::Result;
use crate::{ use crate::{
networking::{GameType, ServerAddress}, networking::{GameType, ServerAddress},
state::{GameState, NextState} state::{GameState, NextState}
}; };
use kubi_shared::data::{WorldSaveFile, SharedSaveFile}; use kubi_shared::data::WorldSaveFile;
fn open_local_save_file(path: &Path) -> Result<WorldSaveFile> { fn open_local_save_file(path: &Path) -> Result<WorldSaveFile> {
let mut save_file = WorldSaveFile::new({ let mut save_file = WorldSaveFile::new({

View file

@ -1,4 +1,14 @@
#![allow(clippy::too_many_arguments)] // allowed because systems often need a lot of arguments //TODO move lints to workspace Cargo.toml
#![allow(
clippy::too_many_arguments, // allowed because systems often need a lot of argumentss
clippy::enum_variant_names,
clippy::type_complexity
)]
#![forbid(
static_mut_refs,
unsafe_op_in_unsafe_fn,
rust_2024_compatibility,
)]
use shipyard::{ use shipyard::{
World, Workload, IntoWorkload, World, Workload, IntoWorkload,

View file

@ -1,5 +1,4 @@
use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator};
use winit::event_loop::ControlFlow;
use std::net::SocketAddr; use std::net::SocketAddr;
use uflow::{ use uflow::{
client::{Client, Config as ClientConfig, Event as ClientEvent}, client::{Client, Config as ClientConfig, Event as ClientEvent},

View file

@ -1,4 +1,4 @@
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, Unique, UniqueView, UniqueViewMut, View}; use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, Unique, UniqueViewMut, View};
use uflow::{client::Event as ClientEvent, SendMode}; use uflow::{client::Event as ClientEvent, SendMode};
use kubi_shared::networking::{ use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType}, messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},

View file

@ -1,9 +1,7 @@
use std::f32::consts::PI; use glam::uvec2;
use glam::{uvec2, Vec2};
use hui::{ use hui::{
draw::{ImageHandle, TextureFormat}, draw::{ImageHandle, TextureFormat},
element::{container::Container, image::Image, transformer::ElementTransformExt, UiElementExt}, element::{container::Container, image::Image, UiElementExt},
layout::Alignment, layout::Alignment,
size size
}; };

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,6 @@
use glam::{IVec3, ivec3}; use glam::{IVec3, ivec3};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use kubi_shared::block::{Block, BlockTexture, RenderType, Transparency}; use kubi_shared::block::{Block, RenderType, Transparency};
use crate::world::chunk::CHUNK_SIZE; use crate::world::chunk::CHUNK_SIZE;
use crate::rendering::world::ChunkVertex; use crate::rendering::world::ChunkVertex;

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 }
} }
}); });