veloren_world/util/
mod.rs

1pub mod fast_noise;
2pub mod gen_cache;
3pub mod map_array;
4pub mod map_vec;
5pub mod math;
6pub mod random;
7pub mod sampler;
8pub mod seed_expan;
9pub mod small_cache;
10pub mod structure;
11pub mod unit_chooser;
12
13// Reexports
14pub use self::{
15    fast_noise::{FastNoise, FastNoise2d},
16    map_vec::MapVec,
17    random::{RandomField, RandomPerm},
18    sampler::{Sampler, SamplerMut},
19    small_cache::SmallCache,
20    structure::StructureGen2d,
21    unit_chooser::UnitChooser,
22};
23
24pub use common::grid::Grid;
25
26use fxhash::{FxHasher32, FxHasher64};
27use hashbrown::{HashMap, HashSet};
28use std::hash::BuildHasherDefault;
29use vek::*;
30
31// Deterministic HashMap and HashSet
32pub type DHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher64>>;
33pub type DHashSet<T> = HashSet<T, BuildHasherDefault<FxHasher32>>;
34
35pub fn attempt<T>(max_iters: usize, mut f: impl FnMut() -> Option<T>) -> Option<T> {
36    (0..max_iters).find_map(|_| f())
37}
38
39pub fn close(x: f32, tgt: f32, falloff: f32) -> f32 {
40    (1.0 - (x - tgt).abs() / falloff).max(0.0).powf(0.125)
41}
42
43pub fn close_fast(x: f32, tgt: f32, falloff: f32, falloff_strength: i32) -> f32 {
44    (1.0 - ((x - tgt) / falloff).powi(falloff_strength * 2)).max(0.0)
45}
46
47pub const CARDINALS: [Vec2<i32>; 4] = [
48    Vec2::new(1, 0),
49    Vec2::new(0, 1),
50    Vec2::new(-1, 0),
51    Vec2::new(0, -1),
52];
53
54pub const DIRS: [Vec2<i32>; 8] = [
55    Vec2::new(1, 0),
56    Vec2::new(1, 1),
57    Vec2::new(0, 1),
58    Vec2::new(-1, 1),
59    Vec2::new(-1, 0),
60    Vec2::new(-1, -1),
61    Vec2::new(0, -1),
62    Vec2::new(1, -1),
63];
64
65pub const DIAGONALS: [Vec2<i32>; 4] = [
66    Vec2::new(1, 1),
67    Vec2::new(-1, 1),
68    Vec2::new(-1, -1),
69    Vec2::new(1, -1),
70];
71
72pub const NEIGHBORS: [Vec2<i32>; 8] = [
73    Vec2::new(1, 0),
74    Vec2::new(1, 1),
75    Vec2::new(0, 1),
76    Vec2::new(-1, 1),
77    Vec2::new(-1, 0),
78    Vec2::new(-1, -1),
79    Vec2::new(0, -1),
80    Vec2::new(1, -1),
81];
82
83pub const NEIGHBORS3: [Vec3<i32>; 26] = [
84    Vec3::new(0, 0, -1),
85    Vec3::new(0, 0, 1),
86    Vec3::new(0, -1, 0),
87    Vec3::new(0, -1, -1),
88    Vec3::new(0, -1, 1),
89    Vec3::new(0, 1, 0),
90    Vec3::new(0, 1, -1),
91    Vec3::new(0, 1, 1),
92    Vec3::new(-1, 0, 0),
93    Vec3::new(-1, 0, -1),
94    Vec3::new(-1, 0, 1),
95    Vec3::new(-1, -1, 0),
96    Vec3::new(-1, -1, -1),
97    Vec3::new(-1, -1, 1),
98    Vec3::new(-1, 1, 0),
99    Vec3::new(-1, 1, -1),
100    Vec3::new(-1, 1, 1),
101    Vec3::new(1, 0, 0),
102    Vec3::new(1, 0, -1),
103    Vec3::new(1, 0, 1),
104    Vec3::new(1, -1, 0),
105    Vec3::new(1, -1, -1),
106    Vec3::new(1, -1, 1),
107    Vec3::new(1, 1, 0),
108    Vec3::new(1, 1, -1),
109    Vec3::new(1, 1, 1),
110];
111
112pub const LOCALITY: [Vec2<i32>; 9] = [
113    Vec2::new(0, 0),
114    Vec2::new(0, 1),
115    Vec2::new(1, 0),
116    Vec2::new(0, -1),
117    Vec2::new(-1, 0),
118    Vec2::new(1, 1),
119    Vec2::new(1, -1),
120    Vec2::new(-1, 1),
121    Vec2::new(-1, -1),
122];
123
124pub const CARDINAL_LOCALITY: [Vec2<i32>; 5] = [
125    Vec2::new(0, 0),
126    Vec2::new(0, 1),
127    Vec2::new(1, 0),
128    Vec2::new(0, -1),
129    Vec2::new(-1, 0),
130];
131
132pub const SQUARE_4: [Vec2<i32>; 4] = [
133    Vec2::new(0, 0),
134    Vec2::new(1, 0),
135    Vec2::new(0, 1),
136    Vec2::new(1, 1),
137];
138
139pub const SQUARE_9: [Vec2<i32>; 9] = [
140    Vec2::new(-1, -1),
141    Vec2::new(0, -1),
142    Vec2::new(1, -1),
143    Vec2::new(-1, 0),
144    Vec2::new(0, 0),
145    Vec2::new(1, 0),
146    Vec2::new(-1, 1),
147    Vec2::new(0, 1),
148    Vec2::new(1, 1),
149];