mirror of
https://github.com/gamingwithhomie/larp.git
synced 2026-12-06 07:38:36 +00:00
Merge pull request #153
This commit is contained in:
@ -68,11 +68,72 @@ static CONFIG_REV: AtomicU64 = AtomicU64::new(0);
|
||||
static SENS_BITS: AtomicU32 = AtomicU32::new(0);
|
||||
static INVERT_Y: AtomicBool = AtomicBool::new(false);
|
||||
static WHEEL_INVERT: AtomicBool = AtomicBool::new(false);
|
||||
static VEHICLE_AUTO_CENTER: AtomicBool = AtomicBool::new(true);
|
||||
/// Vehicle camera auto-center mode (`vehicle_auto_center` / `LARP_VEHICLE_AUTO_CENTER`).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum AutoCenterMode {
|
||||
/// Stock title behavior: auto-center vehicle camera normally.
|
||||
#[default]
|
||||
On = 0,
|
||||
/// Hold camera free for 1.5 s after mouse motion, then auto-center smoothly (GTA V PC feel).
|
||||
Auto = 1,
|
||||
/// Never auto-center vehicle camera while in a vehicle.
|
||||
Off = 2,
|
||||
}
|
||||
|
||||
impl AutoCenterMode {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::On => "on",
|
||||
Self::Auto => "auto",
|
||||
Self::Off => "off",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ini_value(self) -> &'static str {
|
||||
match self {
|
||||
Self::On => "1",
|
||||
Self::Auto => "auto",
|
||||
Self::Off => "0",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_u8(v: u8) -> Self {
|
||||
match v {
|
||||
1 => Self::Auto,
|
||||
2 => Self::Off,
|
||||
_ => Self::On,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(v: &str) -> Option<Self> {
|
||||
match v.trim().to_ascii_lowercase().as_str() {
|
||||
"auto" | "delay" | "pc" => Some(Self::Auto),
|
||||
"0" | "off" | "false" | "no" => Some(Self::Off),
|
||||
"1" | "on" | "true" | "yes" => Some(Self::On),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static VEHICLE_AUTO_CENTER_MODE: AtomicU8 = AtomicU8::new(AutoCenterMode::On as u8);
|
||||
/// `keyboard_driving` / `LARP_KEYBOARD_DRIVING` (off by default; `crate::vehicle`).
|
||||
static KEYBOARD_DRIVING: AtomicBool = AtomicBool::new(false);
|
||||
/// The capture was let go because the world map opened; taken back when it closes.
|
||||
static MAP_RELEASED: AtomicBool = AtomicBool::new(false);
|
||||
/// True while the player holds `right_stick_mouse` (default Alt) under
|
||||
/// `LARP_MOUSE_CAMERA=direct` to steer the 360-degree Right Stick with the mouse.
|
||||
static RIGHT_STICK_MOUSE_ACTIVE: AtomicBool = AtomicBool::new(false);
|
||||
/// Cached map-open flag updated every 8 ms poll so high-rate `WM_INPUT` packets
|
||||
/// avoid calling `mapmouse::is_map_active()` during live captured gameplay (with
|
||||
/// a 50 ms staleness guard for hybrid `HOST_PAD_USED` sessions).
|
||||
static MAP_OPEN_HINT: AtomicBool = AtomicBool::new(false);
|
||||
static LAST_MAP_POLL_US: AtomicU64 = AtomicU64::new(0);
|
||||
const MAP_HINT_MAX_AGE_US: u64 = 50_000;
|
||||
|
||||
pub fn right_stick_mouse_active() -> bool {
|
||||
RIGHT_STICK_MOUSE_ACTIVE.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
const MIN_SENS: f32 = 0.05;
|
||||
const MAX_SENS: f32 = 10.0;
|
||||
@ -242,6 +303,20 @@ pub(crate) const CONTROLS: &[ControlMeta] = &[
|
||||
about: "Keyboard fallback for Left Trigger (RMB always aims/brakes).",
|
||||
default_text: "Z",
|
||||
},
|
||||
ControlMeta {
|
||||
key: "handbrake",
|
||||
aliases: &[],
|
||||
label: "Driving handbrake (A)",
|
||||
about: "Vehicle handbrake when keyboard_driving=1 (yields to X in Cinematic Camera for Auto Drive).",
|
||||
default_text: "Space",
|
||||
},
|
||||
ControlMeta {
|
||||
key: "right_stick_mouse",
|
||||
aliases: &["rightstickmouse"],
|
||||
label: "Mouse -> Right stick",
|
||||
about: "Hold under LARP_MOUSE_CAMERA=direct to steer the 360-degree Right Stick with mouse (minigames).",
|
||||
default_text: "Alt",
|
||||
},
|
||||
];
|
||||
|
||||
const MB_LEFT: u32 = 1 << 0;
|
||||
@ -261,6 +336,12 @@ struct Bindings {
|
||||
move_down: u8,
|
||||
move_left: u8,
|
||||
move_right: u8,
|
||||
/// Vehicle handbrake key when `keyboard_driving = 1` (default Space -> Xbox A,
|
||||
/// yielding back to Xbox X in Cinematic Camera so holding Space enables Auto Drive).
|
||||
handbrake_vk: u8,
|
||||
/// Modifier held under `LARP_MOUSE_CAMERA=direct` to steer the analog Right Stick
|
||||
/// with the mouse (carjacking, lockpicking, fishing; default Alt).
|
||||
right_stick_mouse_vk: u8,
|
||||
}
|
||||
|
||||
fn parse_vk(name: &str) -> Option<u8> {
|
||||
@ -438,7 +519,7 @@ struct LiveStore {
|
||||
sens: f32,
|
||||
invert_y: bool,
|
||||
wheel_invert: bool,
|
||||
vehicle_auto_center: bool,
|
||||
vehicle_auto_center: AutoCenterMode,
|
||||
keyboard_driving: bool,
|
||||
enabled: bool,
|
||||
}
|
||||
@ -546,6 +627,8 @@ fn build_bindings(custom: &HashMap<String, String>) -> Bindings {
|
||||
move_down: single_key(&["movedown", "move_down"], b'S'),
|
||||
move_left: single_key(&["moveleft", "move_left"], b'A'),
|
||||
move_right: single_key(&["moveright", "move_right"], b'D'),
|
||||
handbrake_vk: single_key(&["handbrake"], 0x20),
|
||||
right_stick_mouse_vk: single_key(&["rightstickmouse", "right_stick_mouse"], 0x12),
|
||||
}
|
||||
}
|
||||
|
||||
@ -601,7 +684,8 @@ fn sync_atomics(s: &LiveStore) {
|
||||
SENS_BITS.store(s.sens.to_bits(), Ordering::Relaxed);
|
||||
INVERT_Y.store(s.invert_y, Ordering::Relaxed);
|
||||
WHEEL_INVERT.store(s.wheel_invert, Ordering::Relaxed);
|
||||
VEHICLE_AUTO_CENTER.store(s.vehicle_auto_center, Ordering::Relaxed);
|
||||
VEHICLE_AUTO_CENTER_MODE.store(s.vehicle_auto_center as u8, Ordering::Relaxed);
|
||||
crate::vehicle::set_auto_center_mode(s.vehicle_auto_center);
|
||||
KEYBOARD_DRIVING.store(s.keyboard_driving, Ordering::Relaxed);
|
||||
MNK_LIVE_ENABLED.store(s.enabled, Ordering::Relaxed);
|
||||
CONFIG_REV.fetch_add(1, Ordering::Release);
|
||||
@ -625,7 +709,9 @@ fn store() -> &'static Mutex<LiveStore> {
|
||||
.is_ok_and(|v| v.trim() == "1");
|
||||
let mut auto_center = std::env::var("LARP_VEHICLE_AUTO_CENTER")
|
||||
.or_else(|_| std::env::var("DARNER_VEHICLE_AUTO_CENTER"))
|
||||
.map_or(true, |v| v.trim() != "0");
|
||||
.ok()
|
||||
.and_then(|v| AutoCenterMode::parse(&v))
|
||||
.unwrap_or(AutoCenterMode::On);
|
||||
let mut keyboard_driving =
|
||||
std::env::var("LARP_KEYBOARD_DRIVING").is_ok_and(|v| v.trim() == "1");
|
||||
let mut enabled = controls_mode() != ControlsMode::Pad;
|
||||
@ -689,8 +775,10 @@ fn store() -> &'static Mutex<LiveStore> {
|
||||
if let Some(v) = custom.get("wheel_invert") {
|
||||
wheel_inv = v == "1" || v.eq_ignore_ascii_case("true") || v.eq_ignore_ascii_case("on");
|
||||
}
|
||||
if let Some(v) = custom.get("vehicle_auto_center") {
|
||||
auto_center = !(v == "0" || v.eq_ignore_ascii_case("false") || v.eq_ignore_ascii_case("off"));
|
||||
if let Some(v) = custom.get("vehicle_auto_center")
|
||||
&& let Some(mode) = AutoCenterMode::parse(v)
|
||||
{
|
||||
auto_center = mode;
|
||||
}
|
||||
if controls_mode() == ControlsMode::Auto
|
||||
&& let Some(v) = custom.get("enabled")
|
||||
@ -838,10 +926,10 @@ pub(crate) fn set_wheel_invert(on: bool) -> Result<PathBuf, String> {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn set_vehicle_auto_center(on: bool) -> Result<PathBuf, String> {
|
||||
pub(crate) fn set_vehicle_auto_center_mode(mode: AutoCenterMode) -> Result<PathBuf, String> {
|
||||
mutate_and_save(|s| {
|
||||
s.vehicle_auto_center = on;
|
||||
let val = if on { "1" } else { "0" };
|
||||
s.vehicle_auto_center = mode;
|
||||
let val = mode.ini_value();
|
||||
s.entries
|
||||
.insert("vehicle_auto_center".to_owned(), val.to_owned());
|
||||
s.raw_text = upsert_ini_key(&s.raw_text, "vehicle_auto_center", &[], val);
|
||||
@ -936,7 +1024,7 @@ pub(crate) fn reset_defaults() -> Result<PathBuf, String> {
|
||||
s.sens = 1.0;
|
||||
s.invert_y = false;
|
||||
s.wheel_invert = false;
|
||||
s.vehicle_auto_center = true;
|
||||
s.vehicle_auto_center = AutoCenterMode::On;
|
||||
s.enabled = controls_mode() != ControlsMode::Pad;
|
||||
})
|
||||
}
|
||||
@ -954,7 +1042,7 @@ pub(crate) fn reset_standard_fps() -> Result<PathBuf, String> {
|
||||
s.sens = 1.0;
|
||||
s.invert_y = false;
|
||||
s.wheel_invert = false;
|
||||
s.vehicle_auto_center = true;
|
||||
s.vehicle_auto_center = AutoCenterMode::On;
|
||||
s.enabled = controls_mode() != ControlsMode::Pad;
|
||||
})
|
||||
}
|
||||
@ -1055,12 +1143,15 @@ pub fn look_sensitivity() -> f32 {
|
||||
f32::from_bits(SENS_BITS.load(Ordering::Relaxed)).max(0.01)
|
||||
}
|
||||
|
||||
/// Whether the title's vehicle camera auto-center stays (`vehicle_auto_center`,
|
||||
/// `LARP_VEHICLE_AUTO_CENTER`; on by default). Off, `crate::vehicle` holds
|
||||
/// the camera where the player left it.
|
||||
pub fn vehicle_auto_center() -> bool {
|
||||
/// Vehicle camera auto-center mode (`vehicle_auto_center`, `LARP_VEHICLE_AUTO_CENTER`).
|
||||
pub fn vehicle_auto_center_mode() -> AutoCenterMode {
|
||||
let _ = load_config();
|
||||
VEHICLE_AUTO_CENTER.load(Ordering::Relaxed)
|
||||
AutoCenterMode::from_u8(VEHICLE_AUTO_CENTER_MODE.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
/// Whether the title's vehicle camera auto-center is enabled (`on` or `auto`).
|
||||
pub fn vehicle_auto_center() -> bool {
|
||||
vehicle_auto_center_mode() != AutoCenterMode::Off
|
||||
}
|
||||
|
||||
/// Whether keyboard driving is asked for (`keyboard_driving`,
|
||||
@ -1124,6 +1215,7 @@ pub fn set_capture(hwnd: windows_sys::Win32::Foundation::HWND, capture: bool) {
|
||||
ClipCursor(std::ptr::null());
|
||||
while ShowCursor(1) < 0 {}
|
||||
MOUSE_BUTTONS.store(0, Ordering::Relaxed);
|
||||
RIGHT_STICK_MOUSE_ACTIVE.store(false, Ordering::Relaxed);
|
||||
discard_motion();
|
||||
tracing::debug!("mouse: released");
|
||||
}
|
||||
@ -1152,10 +1244,22 @@ pub fn handle_window_message(
|
||||
register_raw_mouse(hwnd);
|
||||
}
|
||||
|
||||
// The world map (`crate::mapmouse`, build 1.0.2.1): while it is open the
|
||||
// mouse is let go and its messages drive the map; the capture comes back
|
||||
// with the first mouse move after the map closes.
|
||||
let on_map = crate::mapmouse::is_map_active();
|
||||
// Fast path for high-rate captured `WM_INPUT` packets: skip `mapmouse::is_map_active()`
|
||||
// when the 8 ms poll recently confirmed the map is closed.
|
||||
let hint_fresh = {
|
||||
let last = LAST_MAP_POLL_US.load(Ordering::Acquire);
|
||||
last != 0 && super::now().saturating_sub(last) <= MAP_HINT_MAX_AGE_US
|
||||
};
|
||||
let on_map = if message == wm::WM_INPUT
|
||||
&& CAPTURED.load(Ordering::Relaxed)
|
||||
&& !MAP_RELEASED.load(Ordering::Relaxed)
|
||||
&& hint_fresh
|
||||
&& !MAP_OPEN_HINT.load(Ordering::Relaxed)
|
||||
{
|
||||
false
|
||||
} else {
|
||||
crate::mapmouse::is_map_active()
|
||||
};
|
||||
if on_map {
|
||||
if CAPTURED.load(Ordering::Relaxed) {
|
||||
set_capture(hwnd, false);
|
||||
@ -1275,7 +1379,8 @@ pub fn handle_window_message(
|
||||
}
|
||||
wm::WM_KEYDOWN => {
|
||||
// F10 releases mouse capture cleanly; F5 toggles force on-foot mode,
|
||||
// F6 toggles cursor capture, and '[' / ']' adjust look sensitivity live.
|
||||
// F6 toggles cursor capture, F8 cycles vehicle camera auto-center when hooked,
|
||||
// and '[' / ']' adjust look sensitivity live.
|
||||
let is_repeat = (lparam & (1 << 30)) != 0;
|
||||
if wparam == 0x79 /* VK_F10 */ {
|
||||
set_capture(hwnd, false);
|
||||
@ -1291,6 +1396,12 @@ pub fn handle_window_message(
|
||||
let next = !CAPTURED.load(Ordering::Relaxed);
|
||||
set_capture(hwnd, next);
|
||||
return true;
|
||||
} else if wparam == 0x77 /* VK_F8 */
|
||||
&& crate::vehicle::auto_center_hook_active()
|
||||
{
|
||||
let next = crate::vehicle::cycle_auto_center();
|
||||
let _ = set_vehicle_auto_center_mode(next);
|
||||
return true;
|
||||
} else if CAPTURED.load(Ordering::Relaxed) {
|
||||
if wparam == 0xDB /* '[' */ {
|
||||
let next = adjust_sensitivity(1.0 / 1.15);
|
||||
@ -1328,8 +1439,13 @@ pub fn handle_window_message(
|
||||
// Relative motion
|
||||
RAW_DX.fetch_add(m.lLastX, Ordering::Relaxed);
|
||||
RAW_DY.fetch_add(m.lLastY, Ordering::Relaxed);
|
||||
if m.lLastX != 0 || m.lLastY != 0 {
|
||||
crate::vehicle::note_mouse_motion();
|
||||
}
|
||||
// The direct camera's share (LARP_MOUSE_CAMERA=direct; nothing otherwise).
|
||||
crate::mousehook::add_motion(m.lLastX, m.lLastY);
|
||||
if !RIGHT_STICK_MOUSE_ACTIVE.load(Ordering::Relaxed) {
|
||||
crate::mousehook::add_motion(m.lLastX, m.lLastY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1412,16 +1528,35 @@ fn bound_vk_held(keys: &[KeyState], mb: u32, vk: u8) -> bool {
|
||||
/// (useful in scripted vehicle seats where the game expects normal prompts).
|
||||
static FORCE_ON_FOOT_MODE: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
fn eval_buttons(cfg: &Bindings, keys: &[KeyState], mb: u32, driving: bool) -> u32 {
|
||||
// At the wheel Shift / Ctrl lean and Space acts as the A handbrake
|
||||
// (GTA VI default) instead of pressing their on-foot bindings.
|
||||
let driving_reserved = |vk: u8| matches!(vk, 0x10 | 0x11 | 0x20 | 0xA0..=0xA3);
|
||||
fn normalize_unit_circle(x: f32, y: f32) -> (f32, f32) {
|
||||
let len = x.hypot(y);
|
||||
if len > 1.0 { (x / len, y / len) } else { (x, y) }
|
||||
}
|
||||
|
||||
fn eval_buttons(
|
||||
cfg: &Bindings,
|
||||
keys: &[KeyState],
|
||||
mb: u32,
|
||||
driving: bool,
|
||||
cinematic: bool,
|
||||
direct_active: bool,
|
||||
) -> u32 {
|
||||
// At the wheel Shift / Ctrl lean, and the configurable handbrake key (default
|
||||
// Space) acts as Xbox A handbrake instead of its on-foot binding — yielding
|
||||
// back to its on-foot binding (Xbox X) in Cinematic Camera so holding Space
|
||||
// enables/disables Auto Drive as prompted on screen.
|
||||
let lean_key = |vk: u8| matches!(vk, 0x10 | 0x11 | 0xA0..=0xA3);
|
||||
let handbrake_active = driving && !cinematic && cfg.handbrake_vk != 0;
|
||||
let mut buttons = cfg
|
||||
.buttons
|
||||
.iter()
|
||||
.filter(|&&(vk, _)| !(driving && driving_reserved(vk)) && bound_vk_held(keys, mb, vk))
|
||||
.filter(|&&(vk, _)| {
|
||||
!(driving && lean_key(vk))
|
||||
&& !(handbrake_active && vk == cfg.handbrake_vk)
|
||||
&& bound_vk_held(keys, mb, vk)
|
||||
})
|
||||
.fold(0u32, |all, &(_, bit)| all | bit);
|
||||
if driving && key_held(keys, 0x20) {
|
||||
if handbrake_active && bound_vk_held(keys, mb, cfg.handbrake_vk) {
|
||||
buttons |= A;
|
||||
}
|
||||
|
||||
@ -1430,7 +1565,10 @@ fn eval_buttons(cfg: &Bindings, keys: &[KeyState], mb: u32, driving: bool) -> u3
|
||||
}
|
||||
|
||||
let mouse_claimed = |vk: u8| {
|
||||
cfg.buttons.iter().any(|&(k, _)| k == vk) || cfg.focus_vks.contains(&vk)
|
||||
cfg.buttons.iter().any(|&(k, _)| k == vk)
|
||||
|| cfg.focus_vks.contains(&vk)
|
||||
|| (handbrake_active && cfg.handbrake_vk == vk)
|
||||
|| (direct_active && cfg.right_stick_mouse_vk == vk)
|
||||
};
|
||||
if mb & MB_MIDDLE != 0 && !mouse_claimed(0x04) {
|
||||
buttons |= RIGHT_STICK;
|
||||
@ -1449,6 +1587,7 @@ fn apply_driving_overrides(
|
||||
keys: &[KeyState],
|
||||
mb: u32,
|
||||
radial_wheel_active: bool,
|
||||
cinematic: bool,
|
||||
buttons: &mut u32,
|
||||
left_trigger: &mut f32,
|
||||
right_trigger: &mut f32,
|
||||
@ -1461,14 +1600,21 @@ fn apply_driving_overrides(
|
||||
0.0
|
||||
}
|
||||
};
|
||||
// Move up / down are gas / brake, Shift / Ctrl lean, Space is A handbrake
|
||||
// (in `eval_buttons`), and LMB fires RB without RMB strobing the LB wheel.
|
||||
*left_trigger = on(cfg.left_trigger_vk).max(on(cfg.move_down));
|
||||
*right_trigger = on(cfg.right_trigger_vk).max(on(cfg.move_up));
|
||||
*left_y = on(0x10) - on(0x11);
|
||||
if !radial_wheel_active && (mb & MB_LEFT) != 0 {
|
||||
*buttons |= RIGHT_SHOULDER;
|
||||
// Move up / down are gas / brake, Shift / Ctrl lean, and the handbrake key
|
||||
// sends A (in `eval_buttons`). In normal driving, LMB fires RB without RMB
|
||||
// strobing the LB wheel; in Cinematic Camera, RMB / LMB stay LT / RT so
|
||||
// `SPEED 1 / 2 / 3 [RMB] [LMB]` prompts work directly alongside W / S.
|
||||
if cinematic {
|
||||
*left_trigger = (*left_trigger).max(on(cfg.move_down));
|
||||
*right_trigger = (*right_trigger).max(on(cfg.move_up));
|
||||
} else {
|
||||
*left_trigger = on(cfg.left_trigger_vk).max(on(cfg.move_down));
|
||||
*right_trigger = on(cfg.right_trigger_vk).max(on(cfg.move_up));
|
||||
if !radial_wheel_active && (mb & MB_LEFT) != 0 {
|
||||
*buttons |= RIGHT_SHOULDER;
|
||||
}
|
||||
}
|
||||
*left_y = on(0x10) - on(0x11);
|
||||
}
|
||||
|
||||
/// Builds a `PadState` from PC Mouse & Keyboard when `LARP_CONTROLS=mnk` or mouse is captured.
|
||||
@ -1482,16 +1628,37 @@ pub(super) fn mnk_pad(keys: &[KeyState]) -> PadState {
|
||||
}
|
||||
};
|
||||
|
||||
let mb = if CAPTURED.load(Ordering::Relaxed) {
|
||||
let direct_active = crate::mousehook::active();
|
||||
let captured = CAPTURED.load(Ordering::Relaxed);
|
||||
MAP_OPEN_HINT.store(crate::mapmouse::is_map_active(), Ordering::Relaxed);
|
||||
LAST_MAP_POLL_US.store(super::now(), Ordering::Release);
|
||||
let wheel_open = crate::mousehook::refresh_radial_wheel_cache();
|
||||
|
||||
let mb = if captured {
|
||||
MOUSE_BUTTONS.load(Ordering::Relaxed)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// Right-Stick mouse hold (default Alt) for carjacking / lockpicking / fishing
|
||||
// under `LARP_MOUSE_CAMERA=direct`: temporarily pauses direct camera look and
|
||||
// steers the 360-degree analog right stick directly with the mouse.
|
||||
let rs_mouse_held = direct_active
|
||||
&& captured
|
||||
&& cfg.right_stick_mouse_vk != 0
|
||||
&& bound_vk_held(keys, mb, cfg.right_stick_mouse_vk)
|
||||
&& !key_held(keys, 0x0D) // don't collide with Alt+Enter
|
||||
&& !key_held(keys, 0x09); // don't collide with Alt+Tab
|
||||
let was_rs_mouse = RIGHT_STICK_MOUSE_ACTIVE.swap(rs_mouse_held, Ordering::Relaxed);
|
||||
if rs_mouse_held && !was_rs_mouse {
|
||||
crate::mousehook::clear_motion();
|
||||
}
|
||||
|
||||
let driving = !FORCE_ON_FOOT_MODE.load(Ordering::Relaxed)
|
||||
&& crate::vehicle::keyboard_driving()
|
||||
&& crate::vehicle::seat() == crate::vehicle::Seat::Driver;
|
||||
let mut buttons = eval_buttons(&cfg, keys, mb, driving);
|
||||
let cinematic = driving && crate::vehicle::is_cinematic_camera_active();
|
||||
let mut buttons = eval_buttons(&cfg, keys, mb, driving, cinematic, direct_active);
|
||||
|
||||
let wheel = WHEEL_ACCUM.swap(0, Ordering::Relaxed);
|
||||
if wheel > 0 {
|
||||
@ -1508,7 +1675,8 @@ pub(super) fn mnk_pad(keys: &[KeyState]) -> PadState {
|
||||
&cfg,
|
||||
keys,
|
||||
mb,
|
||||
crate::mousehook::is_radial_wheel_active(),
|
||||
wheel_open,
|
||||
cinematic,
|
||||
&mut buttons,
|
||||
&mut left_trigger,
|
||||
&mut right_trigger,
|
||||
@ -1541,19 +1709,18 @@ pub(super) fn mnk_pad(keys: &[KeyState]) -> PadState {
|
||||
let dy = RAW_DY.swap(0, Ordering::Relaxed) as f32;
|
||||
let sens = f32::from_bits(SENS_BITS.load(Ordering::Relaxed)).max(0.01);
|
||||
// With the direct camera (LARP_MOUSE_CAMERA=direct) live, the mouse turns the camera
|
||||
// itself (`mousehook`), not the stick; in menus, the map and the weapon wheel it stays here.
|
||||
let (mut rx, mut ry) = if crate::mousehook::camera_live() {
|
||||
// itself (`mousehook`), not the stick; in menus, the map, the weapon wheel, and while
|
||||
// holding `right_stick_mouse` (Alt) for minigames it stays here.
|
||||
let (mut rx, mut ry) = if !rs_mouse_held && crate::mousehook::camera_live() {
|
||||
(0.0, 0.0)
|
||||
} else {
|
||||
smooth_mouse_stick(dx, dy, sens, INVERT_Y.load(Ordering::Relaxed))
|
||||
};
|
||||
|
||||
// Keyboard arrow-style fallback for right stick (I/J/K/L) if mouse isn't moving
|
||||
if rx == 0.0 {
|
||||
rx = on(b'L') - on(b'J');
|
||||
}
|
||||
if ry == 0.0 {
|
||||
ry = on(b'I') - on(b'K');
|
||||
// Keyboard arrow-style fallback for right stick (I/J/K/L) normalized to unit circle
|
||||
// so diagonal minigame sweeps stay circular instead of sqrt(2) corners.
|
||||
if rx == 0.0 && ry == 0.0 {
|
||||
(rx, ry) = normalize_unit_circle(on(b'L') - on(b'J'), on(b'I') - on(b'K'));
|
||||
}
|
||||
|
||||
PadState {
|
||||
@ -1623,6 +1790,8 @@ mod tests {
|
||||
move_down: b'S',
|
||||
move_left: b'A',
|
||||
move_right: b'D',
|
||||
handbrake_vk: 0x20,
|
||||
right_stick_mouse_vk: 0x12,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1717,14 +1886,14 @@ mod tests {
|
||||
fn focus_presses_ls_and_rs_and_mouse4_override_suppresses_default_lb() {
|
||||
// Default: CapsLock presses LS + RS, while Mouse4 keeps default LB.
|
||||
let def = build_bindings(&HashMap::new());
|
||||
assert_eq!(eval_buttons(&def, &[ks(0x14)], 0, false), LEFT_STICK | RIGHT_STICK);
|
||||
assert_eq!(eval_buttons(&def, &[], MB_X1, false), LEFT_SHOULDER);
|
||||
assert_eq!(eval_buttons(&def, &[ks(0x14)], 0, false, false, false), LEFT_STICK | RIGHT_STICK);
|
||||
assert_eq!(eval_buttons(&def, &[], MB_X1, false, false, false), LEFT_SHOULDER);
|
||||
|
||||
// Rebound to Mouse4: Mouse4 presses LS + RS only (does not also trigger LB).
|
||||
let map = parse_ini_text("focus = Mouse4\n");
|
||||
let b = build_bindings(&map);
|
||||
assert_eq!(eval_buttons(&b, &[], MB_X1, false), LEFT_STICK | RIGHT_STICK);
|
||||
assert_eq!(eval_buttons(&b, &[], MB_X2, false), RIGHT_SHOULDER);
|
||||
assert_eq!(eval_buttons(&b, &[], MB_X1, false, false, false), LEFT_STICK | RIGHT_STICK);
|
||||
assert_eq!(eval_buttons(&b, &[], MB_X2, false, false, false), RIGHT_SHOULDER);
|
||||
assert_eq!(parse_vk("Mouse4"), Some(0x05));
|
||||
assert_eq!(parse_vk("Mouse5"), Some(0x06));
|
||||
assert_eq!(vk_name(0x05), "Mouse4");
|
||||
@ -1734,7 +1903,7 @@ mod tests {
|
||||
#[test]
|
||||
fn driving_space_maps_to_a_handbrake_without_on_foot_binding() {
|
||||
let cfg = default_bindings();
|
||||
let buttons = eval_buttons(&cfg, &[ks(0x20)], 0, true);
|
||||
let buttons = eval_buttons(&cfg, &[ks(0x20)], 0, true, false, false);
|
||||
assert_ne!(buttons & A, 0, "Space should press A handbrake while driving");
|
||||
assert_eq!(buttons & X, 0, "Space must not also trigger on-foot X binding");
|
||||
}
|
||||
@ -1743,30 +1912,71 @@ mod tests {
|
||||
fn driving_rmb_does_not_strobe_lb_while_lmb_fires_rb() {
|
||||
let cfg = default_bindings();
|
||||
let (mut buttons, mut lt, mut rt, mut ly) = (0u32, 1.0, 0.0, 0.0);
|
||||
apply_driving_overrides(&cfg, &[], MB_RIGHT, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
apply_driving_overrides(&cfg, &[], MB_RIGHT, false, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
assert_eq!(buttons & LEFT_SHOULDER, 0, "RMB must not press LB while driving");
|
||||
assert_eq!(lt, 0.0, "RMB must not apply brake trigger in keyboard driving");
|
||||
|
||||
apply_driving_overrides(&cfg, &[], MB_LEFT, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
apply_driving_overrides(&cfg, &[], MB_LEFT, false, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
assert_ne!(buttons & RIGHT_SHOULDER, 0, "LMB should press RB while driving");
|
||||
|
||||
let mut wheel_buttons = 0u32;
|
||||
apply_driving_overrides(&cfg, &[], MB_LEFT, true, &mut wheel_buttons, &mut lt, &mut rt, &mut ly);
|
||||
apply_driving_overrides(&cfg, &[], MB_LEFT, true, false, &mut wheel_buttons, &mut lt, &mut rt, &mut ly);
|
||||
assert_eq!(
|
||||
wheel_buttons & RIGHT_SHOULDER,
|
||||
0,
|
||||
"LMB must not press RB while the radial wheel is open"
|
||||
);
|
||||
|
||||
// In Cinematic Camera, RMB / LMB stay LT / RT for Auto Drive speed controls.
|
||||
let (mut cin_buttons, mut cin_lt, mut cin_rt, mut cin_ly) = (0u32, 1.0, 1.0, 0.0);
|
||||
apply_driving_overrides(&cfg, &[], MB_LEFT | MB_RIGHT, false, true, &mut cin_buttons, &mut cin_lt, &mut cin_rt, &mut cin_ly);
|
||||
assert_eq!((cin_buttons, cin_lt, cin_rt), (0, 1.0, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn driving_preserves_gas_brake_and_motorcycle_lean() {
|
||||
let cfg = default_bindings();
|
||||
let keys = [ks(b'W'), ks(0xA0)];
|
||||
let mut buttons = eval_buttons(&cfg, &keys, 0, true);
|
||||
let mut buttons = eval_buttons(&cfg, &keys, 0, true, false, false);
|
||||
let (mut lt, mut rt, mut ly) = (0.0, 0.0, 0.0);
|
||||
apply_driving_overrides(&cfg, &keys, 0, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
apply_driving_overrides(&cfg, &keys, 0, false, false, &mut buttons, &mut lt, &mut rt, &mut ly);
|
||||
assert_eq!((lt, rt, ly), (0.0, 1.0, 1.0));
|
||||
assert_eq!(buttons & A, 0, "Shift lean should suppress on-foot A binding");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn driving_handbrake_sends_a_normally_and_yields_to_x_in_cinematic_mode() {
|
||||
let mut cfg = default_bindings();
|
||||
cfg.buttons.push((b'G', X));
|
||||
let space = [ks(0x20)];
|
||||
// On foot: Space remains X.
|
||||
assert_eq!(eval_buttons(&cfg, &space, 0, false, false, false), X);
|
||||
// Normal driving: Space becomes A (Handbrake) and suppresses X.
|
||||
assert_eq!(eval_buttons(&cfg, &space, 0, true, false, false), A);
|
||||
// Normal driving with secondary X key held simultaneously: keeps both A and X.
|
||||
let space_and_g = [ks(0x20), ks(b'G')];
|
||||
assert_eq!(eval_buttons(&cfg, &space_and_g, 0, true, false, false), A | X);
|
||||
// Cinematic camera in vehicle: Space yields back to X (Enable Auto Drive).
|
||||
assert_eq!(eval_buttons(&cfg, &space, 0, true, true, false), X);
|
||||
// Mouse4 bound to handbrake: stays LB on foot, pulls pure handbrake (A) while driving.
|
||||
let mut mb_cfg = default_bindings();
|
||||
mb_cfg.handbrake_vk = 0x05;
|
||||
assert_eq!(eval_buttons(&mb_cfg, &[], MB_X1, false, false, false), LEFT_SHOULDER);
|
||||
assert_eq!(eval_buttons(&mb_cfg, &[], MB_X1, true, false, false), A);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_center_mode_parses_all_aliases_and_normalizes_right_stick() {
|
||||
assert_eq!(AutoCenterMode::parse("auto"), Some(AutoCenterMode::Auto));
|
||||
assert_eq!(AutoCenterMode::parse("pc"), Some(AutoCenterMode::Auto));
|
||||
assert_eq!(AutoCenterMode::parse("0"), Some(AutoCenterMode::Off));
|
||||
assert_eq!(AutoCenterMode::parse("off"), Some(AutoCenterMode::Off));
|
||||
assert_eq!(AutoCenterMode::parse("1"), Some(AutoCenterMode::On));
|
||||
assert_eq!(AutoCenterMode::parse("true"), Some(AutoCenterMode::On));
|
||||
assert_eq!(AutoCenterMode::parse("invalid"), None);
|
||||
|
||||
assert_eq!(normalize_unit_circle(1.0, 0.0), (1.0, 0.0));
|
||||
let (dx, dy) = normalize_unit_circle(1.0, 1.0);
|
||||
assert!((dx.hypot(dy) - 1.0).abs() < 1e-5);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
//! prologue matches that build's bytes.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::atomic::{AtomicI32, AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
@ -49,10 +49,16 @@ const DEFAULT_CAMERA_SENS: f32 = 0.0015;
|
||||
const LIVE_US: u64 = 100_000;
|
||||
/// A second look camera that asks within this time gets the same motion.
|
||||
const BLEND_US: u64 = 1_500;
|
||||
/// Maximum age of the cached radial-wheel hint before falling back to a live HUD check.
|
||||
const RADIAL_HINT_MAX_AGE_US: u64 = 50_000;
|
||||
|
||||
/// The image, once the look hook is in (0: not hooked).
|
||||
static BASE: AtomicUsize = AtomicUsize::new(0);
|
||||
static AFTER: AtomicUsize = AtomicUsize::new(0);
|
||||
/// Cached radial-wheel flag refreshed on the 8 ms poll so `look_returned` avoids
|
||||
/// walking the HUD table on the camera thread.
|
||||
static RADIAL_WHEEL_CACHED: AtomicBool = AtomicBool::new(false);
|
||||
static RADIAL_WHEEL_CACHED_US: AtomicU64 = AtomicU64::new(0);
|
||||
/// Mouse counts not yet given to the camera.
|
||||
static CAM_DX: AtomicI32 = AtomicI32::new(0);
|
||||
static CAM_DY: AtomicI32 = AtomicI32::new(0);
|
||||
@ -108,6 +114,28 @@ fn bytes(base: usize, rva: usize, n: usize) -> Vec<u8> {
|
||||
unsafe { std::slice::from_raw_parts((base + rva) as *const u8, n) }.to_vec()
|
||||
}
|
||||
|
||||
/// Whether the direct 1:1 mouse camera hook is installed (`LARP_MOUSE_CAMERA=direct`).
|
||||
pub fn active() -> bool {
|
||||
BASE.load(Ordering::Acquire) != 0
|
||||
}
|
||||
|
||||
/// Refreshes the cached radial-wheel state from the 8 ms input poll thread.
|
||||
pub fn refresh_radial_wheel_cache() -> bool {
|
||||
let open = is_radial_wheel_active();
|
||||
RADIAL_WHEEL_CACHED.store(open, Ordering::Relaxed);
|
||||
RADIAL_WHEEL_CACHED_US.store(now_us(), Ordering::Release);
|
||||
open
|
||||
}
|
||||
|
||||
fn cached_radial_wheel_active() -> bool {
|
||||
let last = RADIAL_WHEEL_CACHED_US.load(Ordering::Acquire);
|
||||
if last != 0 && now_us().saturating_sub(last) <= RADIAL_HINT_MAX_AGE_US {
|
||||
RADIAL_WHEEL_CACHED.load(Ordering::Relaxed)
|
||||
} else {
|
||||
is_radial_wheel_active()
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw mouse motion (counts), from `WM_INPUT` while the mouse is captured.
|
||||
pub fn add_motion(dx: i32, dy: i32) {
|
||||
if BASE.load(Ordering::Relaxed) != 0 {
|
||||
@ -126,11 +154,11 @@ pub fn clear_motion() {
|
||||
/// title asked for the camera lately, and no weapon / radio wheel is open.
|
||||
/// Otherwise the mouse goes to the right stick.
|
||||
pub fn camera_live() -> bool {
|
||||
if BASE.load(Ordering::Relaxed) == 0 {
|
||||
if BASE.load(Ordering::Relaxed) == 0 || mouse::right_stick_mouse_active() {
|
||||
return false;
|
||||
}
|
||||
let last = LAST_LOOK_US.load(Ordering::Relaxed);
|
||||
last != 0 && now_us().saturating_sub(last) < LIVE_US && !is_radial_wheel_active()
|
||||
last != 0 && now_us().saturating_sub(last) < LIVE_US && !cached_radial_wheel_active()
|
||||
}
|
||||
|
||||
/// The motion since the last hand-out; repeated for the second camera that
|
||||
@ -174,7 +202,7 @@ unsafe extern "C" fn look_returned(_rax: u64, slot: *mut u64) {
|
||||
LAST_LOOK_US.store(now_us(), Ordering::Relaxed);
|
||||
return;
|
||||
}
|
||||
if is_radial_wheel_active() {
|
||||
if cached_radial_wheel_active() || mouse::right_stick_mouse_active() {
|
||||
clear_motion();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -37,7 +37,12 @@ const ON_OFF: &[(&str, &str)] = &[("1", "menu.choice.on"), ("0", "menu.choice.of
|
||||
|
||||
/// Every start option, by its key in `LAYOUT` (`+key`).
|
||||
pub(super) const STARTS: &[Start] = &[
|
||||
Start { key: "vehicle_auto_center", store: Store::Controls, values: ON_OFF, first: Some("LARP_VEHICLE_AUTO_CENTER") },
|
||||
Start {
|
||||
key: "vehicle_auto_center",
|
||||
store: Store::Controls,
|
||||
values: &[("1", "menu.choice.on"), ("auto", "menu.choice.auto"), ("0", "menu.choice.off")],
|
||||
first: Some("LARP_VEHICLE_AUTO_CENTER"),
|
||||
},
|
||||
Start {
|
||||
key: "sensitivity",
|
||||
store: Store::Controls,
|
||||
|
||||
@ -478,11 +478,8 @@ fn act_on(s: &mut State, act: u32) {
|
||||
|
||||
fn control_choice_count(row: ControlRow) -> usize {
|
||||
match row {
|
||||
ControlRow::Enabled
|
||||
| ControlRow::InvertY
|
||||
| ControlRow::WheelInvert
|
||||
| ControlRow::VehicleAutoCenter => 2,
|
||||
ControlRow::ResetDefaults => 3,
|
||||
ControlRow::Enabled | ControlRow::InvertY | ControlRow::WheelInvert => 2,
|
||||
ControlRow::VehicleAutoCenter | ControlRow::ResetDefaults => 3,
|
||||
ControlRow::Sensitivity => SENS_STEPS.len(),
|
||||
ControlRow::Bind(_) => 0,
|
||||
}
|
||||
@ -494,7 +491,11 @@ fn control_current_edit_idx(row: ControlRow) -> usize {
|
||||
ControlRow::Sensitivity => nearest_sens_idx(mouse::look_sensitivity()),
|
||||
ControlRow::InvertY => usize::from(mouse::look_invert_y()),
|
||||
ControlRow::WheelInvert => usize::from(mouse::wheel_invert()),
|
||||
ControlRow::VehicleAutoCenter => usize::from(mouse::vehicle_auto_center()),
|
||||
ControlRow::VehicleAutoCenter => match mouse::vehicle_auto_center_mode() {
|
||||
mouse::AutoCenterMode::Off => 0,
|
||||
mouse::AutoCenterMode::Auto => 1,
|
||||
mouse::AutoCenterMode::On => 2,
|
||||
},
|
||||
ControlRow::ResetDefaults => 0, // 0 = cancel, 1 = Standard, 2 = Standard FPS
|
||||
ControlRow::Bind(_) => 0,
|
||||
}
|
||||
@ -502,16 +503,18 @@ fn control_current_edit_idx(row: ControlRow) -> usize {
|
||||
|
||||
fn control_edit_choice_text(row: ControlRow, idx: usize) -> String {
|
||||
match row {
|
||||
ControlRow::Enabled
|
||||
| ControlRow::InvertY
|
||||
| ControlRow::WheelInvert
|
||||
| ControlRow::VehicleAutoCenter => {
|
||||
ControlRow::Enabled | ControlRow::InvertY | ControlRow::WheelInvert => {
|
||||
if idx == 0 {
|
||||
"off".to_owned()
|
||||
} else {
|
||||
"on".to_owned()
|
||||
}
|
||||
}
|
||||
ControlRow::VehicleAutoCenter => match idx {
|
||||
0 => "off".to_owned(),
|
||||
1 => "auto (1.5s)".to_owned(),
|
||||
_ => "on".to_owned(),
|
||||
},
|
||||
ControlRow::Sensitivity => format!("{:.2}", SENS_STEPS[idx.min(SENS_STEPS.len() - 1)]),
|
||||
ControlRow::ResetDefaults => match idx {
|
||||
1 => "Standard (GTA)".to_owned(),
|
||||
@ -564,11 +567,15 @@ fn apply_control_edit(s: &mut State, row: ControlRow, idx: usize) {
|
||||
})
|
||||
}
|
||||
ControlRow::VehicleAutoCenter => {
|
||||
let on = idx != 0;
|
||||
mouse::set_vehicle_auto_center(on).map(|p| {
|
||||
let mode = match idx {
|
||||
0 => mouse::AutoCenterMode::Off,
|
||||
1 => mouse::AutoCenterMode::Auto,
|
||||
_ => mouse::AutoCenterMode::On,
|
||||
};
|
||||
mouse::set_vehicle_auto_center_mode(mode).map(|p| {
|
||||
format!(
|
||||
"Vehicle auto-center -> {} ({})",
|
||||
if on { "on" } else { "off" },
|
||||
mode.as_str(),
|
||||
p.file_name().unwrap_or_default().to_string_lossy()
|
||||
)
|
||||
})
|
||||
@ -850,9 +857,17 @@ fn control_row_info(item: ControlRow) -> (&'static str, String, &'static str, &'
|
||||
),
|
||||
ControlRow::VehicleAutoCenter => (
|
||||
"Vehicle auto-center",
|
||||
on_off(mouse::vehicle_auto_center()),
|
||||
"restart",
|
||||
"Allow vehicle camera auto-centering while driving (takes effect on restart).",
|
||||
match mouse::vehicle_auto_center_mode() {
|
||||
mouse::AutoCenterMode::Off => "off".to_owned(),
|
||||
mouse::AutoCenterMode::Auto => "auto (1.5s)".to_owned(),
|
||||
mouse::AutoCenterMode::On => "on".to_owned(),
|
||||
},
|
||||
if crate::vehicle::auto_center_hook_active() {
|
||||
"now"
|
||||
} else {
|
||||
"restart"
|
||||
},
|
||||
"Vehicle camera auto-center: on (stock), auto (1.5s mouse hold), or off (F8 cycles live when hooked).",
|
||||
),
|
||||
ControlRow::Bind(i) => {
|
||||
let m = mouse::CONTROLS[i];
|
||||
|
||||
@ -19,11 +19,13 @@
|
||||
//! title's entity / component state, and the auto-center bypass.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
|
||||
use std::sync::LazyLock;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, AtomicUsize, Ordering};
|
||||
use std::time::Instant;
|
||||
|
||||
use larp_loader::callhook::{self, Regs};
|
||||
|
||||
use crate::modules::gameinput::mouse;
|
||||
use crate::modules::gameinput::mouse::{self, AutoCenterMode};
|
||||
|
||||
/// The first 16 hex digits of build 1.0.2.1's BLAKE3 hash.
|
||||
const BUILD_1_0_2_1: &str = "b99f876b74d4a77f";
|
||||
@ -38,6 +40,21 @@ const LOCAL_PLAYER: usize = 0x0ab5_6e38;
|
||||
/// (`0x1407c5ee1: call 0x140b69900`).
|
||||
const AUTO_CENTER_CALL_SITE: usize = 0x007c_5ee1;
|
||||
const AUTO_CENTER_UPDATE: usize = 0x00b6_9900;
|
||||
/// `camInterface` camera director globals (`0x145715680`, `0x144e747b0`, `0x1450ea1c0`):
|
||||
/// - `CAM_GAMEPLAY_DIRECTOR` (`0x099f_ece8`): `camGameplayDirector` (`0x3e9ed27f`)
|
||||
/// - `CAM_CINEMATIC_DIRECTOR` (`0x098c_1470`): `camCinematicDirector` (`0xd9b9a56e`)
|
||||
/// - `CAM_CINEMATIC_ALT_DIRECTOR` (`0x098c_1478`): secondary cinematic director (`0xfe7349f3`)
|
||||
/// - `CAM_DOMINANT_DIRECTOR` (`0x098c_14e8`): `camInterface::ms_DominantRenderedDirector`
|
||||
const CAM_GAMEPLAY_DIRECTOR: usize = 0x099f_ece8;
|
||||
const CAM_CINEMATIC_DIRECTOR: usize = 0x098c_1470;
|
||||
const CAM_CINEMATIC_ALT_DIRECTOR: usize = 0x098c_1478;
|
||||
const CAM_DOMINANT_DIRECTOR: usize = 0x098c_14e8;
|
||||
/// `camBaseDirector::m_RenderState` (`0=rendering, 1=in, 2=out, 3=not rendering`; `0x1401a1f1c`, `0x1420096c3`).
|
||||
const CAM_DIR_RENDER_STATE_OFF: u64 = 0x258;
|
||||
const CAM_DIR_PAUSED_OFF: u64 = 0x25e;
|
||||
/// In `auto` mode, hold the vehicle camera free for 1.5 s after mouse motion,
|
||||
/// then let the title smoothly auto-center behind the vehicle (GTA V PC feel).
|
||||
const AUTO_CENTER_IDLE_DELAY_US: u64 = 1_500_000;
|
||||
|
||||
const ENTITY_TYPE_VEHICLE: u8 = 2;
|
||||
const ENTITY_TYPE_PED: u8 = 3;
|
||||
@ -66,7 +83,75 @@ const VEH_SEAT_COMP_SLOT_OFF: usize = 0x77;
|
||||
/// The image, once it is known to be build 1.0.2.1 and a feature is on (0: off).
|
||||
static BASE: AtomicUsize = AtomicUsize::new(0);
|
||||
static AC_AFTER: AtomicUsize = AtomicUsize::new(0);
|
||||
static AC_HOOKED: AtomicBool = AtomicBool::new(false);
|
||||
static KEYBOARD_DRIVING: AtomicBool = AtomicBool::new(false);
|
||||
static AUTO_CENTER_MODE: AtomicU8 = AtomicU8::new(AutoCenterMode::On as u8);
|
||||
static LAST_MOUSE_MOVE_US: AtomicU64 = AtomicU64::new(0);
|
||||
static START: LazyLock<Instant> = LazyLock::new(Instant::now);
|
||||
|
||||
fn now_us() -> u64 {
|
||||
START.elapsed().as_micros() as u64 + 1
|
||||
}
|
||||
|
||||
/// Records live mouse motion for `vehicle_auto_center = auto` (1.5 s hold).
|
||||
pub fn note_mouse_motion() {
|
||||
if AC_HOOKED.load(Ordering::Relaxed)
|
||||
&& AUTO_CENTER_MODE.load(Ordering::Relaxed) == AutoCenterMode::Auto as u8
|
||||
{
|
||||
LAST_MOUSE_MOVE_US.store(now_us(), Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the vehicle camera auto-center call hook is installed (`F8` cycling available).
|
||||
pub fn auto_center_hook_active() -> bool {
|
||||
AC_HOOKED.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn should_bypass_auto_center_at(mode: AutoCenterMode, last_move_us: u64, now: u64) -> bool {
|
||||
match mode {
|
||||
AutoCenterMode::Off => true,
|
||||
AutoCenterMode::On => false,
|
||||
AutoCenterMode::Auto => {
|
||||
last_move_us != 0 && now.saturating_sub(last_move_us) < AUTO_CENTER_IDLE_DELAY_US
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn should_bypass_auto_center() -> bool {
|
||||
let mode = AutoCenterMode::from_u8(AUTO_CENTER_MODE.load(Ordering::Relaxed));
|
||||
should_bypass_auto_center_at(mode, LAST_MOUSE_MOVE_US.load(Ordering::Relaxed), now_us())
|
||||
}
|
||||
|
||||
/// Updates the live vehicle camera auto-center mode (used by `F10 -> Controls`).
|
||||
pub fn set_auto_center_mode(mode: AutoCenterMode) {
|
||||
AUTO_CENTER_MODE.store(mode as u8, Ordering::Release);
|
||||
}
|
||||
|
||||
/// Cycles vehicle camera auto-center mode live (`F8`: `auto -> off -> on -> auto`).
|
||||
pub fn cycle_auto_center() -> AutoCenterMode {
|
||||
let mut cur = AUTO_CENTER_MODE.load(Ordering::Acquire);
|
||||
let next = loop {
|
||||
let next_mode = match AutoCenterMode::from_u8(cur) {
|
||||
AutoCenterMode::Auto => AutoCenterMode::Off,
|
||||
AutoCenterMode::Off => AutoCenterMode::On,
|
||||
AutoCenterMode::On => AutoCenterMode::Auto,
|
||||
};
|
||||
match AUTO_CENTER_MODE.compare_exchange_weak(
|
||||
cur,
|
||||
next_mode as u8,
|
||||
Ordering::AcqRel,
|
||||
Ordering::Acquire,
|
||||
) {
|
||||
Ok(_) => break next_mode,
|
||||
Err(observed) => cur = observed,
|
||||
}
|
||||
};
|
||||
tracing::info!(
|
||||
mode = next.as_str(),
|
||||
"vehicle controls: camera auto-center cycled (F8)"
|
||||
);
|
||||
next
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Auto-center updates under way on this thread, bypassed:
|
||||
@ -104,8 +189,9 @@ pub fn keyboard_driving() -> bool {
|
||||
/// `base` is the mapped title image and no title thread runs yet.
|
||||
pub unsafe fn install(blake3_hex: &str, base: usize) {
|
||||
let driving = mouse::keyboard_driving_wanted();
|
||||
let no_auto_center = !mouse::vehicle_auto_center();
|
||||
if !driving && !no_auto_center {
|
||||
let ac_mode = mouse::vehicle_auto_center_mode();
|
||||
let hook_auto_center = ac_mode != AutoCenterMode::On || crate::mousehook::active();
|
||||
if !driving && !hook_auto_center {
|
||||
return;
|
||||
}
|
||||
if !blake3_hex.to_ascii_lowercase().starts_with(BUILD_1_0_2_1) {
|
||||
@ -115,21 +201,22 @@ pub unsafe fn install(blake3_hex: &str, base: usize) {
|
||||
return;
|
||||
}
|
||||
BASE.store(base, Ordering::Release);
|
||||
AUTO_CENTER_MODE.store(ac_mode as u8, Ordering::Release);
|
||||
if driving {
|
||||
KEYBOARD_DRIVING.store(true, Ordering::Relaxed);
|
||||
tracing::info!(
|
||||
"vehicle controls: keyboard driving on (W / S are gas / brake at the wheel)"
|
||||
);
|
||||
}
|
||||
if no_auto_center {
|
||||
if hook_auto_center {
|
||||
// SAFETY: the caller's promise.
|
||||
unsafe { install_auto_center(base) };
|
||||
unsafe { install_auto_center(base, ac_mode) };
|
||||
}
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// As [`install`].
|
||||
unsafe fn install_auto_center(base: usize) {
|
||||
unsafe fn install_auto_center(base: usize, mode: AutoCenterMode) {
|
||||
let stub = match callhook::after_stub(ac_returned) {
|
||||
Ok(stub) => stub,
|
||||
Err(e) => {
|
||||
@ -147,9 +234,13 @@ unsafe fn install_auto_center(base: usize) {
|
||||
ac_entered,
|
||||
)
|
||||
} {
|
||||
Ok(()) => tracing::info!(
|
||||
"vehicle controls: vehicle camera auto-center off (vehicle_auto_center = 0)"
|
||||
),
|
||||
Ok(()) => {
|
||||
AC_HOOKED.store(true, Ordering::Release);
|
||||
tracing::info!(
|
||||
mode = mode.as_str(),
|
||||
"vehicle controls: camera auto-center hook installed (F8 cycles auto/off/on)"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "vehicle controls: auto-center call not hooked, the title's auto-center stays")
|
||||
}
|
||||
@ -185,7 +276,7 @@ unsafe extern "C" fn ac_returned(_rax: u64, slot: *mut u64) {
|
||||
/// The heading / pitch the auto-center at `ac` follows, when its update is
|
||||
/// to be bypassed (flag 0, the player in a vehicle).
|
||||
fn auto_center_state(ac: u64, flag: u64) -> Option<(f32, f32)> {
|
||||
if (flag & 0xff) != 0 || !ptr_ok(ac) || seat() == Seat::None {
|
||||
if !should_bypass_auto_center() || (flag & 0xff) != 0 || !ptr_ok(ac) || seat() == Seat::None {
|
||||
return None;
|
||||
}
|
||||
// SAFETY: `ac + 0xd8` / `ac + 0xe0` hold pointers to the current heading
|
||||
@ -292,6 +383,59 @@ pub fn seat() -> Seat {
|
||||
seat
|
||||
}
|
||||
|
||||
unsafe fn director_rendering(dir: u64, dominant: u64) -> bool {
|
||||
if !ptr_ok(dir) {
|
||||
return false;
|
||||
}
|
||||
if dir == dominant {
|
||||
return true;
|
||||
}
|
||||
// SAFETY: `dir` is a pointer-checked `camBaseDirector*` from `.data`; `+0x258` is
|
||||
// `m_RenderState` (`< 3` = rendering / blending) and `+0x25e` is `m_Paused` (`0x1420096c3`).
|
||||
unsafe {
|
||||
let render_state = ((dir + CAM_DIR_RENDER_STATE_OFF) as *const u32).read_unaligned();
|
||||
let paused = ((dir + CAM_DIR_PAUSED_OFF) as *const u8).read();
|
||||
paused == 0 && render_state < 3
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn cinematic_camera_active_in(base: usize) -> (bool, &'static str) {
|
||||
// SAFETY: caller guarantees `base` points to the validated v1.0.2.1 image
|
||||
// (or a test arena covering all director pointer slots).
|
||||
unsafe {
|
||||
let dominant = ((base + CAM_DOMINANT_DIRECTOR) as *const u64).read_unaligned();
|
||||
let gameplay = ((base + CAM_GAMEPLAY_DIRECTOR) as *const u64).read_unaligned();
|
||||
let cin_a = ((base + CAM_CINEMATIC_DIRECTOR) as *const u64).read_unaligned();
|
||||
let cin_b = ((base + CAM_CINEMATIC_ALT_DIRECTOR) as *const u64).read_unaligned();
|
||||
if director_rendering(cin_a, dominant) {
|
||||
(true, "cinematic_1470")
|
||||
} else if director_rendering(cin_b, dominant) {
|
||||
(true, "cinematic_1478")
|
||||
} else if ptr_ok(dominant) && ptr_ok(gameplay) && dominant != gameplay {
|
||||
(true, "non_gameplay")
|
||||
} else {
|
||||
(false, "gameplay")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` when GTA VI's Cinematic Camera (or another non-gameplay camera
|
||||
/// director) is rendering (`0x144e747b0`, `0x1420096c3`, `0x14123ede3`).
|
||||
pub fn is_cinematic_camera_active() -> bool {
|
||||
static LAST: AtomicBool = AtomicBool::new(false);
|
||||
let base = BASE.load(Ordering::Acquire);
|
||||
let (active, director) = if base == 0 || !in_play(base) {
|
||||
(false, "inactive")
|
||||
} else {
|
||||
// SAFETY: `BASE` is set only for build 1.0.2.1; all director globals lie in `.data`.
|
||||
unsafe { cinematic_camera_active_in(base) }
|
||||
};
|
||||
if LAST.swap(active, Ordering::Relaxed) != active {
|
||||
tracing::debug!(cinematic = active, director, "vehicle controls: cinematic camera changed");
|
||||
}
|
||||
active
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// `map` and `pools` may be any values.
|
||||
unsafe fn ecs_slot_ptr(
|
||||
@ -474,3 +618,86 @@ unsafe fn is_aircraft(veh: u64) -> bool {
|
||||
veh_type <= 6 && (VEH_TYPE_AIRCRAFT_MASK >> veh_type) & 1 != 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn write_u64(buf: &mut [u8], off: usize, val: u64) {
|
||||
buf[off..off + 8].copy_from_slice(&val.to_ne_bytes());
|
||||
}
|
||||
|
||||
fn write_u32(buf: &mut [u8], off: usize, val: u32) {
|
||||
buf[off..off + 4].copy_from_slice(&val.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_center_mode_delays_for_1_5s_and_cycles() {
|
||||
assert!(!should_bypass_auto_center_at(AutoCenterMode::On, 100, 200));
|
||||
assert!(should_bypass_auto_center_at(AutoCenterMode::Off, 0, 10_000_000));
|
||||
// Auto mode: no mouse motion yet -> allow centering.
|
||||
assert!(!should_bypass_auto_center_at(AutoCenterMode::Auto, 0, 500_000));
|
||||
// Within 1.5 s of mouse motion -> hold camera free.
|
||||
assert!(should_bypass_auto_center_at(
|
||||
AutoCenterMode::Auto,
|
||||
1_000_000,
|
||||
2_499_999
|
||||
));
|
||||
// At/after 1.5 s idle -> smoothly auto-center.
|
||||
assert!(!should_bypass_auto_center_at(
|
||||
AutoCenterMode::Auto,
|
||||
1_000_000,
|
||||
2_500_000
|
||||
));
|
||||
AUTO_CENTER_MODE.store(AutoCenterMode::On as u8, Ordering::SeqCst);
|
||||
assert_eq!(cycle_auto_center(), AutoCenterMode::Auto);
|
||||
assert_eq!(cycle_auto_center(), AutoCenterMode::Off);
|
||||
assert_eq!(cycle_auto_center(), AutoCenterMode::On);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cinematic_camera_active_matches_dominant_director_pointer() {
|
||||
let mut arena = vec![0u8; CAM_GAMEPLAY_DIRECTOR + 16];
|
||||
let base = arena.as_ptr() as usize;
|
||||
let mut cin_a = vec![0u64; 0x60];
|
||||
let mut cin_b = vec![0u64; 0x60];
|
||||
let gameplay = vec![0u64; 0x60];
|
||||
let other = vec![0u64; 0x60];
|
||||
let set_rs = |dir: &mut Vec<u64>, val: u32| {
|
||||
// SAFETY: `dir` holds 0x60 u64s (0x300 bytes), covering `+0x258`.
|
||||
let bytes = unsafe {
|
||||
std::slice::from_raw_parts_mut(dir.as_mut_ptr().cast::<u8>(), dir.len() * 8)
|
||||
};
|
||||
write_u32(bytes, CAM_DIR_RENDER_STATE_OFF as usize, val);
|
||||
};
|
||||
set_rs(&mut cin_a, 3);
|
||||
set_rs(&mut cin_b, 3);
|
||||
let cin_a_ptr = cin_a.as_ptr() as u64;
|
||||
let cin_b_ptr = cin_b.as_ptr() as u64;
|
||||
let game_ptr = gameplay.as_ptr() as u64;
|
||||
let other_ptr = other.as_ptr() as u64;
|
||||
unsafe {
|
||||
assert!(!cinematic_camera_active_in(base).0);
|
||||
write_u64(&mut arena, CAM_GAMEPLAY_DIRECTOR, game_ptr);
|
||||
write_u64(&mut arena, CAM_CINEMATIC_DIRECTOR, cin_a_ptr);
|
||||
write_u64(&mut arena, CAM_CINEMATIC_ALT_DIRECTOR, cin_b_ptr);
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, game_ptr);
|
||||
assert!(!cinematic_camera_active_in(base).0);
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, cin_a_ptr);
|
||||
assert_eq!(cinematic_camera_active_in(base), (true, "cinematic_1470"));
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, cin_b_ptr);
|
||||
assert_eq!(cinematic_camera_active_in(base), (true, "cinematic_1478"));
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, game_ptr);
|
||||
set_rs(&mut cin_b, 1);
|
||||
assert_eq!(cinematic_camera_active_in(base), (true, "cinematic_1478"));
|
||||
set_rs(&mut cin_b, 3);
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, other_ptr);
|
||||
assert_eq!(cinematic_camera_active_in(base), (true, "non_gameplay"));
|
||||
write_u64(&mut arena, CAM_GAMEPLAY_DIRECTOR, 0);
|
||||
write_u64(&mut arena, CAM_CINEMATIC_DIRECTOR, 0x100);
|
||||
write_u64(&mut arena, CAM_CINEMATIC_ALT_DIRECTOR, 0x100);
|
||||
write_u64(&mut arena, CAM_DOMINANT_DIRECTOR, 0x100);
|
||||
assert!(!cinematic_camera_active_in(base).0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user