Merge pull request #360 from ludovic111/feat/dualsense-triggers
DualSense adaptive triggers
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,99 @@ 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)
|
||||
}
|
||||
Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace,
|
||||
_ => 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, 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
|
||||
.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
|
||||
}
|
||||
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.
|
||||
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 +612,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,
|
||||
})),
|
||||
|
||||
@@ -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`;
|
||||
|
||||
@@ -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,91 @@ impl Rumble {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 catch that gives way where it fires.
|
||||
Click,
|
||||
/// Pulling it boosts (a rocket while gliding, the stick while riding a pig or a strider):
|
||||
/// 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 {
|
||||
/// 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(2, 1 + (5.0 * charge.clamp(0.0, 1.0)).round() as u8)
|
||||
}
|
||||
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);
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
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(self.trigger_threshold)
|
||||
})
|
||||
}
|
||||
}
|
||||
/// `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 +1312,59 @@ mod tests {
|
||||
assert_eq!(w.observe(Some(1.0)), Some(1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn triggers_feel_their_action() {
|
||||
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, trigger::feedback(2, 6));
|
||||
assert_eq!(right, trigger::vibration(1, 3, 30));
|
||||
assert_eq!(
|
||||
s.trigger_effects(TriggerFeel::Draw(0.0), TriggerFeel::Free),
|
||||
[trigger::feedback(2, 1), 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),
|
||||
[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();
|
||||
|
||||
@@ -103,6 +103,7 @@ pub const SECTIONS: &[Section] = &[
|
||||
[
|
||||
Opt::PadEnabled,
|
||||
Opt::PadVibration,
|
||||
Opt::PadAdaptiveTriggers,
|
||||
Opt::PadLookSensitivity,
|
||||
Opt::PadToggleSneak,
|
||||
Opt::PadInvertX,
|
||||
@@ -233,6 +234,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,148 @@
|
||||
//! 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 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;
|
||||
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; 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| {
|
||||
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);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
+72
-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,75 @@ 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 | 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. `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):
|
||||
|
||||
@@ -331,6 +401,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