aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategy_cursor.rs
blob: c38a5bc9b30f49a930e40978ed6eb1dd74486def (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
//! The original strategic mouse cursor strips in `ui/cursor.cfg`.

use crate::{ui_configuration, ui_sprite, RgbaImage};
use fparkan_vfs::Vfs;

/// Number of adjacent frames loaded by iron3d+0x57d00.
pub const PHASES: u32 = 4;

/// The cursor modes used for strategic routes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StrategyCursor {
    /// Ordinary UI pointer.
    Arrow,
    /// A valid order waypoint.
    Place,
    /// An invalid order waypoint.
    WrongPlace,
    /// An eligible patrol point or object.
    Guard,
}

impl StrategyCursor {
    /// Modes in draw-range order.
    pub const ALL: [Self; 4] = [Self::Arrow, Self::Place, Self::WrongPlace, Self::Guard];

    fn key(self) -> &'static str {
        match self {
            Self::Arrow => "ARROW",
            Self::Place => "PLACE",
            Self::WrongPlace => "WRONG_PLACE",
            Self::Guard => "GUARD",
        }
    }
}

/// Four adjacent frames and their anchoring and phase delay.
pub struct CursorStrip {
    pub image: RgbaImage,
    pub hotspot: [u32; 2],
    pub phase_delay_ms: u32,
}

/// Loads the route cursor strips from the installed UI archive.
pub fn load(vfs: &dyn Vfs) -> Result<[CursorStrip; StrategyCursor::ALL.len()], String> {
    let config = ui_configuration::read(vfs, "ui/cursor.cfg")?;
    let config = String::from_utf8_lossy(&config);
    let region = |kind: StrategyCursor| {
        ui_configuration::object(&config, kind.key())
            .ok_or_else(|| format!("missing cursor {}", kind.key()))
    };
    let mut strips = Vec::with_capacity(StrategyCursor::ALL.len());
    let mut page = None;
    for kind in StrategyCursor::ALL {
        let fields = region(kind)?;
        let texture = ui_configuration::field(&fields, "TEXTURE")?;
        if page.as_ref().is_none_or(|(current, _)| *current != texture) {
            page = Some((texture, ui_sprite::load_ui_page(vfs, texture)?));
        }
        let Some((_, atlas)) = &page else {
            unreachable!("the cursor atlas was loaded above");
        };
        let number = |key| ui_configuration::number(&fields, key);
        let [x, y] = [number("OFFSET_X")?, number("OFFSET_Y")?];
        let [w, h] = [number("WIDTH")?, number("HEIGHT")?];
        let strip_width = w.checked_mul(PHASES).ok_or("cursor strip width overflow")?;
        let image = ui_sprite::atlas_region(atlas, [x, y, strip_width, h])
            .ok_or_else(|| format!("cursor {} outside atlas", kind.key()))?;
        strips.push(CursorStrip {
            image,
            hotspot: [number("EXTENT_X")?, number("EXTENT_Y")?],
            phase_delay_ms: number("PHASE_DELAY")?,
        });
    }
    Ok(strips
        .try_into()
        .unwrap_or_else(|_| unreachable!("one strip per mode")))
}

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

    #[test]
    #[ignore = "requires licensed corpus"]
    fn original_strategy_cursor_frames_and_hotspots() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
        let strips = load(&DirectoryVfs::new(root)).unwrap();
        assert_eq!(
            strips.each_ref().map(|strip| strip.hotspot),
            [[0, 0], [8, 8], [8, 8], [8, 8]]
        );
        for strip in strips {
            assert_eq!([strip.image.width, strip.image.height], [64, 16]);
            assert_eq!(strip.phase_delay_ms, 150);
            for phase in 0..PHASES {
                let offset = (phase * 16 * 4) as usize;
                assert!(strip
                    .image
                    .rgba8
                    .chunks_exact(64 * 4)
                    .any(|row| row[offset..offset + 16 * 4]
                        .chunks_exact(4)
                        .any(|pixel| pixel[3] != 0)));
            }
        }
    }
}