Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope and scanner mouse sensitivity modifiers #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/include/common/config/GameConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct GameConfig

// Audio
CfgVar<float> level_sound_volume = 1.0f;
CfgVar<float> scope_sensitivity_modifier = 1.0f;
CfgVar<float> scanner_sensitivity_modifier = 1.0f;
CfgVar<bool> eax_sound = true;

// Multiplayer
Expand Down
2 changes: 2 additions & 0 deletions common/src/config/GameConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ bool GameConfig::visit_vars(T&& visitor, bool is_save)
result &= visitor(dash_faction_key, "Fast Start", fast_start);
result &= visitor(dash_faction_key, "Scoreboard Animations", scoreboard_anim);
result &= visitor(dash_faction_key, "Level Sound Volume", level_sound_volume);
result &= visitor(dash_faction_key, "Scope Sensitivity Modifier", scope_sensitivity_modifier);
result &= visitor(dash_faction_key, "Scanner Sensitivity Modifier", scanner_sensitivity_modifier);
result &= visitor(dash_faction_key, "Allow Overwriting Game Files", allow_overwrite_game_files);
result &= visitor(dash_faction_key, "Version", dash_faction_version);
result &= visitor(dash_faction_key, "Swap Assault Rifle Controls", swap_assault_rifle_controls);
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Version 1.9.0 (not released yet)
- Do not load unnecessary VPPs in dedicated server mode
- Add level filename to "Level Initializing" console message
- Properly handle WM_PAINT in dedicated server, may improve performance (DF bug)
- Add `scope_sensitivity_modifier` and `scanner_sensitivity_modifier` commands

Version 1.8.0 (released 2022-09-17)
-----------------------------------
Expand Down
75 changes: 75 additions & 0 deletions game_patch/input/mouse.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cassert>
#include <algorithm>
#include <patch_common/FunHook.h>
#include <patch_common/CodeInjection.h>
#include <patch_common/AsmWriter.h>
Expand Down Expand Up @@ -111,6 +112,73 @@ ConsoleCommand2 ms_cmd{
"ms <value>",
};

float scope_sensitivity_factor = 1.0f;
float scanner_sensitivity_factor = 1.0f;
GooberRF marked this conversation as resolved.
Show resolved Hide resolved

constexpr float clamp_sensitivity_modifier(float modifier)
{
constexpr const float min = 0.01f; // prevent division by zero in scope sens
constexpr const float max = 4.0f;
return std::clamp(modifier, min, max);
}

void patch_scope_and_scanner_sensitivity()
{
AsmWriter{0x004309B1}.fmul<float>(AsmRegMem{&scope_sensitivity_factor});
AsmWriter{0x004309DE}.fmul<float>(AsmRegMem{&scanner_sensitivity_factor});
}

void update_scope_sensitivity()
{
float modifier = clamp_sensitivity_modifier(g_game_config.scope_sensitivity_modifier);
scope_sensitivity_factor = rf::scope_sensitivity_constant * (1.0f / modifier);
}

void update_scanner_sensitivity()
{
float modifier = clamp_sensitivity_modifier(g_game_config.scanner_sensitivity_modifier);
scanner_sensitivity_factor = rf::scanner_sensitivity_constant * modifier;
}
GooberRF marked this conversation as resolved.
Show resolved Hide resolved

ConsoleCommand2 scope_sens_cmd{
"scope_sensitivity_modifier",
[](std::optional<float> value_opt) {
if (value_opt) {
float value = clamp_sensitivity_modifier(value_opt.value());
GooberRF marked this conversation as resolved.
Show resolved Hide resolved
g_game_config.scope_sensitivity_modifier = value;
g_game_config.save();
update_scope_sensitivity();
//rf::console::print("Scope sensitivity modifier set to {:.2f}", value);
}
else {
rf::console::print("Scope sensitivity modifier: {:.2f}",
static_cast<float>(g_game_config.scope_sensitivity_modifier));
}

},
"Sets mouse sensitivity modifier used while scoped in.",
"scope_sensitivity_modifier <value> (valid range: 0.01 - 4.00)",
};

ConsoleCommand2 scanner_sens_cmd{
"scanner_sensitivity_modifier",
[](std::optional<float> value_opt) {
if (value_opt) {
float value = clamp_sensitivity_modifier(value_opt.value());
g_game_config.scanner_sensitivity_modifier = value;
g_game_config.save();
update_scanner_sensitivity();
//rf::console::print("Scanner sensitivity modifier set to {:.2f}", value);
}
else {
rf::console::print("Scanner sensitivity modifier: {:.2f}",
static_cast<float>(g_game_config.scanner_sensitivity_modifier));
}
},
"Sets mouse sensitivity modifier used while in a rail scanner.",
"scanner_sensitivity_modifier <value> (valid range: 0.01 - 4.00)",
};

rf::Vector3 fw_vector_from_non_linear_yaw_pitch(float yaw, float pitch)
{
// Based on RF code
Expand Down Expand Up @@ -242,6 +310,11 @@ ConsoleCommand2 linear_pitch_cmd{

void mouse_apply_patch()
{
// Scale zoomed sensitivity
patch_scope_and_scanner_sensitivity();
update_scope_sensitivity();
update_scanner_sensitivity();

// Disable mouse when window is not active
mouse_eval_deltas_hook.install();

Expand All @@ -262,5 +335,7 @@ void mouse_apply_patch()
// Commands
input_mode_cmd.register_cmd();
ms_cmd.register_cmd();
scope_sens_cmd.register_cmd();
scanner_sens_cmd.register_cmd();
linear_pitch_cmd.register_cmd();
}
2 changes: 2 additions & 0 deletions game_patch/rf/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ namespace rf
static auto& mouse_set_visible = addr_as_ref<void(bool visible)>(0x0051E680);
static auto& key_process_event = addr_as_ref<void(int scan_code, int key_down, int delta_time)>(0x0051E6C0);

static auto& scope_sensitivity_constant = addr_as_ref<float>(0x005895C0);
static auto& scanner_sensitivity_constant = addr_as_ref<float>(0x005893D4);
static auto& mouse_initialized = addr_as_ref<uint8_t>(0x01885461);
static auto& direct_input_disabled = addr_as_ref<bool>(0x005A4F88);
static auto& di_mouse = addr_as_ref<LPDIRECTINPUTDEVICE8A>(0x0188545C);
Expand Down