mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-29 02:08:48 -06:00
Compare commits
No commits in common. "6a96d6c3d3ef68d2b956b553d4909c7c23514ed4" and "b34f1a94b180d65e6c7b40c496919ae6b75b1a73" have entirely different histories.
6a96d6c3d3
...
b34f1a94b1
|
@ -1,342 +1,334 @@
|
||||||
use bracket_noise::prelude::*;
|
use bracket_noise::prelude::*;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use glam::{IVec3, ivec3, Vec3Swizzles, IVec2};
|
use glam::{IVec3, ivec3, Vec3Swizzles, IVec2};
|
||||||
use rand_xoshiro::Xoshiro256StarStar;
|
use rand_xoshiro::Xoshiro256StarStar;
|
||||||
use crate::{
|
use crate::{
|
||||||
chunk::{BlockData, CHUNK_SIZE},
|
chunk::{BlockData, CHUNK_SIZE},
|
||||||
block::Block,
|
block::Block,
|
||||||
queue::QueuedBlock,
|
queue::QueuedBlock,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn mountain_ramp(mut x: f32) -> f32 {
|
fn mountain_ramp(mut x: f32) -> f32 {
|
||||||
x *= 2.0;
|
x *= 2.0;
|
||||||
if x < 0.4 {
|
if x < 0.4 {
|
||||||
0.5 * x
|
0.5 * x
|
||||||
} else if x < 0.55 {
|
} else if x < 0.55 {
|
||||||
4. * (x - 0.4) + 0.2
|
4. * (x - 0.4) + 0.2
|
||||||
} else {
|
} else {
|
||||||
0.4444 * (x - 0.55) + 0.8
|
0.4444 * (x - 0.55) + 0.8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn local_height(height: i32, chunk_position: IVec3) -> usize {
|
fn local_height(height: i32, chunk_position: IVec3) -> usize {
|
||||||
let offset = chunk_position * CHUNK_SIZE as i32;
|
let offset = chunk_position * CHUNK_SIZE as i32;
|
||||||
(height - offset.y).clamp(0, CHUNK_SIZE as i32) as usize
|
(height - offset.y).clamp(0, CHUNK_SIZE as i32) as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
fn local_y_position(height: i32, chunk_position: IVec3) -> Option<usize> {
|
fn local_y_position(height: i32, chunk_position: IVec3) -> Option<usize> {
|
||||||
let offset = chunk_position * CHUNK_SIZE as i32;
|
let offset = chunk_position * CHUNK_SIZE as i32;
|
||||||
let position = height - offset.y;
|
let position = height - offset.y;
|
||||||
(0..CHUNK_SIZE as i32).contains(&position).then_some(position as usize)
|
(0..CHUNK_SIZE as i32).contains(&position).then_some(position as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) {
|
pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) {
|
||||||
let offset = chunk_position * CHUNK_SIZE as i32;
|
let offset = chunk_position * CHUNK_SIZE as i32;
|
||||||
let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]);
|
let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]);
|
||||||
let mut queue = Vec::with_capacity(0);
|
let mut queue = Vec::with_capacity(0);
|
||||||
|
|
||||||
let mut smart_place = |blocks: &mut BlockData, position: IVec3, block: Block| {
|
let mut smart_place = |blocks: &mut BlockData, position: IVec3, block: Block| {
|
||||||
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| {
|
queue.retain(|block: &QueuedBlock| {
|
||||||
block.position != event_pos
|
block.position != event_pos
|
||||||
});
|
});
|
||||||
queue.push(QueuedBlock {
|
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;
|
blocks[position.x as usize][position.y as usize][position.z as usize] = block;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//STICK
|
let mut height_noise = FastNoise::seeded(seed);
|
||||||
if chunk_position.x == 0 && chunk_position.y == 5 {
|
height_noise.set_fractal_type(FractalType::FBM);
|
||||||
for z in 0..CHUNK_SIZE {
|
height_noise.set_fractal_octaves(4);
|
||||||
blocks[0][0][z] = Block::Stone;
|
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);
|
||||||
let mut height_noise = FastNoise::seeded(seed);
|
elevation_noise.set_frequency(0.001);
|
||||||
height_noise.set_fractal_type(FractalType::FBM);
|
|
||||||
height_noise.set_fractal_octaves(4);
|
let mut cave_noise_a = FastNoise::seeded(seed.rotate_left(2));
|
||||||
height_noise.set_frequency(0.003);
|
cave_noise_a.set_fractal_type(FractalType::FBM);
|
||||||
|
cave_noise_a.set_fractal_octaves(2);
|
||||||
let mut elevation_noise = FastNoise::seeded(seed.rotate_left(1));
|
cave_noise_a.set_frequency(0.01);
|
||||||
elevation_noise.set_fractal_type(FractalType::FBM);
|
|
||||||
elevation_noise.set_fractal_octaves(1);
|
let mut cave_noise_b = FastNoise::seeded(seed.rotate_left(3));
|
||||||
elevation_noise.set_frequency(0.001);
|
cave_noise_b.set_fractal_type(FractalType::FBM);
|
||||||
|
cave_noise_b.set_fractal_octaves(3);
|
||||||
let mut cave_noise_a = FastNoise::seeded(seed.rotate_left(2));
|
cave_noise_b.set_frequency(0.015);
|
||||||
cave_noise_a.set_fractal_type(FractalType::FBM);
|
|
||||||
cave_noise_a.set_fractal_octaves(2);
|
let mut cave_noise_holes = FastNoise::seeded(seed.rotate_left(4));
|
||||||
cave_noise_a.set_frequency(0.01);
|
cave_noise_holes.set_fractal_type(FractalType::FBM);
|
||||||
|
cave_noise_holes.set_fractal_octaves(2);
|
||||||
let mut cave_noise_b = FastNoise::seeded(seed.rotate_left(3));
|
cave_noise_holes.set_frequency(0.005);
|
||||||
cave_noise_b.set_fractal_type(FractalType::FBM);
|
|
||||||
cave_noise_b.set_fractal_octaves(3);
|
let mut ravine_nose_line = FastNoise::seeded(seed.rotate_left(5));
|
||||||
cave_noise_b.set_frequency(0.015);
|
ravine_nose_line.set_fractal_type(FractalType::Billow);
|
||||||
|
ravine_nose_line.set_fractal_octaves(2);
|
||||||
let mut cave_noise_holes = FastNoise::seeded(seed.rotate_left(4));
|
ravine_nose_line.set_frequency(0.005);
|
||||||
cave_noise_holes.set_fractal_type(FractalType::FBM);
|
|
||||||
cave_noise_holes.set_fractal_octaves(2);
|
let mut ravine_noise_location = FastNoise::seeded(seed.rotate_left(6));
|
||||||
cave_noise_holes.set_frequency(0.005);
|
ravine_noise_location.set_fractal_type(FractalType::FBM);
|
||||||
|
ravine_noise_location.set_fractal_octaves(1);
|
||||||
let mut ravine_nose_line = FastNoise::seeded(seed.rotate_left(5));
|
ravine_noise_location.set_frequency(0.005);
|
||||||
ravine_nose_line.set_fractal_type(FractalType::Billow);
|
|
||||||
ravine_nose_line.set_fractal_octaves(2);
|
let mut river_noise = FastNoise::seeded(seed.rotate_left(7));
|
||||||
ravine_nose_line.set_frequency(0.005);
|
river_noise.set_fractal_type(FractalType::Billow);
|
||||||
|
river_noise.set_fractal_octaves(2);
|
||||||
let mut ravine_noise_location = FastNoise::seeded(seed.rotate_left(6));
|
river_noise.set_frequency(0.5 * 0.005);
|
||||||
ravine_noise_location.set_fractal_type(FractalType::FBM);
|
|
||||||
ravine_noise_location.set_fractal_octaves(1);
|
let mut rng = Xoshiro256StarStar::seed_from_u64(
|
||||||
ravine_noise_location.set_frequency(0.005);
|
seed
|
||||||
|
^ (chunk_position.x as u32 as u64)
|
||||||
let mut river_noise = FastNoise::seeded(seed.rotate_left(7));
|
^ ((chunk_position.z as u32 as u64) << 32)
|
||||||
river_noise.set_fractal_type(FractalType::Billow);
|
);
|
||||||
river_noise.set_fractal_octaves(2);
|
let rng_map_a: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
|
||||||
river_noise.set_frequency(0.5 * 0.005);
|
let rng_map_b: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
|
||||||
|
|
||||||
let mut rng = Xoshiro256StarStar::seed_from_u64(
|
//Generate height map
|
||||||
seed
|
let mut within_heightmap = false;
|
||||||
^ (chunk_position.x as u32 as u64)
|
let mut deco_heightmap = [[None; CHUNK_SIZE]; CHUNK_SIZE];
|
||||||
^ ((chunk_position.z as u32 as u64) << 32)
|
|
||||||
);
|
for x in 0..CHUNK_SIZE {
|
||||||
let rng_map_a: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
|
for z in 0..CHUNK_SIZE {
|
||||||
let rng_map_b: [[f32; CHUNK_SIZE]; CHUNK_SIZE] = rng.gen();
|
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)
|
||||||
//Generate height map
|
let raw_heightmap_value = height_noise.get_noise(noise_x, noise_y);
|
||||||
let mut within_heightmap = false;
|
let raw_elevation_value = elevation_noise.get_noise(noise_x, noise_y);
|
||||||
let mut deco_heightmap = [[None; CHUNK_SIZE]; CHUNK_SIZE];
|
let raw_ravine_location_value = ravine_noise_location.get_noise(noise_x, noise_y);
|
||||||
|
//compute height
|
||||||
for x in 0..CHUNK_SIZE {
|
let mut is_surface = true;
|
||||||
for z in 0..CHUNK_SIZE {
|
let mut river_fill_height = None;
|
||||||
let (noise_x, noise_y) = ((offset.x + x as i32) as f32, (offset.z + z as i32) as f32);
|
let height = {
|
||||||
//sample noises (that are needed right now)
|
let local_elevation = raw_elevation_value.powi(4).sqrt();
|
||||||
let raw_heightmap_value = height_noise.get_noise(noise_x, noise_y);
|
let mut height = (mountain_ramp(raw_heightmap_value) * local_elevation * 100.) as i32;
|
||||||
let raw_elevation_value = elevation_noise.get_noise(noise_x, noise_y);
|
//Flatten valleys
|
||||||
let raw_ravine_location_value = ravine_noise_location.get_noise(noise_x, noise_y);
|
if height < 0 {
|
||||||
//compute height
|
height /= 2;
|
||||||
let mut is_surface = true;
|
}
|
||||||
let mut river_fill_height = None;
|
//Generate rivers
|
||||||
let height = {
|
{
|
||||||
let local_elevation = raw_elevation_value.powi(4).sqrt();
|
let river_width = (height as f32 / -5.).clamp(0.5, 1.);
|
||||||
let mut height = (mountain_ramp(raw_heightmap_value) * local_elevation * 100.) as i32;
|
let river_value = river_noise.get_noise(noise_x, noise_y);
|
||||||
//Flatten valleys
|
if ((-0.00625 * river_width)..(0.00625 * river_width)).contains(&(river_value.powi(2))) {
|
||||||
if height < 0 {
|
is_surface = false;
|
||||||
height /= 2;
|
river_fill_height = Some(height - 1);
|
||||||
}
|
//river_fill_height = Some(-3);
|
||||||
//Generate rivers
|
height -= (river_width * 15. * ((0.00625 * river_width) - river_value.powi(2)) * (1. / (0.00625 * river_width))).round() as i32;
|
||||||
{
|
}
|
||||||
let river_width = (height as f32 / -5.).clamp(0.5, 1.);
|
}
|
||||||
let river_value = river_noise.get_noise(noise_x, noise_y);
|
//Generate ravines
|
||||||
if ((-0.00625 * river_width)..(0.00625 * river_width)).contains(&(river_value.powi(2))) {
|
if height < 0 && raw_ravine_location_value > 0.4 {
|
||||||
is_surface = false;
|
let raw_ravine_value = ravine_nose_line.get_noise(noise_x, noise_y);
|
||||||
river_fill_height = Some(height - 1);
|
if (-0.0125..0.0125).contains(&(raw_ravine_value.powi(2))) {
|
||||||
//river_fill_height = Some(-3);
|
is_surface = false;
|
||||||
height -= (river_width * 15. * ((0.00625 * river_width) - river_value.powi(2)) * (1. / (0.00625 * river_width))).round() as i32;
|
height -= (100. * (0.0125 - raw_ravine_value.powi(2)) * (1. / 0.0125)).round() as i32;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Generate ravines
|
height
|
||||||
if height < 0 && raw_ravine_location_value > 0.4 {
|
};
|
||||||
let raw_ravine_value = ravine_nose_line.get_noise(noise_x, noise_y);
|
//add to heightmap
|
||||||
if (-0.0125..0.0125).contains(&(raw_ravine_value.powi(2))) {
|
if is_surface {
|
||||||
is_surface = false;
|
deco_heightmap[x][z] = Some(height);
|
||||||
height -= (100. * (0.0125 - raw_ravine_value.powi(2)) * (1. / 0.0125)).round() as i32;
|
//place dirt
|
||||||
}
|
for y in 0..local_height(height, chunk_position) {
|
||||||
}
|
blocks[x][y][z] = Block::Dirt;
|
||||||
height
|
within_heightmap = true;
|
||||||
};
|
}
|
||||||
//add to heightmap
|
//place stone
|
||||||
if is_surface {
|
for y in 0..local_height(height - 5 - (raw_heightmap_value * 5.) as i32, chunk_position) {
|
||||||
deco_heightmap[x][z] = Some(height);
|
blocks[x][y][z] = Block::Stone;
|
||||||
//place dirt
|
within_heightmap = true;
|
||||||
for y in 0..local_height(height, chunk_position) {
|
}
|
||||||
blocks[x][y][z] = Block::Dirt;
|
//place grass
|
||||||
within_heightmap = true;
|
if let Some(y) = local_y_position(height, chunk_position) {
|
||||||
}
|
blocks[x][y][z] = Block::Grass;
|
||||||
//place stone
|
within_heightmap = true;
|
||||||
for y in 0..local_height(height - 5 - (raw_heightmap_value * 5.) as i32, chunk_position) {
|
}
|
||||||
blocks[x][y][z] = Block::Stone;
|
} else if let Some(river_fill_height) = river_fill_height {
|
||||||
within_heightmap = true;
|
//Place water
|
||||||
}
|
for y in 0..local_height(river_fill_height, chunk_position) {
|
||||||
//place grass
|
blocks[x][y][z] = Block::Water;
|
||||||
if let Some(y) = local_y_position(height, chunk_position) {
|
within_heightmap = true;
|
||||||
blocks[x][y][z] = Block::Grass;
|
}
|
||||||
within_heightmap = true;
|
//Place stone
|
||||||
}
|
for y in 0..local_height(height, chunk_position) {
|
||||||
} else if let Some(river_fill_height) = river_fill_height {
|
blocks[x][y][z] = Block::Stone;
|
||||||
//Place water
|
within_heightmap = true;
|
||||||
for y in 0..local_height(river_fill_height, chunk_position) {
|
}
|
||||||
blocks[x][y][z] = Block::Water;
|
//Place dirt
|
||||||
within_heightmap = true;
|
if let Some(y) = local_y_position(height, chunk_position) {
|
||||||
}
|
blocks[x][y][z] = Block::Dirt;
|
||||||
//Place stone
|
within_heightmap = true;
|
||||||
for y in 0..local_height(height, chunk_position) {
|
}
|
||||||
blocks[x][y][z] = Block::Stone;
|
} else {
|
||||||
within_heightmap = true;
|
//Place stone
|
||||||
}
|
for y in 0..local_height(height, chunk_position) {
|
||||||
//Place dirt
|
blocks[x][y][z] = Block::Stone;
|
||||||
if let Some(y) = local_y_position(height, chunk_position) {
|
within_heightmap = true;
|
||||||
blocks[x][y][z] = Block::Dirt;
|
}
|
||||||
within_heightmap = true;
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
//Place stone
|
|
||||||
for y in 0..local_height(height, chunk_position) {
|
//Carve out caves
|
||||||
blocks[x][y][z] = Block::Stone;
|
if within_heightmap {
|
||||||
within_heightmap = true;
|
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.);
|
||||||
//Carve out caves
|
let inv_cave_size = 1. - cave_size;
|
||||||
if within_heightmap {
|
if cave_size < 0.1 { continue }
|
||||||
for z in 0..CHUNK_SIZE {
|
|
||||||
for y in 0..CHUNK_SIZE {
|
let position = ivec3(x as i32, y as i32, z as i32) + offset;
|
||||||
for x in 0..CHUNK_SIZE {
|
|
||||||
if blocks[x][y][z] != Block::Stone { continue }
|
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 cave_size = ((offset.y + y as i32) as f32 / -100.).clamp(0., 1.);
|
let raw_cavemap_value_b = cave_noise_b.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
|
||||||
let inv_cave_size = 1. - cave_size;
|
((cave_size * -0.15)..=(cave_size * 0.15)).contains(&raw_cavemap_value_a) &&
|
||||||
if cave_size < 0.1 { continue }
|
((cave_size * -0.15)..=(cave_size * 0.15)).contains(&raw_cavemap_value_b)
|
||||||
|
};
|
||||||
let position = ivec3(x as i32, y as i32, z as i32) + offset;
|
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);
|
||||||
let is_cave = || {
|
((0.9 + (0.1 * inv_cave_size))..=1.0).contains(&raw_cavemap_value_holes.abs())
|
||||||
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) &&
|
if is_cave() || is_hole_cave() {
|
||||||
((cave_size * -0.15)..=(cave_size * 0.15)).contains(&raw_cavemap_value_b)
|
blocks[x][y][z] = Block::Air;
|
||||||
};
|
if deco_heightmap[x][z] == Some(y as i32 + offset.y) {
|
||||||
let is_hole_cave = || {
|
deco_heightmap[x][z] = None
|
||||||
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
|
//Add decorations
|
||||||
}
|
for x in 0..CHUNK_SIZE {
|
||||||
}
|
for z in 0..CHUNK_SIZE {
|
||||||
}
|
//get height
|
||||||
}
|
let Some(height) = deco_heightmap[x][z] else { continue };
|
||||||
}
|
//check for air
|
||||||
}
|
// if blocks[x][local_y][z] == Block::Air {
|
||||||
|
// continue
|
||||||
//Add decorations
|
// }
|
||||||
for x in 0..CHUNK_SIZE {
|
//place tall grass
|
||||||
for z in 0..CHUNK_SIZE {
|
if rng_map_a[x][z] < 0.03 {
|
||||||
//get height
|
if let Some(y) = local_y_position(height + 1, chunk_position) {
|
||||||
let Some(height) = deco_heightmap[x][z] else { continue };
|
blocks[x][y][z] = Block::TallGrass;
|
||||||
//check for air
|
}
|
||||||
// if blocks[x][local_y][z] == Block::Air {
|
}
|
||||||
// continue
|
//place trees!
|
||||||
// }
|
if rng_map_a[x][z] < 0.001 {
|
||||||
//place tall grass
|
//Replace grass with dirt under the tree
|
||||||
if rng_map_a[x][z] < 0.03 {
|
if let Some(y) = local_y_position(height, chunk_position) {
|
||||||
if let Some(y) = local_y_position(height + 1, chunk_position) {
|
blocks[x][y][z] = Block::Dirt;
|
||||||
blocks[x][y][z] = Block::TallGrass;
|
}
|
||||||
}
|
|
||||||
}
|
//Place wood (no smart_place needed here!)
|
||||||
//place trees!
|
let tree_height = 4 + (rng_map_b[x][z] * 3.).round() as i32;
|
||||||
if rng_map_a[x][z] < 0.001 {
|
for tree_y in 0..tree_height {
|
||||||
//Replace grass with dirt under the tree
|
if let Some(y) = local_y_position(height + 1 + tree_y, chunk_position) {
|
||||||
if let Some(y) = local_y_position(height, chunk_position) {
|
blocks[x][y][z] = Block::Wood;
|
||||||
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;
|
||||||
let tree_height = 4 + (rng_map_b[x][z] * 3.).round() as i32;
|
|
||||||
for tree_y in 0..tree_height {
|
//Place leaf blocks
|
||||||
if let Some(y) = local_y_position(height + 1 + tree_y, chunk_position) {
|
if let Some(y) = local_y_position(height + 1, chunk_position) {
|
||||||
blocks[x][y][z] = Block::Wood;
|
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);
|
||||||
let tree_height = 4 + (rng_map_b[x][z] * 3.).round() as i32;
|
// }
|
||||||
|
// Part that wraps around the tree
|
||||||
//Place leaf blocks
|
{
|
||||||
if let Some(y) = local_y_position(height + 1, chunk_position) {
|
let tree_leaf_height = tree_height - 3;
|
||||||
let tree_pos = ivec3(x as i32, y as i32, z as i32);
|
let leaf_width = 2;
|
||||||
// Place wood (smart_place)
|
for tree_y in tree_leaf_height..tree_height {
|
||||||
// for tree_y in 0..tree_height {
|
for tree_x in (-leaf_width)..=leaf_width {
|
||||||
// smart_place(&mut blocks, tree_pos + tree_y * IVec3::Y, Block::Wood);
|
for tree_z in (-leaf_width)..=leaf_width {
|
||||||
// }
|
let tree_offset = ivec3(tree_x, tree_y, tree_z);
|
||||||
// Part that wraps around the tree
|
if tree_offset.xz() == IVec2::ZERO { continue }
|
||||||
{
|
smart_place(&mut blocks, tree_pos + tree_offset, Block::Leaf);
|
||||||
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 {
|
//part above the tree
|
||||||
let tree_offset = ivec3(tree_x, tree_y, tree_z);
|
{
|
||||||
if tree_offset.xz() == IVec2::ZERO { continue }
|
let leaf_above_height = 2;
|
||||||
smart_place(&mut blocks, tree_pos + tree_offset, Block::Leaf);
|
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);
|
||||||
//part above the tree
|
smart_place(&mut blocks, tree_pos + tree_offset, Block::Leaf);
|
||||||
{
|
}
|
||||||
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);
|
||||||
(blocks, queue)
|
// dirt_noise.set_frequency(0.1);
|
||||||
|
|
||||||
// let mut cave_noise = FastNoise::seeded(seed);
|
//
|
||||||
// cave_noise.set_fractal_type(FractalType::FBM);
|
|
||||||
// cave_noise.set_frequency(0.1);
|
// if chunk_position.y >= 0 {
|
||||||
|
// if chunk_position.y == 0 {
|
||||||
// let mut dirt_noise = FastNoise::seeded(seed.rotate_left(1));
|
// for x in 0..CHUNK_SIZE {
|
||||||
// dirt_noise.set_fractal_type(FractalType::FBM);
|
// for z in 0..CHUNK_SIZE {
|
||||||
// dirt_noise.set_frequency(0.1);
|
// blocks[x][0][z] = Block::Dirt;
|
||||||
|
// blocks[x][1][z] = Block::Grass;
|
||||||
//
|
// }
|
||||||
|
// }
|
||||||
// if chunk_position.y >= 0 {
|
// }
|
||||||
// if chunk_position.y == 0 {
|
// } else {
|
||||||
// for x in 0..CHUNK_SIZE {
|
// for x in 0..CHUNK_SIZE {
|
||||||
// for z in 0..CHUNK_SIZE {
|
// for y in 0..CHUNK_SIZE {
|
||||||
// blocks[x][0][z] = Block::Dirt;
|
// for z in 0..CHUNK_SIZE {
|
||||||
// blocks[x][1][z] = Block::Grass;
|
// 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 {
|
||||||
// } else {
|
// blocks[x][y][z] = Block::Stone;
|
||||||
// for x in 0..CHUNK_SIZE {
|
// } else if v_dirt_noise > 0.5 {
|
||||||
// for y in 0..CHUNK_SIZE {
|
// blocks[x][y][z] = Block::Dirt;
|
||||||
// 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;
|
// blocks
|
||||||
// } else if v_dirt_noise > 0.5 {
|
|
||||||
// blocks[x][y][z] = Block::Dirt;
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// blocks
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -25,14 +25,12 @@ impl Default for GlobalClPhysicsConfig {
|
||||||
//XXX: maybe a capsule? (or configurable hull?)
|
//XXX: maybe a capsule? (or configurable hull?)
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct ClPhysicsActor {
|
pub struct ClPhysicsActor {
|
||||||
pub disable: bool,
|
|
||||||
pub offset: Vec3,
|
pub offset: Vec3,
|
||||||
pub forces: Vec3,
|
pub forces: Vec3,
|
||||||
pub velocity: Vec3,
|
pub velocity: Vec3,
|
||||||
pub terminal_velocity: f32,
|
pub terminal_velocity: f32,
|
||||||
//TODO: this should be configurable per block
|
//TODO: this should be configurable per block
|
||||||
pub ground_friction: f32,
|
pub friction_agains_ground: f32,
|
||||||
pub gravity_scale: f32,
|
|
||||||
flag_ground: bool,
|
flag_ground: bool,
|
||||||
flag_collision: bool,
|
flag_collision: bool,
|
||||||
}
|
}
|
||||||
|
@ -51,13 +49,11 @@ impl Default for ClPhysicsActor {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
//HACK: for player
|
//HACK: for player
|
||||||
disable: false,
|
|
||||||
offset: vec3(0., 1.5, 0.),
|
offset: vec3(0., 1.5, 0.),
|
||||||
forces: Vec3::ZERO,
|
forces: Vec3::ZERO,
|
||||||
velocity: Vec3::ZERO,
|
velocity: Vec3::ZERO,
|
||||||
terminal_velocity: 40.,
|
terminal_velocity: 40.,
|
||||||
ground_friction: 10.,
|
friction_agains_ground: 0.5,
|
||||||
gravity_scale: 1.,
|
|
||||||
flag_ground: false,
|
flag_ground: false,
|
||||||
flag_collision: false,
|
flag_collision: false,
|
||||||
}
|
}
|
||||||
|
@ -97,11 +93,6 @@ pub fn update_client_physics_late(
|
||||||
dt: UniqueView<DeltaTime>,
|
dt: UniqueView<DeltaTime>,
|
||||||
) {
|
) {
|
||||||
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
|
for (mut actor, mut transform) in (&mut actors, &mut transforms).iter() {
|
||||||
if actor.disable {
|
|
||||||
actor.forces = Vec3::ZERO;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//apply forces
|
//apply forces
|
||||||
let actor_forces = actor.forces;
|
let actor_forces = actor.forces;
|
||||||
actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32();
|
actor.velocity += (actor_forces + conf.gravity) * dt.0.as_secs_f32();
|
||||||
|
@ -114,8 +105,7 @@ pub fn update_client_physics_late(
|
||||||
//get grid-aligned pos and blocks
|
//get grid-aligned pos and blocks
|
||||||
let actor_block_pos = actor_position.floor().as_ivec3();
|
let actor_block_pos = actor_position.floor().as_ivec3();
|
||||||
let actor_block = world.get_block(actor_block_pos);
|
let actor_block = world.get_block(actor_block_pos);
|
||||||
let actor_block_pos_slightly_below = (actor_position + Vec3::NEG_Y * 0.01).floor().as_ivec3();
|
let actor_block_below = world.get_block(actor_block_pos + IVec3::NEG_Y);
|
||||||
let actor_block_below = world.get_block(actor_block_pos_slightly_below);
|
|
||||||
|
|
||||||
//update flags
|
//update flags
|
||||||
actor.flag_collision = actor_block.is_solid();
|
actor.flag_collision = actor_block.is_solid();
|
||||||
|
@ -140,21 +130,14 @@ pub fn update_client_physics_late(
|
||||||
//HACK: for now, just stop the vertical velocity if on ground altogether,
|
//HACK: for now, just stop the vertical velocity if on ground altogether,
|
||||||
//as we don't have proper collision velocity resolution yet (we need to compute dot product or sth)
|
//as we don't have proper collision velocity resolution yet (we need to compute dot product or sth)
|
||||||
if actor.flag_ground {
|
if actor.flag_ground {
|
||||||
actor.velocity.y = actor.velocity.y.max(0.);
|
actor.velocity.y = 0.;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Apply velocity
|
//Apply velocity
|
||||||
actor_position += actor.velocity * dt.0.as_secs_f32();
|
actor_position += actor.velocity * dt.0.as_secs_f32();
|
||||||
actor_position += actor.offset;
|
actor_position += actor.offset;
|
||||||
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation.normalize(), actor_position);
|
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, actor_position);
|
||||||
|
|
||||||
//Apply friction
|
|
||||||
// if actor.flag_ground {
|
|
||||||
// let actor_velocity = actor.velocity;
|
|
||||||
// let actor_friction = actor.ground_friction;
|
|
||||||
// actor.velocity -= (actor_velocity * actor_friction * dt.0.as_secs_f32()) * vec3(1., 0., 1.);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
||||||
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
||||||
|
|
|
@ -187,7 +187,7 @@ fn update_input_state (
|
||||||
inputs.look += raw_inputs.mouse_delta.as_vec2();
|
inputs.look += raw_inputs.mouse_delta.as_vec2();
|
||||||
inputs.action_a |= raw_inputs.button_state[0];
|
inputs.action_a |= raw_inputs.button_state[0];
|
||||||
inputs.action_b |= raw_inputs.button_state[1];
|
inputs.action_b |= raw_inputs.button_state[1];
|
||||||
inputs.jump |= raw_inputs.keyboard_state.contains(KeyCode::Space as u32);
|
inputs.jump |= raw_inputs.button_state[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_input_state_gamepad (
|
fn update_input_state_gamepad (
|
||||||
|
|
|
@ -57,7 +57,7 @@ use events::{
|
||||||
player_actions::generate_move_events,
|
player_actions::generate_move_events,
|
||||||
};
|
};
|
||||||
use input::{init_input, process_inputs};
|
use input::{init_input, process_inputs};
|
||||||
use player_controller::{debug_switch_ctl_type, update_player_controllers};
|
use player_controller::update_player_controllers;
|
||||||
use rendering::{
|
use rendering::{
|
||||||
Renderer,
|
Renderer,
|
||||||
RenderTarget,
|
RenderTarget,
|
||||||
|
@ -133,7 +133,6 @@ fn update() -> Workload {
|
||||||
update_loaded_world_around_player,
|
update_loaded_world_around_player,
|
||||||
).into_sequential_workload().run_if(is_ingame_or_loading),
|
).into_sequential_workload().run_if(is_ingame_or_loading),
|
||||||
(
|
(
|
||||||
debug_switch_ctl_type,
|
|
||||||
update_player_controllers,
|
update_player_controllers,
|
||||||
update_client_physics_late,
|
update_client_physics_late,
|
||||||
generate_move_events,
|
generate_move_events,
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use glam::{vec3, EulerRot, Mat4, Quat, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles};
|
use glam::{Vec3, Mat4, Quat, EulerRot, Vec2};
|
||||||
use shipyard::{track, Component, Get, IntoIter, IntoWithId, IntoWorkload, Unique, UniqueView, View, ViewMut, Workload};
|
use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload, track};
|
||||||
use winit::keyboard::KeyCode;
|
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
use crate::{client_physics::ClPhysicsActor, delta_time::DeltaTime, input::{Inputs, PrevInputs, RawKbmInputState}, settings::GameSettings, transform::Transform};
|
use crate::{transform::Transform, input::Inputs, settings::GameSettings, delta_time::DeltaTime};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum PlayerControllerType {
|
pub enum PlayerControllerType {
|
||||||
|
@ -19,7 +18,7 @@ pub struct PlayerController {
|
||||||
impl PlayerController {
|
impl PlayerController {
|
||||||
pub const DEFAULT_FLY_CAM: Self = Self {
|
pub const DEFAULT_FLY_CAM: Self = Self {
|
||||||
control_type: PlayerControllerType::FlyCam,
|
control_type: PlayerControllerType::FlyCam,
|
||||||
speed: 50.,
|
speed: 30.,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const DEFAULT_FPS_CTL: Self = Self {
|
pub const DEFAULT_FPS_CTL: Self = Self {
|
||||||
|
@ -60,61 +59,16 @@ fn update_look(
|
||||||
fn update_movement(
|
fn update_movement(
|
||||||
controllers: View<PlayerController>,
|
controllers: View<PlayerController>,
|
||||||
mut transforms: ViewMut<Transform, track::All>,
|
mut transforms: ViewMut<Transform, track::All>,
|
||||||
mut actors: ViewMut<ClPhysicsActor>,
|
|
||||||
inputs: UniqueView<Inputs>,
|
inputs: UniqueView<Inputs>,
|
||||||
prev_inputs: UniqueView<PrevInputs>,
|
|
||||||
dt: UniqueView<DeltaTime>,
|
dt: UniqueView<DeltaTime>,
|
||||||
) {
|
) {
|
||||||
let jump = inputs.jump && !prev_inputs.0.jump;
|
if inputs.movement == Vec2::ZERO { return }
|
||||||
if (inputs.movement == Vec2::ZERO) && !jump { return }
|
let movement = inputs.movement * dt.0.as_secs_f32();
|
||||||
let movement = inputs.movement.extend(jump as u32 as f32).xzy();
|
for (ctl, mut transform) in (&controllers, &mut transforms).iter() {
|
||||||
for (id, (ctl, mut transform)) in (&controllers, &mut transforms).iter().with_id() {
|
|
||||||
let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
||||||
let rotation_norm = rotation.normalize();
|
let rotation_norm = rotation.normalize();
|
||||||
match ctl.control_type {
|
translation += (rotation_norm * Vec3::NEG_Z).normalize() * movement.y * ctl.speed;
|
||||||
PlayerControllerType::FlyCam => {
|
translation += (rotation_norm * Vec3::X).normalize() * movement.x * ctl.speed;
|
||||||
translation += (rotation_norm * Vec3::NEG_Z).normalize() * movement.z * ctl.speed * dt.0.as_secs_f32();
|
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
|
||||||
translation += (rotation_norm * Vec3::X).normalize() * movement.x * ctl.speed * dt.0.as_secs_f32();
|
|
||||||
translation += Vec3::Y * movement.y * ctl.speed * dt.0.as_secs_f32();
|
|
||||||
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
|
|
||||||
},
|
|
||||||
PlayerControllerType::FpsCtl => {
|
|
||||||
let mut actor = (&mut actors).get(id).unwrap();
|
|
||||||
let actor_on_ground = actor.on_ground();
|
|
||||||
|
|
||||||
let euler = rotation_norm.to_euler(EulerRot::YZX);
|
|
||||||
let right = Vec2::from_angle(-euler.0).extend(0.).xzy();
|
|
||||||
let forward = Vec2::from_angle(-(euler.0 + PI/2.)).extend(0.).xzy();
|
|
||||||
|
|
||||||
actor.apply_force(ctl.speed * (
|
|
||||||
(forward * movement.z) +
|
|
||||||
(right * movement.x) +
|
|
||||||
//TODO: remove hardcoded jump force
|
|
||||||
(Vec3::Y * movement.y * 125. * (actor_on_ground as u8 as f32))
|
|
||||||
));
|
|
||||||
|
|
||||||
// translation += forward * movement.z * ctl.speed * dt.0.as_secs_f32();
|
|
||||||
// translation += right * movement.x * ctl.speed * dt.0.as_secs_f32();
|
|
||||||
// translation += Vec3::Y * movement.y * ctl.speed * dt.0.as_secs_f32();
|
|
||||||
|
|
||||||
// transform.0 = Mat4::from_scale_rotation_translation(scale, rotation_norm, translation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn debug_switch_ctl_type(
|
|
||||||
mut controllers: ViewMut<PlayerController>,
|
|
||||||
mut actors: ViewMut<ClPhysicsActor>,
|
|
||||||
kbm_state: UniqueView<RawKbmInputState>,
|
|
||||||
) {
|
|
||||||
for (mut controller, mut actor) in (&mut controllers, &mut actors).iter() {
|
|
||||||
if kbm_state.keyboard_state.contains(KeyCode::F4 as u32) {
|
|
||||||
*controller = PlayerController::DEFAULT_FPS_CTL;
|
|
||||||
actor.disable = false;
|
|
||||||
} else if kbm_state.keyboard_state.contains(KeyCode::F5 as u32) {
|
|
||||||
*controller = PlayerController::DEFAULT_FLY_CAM;
|
|
||||||
actor.disable = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue