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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
pub mod herringbone;
pub mod tileset_loader;
use std::ops::{Add, Mul, Sub};
use glam::IVec2;
use crate::{
config::{CHUNK_SIZE, TILESET_SCALING},
proc_gen::{
herringbone::{split_position, variant_index},
tileset_loader::{Tile, TileOrientation, TilePixelType, Tileset, load_tileset},
},
sim::{cell::Cell, cell_manager::chunk::Chunk, entity::EntityDef},
};
#[inline]
fn lerp<T>(a: T, b: T, t: f32) -> T
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>,
{
a + (b - a) * t
}
const CHUNK_PIXELS: i32 = CHUNK_SIZE / TILESET_SCALING;
const PIXELS_NEIGHBOURHOOD_SIZE: i32 = CHUNK_PIXELS + 2;
// 2d slice of solidity; chunk + 1 margin
struct BiomeChunkContext {
pixels: [TilePixelType; (PIXELS_NEIGHBOURHOOD_SIZE * PIXELS_NEIGHBOURHOOD_SIZE) as usize],
}
pub struct InterpolatedPixel {
pub interpolated_pixel_indices: [(u8, f32); 4],
pub interpolated_solidity: f32,
}
impl BiomeChunkContext {
fn interpolated_at(&self, local: IVec2, tileset: &Tileset) -> InterpolatedPixel {
// pixel coord centre of cell coord local
let pixel_position = (local.as_vec2() + 0.5) / TILESET_SCALING as f32 + 0.5;
let pixel = pixel_position.floor();
// distance from the current pixel to the centre of the cell
let frac = pixel_position - pixel;
let pixel = pixel.as_ivec2();
let instantaneous_pixel_at = |dx, dy| {
self.pixels[((pixel.x + dx) + (pixel.y + dy) * PIXELS_NEIGHBOURHOOD_SIZE) as usize]
};
let instantaneous_idx_at = |dx, dy| match instantaneous_pixel_at(dx, dy) {
TilePixelType::Void => 0,
TilePixelType::Terrain(_) => 1,
TilePixelType::Custom(i) => i,
};
let instantaneous_solidity_at = |dx, dy| match instantaneous_pixel_at(dx, dy) {
TilePixelType::Void => 0.0,
TilePixelType::Terrain(o) => o,
TilePixelType::Custom(i) => {
tileset.manifest.palette.get(&i).map_or(0.0, |p| p.solidity)
}
};
// linearly interpolate between the solidity of the horizontal cells above and below according to the fractional x component
let s_top = lerp(
instantaneous_solidity_at(0, 0),
instantaneous_solidity_at(1, 0),
frac.x,
);
let s_bottom = lerp(
instantaneous_solidity_at(0, 1),
instantaneous_solidity_at(1, 1),
frac.x,
);
// and then interpolate between those according to the y component
let interpolated_solidity = lerp(s_top, s_bottom, frac.y) - 0.5;
// same for the pixel
// TODO don't allocate per-cell
let mut pixel_indices: Vec<u8> = Vec::new();
let tl = instantaneous_idx_at(0, 0);
if tl != 0 && !pixel_indices.contains(&tl) {
pixel_indices.push(tl);
}
let tr = instantaneous_idx_at(1, 0);
if tr != 0 && !pixel_indices.contains(&tr) {
pixel_indices.push(tr);
}
let bl = instantaneous_idx_at(0, 1);
if bl != 0 && !pixel_indices.contains(&bl) {
pixel_indices.push(bl);
}
let br = instantaneous_idx_at(1, 1);
if br != 0 && !pixel_indices.contains(&br) {
pixel_indices.push(br);
}
let mut interpolated_pixel_indices: [(u8, f32); 4] = [(0, 0.0); 4];
for (i, &p) in pixel_indices.iter().enumerate() {
let i_top = lerp(
if tl == p { 1.0 } else { 0.0 },
if tr == p { 1.0 } else { 0.0 },
frac.x,
);
let i_bottom = lerp(
if bl == p { 1.0 } else { 0.0 },
if br == p { 1.0 } else { 0.0 },
frac.x,
);
let interpolated_strength = lerp(i_top, i_bottom, frac.y);
interpolated_pixel_indices[i] = (p, interpolated_strength);
}
InterpolatedPixel {
interpolated_pixel_indices,
interpolated_solidity,
}
}
}
pub trait Biome {
fn fragment(&self, pixel: InterpolatedPixel, world: IVec2) -> Cell;
fn derive_entities_from_tile(&self, _tile: &Tile) -> Vec<EntityDef> {
Vec::new()
}
}
pub struct TilesetWorldGenerator {
tileset: Tileset,
biome: Box<dyn Biome>,
}
impl TilesetWorldGenerator {
pub fn new(tileset_path: &str, biome: Box<dyn Biome>) -> Self {
TilesetWorldGenerator {
tileset: load_tileset(tileset_path),
biome,
}
}
fn pixel_at(&self, pixel: IVec2) -> TilePixelType {
let (grid, local, orientation) =
split_position(pixel, self.tileset.manifest.dimensions.short as i32);
let tiles = match orientation {
TileOrientation::Horizontal => &self.tileset.horizontal_tiles,
TileOrientation::Vertical => &self.tileset.vertical_tiles,
};
tiles[variant_index(grid, tiles.len())].pixel_at(local)
}
fn get_biome_chunk_context(&self, chunk_position: IVec2) -> BiomeChunkContext {
let origin = chunk_position * CHUNK_PIXELS - 1;
let mut pixels =
[TilePixelType::Void; (PIXELS_NEIGHBOURHOOD_SIZE * PIXELS_NEIGHBOURHOOD_SIZE) as usize];
for y in 0..PIXELS_NEIGHBOURHOOD_SIZE {
for x in 0..PIXELS_NEIGHBOURHOOD_SIZE {
pixels[(x + y * PIXELS_NEIGHBOURHOOD_SIZE) as usize] =
self.pixel_at(origin + IVec2::new(x, y));
}
}
BiomeChunkContext { pixels }
}
pub fn generate_chunk(&self, chunk_position: IVec2) -> Chunk {
let mut chunk = Chunk::void();
let biome_chunk_context = self.get_biome_chunk_context(chunk_position);
let chunk_min = chunk_position * CHUNK_SIZE;
for y in 0..CHUNK_SIZE {
for x in 0..CHUNK_SIZE {
let local = IVec2::new(x, y);
chunk.cells[(x + y * CHUNK_SIZE) as usize] = self.biome.fragment(
biome_chunk_context.interpolated_at(local, &self.tileset),
chunk_min + local,
);
}
}
chunk.sleeping = false;
chunk.mark_collider_dirty(0);
chunk
}
}
|