1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
pub mod fast_noise;
pub mod gen_cache;
pub mod map_array;
pub mod map_vec;
pub mod math;
pub mod random;
pub mod sampler;
pub mod seed_expan;
pub mod small_cache;
pub mod structure;
pub mod unit_chooser;

// Reexports
pub use self::{
    fast_noise::{FastNoise, FastNoise2d},
    map_vec::MapVec,
    random::{RandomField, RandomPerm},
    sampler::{Sampler, SamplerMut},
    small_cache::SmallCache,
    structure::StructureGen2d,
    unit_chooser::UnitChooser,
};

pub use common::grid::Grid;

use fxhash::{FxHasher32, FxHasher64};
use hashbrown::{HashMap, HashSet};
use std::hash::BuildHasherDefault;
use vek::*;

// Deterministic HashMap and HashSet
pub type DHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher64>>;
pub type DHashSet<T> = HashSet<T, BuildHasherDefault<FxHasher32>>;

pub fn attempt<T>(max_iters: usize, mut f: impl FnMut() -> Option<T>) -> Option<T> {
    (0..max_iters).find_map(|_| f())
}

pub fn close(x: f32, tgt: f32, falloff: f32) -> f32 {
    (1.0 - (x - tgt).abs() / falloff).max(0.0).powf(0.125)
}

pub fn close_fast(x: f32, tgt: f32, falloff: f32, falloff_strength: i32) -> f32 {
    (1.0 - ((x - tgt) / falloff).powi(falloff_strength * 2)).max(0.0)
}

pub const CARDINALS: [Vec2<i32>; 4] = [
    Vec2::new(0, 1),
    Vec2::new(1, 0),
    Vec2::new(0, -1),
    Vec2::new(-1, 0),
];

pub const DIRS: [Vec2<i32>; 8] = [
    Vec2::new(1, 0),
    Vec2::new(1, 1),
    Vec2::new(0, 1),
    Vec2::new(-1, 1),
    Vec2::new(-1, 0),
    Vec2::new(-1, -1),
    Vec2::new(0, -1),
    Vec2::new(1, -1),
];

pub const DIAGONALS: [Vec2<i32>; 4] = [
    Vec2::new(-1, -1),
    Vec2::new(1, -1),
    Vec2::new(-1, 1),
    Vec2::new(1, 1),
];

pub const NEIGHBORS: [Vec2<i32>; 8] = [
    Vec2::new(1, 0),
    Vec2::new(1, 1),
    Vec2::new(0, 1),
    Vec2::new(-1, 1),
    Vec2::new(-1, 0),
    Vec2::new(-1, -1),
    Vec2::new(0, -1),
    Vec2::new(1, -1),
];

pub const NEIGHBORS3: [Vec3<i32>; 26] = [
    Vec3::new(0, 0, -1),
    Vec3::new(0, 0, 1),
    Vec3::new(0, -1, 0),
    Vec3::new(0, -1, -1),
    Vec3::new(0, -1, 1),
    Vec3::new(0, 1, 0),
    Vec3::new(0, 1, -1),
    Vec3::new(0, 1, 1),
    Vec3::new(-1, 0, 0),
    Vec3::new(-1, 0, -1),
    Vec3::new(-1, 0, 1),
    Vec3::new(-1, -1, 0),
    Vec3::new(-1, -1, -1),
    Vec3::new(-1, -1, 1),
    Vec3::new(-1, 1, 0),
    Vec3::new(-1, 1, -1),
    Vec3::new(-1, 1, 1),
    Vec3::new(1, 0, 0),
    Vec3::new(1, 0, -1),
    Vec3::new(1, 0, 1),
    Vec3::new(1, -1, 0),
    Vec3::new(1, -1, -1),
    Vec3::new(1, -1, 1),
    Vec3::new(1, 1, 0),
    Vec3::new(1, 1, -1),
    Vec3::new(1, 1, 1),
];

pub const LOCALITY: [Vec2<i32>; 9] = [
    Vec2::new(0, 0),
    Vec2::new(0, 1),
    Vec2::new(1, 0),
    Vec2::new(0, -1),
    Vec2::new(-1, 0),
    Vec2::new(1, 1),
    Vec2::new(1, -1),
    Vec2::new(-1, 1),
    Vec2::new(-1, -1),
];

pub const CARDINAL_LOCALITY: [Vec2<i32>; 5] = [
    Vec2::new(0, 0),
    Vec2::new(0, 1),
    Vec2::new(1, 0),
    Vec2::new(0, -1),
    Vec2::new(-1, 0),
];

pub const SQUARE_4: [Vec2<i32>; 4] = [
    Vec2::new(0, 0),
    Vec2::new(1, 0),
    Vec2::new(0, 1),
    Vec2::new(1, 1),
];

pub const SQUARE_9: [Vec2<i32>; 9] = [
    Vec2::new(-1, -1),
    Vec2::new(0, -1),
    Vec2::new(1, -1),
    Vec2::new(-1, 0),
    Vec2::new(0, 0),
    Vec2::new(1, 0),
    Vec2::new(-1, 1),
    Vec2::new(0, 1),
    Vec2::new(1, 1),
];