1use crate::nature::seeding::Cells;
8use crate::terrain::{CellType, HeightGrid};
9
10pub const ROTATION_STEP: f32 = 90.0;
13pub const HEIGHT_TOLERANCE: f32 = 1000.0;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
19pub struct Rect {
20 pub x0: i32,
22 pub y0: i32,
24 pub x1: i32,
26 pub y1: i32,
28}
29
30impl Rect {
31 pub fn width(&self) -> i32 {
33 self.x1 - self.x0 + 1
34 }
35 pub fn height(&self) -> i32 {
37 self.y1 - self.y0 + 1
38 }
39}
40
41pub fn footprint(
47 map_size: i32,
48 cell: (i32, i32),
49 width: i32,
50 height: i32,
51 rotation: i32,
52) -> Option<Rect> {
53 let (w, h) = if rotation == 1 || rotation == 3 {
54 (height, width)
55 } else {
56 (width, height)
57 };
58 let span = |c: i32, s: i32| {
59 if s & 1 == 0 {
60 (c - (s >> 1) + 1, (s >> 1) + c)
61 } else {
62 let half = (s - 1) >> 1;
63 (c - half, half + c)
64 }
65 };
66 let (x0, x1) = span(cell.0, w);
67 let (y0, y1) = span(cell.1, h);
68 let n = map_size;
69 if x0 < 0 || n <= x0 || y0 < 0 || n <= y0 || x0 < 1 || n <= x1 || y1 < 0 || n <= y1 {
70 return None;
71 }
72 Some(Rect { x0, y0, x1, y1 })
73}
74
75pub fn footprint_index(i: i32, j: i32, w: i32, h: i32, degrees: i32) -> usize {
80 (match degrees {
81 90 => i * (h + 1) + j,
82 180 => j * (w + 1) + (w - 1 - i),
83 270 => (w - 1 - i) * (h + 1) + (h - 1 - j),
84 _ => (h - 1 - j) * (w + 1) + i,
85 }) as usize
86}
87
88#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
90pub struct PlacementRules {
91 pub mask: u32,
94 pub masks: Vec<u32>,
97 pub mask_chars: String,
99 pub footprint: String,
102 pub width: i32,
105 pub height: i32,
107 pub cell_type: u8,
110}
111
112#[allow(clippy::too_many_arguments)]
119pub fn cell_admits(
120 cells: &Cells,
121 terrain: &HeightGrid,
122 x: i32,
123 y: i32,
124 mask: u32,
125 min_normal: f32,
126 tolerance: f32,
127 reference: f32,
128 override_occupant: bool,
129 override_flag: bool,
130 owner_ok: impl Fn(usize) -> bool,
131) -> bool {
132 let n = cells.size as i32;
133 if x < 0 || n <= x || y < 0 || n <= y {
134 return false;
135 }
136 let i = (n * y + x) as usize;
137 if !mask & (1u32 << (cells.types[i].0 & 0x1f)) != 0 {
138 return false;
139 }
140 let occupant = cells.occupants[i];
141 if !(occupant.is_none_or(&owner_ok) || override_occupant) {
142 return false;
143 }
144 if !(override_flag || cells.flags[i] == 0) {
145 return false;
146 }
147 let half = (n >> 1) as f32 - 0.5;
148 let (px, py) = ((i as i32 % n) as f32 - half, (i as i32 / n) as f32 - half);
149 let mut normal = [0.0f32; 3];
150 let h = cells
151 .floors
152 .ground_normal(cells.size, terrain, px, py, &mut normal);
153 #[allow(clippy::neg_cmp_op_on_partial_ord)]
155 let flat = !(normal[2] < min_normal);
156 #[allow(clippy::neg_cmp_op_on_partial_ord)]
158 let level = !(tolerance < (h - reference).abs());
159 flat && level
160}
161
162pub fn can_place(
167 cells: &Cells,
168 terrain: &HeightGrid,
169 rules: &PlacementRules,
170 rect: Rect,
171 degrees: i32,
172 owner_ok: impl Fn(usize) -> bool,
173) -> bool {
174 (rect.y0..=rect.y1).all(|y| {
175 (rect.x0..=rect.x1)
176 .all(|x| footprint_cell(cells, terrain, rules, rect, degrees, x, y, &owner_ok).0)
177 })
178}
179
180#[allow(clippy::too_many_arguments)]
188pub fn footprint_cell(
189 cells: &Cells,
190 terrain: &HeightGrid,
191 rules: &PlacementRules,
192 rect: Rect,
193 degrees: i32,
194 x: i32,
195 y: i32,
196 owner_ok: impl Fn(usize) -> bool,
197) -> (bool, bool) {
198 let (w, h) = (rect.width(), rect.height());
199 let k = footprint_index(x - rect.x0, y - rect.y0, w, h, degrees);
200 let untouched = rules.footprint.as_bytes().get(k) == Some(&b'.');
201 let mut mask = rules.mask;
202 if !rules.masks.is_empty() {
203 if let Some(&c) = rules.mask_chars.as_bytes().get(k) {
204 if let Some(&m) = rules.masks.get((c as i32 - 0x30) as usize) {
205 mask = m;
206 }
207 }
208 }
209 let admits = cell_admits(
210 cells,
211 terrain,
212 x,
213 y,
214 mask,
215 0.0,
216 HEIGHT_TOLERANCE,
217 0.0,
218 false,
219 untouched,
220 owner_ok,
221 );
222 (admits, untouched)
223}
224
225pub fn stamp(cells: &mut Cells, rules: &PlacementRules, rect: Rect, degrees: i32, entity: usize) {
231 let n = cells.size as i32;
232 let (w, h) = (rect.width(), rect.height());
233 let fp = rules.footprint.as_bytes();
234 for y in rect.y0..=rect.y1 {
235 for x in rect.x0..=rect.x1 {
236 let i = (n * y + x) as usize;
237 let (set, owned, ty) = if fp.is_empty() {
238 (true, true, rules.cell_type)
239 } else {
240 let c = fp
241 .get(footprint_index(x - rect.x0, y - rect.y0, w, h, degrees))
242 .copied()
243 .unwrap_or(b'.');
244 (
245 c == b'#' || c == b'_',
246 c != b'.',
247 if c == b'_' { 2 } else { rules.cell_type },
248 )
249 };
250 if set {
251 cells.set_type(i, CellType(ty));
252 }
253 cells.objects[i] = owned.then_some(entity);
254 }
255 }
256}
257
258pub fn zone(cells: &mut Cells, rules: &PlacementRules, rect: Rect, degrees: i32, entity: usize) {
265 let n = cells.size as i32;
266 let (w, h) = (rect.width(), rect.height());
267 let fp = rules.footprint.as_bytes();
268 for y in rect.y0..=rect.y1 {
269 for x in rect.x0..=rect.x1 {
270 let i = (n * y + x) as usize;
271 let untouched = !fp.is_empty()
272 && fp
273 .get(footprint_index(x - rect.x0, y - rect.y0, w, h, degrees))
274 .copied()
275 .unwrap_or(b'.')
276 == b'.';
277 if untouched {
278 cells.occupants[i] = None;
279 cells.flags[i] = cells.flags[i].wrapping_add(1);
280 } else {
281 cells.occupants[i] = Some(entity);
282 }
283 }
284 }
285}
286
287#[cfg(test)]
288mod tests {
289 use super::*;
290
291 #[test]
292 fn footprints_centre_on_the_cursor() {
293 let r = footprint(256, (100, 100), 8, 5, 0).unwrap();
295 assert_eq!((r.x0, r.x1, r.y0, r.y1), (97, 104, 98, 102));
296 let r = footprint(256, (100, 100), 8, 5, 1).unwrap();
297 assert_eq!((r.width(), r.height()), (5, 8));
298 assert!(footprint(256, (0, 100), 3, 3, 0).is_none());
299 }
300
301 #[test]
302 fn rotated_indices_cover_the_string() {
303 let (w, h) = (2, 2);
305 let mut seen: Vec<usize> = Vec::new();
306 for deg in [0, 90, 180, 270] {
307 seen.clear();
308 for j in 0..h {
309 for i in 0..w {
310 seen.push(footprint_index(i, j, w, h, deg));
311 }
312 }
313 seen.sort();
314 assert_eq!(seen, vec![0, 1, 3, 4], "{deg}");
315 }
316 }
317}