seeding! hooray!

This commit is contained in:
zoe 2022-05-09 00:52:47 +02:00
parent 817f6898be
commit d742c9d4cd
8 changed files with 68 additions and 20 deletions

View file

@ -1,3 +1,3 @@
source_md5="d864960022980c4270042a1ed80b9ac4"
dest_md5="5f766eea69301dc969f05fac899cb4cb"
source_md5="30885b768ea8e61f6f1072099ba8c3af"
dest_md5="3858a5d8c4a4ecf0f3bd148fa4d0440a"

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 7 KiB

View file

@ -2,8 +2,8 @@ extends Node
export var xsize = 64
export var ysize = 64
export var zsize = 8
export var cityname = "cool city"
export var zsize = 6
export var cityname = "night city"
func _ready():
StateServer.generate_world(xsize, ysize, zsize, cityname)

Binary file not shown.

View file

@ -1,9 +1,8 @@
use gdnative::prelude::*;
use rand::{prelude::StdRng, SeedableRng};
use rand::prelude::StdRng;
use rand::prelude::*;
use rand_seeder::Seeder;
use strum::{EnumCount, IntoEnumIterator};
use strum::EnumCount;
mod tiles;
@ -13,6 +12,7 @@ pub struct World {
pub zsize: usize,
tiles: Vec<Vec<Vec<tiles::Tiletypes>>>,
seed: String,
tileattributes: tiles::Attributelists,
}
impl World {
@ -40,17 +40,35 @@ impl World {
for y in 0..self.ysize {
for z in 0..self.zsize {
ret.push(Vector3::new(x as f32, y as f32, z as f32));
self.tiles[x][y][z] = tiles::Tiletypes::from_repr(
rng.gen_range(0..tiles::Tiletypes::COUNT) as u8,
)
.unwrap();
self.tiles[x][y][z] = self.new_tile_init_step(x, y, z, &mut rng)
}
}
}
ret
}
fn tile_at(&self, x: usize, y: usize, z:usize) -> tiles::Tiletypes {
fn new_tile_init_step(&self, x: usize, y: usize, z: usize, rng: &mut StdRng) -> tiles::Tiletypes {
// first generate bedrock
if z == 0 {
if y == 0 || x == 0 || y == self.ysize - 1 || x == self.xsize - 1{
return tiles::Tiletypes::Water;
}
let random = rng.gen_range(0..3) as u8;
match random {
0 => return tiles::Tiletypes::Grass,
_ => return self.tileattributes.bedrock.choose(rng).unwrap().to_owned(),
}
}
// then make sure the top row of tiles is air
if z == self.zsize - 1
|| !tiles::Tile::new(self.tiles[x][y][z - 1]).is_support
|| tiles::Tile::new(self.tiles[x][y][z - 1]).must_be_on_top
{
return tiles::Tiletypes::Air;
}
// if no tile fits, then return air
tiles::Tiletypes::Air
}
@ -61,6 +79,7 @@ impl World {
zsize,
tiles: get_vec3(xsize, ysize, zsize),
seed,
tileattributes: tiles::Attributelists::new(),
}
}
}

View file

@ -1,5 +1,5 @@
use derive_builder::Builder;
use strum::{AsRefStr, EnumCount, EnumDiscriminants, EnumIter, FromRepr};
use strum::{AsRefStr, EnumCount, EnumDiscriminants, EnumIter, FromRepr, IntoEnumIterator};
#[derive(AsRefStr, EnumIter, FromRepr, EnumDiscriminants, Clone, Copy, PartialEq, EnumCount)]
#[repr(u8)]
@ -9,6 +9,7 @@ pub enum Tiletypes {
Grass,
Dirt,
Sand,
Rock,
}
#[allow(dead_code)]
@ -16,19 +17,17 @@ pub enum Tiletypes {
pub struct Tile {
kind: Tiletypes,
#[builder(default = "true")]
is_support: bool,
#[builder(default = "true")]
needs_support: bool,
#[builder(default = "vec![]")]
can_be_above: Vec<Tiletypes>,
#[builder(default = "vec![]")]
can_be_next_to: Vec<Tiletypes>,
pub is_support: bool,
#[builder(default = "true")]
pub hide_top_left: bool,
#[builder(default = "true")]
pub hide_top_right: bool,
#[builder(default = "true")]
pub hide_below: bool,
#[builder(default = "false")]
pub must_be_on_top: bool,
#[builder(default = "true")]
pub can_be_bedrock: bool,
}
impl Tile {
@ -42,6 +41,21 @@ impl Tile {
.hide_below(false)
.hide_top_right(false)
.hide_top_left(false)
.can_be_bedrock(false)
.build()
.unwrap()
}
Tiletypes::Water => {
tile = TileBuilder::default()
.kind(kind)
.is_support(false)
.build()
.unwrap()
}
Tiletypes::Grass => {
tile = TileBuilder::default()
.kind(kind)
.must_be_on_top(true)
.build()
.unwrap()
}
@ -53,3 +67,18 @@ impl Tile {
self.kind.as_ref().to_string()
}
}
pub struct Attributelists {
pub bedrock: Vec<Tiletypes>,
}
impl Attributelists {
pub fn new() -> Attributelists{
let mut bedrock: Vec<Tiletypes> = vec![];
for tiletype in Tiletypes::iter(){
let tile = Tile::new(tiletype);
if tile.can_be_bedrock { bedrock.push(tiletype); }
}
Attributelists { bedrock }
}
}