Cargo fmt most things (except erosion.rs).

This commit is contained in:
Joshua Yanovski 2020-01-11 21:38:30 +01:00
parent 982b538a37
commit 4f863470e1
16 changed files with 768 additions and 695 deletions

View File

@ -113,11 +113,8 @@ impl Client {
log::debug!("Preparing image...");
let world_map = Arc::new(image::DynamicImage::ImageRgba8({
// Should not fail if the dimensions are correct.
let world_map = image::ImageBuffer::from_raw(
map_size.x,
map_size.y,
world_map_raw,
);
let world_map =
image::ImageBuffer::from_raw(map_size.x, map_size.y, world_map_raw);
world_map.ok_or(Error::Other("Server sent a bad world map image".into()))?
}));
log::debug!("Done preparing image...");

View File

@ -37,7 +37,7 @@ pub enum ServerMsg {
entity_package: sync::EntityPackage<EcsCompPacket>,
server_info: ServerInfo,
time_of_day: state::TimeOfDay,
world_map: (Vec2<u32>, Vec<u32>)
world_map: (Vec2<u32>, Vec<u32>),
},
PlayerListUpdate(PlayerListUpdate),
StateAnswer(Result<ClientState, (RequestStateError, ClientState)>),

View File

@ -46,7 +46,10 @@ use std::{
};
use uvth::{ThreadPool, ThreadPoolBuilder};
use vek::*;
use world::{sim::{FileOpts, WORLD_SIZE, WorldOpts}, World};
use world::{
sim::{FileOpts, WorldOpts, WORLD_SIZE},
World,
};
const CLIENT_TIMEOUT: f64 = 20.0; // Seconds
pub enum Event {
@ -104,7 +107,9 @@ impl Server {
state.ecs_mut().register::<RegionSubscription>();
state.ecs_mut().register::<Client>();
let world = World::generate(settings.world_seed, WorldOpts {
let world = World::generate(
settings.world_seed,
WorldOpts {
seed_elements: true,
world_file: if let Some(ref file) = settings.map_file {
FileOpts::Load(file.clone())
@ -112,7 +117,8 @@ impl Server {
FileOpts::Generate
},
..WorldOpts::default()
});
},
);
let spawn_point = {
// NOTE: all of these `.map(|e| e as [type])` calls should compile into no-ops,

View File

@ -104,8 +104,7 @@ impl ServerSettings {
max_players: 100,
start_time: 9.0 * 3600.0,
admins: vec!["singleplayer".to_string()], // TODO: Let the player choose if they want to use admin commands or not
..
Self::load() // Fill in remaining fields from settings.ron.
..Self::load() // Fill in remaining fields from settings.ron.
}
}

View File

@ -162,7 +162,7 @@ impl<'a> Widget for Map<'a> {
let (world_map, worldsize) = self.world_map;
let worldsize = worldsize.map2(TerrainChunkSize::RECT_SIZE, |e, f| e as f64 * f as f64);
Image::new(world_map/*self.imgs.map_placeholder*/)
Image::new(world_map /*self.imgs.map_placeholder*/)
.middle_of(state.ids.map_bg)
.color(Some(Color::Rgba(1.0, 1.0, 1.0, fade - 0.1)))
.w_h(700.0, 700.0)

View File

@ -137,7 +137,7 @@ impl<'a> Widget for MiniMap<'a> {
// Map Image
let (world_map, worldsize) = self.world_map;
let worldsize = worldsize.map2(TerrainChunkSize::RECT_SIZE, |e, f| e as f64 * f as f64);
Image::new(world_map/*self.imgs.map_placeholder*/)
Image::new(world_map /*self.imgs.map_placeholder*/)
.middle_of(state.ids.mmap_frame_bg)
.w_h(92.0 * 4.0 * zoom, 82.0 * 4.0 * zoom)
.parent(state.ids.mmap_frame_bg)

View File

@ -454,7 +454,10 @@ impl Hud {
// Generate ids.
let ids = Ids::new(ui.id_generator());
// Load world map
let world_map = (ui.add_graphic(Graphic::Image(client.world_map.0.clone())), client.world_map.1);
let world_map = (
ui.add_graphic(Graphic::Image(client.world_map.0.clone())),
client.world_map.1,
);
// Load images.
let imgs = Imgs::load(&mut ui).expect("Failed to load images!");
// Load rotation images.

View File

@ -20,7 +20,7 @@ use client::Client;
use common::{
comp,
terrain::{BlockKind, TerrainChunk, TerrainChunkSize},
vol::{RectVolSize, ReadVol},
vol::{ReadVol, RectVolSize},
};
use specs::{Join, WorldExt};
use vek::*;

View File

@ -175,7 +175,9 @@ fn mesh_worker<V: BaseVol<Vox = Block> + RectRasterableVol + ReadVol + Debug>(
let kind = volume.get(wpos).unwrap_or(&Block::empty()).kind();
if let Some(cfg) = sprite_config_for(kind) {
let seed = wpos.x as u64 * 3 + wpos.y as u64 * 7 + wpos.x as u64 * wpos.y as u64; // Awful PRNG
let seed = wpos.x as u64 * 3
+ wpos.y as u64 * 7
+ wpos.x as u64 * wpos.y as u64; // Awful PRNG
let instance = SpriteInstance::new(
Mat4::identity()

View File

@ -6,10 +6,13 @@ const W: usize = 640;
const H: usize = 480;
fn main() {
let world = World::generate(0, WorldOpts {
let world = World::generate(
0,
WorldOpts {
seed_elements: true,
..WorldOpts::default()
});
},
);
let sampler = world.sample_columns();

View File

@ -36,12 +36,15 @@ fn main() {
let mut _map_file = PathBuf::from("./maps");
_map_file.push(map_file);
let world = World::generate(1337, WorldOpts {
let world = World::generate(
1337,
WorldOpts {
seed_elements: false,
// world_file: sim::FileOpts::Load(_map_file),
world_file: sim::FileOpts::Save,
..WorldOpts::default()
});
},
);
let sampler = world.sim();
@ -87,12 +90,14 @@ fn main() {
for i in 0..W {
for j in 0..H {
let pos = (focus_rect + Vec2::new(i as f64, j as f64) * scale).map(|e: f64| e as i32);
let pos =
(focus_rect + Vec2::new(i as f64, j as f64) * scale).map(|e: f64| e as i32);
/* let top_left = pos;
let top_right = focus + Vec2::new(i as i32 + light_res, j as i32) * scale;
let bottom_left = focus + Vec2::new(i as i32, j as i32 + light_res) * scale; */
let (alt, basement, water_alt, humidity, temperature, downhill, river_kind) = sampler
let (alt, basement, water_alt, humidity, temperature, downhill, river_kind) =
sampler
.get(pos)
.map(|sample| {
(
@ -105,7 +110,15 @@ fn main() {
sample.river.river_kind,
)
})
.unwrap_or((CONFIG.sea_level, CONFIG.sea_level, CONFIG.sea_level, 0.0, 0.0, None, None));
.unwrap_or((
CONFIG.sea_level,
CONFIG.sea_level,
CONFIG.sea_level,
0.0,
0.0,
None,
None,
));
let humidity = humidity.min(1.0).max(0.0);
let temperature = temperature.min(1.0).max(-1.0) * 0.5 + 0.5;
let pos = pos * TerrainChunkSize::RECT_SIZE.map(|e| e as i32);
@ -177,15 +190,9 @@ fn main() {
let true_water_alt = (alt.max(water_alt) as f64 - focus.z) / gain as f64;
let true_alt = (alt as f64 - focus.z) / gain as f64;
let water_depth = (true_water_alt - true_alt)
.min(1.0)
.max(0.0);
let water_alt = true_water_alt
.min(1.0)
.max(0.0);
let alt = true_alt
.min(1.0)
.max(0.0);
let water_depth = (true_water_alt - true_alt).min(1.0).max(0.0);
let water_alt = true_water_alt.min(1.0).max(0.0);
let alt = true_alt.min(1.0).max(0.0);
let quad =
|x: f32| ((x as f64 * QUADRANTS as f64).floor() as usize).min(QUADRANTS - 1);
if river_kind.is_none() || humidity != 0.0 {
@ -205,17 +212,29 @@ fn main() {
}
buf[j * W + i] = match (river_kind, (is_water, true_alt >= true_sea_level)) {
(_, (false, _)) | ( None, (_, true)) => {
(_, (false, _)) | (None, (_, true)) => {
let (r, g, b) = (
(if is_shaded { alt } else { alt } * if is_temperature { temperature as f64 } else if is_shaded { alt } else { 0.0 }).sqrt(),
if is_shaded { 0.2 + (alt * 0.8) } else { alt },
(if is_shaded { alt } else { alt } * if is_humidity { humidity as f64 } else if is_shaded { alt } else { 0.0 }).sqrt(),
);
let light = if is_shaded {
light
(if is_shaded { alt } else { alt }
* if is_temperature {
temperature as f64
} else if is_shaded {
alt
} else {
1.0
};
0.0
})
.sqrt(),
if is_shaded { 0.2 + (alt * 0.8) } else { alt },
(if is_shaded { alt } else { alt }
* if is_humidity {
humidity as f64
} else if is_shaded {
alt
} else {
0.0
})
.sqrt(),
);
let light = if is_shaded { light } else { 1.0 };
u32::from_le_bytes([
(b * light * 255.0) as u8,
(g * light * 255.0) as u8,
@ -228,7 +247,7 @@ fn main() {
(/*alt*//*alt * *//*(1.0 - humidity)*/(alt * temperature).sqrt() * 255.0) as u8,
255,
]) */
},
}
(Some(RiverKind::Ocean), _) => u32::from_le_bytes([
((64.0 - water_depth * 64.0) * 1.0) as u8,
((32.0 - water_depth * 32.0) * 1.0) as u8,
@ -242,8 +261,8 @@ fn main() {
255,
]),
(None, _) | (Some(RiverKind::Lake { .. }), _) => u32::from_le_bytes([
(((64.0 + water_alt * 191.0) + (- water_depth * 64.0)) * 1.0) as u8,
(((32.0 + water_alt * 95.0) + (- water_depth * 32.0)) * 1.0) as u8,
(((64.0 + water_alt * 191.0) + (-water_depth * 64.0)) * 1.0) as u8,
(((32.0 + water_alt * 95.0) + (-water_depth * 32.0)) * 1.0) as u8,
0,
255,
]),
@ -280,7 +299,8 @@ fn main() {
}
if win.get_mouse_down(minifb::MouseButton::Left) {
if let Some((mx, my)) = win.get_mouse_pos(minifb::MouseMode::Clamp) {
let pos = (focus_rect + (Vec2::new(mx as f64, my as f64) * scale)).map(|e| e as i32);
let pos =
(focus_rect + (Vec2::new(mx as f64, my as f64) * scale)).map(|e| e as i32);
println!(
"Chunk position: {:?}",
pos.map2(TerrainChunkSize::RECT_SIZE, |e, f| e * f as i32)

View File

@ -265,7 +265,8 @@ impl<'a> BlockGen<'a> {
sub_surface_color,
stone_col.map(|e| e as f32 / 255.0),
(height - grass_depth - wposf.z as f32) * 0.15,
).map(|e| (e * 255.0) as u8);
)
.map(|e| (e * 255.0) as u8);
// Underground
if (wposf.z as f32) > alt - 32.0 * chaos {

View File

@ -443,7 +443,8 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
sim.get_interpolated_monotone(wpos, |chunk| chunk.alt)?
// sim.get_interpolated(wpos, |chunk| chunk.alt)?
};
let basement = alt + sim./*get_interpolated*/get_interpolated_monotone(wpos, |chunk| chunk.basement.sub(chunk.alt))?;
let basement = alt
+ sim./*get_interpolated*/get_interpolated_monotone(wpos, |chunk| chunk.basement.sub(chunk.alt))?;
// Find the average distance to each neighboring body of water.
let mut river_count = 0.0f64;
@ -830,7 +831,6 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
.mul(1.0 - humidity) */
/* .mul(32.0) */;
let riverless_alt_delta = Lerp::lerp(0.0, riverless_alt_delta, warp_factor);
let alt = alt_ + riverless_alt_delta;
let basement = basement.min(alt);
@ -895,7 +895,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
);
let tundra = Lerp::lerp(snow, Rgb::new(0.01, 0.3, 0.0), 0.4 + marble * 0.6);
let dead_tundra = Lerp::lerp(warm_stone, Rgb::new(0.3, 0.12, 0.2), marble);
let cliff = Rgb::lerp(cold_stone, /*warm_stone*/hot_stone, marble);
let cliff = Rgb::lerp(cold_stone, /*warm_stone*/ hot_stone, marble);
let grass = Rgb::lerp(
cold_grass,
@ -953,11 +953,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
.mul(1.0),
);
let sub_surface_color = Lerp::lerp(
cliff,
ground,
alt.sub(basement).mul(0.25)
);
let sub_surface_color = Lerp::lerp(cliff, ground, alt.sub(basement).mul(0.25));
/* let ground = Rgb::lerp(
dead_tundra,
@ -1087,15 +1083,18 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
); */
// Snow covering
let snow_cover =
temp.sub(CONFIG.snow_temp)
let snow_cover = temp
.sub(CONFIG.snow_temp)
.max(-humidity.sub(CONFIG.desert_hum))
.mul(16.0)
.add((marble_small - 0.5) * 0.5);
let (alt, ground, sub_surface_color) = if snow_cover /*< 0.1*/<= 0.5 && alt > water_level {
// Allow snow cover.
(alt + 1.0 - snow_cover.max(0.0), Rgb::lerp(snow, ground, snow_cover),
Lerp::lerp(sub_surface_color, ground, alt.sub(basement).mul(0.15)))
(
alt + 1.0 - snow_cover.max(0.0),
Rgb::lerp(snow, ground, snow_cover),
Lerp::lerp(sub_surface_color, ground, alt.sub(basement).mul(0.15)),
)
} else {
(alt, ground, sub_surface_color)
};
@ -1165,7 +1164,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
// Beach
((ocean_level - 1.0) / 2.0).max(0.0),
),
sub_surface_color,// /*warm_grass*/Lerp::lerp(cliff, dirt, alt.sub(basement).mul(0.25)),
sub_surface_color, // /*warm_grass*/Lerp::lerp(cliff, dirt, alt.sub(basement).mul(0.25)),
// No growing directly on bedrock.
tree_density: Lerp::lerp(0.0, tree_density, alt.sub(2.0).sub(basement).mul(0.5)),
forest_kind: sim_chunk.forest_kind,

View File

@ -1,5 +1,5 @@
use rayon::prelude::*;
use super::Alt;
use rayon::prelude::*;
/// From https://github.com/fastscape-lem/fastscapelib-fortran/blob/master/src/Diffusion.f90
///
@ -36,35 +36,43 @@ use super::Alt;
implicit none
*/
pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
h: &mut [Alt], b: &mut [Alt],
kd: impl Fn(usize) -> f64, kdsed: f64,
) {
pub fn diffusion(
nx: usize,
ny: usize,
xl: f64,
yl: f64,
dt: f64,
_ibc: (),
h: &mut [Alt],
b: &mut [Alt],
kd: impl Fn(usize) -> f64,
kdsed: f64,
) {
let aij = |i: usize, j: usize| j * nx + i;
/*
/*
double precision, dimension(:), allocatable :: f,diag,sup,inf,res
double precision, dimension(:,:), allocatable :: zint,kdint,zintp
integer i,j,ij
double precision factxp,factxm,factyp,factym,dx,dy
*/
let mut f : Vec<f64>;
let mut diag : Vec<f64>;
let mut sup : Vec<f64>;
let mut inf : Vec<f64>;
let mut res : Vec<f64>;
let mut zint : Vec<f64>;
let mut kdint : Vec<f64>;
let mut zintp : Vec<f64>;
let mut i : usize;
let mut j : usize;
let mut ij : usize;
let mut factxp : f64;
let mut factxm : f64;
let mut factyp : f64;
let mut factym : f64;
let mut dx : f64;
let mut dy : f64;
/*
*/
let mut f: Vec<f64>;
let mut diag: Vec<f64>;
let mut sup: Vec<f64>;
let mut inf: Vec<f64>;
let mut res: Vec<f64>;
let mut zint: Vec<f64>;
let mut kdint: Vec<f64>;
let mut zintp: Vec<f64>;
let mut i: usize;
let mut j: usize;
let mut ij: usize;
let mut factxp: f64;
let mut factxm: f64;
let mut factyp: f64;
let mut factym: f64;
let mut dx: f64;
let mut dy: f64;
/*
character cbc*4
!print*,'Diffusion'
@ -73,20 +81,20 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
dx=xl/(nx-1)
dy=yl/(ny-1)
*/
*/
// 2048*32/2048/2048
// 1 / 64 m
dx = xl / /*(nx - 1)*/nx as f64;
dy = yl / /*(ny - 1)*/ny as f64;
/*
/*
! creates 2D internal arrays to store topo and kd
allocate (zint(nx,ny),kdint(nx,ny),zintp(nx,ny))
*/
*/
zint = vec![Default::default(); nx * ny];
kdint = vec![Default::default(); nx * ny];
zintp = vec![Default::default(); nx * ny];
/*
/*
do j=1,ny
do i=1,nx
ij=(j-1)*nx+i
@ -97,7 +105,7 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
enddo
zintp = zint
*/
*/
for j in 0..ny {
for i in 0..nx {
// ij = vec2_as_uniform_idx(i, j);
@ -111,7 +119,7 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
}
zintp = zint.clone();
/*
/*
! first pass along the x-axis
allocate (f(nx),diag(nx),sup(nx),inf(nx),res(nx))
@ -121,14 +129,14 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf=0.d0
res=0.d0
do j=2,ny-1
*/
*/
f = vec![0.0; nx];
diag = vec![0.0; nx];
sup = vec![0.0; nx];
inf = vec![0.0; nx];
res = vec![0.0; nx];
for j in 1..ny-1 {
/*
for j in 1..ny - 1 {
/*
do i=2,nx-1
factxp=(kdint(i+1,j)+kdint(i,j))/2.d0*(dt/2.)/dx**2
factxm=(kdint(i-1,j)+kdint(i,j))/2.d0*(dt/2.)/dx**2
@ -139,22 +147,21 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf(i)=-factxm
f(i)=zintp(i,j)+factyp*zintp(i,j+1)-(factyp+factym)*zintp(i,j)+factym*zintp(i,j-1)
enddo
*/
for i in 1..nx-1 {
factxp = (kdint[aij(i+1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i-1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(i, j+1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(i, j-1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
*/
for i in 1..nx - 1 {
factxp = (kdint[aij(i + 1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i - 1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(i, j + 1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(i, j - 1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[i] = 1.0 + factxp + factxm;
sup[i] = -factxp;
inf[i] = -factxm;
f[i] =
zintp[aij(i, j)] + factyp * zintp[aij(i, j+1)] -
(factyp + factym) *
zintp[aij(i, j)] + factym * zintp[aij(i, j-1)];
f[i] = zintp[aij(i, j)] + factyp * zintp[aij(i, j + 1)]
- (factyp + factym) * zintp[aij(i, j)]
+ factym * zintp[aij(i, j - 1)];
}
/*
! left bc
/*
! left bc
if (cbc(4:4).eq.'1') then
diag(1)=1.
sup(1)=0.
@ -167,7 +174,7 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
sup(1)=-factxp
f(1)=zintp(1,j)+factyp*zintp(1,j+1)-(factyp+factym)*zintp(1,j)+factym*zintp(1,j-1)
endif
*/
*/
if true {
diag[0] = 1.0;
sup[0] = 0.0;
@ -175,17 +182,16 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
} else {
// reflective boundary
factxp = (kdint[aij(1, j)] + kdint[aij(0, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(0, j+1)] + kdint[aij(0, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(0, j-1)] + kdint[aij(0, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factyp = (kdint[aij(0, j + 1)] + kdint[aij(0, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(0, j - 1)] + kdint[aij(0, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[0] = 1.0 + factxp;
sup[0] = -factxp;
f[0] =
zintp[aij(0, j)] + factyp * zintp[aij(0, j+1)] -
(factyp + factym) *
zintp[aij(0, j)] + factym * zintp[aij(0, j-1)];
f[0] = zintp[aij(0, j)] + factyp * zintp[aij(0, j + 1)]
- (factyp + factym) * zintp[aij(0, j)]
+ factym * zintp[aij(0, j - 1)];
}
/*
! right bc
/*
! right bc
if (cbc(2:2).eq.'1') then
diag(nx)=1.
inf(nx)=0.
@ -198,40 +204,41 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf(nx)=-factxm
f(nx)=zintp(nx,j)+factyp*zintp(nx,j+1)-(factyp+factym)*zintp(nx,j)+factym*zintp(nx,j-1)
endif
*/
*/
if true {
diag[nx - 1] = 1.0;
inf[nx - 1] = 0.0;
f[nx - 1] = zintp[aij(nx - 1, j)];
} else {
// reflective boundary
factxm = (kdint[aij(nx-2, j)] + kdint[aij(nx-1, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(nx-1, j+1)] + kdint[aij(nx-1, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(nx-1, j-1)] + kdint[aij(nx-1, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factxm = (kdint[aij(nx - 2, j)] + kdint[aij(nx - 1, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp =
(kdint[aij(nx - 1, j + 1)] + kdint[aij(nx - 1, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym =
(kdint[aij(nx - 1, j - 1)] + kdint[aij(nx - 1, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[nx - 1] = 1.0 + factxm;
inf[nx - 1] = -factxm;
f[nx - 1] =
zintp[aij(nx-1, j)] + factyp * zintp[aij(nx-1, j+1)] -
(factyp + factym) *
zintp[aij(nx-1, j)] + factym * zintp[aij(nx-1, j-1)];
f[nx - 1] = zintp[aij(nx - 1, j)] + factyp * zintp[aij(nx - 1, j + 1)]
- (factyp + factym) * zintp[aij(nx - 1, j)]
+ factym * zintp[aij(nx - 1, j - 1)];
}
/*
/*
call tridag (inf,diag,sup,f,res,nx)
do i=1,nx
zint(i,j)=res(i)
enddo
*/
*/
tridag(&inf, &diag, &sup, &f, &mut res, nx);
for i in 0..nx {
zint[aij(i, j)] = res[i];
}
/*
/*
enddo
deallocate (f,diag,sup,inf,res)
*/
*/
}
/*
/*
! second pass along y-axis
allocate (f(ny),diag(ny),sup(ny),inf(ny),res(ny))
@ -241,14 +248,14 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf=0.d0
res=0.d0
do i=2,nx-1
*/
*/
f = vec![0.0; ny];
diag = vec![0.0; ny];
sup = vec![0.0; ny];
inf = vec![0.0; ny];
res = vec![0.0; ny];
for i in 1..nx-1 {
/*
for i in 1..nx - 1 {
/*
do j=2,ny-1
factxp=(kdint(i+1,j)+kdint(i,j))/2.d0*(dt/2.)/dx**2
factxm=(kdint(i-1,j)+kdint(i,j))/2.d0*(dt/2.)/dx**2
@ -259,22 +266,21 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf(j)=-factym
f(j)=zint(i,j)+factxp*zint(i+1,j)-(factxp+factxm)*zint(i,j)+factxm*zint(i-1,j)
enddo
*/
for j in 1..ny-1 {
factxp = (kdint[aij(i+1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i-1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(i, j+1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(i, j-1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
*/
for j in 1..ny - 1 {
factxp = (kdint[aij(i + 1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i - 1, j)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(i, j + 1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
factym = (kdint[aij(i, j - 1)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[j] = 1.0 + factyp + factym;
sup[j] = -factyp;
inf[j] = -factym;
f[j] =
zint[aij(i, j)] + factxp * zint[aij(i+1, j)] -
(factxp + factxm) *
zint[aij(i, j)] + factxm * zint[aij(i-1, j)];
f[j] = zint[aij(i, j)] + factxp * zint[aij(i + 1, j)]
- (factxp + factxm) * zint[aij(i, j)]
+ factxm * zint[aij(i - 1, j)];
}
/*
! bottom bc
/*
! bottom bc
if (cbc(1:1).eq.'1') then
diag(1)=1.
sup(1)=0.
@ -287,7 +293,7 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
sup(1)=-factyp
f(1)=zint(i,1)+factxp*zint(i+1,1)-(factxp+factxm)*zint(i,1)+factxm*zint(i-1,1)
endif
*/
*/
if true {
diag[0] = 1.0;
sup[0] = 0.0;
@ -297,18 +303,17 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
// TODO: Check whether this j was actually supposed to be a 0 in the original
// (probably).
// factxp = (kdint[aij(i+1, 0)] + kdint[aij(i, j)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxp = (kdint[aij(i+1, 0)] + kdint[aij(i, 0)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i-1, 0)] + kdint[aij(i, 0)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxp = (kdint[aij(i + 1, 0)] + kdint[aij(i, 0)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i - 1, 0)] + kdint[aij(i, 0)]) / 2.0 * (dt / 2.0) / (dx * dx);
factyp = (kdint[aij(i, 1)] + kdint[aij(i, 0)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[0] = 1.0 + factyp;
sup[0] = -factyp;
f[0] =
zint[aij(i, 0)] + factxp * zint[aij(i+1, 0)] -
(factxp + factxm) *
zint[aij(i, 0)] + factxm * zint[aij(i-1, 0)];
f[0] = zint[aij(i, 0)] + factxp * zint[aij(i + 1, 0)]
- (factxp + factxm) * zint[aij(i, 0)]
+ factxm * zint[aij(i - 1, 0)];
}
/*
! top bc
/*
! top bc
if (cbc(3:3).eq.'1') then
diag(ny)=1.
inf(ny)=0.
@ -321,39 +326,40 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
inf(ny)=-factym
f(ny)=zint(i,ny)+factxp*zint(i+1,ny)-(factxp+factxm)*zint(i,ny)+factxm*zint(i-1,ny)
endif
*/
*/
if true {
diag[ny - 1] = 1.0;
inf[ny - 1] = 0.0;
f[ny - 1] = zint[aij(i, ny - 1)];
} else {
// reflective boundary
factxp = (kdint[aij(i+1, ny-1)] + kdint[aij(i, ny-1)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm = (kdint[aij(i-1, ny-1)] + kdint[aij(i, ny-1)]) / 2.0 * (dt / 2.0) / (dx * dx);
factym = (kdint[aij(i, ny-2)] + kdint[aij(i, ny-1)]) / 2.0 * (dt / 2.0) / (dy * dy);
factxp =
(kdint[aij(i + 1, ny - 1)] + kdint[aij(i, ny - 1)]) / 2.0 * (dt / 2.0) / (dx * dx);
factxm =
(kdint[aij(i - 1, ny - 1)] + kdint[aij(i, ny - 1)]) / 2.0 * (dt / 2.0) / (dx * dx);
factym = (kdint[aij(i, ny - 2)] + kdint[aij(i, ny - 1)]) / 2.0 * (dt / 2.0) / (dy * dy);
diag[ny - 1] = 1.0 + factym;
inf[ny - 1] = -factym;
f[ny - 1] =
zint[aij(i, ny-1)] + factxp * zint[aij(i+1, ny-1)] -
(factxp + factxm) *
zint[aij(i, ny-1)] + factxm * zint[aij(i-1, ny-1)];
f[ny - 1] = zint[aij(i, ny - 1)] + factxp * zint[aij(i + 1, ny - 1)]
- (factxp + factxm) * zint[aij(i, ny - 1)]
+ factxm * zint[aij(i - 1, ny - 1)];
}
/*
/*
call tridag (inf,diag,sup,f,res,ny)
do j=1,ny
zintp(i,j)=res(j)
enddo
*/
*/
tridag(&inf, &diag, &sup, &f, &mut res, ny);
for j in 0..ny {
zintp[aij(i, j)] = res[j];
}
/*
/*
enddo
deallocate (f,diag,sup,inf,res)
*/
*/
}
/*
/*
! stores result in 1D array
do j=1,ny
@ -366,7 +372,7 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
enddo
b=min(h,b)
*/
*/
for j in 0..ny {
for i in 0..nx {
ij = aij(i, j);
@ -378,13 +384,13 @@ pub fn diffusion(nx: usize, ny: usize, xl: f64, yl: f64, dt: f64, _ibc: (),
b.par_iter_mut().zip(h).for_each(|(mut b, h)| {
*b = h.min(*b);
});
/*
/*
deallocate (zint,kdint,zintp)
return
end subroutine Diffusion
*/
end subroutine Diffusion
*/
}
/*
@ -400,7 +406,7 @@ end subroutine Diffusion
double precision a(n),b(n),c(n),r(n),u(n)
*/
pub fn tridag(a: &[f64], b: &[f64], c: &[f64], r: &[f64], u: &mut [f64], n: usize) {
/*
/*
INTEGER j
double precision bet
double precision,dimension(:),allocatable::gam
@ -408,22 +414,22 @@ pub fn tridag(a: &[f64], b: &[f64], c: &[f64], r: &[f64], u: &mut [f64], n: usiz
allocate (gam(n))
if(b(1).eq.0.d0) stop 'in tridag'
*/
let mut j : usize;
let mut bet : f64;
let mut precision : f64;
*/
let mut j: usize;
let mut bet: f64;
let mut precision: f64;
let mut gam: Vec<f64>;
gam = vec![Default::default(); n];
assert!(b[0] != 0.0);
/*
/*
! first pass
! first pass
bet=b(1)
u(1)=r(1)/bet
do 11 j=2,n
bet=b(1)
u(1)=r(1)/bet
do 11 j=2,n
gam(j)=c(j-1)/bet
bet=b(j)-a(j)*gam(j)
if(bet.eq.0.) then
@ -432,7 +438,7 @@ do 11 j=2,n
endif
u(j)=(r(j)-a(j)*u(j-1))/bet
11 continue
*/
*/
bet = b[0];
u[0] = r[0] / bet;
for j in 1..n {
@ -448,21 +454,21 @@ do 11 j=2,n
// = r'[j] / b'[j]
u[j] = (r[j] - a[j] * u[j - 1]) / bet;
}
/*
/*
! second pass
do 12 j=n-1,1,-1
u(j)=u(j)-gam(j+1)*u(j+1)
12 continue
*/
*/
for j in (0..n - 1).rev() {
u[j] = u[j] - gam[j + 1] * u[j + 1];
}
/*
/*
deallocate (gam)
return
END
*/
*/
}

View File

@ -12,9 +12,9 @@ pub use self::erosion::{
pub use self::location::Location;
pub use self::settlement::Settlement;
pub use self::util::{
cdf_irwin_hall, downhill, get_oceans, HybridMulti as HybridMulti_, local_cells, map_edge_factor, neighbors,
NEIGHBOR_DELTA, ScaleBias,
uniform_idx_as_vec2, uniform_noise, uphill, vec2_as_uniform_idx, InverseCdf,
cdf_irwin_hall, downhill, get_oceans, local_cells, map_edge_factor, neighbors,
uniform_idx_as_vec2, uniform_noise, uphill, vec2_as_uniform_idx, HybridMulti as HybridMulti_,
InverseCdf, ScaleBias, NEIGHBOR_DELTA,
};
use crate::{
@ -29,8 +29,8 @@ use common::{
vol::RectVolSize,
};
use noise::{
BasicMulti, Billow, Fbm, HybridMulti, MultiFractal, NoiseFn, RangeFunction,
RidgedMulti, Seedable, SuperSimplex, Worley,
BasicMulti, Billow, Fbm, HybridMulti, MultiFractal, NoiseFn, RangeFunction, RidgedMulti,
Seedable, SuperSimplex, Worley,
};
use num::{Float, Signed};
use rand::{Rng, SeedableRng};
@ -39,9 +39,9 @@ use rayon::prelude::*;
use serde_derive::{Deserialize, Serialize};
use std::{
collections::HashMap,
io::{BufReader, BufWriter},
f32, f64,
fs::File,
io::{BufReader, BufWriter},
ops::{Add, Div, Mul, Neg, Sub},
path::PathBuf,
sync::Arc,
@ -54,10 +54,7 @@ use vek::*;
// cleanly representable in f32 (that stops around 1024 * 4 * 1024 * 4, for signed floats anyway)
// but I think that is probably less important since I don't think we actually cast a chunk id to
// float, just coordinates... could be wrong though!
pub const WORLD_SIZE: Vec2<usize> = Vec2 {
x: 1024,
y: 1024,
};
pub const WORLD_SIZE: Vec2<usize> = Vec2 { x: 1024, y: 1024 };
/// A structure that holds cached noise values and cumulative distribution functions for the input
/// that led to those values. See the definition of InverseCdf for a description of how to
@ -107,7 +104,7 @@ pub(crate) struct GenCtx {
pub town_gen: StructureGen2d,
pub river_seed: RandomField,
pub rock_strength_nz: /*HybridMulti_*/Fbm,
pub rock_strength_nz: Fbm,
pub uplift_nz: Worley,
}
@ -146,7 +143,7 @@ impl Default for WorldOpts {
/// A way to store certain components between runs of map generation. Only intended for
/// development purposes--no attempt is made to detect map invalidation or make sure that the map
/// is synchronized with updates to noise-rs, changes to other parameters, etc.
#[derive(Serialize,Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct WorldFile {
/// Saved altitude height map.
pub alt: Box<[Alt]>,
@ -166,7 +163,9 @@ pub struct WorldSim {
impl WorldSim {
pub fn generate(seed: u32, opts: WorldOpts) -> Self {
let mut rng = ChaChaRng::from_seed(seed_expan::rng_state(seed));
let continent_scale = 5_000.0f64/*32768.0*/.div(32.0).mul(TerrainChunkSize::RECT_SIZE.x as f64);
let continent_scale = 5_000.0f64 /*32768.0*/
.div(32.0)
.mul(TerrainChunkSize::RECT_SIZE.x as f64);
let rock_lacunarity = /*0.5*/2.0/*HybridMulti::DEFAULT_LACUNARITY*/;
let uplift_scale = /*512.0*//*256.0*/128.0;
let uplift_turb_scale = uplift_scale / 4.0/*32.0*//*64.0*/;
@ -290,9 +289,9 @@ impl WorldSim {
let erosion_pow_low = /*0.25*//*1.5*//*2.0*//*0.5*//*4.0*//*0.25*//*1.0*//*2.0*//*1.5*//*1.5*//*0.35*//*0.43*//*0.5*//*0.45*//*0.37*/1.002;
let erosion_pow_high = /*1.5*//*1.0*//*0.55*//*0.51*//*2.0*/1.002;
let erosion_center = /*0.45*//*0.75*//*0.75*//*0.5*//*0.75*/0.5;
let n_steps = /*200*//*10_000*//*1000*//*50*//*100*/100;//100; // /*100*//*50*//*100*//*100*//*50*//*25*/25/*100*//*37*/;//150;//37/*100*/;//50;//50;//37;//50;//37; // /*37*//*29*//*40*//*150*/37; //150;//200;
let n_small_steps = 0;//25;//8;//50;//50;//8;//8;//8;//8;//8; // 8
let n_post_load_steps = 0;//25;//8
let n_steps = /*200*//*10_000*//*1000*//*50*//*100*/100; //100; // /*100*//*50*//*100*//*100*//*50*//*25*/25/*100*//*37*/;//150;//37/*100*/;//50;//50;//37;//50;//37; // /*37*//*29*//*40*//*150*/37; //150;//200;
let n_small_steps = 0; //25;//8;//50;//50;//8;//8;//8;//8;//8; // 8
let n_post_load_steps = 0; //25;//8
// Logistic regression. Make sure x ∈ (0, 1).
let logit = |x: f64| x.ln() - (-x).ln_1p();
@ -304,7 +303,8 @@ impl WorldSim {
let exp_inverse_cdf = |x: f64/*, pow: f64*/| -(-x).ln_1p()/* / ln(pow)*/;
// 2 / pi * ln(tan(pi/2 * p))
let hypsec_inverse_cdf = |x: f64| f64::consts::FRAC_2_PI * ((x * f64::consts::FRAC_PI_2).tan().ln());
let hypsec_inverse_cdf =
|x: f64| f64::consts::FRAC_2_PI * ((x * f64::consts::FRAC_PI_2).tan().ln());
let min_epsilon =
1.0 / (WORLD_SIZE.x as f64 * WORLD_SIZE.y as f64).max(f64::EPSILON as f64 * 0.5);
@ -364,8 +364,7 @@ impl WorldSim {
.alt_nz
.get((wposf.div(10_000.0)).into_array())
.min(1.0)
.max(-1.0)
/* .mul(0.25)
.max(-1.0)/* .mul(0.25)
.add(0.125) */)
// .add(0.5)
.sub(0.05)
@ -525,7 +524,8 @@ impl WorldSim {
});
// Calculate oceans.
let old_height = |posi: usize| alt_old[posi].1 * CONFIG.mountain_scale * height_scale as f32;
let old_height =
|posi: usize| alt_old[posi].1 * CONFIG.mountain_scale * height_scale as f32;
/* let is_ocean = (0..WORLD_SIZE.x * WORLD_SIZE.y)
.into_par_iter()
.map(|i| map_edge_factor(i) == 0.0)
@ -534,42 +534,43 @@ impl WorldSim {
let is_ocean_fn = |posi: usize| is_ocean[posi];
let turb_wposf_div = 8.0/*64.0*/;
let uplift_nz_dist = gen_ctx.uplift_nz
.clone()
.enable_range(true);
let uplift_nz_dist = gen_ctx.uplift_nz.clone().enable_range(true);
// Recalculate altitudes without oceans.
// NaNs in these uniform vectors wherever is_ocean_fn returns true.
let (alt_old_no_ocean, alt_old_inverse) =
uniform_noise(|posi, _| {
let (alt_old_no_ocean, alt_old_inverse) = uniform_noise(|posi, _| {
if is_ocean_fn(posi) {
None
} else {
Some(old_height(posi) /*.abs()*/)
}
});
let (uplift_uniform, _) =
uniform_noise(|posi, wposf| {
let (uplift_uniform, _) = uniform_noise(|posi, wposf| {
if is_ocean_fn(posi) {
None
} else {
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
// let turb = Vec2::zero();
let turb_wposf = wposf + turb;
let turb_wposi = turb_wposf
.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as f64)
.map2(WORLD_SIZE, |e, f| (e as i32).max(f as i32 - 1).min(0));
let turb_posi = vec2_as_uniform_idx(turb_wposi);
let udist = uplift_nz_dist.get(turb_wposf.into_array())
let udist = uplift_nz_dist
.get(turb_wposf.into_array())
.min(1.0)
.max(-1.0)
.mul(0.5)
.add(0.5);
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -588,15 +589,22 @@ impl WorldSim {
let oheight = /*alt_old*//*alt_base*/alt_old_no_ocean[/*(turb_posi / 64) * 64*/posi].0 as f64 - 0.5;
assert!(udist >= 0.0);
assert!(udist <= 1.0);
let uheight_1 = uheight;//.powf(2.0);
let uheight_1 = uheight; //.powf(2.0);
let udist_1 = (0.5 - udist).mul(2.0).max(0.0);
let udist_2 = udist.mul(2.0).min(1.0);
let udist_3 = (1.0 - udist).max(0.0);
let udist_4 = udist.min(1.0);
let variation = 1.0.min(64.0 * 64.0 / (WORLD_SIZE.x as f64 * WORLD_SIZE.y as f64 * (TerrainChunkSize::RECT_SIZE.x as f64 * TerrainChunkSize::RECT_SIZE.y as f64 / 128.0 / 128.0)));
let variation = 1.0.min(
64.0 * 64.0
/ (WORLD_SIZE.x as f64
* WORLD_SIZE.y as f64
* (TerrainChunkSize::RECT_SIZE.x as f64
* TerrainChunkSize::RECT_SIZE.y as f64
/ 128.0
/ 128.0)),
);
let variation_1 = (uheight * /*udist_2*/udist_4).min(variation);
let height =
(oheight + 0.5).powf(2.0);
let height = (oheight + 0.5).powf(2.0);
// 1.0 - variation + variation * uchaos_1;
// uheight * /*udist_2*/udist_4 - variation_1 + variation_1 * uchaos_1;
// uheight * (0.5 + 0.5 * ((uchaos as f64) / 1.32)) - 0.125;
@ -617,7 +625,6 @@ impl WorldSim {
// (uheight + oheight.powf(2.0) * 0.2).max(0.0).min(1.0);
// * (1.0 - udist);// uheight * (1.0 - udist)/*oheight*//* * udist*/ + oheight * udist;/*uheight * (1.0 - udist);*/
// let height = uheight * (0.5 - udist) * 0.8 + (oheight.signum() * oheight.max(0.0).abs().powf(2.0)) * 0.2;// * (1.0 - udist);// uheight * (1.0 - udist)/*oheight*//* * udist*/ + oheight * udist;/*uheight * (1.0 - udist);*/
Some(height)
}
});
@ -684,22 +691,25 @@ impl WorldSim {
if is_ocean_fn(posi) {
return 1.0;
}
let wposf = (uniform_idx_as_vec2(posi)
* TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
let wposf = (uniform_idx_as_vec2(posi) * TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
.map(|e| e as f64);
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
// let turb = Vec2::zero();
let turb_wposf = wposf + turb;
let turb_wposi = turb_wposf
.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as f64)
.map2(WORLD_SIZE, |e, f| (e as i32).max(f as i32 - 1).min(0));
let turb_posi = vec2_as_uniform_idx(turb_wposi);
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -716,9 +726,7 @@ impl WorldSim {
// ((3.5 - 1.5) * (1.0 - uheight) + 1.5) as f32
1.0
};
let theta_func = |posi| {
0.4
};
let theta_func = |posi| 0.4;
let kf_func = {
|posi| {
if is_ocean_fn(posi) {
@ -731,19 +739,23 @@ impl WorldSim {
let wposf = (uniform_idx_as_vec2(posi)
* TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
.map(|e| e as f64);
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
// let turb = Vec2::zero();
let turb_wposf = wposf + turb;
let turb_wposi = turb_wposf
.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as f64)
.map2(WORLD_SIZE, |e, f| (e as i32).max(f as i32 - 1).min(0));
let turb_posi = vec2_as_uniform_idx(turb_wposi);
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -784,7 +796,7 @@ impl WorldSim {
// ((1.0 - uheight) * (0.5 - 0.5 * /*((1.32 - uchaos as f64) / 1.32)*/oheight_2) * (1.5e-4 - 2.0e-6) + 2.0e-6)
// ((1.0 - uheight) * (0.5 - 0.5 * /*((1.32 - uchaos as f64) / 1.32)*/oheight) * (1.5e-4 - 2.0e-6) + 2.0e-6)
// 2e-5
2.5e-6 * 16.0.powf(0.4)/* / 4.0 * 0.25 *//* * 4.0*/
2.5e-6 * 16.0.powf(0.4) /* / 4.0 * 0.25 *//* * 4.0*/
// 2.9e-10
// ((1.0 - uheight) * (5e-5 - 2.9e-10) + 2.9e-10)
// ((1.0 - uheight) * (5e-5 - 2.9e-14) + 2.9e-14)
@ -798,19 +810,23 @@ impl WorldSim {
let wposf = (uniform_idx_as_vec2(posi)
* TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
.map(|e| e as f64);
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
// let turb = Vec2::zero();
let turb_wposf = wposf + turb;
let turb_wposi = turb_wposf
.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as f64)
.map2(WORLD_SIZE, |e, f| (e as i32).max(f as i32 - 1).min(0));
let turb_posi = vec2_as_uniform_idx(turb_wposi);
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -824,28 +840,32 @@ impl WorldSim {
// (uheight * (1.0e-1 - 1.0e-2) + 1.0e-2)
}
};
let g_func =
|posi| {
if /*is_ocean_fn(posi)*/map_edge_factor(posi) == 0.0 {
let g_func = |posi| {
if
/*is_ocean_fn(posi)*/
map_edge_factor(posi) == 0.0 {
return 0.0;
// return 5.0;
}
let wposf = (uniform_idx_as_vec2(posi)
* TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
let wposf = (uniform_idx_as_vec2(posi) * TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
.map(|e| e as f64);
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
// let turb = Vec2::zero();
let turb_wposf = wposf + turb;
let turb_wposi = turb_wposf
.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as f64)
.map2(WORLD_SIZE, |e, f| (e as i32).max(f as i32 - 1).min(0));
let turb_posi = vec2_as_uniform_idx(turb_wposi);
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -884,15 +904,13 @@ impl WorldSim {
1.0
// 1.5
};
let uplift_fn =
|posi| {
let uplift_fn = |posi| {
if is_ocean_fn(posi) {
/* return 1e-2
.mul(max_erosion_per_delta_t) as f32; */
return 0.0;
}
let wposf = (uniform_idx_as_vec2(posi)
* TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
let wposf = (uniform_idx_as_vec2(posi) * TerrainChunkSize::RECT_SIZE.map(|e| e as i32))
.map(|e| e as f64);
let alt_main = {
// Extension upwards from the base. A positive number from 0 to 1 curved to be
@ -918,8 +936,7 @@ impl WorldSim {
.mul(alt_main.powf(0.8).max(/*0.25*/ 0.15))
.mul(0.3)
.add(1.0)
.mul(0.4)
/* + spring(alt_main.abs().powf(0.5).min(0.75).mul(60.0).sin(), 4.0)
.mul(0.4)/* + spring(alt_main.abs().powf(0.5).min(0.75).mul(60.0).sin(), 4.0)
.mul(0.045)*/)
};
let height =
@ -966,7 +983,8 @@ impl WorldSim {
// assert!(alt_main >= 0.0);
let (bump_factor, bump_max) = if
/*height < f32::EPSILON as f64 * 0.5*//*false*/
/*true*/false {
/*true*/
false {
(
/*(alt_main./*to_le_bytes()[7]*/to_bits() & 1) as f64*/
(alt_main / CONFIG.mountain_scale as f64 * 128.0).mul(0.1).powf(1.2) * /*(1.0 / CONFIG.mountain_scale as f64)*/(f32::EPSILON * 0.5) as f64,
@ -981,14 +999,18 @@ impl WorldSim {
// tan(pi/6)*32 ~ 18
// tan(54/360*2*pi)*32
// let height = 1.0f64;
let turb_wposf =
wposf.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64)).div(turb_wposf_div);
let turb_wposf = wposf
.div(TerrainChunkSize::RECT_SIZE.map(|e| e as f64))
.div(turb_wposf_div);
let turb = Vec2::new(
gen_ctx.turb_x_nz.get(turb_wposf.into_array()),
gen_ctx.turb_y_nz.get(turb_wposf.into_array()),
) * uplift_turb_scale * TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
) * uplift_turb_scale
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64);
let turb_wposf = wposf + turb;
let uheight = gen_ctx.uplift_nz.get(turb_wposf.into_array())
let uheight = gen_ctx
.uplift_nz
.get(turb_wposf.into_array())
/* .min(0.5)
.max(-0.5)*/
.min(1.0)
@ -1062,8 +1084,7 @@ impl WorldSim {
.mul(alt_main.powf(0.8).max(/*0.25*/ 0.15))
.mul(0.3)
.add(1.0)
.mul(0.4)
/* + spring(alt_main.abs().powf(0.5).min(0.75).mul(60.0).sin(), 4.0)
.mul(0.4)/* + spring(alt_main.abs().powf(0.5).min(0.75).mul(60.0).sin(), 4.0)
.mul(0.045)*/)
};
@ -1112,7 +1133,7 @@ impl WorldSim {
};
let reader = BufReader::new(file);
let map : WorldFile = match bincode::deserialize_from(reader) {
let map: WorldFile = match bincode::deserialize_from(reader) {
Ok(map) => map,
Err(err) => {
log::warn!("Couldn't parse map: {:?})", err);
@ -1120,7 +1141,9 @@ impl WorldSim {
}
};
if map.alt.len() != map.basement.len() || map.alt.len() != WORLD_SIZE.x as usize * WORLD_SIZE.y as usize {
if map.alt.len() != map.basement.len()
|| map.alt.len() != WORLD_SIZE.x as usize * WORLD_SIZE.y as usize
{
log::warn!("World size of map is invalid.");
return None;
}
@ -1149,8 +1172,16 @@ impl WorldSim {
n_steps,
&river_seed,
&rock_strength_nz,
|posi| alt_func(posi),// + if is_ocean_fn(posi) { 0.0 } else { 128.0 },
|posi| alt_func(posi) - if is_ocean_fn(posi) { 0.0 } else { /*1400.0*//*CONFIG.mountain_scale * 0.75*/0.0 },// if is_ocean_fn(posi) { old_height(posi) } else { 0.0 },
|posi| alt_func(posi), // + if is_ocean_fn(posi) { 0.0 } else { 128.0 },
|posi| {
alt_func(posi)
- if is_ocean_fn(posi) {
0.0
} else {
/*1400.0*//*CONFIG.mountain_scale * 0.75*/
0.0
}
}, // if is_ocean_fn(posi) { old_height(posi) } else { 0.0 },
is_ocean_fn,
uplift_fn,
|posi| n_func(posi),
@ -1180,13 +1211,10 @@ impl WorldSim {
};
// Save map, if necessary.
let map = WorldFile {
alt,
basement,
};
let map = WorldFile { alt, basement };
(|| {
if let FileOpts::Save = opts.world_file {
use std::{time::SystemTime};
use std::time::SystemTime;
// Check if folder exists and create it if it does not
let mut path = PathBuf::from("./maps");
if !path.exists() {
@ -1243,7 +1271,10 @@ impl WorldSim {
let is_ocean = get_oceans(|posi| alt[posi]);
let is_ocean_fn = |posi: usize| is_ocean[posi];
let mut dh = downhill(|posi| alt[posi] as f32/*&alt*/, /*old_height*/ is_ocean_fn);
let mut dh = downhill(
|posi| alt[posi] as f32, /*&alt*/
/*old_height*/ is_ocean_fn,
);
let (boundary_len, indirection, water_alt_pos, _) = get_lakes(/*&/*water_alt*/alt*/|posi| alt[posi] as f32, &mut dh);
let flux_old = get_drainage(&water_alt_pos, &dh, boundary_len);
@ -1258,7 +1289,9 @@ impl WorldSim {
/* // Find the pass this lake is flowing into (i.e. water at the lake bottom gets
// pushed towards the point identified by pass_idx).
let neighbor_pass_idx = dh[lake_idx]; */
let chunk_water_alt = if /*neighbor_pass_idx*/dh[lake_idx] < 0 {
let chunk_water_alt = if
/*neighbor_pass_idx*/
dh[lake_idx] < 0 {
// This is either a boundary node (dh[chunk_idx] == -2, i.e. water is at sea level)
// or part of a lake that flows directly into the ocean. In the former case, water
// is at sea level so we just return 0.0. In the latter case, the lake bottom must
@ -1312,7 +1345,9 @@ impl WorldSim {
/* // Find the pass this lake is flowing into (i.e. water at the lake bottom gets
// pushed towards the point identified by pass_idx).
let neighbor_pass_idx = dh[lake_idx]; */
if /*neighbor_pass_idx*/dh[lake_idx] < 0 {
if
/*neighbor_pass_idx*/
dh[lake_idx] < 0 {
// This is either a boundary node (dh[chunk_idx] == -2, i.e. water is at sea level)
// or part of a lake that flows directly into the ocean. In the former case, water
// is at sea level so we just return 0.0. In the latter case, the lake bottom must
@ -1970,9 +2005,7 @@ impl SimChunk {
let height_scale = 1.0; // 1.0 / CONFIG.mountain_scale;
let mut alt = CONFIG.sea_level.add(alt_pre.div(height_scale));
let mut basement = CONFIG.sea_level.add(basement_pre.div(height_scale));
let water_alt = CONFIG
.sea_level
.add(water_alt_pre.div(height_scale));
let water_alt = CONFIG.sea_level.add(water_alt_pre.div(height_scale));
let downhill = if downhill_pre == -2 {
None
} else if downhill_pre < 0 {
@ -2030,7 +2063,9 @@ impl SimChunk {
}
// No trees in the ocean, with zero humidity (currently), or directly on bedrock.
let tree_density = if is_underwater/* || alt - basement.min(alt) < 2.0 */ {
let tree_density = if is_underwater
/* || alt - basement.min(alt) < 2.0 */
{
0.0
} else {
let tree_density = (gen_ctx.tree_nz.get((wposf.div(1024.0)).into_array()))

View File

@ -218,7 +218,7 @@ pub fn local_cells(posi: usize) -> impl Clone + Iterator<Item = usize> {
}
// NOTE: want to keep this such that the chunk index is in ascending order!
pub const NEIGHBOR_DELTA : [(i32, i32); 8] = [
pub const NEIGHBOR_DELTA: [(i32, i32); 8] = [
(-1, -1),
(0, -1),
(1, -1),
@ -249,10 +249,14 @@ pub fn uphill<'a>(dh: &'a [isize], posi: usize) -> impl Clone + Iterator<Item =
/// Compute the neighbor "most downhill" from all chunks.
///
/// TODO: See if allocating in advance is worthwhile.
pub fn downhill<F: Float>(h: impl Fn(usize) -> F + Sync, is_ocean: impl Fn(usize) -> bool + Sync) -> Box<[isize]> {
pub fn downhill<F: Float>(
h: impl Fn(usize) -> F + Sync,
is_ocean: impl Fn(usize) -> bool + Sync,
) -> Box<[isize]> {
// Constructs not only the list of downhill nodes, but also computes an ordering (visiting
// nodes in order from roots to leaves).
(0..WORLD_SIZE.x * WORLD_SIZE.y).into_par_iter()
(0..WORLD_SIZE.x * WORLD_SIZE.y)
.into_par_iter()
// .enumerate()
.map(|(posi/*, &nh*/)| {
let nh = h(posi);
@ -702,5 +706,3 @@ impl<'a, F: NoiseFn<T> + 'a, T> NoiseFn<T> for ScaleBias<'a, F> {
(self.source.get(point) * self.scale) + self.bias
}
}