forked from Ports/goldeneye-pc-port
Holding RMB (or the Aim bind, default Left Shift) in a stage now releases the click-to-lock grab and drives the aim stick from a closed position loop on the live camera (vv_theta/vv_verta read via player.h): the crosshair chases the point under the free OS cursor and HOLDS it -- stationary cursor = stationary aim, even while walking. Release re-grabs with no snap. - Target derived from cursor NDC against the real render viewport rect (viGetView*), portScaleFovY so scope zooms compose for free; re-anchors on every cursor move or FOV change. - Coast prediction (game's 0.05*mult/tick speed decay) prevents overshoot; first-order dithering maps continuous demand onto the game's 10 discrete stick levels for precision work (shoot-the-lock etc.). - LMB fires while the cursor is out; ESC still releases capture; menus and MouseCaptureMode=0 unchanged. New knob: Input.AimAbsolute (default 1). Also: -I include shadows <math.h> with the game's constants-only header, which left fabs/pow/tan in input.c on implicit declarations -- new port/include/port_math.h pulls the real system math.h via #include_next.
1470 lines
69 KiB
C
1470 lines
69 KiB
C
/*
|
|
* Input: SDL2 keyboard/mouse/gamepad -> N64 controller state (Phase 3, D118).
|
|
*
|
|
* The game reads controllers via osContStartReadData / osContGetReadData
|
|
* (src/joy.c). libultra.c's SI section calls into this module once per
|
|
* controller poll: inputUpdate() refreshes SDL + accumulates the mouse-aim
|
|
* delta, then inputComputePad() produces the 16-bit button mask + stick for
|
|
* one controller. This is the single source of controller state on PC --
|
|
* the old contSnapshotFromKeyboard() now delegates here.
|
|
*
|
|
* Deliberately NOT a full port of pd_port/port/src/input.c (1551 lines): GE's
|
|
* menu/config code never calls that module's VK/bind-string API, so this is a
|
|
* focused implementation matching port/include/input.h plus the two helpers
|
|
* libultra.c needs.
|
|
*
|
|
* ------------------------------------------------------------------------
|
|
* BINDING SCHEME (documented in docs/internals.md sec F "D118")
|
|
*
|
|
* GE's default "1.1" control style: analog stick = move/strafe, the four
|
|
* C-buttons = aim/turn/look (DIGITAL on N64), R = aim mode, Z = fire.
|
|
*
|
|
* Keyboard + mouse (controller 0):
|
|
* W/S/A/D or arrows .. analog stick (move / strafe)
|
|
* mouse motion ....... aim (mode-aware -- see MOUSE-LOOK below)
|
|
* left mouse / LCtrl . Z trigger (fire)
|
|
* right mouse / LShift R trigger (aim mode)
|
|
* Space / Z / E ...... A button (action / use)
|
|
* X / R / F ......... B button (reload / cancel)
|
|
* Q ................. L trigger
|
|
* Enter / Tab ....... Start
|
|
*
|
|
* Xbox / SDL_GameController (controller 0 merges pad 0 with kbd/mouse;
|
|
* pads 1-3 -> controllers 1-3):
|
|
* left stick ........ analog stick (move / strafe)
|
|
* right stick ....... C-buttons (digital, 50% threshold -- aim)
|
|
* right trigger ..... Z trigger (fire)
|
|
* left trigger ...... R trigger (aim mode)
|
|
* A / X ............. A button
|
|
* B / Y ............. B button
|
|
* LB ............... L trigger
|
|
* RB ............... B button (reload)
|
|
* D-pad ............ N64 D-pad
|
|
* Start ............ Start
|
|
*
|
|
* MOUSE-LOOK (mode-aware, no src/ changes)
|
|
* GE's aim model (bondview2.c bondviewProcessInput / MoveData) is
|
|
* mode-dependent, so the mouse->pad mapping is too:
|
|
*
|
|
* - Hipfire (!insightaimmode): yaw = analog stick-X ("natural turn");
|
|
* pitch = DIGITAL C-up/C-down (stick-Y here is move fwd/back and does
|
|
* not pitch). Mouse Y emits a C-button on polls where it moved.
|
|
* - Aim mode (R / RMB held): yaw AND pitch are analog -- the stick pushed
|
|
* past +/-60 gives proportional (stick-60)/10 aim speed. Mouse X/Y push
|
|
* the stick into the 61..80 band. We emit NO C-buttons in this mode
|
|
* because there they mean crouch / lean / zoom (this was D118c:
|
|
* aim + look-down -> crouch).
|
|
*
|
|
* "aim button held" is read from our own RMB/LShift state -- exact for
|
|
* the default hold-to-aim scheme; a toggle-aim scheme would need a read
|
|
* of g_CurrentPlayer->insightaimmode (still no logic change).
|
|
*
|
|
* GE's native pitch is inverted ("flight" style: C-up -> look down). We
|
|
* hide that: mouse-down looks down by default, MouseInvertY flips it.
|
|
*
|
|
* Tunable via ge007.ini [Input]: MouseAimSpeed (aim mode), MouseTurnSpeed
|
|
* (hipfire yaw), MouseInvertY, MouseEnabled. Residual: in hipfire, yaw
|
|
* (analog) and pitch (digital) still feel different (D118a) -- a fully
|
|
* analog hipfire pitch needs an #ifdef PORT hook in bondview.c (TODO).
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
#include "port_math.h" /* real system math decls; see header for why */
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include "platform.h"
|
|
#include "system.h"
|
|
#include "config.h"
|
|
#include "input.h"
|
|
#include "optionsoverlay.h"
|
|
/* D194 absolute aim: read-only access to the live camera (struct player).
|
|
* Game header pulled in through the same shim path every other compiled game
|
|
* file uses; we only READ vv_theta/vv_verta/speedtheta/speedverta/aspect. */
|
|
#include "player.h"
|
|
|
|
/* N64 button bits (from PR/os.h -- duplicated here to avoid pulling os.h,
|
|
* whose `u8 errno;` field collides with <errno.h>'s macro). */
|
|
#define GE_CONT_A 0x8000
|
|
#define GE_CONT_B 0x4000
|
|
#define GE_CONT_G 0x2000 /* Z trigger */
|
|
#define GE_CONT_START 0x1000
|
|
#define GE_CONT_UP 0x0800
|
|
#define GE_CONT_DOWN 0x0400
|
|
#define GE_CONT_LEFT 0x0200
|
|
#define GE_CONT_RIGHT 0x0100
|
|
#define GE_CONT_L 0x0020
|
|
#define GE_CONT_R 0x0010
|
|
#define GE_CONT_E 0x0008 /* C-up */
|
|
#define GE_CONT_D 0x0004 /* C-down */
|
|
#define GE_CONT_C 0x0002 /* C-left */
|
|
#define GE_CONT_F 0x0001 /* C-right */
|
|
|
|
#define MAX_PADS 4
|
|
#define STICK_DEADZONE 7000
|
|
#define STICK_MAX 80
|
|
|
|
/* Front-end pointer mode: GE's menu cursor ("crosshair") is stick-driven
|
|
* (front.c frontUpdateControlStickPosition reads joyGetStickX/Y and
|
|
* integrates a screen position). During a running stage current_menu ==
|
|
* MENU_RUN_STAGE (11, src/bondconstants.h enum MENU); every other value is
|
|
* a front-end / briefing / debrief / cheat screen where the mouse should
|
|
* feel like a pointer, not a look-axis. We read the game global directly
|
|
* (enum MENU is ABI int) -- UI-context only, no logic change.
|
|
* MENU_INVALID (-1) means the front end never ran (bare -level_XX boot):
|
|
* treat that as in-game so direct-launch mouse-look is unaffected. */
|
|
extern int current_menu;
|
|
#define GE_MENU_RUN_STAGE 11
|
|
#define GE_MENU_INVALID (-1)
|
|
/* D169: the front-end cursor lives in the game's virtual-screen field, which
|
|
* is 440x330 in the front end -- not the 320x240 the pointer P-controller
|
|
* assumed. Read the live field so the mouse pointer can reach the whole
|
|
* mission-select grid. These are plain non-static engine accessors
|
|
* (src/game/bondview.c) and globals (src/game/front.c); UI geometry only, no
|
|
* logic change. */
|
|
extern float getPlayer_c_screenwidth(void);
|
|
extern float getPlayer_c_screenheight(void);
|
|
extern float getPlayer_c_screenleft(void);
|
|
extern float getPlayer_c_screentop(void);
|
|
extern float cursor_h_pos, cursor_v_pos;
|
|
|
|
/* D194/D238 -- natural-pitch control scheme.
|
|
*
|
|
* GE's hipfire pitch is structurally digital: bondviewProcessInput's default
|
|
* (1.1/HONEY) scheme reads pitch only from C-up/C-down (see D166), so the
|
|
* port emulates continuous pitch by duty-cycling that button -- an
|
|
* approximation that can never fully match yaw's genuinely continuous,
|
|
* unbounded analog mapping, which is the root of the "vertical feels slower"
|
|
* complaint. GE's own 1.2/SOLITARE scheme (cur_player_get/set_control_type,
|
|
* src/game/options.c; dispatch in bondviewProcessInput, bondview2.c ~5192-
|
|
* 5450) gives BOTH pitch and yaw continuous analog stick control in hipfire
|
|
* (canNaturalTurn/canNaturalPitch) -- at the cost of moving movement
|
|
* (forward/back/strafe) from the analog stick onto digital step buttons
|
|
* (digitalStepForward/Back/Left/Right, fed from C-buttons/D-pad). This is an
|
|
* ORIGINAL, player-selectable GE control style (not new game logic) -- we
|
|
* select it from the port the same way the options menu would
|
|
* (cur_player_set_control_type), and remap WASD/gamepad accordingly so
|
|
* movement keeps working under it. No src/ edits; no behavior invented that
|
|
* GE didn't already support. Escape hatch: Input.NaturalPitch=0 reverts to
|
|
* the 1.1/HONEY default with the D166 digital-pitch-pulse hack. */
|
|
extern int cur_player_get_control_type(void);
|
|
extern void cur_player_set_control_type(int type);
|
|
|
|
/* D194 absolute aim: live camera/projection accessors (src/fr.c, src/game/,
|
|
* port/src/video.c). All are plain reads of state owned by the game thread,
|
|
* sampled from inputComputePad which already runs in that same context
|
|
* (contSnapshotFromKeyboard <- osContStartReadData <- joy.c) -- no new
|
|
* cross-thread access, and nothing here writes. */
|
|
extern f32 viGetFovY(void);
|
|
extern s16 viGetViewWidth(void);
|
|
extern s16 viGetViewHeight(void);
|
|
extern s16 viGetViewLeft(void);
|
|
extern s16 viGetViewTop(void);
|
|
extern s16 getWidth320or440(void); /* CFB width the viewport rect lives in (320 NTSC) */
|
|
extern s16 getHeight330or240(void); /* CFB height (240 NTSC) */
|
|
extern f32 portScaleFovY(f32 fovy, s32 isTitleScreen);
|
|
extern s32 lvlGetCurrentStageToLoad(void);
|
|
#define CONTROLLER_CONFIG_HONEY_ 0
|
|
#define CONTROLLER_CONFIG_SOLITARE_ 1
|
|
#define MENU_POINTER_GAIN 1.5
|
|
#define TRIG_THRESHOLD (30 * 256)
|
|
#define RSTICK_THRESHOLD 0x4000
|
|
/* Mouse-look tuning. GE's aim model is mode-dependent (bondview2.c
|
|
* bondviewProcessInput):
|
|
* - Hipfire (!insightaimmode): yaw = analog stick-X ("natural turn"),
|
|
* pitch = DIGITAL C-up/C-down only (stick-Y is move fwd/back here).
|
|
* - Aim mode (R held): yaw AND pitch = analog stick pushed past
|
|
* +/-60 -> proportional (stick-60)/10. C-up/C-down mean crouch/lean/
|
|
* zoom in this mode, NOT aim -- so we must NOT emit them while aiming
|
|
* (that was D118c: aim + mouse-down -> crouch).
|
|
* GE's native pitch is inverted ("flight" style): C-up -> look down. We
|
|
* hide that so mouse-down looks down by default; MouseInvertY flips it. */
|
|
#define MOUSE_TURN_GAIN 6.0 /* hipfire: raw px this poll -> stick-X counts */
|
|
#define MOUSE_PITCH_THRESH 1.5 /* hipfire: px/poll before a C-button fires */
|
|
#define AIM_MOVE_THRESH 0.3 /* aim mode: px/poll before the stick moves */
|
|
#define HIP_PITCH_FULL 6.0 /* hipfire pitch: |px/poll| for a solid C hold (D166) */
|
|
|
|
/* D194(a) -- aim-mode response curve.
|
|
*
|
|
* bondviewPlayerControlStuff (bondview2.c) turns an aim-mode stick value
|
|
* into turn speed as `(stick_x - 60) / 10.0`, clamped to 1.0 -- i.e. the
|
|
* game's own proportional band is stick in [61,70]; anything from 71 up to
|
|
* whatever AimBand allows (60+AimBand, up to 100) clamps to the exact same
|
|
* max speed as 70. The old code aimed for the port's [61, 60+AimBand] range
|
|
* (up to [61,80]) as if it were all proportional, so a config'd AimBand>10
|
|
* bought nothing, and AIM_GAIN=4.0 reached the (effectively already-maxed)
|
|
* top of that range by ~5 px/poll -- "near bang-bang", matching the D194(a)
|
|
* complaint. Fix: curve-map px/poll onto the game's REAL proportional range
|
|
* [61,70] (independent of AimBand, which still acts as an extra ceiling for
|
|
* anyone who wants to cap below the game's own max), with a configurable
|
|
* gamma so slow motions land near the low end instead of jumping to nearly
|
|
* full speed immediately. */
|
|
#define AIM_STICK_MIN 61
|
|
#define AIM_STICK_GAME_MAX 70 /* bondview2.c: (stick-60)/10 saturates at stick=70 */
|
|
#define AIM_FULL_SPEED_PX 20.0 /* px/poll (at MouseAimSpeed=100) that reaches AIM_STICK_GAME_MAX */
|
|
|
|
/* D194 absolute (cursor-anchored) aim tuning. FULLERR_DEG = residual angular
|
|
* error that demands full stick: at that size the cursor is a full-screen turn
|
|
* away, so full speed there is exactly "slew as fast as possible"; nearer the
|
|
* target the demand falls off linearly -- the P-controller that makes the
|
|
* crosshair settle on the cursor instead of coasting past it. DEADBAND_DEG is
|
|
* the hold zone (~0.75 deg is sub-pixel at normal scope zoom). COAST_K predicts
|
|
* how far the view keeps moving if we cut the stick now (the game eases
|
|
* speedtheta/speedverta to zero at 0.05*mult/tick and integrates them at 3.5x,
|
|
* bondview2.c UpdateSpeedTheta/Verta -- coast = 3.5*s^2/(2*0.05*mult) =
|
|
* 35*s^2/mult degrees), so the demand is against where we'll BE, not where we
|
|
* ARE. All three are cheap to retune; they change no game logic. */
|
|
#define AIM_ABS_FULLERR_DEG 15.0
|
|
#define AIM_ABS_DEADBAND_DEG 0.75
|
|
#define AIM_ABS_COAST_K 35.0f
|
|
#define AIM_ABS_DEG2RAD 0.0174532925
|
|
|
|
/* D194(b) -- mouse sensitivity coupled to frame/poll rate.
|
|
*
|
|
* MOUSE_TURN_GAIN/AIM_GAIN above treat "px accumulated since the last
|
|
* inputComputePad(0) drain" as a fixed per-poll unit, but the real interval
|
|
* between drains drifts with render/scene load (D193 measured a rock-steady
|
|
* 60 sim ticks/s in the idle case, but the drain itself piggybacks on
|
|
* whatever cadence calls contSnapshotFromKeyboard() -- 1-2x per rendered
|
|
* frame per D165's comment -- so a hitch or a variable-length frame changes
|
|
* how much real time one "poll" of accumulated px represents). Net effect:
|
|
* the same physical hand motion emits a different turn rate depending on
|
|
* how ragged the poll cadence is, which reads as "sensitivity changes with
|
|
* movement/frame rate" (the user's own words).
|
|
*
|
|
* Fix: measure real elapsed time since the last drain with a monotonic
|
|
* clock and rescale the accumulated px by (dtRef / dtActual) before it
|
|
* hits MOUSE_TURN_GAIN/AIM_GAIN, where dtRef is the nominal 1/60s tick
|
|
* (matches D193's measured steady-state sim rate) -- so "px this poll"
|
|
* always means "px for a nominal 1/60s tick" regardless of how long that
|
|
* tick actually took in real time. Clamped both directions so one big
|
|
* stall (level load, GC pause) can't fling the view. Menu-cursor paths
|
|
* (D165/D169's P-controller, which has its own poll-count-based settling
|
|
* design) are deliberately NOT touched by this -- gameplay look only. */
|
|
#define MOUSE_DT_REF (1.0 / 60.0)
|
|
#define MOUSE_DT_SCALE_MIN 0.25 /* cap correction for an abnormally long poll gap */
|
|
#define MOUSE_DT_SCALE_MAX 4.0 /* cap correction for an abnormally short poll gap */
|
|
|
|
/* Item 1 (D165) — front-end 1:1 pointer. front.c frontUpdateControlStickPosition
|
|
* INTEGRATES the stick as a velocity into a screen-pixel cursor position
|
|
* (cursor_h_pos += (stickx*0.075 +/- 0.5) * delta, deadzone +/-5, clamp +/-70,
|
|
* cursor clamped into the ~320x240 virtual screen rect minus a 20px margin).
|
|
* Feeding it mouse *velocity* therefore gives velocity^2 feel. Instead we run a
|
|
* P-controller: keep our own estimate of where the game cursor is (menuEst*,
|
|
* integrated with the SAME recurrence as front.c), accumulate a target from
|
|
* mouse motion, and emit stick = clamp(GAIN*(target-est)). The estimate re-syncs
|
|
* to the real cursor whenever the target is held at a screen edge. */
|
|
#define MENU_CURSOR_LO 20.0
|
|
#define MENU_CURSOR_HI_H 300.0
|
|
#define MENU_CURSOR_HI_V 220.0
|
|
#define MENU_CURSOR_MID_H 160.0
|
|
#define MENU_CURSOR_MID_V 120.0
|
|
#define MENU_P_GAIN 6.0 /* stick = GAIN*(target-est); 0.075*GAIN<1 => no overshoot */
|
|
#define MENU_EST_DELTA 1.0 /* deltaEst per poll (front.c integrates ~1 tick/frame) */
|
|
#define MENU_MOUSE_TO_PX 1.0 /* device px -> virtual cursor px (x MenuPointerSpeed/100) */
|
|
|
|
static void applyGrab(int want);
|
|
static void reconcileGrab(int menuMode);
|
|
static void applyCursorVisibility(void);
|
|
|
|
static int numControllers = 1;
|
|
static int connectedMask = 0x1; /* controller 0 always present */
|
|
|
|
static SDL_GameController *pads[MAX_PADS];
|
|
|
|
static int mouseEnabled = 1;
|
|
static int mouseGrabbed = 1; /* released while the window is unfocused */
|
|
|
|
/* WI-1: Quake-style click-to-lock cursor capture.
|
|
* MouseCaptureMode 0 (default) = legacy: cursor is grabbed whenever the
|
|
* window has focus and MouseEnabled is set (prior behaviour, byte-exact).
|
|
* MouseCaptureMode 1 = native-PC: the cursor is free until you click in the
|
|
* game window; ESC (or focus loss, or opening a menu) frees it again.
|
|
* Re-entering a stage while still "armed" re-locks automatically so
|
|
* unpausing / starting a level does not need a click.
|
|
* Controller input is entirely independent of all of this. */
|
|
static int mouseCaptureMode = 1; /* WI-1 default: Quake-style click-to-lock + 1:1 menu pointer. 0 = legacy always-grab. */
|
|
static int captureArmed = 0; /* user has clicked to lock (capture mode) */
|
|
static int windowFocused = 1;
|
|
static int mouseAimSpeed = 16; /* aim-mode sensitivity, percent (B3: 50 -> 25 M-29 -> 16; still overshot at 25) */
|
|
static int aimBand = 20; /* aim mode: usable stick range above the 60 gate */
|
|
static int mouseTurnSpeed = 100; /* hipfire yaw sensitivity, percent */
|
|
static int menuPointerSpeed = 100; /* front-end cursor speed, percent */
|
|
static int mouseInvertY = 0; /* 1 = mouse-down looks up */
|
|
static int mouseYScale = 100; /* extra vertical (pitch) sensitivity, % */
|
|
static int mouseSmoothing = 0; /* 0 = raw; 1..90 = low-pass strength (%) */
|
|
static int mouseRawInput = 0; /* 1 = bypass OS pointer accel for aim */
|
|
static int mouseDtDecouple = 1; /* D194(b): normalize look sensitivity by real
|
|
* elapsed poll time instead of raw px/poll.
|
|
* Escape hatch: 0 = prior (coupled) behaviour. */
|
|
static int mouseSensitivity = 100; /* D238: single master sensitivity, percent --
|
|
* multiplies BOTH MouseAimSpeed and MouseTurnSpeed
|
|
* so one number tunes overall feel; the two legacy
|
|
* knobs stay as an independent per-mode trim on
|
|
* top of it (both default 100/16 => unchanged
|
|
* unless the user also touches those). */
|
|
static int aimCurveGamma = 160; /* D194(a): aim-mode response curve exponent x100
|
|
* (100 = linear, >100 = more low-speed control /
|
|
* less bang-bang, <100 = more twitchy). */
|
|
static int aimAbsolute = 1; /* D194: 1 = cursor-anchored (GEPD-style) aim while
|
|
* RMB is held -- the crosshair chases and HOLDS
|
|
* the point under the free cursor; 0 = legacy
|
|
* velocity stick synthesized from mouse delta. */
|
|
|
|
/* D194 absolute aim state. Touched ONLY in inputComputePad (game thread), the
|
|
* same confinement as every other static here. The target pair is re-derived
|
|
* from live camera values on press and on every mouse move, so there is no
|
|
* free-running per-poll accumulator for multi-tick catch-up (D193) to
|
|
* double-consume; the frac carry only dithers this poll's stick quantization. */
|
|
static int s_absAimActive = 0;
|
|
static float s_absTgtTheta = 0.0f, s_absTgtVerta = 0.0f;
|
|
static double s_absFracX = 0.0, s_absFracY = 0.0;
|
|
/* Last cursor position / quantized FOV seen while anchoring: the target is
|
|
* re-derived when either changes. (inputUpdate() does not accumulate
|
|
* mouseDX/DY while the cursor is free, so position must be tracked here.) */
|
|
static int s_absLastMx = 0, s_absLastMy = 0, s_absLastFovQ = 0;
|
|
/* D194: nonzero while an aim hold has temporarily released the grab so the
|
|
* free cursor can be read as an absolute position (set in the idx==0 block of
|
|
* inputComputePad); reconcileGrab(), applyCursorVisibility() and the mouse-
|
|
* button mask honour it. */
|
|
static int s_absAimSuspend = 0;
|
|
|
|
static int aimAbsCompute(int *outSx, int *outSy); /* D194, defined below */
|
|
static int naturalPitchMode = 1; /* D194/D238: 1 = force GE's own 1.2/SOLITARE
|
|
* control style for continuous analog pitch;
|
|
* 0 = legacy 1.1/HONEY + D166 digital pulse. */
|
|
static Uint64 s_lastLookPollCounter = 0; /* D194(b): monotonic clock, gameplay-look drain only */
|
|
|
|
/* Gamepad tuning -- defaults reproduce the old hardcoded constants exactly. */
|
|
static int padDeadzone = STICK_DEADZONE; /* left-stick deadzone, raw 0..32767 */
|
|
static int padTriggerPct = 23; /* trigger press point, % of travel (~30*256) */
|
|
static int padLookInvertY = 0; /* 1 = invert right-stick (look) Y */
|
|
|
|
/* Smoothed mouse delta carried between polls when mouseSmoothing > 0. */
|
|
static double mouseSmDX = 0.0, mouseSmDY = 0.0;
|
|
|
|
/* Raw relative-mouse delta accumulated since the last inputComputePad(0).
|
|
* NOT a persistent aim accumulator: mouse-look is a displacement device and
|
|
* GE's aim is a rate device, so we consume the whole delta each poll and
|
|
* reset -- stop moving and the stick/ C-button releases immediately. */
|
|
static double mouseDX = 0.0;
|
|
static double mouseDY = 0.0;
|
|
|
|
/* Mouse-wheel -> weapon cycle (D223): directional, like a normal PC shooter.
|
|
* The port forces CONTROLLER_CONFIG_HONEY (bondview.c:1484), so GE's default
|
|
* scheme applies (bondview2.c:5177-5179): invButtons = A_BUTTON,
|
|
* shootButtons = Z_TRIG, and the cycle signals are (bondview2.c:5337-5346):
|
|
* forward = fresh A edge while Z is NOT held
|
|
* backward = Z fresh edge while A IS held (the N64 "hold A, tap Z" trick)
|
|
* moveData.triggerOn (bondview2.c:5419) requires A to be released, so the
|
|
* synthesized Z edge in the backward sequence can never fire a shot.
|
|
*
|
|
* wheelFwd: remaining polls of the A pulse (clean press+release edge).
|
|
* wheelBack: 2 = present A-only next poll; 1 = present A+Z next poll (the
|
|
* fresh-Z-edge-while-A-held poll). A must already read "held"
|
|
* oldbuttons-wise before Z's edge, hence the two-poll sequence. */
|
|
#define WHEEL_FWD_POLLS 2
|
|
static int wheelFwd = 0;
|
|
static int wheelBack = 0;
|
|
|
|
/* Item 1 (D165) — front-end pointer P-controller state. */
|
|
static int menuPointerMode = 1; /* 0 = legacy velocity, 1 = 1:1 pointer */
|
|
static int hipfirePitchSpeed = 100; /* D166: hipfire pitch pulse rate, percent */
|
|
static double menuEstH = 0.0, menuEstV = 0.0; /* estimate of the game cursor (virtual px) */
|
|
static double menuTgtH = 0.0, menuTgtV = 0.0; /* mouse-driven target (virtual px) */
|
|
static int menuPrevActive = 0;
|
|
static double hipPitchPhase = 0.0; /* D166: hipfire pitch pulse phase 0..1 */
|
|
static int lastMenuMouseX = -1, lastMenuMouseY = -1; /* WI-2: last abs cursor seen in a menu */
|
|
|
|
/* Integrate one poll of our cursor estimate with front.c's exact recurrence
|
|
* (frontUpdateControlStickPosition): the game receives `stick` as an s8, applies
|
|
* a +/-5 deadzone with a -5 offset, clamps to +/-70, then
|
|
* cursor += (stick*0.075 +/- 0.5) * delta, and clamps the cursor to [lo, hi]. */
|
|
static double menuCursorStep(double est, double stick, double lo, double hi)
|
|
{
|
|
if (stick > 127.0) stick = 127.0;
|
|
if (stick < -128.0) stick = -128.0;
|
|
double x = (double)(int)stick;
|
|
if (x < -5.0) x += 5.0;
|
|
else if (x >= 6.0) x -= 5.0;
|
|
else x = 0.0;
|
|
if (x >= 71.0) x = 70.0;
|
|
else if (x < -70.0) x = -70.0;
|
|
if (x > 0.0) est += (x * 0.075 + 0.5) * MENU_EST_DELTA;
|
|
else if (x < 0.0) est += (x * 0.075 - 0.5) * MENU_EST_DELTA;
|
|
if (est > hi) est = hi;
|
|
else if (est < lo) est = lo;
|
|
return est;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
static void inputOpenPads(void)
|
|
{
|
|
connectedMask = 0x1;
|
|
int n = SDL_NumJoysticks();
|
|
for (int i = 0; i < n && i < MAX_PADS; ++i) {
|
|
if (!SDL_IsGameController(i)) {
|
|
continue;
|
|
}
|
|
if (pads[i]) {
|
|
continue;
|
|
}
|
|
pads[i] = SDL_GameControllerOpen(i);
|
|
if (pads[i]) {
|
|
connectedMask |= (1 << i);
|
|
sysLogPrintf(LOG_NOTE, "input: opened gamepad %d '%s' as controller %d",
|
|
i, SDL_GameControllerName(pads[i]), i);
|
|
}
|
|
}
|
|
for (int i = 0; i < MAX_PADS; ++i) {
|
|
if (pads[i]) {
|
|
connectedMask |= (1 << i);
|
|
}
|
|
}
|
|
numControllers = 1;
|
|
for (int i = 1; i < MAX_PADS; ++i) {
|
|
if (connectedMask & (1 << i)) {
|
|
numControllers = i + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------
|
|
* Scripted input (test harness, port-only). GE_INPUTSCRIPT lets a headless
|
|
* run walk the front-end / pause menus with no human at the keyboard.
|
|
*
|
|
* GE_INPUTSCRIPT="120:START;180:A;240:A;600:SDOWN;900:SNONE,A"
|
|
*
|
|
* Each entry is `<frame>:<tok>[,<tok>...]`. Buttons (A B Z START L R UP DOWN
|
|
* LEFT RIGHT CUP CDOWN CLEFT CRIGHT) pulse for INPUTSCRIPT_PULSE controller
|
|
* reads from <frame>. Analog-stick tokens (SUP SDOWN SLEFT SRIGHT) are
|
|
* SUSTAINED: the stick stays deflected until a later entry changes it; SNONE
|
|
* re-centres it. "Frame" = count of controller-0 reads since launch (roughly
|
|
* 2 per rendered frame -- watch GE_INPUTLOG to calibrate). Unset env => no
|
|
* effect; when set it is the ONLY controller-0 input source. */
|
|
#define INPUTSCRIPT_MAX 64
|
|
#define INPUTSCRIPT_PULSE 6
|
|
|
|
struct scriptEntry { long frame; unsigned mask; int sx, sy; int hasStick; };
|
|
static struct scriptEntry scriptEntries[INPUTSCRIPT_MAX];
|
|
static int scriptCount = -1; /* -1 = not parsed yet, 0 = parsed empty */
|
|
static long scriptFrame = 0;
|
|
static int scriptCurSX = 0; /* stick set by the last scriptApply() */
|
|
static int scriptCurSY = 0;
|
|
|
|
/* Apply one token to `e`. Buttons: A B Z START L R UP DOWN LEFT RIGHT CUP
|
|
* CDOWN CLEFT CRIGHT (D-pad/C-buttons). Analog stick: SUP SDOWN SLEFT SRIGHT
|
|
* (full +/-80 deflection -- moves menu cursors). */
|
|
static void scriptApplyToken(struct scriptEntry *e, const char *s, int n)
|
|
{
|
|
struct { const char *k; unsigned v; } btn[] = {
|
|
{"A",GE_CONT_A}, {"B",GE_CONT_B}, {"Z",GE_CONT_G}, {"START",GE_CONT_START},
|
|
{"L",GE_CONT_L}, {"R",GE_CONT_R}, {"UP",GE_CONT_UP}, {"DOWN",GE_CONT_DOWN},
|
|
{"LEFT",GE_CONT_LEFT}, {"RIGHT",GE_CONT_RIGHT},
|
|
{"CUP",GE_CONT_E}, {"CDOWN",GE_CONT_D}, {"CLEFT",GE_CONT_C}, {"CRIGHT",GE_CONT_F},
|
|
};
|
|
for (size_t i = 0; i < sizeof(btn)/sizeof(btn[0]); ++i) {
|
|
if ((int)strlen(btn[i].k) == n && SDL_strncasecmp(btn[i].k, s, n) == 0) {
|
|
e->mask |= btn[i].v;
|
|
return;
|
|
}
|
|
}
|
|
e->hasStick = 1;
|
|
if (n == 3 && SDL_strncasecmp("SUP", s, 3) == 0) { e->sy = STICK_MAX; return; }
|
|
if (n == 5 && SDL_strncasecmp("SDOWN", s, 5) == 0) { e->sy = -STICK_MAX; return; }
|
|
if (n == 5 && SDL_strncasecmp("SLEFT", s, 5) == 0) { e->sx = -STICK_MAX; return; }
|
|
if (n == 6 && SDL_strncasecmp("SRIGHT", s, 6) == 0) { e->sx = STICK_MAX; return; }
|
|
if (n == 5 && SDL_strncasecmp("SNONE", s, 5) == 0) { return; } /* recentre */
|
|
e->hasStick = 0;
|
|
sysLogPrintf(LOG_WARNING, "GE_INPUTSCRIPT: unknown token '%.*s'", n, s);
|
|
}
|
|
|
|
static void scriptParse(void)
|
|
{
|
|
scriptCount = 0;
|
|
const char *env = getenv("GE_INPUTSCRIPT");
|
|
if (!env || !*env) {
|
|
return;
|
|
}
|
|
const char *p = env;
|
|
while (*p && scriptCount < INPUTSCRIPT_MAX) {
|
|
char *end = NULL;
|
|
long fr = strtol(p, &end, 10);
|
|
if (end == p || *end != ':') {
|
|
sysLogPrintf(LOG_WARNING, "GE_INPUTSCRIPT: bad entry near '%s'", p);
|
|
break;
|
|
}
|
|
p = end + 1;
|
|
struct scriptEntry *e = &scriptEntries[scriptCount];
|
|
e->frame = fr;
|
|
e->mask = 0;
|
|
e->sx = e->sy = 0;
|
|
e->hasStick = 0;
|
|
while (*p && *p != ';') {
|
|
const char *tok = p;
|
|
while (*p && *p != ',' && *p != ';') ++p;
|
|
scriptApplyToken(e, tok, (int)(p - tok));
|
|
if (*p == ',') ++p;
|
|
}
|
|
if (*p == ';') ++p;
|
|
scriptCount++;
|
|
}
|
|
sysLogPrintf(LOG_INFO, "GE_INPUTSCRIPT: %d entr%s parsed",
|
|
scriptCount, scriptCount == 1 ? "y" : "ies");
|
|
}
|
|
|
|
static int scriptIsActive(void)
|
|
{
|
|
if (scriptCount < 0) {
|
|
scriptParse();
|
|
}
|
|
return scriptCount > 0;
|
|
}
|
|
|
|
/* When a script is loaded it is the SOLE source of controller-0 input: real
|
|
* keyboard/mouse/pad is ignored so headless menu walks are deterministic
|
|
* (a relative-mouse SDL window with no focus otherwise spews phantom deltas).
|
|
* Returns the scripted button mask for the current frame; advances the frame
|
|
* counter (call exactly once per controller-0 read). */
|
|
static unsigned scriptApply(unsigned button)
|
|
{
|
|
if (!scriptIsActive()) {
|
|
return button;
|
|
}
|
|
unsigned m = 0;
|
|
long bestStickFrame = -1;
|
|
for (int i = 0; i < scriptCount; ++i) {
|
|
long d = scriptFrame - scriptEntries[i].frame;
|
|
if (d >= 0 && d < INPUTSCRIPT_PULSE) {
|
|
m |= scriptEntries[i].mask;
|
|
}
|
|
/* sustained stick: the latest-starting entry that carried a stick token */
|
|
if (d >= 0 && scriptEntries[i].hasStick && scriptEntries[i].frame > bestStickFrame) {
|
|
bestStickFrame = scriptEntries[i].frame;
|
|
scriptCurSX = scriptEntries[i].sx;
|
|
scriptCurSY = scriptEntries[i].sy;
|
|
}
|
|
}
|
|
scriptFrame++;
|
|
return m;
|
|
}
|
|
|
|
static void inputRebuildBinds(void); /* D214; defined below with keyDown() */
|
|
|
|
int inputInit(void)
|
|
{
|
|
if (!SDL_WasInit(SDL_INIT_GAMECONTROLLER)) {
|
|
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) {
|
|
sysLogPrintf(LOG_WARNING, "input: SDL_INIT_GAMECONTROLLER failed: %s",
|
|
SDL_GetError());
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < MAX_PADS; ++i) {
|
|
pads[i] = NULL;
|
|
}
|
|
inputOpenPads();
|
|
|
|
inputRebuildBinds(); /* D214: parse [Bind] now that configLoad() has run */
|
|
|
|
/* Relative mouse mode for mouse-look. In click-to-lock mode we start
|
|
* released and wait for a click in the window (video.c -> inputNotifyClick). */
|
|
mouseGrabbed = mouseEnabled && !mouseCaptureMode;
|
|
if (mouseEnabled) {
|
|
if (mouseRawInput) {
|
|
/* Feed the raw device delta straight through: no OS pointer
|
|
* acceleration, no warp-based emulation. Must be set before
|
|
* relative mode is enabled. */
|
|
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALE, "0");
|
|
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "0");
|
|
sysLogPrintf(LOG_INFO, "input: raw mouse input (no OS pointer accel)");
|
|
}
|
|
SDL_SetRelativeMouseMode(mouseGrabbed ? SDL_TRUE : SDL_FALSE);
|
|
/* Drain the initial jump. */
|
|
SDL_GetRelativeMouseState(NULL, NULL);
|
|
}
|
|
|
|
applyCursorVisibility(); /* hide the OS cursor if we start focused */
|
|
|
|
sysLogPrintf(LOG_INFO, "input: ready (mask=0x%x, %d controller(s), aimSpeed=%d)",
|
|
connectedMask, numControllers, mouseAimSpeed);
|
|
return connectedMask;
|
|
}
|
|
|
|
void inputDestroy(void)
|
|
{
|
|
for (int i = 0; i < MAX_PADS; ++i) {
|
|
if (pads[i]) {
|
|
SDL_GameControllerClose(pads[i]);
|
|
pads[i] = NULL;
|
|
}
|
|
}
|
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER)) {
|
|
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
}
|
|
}
|
|
|
|
/* Poll once per controller-read: refresh SDL device state and integrate the
|
|
* mouse-aim delta into the accumulator. */
|
|
void inputUpdate(void)
|
|
{
|
|
SDL_GameControllerUpdate();
|
|
|
|
if (!mouseEnabled || !mouseGrabbed) {
|
|
return;
|
|
}
|
|
|
|
int dx = 0, dy = 0;
|
|
SDL_GetRelativeMouseState(&dx, &dy);
|
|
|
|
/* Raw px; per-mode sensitivity is applied in inputComputePad(). Accumulate
|
|
* in case inputUpdate() is polled more than once between pad reads. */
|
|
mouseDX += dx;
|
|
mouseDY += dy;
|
|
}
|
|
|
|
static int scaleAxis(int v)
|
|
{
|
|
int dz = padDeadzone;
|
|
if (dz < 0) dz = 0;
|
|
if (dz > 30000) dz = 30000;
|
|
if (v > -dz && v < dz) {
|
|
return 0;
|
|
}
|
|
if (v < 0) v += dz; else v -= dz;
|
|
int out = (int)((long)v * STICK_MAX / (32767 - dz));
|
|
if (out > STICK_MAX) out = STICK_MAX;
|
|
if (out < -STICK_MAX) out = -STICK_MAX;
|
|
return out;
|
|
}
|
|
|
|
static int keyDown(const Uint8 *ks, SDL_Scancode sc)
|
|
{
|
|
return ks && sc != SDL_SCANCODE_UNKNOWN && ks[sc];
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------
|
|
* D214 — keyboard rebinding (PD parity: config-string driven, no UI).
|
|
*
|
|
* Each gameplay action maps to a comma-separated list of SDL scancode names
|
|
* (as printed by SDL_GetScancodeName: "W", "Up", "Left Ctrl", "Space", ...).
|
|
* The defaults reproduce the previously-hardcoded FPS layout exactly, so a
|
|
* fresh or [Bind]-less ini changes nothing. Parsed once in inputInit(), after
|
|
* configLoad(). Mouse buttons (fire = LMB, aim = RMB) stay hardwired.
|
|
* ---------------------------------------------------------------------- */
|
|
enum {
|
|
IA_FORWARD, IA_BACK, IA_STRAFE_L, IA_STRAFE_R, IA_TURN_L, IA_TURN_R,
|
|
IA_FIRE, IA_AIM, IA_ACTION, IA_CANCEL, IA_LEAN_L, IA_START, IA_COUNT
|
|
};
|
|
|
|
static const struct { const char *key; const char *def; } kBindDefs[IA_COUNT] = {
|
|
[IA_FORWARD] = { "Input.Bind.Forward", "W,Up" },
|
|
[IA_BACK] = { "Input.Bind.Back", "S,Down" },
|
|
[IA_STRAFE_L] = { "Input.Bind.StrafeLeft", "A" },
|
|
[IA_STRAFE_R] = { "Input.Bind.StrafeRight", "D" },
|
|
[IA_TURN_L] = { "Input.Bind.TurnLeft", "Left" },
|
|
[IA_TURN_R] = { "Input.Bind.TurnRight", "Right" },
|
|
[IA_FIRE] = { "Input.Bind.Fire", "Left Ctrl" },
|
|
[IA_AIM] = { "Input.Bind.Aim", "Left Shift" },
|
|
[IA_ACTION] = { "Input.Bind.Action", "Space,Z,E" },
|
|
[IA_CANCEL] = { "Input.Bind.Cancel", "X,R,F,Escape" },
|
|
[IA_LEAN_L] = { "Input.Bind.LeanLeft", "Q" },
|
|
[IA_START] = { "Input.Bind.Start", "Return,Tab" },
|
|
};
|
|
|
|
#define BIND_MAX_KEYS 4
|
|
static char g_bindStr[IA_COUNT][64];
|
|
static SDL_Scancode g_bind[IA_COUNT][BIND_MAX_KEYS];
|
|
|
|
static void inputRebuildBinds(void)
|
|
{
|
|
for (int a = 0; a < IA_COUNT; a++) {
|
|
for (int k = 0; k < BIND_MAX_KEYS; k++) {
|
|
g_bind[a][k] = SDL_SCANCODE_UNKNOWN;
|
|
}
|
|
char buf[64];
|
|
strncpy(buf, g_bindStr[a][0] ? g_bindStr[a] : kBindDefs[a].def, sizeof(buf) - 1);
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
int n = 0;
|
|
for (char *tok = strtok(buf, ","); tok && n < BIND_MAX_KEYS; tok = strtok(NULL, ",")) {
|
|
while (*tok == ' ' || *tok == '\t') tok++;
|
|
char *end = tok + strlen(tok);
|
|
while (end > tok && (end[-1] == ' ' || end[-1] == '\t')) *--end = 0;
|
|
if (!*tok) continue;
|
|
SDL_Scancode sc = SDL_GetScancodeFromName(tok);
|
|
if (sc == SDL_SCANCODE_UNKNOWN) {
|
|
sysLogPrintf(LOG_WARNING, "input: %s: unknown key name '%s'",
|
|
kBindDefs[a].key, tok);
|
|
continue;
|
|
}
|
|
g_bind[a][n++] = sc;
|
|
}
|
|
if (n == 0) {
|
|
sysLogPrintf(LOG_WARNING, "input: %s has no valid keys; action unbound",
|
|
kBindDefs[a].key);
|
|
}
|
|
}
|
|
}
|
|
|
|
static int actHeld(const Uint8 *ks, int act)
|
|
{
|
|
for (int k = 0; k < BIND_MAX_KEYS; k++) {
|
|
if (keyDown(ks, g_bind[act][k])) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Fill button mask + stick for controller idx. Returns the 16-bit mask. */
|
|
unsigned inputComputePad(int idx, signed char *stick_x, signed char *stick_y)
|
|
{
|
|
unsigned button = 0;
|
|
int sx = 0, sy = 0;
|
|
|
|
/* D194/D238: self-correcting every poll -- cheap (plain field writes,
|
|
* see options.c cur_player_set_control_type), and re-asserts itself if
|
|
* anything else ever calls the setter (menu, save load) in between. */
|
|
if (idx == 0 && g_CurrentPlayer != NULL) {
|
|
int wantSolitare = naturalPitchMode ? CONTROLLER_CONFIG_SOLITARE_ : CONTROLLER_CONFIG_HONEY_;
|
|
if (cur_player_get_control_type() != wantSolitare) {
|
|
cur_player_set_control_type(wantSolitare);
|
|
}
|
|
}
|
|
|
|
if (idx < 0 || idx >= MAX_PADS) {
|
|
if (stick_x) *stick_x = 0;
|
|
if (stick_y) *stick_y = 0;
|
|
return 0;
|
|
}
|
|
|
|
/* F10 options overlay: while it is open, controller 0 is fully swallowed
|
|
* (neutral pad, no stick) and the nav keys / wheel drive the overlay
|
|
* instead. Mirrors the WI-1 "cursor free in a stage -> withhold input"
|
|
* pattern. Controllers 1-3 are untouched. */
|
|
if (idx == 0 && optionsOverlayIsOpen()) {
|
|
optionsOverlayHandleInput();
|
|
if (stick_x) *stick_x = 0;
|
|
if (stick_y) *stick_y = 0;
|
|
return 0;
|
|
}
|
|
|
|
/* ---- keyboard + mouse: controller 0 only ---- */
|
|
if (idx == 0) {
|
|
const Uint8 *ks = SDL_GetKeyboardState(NULL);
|
|
Uint32 mb = mouseEnabled ? SDL_GetMouseState(NULL, NULL) : 0;
|
|
int menuMode = (current_menu != GE_MENU_RUN_STAGE &&
|
|
current_menu != GE_MENU_INVALID);
|
|
|
|
/* D194 absolute aim (GEPD-style): while an aim button is physically
|
|
* held, temporarily release the grab so the free OS cursor can be read
|
|
* as an absolute position; reconcileGrab() honours s_absAimSuspend and
|
|
* restores the grab when the button comes up. rmbRaw is pre-mask: once
|
|
* the grab releases, the button mask below zeroes mb, but the physical
|
|
* state must keep the hold (and the game's aim mode) alive. */
|
|
int rmbRaw = (mb & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
|
|
/* Capture mode only: legacy (MouseCaptureMode=0) keeps its exact
|
|
* focus-grabbed relative behaviour. */
|
|
s_absAimSuspend = mouseEnabled && mouseCaptureMode && !menuMode && windowFocused &&
|
|
(rmbRaw || actHeld(ks, IA_AIM));
|
|
reconcileGrab(menuMode);
|
|
/* D196: the F10 options overlay forces the OS cursor visible via
|
|
* inputSuspendForOverlay() but nothing re-hides it on close unless
|
|
* reconcileGrab() happens to re-grab (only true if you had already
|
|
* clicked-to-lock in a stage). Re-assert the correct visibility
|
|
* every poll here -- this line is unreachable while the overlay is
|
|
* open (early return above), so it only fires once it has closed. */
|
|
applyCursorVisibility();
|
|
|
|
/* Click-to-lock, in a stage, cursor free: the mouse buttons must not
|
|
* reach the game (no phantom fire) -- the first click only re-locks
|
|
* (handled in video.c -> inputNotifyClick). Menus keep their buttons. */
|
|
/* ...but not while an aim hold has the cursor out for absolute aim:
|
|
* then the buttons are intentional (RMB is the hold itself, LMB fires). */
|
|
if (mouseCaptureMode && !mouseGrabbed && !menuMode && !s_absAimSuspend) {
|
|
mb = 0;
|
|
}
|
|
|
|
/* GE default control (1.1): stick Y = move fwd/back, stick X = turn,
|
|
* C-left/right = sidestep, C-up/down = look. FPS layout: W/S move,
|
|
* A/D strafe (C-buttons), mouse X turns (stick X), mouse Y looks. */
|
|
/* D194/D238 natural-pitch mode (GE's own 1.2/SOLITARE style) reads
|
|
* forward/back from digital C-up/C-down instead of the analog stick
|
|
* (bondview2.c: digitalStepForward/Back <- U/D_JPAD|U/D_CBUTTONS),
|
|
* because the stick's Y axis is what carries continuous analog
|
|
* pitch there instead. Strafe and turn are unchanged -- both
|
|
* schemes read them the same way. */
|
|
if (naturalPitchMode) {
|
|
if (actHeld(ks, IA_FORWARD)) button |= GE_CONT_E; /* C-up = forward */
|
|
if (actHeld(ks, IA_BACK)) button |= GE_CONT_D; /* C-down = back */
|
|
} else {
|
|
if (actHeld(ks, IA_FORWARD)) sy = STICK_MAX;
|
|
if (actHeld(ks, IA_BACK)) sy = -STICK_MAX;
|
|
}
|
|
if (actHeld(ks, IA_STRAFE_L)) button |= GE_CONT_C; /* strafe left */
|
|
if (actHeld(ks, IA_STRAFE_R)) button |= GE_CONT_F; /* strafe right */
|
|
if (actHeld(ks, IA_TURN_L)) sx = -STICK_MAX; /* keyboard turn */
|
|
if (actHeld(ks, IA_TURN_R)) sx = STICK_MAX;
|
|
|
|
if ((mb & SDL_BUTTON(SDL_BUTTON_LEFT)) || actHeld(ks, IA_FIRE))
|
|
button |= GE_CONT_G;
|
|
/* While s_absAimSuspend has the cursor out (capture mode), mb is
|
|
* masked above, so fall back to the physical rmbRaw to keep the hold,
|
|
* and with it the game's aim mode, alive for the whole press. */
|
|
int aimHeld = (s_absAimSuspend ? rmbRaw
|
|
: (mb & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0) ||
|
|
actHeld(ks, IA_AIM);
|
|
if (!aimHeld) s_absAimActive = 0; /* D194: absolute target dies with the hold */
|
|
if (aimHeld)
|
|
button |= GE_CONT_R;
|
|
if (actHeld(ks, IA_ACTION))
|
|
button |= GE_CONT_A;
|
|
if (wheelFwd > 0) { /* wheel up: fresh A edge = cycle forward */
|
|
button |= GE_CONT_A;
|
|
wheelFwd--;
|
|
} else if (wheelBack > 0) { /* wheel down: A+Z together, not staggered.
|
|
* D223 follow-up: staggering (A alone for a poll, THEN adding Z) races
|
|
* the real game-tick rate -- if the two states land in separate ticks,
|
|
* the "A alone" tick is itself a fresh A edge with no Z held, which
|
|
* the game's own weaponForwardOffset formula reads as a genuine
|
|
* cycle-FORWARD request (bondview2.c weaponForwardOffset/
|
|
* weaponBackOffset, both control-scheme sites) *before* the
|
|
* correcting backward tick runs -- so depending on real-time
|
|
* poll/tick alignment (D117-class nondeterminism) a single wheel-down
|
|
* notch could silently do a stray forward step, or forward-then-back
|
|
* (net a skipped slot on wrap). Presenting A and Z together from the
|
|
* very first poll means whichever single tick samples the 0->(A|Z)
|
|
* transition sees them rising simultaneously; weaponForwardOffset
|
|
* requires Z NOT held, so it's unambiguous -- only backward fires,
|
|
* every time, regardless of tick timing. moveData.triggerOn (actual
|
|
* fire) is separately gated off while A/invButtons is held, so this
|
|
* doesn't risk an accidental shot either. */
|
|
button |= GE_CONT_A | GE_CONT_G;
|
|
wheelBack--;
|
|
}
|
|
if (actHeld(ks, IA_CANCEL)) /* D145: Escape is in the default Cancel bind */
|
|
button |= GE_CONT_B;
|
|
if (actHeld(ks, IA_LEAN_L))
|
|
button |= GE_CONT_L;
|
|
if (actHeld(ks, IA_START))
|
|
button |= GE_CONT_START;
|
|
|
|
/* Mouse-look. Mode-dependent (see the tuning-constants comment):
|
|
* aim mode -> push the analog stick past +/-60 for proportional
|
|
* yaw + pitch; emit NO C-buttons (they mean crouch here).
|
|
* hipfire -> yaw on analog stick-X; pitch on digital C-up/C-down.
|
|
* "look down" convention: mouse-down looks down by default; GE's
|
|
* native pitch is inverted so hipfire down = C-up (GE_CONT_E) and
|
|
* aim-mode down = +stick_y. MouseInvertY flips both. */
|
|
if (mouseEnabled) {
|
|
double invert = mouseInvertY ? -1.0 : 1.0;
|
|
|
|
/* Optional exponential low-pass (mouseSmoothing = blend % of the
|
|
* previous poll). Off (0) => edx/edy are the raw deltas. */
|
|
double edx = mouseDX, edy = mouseDY;
|
|
if (mouseSmoothing > 0) {
|
|
double a = mouseSmoothing / 100.0;
|
|
if (a > 0.90) a = 0.90;
|
|
mouseSmDX = mouseSmDX * a + edx * (1.0 - a);
|
|
mouseSmDY = mouseSmDY * a + edy * (1.0 - a);
|
|
edx = mouseSmDX;
|
|
edy = mouseSmDY;
|
|
}
|
|
edy *= mouseYScale / 100.0;
|
|
|
|
double dyLook = edy * invert; /* >0 => look down */
|
|
|
|
/* D194(b): gameplay-look-only dt normalization -- see the
|
|
* MOUSE_DT_REF comment above. Computed unconditionally (so the
|
|
* clock stays warm across mode switches) but only ever applied
|
|
* below, inside the aimHeld/hipfire branches. */
|
|
double lookDtScale = 1.0;
|
|
{
|
|
Uint64 now = SDL_GetPerformanceCounter();
|
|
if (s_lastLookPollCounter != 0) {
|
|
double freq = (double)SDL_GetPerformanceFrequency();
|
|
double dtActual = (double)(now - s_lastLookPollCounter) / freq;
|
|
if (dtActual > 0.0005) { /* ignore sub-ms jitter / double polls */
|
|
lookDtScale = MOUSE_DT_REF / dtActual;
|
|
if (lookDtScale < MOUSE_DT_SCALE_MIN) lookDtScale = MOUSE_DT_SCALE_MIN;
|
|
if (lookDtScale > MOUSE_DT_SCALE_MAX) lookDtScale = MOUSE_DT_SCALE_MAX;
|
|
}
|
|
}
|
|
s_lastLookPollCounter = now;
|
|
}
|
|
if (!mouseDtDecouple) lookDtScale = 1.0;
|
|
|
|
if (configGetInputLog() && !menuMode && (aimHeld || fabs(edx) > 0.01 || fabs(dyLook) > 0.01)) {
|
|
sysLogPrintf(LOG_NOTE, "GE_INPUTLOG lookdt scale=%.3f raw=(%.2f,%.2f)",
|
|
lookDtScale, edx, dyLook);
|
|
}
|
|
|
|
if (menuMode && !menuPointerMode) {
|
|
/* Legacy velocity mode (Input.MenuPointerMode = 0): mouse
|
|
* velocity -> stick. Kept as a fallback; integrates as
|
|
* velocity^2 through front.c (D165). */
|
|
double g = MENU_POINTER_GAIN * (menuPointerSpeed / 100.0);
|
|
sx += (int)(edx * g);
|
|
sy -= (int)(dyLook * g); /* front-end cursor: +sy = up */
|
|
} else if (menuMode) {
|
|
/* Front-end menu pointer.
|
|
*
|
|
* front.c frontUpdateControlStickPosition() is the game's cursor
|
|
* integrator: it reads joyGetStickX/Y, applies a +/-5 deadband,
|
|
* clamps the stick to +/-70, then does
|
|
* cursor_h_pos += (stick*0.075 +/- 0.5) * timerDelta
|
|
* and clamps cursor_h/v_pos into [screenleft+20 ..
|
|
* screenleft+screenwidth-20] x [screentop+20 ..
|
|
* screentop+screenheight-20]. That integrator caps the cursor at
|
|
* ~5.75 virtual px per poll, so feeding it a stick derived from
|
|
* an absolute mouse position (D165/D169 P-controller) is
|
|
* unavoidably laggy/floaty and desyncs from the real cursor.
|
|
*
|
|
* With click-to-lock (MouseCaptureMode=1) the front end has a
|
|
* real free OS cursor, so we know exactly where the pointer is.
|
|
* Write cursor_h/v_pos DIRECTLY from the absolute mouse position
|
|
* whenever the mouse moved, and emit a zero stick so the game's
|
|
* integrator adds nothing (stick 0 -> deadband). When the mouse
|
|
* is idle we leave the cursor alone and let the keyboard stick
|
|
* (WASD/arrows, added above) drive it through the game as usual.
|
|
* cursor_h/v_pos are the plain front.c UI globals every menu
|
|
* hit-test already reads -- no logic change, just where the
|
|
* pointer is placed. */
|
|
double loH = MENU_CURSOR_LO, hiH = MENU_CURSOR_HI_H;
|
|
double loV = MENU_CURSOR_LO, hiV = MENU_CURSOR_HI_V;
|
|
{
|
|
double sw = getPlayer_c_screenwidth(), sh = getPlayer_c_screenheight();
|
|
double sl = getPlayer_c_screenleft(), st = getPlayer_c_screentop();
|
|
if (sw > 200.0 && sw < 2000.0 && sh > 150.0 && sh < 2000.0) {
|
|
loH = sl + 20.0; hiH = sl + sw - 20.0;
|
|
loV = st + 20.0; hiV = st + sh - 20.0;
|
|
}
|
|
}
|
|
|
|
int haveAbs = 0;
|
|
if (mouseCaptureMode && !mouseGrabbed) {
|
|
int mx = 0, my = 0;
|
|
SDL_GetMouseState(&mx, &my);
|
|
SDL_Window *w = SDL_GetMouseFocus();
|
|
int ww = 0, wh = 0;
|
|
if (w) SDL_GetWindowSize(w, &ww, &wh);
|
|
if (ww > 0 && wh > 0) {
|
|
haveAbs = 1;
|
|
if (!menuPrevActive) {
|
|
/* just entered a menu: adopt the current pointer as
|
|
* the baseline, don't yank the cursor this frame */
|
|
lastMenuMouseX = mx;
|
|
lastMenuMouseY = my;
|
|
}
|
|
if (mx != lastMenuMouseX || my != lastMenuMouseY) {
|
|
double fx = (double)mx / (double)ww;
|
|
double fy = (double)my / (double)wh;
|
|
if (fx < 0.0) fx = 0.0; else if (fx > 1.0) fx = 1.0;
|
|
if (fy < 0.0) fy = 0.0; else if (fy > 1.0) fy = 1.0;
|
|
cursor_h_pos = (float)(loH + fx * (hiH - loH));
|
|
cursor_v_pos = (float)(loV + fy * (hiV - loV));
|
|
sx = 0; /* pointer owns the cursor this poll */
|
|
sy = 0;
|
|
}
|
|
lastMenuMouseX = mx;
|
|
lastMenuMouseY = my;
|
|
}
|
|
}
|
|
|
|
if (!haveAbs) {
|
|
/* Legacy relative path (MouseCaptureMode=0, cursor grabbed):
|
|
* P-controller onto a mouse-accumulated target (D165/D169).
|
|
* D180: clamp the estimate to the same live rect as the
|
|
* target -- D169 widened the target clamp but left the
|
|
* estimate pinned to the old 320x240 constants, so the
|
|
* right/bottom of the menu was unreachable and the
|
|
* controller wound up into never-settling drift. */
|
|
if (!menuPrevActive) {
|
|
menuEstH = menuTgtH = cursor_h_pos;
|
|
menuEstV = menuTgtV = cursor_v_pos;
|
|
hipPitchPhase = 0.0;
|
|
}
|
|
double s = (menuPointerSpeed / 100.0) * MENU_MOUSE_TO_PX;
|
|
menuTgtH += edx * s;
|
|
menuTgtV += dyLook * s;
|
|
if (menuTgtH < loH) menuTgtH = loH;
|
|
if (menuTgtH > hiH) menuTgtH = hiH;
|
|
if (menuTgtV < loV) menuTgtV = loV;
|
|
if (menuTgtV > hiV) menuTgtV = hiV;
|
|
|
|
double effH = MENU_P_GAIN * (menuTgtH - menuEstH);
|
|
double effV = MENU_P_GAIN * (menuTgtV - menuEstV);
|
|
if (effH > 70.0) effH = 70.0; else if (effH < -70.0) effH = -70.0;
|
|
if (effV > 70.0) effV = 70.0; else if (effV < -70.0) effV = -70.0;
|
|
|
|
menuEstH = menuCursorStep(menuEstH, effH, loH, hiH);
|
|
menuEstV = menuCursorStep(menuEstV, effV, loV, hiV);
|
|
|
|
sx += (int)(effH + (effH >= 0.0 ? 0.5 : -0.5));
|
|
sy -= (int)(effV + (effV >= 0.0 ? 0.5 : -0.5));
|
|
}
|
|
|
|
if (configGetInputLog()) {
|
|
sysLogPrintf(LOG_NOTE,
|
|
"GE_INPUTLOG menuptr abs=%d cursor=(%.1f,%.1f) stick=(%d,%d)",
|
|
haveAbs, (double)cursor_h_pos, (double)cursor_v_pos, sx, sy);
|
|
}
|
|
} else if (aimHeld) {
|
|
/* D194 absolute aim: when a free cursor is available the stick
|
|
* is driven by a closed position loop on the live camera (see
|
|
* aimAbsCompute) instead of this poll's mouse velocity -- the
|
|
* crosshair chases the point under the cursor and HOLDS it.
|
|
* Otherwise fall through to the legacy velocity stick below. */
|
|
int absSx = 0, absSy = 0;
|
|
if (aimAbsCompute(&absSx, &absSy)) {
|
|
/* Absolute aim owns the look axes this poll; keyboard turn
|
|
* (sx set above) is overridden only when we demand motion. */
|
|
if (absSx != 0) sx = absSx;
|
|
if (absSy != 0) sy = absSy;
|
|
} else {
|
|
double aimEdx = edx * lookDtScale, aimDyLook = dyLook * lookDtScale;
|
|
double aimSens = (mouseAimSpeed / 100.0) * (mouseSensitivity / 100.0);
|
|
double gamma = aimCurveGamma / 100.0;
|
|
double normX = fabs(aimEdx) * aimSens / AIM_FULL_SPEED_PX;
|
|
double normY = fabs(aimDyLook) * aimSens / AIM_FULL_SPEED_PX;
|
|
if (normX > 1.0) normX = 1.0;
|
|
if (normY > 1.0) normY = 1.0;
|
|
int ceilStick = 60 + aimBand;
|
|
if (ceilStick > AIM_STICK_GAME_MAX) ceilStick = AIM_STICK_GAME_MAX;
|
|
if (fabs(aimEdx) >= AIM_MOVE_THRESH) {
|
|
int m = AIM_STICK_MIN + (int)(pow(normX, gamma) * (AIM_STICK_GAME_MAX - AIM_STICK_MIN));
|
|
if (m > ceilStick) m = ceilStick;
|
|
sx += (aimEdx > 0) ? m : -m;
|
|
}
|
|
if (fabs(aimDyLook) >= AIM_MOVE_THRESH) {
|
|
int m = AIM_STICK_MIN + (int)(pow(normY, gamma) * (AIM_STICK_GAME_MAX - AIM_STICK_MIN));
|
|
if (m > ceilStick) m = ceilStick;
|
|
sy += (aimDyLook > 0) ? m : -m; /* +stick_y = look down */
|
|
}
|
|
} /* D194: end legacy velocity-aim fallback */
|
|
} else {
|
|
double hipEdx = edx * lookDtScale, hipDyLook = dyLook * lookDtScale;
|
|
double hipSens = (mouseTurnSpeed / 100.0) * (mouseSensitivity / 100.0);
|
|
sx += (int)(hipEdx * hipSens * MOUSE_TURN_GAIN);
|
|
if (naturalPitchMode) {
|
|
/* D194/D238: SOLITARE gives hipfire pitch the same
|
|
* continuous analog stick treatment as yaw -- same
|
|
* formula as the sx line above, so X and Y are, by
|
|
* construction, symmetric. Forward/back moved to
|
|
* digital C-up/C-down above, freeing the stick's Y axis
|
|
* for this. */
|
|
sy += (int)(hipDyLook * hipSens * MOUSE_TURN_GAIN);
|
|
} else {
|
|
/* D166 (legacy): hipfire pitch as C-button pulses whose
|
|
* frequency scales with mouse-Y speed -- fast mouse =
|
|
* solid hold, slow = sparse taps. Kept as the
|
|
* Input.NaturalPitch=0 escape hatch. */
|
|
double sp = fabs(hipDyLook);
|
|
if (sp >= MOUSE_PITCH_THRESH) {
|
|
double duty = sp * (hipfirePitchSpeed / 100.0) * (mouseSensitivity / 100.0) / HIP_PITCH_FULL;
|
|
if (duty > 1.0) duty = 1.0;
|
|
hipPitchPhase += duty;
|
|
if (hipPitchPhase >= 1.0) {
|
|
hipPitchPhase -= 1.0;
|
|
button |= (hipDyLook > 0) ? GE_CONT_E : GE_CONT_D; /* E=C-up=look down */
|
|
}
|
|
} else {
|
|
hipPitchPhase = 0.0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (menuMode && mouseEnabled) {
|
|
/* Clicks are select / back in the front end, not fire / aim. */
|
|
button &= ~(GE_CONT_G | GE_CONT_R);
|
|
if (mb & SDL_BUTTON(SDL_BUTTON_LEFT)) button |= GE_CONT_A;
|
|
if (mb & SDL_BUTTON(SDL_BUTTON_RIGHT)) button |= GE_CONT_B;
|
|
}
|
|
menuPrevActive = menuMode;
|
|
|
|
mouseDX = 0.0;
|
|
mouseDY = 0.0;
|
|
}
|
|
|
|
/* ---- gamepad ---- */
|
|
SDL_GameController *pad = pads[idx];
|
|
if (pad) {
|
|
int lx = SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTX);
|
|
int ly = SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_LEFTY);
|
|
int rx = SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTX);
|
|
int ry = SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_RIGHTY);
|
|
|
|
if (naturalPitchMode) {
|
|
/* D194/D238: SOLITARE swaps stick roles -- left stick becomes
|
|
* digital-step movement (same analog-for-movement tradeoff as
|
|
* the keyboard remap above), right stick becomes continuous
|
|
* natural look (replacing its old digital-C-button emulation),
|
|
* matching how the mouse now drives look continuously too. */
|
|
if (ly < -RSTICK_THRESHOLD) button |= GE_CONT_E; /* stick up = forward */
|
|
if (ly > RSTICK_THRESHOLD) button |= GE_CONT_D; /* stick down = back */
|
|
if (lx < -RSTICK_THRESHOLD) button |= GE_CONT_C; /* strafe left */
|
|
if (lx > RSTICK_THRESHOLD) button |= GE_CONT_F; /* strafe right */
|
|
|
|
int rxs = scaleAxis(rx);
|
|
int rys = -scaleAxis(ry); /* SDL up = negative -> N64 up = positive */
|
|
if (padLookInvertY) rys = -rys;
|
|
if (rxs) sx = rxs;
|
|
if (rys) sy = rys;
|
|
} else {
|
|
int px = scaleAxis(lx);
|
|
int py = -scaleAxis(ly); /* SDL up = negative -> N64 up = positive */
|
|
if (px) sx = px;
|
|
if (py) sy = py;
|
|
|
|
if (padLookInvertY) ry = -ry;
|
|
if (rx > RSTICK_THRESHOLD) button |= GE_CONT_F;
|
|
if (rx < -RSTICK_THRESHOLD) button |= GE_CONT_C;
|
|
if (ry > RSTICK_THRESHOLD) button |= GE_CONT_D;
|
|
if (ry < -RSTICK_THRESHOLD) button |= GE_CONT_E;
|
|
}
|
|
|
|
int trigPt = padTriggerPct * 327; /* % of the 0..32767 trigger travel */
|
|
if (SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > trigPt)
|
|
button |= GE_CONT_G;
|
|
if (SDL_GameControllerGetAxis(pad, SDL_CONTROLLER_AXIS_TRIGGERLEFT) > trigPt)
|
|
button |= GE_CONT_R;
|
|
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_A) ||
|
|
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_X))
|
|
button |= GE_CONT_A;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_B) ||
|
|
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_Y) ||
|
|
SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER))
|
|
button |= GE_CONT_B;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_LEFTSHOULDER))
|
|
button |= GE_CONT_L;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_START))
|
|
button |= GE_CONT_START;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_UP))
|
|
button |= GE_CONT_UP;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_DOWN))
|
|
button |= GE_CONT_DOWN;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_LEFT))
|
|
button |= GE_CONT_LEFT;
|
|
if (SDL_GameControllerGetButton(pad, SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
|
|
button |= GE_CONT_RIGHT;
|
|
}
|
|
|
|
if (sx > STICK_MAX) sx = STICK_MAX;
|
|
if (sx < -STICK_MAX) sx = -STICK_MAX;
|
|
if (sy > STICK_MAX) sy = STICK_MAX;
|
|
if (sy < -STICK_MAX) sy = -STICK_MAX;
|
|
|
|
if (stick_x) *stick_x = (signed char)sx;
|
|
if (stick_y) *stick_y = (signed char)sy;
|
|
|
|
if (idx == 0 && scriptIsActive()) {
|
|
button = scriptApply(button);
|
|
sx = scriptCurSX;
|
|
sy = scriptCurSY;
|
|
}
|
|
|
|
if (configGetInputLog() && (button || sx || sy)) {
|
|
sysLogPrintf(LOG_NOTE, "GE_INPUTLOG cont%d: btn=%04x stick=(%d,%d)",
|
|
idx, button, sx, sy);
|
|
}
|
|
|
|
return button;
|
|
}
|
|
|
|
static void applyGrab(int want)
|
|
{
|
|
want = want && mouseEnabled;
|
|
if (want == mouseGrabbed) {
|
|
return;
|
|
}
|
|
mouseGrabbed = want;
|
|
SDL_SetRelativeMouseMode(want ? SDL_TRUE : SDL_FALSE);
|
|
if (want) {
|
|
SDL_GetRelativeMouseState(NULL, NULL); /* drain the accumulated jump */
|
|
}
|
|
mouseDX = mouseDY = 0.0;
|
|
}
|
|
|
|
/* Reconcile the SDL grab state with what the current mode wants. Called once
|
|
* per controller-0 poll (menuMode known there) and from the notify hooks. */
|
|
static void reconcileGrab(int menuMode)
|
|
{
|
|
int want;
|
|
if (!mouseCaptureMode) {
|
|
want = windowFocused; /* legacy */
|
|
} else {
|
|
want = captureArmed && windowFocused && !menuMode; /* click-to-lock */
|
|
}
|
|
if (s_absAimSuspend) {
|
|
want = 0; /* D194: an aim hold wants the free cursor for absolute aim */
|
|
}
|
|
applyGrab(want);
|
|
}
|
|
|
|
/* Hide the OS cursor while the game window is focused (normal PC-game
|
|
* behaviour): in a stage the mouse drives the look axis, and in menus GE draws
|
|
* its own crosshair that now tracks the pointer 1:1 -- a visible OS arrow on
|
|
* top is just clutter. Show it again when focus is lost so the desktop behaves
|
|
* normally. Relative-mouse mode hides the cursor too, but only while grabbed;
|
|
* this covers the free-but-focused states (menus, pre-click stage). */
|
|
static void applyCursorVisibility(void)
|
|
{
|
|
/* D194: while an aim hold has the cursor out for absolute aim, show it --
|
|
* the user is pointing with it and needs to see where. */
|
|
int hide = windowFocused && mouseEnabled && !s_absAimSuspend;
|
|
SDL_ShowCursor(hide ? SDL_DISABLE : SDL_ENABLE);
|
|
}
|
|
|
|
/* video.c focus events. In legacy mode this directly grabs/releases; in
|
|
* capture mode it just records focus and lets reconcileGrab() decide. */
|
|
void inputSetMouseGrab(int on)
|
|
{
|
|
windowFocused = on ? 1 : 0;
|
|
reconcileGrab(0); /* menuMode re-checked on the next poll anyway */
|
|
applyCursorVisibility();
|
|
}
|
|
|
|
/* A mouse click landed in the game window (video.c). In click-to-lock mode
|
|
* this arms + grabs; otherwise it is ignored (the game sees the click). */
|
|
void inputNotifyClick(void)
|
|
{
|
|
if (mouseCaptureMode && mouseEnabled && windowFocused) {
|
|
captureArmed = 1;
|
|
reconcileGrab(0);
|
|
}
|
|
}
|
|
|
|
/* ESC pressed (video.c). In click-to-lock mode, releases the cursor and
|
|
* reports 1 so the caller can swallow the key; otherwise reports 0. */
|
|
int inputReleaseCapture(void)
|
|
{
|
|
if (mouseCaptureMode && mouseGrabbed) {
|
|
captureArmed = 0;
|
|
applyGrab(0);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int inputMouseCaptureActive(void) { return mouseCaptureMode && !mouseGrabbed; }
|
|
|
|
/* F10 options overlay: while it owns controller 0 the inputComputePad poll
|
|
* early-returns before reconcileGrab()/applyCursorVisibility(), so whatever
|
|
* grab state was live when F10 was pressed (relative mode + hidden cursor in a
|
|
* stage) would persist and the mouse UI would be unusable. Force the cursor
|
|
* free + visible every poll; the normal reconcile resumes once the overlay
|
|
* closes and the early-return no longer fires. */
|
|
void inputSuspendForOverlay(void)
|
|
{
|
|
if (mouseGrabbed) {
|
|
mouseGrabbed = 0;
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
|
mouseDX = mouseDY = 0.0;
|
|
}
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
}
|
|
|
|
void inputPostWheel(int notches)
|
|
{
|
|
/* D223: keep the direction -- up cycles to the next weapon, down to the
|
|
* previous one. A burst of same-direction notches just re-arms the same
|
|
* pulse; a direction change mid-sequence restarts it (last wins). */
|
|
if (notches > 0) { wheelFwd = WHEEL_FWD_POLLS; wheelBack = 0; }
|
|
else if (notches < 0) { wheelBack = 2; wheelFwd = 0; }
|
|
}
|
|
|
|
/* Called from the host event pump on SDL_CONTROLLERDEVICEADDED/REMOVED.
|
|
* Closes every open pad and re-opens whatever is present now. `connectedMask`
|
|
* bit 0 (keyboard/mouse) is always kept. Note: the game latches the mask at
|
|
* osContInit (boot), so a pad added later still merges into controller 0 for
|
|
* play -- it just won't appear as a separate controller channel. */
|
|
void inputRescanPads(void)
|
|
{
|
|
for (int i = 0; i < MAX_PADS; ++i) {
|
|
if (pads[i]) {
|
|
SDL_GameControllerClose(pads[i]);
|
|
pads[i] = NULL;
|
|
}
|
|
}
|
|
inputOpenPads();
|
|
sysLogPrintf(LOG_NOTE, "input: rescanned pads (mask=0x%x, %d controller(s))",
|
|
connectedMask, numControllers);
|
|
}
|
|
|
|
int inputConnectedMask(void)
|
|
{
|
|
return connectedMask;
|
|
}
|
|
|
|
int inputGetNumControllers(void)
|
|
{
|
|
return numControllers;
|
|
}
|
|
|
|
/* D194 absolute (cursor-anchored) aim mode -- GEPD "mouse injector" style.
|
|
*
|
|
* Instead of synthesizing a velocity stick from this poll's mouse delta
|
|
* (which can only ever be one of ten discrete (stick-60)/10 speeds, with a
|
|
* hard 10%-speed floor on the smallest nonzero nudge), drive the game's own
|
|
* aim-speed plant as a closed position loop: the cursor's screen position IS
|
|
* the desired aim direction. On RMB press / every mouse move we record the
|
|
* view angles that would put the point under the cursor at screen center;
|
|
* each poll we emit a stick proportional to the remaining angular error, so
|
|
* the crosshair slews to the cursor and HOLDS there -- stationary cursor =
|
|
* stationary aim (even while walking), no floor jump, no release snap.
|
|
*
|
|
* Returns 1 if it handled this poll (callers must use outSx/outSy); 0 means
|
|
* fall back to the legacy velocity stick (grabbed mouse, menus, gamepad,
|
|
* degenerate geometry).
|
|
*/
|
|
static float aimAbsAngDiff(float a, float b)
|
|
{
|
|
float d = a - b;
|
|
while (d > 180.0f) d -= 360.0f;
|
|
while (d < -180.0f) d += 360.0f;
|
|
return d;
|
|
}
|
|
|
|
/* Quantize [0,10] stick-units to an int with first-order dithering so the
|
|
* game's ten discrete aim speeds time-average into a finer low-speed range
|
|
* (precision work: shoot-the-lock / laser-cut tasks). */
|
|
static int aimAbsQuantize(double *frac, double units)
|
|
{
|
|
int m = (int)units; /* floor for units >= 0 */
|
|
*frac += units - (double)m;
|
|
if (*frac >= 1.0) { m++; *frac -= 1.0; }
|
|
if (m > 10) m = 10;
|
|
return m;
|
|
}
|
|
|
|
static int aimAbsCompute(int *outSx, int *outSy)
|
|
{
|
|
*outSx = 0;
|
|
*outSy = 0;
|
|
|
|
/* Absolute aim needs a free OS cursor to read an absolute position from. */
|
|
if (!aimAbsolute || !mouseCaptureMode || mouseGrabbed || g_CurrentPlayer == NULL) {
|
|
s_absAimActive = 0;
|
|
return 0;
|
|
}
|
|
|
|
SDL_Window *w = SDL_GetMouseFocus();
|
|
int mx = 0, my = 0, ww = 0, wh = 0;
|
|
if (!w)
|
|
return 0;
|
|
SDL_GetWindowSize(w, &ww, &wh); /* void in SDL2; sanity-check the values */
|
|
if (ww <= 1 || wh <= 1)
|
|
return 0;
|
|
SDL_GetMouseState(&mx, &my);
|
|
|
|
s16 vw = viGetViewWidth(), vh = viGetViewHeight();
|
|
s16 bufW = getWidth320or440(), bufH = getHeight330or240();
|
|
if (vw <= 1 || vh <= 1 || bufW <= 1 || bufH <= 1)
|
|
return 0;
|
|
|
|
/* Cursor in the game's CFB space (320x240 NTSC), then NDC against the live
|
|
* render viewport rect. The TV-safe-area letterbox is part of the image,
|
|
* so centering on the viewport -- not the window -- is exact. */
|
|
double cxv = (double)mx / (double)ww * (double)bufW;
|
|
double cyv = (double)my / (double)wh * (double)bufH;
|
|
double ndcX = 2.0 * (cxv - ((double)viGetViewLeft() + (double)vw * 0.5)) / (double)vw;
|
|
double ndcY = 2.0 * (cyv - ((double)viGetViewTop() + (double)vh * 0.5)) / (double)vh;
|
|
if (mouseInvertY) ndcY = -ndcY;
|
|
if (ndcX > 1.0) ndcX = 1.0; else if (ndcX < -1.0) ndcX = -1.0;
|
|
if (ndcY > 1.0) ndcY = 1.0; else if (ndcY < -1.0) ndcY = -1.0;
|
|
|
|
/* The effective FOV the render actually uses (fr.c viSetupCurrentPlayerView),
|
|
* so scope zooms and Video.FovScale compose for free. */
|
|
f32 fovy = portScaleFovY(viGetFovY(), lvlGetCurrentStageToLoad() == LEVELID_TITLE);
|
|
f32 aspect = g_CurrentPlayer->aspect;
|
|
if (fovy < 5.0f || fovy > 170.0f || aspect < 0.1f)
|
|
return 0;
|
|
|
|
double tanV = tan(fovy * 0.5 * AIM_ABS_DEG2RAD);
|
|
double offYawR = atan(ndcX * tanV * (double)aspect) / AIM_ABS_DEG2RAD; /* deg, right + */
|
|
double offPitchD = atan(ndcY * tanV) / AIM_ABS_DEG2RAD; /* deg, down + */
|
|
|
|
float curT = g_CurrentPlayer->vv_theta; /* yaw, deg; a RIGHT turn DECREASES it */
|
|
float curV = g_CurrentPlayer->vv_verta; /* pitch, deg; looking DOWN increases it */
|
|
|
|
/* Re-anchor whenever the cursor moves or the effective FOV changes
|
|
* (scope zoom in/out). While held still, the target stays put and the
|
|
* crosshair converges on it and HOLDS. */
|
|
int fovQ = (int)(fovy * 4.0f); /* quarter-degree steps */
|
|
if (!s_absAimActive || mx != s_absLastMx || my != s_absLastMy || fovQ != s_absLastFovQ) {
|
|
s_absAimActive = 1;
|
|
s_absLastMx = mx; s_absLastMy = my; s_absLastFovQ = fovQ;
|
|
s_absTgtTheta = (float)(curT - offYawR);
|
|
s_absTgtVerta = (float)(curV + offPitchD);
|
|
}
|
|
|
|
float errT = aimAbsAngDiff(s_absTgtTheta, curT); /* +: theta must rise -> turn LEFT */
|
|
float errV = s_absTgtVerta - curV; /* +: look DOWN */
|
|
|
|
/* Demand against the RESIDUAL error after predicted coast (see COAST_K). */
|
|
f32 mult = viGetFovY() / 60.0f;
|
|
if (mult < 0.01f) mult = 0.01f;
|
|
float sT = g_CurrentPlayer->speedtheta;
|
|
float sV = g_CurrentPlayer->speedverta;
|
|
double eX = (double)errT - AIM_ABS_COAST_K * (double)sT * fabs((double)sT) / mult;
|
|
double eY = (double)errV - AIM_ABS_COAST_K * (double)sV * fabs((double)sV) / mult;
|
|
|
|
double uX = fabs(eX) / AIM_ABS_FULLERR_DEG; if (uX > 1.0) uX = 1.0;
|
|
double uY = fabs(eY) / AIM_ABS_FULLERR_DEG; if (uY > 1.0) uY = 1.0;
|
|
if (fabs(errT) < AIM_ABS_DEADBAND_DEG && fabs(eX) < AIM_ABS_DEADBAND_DEG) uX = 0.0;
|
|
if (fabs(errV) < AIM_ABS_DEADBAND_DEG && fabs(eY) < AIM_ABS_DEADBAND_DEG) uY = 0.0;
|
|
|
|
int magX = aimAbsQuantize(&s_absFracX, uX * 10.0);
|
|
int magY = aimAbsQuantize(&s_absFracY, uY * 10.0);
|
|
|
|
if (magX > 0) *outSx = (errT < 0.0f) ? 60 + magX : -(60 + magX);
|
|
if (magY > 0) *outSy = (errV > 0.0f) ? 60 + magY : -(60 + magY);
|
|
|
|
if (configGetInputLog()) {
|
|
sysLogPrintf(LOG_NOTE,
|
|
"GE_INPUTLOG absaim tgt=(%.2f,%.2f) cur=(%.2f,%.2f) err=(%.2f,%.2f) "
|
|
"spd=(%.3f,%.3f) stick=(%d,%d)",
|
|
(double)s_absTgtTheta, (double)s_absTgtVerta,
|
|
(double)curT, (double)curV,
|
|
(double)errT, (double)errV,
|
|
(double)sT, (double)sV, *outSx, *outSy);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
PD_CONSTRUCTOR static void inputConfigInit(void)
|
|
{
|
|
configRegisterInt("Input.MouseEnabled", &mouseEnabled, 0, 1);
|
|
configRegisterInt("Input.MouseCaptureMode", &mouseCaptureMode, 0, 1);
|
|
configRegisterInt("Input.MouseAimSpeed", &mouseAimSpeed, 1, 500);
|
|
configRegisterInt("Input.AimAbsolute", &aimAbsolute, 0, 1); /* D194 */
|
|
configRegisterInt("Input.AimBand", &aimBand, 5, 40);
|
|
configRegisterInt("Input.MouseTurnSpeed", &mouseTurnSpeed, 1, 500);
|
|
configRegisterInt("Input.MenuPointerSpeed", &menuPointerSpeed, 10, 500);
|
|
configRegisterInt("Input.MenuPointerMode", &menuPointerMode, 0, 1);
|
|
configRegisterInt("Input.HipfirePitchSpeed", &hipfirePitchSpeed, 10, 500);
|
|
configRegisterInt("Input.MouseInvertY", &mouseInvertY, 0, 1);
|
|
configRegisterInt("Input.MouseYScale", &mouseYScale, 1, 500);
|
|
configRegisterInt("Input.MouseSmoothing", &mouseSmoothing, 0, 90);
|
|
configRegisterInt("Input.MouseRawInput", &mouseRawInput, 0, 1);
|
|
configRegisterInt("Input.MouseDtDecouple", &mouseDtDecouple, 0, 1); /* D194(b) */
|
|
configRegisterInt("Input.MouseSensitivity", &mouseSensitivity, 1, 500); /* D238 */
|
|
configRegisterInt("Input.MouseAimCurve", &aimCurveGamma, 50, 400); /* D194(a), x100 */
|
|
configRegisterInt("Input.NaturalPitch", &naturalPitchMode, 0, 1); /* D194/D238 */
|
|
configRegisterInt("Input.PadDeadzone", &padDeadzone, 0, 30000);
|
|
configRegisterInt("Input.PadTriggerPct", &padTriggerPct, 1, 99);
|
|
configRegisterInt("Input.PadLookInvertY", &padLookInvertY, 0, 1);
|
|
|
|
/* D214: keyboard rebinding. Seed each buffer with its default so the knob
|
|
* is visible/editable in a fresh ge007.ini; configLoad() overwrites any the
|
|
* user set, then inputInit() calls inputRebuildBinds(). */
|
|
for (int a = 0; a < IA_COUNT; a++) {
|
|
strncpy(g_bindStr[a], kBindDefs[a].def, sizeof(g_bindStr[a]) - 1);
|
|
configRegisterString(kBindDefs[a].key, g_bindStr[a], sizeof(g_bindStr[a]));
|
|
}
|
|
}
|