Skip to content

Commit

Permalink
#10 basic cave gen
Browse files Browse the repository at this point in the history
  • Loading branch information
justanothercell committed May 20, 2023
1 parent 2a11a4a commit 2fb26cc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
7 changes: 7 additions & 0 deletions engine/src/math/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl<T> Vector2<T> {
pub fn area(&self) -> T where T: Mul<Output=T> + Copy {
self.x * self.y
}

pub fn into_array(self) -> [T;2] {
[self.x, self.y]
}
}

impl Vector2<i32> {
Expand All @@ -55,6 +59,9 @@ impl Vector2<i32> {
pub fn to_f32(&self) -> Vector2<f32> {
Vector2::new(self.x as f32, self.y as f32)
}
pub fn to_f64(&self) -> Vector2<f64> {
Vector2::new(self.x as f64, self.y as f64)
}
}

impl Vector2<u32> {
Expand Down
6 changes: 5 additions & 1 deletion mods/world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ edition = "2021"
client = []
server = []

[profile.dev.package.noise]
opt-level = 3

[dependencies]
aeonetica_engine = { package="engine", path="../../engine" }
aeonetica_client = { package="client", path="../../client" }
aeonetica_server = { package="server", path="../../server" }

rand = "0.8.5"
rand = "0.8.5"
noise = "0.8.2"
47 changes: 43 additions & 4 deletions mods/world/src/server/gen.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
use noise::{Fbm, NoiseFn, OpenSimplex, RidgedMulti, Terrace};
use aeonetica_engine::log;
use aeonetica_engine::math::vector::Vector2;
use crate::common::{CHUNK_SIZE, Population};
use crate::server::world::World;
use crate::tiles::Tile;

pub(crate) struct GenProvider {
pub(crate) seed: u64,
pub(crate) cave_noise: Box<dyn NoiseFn<f64, 2>>
}

impl GenProvider {
pub(crate) fn new(seed: u64) -> Self {
Self {
seed,
cave_noise: {
let fbm = Fbm::<RidgedMulti<OpenSimplex>>::new(seed as u32);
Box::new(Terrace::new(fbm)
.add_control_point(0.0)
.add_control_point(0.1))
}
}
}
}

impl World {
pub(crate) fn populate(&mut self, chunk_pos: Vector2<i32>) {
log!(WARN, "populated {chunk_pos:?}");
let gen = self.generator.clone();
let chunk = self.mut_chunk_at_raw(chunk_pos);
for i in 0..CHUNK_SIZE as i32 {
chunk.set_tile((i, 0).into(), Tile::StoneBrick);
chunk.set_tile((0, i).into(), Tile::StoneBrick);
for x in 0..CHUNK_SIZE as i32 {
for y in 0..CHUNK_SIZE as i32 {
let p = Vector2::new(x, y).to_f64() / 16.0 + chunk_pos.to_f64();
let current = gen.cave_noise.get(p.into_array()) > 0.0;
let around =
(gen.cave_noise.get((p + Vector2::new(1.0/16.0, 0.0/16.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(-1.0/16.0, 0.0/16.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(0.0/16.0, 1.0/16.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(0.0/16.0, -1.0/16.0)).into_array()) > 0.0) as i32;
// a bit of a random approach - found accidentally
let accent =
(gen.cave_noise.get((p + Vector2::new(1.0, 0.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(-1.0, 0.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(0.0, 1.0)).into_array()) > 0.0) as i32 +
(gen.cave_noise.get((p + Vector2::new(0.0, -1.0)).into_array()) > 0.0) as i32;
if (current && around > 1) || around > 2 {
chunk.set_tile((x, y).into(), if accent > 1 {
Tile::Stone
} else {
Tile::StoneBrick
})
}
}
}
chunk.set_tile((chunk.chunk_pos.x.abs(), chunk.chunk_pos.y.abs()).into(), Tile::Stone);
chunk.population = Population::Finished;
}
}
7 changes: 5 additions & 2 deletions mods/world/src/server/world.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::{RefCell, RefMut};
use std::rc::Rc;
use aeonetica_engine::{ClientId, EntityId, log};
use aeonetica_engine::networking::SendMode;
use aeonetica_engine::math::vector::Vector2;
Expand All @@ -8,6 +10,7 @@ use aeonetica_server::ecs::messaging::Messenger;
use aeonetica_server::ecs::module::Module;
use crate::client::WorldHandle;
use crate::common::{Chunk, CHUNK_SIZE, Population};
use crate::server::gen::GenProvider;
use crate::tiles::Tile;

pub const WORLD: &str = "WORLD";
Expand All @@ -29,7 +32,7 @@ impl ChunkHolder {
}

pub struct World {
seed: u64,
pub(crate) generator: Rc<GenProvider>,
origin_ne: ChunkHolder,
origin_se: ChunkHolder,
origin_nw: ChunkHolder,
Expand All @@ -55,7 +58,7 @@ impl World {

}));
entity.add_module(World {
seed,
generator: Rc::new(GenProvider::new(seed)),
origin_ne: ChunkHolder::new((0, 0).into()),
origin_se: ChunkHolder::new((0, -1).into()),
origin_nw: ChunkHolder::new((-1, 0).into()),
Expand Down

0 comments on commit 2fb26cc

Please sign in to comment.