mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
d3d12 gameinput: kbm binds read F144/Numpad160 without overflow; leaving a vehicle by hand follows the remap
merzleckeier's review of 94950d4a5:
- key_named used `then_some(VK_F1 + n - 1)` and `then_some(VK_NUMPAD0 +
digit)`, which add before the range check: F144 or Numpad160 overflowed u8
(a panic with overflow checks) instead of an "unknown key" warning. Now
`then(|| ...)`.
- With no context measured, Caps Lock's driving table ended on the physical
F, so `vehicle:F=RB` left driving and gave no RB. It now ends on a key just
pressed that is the pad's Y in the driving table (F unless remapped;
Binds::exit_pressed). A key that is Y on foot too switches at once, as F
did; one that is another input on foot keeps the driving table while held
(`leaving`), so its Y reaches the title, and the on-foot table comes when
it is let go. Caps Lock or a measured context drops `leaving`.
Tests: four out-of-range names in bind_entries_read (F144, F255,
Numpad160, Num250); leaving_a_vehicle_by_hand_follows_the_remap
(vehicle:F=RB stays RB and driving; both:G=Y leaves at once with Y; G Y in a
vehicle and RB on foot holds Y until let go, then on-foot W is the stick and
G is RB; Caps Lock while leaving drops it). cargo test -p leonida-hle 258
passed, 2 ignored; clippy -D warnings clean. With the old then_some and the
physical-F rule both tests fail (overflow panic, vehicle:F=RB leaving).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -263,10 +263,10 @@ fn key_named(name: &str) -> Option<u8> {
|
||||
}
|
||||
let number = |rest: &str| rest.parse::<u8>().ok().filter(|_| rest.bytes().all(|b| b.is_ascii_digit()));
|
||||
if let Some(digit) = name.strip_prefix("numpad").or_else(|| name.strip_prefix("num")).and_then(number) {
|
||||
return (digit <= 9).then_some(VK_NUMPAD0 + digit);
|
||||
return (digit <= 9).then(|| VK_NUMPAD0 + digit);
|
||||
}
|
||||
if let Some(n) = name.strip_prefix('f').and_then(number) {
|
||||
return (1..=12).contains(&n).then_some(VK_F1 + n - 1);
|
||||
return (1..=12).contains(&n).then(|| VK_F1 + n - 1);
|
||||
}
|
||||
Some(match name.as_str() {
|
||||
"space" => VK_SPACE,
|
||||
@ -463,6 +463,11 @@ impl Binds {
|
||||
foot.len() != vehicle.len() || foot.iter().any(|to| !vehicle.contains(to))
|
||||
}
|
||||
|
||||
/// A key just pressed (`pressed`) that is the pad's Y in the driving table: it leaves the vehicle.
|
||||
fn exit_pressed(&self, pressed: impl Fn(u8) -> bool) -> Option<u8> {
|
||||
self.vehicle.iter().find(|&&(key, to)| to == To::Button(Y) && pressed(key)).map(|&(key, _)| key)
|
||||
}
|
||||
|
||||
/// The pad of a table, with the keys `on` says are down.
|
||||
fn pad(&self, driving: bool, on: impl Fn(u8) -> bool) -> PadState {
|
||||
let mut pad = PadState::default();
|
||||
@ -613,9 +618,12 @@ pub(super) struct Kbm {
|
||||
binds: &'static Binds,
|
||||
/// The table in use.
|
||||
driving: bool,
|
||||
/// Caps Lock's say while no context is measured: the driving table, until Caps Lock again or F (which leaves
|
||||
/// the vehicle).
|
||||
/// Caps Lock's say while no context is measured: the driving table, until Caps Lock again or a key that is the
|
||||
/// pad's Y (enter and leave a vehicle) in the driving table, F unless remapped.
|
||||
by_hand: bool,
|
||||
/// That key, held down while it leaves the vehicle by hand and is another input on foot: the driving table stays
|
||||
/// until it is let go, so its Y reaches the title first.
|
||||
leaving: Option<u8>,
|
||||
/// The measured context as it counts (see [`UNKNOWN_AFTER`]).
|
||||
measured: PlayerContext,
|
||||
unknown_since: Option<u64>,
|
||||
@ -641,6 +649,7 @@ impl Kbm {
|
||||
binds,
|
||||
driving: false,
|
||||
by_hand: false,
|
||||
leaving: None,
|
||||
measured: PlayerContext::Unknown,
|
||||
unknown_since: None,
|
||||
down: Keys::default(),
|
||||
@ -669,20 +678,40 @@ impl Kbm {
|
||||
// What is measured counts; driving by hand is for when nothing is, and starts anew then.
|
||||
self.measured = measured;
|
||||
self.by_hand = false;
|
||||
self.leaving = None;
|
||||
}
|
||||
match self.measured {
|
||||
PlayerContext::InVehicle => (true, "the player is in a vehicle"),
|
||||
PlayerContext::OnFoot => (false, "the player is on foot"),
|
||||
PlayerContext::Unknown if pressed(VK_CAPITAL) => {
|
||||
self.by_hand = !self.by_hand;
|
||||
self.leaving = None;
|
||||
(self.by_hand, "Caps Lock")
|
||||
}
|
||||
PlayerContext::Unknown if pressed(VK_ENTER_EXIT) && self.by_hand => {
|
||||
// Driving by hand, and the player leaves the vehicle: W must not stay the right trigger.
|
||||
self.by_hand = false;
|
||||
(false, "F leaves the vehicle; Caps Lock is the driving keys")
|
||||
}
|
||||
PlayerContext::Unknown => (self.by_hand, "what the player is in is not known"),
|
||||
PlayerContext::Unknown if self.by_hand => match self.leaving {
|
||||
// The key that leaves, another input on foot, let go: now the on-foot table.
|
||||
Some(key) if !down.has(key) => {
|
||||
self.by_hand = false;
|
||||
self.leaving = None;
|
||||
(false, "the key that leaves the vehicle was let go; Caps Lock is the driving keys")
|
||||
}
|
||||
Some(_) => (true, "leaving the vehicle"),
|
||||
// Driving by hand, and the player leaves the vehicle (the driving table's Y, F unless
|
||||
// remapped): W must not stay the right trigger. A key that is Y on foot too switches now;
|
||||
// one that is another input on foot keeps the driving table until let go, so its Y is sent.
|
||||
None => match self.binds.exit_pressed(pressed) {
|
||||
Some(key) if self.binds.changes(key) => {
|
||||
self.leaving = Some(key);
|
||||
(true, "leaving the vehicle")
|
||||
}
|
||||
Some(_) => {
|
||||
self.by_hand = false;
|
||||
(false, "the key that leaves the vehicle; Caps Lock is the driving keys")
|
||||
}
|
||||
None => (true, "what the player is in is not known"),
|
||||
},
|
||||
},
|
||||
PlayerContext::Unknown => (false, "what the player is in is not known"),
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1218,6 +1247,11 @@ mod tests {
|
||||
("foot:F0=RT", "unknown key"),
|
||||
("foot:F+1=RT", "unknown key"),
|
||||
("foot:Numpad10=RT", "unknown key"),
|
||||
// Out of range before any arithmetic: no u8 overflow (a panic with overflow checks).
|
||||
("foot:F144=RT", "unknown key"),
|
||||
("foot:F255=RT", "unknown key"),
|
||||
("foot:Numpad160=RT", "unknown key"),
|
||||
("foot:Num250=RT", "unknown key"),
|
||||
("foot:Numpad=RT", "unknown key"),
|
||||
("foot:G=", "unknown action"),
|
||||
("foot:G=Jump", "unknown action"),
|
||||
@ -1353,4 +1387,44 @@ mod tests {
|
||||
assert_eq!(k.pad(&held, CAR, 0), super::super::fallback(&held));
|
||||
assert_eq!(k.pad(&held, CAR, 0).right_trigger, 1.0);
|
||||
}
|
||||
|
||||
/// Caps Lock's driving table (no context measured), then `held` pressed at 1 s: the pad then and whether the
|
||||
/// driving table is still in use.
|
||||
fn by_hand_press(k: &mut Kbm, held: &[u8]) -> (PadState, bool) {
|
||||
k.pad(&keys(&[VK_CAPITAL]), UNKNOWN, 0);
|
||||
k.pad(&keys(&[]), UNKNOWN, 8 * MS);
|
||||
assert!(k.driving);
|
||||
let pad = k.pad(&keys(held), UNKNOWN, 1000 * MS);
|
||||
(pad, k.driving)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaving_a_vehicle_by_hand_follows_the_remap() {
|
||||
// F remapped to RB in a vehicle: it is RB there and no longer leaves the driving table.
|
||||
let mut k = Kbm::with_binds(Config::default(), binds("vehicle:F=RB"));
|
||||
assert_eq!(by_hand_press(&mut k, b"F"), (button(RIGHT_SHOULDER), true));
|
||||
// G made Y in both tables, F RB: G leaves at once (its Y sent), F stays RB.
|
||||
let mut k = Kbm::with_binds(Config::default(), binds("both:F=RB;both:G=Y"));
|
||||
assert_eq!(by_hand_press(&mut k, b"G"), (button(Y), false));
|
||||
let mut k = Kbm::with_binds(Config::default(), binds("both:F=RB;both:G=Y"));
|
||||
assert_eq!(by_hand_press(&mut k, b"F"), (button(RIGHT_SHOULDER), true));
|
||||
// G Y in a vehicle only (RB on foot): the driving table stays while it is held, so the title gets its Y,
|
||||
// and the on-foot table comes when it is let go; G then gives nothing until pressed again... it is let go.
|
||||
let mut k = Kbm::with_binds(Config::default(), binds("vehicle:G=Y;foot:G=RB"));
|
||||
assert_eq!(by_hand_press(&mut k, b"G"), (button(Y), true));
|
||||
assert_eq!(k.pad(&keys(b"G"), UNKNOWN, 1100 * MS), button(Y));
|
||||
assert!(k.driving);
|
||||
k.pad(&keys(&[]), UNKNOWN, 1200 * MS);
|
||||
assert!(!k.driving);
|
||||
// On foot G is RB; W is the stick again, not the trigger.
|
||||
assert_eq!(k.pad(&keys(b"W"), UNKNOWN, 2000 * MS), PadState { left_y: 1.0, ..Default::default() });
|
||||
k.pad(&keys(&[]), UNKNOWN, 2008 * MS);
|
||||
assert_eq!(k.pad(&keys(b"G"), UNKNOWN, 2016 * MS), button(RIGHT_SHOULDER));
|
||||
// Caps Lock while G is held to leave: Caps Lock's word counts, the leaving is dropped.
|
||||
let mut k = Kbm::with_binds(Config::default(), binds("vehicle:G=Y;foot:G=RB"));
|
||||
by_hand_press(&mut k, b"G");
|
||||
k.pad(&keys(&[b'G', VK_CAPITAL]), UNKNOWN, 1100 * MS);
|
||||
assert!(!k.driving);
|
||||
assert_eq!(k.leaving, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user