This commit is contained in:
Joshua Barretto 2019-06-26 01:27:41 +01:00
parent b40d19ad0e
commit e414b82d2f
6 changed files with 48 additions and 53 deletions

View File

@ -1,7 +1,7 @@
use rand::thread_rng;
use std::ops::{Add, Mul, Sub};
use vek::*;
use veloren_world::sim::Settlement;
use rand::thread_rng;
const W: usize = 640;
const H: usize = 480;
@ -21,7 +21,12 @@ fn main() {
let seed = settlement.get_at(pos).map(|b| b.seed).unwrap_or(0);
buf[j * W + i] = u32::from_le_bytes([(seed >> 0) as u8, (seed >> 8) as u8, (seed >> 16) as u8, (seed >> 24) as u8]);
buf[j * W + i] = u32::from_le_bytes([
(seed >> 0) as u8,
(seed >> 8) as u8,
(seed >> 16) as u8,
(seed >> 24) as u8,
]);
}
}

View File

@ -1,15 +1,14 @@
use noise::{NoiseFn, Seedable, SuperSimplex};
use rand::thread_rng;
use std::ops::{Add, Mul, Sub};
use vek::*;
use veloren_world::sim::Settlement;
use rand::thread_rng;
use noise::{Seedable, NoiseFn, SuperSimplex};
const W: usize = 640;
const H: usize = 640;
fn main() {
let mut win =
minifb::Window::new("Turb", W, H, minifb::WindowOptions::default()).unwrap();
let mut win = minifb::Window::new("Turb", W, H, minifb::WindowOptions::default()).unwrap();
let nz_x = SuperSimplex::new().set_seed(0);
let nz_y = SuperSimplex::new().set_seed(1);
@ -24,13 +23,9 @@ fn main() {
let pos = pos * 10.0;
let pos = (0..10)
.fold(pos, |pos, _| pos.map(|e| e.powf(3.0) - 1.0));
let pos = (0..10).fold(pos, |pos, _| pos.map(|e| e.powf(3.0) - 1.0));
let val = if pos
.map(|e| e.abs() < 0.5)
.reduce_and()
{
let val = if pos.map(|e| e.abs() < 0.5).reduce_and() {
1.0f32
} else {
0.0

View File

@ -139,7 +139,8 @@ impl<'a> Sampler for ColumnGen<'a> {
let dist_to_path = match &sim_chunk.location {
Some(loc) => {
let this_loc = &sim.locations[loc.loc_idx];
this_loc.neighbours
this_loc
.neighbours
.iter()
.map(|j| {
let other_loc = &sim.locations[*j];
@ -159,7 +160,7 @@ impl<'a> Sampler for ColumnGen<'a> {
.filter(|x| x.is_finite())
.min_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap_or(f32::INFINITY)
},
}
None => f32::INFINITY,
};
@ -178,11 +179,14 @@ impl<'a> Sampler for ColumnGen<'a> {
let rpos = wposf.map2(loc.center, |a, b| a as f32 - b as f32) / 256.0 + 0.5;
if rpos.map(|e| e >= 0.0 && e < 1.0).reduce_and() {
(loc.settlement.get_at(rpos).map(|b| b.seed % 20 + 10).unwrap_or(0)) as f32
(loc.settlement
.get_at(rpos)
.map(|b| b.seed % 20 + 10)
.unwrap_or(0)) as f32
} else {
0.0
}
},
}
None => 0.0,
};

View File

@ -1,7 +1,7 @@
use super::Settlement;
use fxhash::FxHashSet;
use rand::Rng;
use vek::*;
use fxhash::FxHashSet;
use super::Settlement;
#[derive(Clone, Debug)]
pub struct Location {

View File

@ -160,14 +160,10 @@ impl WorldSim {
loc_clone.sort_by_key(|(_, l)| l.distance_squared(pos));
loc_clone
.iter()
.skip(1)
.take(2)
.for_each(|(j, _)| {
locations[i].neighbours.insert(*j);
locations[*j].neighbours.insert(i);
});
loc_clone.iter().skip(1).take(2).for_each(|(j, _)| {
locations[i].neighbours.insert(*j);
locations[*j].neighbours.insert(i);
});
}
// Simulate invasion!
@ -181,10 +177,8 @@ impl WorldSim {
let loc = Vec2::new(i as i32 + R_COORDS[idx], j as i32 + R_COORDS[idx + 1])
.map(|e| e as usize);
loc_grid[j * grid_size.x + i] = loc_grid
.get(loc.y * grid_size.x + loc.x)
.cloned()
.flatten();
loc_grid[j * grid_size.x + i] =
loc_grid.get(loc.y * grid_size.x + loc.x).cloned().flatten();
}
}
}
@ -195,7 +189,10 @@ impl WorldSim {
for i in 0..WORLD_SIZE.x {
for j in 0..WORLD_SIZE.y {
let chunk_pos = Vec2::new(i as i32, j as i32);
let block_pos = Vec2::new(chunk_pos.x * TerrainChunkSize::SIZE.x as i32, chunk_pos.y * TerrainChunkSize::SIZE.y as i32);
let block_pos = Vec2::new(
chunk_pos.x * TerrainChunkSize::SIZE.x as i32,
chunk_pos.y * TerrainChunkSize::SIZE.y as i32,
);
let cell_pos = Vec2::new(i / cell_size, j / cell_size);
// Find the distance to each region
@ -228,7 +225,10 @@ impl WorldSim {
.unwrap()
.location
.as_ref()
.map(|l| locations[l.loc_idx].center.distance_squared(block_pos) < town_size * town_size)
.map(|l| {
locations[l.loc_idx].center.distance_squared(block_pos)
< town_size * town_size
})
.unwrap_or(false);
if in_town {
self.get_mut(chunk_pos).unwrap().spawn_rate = 0.0;
@ -471,9 +471,7 @@ impl SimChunk {
pub fn get_name(&self, world: &WorldSim) -> Option<String> {
if let Some(loc) = &self.location {
Some(world.locations[loc.loc_idx]
.name()
.to_string())
Some(world.locations[loc.loc_idx].name().to_string())
} else {
None
}

View File

@ -27,25 +27,16 @@ pub struct Building {
enum Lot {
None,
One(Building),
Many {
split_x: bool,
lots: Vec<Lot>,
},
Many { split_x: bool, lots: Vec<Lot> },
}
impl Lot {
pub fn generate(deep: usize, depth: f32, aspect: f32, rng: &mut impl Rng) -> Self {
let depth = if deep < 3 {
8.0
} else {
depth
};
let depth = if deep < 3 { 8.0 } else { depth };
if (depth < 1.0 || deep > 6) && !(deep < 3 || deep % 2 == 1) {
if rng.gen::<f32>() < 0.5 {
Lot::One(Building {
seed: rng.gen(),
})
Lot::One(Building { seed: rng.gen() })
} else {
Lot::None
}
@ -76,11 +67,13 @@ impl Lot {
pub fn get_at(&self, pos: Vec2<f32>) -> Option<&Building> {
match self {
Lot::None => None,
Lot::One(building) => if pos.map(|e| e > 0.1 && e < 0.9).reduce_and() {
Some(building)
} else {
None
},
Lot::One(building) => {
if pos.map(|e| e > 0.1 && e < 0.9).reduce_and() {
Some(building)
} else {
None
}
}
Lot::Many { split_x, lots } => {
let split_dim = if *split_x { pos.x } else { pos.y };
let idx = (split_dim * lots.len() as f32).floor() as usize;
@ -89,7 +82,7 @@ impl Lot {
} else {
Vec2::new(pos.x, (pos.y * lots.len() as f32).fract())
})
},
}
}
}
}