aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/repair_system.rs
blob: 8fa6c09abc78cbeea4decf692c88b965cce322c2 (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
//! State switch for the controlled unit's Control repair device.

use crate::{Engine, MissionControlDeviceTableStatus, StartingCockpit};

/// Switches the selected unit's self-repair device when it is present.
pub fn toggle_self_repair(engine: &mut Engine, cockpit: StartingCockpit) -> Option<bool> {
    let mission = engine.loaded.as_mut()?;
    let table = mission
        .control_device_tables
        .iter_mut()
        .find(|table| table.handle == cockpit.handle)?;
    let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else {
        return None;
    };
    let device = devices.iter_mut().find(|device| {
        device.key == 0 && device.descriptor.tag == 15 && device.factory_tag == 15
    })?;
    // Control+0x22c90 handles the inverse switch command 0x40.
    let enabled = device.native_word_50_bits? != 0x20;
    device.native_word_50_bits = Some(if enabled { 0x20 } else { 0 });
    Some(enabled)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        apply_loaded_mission_control_node_delta, create, frame, load_gameplay_mission,
        player_status, starting_cockpit, EngineServices, MissionMode, MissionRequest,
        MissionStartupInput,
    };
    use fparkan_vfs::DirectoryVfs;
    use std::sync::Arc;

    #[test]
    #[ignore = "requires licensed corpus"]
    fn licensed_hero_repairs_only_when_switched_on() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
        let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
        load_gameplay_mission(
            &mut engine,
            MissionRequest {
                key: "MISSIONS/Single.01/data.tma".into(),
                mode: MissionMode::SingleMission,
            },
            MissionStartupInput {
                queue_time_ms: 0,
                research_creation_gate: false,
            },
            None,
        )
        .unwrap();
        let hero = starting_cockpit(&engine).unwrap();
        apply_loaded_mission_control_node_delta(&mut engine, hero.handle, 0, -250.0).unwrap();
        let health = |engine: &Engine| {
            player_status(engine, hero.handle)
                .and_then(|status| status.health_percent)
                .unwrap()
        };
        let damaged = health(&engine);
        for now in (0..=5_000).step_by(50) {
            frame(&mut engine, now).unwrap();
        }
        assert_eq!(health(&engine), damaged);
        assert_eq!(toggle_self_repair(&mut engine, hero), Some(true));
        for now in (5_050..=15_000).step_by(50) {
            frame(&mut engine, now).unwrap();
        }
        let repaired = health(&engine);
        assert!(
            repaired > damaged && repaired < 100,
            "{damaged} -> {repaired}"
        );
        assert_eq!(toggle_self_repair(&mut engine, hero), Some(false));
        for now in (15_050..=20_000).step_by(50) {
            frame(&mut engine, now).unwrap();
        }
        assert_eq!(health(&engine), repaired);
    }
}