aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/player_motion.rs
blob: 439429cda08f45fff6607bddabf2278058fa0fbc (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
use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState};
use crate::TerrainWorld;

/// Player controls sampled once for a simulation step.
#[derive(Clone, Copy, Debug, Default)]
pub struct PlayerMotion {
    /// Signed strafe and forward input in the range -1 to 1.
    pub movement: [f32; 2],
    /// Relative horizontal mouse motion for this step.
    pub mouse_x: f32,
    /// Relative vertical mouse motion for this step, positive down.
    pub mouse_y: f32,
    /// Time spent on this simulation step, excluding time spent paused.
    pub delta_seconds: f32,
}

/// Proposed hero transform and the body-level sweep used to check buildings.
#[derive(Clone, Copy, Debug)]
pub struct PlayerMotionStep {
    pub(super) pose: TransformState,
    pub(super) pitch_bits: u32,
    /// Sweep segment when the step translates the hero.
    pub sweep: Option<[[f32; 3]; 2]>,
}

/// Proposes a hero step from the world pose and the active terrain.
///
/// # Errors
///
/// Returns an error for invalid motion values or a failed terrain query.
pub fn plan_player_motion(
    engine: &Engine,
    cockpit: StartingCockpit,
    input: PlayerMotion,
) -> Result<Option<PlayerMotionStep>, EngineError> {
    if input.movement == [0.0; 2] && input.mouse_x == 0.0 && input.mouse_y == 0.0 {
        return Ok(None);
    }
    let (Some(mission), Ok(pose)) = (
        &engine.loaded,
        transform_state(&engine.world, cockpit.handle),
    ) else {
        return Ok(None);
    };
    plan_motion(
        &mission.terrain,
        pose,
        f32::from_bits(mission.player_pitch_bits),
        input,
    )
    .map(Some)
}

fn plan_motion(
    terrain: &TerrainWorld,
    mut pose: TransformState,
    pitch: f32,
    input: PlayerMotion,
) -> Result<PlayerMotionStep, EngineError> {
    let from = pose.position.map(f32::from_bits);
    let yaw = f32::from_bits(pose.orientation[2]);
    if !input
        .movement
        .into_iter()
        .chain([
            input.mouse_x,
            input.mouse_y,
            input.delta_seconds,
            yaw,
            pitch,
        ])
        .chain(from)
        .all(f32::is_finite)
        || input.delta_seconds < 0.0
    {
        return Err(EngineError::Movement("invalid player step".into()));
    }
    // shortcut: use constant walk speed and mouse gain until native Control
    // acceleration and mouse sensitivity are measured.
    let yaw = if input.mouse_x != 0.0 {
        let yaw = (yaw - input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003)
            .rem_euclid(std::f32::consts::TAU);
        pose.orientation[2] = yaw.to_bits();
        yaw
    } else {
        yaw
    };
    let [right, forward] = input.movement.map(|axis| axis.clamp(-1.0, 1.0));
    let distance = 8.0 * input.delta_seconds.min(0.1) / right.hypot(forward).max(1.0);
    let (sin, cos) = yaw.sin_cos();
    let xy = [
        from[0] + (cos * right - sin * forward) * distance,
        from[1] + (sin * right + cos * forward) * distance,
    ];
    let mut sweep = None;
    if xy != [from[0], from[1]] {
        if let (Some(old_ground), Some(new_ground)) = (
            terrain
                .height_at([from[0], from[1]])
                .map_err(|err| EngineError::Movement(err.to_string()))?,
            terrain
                .height_at(xy)
                .map_err(|err| EngineError::Movement(err.to_string()))?,
        ) {
            let to = [xy[0], xy[1], new_ground + (from[2] - old_ground)];
            pose.position = to.map(f32::to_bits);
            // shortcut: one sweep at body height until the physical collider
            // and floor transitions are recovered.
            sweep = Some([
                [from[0], from[1], from[2] + 0.5],
                [to[0], to[1], to[2] + 0.5],
            ]);
        }
    }
    // shortcut: use a bounded camera aim until the native turret travel and
    // sensitivity are recovered. The body remains aligned to the terrain.
    let pitch = (pitch - input.mouse_y * 0.005).clamp(-1.0, 1.0);
    Ok(PlayerMotionStep {
        pose,
        pitch_bits: pitch.to_bits(),
        sweep,
    })
}