aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/cockpit_reticle.rs
blob: a4d9343cf3a1fdd518faee324e9c12d7f8a5e5bf (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
//! Original page9 crops and 640×480 placement for the cockpit reticle.

use crate::{
    mission_pager::copy_sprite,
    ui_sprite::{atlas_region, load_ui_page},
    RgbaImage,
};
use fparkan_vfs::Vfs;

/// The rectangle enclosing the reticle sprites in the native viewport.
pub const BOUNDS: [u32; 4] = [232, 199, 408, 281];

/// Assembles the ring, its corners and the mirrored scales from the installed atlas.
pub fn load(vfs: &dyn Vfs) -> Result<RgbaImage, String> {
    let page = load_ui_page(vfs, "page9")?;
    let crop = |rect| atlas_region(&page, rect).ok_or("reticle crop outside page9");
    // iron3d+0x4318d selects these three source rectangles.
    let scale = crop([168, 0, 82, 15])?;
    let ring = crop([77, 0, 64, 64])?;
    let corner = crop([54, 28, 23, 23])?;
    let [x, y, right, bottom] = BOUNDS;
    let mut reticle = RgbaImage {
        width: right - x,
        height: bottom - y,
        rgba8: vec![0; ((right - x) * (bottom - y) * 4) as usize],
    };
    // iron3d+0x43293 draws the lower right corner with a wider span.
    for (sprite, [left, top, right, bottom], flip) in [
        (&ring, [288, 208, 352, 272], [false; 2]),
        (&corner, [279, 199, 300, 220], [false, true]),
        (&corner, [279, 260, 300, 281], [false; 2]),
        (&corner, [340, 199, 361, 220], [true, true]),
        (&corner, [340, 260, 367, 281], [true, false]),
        (&scale, [232, 233, 314, 248], [false; 2]),
        (&scale, [326, 233, 408, 248], [true, false]),
    ] {
        copy_sprite(
            &mut reticle,
            sprite,
            [left - x, top - y, right - x, bottom - y],
            flip,
            [55, 255, 55],
        );
    }
    Ok(reticle)
}

/// Native solid marker bounds from the camera's forward vector in hull space.
pub fn heading_marker([x, y]: [f32; 2]) -> [f32; 4] {
    // iron3d+0x433d5 rounds each edge separately before subtracting it from the center.
    let edge = |center: f32, direction: f32, padding: f32| {
        center - (24.0 * direction + padding).round_ties_even()
    };
    [
        edge(320.0, x, 1.0),
        edge(240.0, y, 1.0),
        edge(320.0, x, -1.0),
        edge(240.0, y, -1.0),
    ]
}

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

    #[test]
    fn heading_marker_uses_camera_rotation_within_the_hull() {
        assert_eq!(heading_marker([0.0, 1.0]), [319.0, 215.0, 321.0, 217.0]);
        // Local matrix sampled from the medium bunker after aiming in Two Fortresses.
        assert_eq!(
            heading_marker([0.274_880, 0.103_380]),
            [312.0, 237.0, 314.0, 239.0]
        );
    }
}