2019-06-10 16:28:02 +00:00
|
|
|
|
mod location;
|
2019-06-25 15:59:09 +00:00
|
|
|
|
mod settlement;
|
2019-08-22 23:31:27 +00:00
|
|
|
|
mod util;
|
2019-06-10 16:28:02 +00:00
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
// Reexports
|
|
|
|
|
pub use self::location::Location;
|
2019-06-25 15:59:09 +00:00
|
|
|
|
pub use self::settlement::Settlement;
|
2019-08-25 15:49:33 +00:00
|
|
|
|
use self::util::{
|
2019-08-25 16:17:51 +00:00
|
|
|
|
cdf_irwin_hall, uniform_idx_as_vec2, uniform_noise, vec2_as_uniform_idx, InverseCdf,
|
2019-08-25 15:49:33 +00:00
|
|
|
|
};
|
2019-06-18 21:22:31 +00:00
|
|
|
|
|
2019-06-22 21:44:27 +00:00
|
|
|
|
use crate::{
|
|
|
|
|
all::ForestKind,
|
2019-08-24 22:57:55 +00:00
|
|
|
|
column::ColumnGen,
|
2019-08-23 21:33:14 +00:00
|
|
|
|
generator::TownState,
|
2019-08-23 23:25:01 +00:00
|
|
|
|
util::{seed_expan, FastNoise, Sampler, StructureGen2d},
|
2019-06-22 21:44:27 +00:00
|
|
|
|
CONFIG,
|
|
|
|
|
};
|
2019-06-18 21:22:31 +00:00
|
|
|
|
use common::{
|
|
|
|
|
terrain::{BiomeKind, TerrainChunkSize},
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
vol::RectVolSize,
|
2019-06-18 21:22:31 +00:00
|
|
|
|
};
|
2019-08-22 21:25:17 +00:00
|
|
|
|
use noise::{
|
|
|
|
|
BasicMulti, Billow, HybridMulti, MultiFractal, NoiseFn, RidgedMulti, Seedable, SuperSimplex,
|
|
|
|
|
};
|
2019-07-30 14:10:59 +00:00
|
|
|
|
use rand::{Rng, SeedableRng};
|
|
|
|
|
use rand_chacha::ChaChaRng;
|
2019-08-18 16:35:27 +00:00
|
|
|
|
use std::{
|
2019-08-24 13:23:42 +00:00
|
|
|
|
collections::HashMap,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
f32,
|
|
|
|
|
ops::{Add, Div, Mul, Neg, Sub},
|
2019-08-23 21:33:14 +00:00
|
|
|
|
sync::Arc,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
};
|
2019-06-09 10:24:18 +00:00
|
|
|
|
use vek::*;
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
|
|
|
|
pub const WORLD_SIZE: Vec2<usize> = Vec2 { x: 1024, y: 1024 };
|
2019-05-18 08:59:58 +00:00
|
|
|
|
|
2019-08-21 18:41:32 +00:00
|
|
|
|
/// Calculates the smallest distance along an axis (x, y) from an edge of
|
|
|
|
|
/// the world. This value is maximal at WORLD_SIZE / 2 and minimized at the extremes
|
|
|
|
|
/// (0 or WORLD_SIZE on one or more axes). It then divides the quantity by cell_size,
|
|
|
|
|
/// so the final result is 1 when we are not in a cell along the edge of the world, and
|
|
|
|
|
/// ranges between 0 and 1 otherwise (lower when the chunk is closer to the edge).
|
|
|
|
|
fn map_edge_factor(posi: usize) -> f32 {
|
|
|
|
|
uniform_idx_as_vec2(posi)
|
|
|
|
|
.map2(WORLD_SIZE.map(|e| e as i32), |e, sz| {
|
|
|
|
|
(sz / 2 - (e - sz / 2).abs()) as f32 / 16.0
|
|
|
|
|
})
|
|
|
|
|
.reduce_partial_min()
|
|
|
|
|
.max(0.0)
|
|
|
|
|
.min(1.0)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 21:48:13 +00:00
|
|
|
|
/// 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
|
|
|
|
|
/// interpret the types of its fields.
|
2019-08-21 18:41:32 +00:00
|
|
|
|
struct GenCdf {
|
|
|
|
|
humid_base: InverseCdf,
|
|
|
|
|
temp_base: InverseCdf,
|
|
|
|
|
alt_base: InverseCdf,
|
2019-08-22 19:03:42 +00:00
|
|
|
|
chaos: InverseCdf,
|
2019-08-22 22:57:42 +00:00
|
|
|
|
alt: InverseCdf,
|
2019-08-26 09:49:14 +00:00
|
|
|
|
alt_no_seawater: InverseCdf,
|
2019-08-21 18:41:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub(crate) struct GenCtx {
|
2019-06-21 00:53:11 +00:00
|
|
|
|
pub turb_x_nz: SuperSimplex,
|
|
|
|
|
pub turb_y_nz: SuperSimplex,
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub chaos_nz: RidgedMulti,
|
|
|
|
|
pub alt_nz: HybridMulti,
|
|
|
|
|
pub hill_nz: SuperSimplex,
|
|
|
|
|
pub temp_nz: SuperSimplex,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
// Fresh groundwater (currently has no effect, but should influence humidity)
|
2019-06-19 19:58:56 +00:00
|
|
|
|
pub dry_nz: BasicMulti,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
// Humidity noise
|
2019-08-22 21:25:17 +00:00
|
|
|
|
pub humid_nz: Billow,
|
2019-08-19 17:20:54 +00:00
|
|
|
|
// Small amounts of noise for simulating rough terrain.
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub small_nz: BasicMulti,
|
|
|
|
|
pub rock_nz: HybridMulti,
|
2019-06-10 14:22:59 +00:00
|
|
|
|
pub cliff_nz: HybridMulti,
|
2019-08-23 23:25:01 +00:00
|
|
|
|
pub warp_nz: FastNoise,
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub tree_nz: BasicMulti,
|
|
|
|
|
|
|
|
|
|
pub cave_0_nz: SuperSimplex,
|
|
|
|
|
pub cave_1_nz: SuperSimplex,
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
2019-07-09 23:51:54 +00:00
|
|
|
|
pub structure_gen: StructureGen2d,
|
|
|
|
|
pub region_gen: StructureGen2d,
|
2019-06-21 00:53:11 +00:00
|
|
|
|
pub cliff_gen: StructureGen2d,
|
2019-08-23 23:25:01 +00:00
|
|
|
|
|
|
|
|
|
pub fast_turb_x_nz: FastNoise,
|
|
|
|
|
pub fast_turb_y_nz: FastNoise,
|
2019-08-24 13:23:42 +00:00
|
|
|
|
|
|
|
|
|
pub town_gen: StructureGen2d,
|
2019-05-29 13:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
|
pub struct WorldSim {
|
2019-05-20 02:53:04 +00:00
|
|
|
|
pub seed: u32,
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub(crate) chunks: Vec<SimChunk>,
|
2019-06-25 15:59:09 +00:00
|
|
|
|
pub(crate) locations: Vec<Location>,
|
|
|
|
|
|
2019-06-06 14:52:29 +00:00
|
|
|
|
pub(crate) gen_ctx: GenCtx,
|
2019-07-30 14:10:59 +00:00
|
|
|
|
pub rng: ChaChaRng,
|
2019-05-18 08:59:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WorldSim {
|
2019-09-17 14:05:08 +00:00
|
|
|
|
pub fn generate(seed: u32) -> Self {
|
|
|
|
|
let mut rng = ChaChaRng::from_seed(seed_expan::rng_state(seed));
|
2019-08-11 11:35:04 +00:00
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
|
let mut gen_ctx = GenCtx {
|
2019-09-17 14:05:08 +00:00
|
|
|
|
turb_x_nz: SuperSimplex::new().set_seed(rng.gen()),
|
|
|
|
|
turb_y_nz: SuperSimplex::new().set_seed(rng.gen()),
|
|
|
|
|
chaos_nz: RidgedMulti::new().set_octaves(7).set_seed(rng.gen()),
|
|
|
|
|
hill_nz: SuperSimplex::new().set_seed(rng.gen()),
|
2019-05-21 00:57:16 +00:00
|
|
|
|
alt_nz: HybridMulti::new()
|
2019-06-04 17:27:58 +00:00
|
|
|
|
.set_octaves(8)
|
2019-05-21 00:57:16 +00:00
|
|
|
|
.set_persistence(0.1)
|
2019-09-17 14:05:08 +00:00
|
|
|
|
.set_seed(rng.gen()),
|
|
|
|
|
temp_nz: SuperSimplex::new().set_seed(rng.gen()),
|
|
|
|
|
dry_nz: BasicMulti::new().set_seed(rng.gen()),
|
|
|
|
|
small_nz: BasicMulti::new().set_octaves(2).set_seed(rng.gen()),
|
|
|
|
|
rock_nz: HybridMulti::new().set_persistence(0.3).set_seed(rng.gen()),
|
|
|
|
|
cliff_nz: HybridMulti::new().set_persistence(0.3).set_seed(rng.gen()),
|
|
|
|
|
warp_nz: FastNoise::new(rng.gen()), //BasicMulti::new().set_octaves(3).set_seed(gen_seed()),
|
2019-05-25 07:36:11 +00:00
|
|
|
|
tree_nz: BasicMulti::new()
|
2019-06-05 13:13:24 +00:00
|
|
|
|
.set_octaves(12)
|
2019-06-05 18:00:17 +00:00
|
|
|
|
.set_persistence(0.75)
|
2019-09-17 14:05:08 +00:00
|
|
|
|
.set_seed(rng.gen()),
|
|
|
|
|
cave_0_nz: SuperSimplex::new().set_seed(rng.gen()),
|
|
|
|
|
cave_1_nz: SuperSimplex::new().set_seed(rng.gen()),
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
2019-09-17 14:05:08 +00:00
|
|
|
|
structure_gen: StructureGen2d::new(rng.gen(), 32, 24),
|
|
|
|
|
region_gen: StructureGen2d::new(rng.gen(), 400, 96),
|
|
|
|
|
cliff_gen: StructureGen2d::new(rng.gen(), 80, 56),
|
2019-08-18 23:52:26 +00:00
|
|
|
|
humid_nz: Billow::new()
|
2019-08-19 01:41:32 +00:00
|
|
|
|
.set_octaves(12)
|
2019-08-19 03:22:39 +00:00
|
|
|
|
.set_persistence(0.125)
|
2019-08-19 17:20:54 +00:00
|
|
|
|
.set_frequency(1.0)
|
2019-08-18 23:52:26 +00:00
|
|
|
|
// .set_octaves(6)
|
|
|
|
|
// .set_persistence(0.5)
|
2019-09-17 14:05:08 +00:00
|
|
|
|
.set_seed(rng.gen()),
|
2019-08-23 23:25:01 +00:00
|
|
|
|
|
2019-09-17 14:05:08 +00:00
|
|
|
|
fast_turb_x_nz: FastNoise::new(rng.gen()),
|
|
|
|
|
fast_turb_y_nz: FastNoise::new(rng.gen()),
|
2019-08-24 13:23:42 +00:00
|
|
|
|
|
2019-09-17 14:05:08 +00:00
|
|
|
|
town_gen: StructureGen2d::new(rng.gen(), 2048, 1024),
|
2019-05-18 08:59:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-08-21 18:41:32 +00:00
|
|
|
|
// "Base" of the chunk, to be multiplied by CONFIG.mountain_scale (multiplied value is
|
|
|
|
|
// from -0.25 * (CONFIG.mountain_scale * 1.1) to 0.25 * (CONFIG.mountain_scale * 0.9),
|
|
|
|
|
// but value here is from -0.275 to 0.225).
|
2019-08-22 21:25:17 +00:00
|
|
|
|
let alt_base = uniform_noise(|_, wposf| {
|
2019-08-24 19:32:07 +00:00
|
|
|
|
Some(
|
|
|
|
|
(gen_ctx.alt_nz.get((wposf.div(12_000.0)).into_array()) as f32)
|
|
|
|
|
.sub(0.1)
|
|
|
|
|
.mul(0.25),
|
|
|
|
|
)
|
2019-08-24 19:13:32 +00:00
|
|
|
|
});
|
|
|
|
|
|
2019-08-21 18:41:32 +00:00
|
|
|
|
// chaos produces a value in [0.1, 1.24]. It is a meta-level factor intended to reflect how
|
|
|
|
|
// "chaotic" the region is--how much weird stuff is going on on this terrain.
|
2019-09-04 23:03:49 +00:00
|
|
|
|
let chaos = uniform_noise(|_posi, wposf| {
|
2019-08-24 19:13:32 +00:00
|
|
|
|
// From 0 to 1.6, but the distribution before the max is from -1 and 1, so there is a
|
|
|
|
|
// 50% chance that hill will end up at 0.
|
2019-08-24 19:32:07 +00:00
|
|
|
|
let hill = (0.0
|
|
|
|
|
+ gen_ctx
|
|
|
|
|
.hill_nz
|
|
|
|
|
.get((wposf.div(1_500.0)).into_array())
|
|
|
|
|
.mul(1.0) as f32
|
2019-08-24 19:13:32 +00:00
|
|
|
|
+ gen_ctx
|
|
|
|
|
.hill_nz
|
|
|
|
|
.get((wposf.div(400.0)).into_array())
|
|
|
|
|
.mul(0.3) as f32)
|
|
|
|
|
.add(0.3)
|
|
|
|
|
.max(0.0);
|
|
|
|
|
|
2019-08-24 19:32:07 +00:00
|
|
|
|
Some(
|
|
|
|
|
(gen_ctx.chaos_nz.get((wposf.div(3_000.0)).into_array()) as f32)
|
|
|
|
|
.add(1.0)
|
|
|
|
|
.mul(0.5)
|
|
|
|
|
// [0, 1] * [0.25, 1] = [0, 1] (but probably towards the lower end)
|
|
|
|
|
.mul(
|
|
|
|
|
(gen_ctx.chaos_nz.get((wposf.div(6_000.0)).into_array()) as f32)
|
|
|
|
|
.abs()
|
|
|
|
|
.max(0.25)
|
|
|
|
|
.min(1.0),
|
|
|
|
|
)
|
|
|
|
|
// Chaos is always increased by a little when we're on a hill (but remember that
|
|
|
|
|
// hill is 0 about 50% of the time).
|
|
|
|
|
// [0, 1] + 0.15 * [0, 1.6] = [0, 1.24]
|
|
|
|
|
.add(0.2 * hill)
|
|
|
|
|
// We can't have *no* chaos!
|
|
|
|
|
.max(0.1),
|
|
|
|
|
)
|
2019-08-22 21:25:17 +00:00
|
|
|
|
});
|
2019-08-21 18:41:32 +00:00
|
|
|
|
|
|
|
|
|
// We ignore sea level because we actually want to be relative to sea level here and want
|
2019-08-22 22:57:42 +00:00
|
|
|
|
// things in CONFIG.mountain_scale units, but otherwise this is a correct altitude
|
|
|
|
|
// calculation. Note that this is using the "unadjusted" temperature.
|
|
|
|
|
let alt = uniform_noise(|posi, wposf| {
|
|
|
|
|
// This is the extension upwards from the base added to some extra noise from -1 to 1.
|
|
|
|
|
// The extra noise is multiplied by alt_main (the mountain part of the extension)
|
|
|
|
|
// clamped to [0.25, 1], and made 60% larger (so the extra noise is between [-1.6, 1.6],
|
|
|
|
|
// and the final noise is never more than 160% or less than 40% of the original noise,
|
|
|
|
|
// depending on altitude).
|
|
|
|
|
// Adding this to alt_main thus yields a value between -0.4 (if alt_main = 0 and
|
|
|
|
|
// gen_ctx = -1) and 2.6 (if alt_main = 1 and gen_ctx = 1). When the generated small_nz
|
|
|
|
|
// value hits -0.625 the value crosses 0, so most of the points are above 0.
|
|
|
|
|
//
|
|
|
|
|
// Then, we add 1 and divide by 2 to get a value between 0.3 and 1.8.
|
|
|
|
|
let alt_main = {
|
|
|
|
|
// Extension upwards from the base. A positive number from 0 to 1 curved to be
|
|
|
|
|
// maximal at 0. Also to be multiplied by CONFIG.mountain_scale.
|
|
|
|
|
let alt_main = (gen_ctx.alt_nz.get((wposf.div(2_000.0)).into_array()) as f32)
|
|
|
|
|
.abs()
|
2019-08-25 17:04:18 +00:00
|
|
|
|
.powf(1.35);
|
2019-08-22 22:57:42 +00:00
|
|
|
|
|
|
|
|
|
(0.0 + alt_main
|
|
|
|
|
+ (gen_ctx.small_nz.get((wposf.div(300.0)).into_array()) as f32)
|
|
|
|
|
.mul(alt_main.max(0.25))
|
2019-08-24 19:13:32 +00:00
|
|
|
|
.mul(0.3)
|
2019-08-24 19:32:07 +00:00
|
|
|
|
.add(1.0)
|
2019-09-23 19:37:50 +00:00
|
|
|
|
.mul(0.5)
|
|
|
|
|
+ alt_main.mul(100.0).sin().mul(0.025))
|
2019-08-22 22:57:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Now we can compute the final altitude using chaos.
|
|
|
|
|
// We multiply by chaos clamped to [0.1, 1.24] to get a value between 0.03 and 2.232 for
|
|
|
|
|
// alt_pre, then multiply by CONFIG.mountain_scale and add to the base and sea level to
|
|
|
|
|
// get an adjusted value, then multiply the whole thing by map_edge_factor
|
|
|
|
|
// (TODO: compute final bounds).
|
2019-08-24 19:13:32 +00:00
|
|
|
|
Some((alt_base[posi].1 + alt_main.mul(chaos[posi].1)).mul(map_edge_factor(posi)))
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-25 15:49:33 +00:00
|
|
|
|
// Check whether any tiles around this tile are not water (since Lerp will ensure that they
|
|
|
|
|
// are included).
|
|
|
|
|
let pure_water = |posi| {
|
|
|
|
|
let pos = uniform_idx_as_vec2(posi);
|
2019-08-26 09:49:14 +00:00
|
|
|
|
for x in pos.x - 1..=pos.x + 1 {
|
|
|
|
|
for y in pos.y - 1..=pos.y + 1 {
|
2019-08-25 15:49:33 +00:00
|
|
|
|
if x >= 0 && y >= 0 && x < WORLD_SIZE.x as i32 && y < WORLD_SIZE.y as i32 {
|
|
|
|
|
let posi = vec2_as_uniform_idx(Vec2::new(x, y));
|
|
|
|
|
if alt[posi].1.mul(CONFIG.mountain_scale) > 0.0 {
|
2019-08-25 16:17:51 +00:00
|
|
|
|
return false;
|
2019-08-25 15:49:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-26 09:49:14 +00:00
|
|
|
|
// A version of alt that is uniform over *non-seawater* (or land-adjacent seawater) chunks.
|
2019-09-04 23:03:49 +00:00
|
|
|
|
let alt_no_seawater = uniform_noise(|posi, _wposf| {
|
2019-08-26 09:49:14 +00:00
|
|
|
|
if pure_water(posi) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(alt[posi].1)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-25 15:49:33 +00:00
|
|
|
|
// -1 to 1.
|
|
|
|
|
let temp_base = uniform_noise(|posi, wposf| {
|
|
|
|
|
if pure_water(posi) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2019-08-26 09:49:14 +00:00
|
|
|
|
Some(gen_ctx.temp_nz.get((wposf.div(12000.0)).into_array()) as f32)
|
2019-08-25 15:49:33 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-24 19:13:32 +00:00
|
|
|
|
// 0 to 1, hopefully.
|
|
|
|
|
let humid_base = uniform_noise(|posi, wposf| {
|
2019-08-25 15:49:33 +00:00
|
|
|
|
// Check whether any tiles around this tile are water.
|
|
|
|
|
if pure_water(posi) {
|
2019-08-24 19:13:32 +00:00
|
|
|
|
None
|
|
|
|
|
} else {
|
2019-08-24 19:32:07 +00:00
|
|
|
|
Some(
|
|
|
|
|
(gen_ctx.humid_nz.get(wposf.div(1024.0).into_array()) as f32)
|
|
|
|
|
.add(1.0)
|
|
|
|
|
.mul(0.5),
|
|
|
|
|
)
|
2019-08-24 19:13:32 +00:00
|
|
|
|
}
|
2019-08-22 21:25:17 +00:00
|
|
|
|
});
|
2019-08-21 18:41:32 +00:00
|
|
|
|
|
|
|
|
|
let gen_cdf = GenCdf {
|
|
|
|
|
humid_base,
|
|
|
|
|
temp_base,
|
|
|
|
|
alt_base,
|
2019-08-22 19:03:42 +00:00
|
|
|
|
chaos,
|
2019-08-22 22:57:42 +00:00
|
|
|
|
alt,
|
2019-08-26 09:49:14 +00:00
|
|
|
|
alt_no_seawater,
|
2019-08-21 18:41:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
|
let mut chunks = Vec::new();
|
2019-08-21 18:41:32 +00:00
|
|
|
|
for i in 0..WORLD_SIZE.x * WORLD_SIZE.y {
|
|
|
|
|
chunks.push(SimChunk::generate(i, &mut gen_ctx, &gen_cdf));
|
2019-05-18 08:59:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 16:28:02 +00:00
|
|
|
|
let mut this = Self {
|
2019-09-17 14:05:08 +00:00
|
|
|
|
seed: seed,
|
2019-05-18 08:59:58 +00:00
|
|
|
|
chunks,
|
2019-06-25 15:59:09 +00:00
|
|
|
|
locations: Vec::new(),
|
2019-05-21 00:57:16 +00:00
|
|
|
|
gen_ctx,
|
2019-09-17 14:05:08 +00:00
|
|
|
|
rng,
|
2019-06-10 16:28:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
this.seed_elements();
|
2019-06-10 16:28:02 +00:00
|
|
|
|
|
|
|
|
|
this
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
/// Prepare the world for simulation
|
|
|
|
|
pub fn seed_elements(&mut self) {
|
|
|
|
|
let mut rng = self.rng.clone();
|
|
|
|
|
|
2019-07-03 19:58:09 +00:00
|
|
|
|
let cell_size = 16;
|
2019-06-22 21:44:27 +00:00
|
|
|
|
let grid_size = WORLD_SIZE / cell_size;
|
2019-07-03 19:58:09 +00:00
|
|
|
|
let loc_count = 100;
|
2019-06-22 21:44:27 +00:00
|
|
|
|
|
2019-06-25 15:59:09 +00:00
|
|
|
|
let mut loc_grid = vec![None; grid_size.product()];
|
|
|
|
|
let mut locations = Vec::new();
|
2019-06-22 21:44:27 +00:00
|
|
|
|
|
|
|
|
|
// Seed the world with some locations
|
|
|
|
|
for _ in 0..loc_count {
|
|
|
|
|
let cell_pos = Vec2::new(
|
|
|
|
|
self.rng.gen::<usize>() % grid_size.x,
|
|
|
|
|
self.rng.gen::<usize>() % grid_size.y,
|
2019-06-18 21:22:31 +00:00
|
|
|
|
);
|
2019-06-25 15:59:09 +00:00
|
|
|
|
let wpos = (cell_pos * cell_size + cell_size / 2)
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| {
|
2019-06-25 15:59:09 +00:00
|
|
|
|
e as i32 * sz as i32 + sz as i32 / 2
|
2019-06-23 19:43:02 +00:00
|
|
|
|
});
|
2019-06-22 21:44:27 +00:00
|
|
|
|
|
2019-06-25 15:59:09 +00:00
|
|
|
|
locations.push(Location::generate(wpos, &mut rng));
|
|
|
|
|
|
|
|
|
|
loc_grid[cell_pos.y * grid_size.x + cell_pos.x] = Some(locations.len() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find neighbours
|
|
|
|
|
let mut loc_clone = locations
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|l| l.center)
|
|
|
|
|
.enumerate()
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
for i in 0..locations.len() {
|
|
|
|
|
let pos = locations[i].center;
|
|
|
|
|
|
|
|
|
|
loc_clone.sort_by_key(|(_, l)| l.distance_squared(pos));
|
|
|
|
|
|
2019-06-26 00:27:41 +00:00
|
|
|
|
loc_clone.iter().skip(1).take(2).for_each(|(j, _)| {
|
|
|
|
|
locations[i].neighbours.insert(*j);
|
|
|
|
|
locations[*j].neighbours.insert(i);
|
|
|
|
|
});
|
2019-06-22 21:44:27 +00:00
|
|
|
|
}
|
2019-06-18 21:22:31 +00:00
|
|
|
|
|
2019-06-22 21:44:27 +00:00
|
|
|
|
// Simulate invasion!
|
|
|
|
|
let invasion_cycles = 25;
|
|
|
|
|
for _ in 0..invasion_cycles {
|
|
|
|
|
for i in 0..grid_size.x {
|
|
|
|
|
for j in 0..grid_size.y {
|
2019-06-25 15:59:09 +00:00
|
|
|
|
if loc_grid[j * grid_size.x + i].is_none() {
|
2019-06-22 21:44:27 +00:00
|
|
|
|
const R_COORDS: [i32; 5] = [-1, 0, 1, 0, -1];
|
|
|
|
|
let idx = self.rng.gen::<usize>() % 4;
|
2019-06-23 19:43:02 +00:00
|
|
|
|
let loc = Vec2::new(i as i32 + R_COORDS[idx], j as i32 + R_COORDS[idx + 1])
|
|
|
|
|
.map(|e| e as usize);
|
2019-06-22 21:44:27 +00:00
|
|
|
|
|
2019-06-26 00:27:41 +00:00
|
|
|
|
loc_grid[j * grid_size.x + i] =
|
|
|
|
|
loc_grid.get(loc.y * grid_size.x + loc.x).cloned().flatten();
|
2019-06-22 21:44:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Place the locations onto the world
|
|
|
|
|
let gen = StructureGen2d::new(self.seed, cell_size as u32, cell_size as u32 / 2);
|
|
|
|
|
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);
|
2019-06-26 00:27:41 +00:00
|
|
|
|
let block_pos = Vec2::new(
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
chunk_pos.x * TerrainChunkSize::RECT_SIZE.x as i32,
|
|
|
|
|
chunk_pos.y * TerrainChunkSize::RECT_SIZE.y as i32,
|
2019-06-26 00:27:41 +00:00
|
|
|
|
);
|
2019-07-01 18:40:41 +00:00
|
|
|
|
let _cell_pos = Vec2::new(i / cell_size, j / cell_size);
|
2019-06-22 21:44:27 +00:00
|
|
|
|
|
|
|
|
|
// Find the distance to each region
|
|
|
|
|
let near = gen.get(chunk_pos);
|
|
|
|
|
let mut near = near
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(pos, seed)| RegionInfo {
|
|
|
|
|
chunk_pos: *pos,
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
block_pos: pos
|
|
|
|
|
.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e * sz as i32),
|
2019-06-22 21:44:27 +00:00
|
|
|
|
dist: (pos - chunk_pos).map(|e| e as f32).magnitude(),
|
|
|
|
|
seed: *seed,
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
// Sort regions based on distance
|
|
|
|
|
near.sort_by(|a, b| a.dist.partial_cmp(&b.dist).unwrap());
|
|
|
|
|
|
|
|
|
|
let nearest_cell_pos = near[0].chunk_pos.map(|e| e as usize) / cell_size;
|
2019-06-25 15:59:09 +00:00
|
|
|
|
self.get_mut(chunk_pos).unwrap().location = loc_grid
|
2019-06-22 21:44:27 +00:00
|
|
|
|
.get(nearest_cell_pos.y * grid_size.x + nearest_cell_pos.x)
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or(None)
|
2019-06-25 15:59:09 +00:00
|
|
|
|
.map(|loc_idx| LocationInfo { loc_idx, near });
|
|
|
|
|
|
|
|
|
|
let town_size = 200;
|
|
|
|
|
let in_town = self
|
|
|
|
|
.get(chunk_pos)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.location
|
|
|
|
|
.as_ref()
|
2019-06-26 00:27:41 +00:00
|
|
|
|
.map(|l| {
|
|
|
|
|
locations[l.loc_idx].center.distance_squared(block_pos)
|
|
|
|
|
< town_size * town_size
|
|
|
|
|
})
|
2019-06-25 15:59:09 +00:00
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
if in_town {
|
|
|
|
|
self.get_mut(chunk_pos).unwrap().spawn_rate = 0.0;
|
2019-06-18 21:22:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 13:23:42 +00:00
|
|
|
|
// Stage 2 - towns!
|
|
|
|
|
let mut maybe_towns = HashMap::new();
|
|
|
|
|
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);
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
let wpos = chunk_pos.map2(Vec2::from(TerrainChunkSize::RECT_SIZE), |e, sz: u32| {
|
2019-08-24 13:23:42 +00:00
|
|
|
|
e * sz as i32 + sz as i32 / 2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let near_towns = self.gen_ctx.town_gen.get(wpos);
|
|
|
|
|
let town = near_towns
|
|
|
|
|
.iter()
|
2019-09-04 23:03:49 +00:00
|
|
|
|
.min_by_key(|(pos, _seed)| wpos.distance_squared(*pos));
|
2019-08-24 13:23:42 +00:00
|
|
|
|
|
2019-08-28 18:32:44 +00:00
|
|
|
|
if let Some((pos, _)) = town {
|
2019-08-24 13:23:42 +00:00
|
|
|
|
let maybe_town = maybe_towns
|
|
|
|
|
.entry(*pos)
|
|
|
|
|
.or_insert_with(|| {
|
2019-08-28 18:32:44 +00:00
|
|
|
|
TownState::generate(*pos, &mut ColumnGen::new(self), &mut rng)
|
2019-08-24 22:57:55 +00:00
|
|
|
|
.map(|t| Arc::new(t))
|
2019-08-24 13:23:42 +00:00
|
|
|
|
})
|
|
|
|
|
.as_mut()
|
|
|
|
|
// Only care if we're close to the town
|
|
|
|
|
.filter(|town| {
|
2019-08-28 18:32:44 +00:00
|
|
|
|
Vec2::from(town.center()).distance_squared(wpos)
|
|
|
|
|
< town.radius().add(64).pow(2)
|
2019-08-24 13:23:42 +00:00
|
|
|
|
})
|
|
|
|
|
.cloned();
|
|
|
|
|
|
|
|
|
|
self.get_mut(chunk_pos).unwrap().structures.town = maybe_town;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
self.rng = rng;
|
2019-06-25 15:59:09 +00:00
|
|
|
|
self.locations = locations;
|
2019-05-18 08:59:58 +00:00
|
|
|
|
}
|
2019-05-16 17:40:32 +00:00
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
pub fn get(&self, chunk_pos: Vec2<i32>) -> Option<&SimChunk> {
|
2019-05-21 22:31:38 +00:00
|
|
|
|
if chunk_pos
|
2019-06-18 21:22:31 +00:00
|
|
|
|
.map2(WORLD_SIZE, |e, sz| e >= 0 && e < sz as i32)
|
2019-05-21 22:31:38 +00:00
|
|
|
|
.reduce_and()
|
|
|
|
|
{
|
2019-08-25 15:49:33 +00:00
|
|
|
|
Some(&self.chunks[vec2_as_uniform_idx(chunk_pos)])
|
2019-05-16 17:40:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
2019-08-24 13:23:42 +00:00
|
|
|
|
pub fn get_wpos(&self, wpos: Vec2<i32>) -> Option<&SimChunk> {
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
self.get(
|
|
|
|
|
wpos.map2(Vec2::from(TerrainChunkSize::RECT_SIZE), |e, sz: u32| {
|
|
|
|
|
e / sz as i32
|
|
|
|
|
}),
|
|
|
|
|
)
|
2019-08-24 13:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
|
pub fn get_mut(&mut self, chunk_pos: Vec2<i32>) -> Option<&mut SimChunk> {
|
|
|
|
|
if chunk_pos
|
|
|
|
|
.map2(WORLD_SIZE, |e, sz| e >= 0 && e < sz as i32)
|
|
|
|
|
.reduce_and()
|
|
|
|
|
{
|
2019-08-25 15:49:33 +00:00
|
|
|
|
Some(&mut self.chunks[vec2_as_uniform_idx(chunk_pos)])
|
2019-06-18 21:22:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_base_z(&self, chunk_pos: Vec2<i32>) -> Option<f32> {
|
2019-05-21 22:31:38 +00:00
|
|
|
|
self.get(chunk_pos).and_then(|_| {
|
|
|
|
|
(0..2)
|
|
|
|
|
.map(|i| (0..2).map(move |j| (i, j)))
|
2019-05-20 02:53:04 +00:00
|
|
|
|
.flatten()
|
2019-05-21 22:31:38 +00:00
|
|
|
|
.map(|(i, j)| {
|
|
|
|
|
self.get(chunk_pos + Vec2::new(i, j))
|
|
|
|
|
.map(|c| c.get_base_z())
|
|
|
|
|
})
|
2019-05-20 02:53:04 +00:00
|
|
|
|
.flatten()
|
2019-05-21 22:31:38 +00:00
|
|
|
|
.fold(None, |a: Option<f32>, x| a.map(|a| a.min(x)).or(Some(x)))
|
|
|
|
|
})
|
2019-05-20 02:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
|
pub fn get_interpolated<T, F>(&self, pos: Vec2<i32>, mut f: F) -> Option<T>
|
2019-05-21 22:31:38 +00:00
|
|
|
|
where
|
|
|
|
|
T: Copy + Default + Add<Output = T> + Mul<f32, Output = T>,
|
|
|
|
|
F: FnMut(&SimChunk) -> T,
|
2019-05-20 15:01:27 +00:00
|
|
|
|
{
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
let pos = pos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| {
|
2019-05-21 22:31:38 +00:00
|
|
|
|
e as f64 / sz as f64
|
|
|
|
|
});
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
|
let cubic = |a: T, b: T, c: T, d: T, x: f32| -> T {
|
2019-05-20 02:53:04 +00:00
|
|
|
|
let x2 = x * x;
|
|
|
|
|
|
|
|
|
|
// Catmull-Rom splines
|
2019-05-20 15:01:27 +00:00
|
|
|
|
let co0 = a * -0.5 + b * 1.5 + c * -1.5 + d * 0.5;
|
|
|
|
|
let co1 = a + b * -2.5 + c * 2.0 + d * -0.5;
|
|
|
|
|
let co2 = a * -0.5 + c * 0.5;
|
2019-05-20 02:53:04 +00:00
|
|
|
|
let co3 = b;
|
|
|
|
|
|
|
|
|
|
co0 * x2 * x + co1 * x2 + co2 * x + co3
|
2019-05-20 15:01:27 +00:00
|
|
|
|
};
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
2019-05-22 09:42:19 +00:00
|
|
|
|
let mut x = [T::default(); 4];
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
2019-05-22 09:42:19 +00:00
|
|
|
|
for (x_idx, j) in (-1..3).enumerate() {
|
2019-06-19 14:55:26 +00:00
|
|
|
|
let y0 = f(self.get(pos.map2(Vec2::new(j, -1), |e, q| e.max(0.0) as i32 + q))?);
|
2019-06-18 21:22:31 +00:00
|
|
|
|
let y1 = f(self.get(pos.map2(Vec2::new(j, 0), |e, q| e.max(0.0) as i32 + q))?);
|
|
|
|
|
let y2 = f(self.get(pos.map2(Vec2::new(j, 1), |e, q| e.max(0.0) as i32 + q))?);
|
|
|
|
|
let y3 = f(self.get(pos.map2(Vec2::new(j, 2), |e, q| e.max(0.0) as i32 + q))?);
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
2019-05-22 09:42:19 +00:00
|
|
|
|
x[x_idx] = cubic(y0, y1, y2, y3, pos.y.fract() as f32);
|
2019-05-20 02:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 09:42:19 +00:00
|
|
|
|
Some(cubic(x[0], x[1], x[2], x[3], pos.x.fract() as f32))
|
2019-05-20 02:53:04 +00:00
|
|
|
|
}
|
2019-05-21 00:57:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 17:19:40 +00:00
|
|
|
|
pub struct SimChunk {
|
|
|
|
|
pub chaos: f32,
|
|
|
|
|
pub alt_base: f32,
|
|
|
|
|
pub alt: f32,
|
|
|
|
|
pub temp: f32,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
pub humidity: f32,
|
2019-06-04 17:19:40 +00:00
|
|
|
|
pub rockiness: f32,
|
2019-07-08 14:51:38 +00:00
|
|
|
|
pub is_cliffs: bool,
|
2019-06-21 00:53:11 +00:00
|
|
|
|
pub near_cliffs: bool,
|
2019-06-04 17:19:40 +00:00
|
|
|
|
pub tree_density: f32,
|
2019-06-11 18:39:25 +00:00
|
|
|
|
pub forest_kind: ForestKind,
|
2019-06-25 15:59:09 +00:00
|
|
|
|
pub spawn_rate: f32,
|
2019-06-22 21:44:27 +00:00
|
|
|
|
pub location: Option<LocationInfo>,
|
2019-08-23 21:33:14 +00:00
|
|
|
|
|
|
|
|
|
pub structures: Structures,
|
2019-06-22 21:44:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub struct RegionInfo {
|
|
|
|
|
pub chunk_pos: Vec2<i32>,
|
|
|
|
|
pub block_pos: Vec2<i32>,
|
|
|
|
|
pub dist: f32,
|
|
|
|
|
pub seed: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct LocationInfo {
|
2019-06-25 15:59:09 +00:00
|
|
|
|
pub loc_idx: usize,
|
2019-06-22 21:44:27 +00:00
|
|
|
|
pub near: Vec<RegionInfo>,
|
2019-06-04 17:19:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 21:33:14 +00:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Structures {
|
|
|
|
|
pub town: Option<Arc<TownState>>,
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 17:19:40 +00:00
|
|
|
|
impl SimChunk {
|
2019-08-21 18:41:32 +00:00
|
|
|
|
fn generate(posi: usize, gen_ctx: &mut GenCtx, gen_cdf: &GenCdf) -> Self {
|
|
|
|
|
let pos = uniform_idx_as_vec2(posi);
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
|
let wposf = (pos * TerrainChunkSize::RECT_SIZE.map(|e| e as i32)).map(|e| e as f64);
|
2019-06-04 17:19:40 +00:00
|
|
|
|
|
2019-08-21 18:41:32 +00:00
|
|
|
|
let (_, alt_base) = gen_cdf.alt_base[posi];
|
|
|
|
|
let map_edge_factor = map_edge_factor(posi);
|
2019-08-22 19:03:42 +00:00
|
|
|
|
let (_, chaos) = gen_cdf.chaos[posi];
|
2019-08-22 22:57:42 +00:00
|
|
|
|
let (humid_uniform, _) = gen_cdf.humid_base[posi];
|
2019-08-26 09:49:14 +00:00
|
|
|
|
let (_, alt_pre) = gen_cdf.alt[posi];
|
|
|
|
|
let (alt_uniform, _) = gen_cdf.alt_no_seawater[posi];
|
2019-08-22 22:57:42 +00:00
|
|
|
|
let (temp_uniform, _) = gen_cdf.temp_base[posi];
|
2019-08-19 17:20:54 +00:00
|
|
|
|
|
2019-08-20 20:48:22 +00:00
|
|
|
|
// Take the weighted average of our randomly generated base humidity, the scaled
|
|
|
|
|
// negative altitude, and other random variable (to add some noise) to yield the
|
2019-08-21 18:41:32 +00:00
|
|
|
|
// final humidity. Note that we are using the "old" version of chaos here.
|
2019-08-22 21:25:17 +00:00
|
|
|
|
const HUMID_WEIGHTS: [f32; 2] = [1.0, 1.0];
|
2019-08-22 22:57:42 +00:00
|
|
|
|
let humidity = cdf_irwin_hall(&HUMID_WEIGHTS, [humid_uniform, 1.0 - alt_uniform]);
|
2019-08-21 18:41:32 +00:00
|
|
|
|
|
|
|
|
|
// We also correlate temperature negatively with altitude using different weighting than we
|
|
|
|
|
// use for humidity.
|
|
|
|
|
const TEMP_WEIGHTS: [f32; 2] = [2.0, 1.0];
|
2019-08-22 22:57:42 +00:00
|
|
|
|
let temp = cdf_irwin_hall(&TEMP_WEIGHTS, [temp_uniform, 1.0 - alt_uniform])
|
2019-08-21 18:41:32 +00:00
|
|
|
|
// Convert to [-1, 1]
|
|
|
|
|
.sub(0.5)
|
|
|
|
|
.mul(2.0);
|
2019-08-19 17:20:54 +00:00
|
|
|
|
|
|
|
|
|
let alt_base = alt_base.mul(CONFIG.mountain_scale);
|
2019-08-22 21:25:17 +00:00
|
|
|
|
let alt = CONFIG
|
2019-08-22 23:31:27 +00:00
|
|
|
|
.sea_level
|
|
|
|
|
.mul(map_edge_factor)
|
2019-08-22 22:57:42 +00:00
|
|
|
|
.add(alt_pre.mul(CONFIG.mountain_scale));
|
2019-08-19 17:20:54 +00:00
|
|
|
|
|
2019-06-21 00:53:11 +00:00
|
|
|
|
let cliff = gen_ctx.cliff_nz.get((wposf.div(2048.0)).into_array()) as f32 + chaos * 0.2;
|
|
|
|
|
|
2019-08-22 15:45:47 +00:00
|
|
|
|
// Logistic regression. Make sure x ∈ (0, 1).
|
2019-08-22 21:25:17 +00:00
|
|
|
|
let logit = |x: f32| x.ln() - x.neg().ln_1p();
|
2019-08-22 15:45:47 +00:00
|
|
|
|
// 0.5 + 0.5 * tanh(ln(1 / (1 - 0.1) - 1) / (2 * (sqrt(3)/pi)))
|
|
|
|
|
let logistic_2_base = 3.0f32.sqrt().mul(f32::consts::FRAC_2_PI);
|
|
|
|
|
// Assumes μ = 0, σ = 1
|
|
|
|
|
let logistic_cdf = |x: f32| x.div(logistic_2_base).tanh().mul(0.5).add(0.5);
|
|
|
|
|
|
|
|
|
|
// No trees in the ocean or with zero humidity (currently)
|
2019-08-22 21:25:17 +00:00
|
|
|
|
let tree_density = if alt <= CONFIG.sea_level + 5.0 {
|
|
|
|
|
0.0
|
|
|
|
|
} else {
|
2019-08-22 15:45:47 +00:00
|
|
|
|
let tree_density = (gen_ctx.tree_nz.get((wposf.div(1024.0)).into_array()) as f32)
|
2019-08-18 23:52:26 +00:00
|
|
|
|
.mul(1.5)
|
|
|
|
|
.add(1.0)
|
|
|
|
|
.mul(0.5)
|
|
|
|
|
.mul(1.2 - chaos * 0.95)
|
|
|
|
|
.add(0.05)
|
|
|
|
|
.max(0.0)
|
2019-08-22 15:45:47 +00:00
|
|
|
|
.min(1.0);
|
|
|
|
|
// Tree density should go (by a lot) with humidity.
|
|
|
|
|
if humidity <= 0.0 || tree_density <= 0.0 {
|
|
|
|
|
0.0
|
|
|
|
|
} else if humidity >= 1.0 || tree_density >= 1.0 {
|
|
|
|
|
1.0
|
|
|
|
|
} else {
|
2019-08-22 21:48:13 +00:00
|
|
|
|
// Weighted logit sum.
|
2019-08-22 15:45:47 +00:00
|
|
|
|
logistic_cdf(logit(humidity) + 0.5 * logit(tree_density))
|
|
|
|
|
}
|
2019-08-25 15:49:33 +00:00
|
|
|
|
// rescale to (-0.95, 0.95)
|
2019-08-22 21:25:17 +00:00
|
|
|
|
.sub(0.5)
|
2019-08-25 15:49:33 +00:00
|
|
|
|
.mul(0.95)
|
2019-08-22 21:25:17 +00:00
|
|
|
|
.add(0.5)
|
2019-08-22 15:45:47 +00:00
|
|
|
|
};
|
2019-08-18 23:52:26 +00:00
|
|
|
|
|
2019-06-04 17:19:40 +00:00
|
|
|
|
Self {
|
|
|
|
|
chaos,
|
|
|
|
|
alt_base,
|
|
|
|
|
alt,
|
2019-06-11 18:39:25 +00:00
|
|
|
|
temp,
|
2019-08-18 16:35:27 +00:00
|
|
|
|
humidity,
|
2019-06-04 17:19:40 +00:00
|
|
|
|
rockiness: (gen_ctx.rock_nz.get((wposf.div(1024.0)).into_array()) as f32)
|
|
|
|
|
.sub(0.1)
|
2019-06-11 18:39:25 +00:00
|
|
|
|
.mul(1.3)
|
2019-06-04 17:19:40 +00:00
|
|
|
|
.max(0.0),
|
2019-08-23 15:41:02 +00:00
|
|
|
|
is_cliffs: cliff > 0.5 && alt > CONFIG.sea_level + 5.0,
|
2019-08-24 13:23:42 +00:00
|
|
|
|
near_cliffs: cliff > 0.2,
|
2019-08-18 23:52:26 +00:00
|
|
|
|
tree_density,
|
2019-06-11 18:39:25 +00:00
|
|
|
|
forest_kind: if temp > 0.0 {
|
|
|
|
|
if temp > CONFIG.desert_temp {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
if humidity > CONFIG.jungle_hum {
|
|
|
|
|
// Forests in desert temperatures with extremely high humidity
|
|
|
|
|
// should probably be different from palm trees, but we use them
|
|
|
|
|
// for now.
|
|
|
|
|
ForestKind::Palm
|
2019-08-22 21:25:17 +00:00
|
|
|
|
} else if humidity > CONFIG.forest_hum {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
ForestKind::Palm
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else if humidity > CONFIG.desert_hum {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
// Low but not desert humidity, so we should really have some other
|
|
|
|
|
// terrain...
|
|
|
|
|
ForestKind::Savannah
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
ForestKind::Savannah
|
2019-08-18 16:35:27 +00:00
|
|
|
|
}
|
2019-07-08 16:41:20 +00:00
|
|
|
|
} else if temp > CONFIG.tropical_temp {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
if humidity > CONFIG.jungle_hum {
|
|
|
|
|
ForestKind::Mangrove
|
|
|
|
|
} else if humidity > CONFIG.forest_hum {
|
2019-08-19 17:20:54 +00:00
|
|
|
|
// NOTE: Probably the wrong kind of tree for this climate.
|
2019-08-18 16:35:27 +00:00
|
|
|
|
ForestKind::Oak
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else if humidity > CONFIG.desert_hum {
|
2019-08-19 17:20:54 +00:00
|
|
|
|
// Low but not desert... need something besides savannah.
|
2019-08-18 16:35:27 +00:00
|
|
|
|
ForestKind::Savannah
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
ForestKind::Savannah
|
2019-08-18 16:35:27 +00:00
|
|
|
|
}
|
2019-06-11 18:39:25 +00:00
|
|
|
|
} else {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
if humidity > CONFIG.jungle_hum {
|
|
|
|
|
// Temperate climate with jungle humidity...
|
|
|
|
|
// https://en.wikipedia.org/wiki/Humid_subtropical_climates are often
|
|
|
|
|
// densely wooded and full of water. Semitropical rainforests, basically.
|
2019-08-19 17:20:54 +00:00
|
|
|
|
// For now we just treet them like other rainforests.
|
2019-08-21 18:41:32 +00:00
|
|
|
|
ForestKind::Oak
|
2019-08-18 16:35:27 +00:00
|
|
|
|
} else if humidity > CONFIG.forest_hum {
|
|
|
|
|
// Moderate climate, moderate humidity.
|
|
|
|
|
ForestKind::Oak
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else if humidity > CONFIG.desert_hum {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
// With moderate temperature and low humidity, we should probably see
|
|
|
|
|
// something different from savannah, but oh well...
|
|
|
|
|
ForestKind::Savannah
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
ForestKind::Savannah
|
2019-08-18 16:35:27 +00:00
|
|
|
|
}
|
2019-06-11 18:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-08-18 16:35:27 +00:00
|
|
|
|
// For now we don't take humidity into account for cold climates (but we really
|
2019-08-19 01:01:11 +00:00
|
|
|
|
// should!) except that we make sure we only have snow pines when there is snow.
|
2019-08-19 17:20:54 +00:00
|
|
|
|
if temp <= CONFIG.snow_temp && humidity > CONFIG.forest_hum {
|
2019-06-11 18:39:25 +00:00
|
|
|
|
ForestKind::SnowPine
|
2019-08-22 15:45:47 +00:00
|
|
|
|
} else if humidity > CONFIG.desert_hum {
|
|
|
|
|
ForestKind::Pine
|
2019-08-19 01:01:11 +00:00
|
|
|
|
} else {
|
2019-08-22 15:45:47 +00:00
|
|
|
|
// Should really have something like tundra.
|
2019-08-19 01:01:11 +00:00
|
|
|
|
ForestKind::Pine
|
2019-06-11 18:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-06-25 15:59:09 +00:00
|
|
|
|
spawn_rate: 1.0,
|
2019-06-10 16:28:02 +00:00
|
|
|
|
location: None,
|
2019-08-23 21:33:14 +00:00
|
|
|
|
|
|
|
|
|
structures: Structures { town: None },
|
2019-06-04 17:19:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_base_z(&self) -> f32 {
|
2019-07-08 16:00:50 +00:00
|
|
|
|
self.alt - self.chaos * 50.0 - 16.0
|
2019-06-04 17:19:40 +00:00
|
|
|
|
}
|
2019-06-18 21:22:31 +00:00
|
|
|
|
|
2019-06-25 15:59:09 +00:00
|
|
|
|
pub fn get_name(&self, world: &WorldSim) -> Option<String> {
|
|
|
|
|
if let Some(loc) = &self.location {
|
2019-06-26 00:27:41 +00:00
|
|
|
|
Some(world.locations[loc.loc_idx].name().to_string())
|
2019-06-25 15:59:09 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2019-06-18 21:22:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_biome(&self) -> BiomeKind {
|
|
|
|
|
if self.alt < CONFIG.sea_level {
|
|
|
|
|
BiomeKind::Ocean
|
|
|
|
|
} else if self.chaos > 0.6 {
|
|
|
|
|
BiomeKind::Mountain
|
|
|
|
|
} else if self.temp > CONFIG.desert_temp {
|
|
|
|
|
BiomeKind::Desert
|
|
|
|
|
} else if self.temp < CONFIG.snow_temp {
|
|
|
|
|
BiomeKind::Snowlands
|
|
|
|
|
} else if self.tree_density > 0.65 {
|
|
|
|
|
BiomeKind::Forest
|
|
|
|
|
} else {
|
|
|
|
|
BiomeKind::Grassland
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-04 17:19:40 +00:00
|
|
|
|
}
|