clean up generation

This commit is contained in:
zoe 2022-05-19 19:01:57 +02:00
parent c0206bdefc
commit 8a98462f69
7 changed files with 46 additions and 18 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@ use gdnative::prelude::*;
mod saves;
mod world;
mod gen;
#[derive(NativeClass)]
#[inherit(Node)]
@ -14,7 +15,7 @@ pub struct StateServer {
impl StateServer {
fn new(_owner: &Node) -> Self {
StateServer {
world: world::World::new(0, 0, 0, "seed".to_string()),
world: world::World::new(0, 0, 0, gen::get_rng("seed".to_string())),
}
}
@ -33,7 +34,7 @@ impl StateServer {
#[export]
fn generate_world(&mut self, _owner: &Node, xsize: usize, ysize: usize, zsize: usize, seed: String) {
self.world = world::World::new(xsize, ysize, zsize, seed);
self.world = world::World::new(xsize, ysize, zsize, gen::get_rng(seed));
let w = self.world.generate();
_owner.emit_signal("request_init", &[]);
_owner.emit_signal("changed_tiletypes", &[Variant::new(&w)]);

View File

@ -0,0 +1,10 @@
use rand::SeedableRng;
pub type GameRng = rand_xoshiro::Xoshiro256PlusPlus;
pub fn get_rng(seed: impl AsRef<[u8]> /* implemented by String */) -> GameRng {
// blake3::Hash is a [u8; 32] under the hood and implements From and Into to convert to and from it
let hash: [u8; 32] = blake3::hash(seed.as_ref()).into();
// Xoshiro256++ seeds are [u8; 32] :3
GameRng::from_seed(hash)
}

View File

@ -1,5 +1,8 @@
use gdnative::prelude::*;
use self::gen::GameRng;
mod gen;
mod tiles;
@ -8,7 +11,8 @@ pub struct World {
pub ysize: usize,
pub zsize: usize,
tiles: Vec<Vec<Vec<tiles::Tiletypes>>>,
seed: String,
rng: GameRng,
attributes: tiles::Attributelists,
}
impl World {
@ -30,8 +34,7 @@ impl World {
pub fn generate(&mut self) -> Vector3Array {
self.tiles = get_vec3(self.xsize, self.ysize, self.zsize);
let mut rng = gen::get_rng(self.seed.to_owned());
let noisemap = gen::get_noise(&mut rng, (self.xsize, self.ysize));
let noisemap = gen::get_noise(&mut self.rng, (self.xsize, self.ysize));
let mut ret: Vector3Array = Vector3Array::new();
for x in 0..self.xsize {
@ -89,13 +92,14 @@ impl World {
positions
}
pub fn new(xsize: usize, ysize: usize, zsize: usize, seed: String) -> World {
pub fn new(xsize: usize, ysize: usize, zsize: usize, rng: GameRng) -> World {
World {
xsize,
ysize,
zsize,
tiles: get_vec3(xsize, ysize, zsize),
seed,
rng,
attributes: tiles::Attributelists::new(),
}
}
}

View File

@ -2,16 +2,9 @@ use noise::{
utils::{NoiseMap, NoiseMapBuilder, PlaneMapBuilder},
Seedable, SuperSimplex,
};
use rand::{Rng, SeedableRng};
use rand::Rng;
pub use crate::stateserver::gen::GameRng;
pub type GameRng = rand_xoshiro::Xoshiro256PlusPlus;
pub fn get_rng(seed: impl AsRef<[u8]> /* implemented by String */) -> GameRng {
// blake3::Hash is a [u8; 32] under the hood and implements From and Into to convert to and from it
let hash: [u8; 32] = blake3::hash(seed.as_ref()).into();
// Xoshiro256++ seeds are [u8; 32] :3
GameRng::from_seed(hash)
}
pub fn get_noise(rng: &mut GameRng, size: (usize, usize)) -> NoiseMap {
// rng.gen::<u32>() generates a random u32 which is already between 0 and u32::MAX

View File

@ -12,6 +12,18 @@ pub enum Tiletypes {
Rock,
WaterSlab,
GrassSlab,
SandPathTlBr,
SandPathTrBl,
SandPathCross,
SandPathCurveDD,
SandPathCurveUU,
SandPathCurveLL,
SandPathCurveRR,
SandPathTTlBr,
SandPathTrBlBr,
SandPathTrTlBl,
SandPathTlTrBr,
}
#[allow(dead_code)]
@ -30,6 +42,8 @@ pub struct Tile {
pub must_be_on_top: bool,
#[builder(default = "true")]
pub can_be_bedrock: bool,
#[builder(default = "false")]
pub is_grass: bool,
}
impl Tile {
@ -58,6 +72,7 @@ impl Tile {
tile = TileBuilder::default()
.kind(kind)
.must_be_on_top(true)
.is_grass(true)
.build()
.unwrap()
}
@ -66,6 +81,7 @@ impl Tile {
.kind(kind)
.must_be_on_top(true)
.is_support(false)
.is_grass(true)
.build()
.unwrap()
}
@ -87,15 +103,19 @@ impl Tile {
pub struct Attributelists {
pub bedrock: Vec<Tiletypes>,
pub grass: Vec<Tiletypes>,
}
impl Attributelists {
pub fn new() -> Attributelists{
let mut bedrock: Vec<Tiletypes> = vec![];
let mut grass: Vec<Tiletypes> = vec![];
for tiletype in Tiletypes::iter(){
let tile = Tile::new(tiletype);
if tile.can_be_bedrock { bedrock.push(tiletype); }
if tile.can_be_bedrock { bedrock.push(tiletype);
if tile.is_grass {grass.push(tiletype);}
}
}
Attributelists { bedrock }
Attributelists { bedrock, grass }
}
}