From a316768bfcb97ee2eb43ab731c700841da1a0b8c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:42:19 +0200 Subject: [PATCH 1/2] DualSense adaptive triggers Each trigger feels the action bound to it: resistance growing while a bow draws, a trident charges or a crossbow loads, a gun trigger's click on a loaded crossbow, a wall where pulling boosts (a rocket while gliding, the stick of a ridden pig or strider), and a light buzz while mining, scaled by the block's hardness. Free in menus, paused, unfocused, on another pad, with controllers off, and on quit. On by default, "Adaptive Triggers" in the controller options turns them off. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/pad.rs | 96 +++++++++++++- crates/horizon-client/src/gamepad.rs | 100 +++++++++++++- crates/horizon-client/src/options_horizon.rs | 2 + crates/horizon-client/src/options_ui.rs | 7 + .../horizon-dualsense/examples/dualsense.rs | 26 ++-- crates/horizon-dualsense/src/lib.rs | 1 + crates/horizon-dualsense/src/trigger.rs | 124 ++++++++++++++++++ docs/controllers.md | 26 +++- 8 files changed, 369 insertions(+), 13 deletions(-) create mode 100644 crates/horizon-dualsense/src/trigger.rs diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index ce0633615..4216de972 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -6,7 +6,7 @@ use super::{Game, Screen}; use horizon_client::{ Result, - gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble}, + gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble, TriggerFeel}, keybinds::{KeyBinds, key_code, mouse_button}, mouse::Rotation, }; @@ -59,6 +59,8 @@ pub(super) struct Pads { /// Its own output over HID (rumble here; gilrs only reads it), started once this window /// has the pad in hand; dropped, it releases the pad. output: Option, + /// What it was last told to show. + shown: Option, } fn gilrs_button(button: Button) -> gilrs::Button { @@ -188,6 +190,9 @@ impl Game<'_> { pub(super) fn poll_pads(&mut self, event_loop: &ActiveEventLoop) { let settings = &self.settings.controller; if !settings.enabled { + // Dropping the output frees the triggers. + self.pad.output = None; + self.pad.shown = None; if self.pad.state != PadState::default() { self.pad.state = PadState::default(); for (button, down) in self.pad.edges.release_all() { @@ -214,6 +219,7 @@ impl Game<'_> { if self.pad.dualsense && self.pad.in_use && self.focused && self.pad.output.is_none() { self.pad.output = Some(horizon_dualsense::Link::start()); } + self.pad_show(); // Damage from any source, here or on a server; nothing counts on // the title screen. let health = self @@ -407,6 +413,93 @@ impl Game<'_> { (self.pad.dualsense && self.focused && output.open().is_some()).then_some(output) } + /// What the DualSense shows: while playing, its triggers feel what their actions do; in + /// menus, paused, unfocused or with another pad active, they are free. + fn pad_show(&mut self) { + if self.pad.output.is_none() { + return; + } + let playing = self.pad.dualsense + && self.focused + && self.title.is_none() + && self.screen == Screen::Playing + && !self.pause; + let (using, attacking) = if playing { + self.trigger_feels() + } else { + (TriggerFeel::Free, TriggerFeel::Free) + }; + let state = horizon_dualsense::device::State { + triggers: self.settings.controller.trigger_effects(using, attacking), + ..Default::default() + }; + if self.pad.shown != Some(state) + && let Some(output) = &self.pad.output + { + output.set(state); + self.pad.shown = Some(state); + } + } + + /// What `key.use` and `key.attack` are doing, for the triggers bound to them. + fn trigger_feels(&self) -> (TriggerFeel, TriggerFeel) { + let using = match self.using { + Some((slot, ticks)) => match self.stack_in(slot) { + Some(s) if &*s.item == "minecraft:bow" => { + TriggerFeel::Draw(gamepad::bow_power(ticks)) + } + // TridentItem.THROW_THRESHOLD_TIME. + Some(s) if &*s.item == "minecraft:trident" => { + TriggerFeel::Draw(ticks as f32 / 10.0) + } + Some(s) if &*s.item == "minecraft:crossbow" => { + TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) + } + _ => TriggerFeel::Free, + }, + None => self.idle_use_feel(), + }; + // Creative breaks at once; blocks that can't be broken have a negative hardness. + let attacking = match &self.mining { + Some(m) if !self.creative => { + let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); + TriggerFeel::Texture(self.res.facts.get(state).hardness) + } + _ => TriggerFeel::Free, + }; + (using, attacking) + } + + /// The use trigger while nothing is in use: a loaded crossbow clicks, and an item that + /// boosts (a rocket while gliding, the stick of the mount being ridden) has a wall. + fn idle_use_feel(&self) -> TriggerFeel { + let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); + let mount = self + .riding + .and_then(|id| self.mobs.iter().find(|m| m.id == id)); + let mount = mount.map_or("", |m| m.kind.id.as_str()); + let feel = |stack: Option<&horizon_client::items::ItemStack>| match stack + .map(|s| (s, &*s.item)) + { + Some((s, "minecraft:crossbow")) if s.data().is_some_and(|d| !d.charged.is_empty()) => { + TriggerFeel::Click + } + Some((_, "minecraft:firework_rocket")) if gliding => TriggerFeel::Boost, + Some((_, "minecraft:carrot_on_a_stick")) if mount == "minecraft:pig" => { + TriggerFeel::Boost + } + Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { + TriggerFeel::Boost + } + _ => TriggerFeel::Free, + }; + // Use tries the main hand first. + match feel(self.stack_in(self.selected)) { + TriggerFeel::Free => feel(self.offhand.as_ref()), + main => main, + } + } + /// Release the DualSense's output (the game is quitting). pub(super) fn pad_close_output(&mut self) { self.pad.output = None; @@ -513,6 +606,7 @@ impl Game<'_> { "glyphs": self.pad.style.name(), "dualsense_output": self.pad.output.as_ref().and_then(|o| o.open()) .map(|t| format!("{t:?}")), + "trigger_effects": self.pad.shown.map(|s| s.triggers), "last_rumble": self.pad.last_rumble.map(|(why, p)| json!({ "reason": why, "strong": p.strong, "weak": p.weak, "ms": p.millis, })), diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index d67121fbf..79bf0e040 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -385,6 +385,8 @@ pub fn steam_keyboard(choice: TextKeyboard) -> bool { pub struct PadSettings { pub enabled: bool, pub vibration: bool, + /// A DualSense's triggers push back (Horizon's; docs/controllers.md). + pub adaptive_triggers: bool, /// 0..1; 0.5 is the default speed, 1 twice that. pub look_sensitivity: f32, pub invert_x: bool, @@ -407,6 +409,7 @@ impl Default for PadSettings { Self { enabled: true, vibration: true, + adaptive_triggers: true, look_sensitivity: 0.5, invert_x: false, invert_y: false, @@ -460,6 +463,7 @@ impl PadSettings { match key.trim() { "enabled" => s.enabled = flag(s.enabled), "vibration" => s.vibration = flag(s.vibration), + "adaptiveTriggers" => s.adaptive_triggers = flag(s.adaptive_triggers), "lookSensitivity" => s.look_sensitivity = unit(s.look_sensitivity), "invertX" => s.invert_x = flag(s.invert_x), "invertY" => s.invert_y = flag(s.invert_y), @@ -490,9 +494,10 @@ impl PadSettings { } pub fn serialize(&self) -> String { let mut text = format!( - "enabled:{}\nvibration:{}\nlookSensitivity:{}\ninvertX:{}\ninvertY:{}\nmoveDeadzone:{}\nlookDeadzone:{}\ntriggerThreshold:{}\ntoggleSneak:{}\nglyphs:{}\nmenuNavigation:{}\nkeyboard:{}\n", + "enabled:{}\nvibration:{}\nadaptiveTriggers:{}\nlookSensitivity:{}\ninvertX:{}\ninvertY:{}\nmoveDeadzone:{}\nlookDeadzone:{}\ntriggerThreshold:{}\ntoggleSneak:{}\nglyphs:{}\nmenuNavigation:{}\nkeyboard:{}\n", bool_text(self.enabled), bool_text(self.vibration), + bool_text(self.adaptive_triggers), self.look_sensitivity, bool_text(self.invert_x), bool_text(self.invert_y), @@ -947,6 +952,69 @@ impl Rumble { } } +/// What a trigger feels like on a DualSense, from what its action is doing. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum TriggerFeel { + Free, + /// Drawing a bow, charging a trident, loading a crossbow: resistance that grows with the + /// charge, 0..1. + Draw(f32), + /// A loaded crossbow: a gun trigger's click. + Click, + /// Mining a block of this hardness: a light buzz, stronger on harder blocks. + Texture(f32), + /// Pulling it boosts (a rocket while gliding, the stick while riding a pig or a strider): + /// a firm wall halfway. + Boost, +} +impl TriggerFeel { + pub fn effect(self) -> horizon_dualsense::TriggerEffect { + use horizon_dualsense::trigger; + match self { + TriggerFeel::Free => horizon_dualsense::TRIGGER_OFF, + TriggerFeel::Draw(charge) => { + trigger::feedback(1, 1 + (7.0 * charge.clamp(0.0, 1.0)).round() as u8) + } + TriggerFeel::Click => trigger::weapon(3, 6, 8), + TriggerFeel::Texture(hardness) if hardness > 0.0 => { + // Dirt 2, stone 3, deepslate 4, obsidian 5 of 8. + let amplitude = (1.0 + 1.6 * hardness.sqrt()).round().clamp(1.0, 5.0); + trigger::vibration(1, amplitude as u8, 30) + } + TriggerFeel::Texture(_) => horizon_dualsense::TRIGGER_OFF, + TriggerFeel::Boost => trigger::feedback(4, 6), + } + } +} +impl PadSettings { + /// The left and right triggers' effects: each feels the action bound to it, `key.use` or + /// `key.attack`. + pub fn trigger_effects( + &self, + using: TriggerFeel, + attacking: TriggerFeel, + ) -> [horizon_dualsense::TriggerEffect; 2] { + [Button::LeftTrigger, Button::RightTrigger].map(|trigger| { + let actions = self.actions_of(trigger); + let feel = if !self.adaptive_triggers { + TriggerFeel::Free + } else if actions.contains(&"key.use") { + using + } else if actions.contains(&"key.attack") { + attacking + } else { + TriggerFeel::Free + }; + feel.effect() + }) + } +} +/// `BowItem.getPowerForTime`: a bow's draw after this many ticks, 0..1. +pub fn bow_power(ticks: u32) -> f32 { + let f = ticks as f32 / 20.0; + ((f * f + f * 2.0) / 3.0).min(1.0) +} + #[cfg(test)] mod tests { use super::*; @@ -1222,6 +1290,36 @@ mod tests { assert_eq!(w.observe(Some(1.0)), Some(1.0)); } + #[test] + fn triggers_feel_their_action() { + use horizon_dualsense::TRIGGER_OFF; + let mut s = PadSettings::default(); + // Use is LT, attack RT by default. + let [left, right] = s.trigger_effects(TriggerFeel::Draw(1.0), TriggerFeel::Texture(1.5)); + assert_eq!(left, horizon_dualsense::trigger::feedback(1, 8)); + assert_eq!(right, horizon_dualsense::trigger::vibration(1, 3, 30)); + assert_eq!( + s.trigger_effects(TriggerFeel::Draw(0.0), TriggerFeel::Free), + [horizon_dualsense::trigger::feedback(1, 1), TRIGGER_OFF] + ); + // Instant-break blocks don't buzz. + assert_eq!(TriggerFeel::Texture(0.0).effect(), TRIGGER_OFF); + s.adaptive_triggers = false; + assert_eq!( + s.trigger_effects(TriggerFeel::Click, TriggerFeel::Boost), + [TRIGGER_OFF; 2] + ); + assert!(!PadSettings::parse(&s.serialize()).adaptive_triggers); + } + + #[test] + fn bow_draw() { + assert_eq!(bow_power(0), 0.0); + assert!((bow_power(10) - 5.0 / 12.0).abs() < 1e-6); + assert_eq!(bow_power(20), 1.0); + assert_eq!(bow_power(40), 1.0); + } + #[test] fn rumble_pulses() { let small = Rumble::Hurt(1.0).pulse().unwrap(); diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index b7fd68f6c..013333332 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -101,6 +101,7 @@ pub const SECTIONS: &[Section] = &[ [ Opt::PadEnabled, Opt::PadVibration, + Opt::PadAdaptiveTriggers, Opt::PadLookSensitivity, Opt::PadToggleSneak, Opt::PadInvertX, @@ -219,6 +220,7 @@ fn switch_value(opt: Opt, s: &Settings) -> Option { Opt::JapaneseGlyphs => s.japanese_glyphs, Opt::PadEnabled => s.controller.enabled, Opt::PadVibration => s.controller.vibration, + Opt::PadAdaptiveTriggers => s.controller.adaptive_triggers, Opt::PadInvertX => s.controller.invert_x, Opt::PadInvertY => s.controller.invert_y, _ => return None, diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 1e5c623be..5852a61b4 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -71,6 +71,7 @@ pub enum Opt { /// Horizon's controller settings (`horizon-controller.txt`). PadEnabled, PadVibration, + PadAdaptiveTriggers, PadLookSensitivity, PadInvertX, PadInvertY, @@ -169,6 +170,7 @@ impl Opt { // Horizon's own options: English captions (no vanilla keys). Opt::PadEnabled => "Controller", Opt::PadVibration => "Vibration", + Opt::PadAdaptiveTriggers => "Adaptive Triggers", Opt::PadLookSensitivity => "Look Sensitivity", Opt::PadInvertX => "Invert Look X", Opt::PadInvertY => "Invert Look Y", @@ -423,6 +425,7 @@ impl Opt { Opt::JapaneseGlyphs => on_off(s.japanese_glyphs), Opt::PadEnabled => on_off(s.controller.enabled), Opt::PadVibration => on_off(s.controller.vibration), + Opt::PadAdaptiveTriggers => on_off(s.controller.adaptive_triggers), Opt::PadInvertX => on_off(s.controller.invert_x), Opt::PadInvertY => on_off(s.controller.invert_y), Opt::PadToggleSneak => tr(if s.controller.toggle_sneak { @@ -566,6 +569,9 @@ impl Opt { Opt::JapaneseGlyphs => s.japanese_glyphs = !s.japanese_glyphs, Opt::PadEnabled => s.controller.enabled = !s.controller.enabled, Opt::PadVibration => s.controller.vibration = !s.controller.vibration, + Opt::PadAdaptiveTriggers => { + s.controller.adaptive_triggers = !s.controller.adaptive_triggers + } Opt::PadInvertX => s.controller.invert_x = !s.controller.invert_x, Opt::PadInvertY => s.controller.invert_y = !s.controller.invert_y, Opt::PadToggleSneak => s.controller.toggle_sneak = !s.controller.toggle_sneak, @@ -1028,6 +1034,7 @@ impl OptionsUi { rows.extend(pairs(&[ Opt::PadEnabled, Opt::PadVibration, + Opt::PadAdaptiveTriggers, Opt::PadLookSensitivity, Opt::PadToggleSneak, Opt::PadInvertX, diff --git a/crates/horizon-dualsense/examples/dualsense.rs b/crates/horizon-dualsense/examples/dualsense.rs index 90d62ebef..6c0ac6642 100644 --- a/crates/horizon-dualsense/examples/dualsense.rs +++ b/crates/horizon-dualsense/examples/dualsense.rs @@ -6,7 +6,7 @@ //! The pad's input report carries the triggers' active effect, and its accelerometer and gyro //! pick up the motors, so `rumble` measures the shake against the pad lying still. use horizon_dualsense::device::{DualSense, Link, State}; -use horizon_dualsense::{Output, TRIGGER_OFF, TriggerEffect}; +use horizon_dualsense::{Output, TRIGGER_OFF}; use std::time::{Duration, Instant}; /// Offsets in the input report's data, after the report ID (USB 0x01) or the ID and a tag byte @@ -223,13 +223,16 @@ fn leds(pad: &mut DualSense) { } fn triggers(pad: &mut DualSense) { - // Raw effects in the pad's own encoding (docs/controllers.md): resistance from zone 3 on at - // strength 6, then a weapon click between zones 2 and 6. - let feedback: TriggerEffect = [0x21, 0xf8, 0x03, 0x00, 0xda, 0xb6, 0x2d, 0, 0, 0, 0]; - let weapon: TriggerEffect = [0x25, 0x44, 0x00, 0x05, 0, 0, 0, 0, 0, 0, 0]; + use horizon_dualsense::trigger; + // The game's feels (docs/controllers.md "Adaptive triggers"); each holds two seconds so a + // hand on the pad can feel it. for (name, effect) in [ - ("feedback", feedback), - ("weapon", weapon), + ("bow drawn halfway", trigger::feedback(1, 4)), + ("bow drawn fully", trigger::feedback(1, 8)), + ("crossbow click", trigger::weapon(3, 6, 8)), + ("mining stone", trigger::vibration(1, 3, 30)), + ("mining obsidian", trigger::vibration(1, 5, 30)), + ("boost wall", trigger::feedback(4, 6)), ("off", TRIGGER_OFF), ] { write( @@ -240,9 +243,12 @@ fn triggers(pad: &mut DualSense) { ..Output::default() }, ); - std::thread::sleep(Duration::from_millis(100)); - print!(" {name:9}"); + std::thread::sleep(Duration::from_millis(150)); + print!(" {name:18}"); trigger_status(pad); + if effect != TRIGGER_OFF { + std::thread::sleep(Duration::from_millis(1850)); + } } } @@ -256,7 +262,7 @@ fn link() { } println!(" open over {:?} after {:?}", link.open(), start.elapsed()); link.set(State { - triggers: [TRIGGER_OFF, [0x25, 0x44, 0x00, 0x05, 0, 0, 0, 0, 0, 0, 0]], + triggers: [TRIGGER_OFF, horizon_dualsense::trigger::weapon(2, 6, 6)], lightbar: Some([0x8b, 0x5c, 0xf6]), player_leds: Some(0b00100), }); diff --git a/crates/horizon-dualsense/src/lib.rs b/crates/horizon-dualsense/src/lib.rs index dd4823b26..f7e70e756 100644 --- a/crates/horizon-dualsense/src/lib.rs +++ b/crates/horizon-dualsense/src/lib.rs @@ -7,6 +7,7 @@ //! and as report 0x31 over Bluetooth, where a sequence tag leads it and a CRC-32 (seeded with //! the byte 0xA2) closes it. pub mod device; +pub mod trigger; pub use device::{Link, Transport}; diff --git a/crates/horizon-dualsense/src/trigger.rs b/crates/horizon-dualsense/src/trigger.rs new file mode 100644 index 000000000..6d9e82d89 --- /dev/null +++ b/crates/horizon-dualsense/src/trigger.rs @@ -0,0 +1,124 @@ +//! The adaptive triggers' effects in the pad's own encoding. A trigger's travel is cut into ten +//! zones (0 at rest, 9 fully pulled); an effect names the zones it acts in and a strength, 1..8, +//! for each, three bits per zone. Modes as documented by Nielk1's trigger effect notes and +//! DualSenseX; [`crate::TRIGGER_OFF`] frees the trigger. +use crate::TriggerEffect; + +const FEEDBACK: u8 = 0x21; +const WEAPON: u8 = 0x25; +const VIBRATION: u8 = 0x26; + +/// Zone bits and three-bit levels (`level - 1`) for the zones whose level isn't 0. +fn zones(levels: [u8; 10]) -> (u16, u32) { + let mut active = 0; + let mut values = 0; + for (i, level) in levels.into_iter().enumerate() { + if level > 0 { + active |= 1 << i; + values |= u32::from(level.min(8) - 1) << (3 * i); + } + } + (active, values) +} + +fn zoned(mode: u8, levels: [u8; 10]) -> TriggerEffect { + let (active, values) = zones(levels); + let mut e = [0; 11]; + e[0] = mode; + e[1..3].copy_from_slice(&active.to_le_bytes()); + e[3..7].copy_from_slice(&values.to_le_bytes()); + e +} + +/// Resistance in each zone, 0 (none) to 8; it holds wherever the trigger is. +pub fn resistance(levels: [u8; 10]) -> TriggerEffect { + zoned(FEEDBACK, levels) +} + +/// Even resistance from zone `start` (0..9) to the end of the travel, at `strength` (1..8). +pub fn feedback(start: u8, strength: u8) -> TriggerEffect { + resistance(std::array::from_fn(|i| { + if i >= usize::from(start) { + strength.clamp(1, 8) + } else { + 0 + } + })) +} + +/// A gun trigger: resistance from zone `start` that gives way with a click at zone `end` +/// (`2 <= start < end <= 8`), at `strength` (1..8). +pub fn weapon(start: u8, end: u8, strength: u8) -> TriggerEffect { + let start = start.clamp(2, 7); + let end = end.clamp(start + 1, 8); + let mut e = [0; 11]; + e[0] = WEAPON; + e[1..3].copy_from_slice(&((1u16 << start) | (1u16 << end)).to_le_bytes()); + e[3] = strength.clamp(1, 8) - 1; + e +} + +/// A buzz from zone `start` (0..9) on, at `amplitude` (1..8) and `frequency` Hz. +pub fn vibration(start: u8, amplitude: u8, frequency: u8) -> TriggerEffect { + let mut e = zoned( + VIBRATION, + std::array::from_fn(|i| { + if i >= usize::from(start) { + amplitude.clamp(1, 8) + } else { + 0 + } + }), + ); + e[9] = frequency; + e +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn feedback_bytes() { + // Zones 3..9 at strength 6 (stored as 5, three bits each). + assert_eq!( + feedback(3, 6), + [0x21, 0xf8, 0x03, 0x00, 0xda, 0xb6, 0x2d, 0, 0, 0, 0] + ); + assert_eq!(feedback(0, 1), [0x21, 0xff, 0x03, 0, 0, 0, 0, 0, 0, 0, 0]); + // Out-of-range strengths are clamped. + assert_eq!(feedback(0, 0), feedback(0, 1)); + assert_eq!(feedback(0, 9), feedback(0, 8)); + } + + #[test] + fn resistance_per_zone() { + let e = resistance([0, 0, 1, 2, 3, 4, 5, 6, 7, 8]); + assert_eq!(u16::from_le_bytes([e[1], e[2]]), 0b11_1111_1100); + let values = u32::from_le_bytes([e[3], e[4], e[5], e[6]]); + for zone in 2..10 { + assert_eq!((values >> (3 * zone)) & 7, zone - 2); + } + } + + #[test] + fn weapon_bytes() { + assert_eq!( + weapon(2, 6, 6), + [0x25, 0x44, 0x00, 0x05, 0, 0, 0, 0, 0, 0, 0] + ); + // The click is always past the start. + assert_eq!(weapon(7, 3, 8)[1..3], [0x80, 0x01]); + } + + #[test] + fn vibration_bytes() { + let e = vibration(8, 2, 30); + assert_eq!(e[..3], [0x26, 0x00, 0x03]); + assert_eq!( + u32::from_le_bytes([e[3], e[4], e[5], e[6]]), + 1 << 24 | 1 << 27 + ); + assert_eq!(e[9], 30); + } +} diff --git a/docs/controllers.md b/docs/controllers.md index 8fd58274c..76074a196 100644 --- a/docs/controllers.md +++ b/docs/controllers.md @@ -219,6 +219,7 @@ exactly vanilla's, so a vanilla client can share the game folder. ``` enabled:true vibration:true +adaptiveTriggers:true lookSensitivity:0.5 invertX:false invertY:false @@ -232,7 +233,7 @@ bind_pad.hotbarNext:rb ``` Only bindings that differ from the defaults are written. The controls -options get a Controller page: on/off, vibration, look sensitivity, invert +options get a Controller page: on/off, vibration, adaptive triggers, look sensitivity, invert X/Y, deadzones, glyph style, sneak toggle, and a binding list like the key binds screen (pick an action, press a button; conflicts marked as vanilla does; reset). @@ -323,6 +324,28 @@ shared, so gilrs keeps reading the same pad), the HID API on Windows. shake while the motors run (`watch` logs the shake while the game drives the pad). +### Adaptive triggers + +On by default with a DualSense ("Adaptive Triggers" in the controller +options, `adaptiveTriggers:`). Each trigger feels the action bound to it, so +rebinding moves the feel along: with the default mapping that is L2 for +`key.use` and R2 for `key.attack`. + +| While | Trigger | Feel | +|---|---|---| +| Drawing a bow | use | resistance from the first tenth of the travel, growing with the draw (`BowItem.getPowerForTime`) | +| Charging a trident | use | the same, full at the 10 ticks a throw needs | +| Loading a crossbow | use | the same, full when it is loaded | +| Holding a loaded crossbow | use | a gun trigger: resistance that gives way with a click, where it fires | +| Gliding with a rocket, riding a pig with a carrot on a stick or a strider with a warped fungus on a stick | use | a firm wall halfway: pulling through it boosts | +| Mining (survival) | attack | a light 30 Hz buzz, stronger on harder blocks (dirt 2, stone 3, deepslate 4, obsidian 5 of 8); none on blocks that break at once | + +Everything else, and every menu, the pause screen, a window without focus, +another pad being the active one, and controllers turned off, leaves the +triggers free; quitting frees them too. Effects are the pad's own modes +(feedback 0x21, weapon 0x25, vibration 0x26): ten zones along the travel, a +three-bit strength per zone. `pad status` shows the effects in use. + Byte layout from public references, read for facts; no code is copied from them (the kernel and dualsensectl are GPL): @@ -331,6 +354,7 @@ them (the kernel and dualsensectl are GPL): | SDL [`SDL_hidapi_ps5.c`](https://github.com/libsdl-org/SDL/blob/6defbc0129b5ba90bab3f6c67bbc1e9329e24ff1/src/joystick/hidapi/SDL_hidapi_ps5.c) and [`SDL_hints.h`](https://github.com/libsdl-org/SDL/blob/6defbc0129b5ba90bab3f6c67bbc1e9329e24ff1/include/SDL3/SDL_hints.h) (zlib) | `6defbc0` | the 47-byte block, rumble flags and stop, 0x0224, legacy at half, LED reset and the Bluetooth animation clock, USB report length 48, the enhanced-mode switch | | Linux [`hid-playstation.c`](https://github.com/torvalds/linux/blob/045958acaa75205b31578cb22c4345e28dd2e4bf/drivers/hid/hid-playstation.c) (GPL) | `045958a` | valid flags, the Bluetooth sequence and tag 0x10, CRC seed 0xA2, `dualsense_reset_leds`, `use_vibration_v2` from 2.21, player LED patterns, the default blue | | [dualsensectl](https://github.com/nowrep/dualsensectl/blob/d3ae2fa272e1bf9a5c08a0ded3f374b40f7c73ac/main.c) (GPL) | `d3ae2fa` | trigger valid bits (flags 0 bits 2, 3), `reduce_motor_power` (byte 36) and its flag, the trigger effect packing | +| Nielk1's [TriggerEffectGenerator](https://gist.github.com/Nielk1/6d54cc2c00d2201ccb8c2720ad7538db/f73a299fdc52f43be84d39e62734192ad82bb393) (MIT) | rev. `f73a299` | the trigger modes (official 0x05, 0x21, 0x25, 0x26) and their zone packing | | [pydualsense](https://github.com/flok/pydualsense/blob/01445f5ed6ba/pydualsense/pydualsense.py) (MIT) | `01445f5` | sets the attenuation flag with 0 on every report | | [nondebug/dualsense](https://github.com/nondebug/dualsense) | `b87450e` | an all-zero report 0x02 leaves rumble emulation for the audio haptics | From 6663af199ec6b1576b873dcc44f7c946a8ff9358 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 18:38:07 +0200 Subject: [PATCH 2/2] DualSense triggers: catches at the press point, shields, official modes only - A loaded crossbow and the boosts (rocket, pig, strider) get a weapon catch that breaks in the zone where the trigger threshold presses, so the click is the shot (the boost was a feedback wall that never gave way). - Draws start at zone 2 and stop at 6 of 8 (held for as long as you like). - A shield in the use hand weighs (feedback 2, 3); disabled by an axe, the trigger locks (feedback 1, 8), as Deathloop does for a jammed gun. - Mining buzzes slower on harder blocks (40/30/20/10 Hz). - A 0 Hz vibration frees the trigger (Nielk1's factory); tests pin the bytes to DSX++'s and dualsensectl's examples. - docs/controllers.md: the table with the effect of each action and why. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/pad.rs | 10 ++- crates/horizon-client/src/game/shield.rs | 5 +- crates/horizon-client/src/gamepad.rs | 79 +++++++++++++++++++----- crates/horizon-dualsense/src/trigger.rs | 32 ++++++++-- docs/controllers.md | 69 +++++++++++++++++---- 5 files changed, 160 insertions(+), 35 deletions(-) diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 4216de972..fdff9abdb 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -455,6 +455,7 @@ impl Game<'_> { Some(s) if &*s.item == "minecraft:crossbow" => { TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) } + Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace, _ => TriggerFeel::Free, }, None => self.idle_use_feel(), @@ -470,8 +471,9 @@ impl Game<'_> { (using, attacking) } - /// The use trigger while nothing is in use: a loaded crossbow clicks, and an item that - /// boosts (a rocket while gliding, the stick of the mount being ridden) has a wall. + /// The use trigger while nothing is in use: a loaded crossbow clicks, an item that boosts (a + /// rocket while gliding, the stick of the mount being ridden) catches, a shield weighs, and + /// locks while an axe has disabled it. fn idle_use_feel(&self) -> TriggerFeel { let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); let mount = self @@ -491,6 +493,10 @@ impl Game<'_> { Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { TriggerFeel::Boost } + Some((s, _)) if self.blocks_attacks(s).is_some() => match self.shield_cooldown { + 0 => TriggerFeel::Brace, + _ => TriggerFeel::Locked, + }, _ => TriggerFeel::Free, }; // Use tries the main hand first. diff --git a/crates/horizon-client/src/game/shield.rs b/crates/horizon-client/src/game/shield.rs index 733dae55f..849c62958 100644 --- a/crates/horizon-client/src/game/shield.rs +++ b/crates/horizon-client/src/game/shield.rs @@ -30,7 +30,10 @@ impl Game<'_> { &mut self.inventory[slot] } } - fn blocks_attacks(&self, stack: &ItemStack) -> Option<&horizon_client::items::BlocksAttacks> { + pub(super) fn blocks_attacks( + &self, + stack: &ItemStack, + ) -> Option<&horizon_client::items::BlocksAttacks> { self.res.items.get(&stack.item)?.blocks_attacks.as_ref() } /// `ShieldItem.use` (ItemUtils.startUsingInstantly) for the hand at `slot`; diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index 79bf0e040..0649b6bef 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -952,37 +952,59 @@ impl Rumble { } } -/// What a trigger feels like on a DualSense, from what its action is doing. +/// What a trigger feels like on a DualSense, from what its action is doing (docs/controllers.md +/// "Adaptive triggers" for the choices). Only the pad's official modes: Nielk1's notes warn the +/// others may leave the firmware. #[derive(Clone, Copy, Debug, PartialEq)] pub enum TriggerFeel { Free, /// Drawing a bow, charging a trident, loading a crossbow: resistance that grows with the /// charge, 0..1. Draw(f32), - /// A loaded crossbow: a gun trigger's click. + /// A loaded crossbow: a gun trigger's catch that gives way where it fires. Click, - /// Mining a block of this hardness: a light buzz, stronger on harder blocks. - Texture(f32), /// Pulling it boosts (a rocket while gliding, the stick while riding a pig or a strider): - /// a firm wall halfway. + /// a lighter catch that gives way where it fires. Boost, + /// A shield in the use hand, raised or ready: its weight. + Brace, + /// That shield disabled by an axe: the trigger locks until it recovers. + Locked, + /// Mining a block of this hardness: a buzz, stronger and slower on harder blocks. + Texture(f32), } impl TriggerFeel { - pub fn effect(self) -> horizon_dualsense::TriggerEffect { + /// The effect, for a trigger that presses its action at `threshold` (0..1) of its travel. + pub fn effect(self, threshold: f32) -> horizon_dualsense::TriggerEffect { use horizon_dualsense::trigger; + // The zone (of ten along the travel) where the action presses: a catch gives way there, + // so the click and the shot coincide. + let press = ((threshold.clamp(0.0, 1.0) * 10.0) as u8).clamp(3, 8); match self { TriggerFeel::Free => horizon_dualsense::TRIGGER_OFF, + // From the third zone, so the press itself stays light; at most 6 of 8, as the + // draw is held. TriggerFeel::Draw(charge) => { - trigger::feedback(1, 1 + (7.0 * charge.clamp(0.0, 1.0)).round() as u8) + trigger::feedback(2, 1 + (5.0 * charge.clamp(0.0, 1.0)).round() as u8) } - TriggerFeel::Click => trigger::weapon(3, 6, 8), + TriggerFeel::Click => trigger::weapon((press - 2).max(2), press, 8), + TriggerFeel::Boost => trigger::weapon((press - 2).max(2), press, 5), + TriggerFeel::Brace => trigger::feedback(2, 3), + TriggerFeel::Locked => trigger::feedback(1, 8), TriggerFeel::Texture(hardness) if hardness > 0.0 => { // Dirt 2, stone 3, deepslate 4, obsidian 5 of 8. let amplitude = (1.0 + 1.6 * hardness.sqrt()).round().clamp(1.0, 5.0); - trigger::vibration(1, amplitude as u8, 30) + // Sand and dirt a fine 40 Hz grit; stone 30; wood, deepslate and ores 20; + // obsidian a 10 Hz chisel. + let hertz = match hardness { + h if h < 1.0 => 40, + h if h < 2.0 => 30, + h if h < 5.0 => 20, + _ => 10, + }; + trigger::vibration(1, amplitude as u8, hertz) } TriggerFeel::Texture(_) => horizon_dualsense::TRIGGER_OFF, - TriggerFeel::Boost => trigger::feedback(4, 6), } } } @@ -1005,7 +1027,7 @@ impl PadSettings { } else { TriggerFeel::Free }; - feel.effect() + feel.effect(self.trigger_threshold) }) } } @@ -1292,18 +1314,41 @@ mod tests { #[test] fn triggers_feel_their_action() { - use horizon_dualsense::TRIGGER_OFF; + use horizon_dualsense::{TRIGGER_OFF, trigger}; let mut s = PadSettings::default(); // Use is LT, attack RT by default. let [left, right] = s.trigger_effects(TriggerFeel::Draw(1.0), TriggerFeel::Texture(1.5)); - assert_eq!(left, horizon_dualsense::trigger::feedback(1, 8)); - assert_eq!(right, horizon_dualsense::trigger::vibration(1, 3, 30)); + assert_eq!(left, trigger::feedback(2, 6)); + assert_eq!(right, trigger::vibration(1, 3, 30)); assert_eq!( s.trigger_effects(TriggerFeel::Draw(0.0), TriggerFeel::Free), - [horizon_dualsense::trigger::feedback(1, 1), TRIGGER_OFF] + [trigger::feedback(2, 1), TRIGGER_OFF] ); - // Instant-break blocks don't buzz. - assert_eq!(TriggerFeel::Texture(0.0).effect(), TRIGGER_OFF); + // The catch gives way in the zone where the default threshold (0.4) presses: weapon + // 0x25, zones 2 and 4, strength 8 (stored 7) or 5 (stored 4). + let catch = |strength| [0x25, 0x14, 0x00, strength, 0, 0, 0, 0, 0, 0, 0]; + assert_eq!(TriggerFeel::Click.effect(0.4), catch(7)); + assert_eq!(TriggerFeel::Boost.effect(0.4), catch(4)); + // A deeper threshold moves it: zones 5 and 7. + assert_eq!(TriggerFeel::Click.effect(0.7)[1..3], [0xa0, 0x00]); + assert_eq!(TriggerFeel::Click.effect(0.1), trigger::weapon(2, 3, 8)); + assert_eq!(TriggerFeel::Brace.effect(0.4), trigger::feedback(2, 3)); + assert_eq!(TriggerFeel::Locked.effect(0.4), trigger::feedback(1, 8)); + // Harder blocks buzz harder and slower; instant-break blocks don't. + assert_eq!( + TriggerFeel::Texture(0.5).effect(0.4), + trigger::vibration(1, 2, 40) + ); + assert_eq!( + TriggerFeel::Texture(3.0).effect(0.4), + trigger::vibration(1, 4, 20) + ); + assert_eq!( + TriggerFeel::Texture(50.0).effect(0.4), + trigger::vibration(1, 5, 10) + ); + assert_eq!(TriggerFeel::Texture(0.0).effect(0.4), TRIGGER_OFF); + assert_eq!(TriggerFeel::Texture(-1.0).effect(0.4), TRIGGER_OFF); s.adaptive_triggers = false; assert_eq!( s.trigger_effects(TriggerFeel::Click, TriggerFeel::Boost), diff --git a/crates/horizon-dualsense/src/trigger.rs b/crates/horizon-dualsense/src/trigger.rs index 6d9e82d89..973c3e30d 100644 --- a/crates/horizon-dualsense/src/trigger.rs +++ b/crates/horizon-dualsense/src/trigger.rs @@ -1,8 +1,10 @@ //! The adaptive triggers' effects in the pad's own encoding. A trigger's travel is cut into ten //! zones (0 at rest, 9 fully pulled); an effect names the zones it acts in and a strength, 1..8, -//! for each, three bits per zone. Modes as documented by Nielk1's trigger effect notes and -//! DualSenseX; [`crate::TRIGGER_OFF`] frees the trigger. -use crate::TriggerEffect; +//! for each, three bits per zone. Modes and packing as in Nielk1's TriggerEffectGenerator +//! (gist.github.com/Nielk1/6d54cc2c00d2201ccb8c2720ad7538db, rev. f73a299) and dualsensectl; +//! only the modes it calls official, which later firmware keeps. [`crate::TRIGGER_OFF`] frees +//! the trigger. +use crate::{TRIGGER_OFF, TriggerEffect}; const FEEDBACK: u8 = 0x21; const WEAPON: u8 = 0x25; @@ -58,8 +60,12 @@ pub fn weapon(start: u8, end: u8, strength: u8) -> TriggerEffect { e } -/// A buzz from zone `start` (0..9) on, at `amplitude` (1..8) and `frequency` Hz. +/// A buzz from zone `start` (0..9) on, at `amplitude` (1..8) and `frequency` Hz; at 0 Hz, +/// nothing (as Nielk1's factory). pub fn vibration(start: u8, amplitude: u8, frequency: u8) -> TriggerEffect { + if frequency == 0 { + return TRIGGER_OFF; + } let mut e = zoned( VIBRATION, std::array::from_fn(|i| { @@ -121,4 +127,22 @@ mod tests { ); assert_eq!(e[9], 30); } + + #[test] + fn reference_examples() { + // DSX++'s README, Weapon {2, 6, 8}: zones 2 and 6, strength 8 stored as 7. + assert_eq!( + weapon(2, 6, 8), + [0x25, 0x44, 0x00, 0x07, 0, 0, 0, 0, 0, 0, 0] + ); + // dualsensectl's wiki, `weapon 2 3 8`. + assert_eq!(weapon(2, 3, 8)[1..4], [0x0c, 0x00, 0x07]); + // DSX++'s README, Vibration {2, 8, 20}: zones 2..9, amplitude 8 (stored 7) in each + // zone's three bits, the frequency in byte 9. + assert_eq!( + vibration(2, 8, 20), + [0x26, 0xfc, 0x03, 0xc0, 0xff, 0xff, 0x3f, 0, 0, 20, 0] + ); + assert_eq!(vibration(2, 8, 0), TRIGGER_OFF); + } } diff --git a/docs/controllers.md b/docs/controllers.md index 76074a196..a33403938 100644 --- a/docs/controllers.md +++ b/docs/controllers.md @@ -331,20 +331,67 @@ options, `adaptiveTriggers:`). Each trigger feels the action bound to it, so rebinding moves the feel along: with the default mapping that is L2 for `key.use` and R2 for `key.attack`. -| While | Trigger | Feel | -|---|---|---| -| Drawing a bow | use | resistance from the first tenth of the travel, growing with the draw (`BowItem.getPowerForTime`) | -| Charging a trident | use | the same, full at the 10 ticks a throw needs | -| Loading a crossbow | use | the same, full when it is loaded | -| Holding a loaded crossbow | use | a gun trigger: resistance that gives way with a click, where it fires | -| Gliding with a rocket, riding a pig with a carrot on a stick or a strider with a warped fungus on a stick | use | a firm wall halfway: pulling through it boosts | -| Mining (survival) | attack | a light 30 Hz buzz, stronger on harder blocks (dirt 2, stone 3, deepslate 4, obsidian 5 of 8); none on blocks that break at once | +| While | Trigger | Effect (zones 0..9, strength 1..8) | Feel | +|---|---|---|---| +| Drawing a bow | use | feedback from zone 2, strength 1 rising to 6 with the draw (`BowItem.getPowerForTime`) | the string's tension builds; letting go frees the trigger at once | +| Charging a trident | use | the same, at 6 after the 10 ticks a throw needs | | +| Loading a crossbow | use | the same, at 6 when it is loaded | | +| Holding a loaded crossbow | use | weapon: from two zones before the press zone to the press zone, strength 8 | a gun trigger: a hard catch that breaks with a click as it fires | +| Gliding with a rocket, riding a pig or a strider with its stick | use | weapon, the same zones, strength 5 | a lighter catch: pulling through it is the boost | +| A shield in the use hand (raised or ready) | use | feedback from zone 2, strength 3 | its weight | +| That shield disabled by an axe | use | feedback from zone 1, strength 8 | the trigger locks until the shield recovers | +| Mining (survival) | attack | vibration from zone 1: amplitude 2 (dirt) to 5 (obsidian) of 8, 40 Hz on sand and dirt, 30 on stone, 20 on wood, deepslate and ores, 10 on obsidian | a grit that gets heavier and slower on harder blocks; none on blocks that break at once | + +The press zone is where Horizon presses the trigger's action: the trigger +threshold (0.4 by default) times ten, kept between 3 and 8, so with the +defaults a crossbow catches from zone 2 and breaks at zone 4, as its bolt +leaves. This assumes the ten zones split the travel evenly, as dualsensectl +documents them (0 at rest, 9 fully pressed); it is not measured. Everything else, and every menu, the pause screen, a window without focus, another pad being the active one, and controllers turned off, leaves the -triggers free; quitting frees them too. Effects are the pad's own modes -(feedback 0x21, weapon 0x25, vibration 0x26): ten zones along the travel, a -three-bit strength per zone. `pad status` shows the effects in use. +triggers free; quitting frees them too. `pad status` shows the effects in +use. + +Why these values, from what shipped games do and what the community +documents (none of it measured on Horizon's pad yet): + +- **Only the official modes.** Off 0x05, feedback 0x21, weapon 0x25 and + vibration 0x26 are the modes Nielk1's notes call official and expected in + future firmware; bow 0x22, galloping 0x23 and machine 0x27 "might be + removed", and the low "simple" modes (0x01, 0x02) that pydualsense and + DualSense-Windows send are leftovers. The bow mode's snap-back would suit a + bow, but a firmware update could take it away. +- **Bows build tension.** Kena: Bridge of Spirits and Astro's Playroom use + the triggers to make you feel the bow being drawn (PlayStation Blog's + DualSense round-up, as reported by + [GameSpot](https://www.gamespot.com/articles/sony-reveals-how-ps5s-dualsense-controller-works-with-ratchet-clank-returnal-scarlet-nexus-and-more/1100-6491469/)). Minecraft holds the draw for + as long as you like, so the tension stops at 6 of 8 (dualsensectl: 1 is + "almost no resistance", 8 the maximum), and starts after the first two + zones so the press that starts the draw stays light. +- **A loaded crossbow is a gun trigger.** The developer note GameSpot quotes + describes a bazooka whose resistance "will cause the trigger to catch at the + input position just before the bullet is fired, and as you continue to pull + the trigger, the bullet will fire and release the tension": the weapon + effect does exactly that, and Horizon moves its breaking point onto the + threshold so the click is the shot. DSX community presets use the same + shape (DSX++'s example `Weapon {2, 6, 8}`; dualsensectl's + `weapon 2 3 8` "resists strongly and lets go almost immediately"). + Returnal and Ratchet & Clank: Rift Apart use a half-press stop to split two + actions on one trigger; Minecraft has one action per trigger, so Horizon + doesn't. +- **Boosts click lighter.** A rocket or a pig's carrot fires on the pull, so + it gets a catch that gives way rather than a wall that never does (strength + 5, below the crossbow's 8). +- **A disabled shield locks the trigger**, as Deathloop locks R2 halfway + when a gun jams, "to give to the player an immediate feedback even before + the animation plays out" (its director, Dinga Bakaba, in + [Destructoid's report](https://www.destructoid.com/when-a-gun-jams-in-deathloop-the-ps5-controllers-triggers-will-lock-up/)): + the shield can't be raised, and the finger feels why. +- **Mining is a texture.** The vibration mode buzzes the trigger at a given + rate; reWASD's preset uses 30 Hz and its "rifle" 2..20 Hz. Softer blocks + get a finer, faster grit, harder ones a slower, heavier chug, never above + 5 of 8, since the attack trigger is held for seconds. Byte layout from public references, read for facts; no code is copied from them (the kernel and dualsensectl are GPL):