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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<horizon_dualsense::Link>,
|
||||
/// What it was last told to show.
|
||||
shown: Option<horizon_dualsense::device::State>,
|
||||
}
|
||||
|
||||
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,
|
||||
})),
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<bool> {
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
|
||||
@@ -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};
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+25
-1
@@ -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 |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user