Slight refactor, tweaked the acceleration value and deadzones a little bit

This commit is contained in:
Louvenarde 2021-08-14 10:38:00 +02:00
parent ea926dfe67
commit cbfdde2029
2 changed files with 30 additions and 11 deletions

View File

@ -16,6 +16,7 @@ namespace Components
std::chrono::milliseconds XInput::lastNavigationTime = 0ms; std::chrono::milliseconds XInput::lastNavigationTime = 0ms;
std::chrono::milliseconds XInput::msBetweenNavigations = 220ms; std::chrono::milliseconds XInput::msBetweenNavigations = 220ms;
// This should be read from a text file in the players/ folder, most probably / or from config_mp.cfg
std::vector<XInput::ActionMapping> mappings = { std::vector<XInput::ActionMapping> mappings = {
XInput::ActionMapping(XINPUT_GAMEPAD_A, "gostand"), XInput::ActionMapping(XINPUT_GAMEPAD_A, "gostand"),
XInput::ActionMapping(XINPUT_GAMEPAD_B, "stance"), XInput::ActionMapping(XINPUT_GAMEPAD_B, "stance"),
@ -33,6 +34,7 @@ namespace Components
XInput::ActionMapping(XINPUT_GAMEPAD_DPAD_UP, "actionslot 4"), XInput::ActionMapping(XINPUT_GAMEPAD_DPAD_UP, "actionslot 4"),
}; };
// Same thing
std::vector<XInput::MenuMapping> menuMappings = { std::vector<XInput::MenuMapping> menuMappings = {
XInput::MenuMapping(XINPUT_GAMEPAD_A, Game::keyNum_t::K_KP_ENTER), XInput::MenuMapping(XINPUT_GAMEPAD_A, Game::keyNum_t::K_KP_ENTER),
XInput::MenuMapping(XINPUT_GAMEPAD_B, Game::keyNum_t::K_ESCAPE), XInput::MenuMapping(XINPUT_GAMEPAD_B, Game::keyNum_t::K_ESCAPE),
@ -42,16 +44,24 @@ namespace Components
XInput::MenuMapping(XINPUT_GAMEPAD_DPAD_DOWN, Game::keyNum_t::K_KP_DOWNARROW) XInput::MenuMapping(XINPUT_GAMEPAD_DPAD_DOWN, Game::keyNum_t::K_KP_DOWNARROW)
}; };
void GetLeftStick01Value(XINPUT_STATE* xiState, float& x, float& y) { void XInput::GetLeftStick01Value(XINPUT_STATE* xiState, float& x, float& y) {
float maxValue = (float)(std::numeric_limits<SHORT>().max() - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE); GamepadStickTo01(xiState->Gamepad.sThumbLX, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, x);
x = abs(xiState->Gamepad.sThumbLX) > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ? (xiState->Gamepad.sThumbLX - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE * SIGN(xiState->Gamepad.sThumbLX)) / maxValue : .0f; GamepadStickTo01(xiState->Gamepad.sThumbLY, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, y);
y = abs(xiState->Gamepad.sThumbLY) > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE ? (xiState->Gamepad.sThumbLY - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE * SIGN(xiState->Gamepad.sThumbLY)) / maxValue : .0f;
} }
void GetRightStick01Value(XINPUT_STATE* xiState, float& x, float& y) { void XInput::GetRightStick01Value(XINPUT_STATE* xiState, float& x, float& y) {
float maxValue = (float)(std::numeric_limits<SHORT>().max() - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE); GamepadStickTo01(xiState->Gamepad.sThumbRX, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE, x);
x = abs(xiState->Gamepad.sThumbRX) > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ? (xiState->Gamepad.sThumbRX - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE * SIGN(xiState->Gamepad.sThumbRX)) / maxValue : .0f; GamepadStickTo01(xiState->Gamepad.sThumbRY, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE, y);
y = abs(xiState->Gamepad.sThumbRY) > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ? (xiState->Gamepad.sThumbRY - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE * SIGN(xiState->Gamepad.sThumbRY)) / maxValue : .0f; }
void XInput::GamepadStickTo01(SHORT value, SHORT deadzone, float& output01) {
float maxValue = (float)(std::numeric_limits<SHORT>().max() - deadzone);
output01 = abs(value) > deadzone ? (value - deadzone * SIGN(value)) / maxValue : .0f;
// log2 allows for a more neat value curve from 0 to 1
// It is not functional yet, because I suck at maths
//float test = (log2(abs(output01) + 1.f) - 1.f) * SIGN(value);
} }
void XInput::Vibrate(int leftVal, int rightVal) void XInput::Vibrate(int leftVal, int rightVal)
@ -380,8 +390,12 @@ namespace Components
float viewStickX, viewStickY; float viewStickX, viewStickY;
GetRightStick01Value(xiState, viewStickX, viewStickY); GetRightStick01Value(xiState, viewStickX, viewStickY);
#ifdef DEBUG
Components::Logger::Print("X:%f \nY:%f\n(holdingMaxX: %d)\n", viewStickX, viewStickY, XInput::isHoldingMaxLookX);
#endif
// Gamepad horizontal acceleration on view // Gamepad horizontal acceleration on view
if (abs(viewStickX) > 0.9f) { if (abs(viewStickX) > 0.80f) {
if (!XInput::isHoldingMaxLookX) { if (!XInput::isHoldingMaxLookX) {
XInput::isHoldingMaxLookX = true; XInput::isHoldingMaxLookX = true;
XInput::timeAtFirstHeldMaxLookX = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()); XInput::timeAtFirstHeldMaxLookX = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
@ -402,6 +416,8 @@ namespace Components
XInput::isHoldingMaxLookX = false; XInput::isHoldingMaxLookX = false;
XInput::timeAtFirstHeldMaxLookX = 0ms; XInput::timeAtFirstHeldMaxLookX = 0ms;
viewStickX *= lockedSensitivityMultiplier; viewStickX *= lockedSensitivityMultiplier;
Components::Logger::Print("multiplier will be %f\n", lockedSensitivityMultiplier);
} }
float adsMultiplier = 1.0f; float adsMultiplier = 1.0f;
@ -497,7 +513,7 @@ namespace Components
Utils::Hook(0x5A6829, unk_CheckKeyHook, HOOK_CALL).install()->quick(); Utils::Hook(0x5A6829, unk_CheckKeyHook, HOOK_CALL).install()->quick();
Game::Dvar_RegisterFloat("xpad_sensitivity", 1.9f, 0.1f, 10.0f, Game::DVAR_FLAG_SAVED, "View sensitivity for XInput-compatible gamepads"); Game::Dvar_RegisterFloat("xpad_sensitivity", 1.9f, 0.1f, 10.0f, Game::DVAR_FLAG_SAVED, "View sensitivity for XInput-compatible gamepads");
Game::Dvar_RegisterInt("xpad_early_time", 200, 0, 1000, Game::DVAR_FLAG_SAVED, "Time (in milliseconds) of reduced view sensitivity"); Game::Dvar_RegisterInt("xpad_early_time", 130, 0, 1000, Game::DVAR_FLAG_SAVED, "Time (in milliseconds) of reduced view sensitivity");
Game::Dvar_RegisterFloat("xpad_early_multiplier", 0.25f, 0.01f, 1.0f, Game::DVAR_FLAG_SAVED, "By how much the view sensitivity is multiplied during xpad_early_time when moving the view stick"); Game::Dvar_RegisterFloat("xpad_early_multiplier", 0.25f, 0.01f, 1.0f, Game::DVAR_FLAG_SAVED, "By how much the view sensitivity is multiplied during xpad_early_time when moving the view stick");
Game::Dvar_RegisterFloat("xpad_horizontal_multiplier", 1.5f, 1.0f, 20.0f, Game::DVAR_FLAG_SAVED, "Horizontal view sensitivity multiplier"); Game::Dvar_RegisterFloat("xpad_horizontal_multiplier", 1.5f, 1.0f, 20.0f, Game::DVAR_FLAG_SAVED, "Horizontal view sensitivity multiplier");
Game::Dvar_RegisterFloat("xpad_vertical_multiplier", 0.8f, 1.0f, 20.0f, Game::DVAR_FLAG_SAVED, "Vertical view sensitivity multiplier"); Game::Dvar_RegisterFloat("xpad_vertical_multiplier", 0.8f, 1.0f, 20.0f, Game::DVAR_FLAG_SAVED, "Vertical view sensitivity multiplier");

View File

@ -52,7 +52,6 @@ namespace Components
static int unk_CheckKeyHook(int localClientNum, Game::keyNum_t keyCode); static int unk_CheckKeyHook(int localClientNum, Game::keyNum_t keyCode);
static void MouseOverride(Game::clientActive_t* clientActive, float* my, float* mx); static void MouseOverride(Game::clientActive_t* clientActive, float* my, float* mx);
static char MovementOverride(int a1, Game::usercmd_s* cmd);
static void Vibrate(int leftVal = 0, int rightVal = 0); static void Vibrate(int leftVal = 0, int rightVal = 0);
static void CL_FrameStub(); static void CL_FrameStub();
@ -68,5 +67,9 @@ namespace Components
static void MSG_ReadDeltaUsercmdKeyStub(); static void MSG_ReadDeltaUsercmdKeyStub();
static void MSG_ReadDeltaUsercmdKeyStub2(); static void MSG_ReadDeltaUsercmdKeyStub2();
static void GetLeftStick01Value(XINPUT_STATE* xiState, float& x, float& y);
static void GetRightStick01Value(XINPUT_STATE* xiState, float& x, float& y);
static void GamepadStickTo01(SHORT value, SHORT deadzone, float& output01);
}; };
} }