-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathMovementOverride.cs
196 lines (166 loc) · 8.44 KB
/
MovementOverride.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Dalamud.Game.Config;
using FFXIVClientStructs.FFXIV.Client.Game.Character;
using FFXIVClientStructs.FFXIV.Client.Game.Object;
using FFXIVClientStructs.FFXIV.Client.System.Input;
using FFXIVClientStructs.FFXIV.Client.UI;
using ImGuiNET;
using System.Runtime.InteropServices;
namespace BossMod;
[StructLayout(LayoutKind.Explicit, Size = 0x18)]
public unsafe struct PlayerMoveControllerFlyInput
{
[FieldOffset(0x0)] public float Forward;
[FieldOffset(0x4)] public float Left;
[FieldOffset(0x8)] public float Up;
[FieldOffset(0xC)] public float Turn;
[FieldOffset(0x10)] public float u10;
[FieldOffset(0x14)] public byte DirMode;
[FieldOffset(0x15)] public byte HaveBackwardOrStrafe;
}
public sealed unsafe class MovementOverride : IDisposable
{
public Vector3? DesiredDirection;
private float UserMoveLeft;
private float UserMoveUp;
private float ActualMoveLeft;
private float ActualMoveUp;
private readonly ActionTweaksConfig _tweaksConfig = Service.Config.Get<ActionTweaksConfig>();
private bool _movementBlocked;
private bool _controlBlocked;
private bool _legacyMode;
public bool IsMoving() => ActualMoveLeft != 0 || ActualMoveUp != 0;
public bool IsMoveRequested() => UserMoveLeft != 0 || UserMoveUp != 0;
public bool IsForceUnblocked() => _tweaksConfig.MoveEscapeHatch switch
{
ActionTweaksConfig.ModifierKey.Ctrl => ImGui.GetIO().KeyCtrl,
ActionTweaksConfig.ModifierKey.Alt => ImGui.GetIO().KeyAlt,
ActionTweaksConfig.ModifierKey.Shift => ImGui.GetIO().KeyShift,
ActionTweaksConfig.ModifierKey.M12 => UIInputData.Instance()->UIFilteredCursorInputs.MouseButtonHeldFlags.HasFlag(MouseButtonFlags.LBUTTON | MouseButtonFlags.RBUTTON),
_ => false,
};
public bool MovementBlocked
{
get => _movementBlocked && !IsForceUnblocked();
set => _movementBlocked = value;
}
public static readonly float* ForcedMovementDirection = (float*)Service.SigScanner.GetStaticAddressFromSig("F3 0F 11 0D ?? ?? ?? ?? 48 85 DB");
private delegate bool RMIWalkIsInputEnabled(void* self);
private readonly RMIWalkIsInputEnabled _rmiWalkIsInputEnabled1;
private readonly RMIWalkIsInputEnabled _rmiWalkIsInputEnabled2;
private delegate void RMIWalkDelegate(void* self, float* sumLeft, float* sumForward, float* sumTurnLeft, byte* haveBackwardOrStrafe, byte* a6, byte bAdditiveUnk);
private readonly HookAddress<RMIWalkDelegate> _rmiWalkHook;
private delegate void RMIFlyDelegate(void* self, PlayerMoveControllerFlyInput* result);
private readonly HookAddress<RMIFlyDelegate> _rmiFlyHook;
// input source flags: 1 = kb/mouse, 2 = gamepad
private delegate byte MoveControlIsInputActiveDelegate(void* self, byte inputSourceFlags);
private readonly HookAddress<MoveControlIsInputActiveDelegate> _mcIsInputActiveHook;
public MovementOverride()
{
var rmiWalkIsInputEnabled1Addr = Service.SigScanner.ScanText("E8 ?? ?? ?? ?? 84 C0 75 10 38 43 3C");
var rmiWalkIsInputEnabled2Addr = Service.SigScanner.ScanText("E8 ?? ?? ?? ?? 84 C0 75 03 88 47 3F");
Service.Log($"RMIWalkIsInputEnabled1 address: 0x{rmiWalkIsInputEnabled1Addr:X}");
Service.Log($"RMIWalkIsInputEnabled2 address: 0x{rmiWalkIsInputEnabled2Addr:X}");
_rmiWalkIsInputEnabled1 = Marshal.GetDelegateForFunctionPointer<RMIWalkIsInputEnabled>(rmiWalkIsInputEnabled1Addr);
_rmiWalkIsInputEnabled2 = Marshal.GetDelegateForFunctionPointer<RMIWalkIsInputEnabled>(rmiWalkIsInputEnabled2Addr);
_rmiWalkHook = new("E8 ?? ?? ?? ?? 80 7B 3E 00 48 8D 3D", RMIWalkDetour);
_rmiFlyHook = new("E8 ?? ?? ?? ?? 0F B6 0D ?? ?? ?? ?? B8", RMIFlyDetour);
_mcIsInputActiveHook = new("E8 ?? ?? ?? ?? 84 C0 74 09 84 DB 74 1A", MCIsInputActiveDetour);
Service.GameConfig.UiControlChanged += OnConfigChanged;
UpdateLegacyMode();
}
public void Dispose()
{
Service.GameConfig.UiControlChanged -= OnConfigChanged;
_movementBlocked = false;
UserMoveLeft = 0;
UserMoveUp = 0;
ActualMoveLeft = 0;
ActualMoveUp = 0;
_mcIsInputActiveHook.Dispose();
_rmiWalkHook.Dispose();
_rmiFlyHook.Dispose();
}
private void RMIWalkDetour(void* self, float* sumLeft, float* sumForward, float* sumTurnLeft, byte* haveBackwardOrStrafe, byte* a6, byte bAdditiveUnk)
{
_controlBlocked = false;
_rmiWalkHook.Original(self, sumLeft, sumForward, sumTurnLeft, haveBackwardOrStrafe, a6, bAdditiveUnk);
UserMoveLeft = *sumLeft;
UserMoveUp = *sumForward;
// TODO: this allows AI mode to move even if movement is "blocked", is this the right behavior? AI mode should try to avoid moving while casting anyway...
if (MovementBlocked)
{
*sumLeft = 0;
*sumForward = 0;
}
// TODO: we really need to introduce some extra checks that PlayerMoveController::readInput does - sometimes it skips reading input, and returning something non-zero breaks stuff...
var movementAllowed = bAdditiveUnk == 0 && _rmiWalkIsInputEnabled1(self) && _rmiWalkIsInputEnabled2(self); //&& !_movementBlocked
if (movementAllowed && *sumLeft == 0 && *sumForward == 0 && DirectionToDestination(false) is var relDir && relDir != null)
{
var dir = relDir.Value.h.ToDirection();
*sumLeft = dir.X;
*sumForward = dir.Z;
}
if (_tweaksConfig.MisdirectionThreshold < 180 && PlayerHasMisdirection())
{
var currentDir = Angle.FromDirection(new(*sumLeft, *sumForward)) + ForwardMovementDirection();
var dirDelta = currentDir - ForcedMovementDirection->Radians();
if (dirDelta.Normalized().Abs().Deg > _tweaksConfig.MisdirectionThreshold)
{
*sumLeft = *sumForward = 0;
_controlBlocked = true;
}
}
ActualMoveLeft = *sumLeft;
ActualMoveUp = *sumForward;
}
private void RMIFlyDetour(void* self, PlayerMoveControllerFlyInput* result)
{
_controlBlocked = false;
_rmiFlyHook.Original(self, result);
// TODO: we really need to introduce some extra checks that PlayerMoveController::readInput does - sometimes it skips reading input, and returning something non-zero breaks stuff...
if (result->Forward == 0 && result->Left == 0 && result->Up == 0 && DirectionToDestination(true) is var relDir && relDir != null)
{
var dir = relDir.Value.h.ToDirection();
result->Forward = dir.Z;
result->Left = dir.X;
result->Up = relDir.Value.v.Rad;
}
}
private byte MCIsInputActiveDetour(void* self, byte inputSourceFlags)
{
var res = _mcIsInputActiveHook.Original(self, inputSourceFlags);
if (res != 0 && _controlBlocked)
res = 0;
return res;
}
private (Angle h, Angle v)? DirectionToDestination(bool allowVertical)
{
if (DesiredDirection == null || DesiredDirection.Value == default)
return null;
var player = GameObjectManager.Instance()->Objects.IndexSorted[0].Value;
if (player == null)
return null;
var dxz = new WDir(DesiredDirection.Value.X, DesiredDirection.Value.Z);
var dirH = Angle.FromDirection(dxz);
var dirV = allowVertical ? Angle.FromDirection(new(DesiredDirection.Value.Y, dxz.Length())) : default;
return (dirH - ForwardMovementDirection(), dirV);
}
private Angle ForwardMovementDirection() => _legacyMode ? Camera.Instance!.CameraAzimuth.Radians() + 180.Degrees() : GameObjectManager.Instance()->Objects.IndexSorted[0].Value->Rotation.Radians();
private bool PlayerHasMisdirection()
{
var player = (Character*)GameObjectManager.Instance()->Objects.IndexSorted[0].Value;
var sm = player != null && player->IsCharacter() ? player->GetStatusManager() : null;
if (sm == null)
return false;
for (int i = 0; i < sm->NumValidStatuses; ++i)
if (sm->Status[i].StatusId is 1422 or 2936 or 3694 or 3909)
return true;
return false;
}
private void OnConfigChanged(object? sender, ConfigChangeEvent evt) => UpdateLegacyMode();
private void UpdateLegacyMode()
{
_legacyMode = Service.GameConfig.UiControl.TryGetUInt("MoveMode", out var mode) && mode == 1;
Service.Log($"Legacy mode is now {(_legacyMode ? "enabled" : "disabled")}");
}
}