Key bindings layer: skills on 1-7, weapon sets on F1-F7
Raw WM_KEYDOWN/WM_KEYUP are handled in both window procedures (the game swaps 0x8110d0 and 0x8122a0 with SetWindowLong; 0x8110d0 is the in-game one). Bound keys are swallowed and post the original hotkey's key event to the kernel, so F-keys work and bindings are layout independent ([keys] in sacred_re.ini). Replaces the WASD hotkey remap and the digit swap; W/A/S/D and the digit row no longer reach the game. Skill keys still cast through the right mouse button. Enabled switches removed. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9fb6c6b406
commit
cec2556fa3
11 files changed
+409
-279
No files matched your search
+14
-182
@@ -9,47 +9,28 @@
|
||||
// it when sent on their own.) Releasing the keys sends the target-less walk
|
||||
// order a released mouse button sends, which stops it in place.
|
||||
//
|
||||
// W, A and S are game hotkeys: W = action slot 3 (like Q/E/R/B/Space,
|
||||
// 0x613040), A = pick up items nearby, S = save game window. Key events are
|
||||
// filtered before the engine and UI see them: W/A/S/D are swallowed (key
|
||||
// repeat would otherwise reach the hotkeys), and replacement keys are rewritten
|
||||
// into the old letters (temporary choices, see docs/todo.md). Typing into a
|
||||
// text field (chat, console, save names) and Ctrl/Alt combinations are left
|
||||
// alone. See docs/wasd.md.
|
||||
//
|
||||
// sacred_re.ini:
|
||||
// [wasd]
|
||||
// Enabled=1
|
||||
// ActionKeyW=G ; key that now does what W did
|
||||
// ActionKeyA=V ; ... A (pick up nearby)
|
||||
// ActionKeyS=K ; ... S (save game window)
|
||||
// W, A and S are game hotkeys; the key bindings (src/mods/input.cpp) keep
|
||||
// W/A/S/D away from the game and move those actions to other keys. Typing into
|
||||
// a text field (chat, console, save names) is left alone. See docs/wasd.md.
|
||||
|
||||
#include "mods/wasd.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "mods/skillkeys.h"
|
||||
#include "re/config.h"
|
||||
#include "mods/input.h"
|
||||
#include "re/hook.h"
|
||||
#include "re/log.h"
|
||||
#include "re/patch.h"
|
||||
|
||||
namespace mods::wasd {
|
||||
namespace {
|
||||
|
||||
// --- sacred.exe ---------------------------------------------------------------
|
||||
constexpr uint32_t kObjectManager = 0x00ad3bc0; // object manager**
|
||||
constexpr uint32_t kUiManager = 0x017ecb3c; // cUI_Manager*: focused control at +0x148
|
||||
constexpr uint32_t kEventKeyVtable = 0x00895568;
|
||||
constexpr uint32_t kEventCreatureVtable = 0x0088e95c;
|
||||
constexpr uint32_t kTypeObject = 0x008e9648; // RTTI type descriptors
|
||||
constexpr uint32_t kTypeCreature = 0x008e9660;
|
||||
constexpr uint32_t kTypeControl = 0x009de168;
|
||||
constexpr uint32_t kTypeEditControl = 0x009e3608;
|
||||
|
||||
using GetObjectFn = void*(__fastcall*)(void* manager, void* edx, uint32_t id);
|
||||
using DynamicCastFn = void*(__cdecl*)(void* object, int vfDelta, uint32_t source, uint32_t target, int isReference);
|
||||
@@ -75,133 +56,14 @@ struct CreatureEvent {
|
||||
};
|
||||
static_assert(sizeof(CreatureEvent) == 0x44);
|
||||
|
||||
// cEvent_key: +4 1 = pressed / character, 0 = released; +6 and +8 the key
|
||||
// (character for WM_CHAR, virtual key for WM_KEYUP); +0xa modifiers
|
||||
// (0x3 Shift, 0xc Ctrl, 0x30 Alt).
|
||||
constexpr size_t kKeyRaw = 0x6;
|
||||
constexpr size_t kKey = 0x8;
|
||||
constexpr size_t kKeyModifiers = 0xa;
|
||||
constexpr uint16_t kModifiersCtrlAlt = 0x3c;
|
||||
|
||||
// Walk orders aim this far (world units) from the creature, about where the
|
||||
// cursor would be.
|
||||
constexpr double kTargetDistance = 300.0;
|
||||
|
||||
bool Enabled() {
|
||||
static const bool enabled = re::ConfigInt("wasd", "Enabled", 1) != 0;
|
||||
return enabled;
|
||||
}
|
||||
|
||||
bool KeyDown(int key) {
|
||||
return (GetAsyncKeyState(key) & 0x8000) != 0;
|
||||
}
|
||||
|
||||
// Whether a text field has the keyboard (as the window procedure checks it).
|
||||
bool TypingIntoTextField() {
|
||||
auto* ui = *reinterpret_cast<uint8_t**>(kUiManager);
|
||||
if (!ui) {
|
||||
return false;
|
||||
}
|
||||
void* focus = *reinterpret_cast<void**>(ui + 0x148);
|
||||
if (!focus) {
|
||||
return false;
|
||||
}
|
||||
auto* edit = static_cast<uint8_t*>(DynamicCast(focus, 0, kTypeControl, kTypeEditControl, 0));
|
||||
return edit && ((*reinterpret_cast<uint32_t*>(edit + 0x10) >> 16) & 1) == 0;
|
||||
}
|
||||
|
||||
// --- hotkey remapping ----------------------------------------------------------
|
||||
|
||||
struct Remap {
|
||||
char from; // replacement key the player presses
|
||||
char to; // original hotkey letter
|
||||
};
|
||||
Remap g_remaps[3];
|
||||
|
||||
char ConfigKey(const char* key, char fallback) {
|
||||
const std::string value = re::ConfigString("wasd", key, "");
|
||||
if (value.size() == 1 && isalpha(static_cast<unsigned char>(value[0]))) {
|
||||
return static_cast<char>(toupper(static_cast<unsigned char>(value[0])));
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
void LoadRemaps() {
|
||||
g_remaps[0] = {ConfigKey("ActionKeyW", 'G'), 'W'};
|
||||
g_remaps[1] = {ConfigKey("ActionKeyA", 'V'), 'A'};
|
||||
g_remaps[2] = {ConfigKey("ActionKeyS", 'K'), 'S'};
|
||||
}
|
||||
|
||||
bool IsMovementKey(int upper) {
|
||||
return upper == 'W' || upper == 'A' || upper == 'S' || upper == 'D';
|
||||
}
|
||||
|
||||
// Returns the key the game should see instead of `key` (0: none). Also
|
||||
// applies the skill hotkeys' digit swap (src/mods/skillkeys.cpp).
|
||||
uint16_t RemapKey(uint16_t key) {
|
||||
key = mods::skillkeys::RemapKey(key);
|
||||
if (key >= 0x80 || !Enabled()) {
|
||||
return key;
|
||||
}
|
||||
const int upper = toupper(key);
|
||||
const bool lower = upper != key;
|
||||
if (IsMovementKey(upper)) {
|
||||
return 0;
|
||||
}
|
||||
for (const Remap& remap : g_remaps) {
|
||||
if (upper == remap.from) {
|
||||
return static_cast<uint16_t>(lower ? tolower(remap.to) : remap.to);
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
thread_local int g_keyEventDepth = 0;
|
||||
|
||||
// RAII: rewrite a key event for the scope of the outermost receiver.
|
||||
class KeyEventRemap {
|
||||
public:
|
||||
explicit KeyEventRemap(void* event) {
|
||||
if (g_keyEventDepth++ > 0 || !event ||
|
||||
*static_cast<uint32_t*>(event) != kEventKeyVtable) {
|
||||
return;
|
||||
}
|
||||
auto* bytes = static_cast<uint8_t*>(event);
|
||||
const uint16_t modifiers = *reinterpret_cast<uint16_t*>(bytes + kKeyModifiers);
|
||||
if ((modifiers & kModifiersCtrlAlt) != 0 || TypingIntoTextField()) {
|
||||
return;
|
||||
}
|
||||
key_ = reinterpret_cast<uint16_t*>(bytes + kKey);
|
||||
raw_ = reinterpret_cast<uint16_t*>(bytes + kKeyRaw);
|
||||
originalKey_ = *key_;
|
||||
originalRaw_ = *raw_;
|
||||
*key_ = RemapKey(originalKey_);
|
||||
*raw_ = RemapKey(originalRaw_);
|
||||
// W/A/S/D: the game must not see these at all (key repeat while held
|
||||
// interrupts walking); movement polls the keys itself.
|
||||
swallow_ = *key_ == 0;
|
||||
}
|
||||
bool Swallow() const { return swallow_; }
|
||||
~KeyEventRemap() {
|
||||
--g_keyEventDepth;
|
||||
if (key_) {
|
||||
*key_ = originalKey_;
|
||||
*raw_ = originalRaw_;
|
||||
}
|
||||
}
|
||||
KeyEventRemap(const KeyEventRemap&) = delete;
|
||||
KeyEventRemap& operator=(const KeyEventRemap&) = delete;
|
||||
|
||||
private:
|
||||
uint16_t* key_ = nullptr;
|
||||
uint16_t* raw_ = nullptr;
|
||||
uint16_t originalKey_ = 0;
|
||||
uint16_t originalRaw_ = 0;
|
||||
bool swallow_ = false;
|
||||
};
|
||||
|
||||
// --- movement ------------------------------------------------------------------
|
||||
|
||||
void* ControlledCreature(uint8_t* engine) {
|
||||
void* manager = *reinterpret_cast<void**>(kObjectManager);
|
||||
if (!manager) {
|
||||
@@ -235,6 +97,12 @@ struct MoveState {
|
||||
};
|
||||
MoveState g_move;
|
||||
|
||||
} // namespace
|
||||
|
||||
bool IsMovementKey(int vk) {
|
||||
return (vk == 'W' || vk == 'A' || vk == 'S' || vk == 'D');
|
||||
}
|
||||
|
||||
// Called once per engine tick (10 Hz), after the game's own input handling.
|
||||
void Update(uint8_t* engine) {
|
||||
// World axes on screen: (-1,-1) up, (1,1) down, (-1,1) left, (1,-1) right.
|
||||
@@ -242,7 +110,7 @@ void Update(uint8_t* engine) {
|
||||
int dy = 0;
|
||||
const uint32_t flags = *reinterpret_cast<uint32_t*>(engine + kEngineFlags);
|
||||
const bool mouseMoving = (flags & 0x10) != 0;
|
||||
if (!mouseMoving && (flags & 0x4000) == 0 && !TypingIntoTextField()) {
|
||||
if (!mouseMoving && (flags & 0x4000) == 0 && !mods::input::TypingIntoTextField()) {
|
||||
if (KeyDown('W')) { dx -= 1; dy -= 1; }
|
||||
if (KeyDown('S')) { dx += 1; dy += 1; }
|
||||
if (KeyDown('A')) { dx -= 1; dy += 1; }
|
||||
@@ -269,17 +137,6 @@ void Update(uint8_t* engine) {
|
||||
g_move.moving = true;
|
||||
}
|
||||
|
||||
void Startup() {
|
||||
if (!Enabled()) {
|
||||
return;
|
||||
}
|
||||
LoadRemaps();
|
||||
re::Log("wasd: movement on W/A/S/D; old W/A/S actions on %c/%c/%c", g_remaps[0].from, g_remaps[1].from,
|
||||
g_remaps[2].from);
|
||||
}
|
||||
RE_STARTUP(WasdStartup, Startup);
|
||||
|
||||
} // namespace
|
||||
} // namespace mods::wasd
|
||||
|
||||
namespace game {
|
||||
@@ -290,34 +147,9 @@ void __fastcall Engine_Tick(void* self, void* edx, void* event);
|
||||
RE_HOOK(0x0060f1d0, Engine_Tick);
|
||||
void __fastcall Engine_Tick(void* self, void* edx, void* event) {
|
||||
Engine_Tick_original(self, edx, event);
|
||||
if (mods::wasd::Enabled() && self) {
|
||||
if (self) {
|
||||
mods::wasd::Update(static_cast<uint8_t*>(self));
|
||||
}
|
||||
}
|
||||
|
||||
// cEngine::vf00 (0x618130), the engine's event receiver: hotkeys, after
|
||||
// offering every event to the UI manager.
|
||||
char __fastcall Engine_ReceiveEvent(void* self, void* edx, void* event);
|
||||
RE_HOOK(0x00618130, Engine_ReceiveEvent);
|
||||
char __fastcall Engine_ReceiveEvent(void* self, void* edx, void* event) {
|
||||
mods::wasd::KeyEventRemap remap(event);
|
||||
if (remap.Swallow()) {
|
||||
return 1;
|
||||
}
|
||||
return Engine_ReceiveEvent_original(self, edx, event);
|
||||
}
|
||||
|
||||
// cUI_Manager::vf00 (0x757120): UI hotkeys (S = save game window), also
|
||||
// reached without the engine.
|
||||
char __fastcall UiManager_ReceiveEvent(void* self, void* edx, void* event);
|
||||
RE_HOOK(0x00757120, UiManager_ReceiveEvent);
|
||||
char __fastcall UiManager_ReceiveEvent(void* self, void* edx, void* event) {
|
||||
mods::wasd::KeyEventRemap remap(event);
|
||||
if (remap.Swallow()) {
|
||||
return 1;
|
||||
}
|
||||
return UiManager_ReceiveEvent_original(self, edx, event);
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
|
||||
Reference in new issue
Block a user