aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/construction_site.rs
blob: ddb92a8ef78b9b69f5cf1856ea62063eb8c1e628 (plain) (blame)
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
use fparkan_terrain::{NavigationQuery, SurfaceQuery, TerrainWorld};
use fparkan_world::{handle_by_original_id, registration_sequence, transform_state, World};

use crate::{mining::MineSite, MissionObjectDraft};

#[derive(Default)]
pub(super) struct SiteReservations(Vec<([u32; 2], u32)>);

pub(super) fn building_centers<'a>(
    world: &'a World,
    drafts: &'a [MissionObjectDraft],
) -> impl Iterator<Item = [f32; 2]> + 'a {
    drafts.iter().filter_map(|draft| {
        if draft.unit_dat_header_flags? & 0x8000_0000 == 0 {
            return None;
        }
        let handle = handle_by_original_id(world, draft.original_id?)?;
        registration_sequence(world, handle).ok()??;
        let [x, y, _] = transform_state(world, handle)
            .ok()?
            .position
            .map(f32::from_bits);
        Some([x, y])
    })
}

pub(super) fn separated([x, y]: [f32; 2], [a, b]: [f32; 2]) -> bool {
    (x - a).trunc().hypot((y - b).trunc()) >= 250.0
}

impl SiteReservations {
    pub(super) fn find(
        &mut self,
        terrain: &TerrainWorld,
        world: &World,
        drafts: &[MissionObjectDraft],
        mines: &[MineSite],
        base: [f32; 2],
        now_ms: u32,
    ) -> Option<[u32; 2]> {
        self.0
            .retain(|&(_, time)| now_ms.wrapping_sub(time) <= 60_000);
        let occupied = building_centers(world, drafts);
        let deposits = mines.iter().filter_map(|site| {
            let [x, y, _] = site.position;
            site.live_mine(world).is_none().then_some([x, y])
        });
        let occupied = occupied
            .chain(deposits)
            .chain(
                self.0
                    .iter()
                    .map(|&(position, _)| position.map(|axis| axis as f32)),
            )
            .collect::<Vec<_>>();
        let position = find_site(base, &occupied, |[x, y]| {
            // shortcut: use navigable terrain polygons until the ArealMap placement bit is mapped.
            terrain.locate_areal([x, y, 0.0]).ok()??;
            terrain.height_at([x, y]).ok()?
        })?;
        if self.0.len() < 20 {
            self.0.push((position, now_ms));
        }
        Some(position)
    }
}

fn find_site(
    base: [f32; 2],
    occupied: &[[f32; 2]],
    mut ground: impl FnMut([f32; 2]) -> Option<f32>,
) -> Option<[u32; 2]> {
    let [base_x, base_y] = base.map(f32::trunc);
    for ring in 0..10 {
        let radius = ring as f64 * 150.0;
        let step = (150.0 / radius).atan() as f32;
        let mut angle = 0.0_f32;
        while angle < 2.0 * (31415.0 / 10000.0) {
            let (sin, cos) = f64::from(angle).sin_cos();
            let x = base_x + (radius * cos).trunc() as f32;
            let y = base_y + (radius * sin).trunc() as f32;
            if x > 0.0 && y > 0.0 && occupied.iter().all(|&other| separated([x, y], other)) {
                let corners = [
                    [x + 75.0, y + 75.0],
                    [x - 75.0, y - 75.0],
                    [x + 75.0, y - 75.0],
                    [x - 75.0, y + 75.0],
                ];
                let [Some(a), Some(b), Some(c), Some(d)] = corners.map(&mut ground) else {
                    angle += step;
                    continue;
                };
                let mean = (a + b + c + d) * 0.25;
                if [a, b, c, d]
                    .into_iter()
                    .map(|height| (height - mean).abs())
                    .sum::<f32>()
                    * 0.25
                    <= 20.0
                {
                    return Some([x as u32, y as u32]);
                }
            }
            angle += step;
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn site_checks_the_footprint_and_clearance_in_base_ring_order() {
        let flat = |_| Some(10.0);
        assert_eq!(find_site([600.8, 600.9], &[], flat), Some([600, 600]));
        assert_eq!(
            find_site([600.0, 600.0], &[[600.0, 600.0]], flat),
            Some([900, 600])
        );
        assert_eq!(
            find_site([600.0, 600.0], &[[350.0, 600.0]], flat),
            Some([600, 600])
        );
        assert!(find_site([600.0, 600.0], &[], |_| None).is_none());
        assert!(find_site([600.0, 600.0], &[], |[x, _]| Some(x)).is_none());
    }
}