summaryrefslogtreecommitdiff
path: root/Zone.cs
blob: 8fb933aa4bd5328526a5b5258a64f3c18b99bb33 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#define QUARANTINED_FEATURES

using System.Numerics;
using System.Runtime.InteropServices;

using ImGuiNET;
using SharpPluginLoader.Core;
using SharpPluginLoader.Core.Memory;
using SharpPluginLoader.Core.Entities;

namespace PlayerPoser
{
    using static Utility;

    internal static unsafe class Zone
    {
        internal enum ZoneState
        {
            Unknown = 0,
            Hub = 1,
            Combat = 2
        }

#if QUARANTINED_FEATURES
        private static ZoneState forceZoneState = ZoneState.Unknown;
        private static ZoneState lastZoneState = ZoneState.Combat; // Save select default.
        private static NativeAction<nint, int> setZoneStateFunc;
        private static bool zoneStateManualInvoke = false;

        private static NativeAction<nint> setPlayerController1;
        private static NativeAction<nint> setPlayerController5;

        private static bool forcePassiveInCombatZone = false;
        private static Patch zoneStateForcePassive1;
        private static Patch zoneStateForcePassive2;

        private static bool forceCombatInPassiveZone = false;
        private static Patch zoneStateForceCombat1;
        private static Patch zoneStateForceCombat2;
#endif
        private delegate void ZoneStateDelegate(nint player, int flags);
        private static Hook<ZoneStateDelegate>? setZoneState;

        private static bool enableCrawl = false;
        private static nint psuedoObject1;
        private static nint psuedoObject2;
        private static NativeAction<nint, nint> procEnvironmentCollision;
        private delegate void CollisionCheckDelegate(nint unknownPtr1, nint unknownPtr2);
        private static Hook<CollisionCheckDelegate>? collisionCheck;

        private static bool allowHotSpringsAnywhere = false;
        private static Patch jmpOverHotSpringsEval;

        private static bool disableHotSpringsSteam = false;
        private static Patch jmpOverHotSpringsSteam;

        internal static void Initialize()
        {
#if QUARANTINED_FEATURES
            // MonsterHunterWorld.exe+20354F7 - call MonsterHunterWorld.exe+1F73850
            setZoneStateFunc = new NativeAction<nint, int>(0x142035020);

            setPlayerController1 = new NativeAction<nint>(0x141F73850);
            setPlayerController5 = new NativeAction<nint>(0x14118DDC0);

            zoneStateForcePassive1 = new Patch(0x140256A3F + 0x6, [0x1]);
            zoneStateForcePassive2 = new Patch(0x141AC2865 + 0x6, [0x1]);

            zoneStateForceCombat1 = new Patch(0x141AC2938 + 0x6, [0x0]);
            zoneStateForceCombat2 = new Patch(0x141AC28EC + 0x6, [0x0]);
#endif
            setZoneState = Hook.Create<ZoneStateDelegate>(0x142035020, SetZoneStateHook);

            psuedoObject1 = (nint)NativeMemory.AllocZeroed(0x7C);
            GetRef<byte>(psuedoObject1, 0x30) = 0x03;
            psuedoObject2 = (nint)NativeMemory.AllocZeroed(0x7C);
            GetRef<byte>(psuedoObject2, 0x30) = 0x08;
            procEnvironmentCollision = new NativeAction<nint, nint>(0x141F737D0);
            collisionCheck = Hook.Create<CollisionCheckDelegate>(0x1411C4E50, CollisionCheckHook);

            // Player + ECCC = How far underwater is the player.
            jmpOverHotSpringsEval = new Patch(0x1417C1B03, [0xEB]);
            jmpOverHotSpringsSteam = new Patch(0x14203A494, [0xE9, 0x8D, 0x00, 0x00, 0x00, 0x90]);
        }

        private static void SetZoneStateHook(nint player, int flags)
        {
#if QUARANTINED_FEATURES
            ref byte zoneState = ref GetRef<byte>(Get<nint>(0x1451C42B8), 0xD2EA);
            if (zoneStateManualInvoke)
            {
                zoneStateManualInvoke = false;
            }
            else
            {
                lastZoneState = (zoneState == 0x1) ? ZoneState.Hub : ZoneState.Combat;
            }
            ZoneState overrideZoneState = (forceZoneState == ZoneState.Unknown) ? lastZoneState : forceZoneState;
            if (overrideZoneState != ZoneState.Unknown)
            {
                zoneState = ByteFlag(overrideZoneState == ZoneState.Hub);
            }
#endif

            setZoneState!.Original(player, flags);

            Plugin.OnZoneChange(player);
        }

        private static void CollisionCheckHook(nint unknownPtr1, nint unknownPtr2)
        {
            Player? player = null;
            if (enableCrawl && (player = getPlayer()) != null)
            {
                nint controlsAddr = player.Get<nint>(0x12608);
                bool combatControls = controlsAddr != 0x0 && Get<byte>(controlsAddr, 0xB18) == 0x80;
                if (combatControls)
                {
                    procEnvironmentCollision.Invoke(player.Instance, psuedoObject1);
                    procEnvironmentCollision.Invoke(player.Instance, psuedoObject2);
                }
                else
                {
                    enableCrawl = false;
                }
            }

            collisionCheck!.Original(unknownPtr1, unknownPtr2);
        }

        private static Quaternion getRight(Quaternion q)
        {
            return q * Quaternion.CreateFromYawPitchRoll(MathF.PI, 0.0f, 0.0f);
        }

        internal static void drawUI(Player player, bool saveSlotFallback, float width)
        {
            ZoneState zoneState = ZoneState.Unknown;
            nint zoneStateAddr = Get<nint>(0x1451C42B8);
            if (zoneStateAddr == 0x0)
            {
                return;
            }
            bool expand = ImGui.CollapsingHeader("Zone State");
            ImGui.SameLine();
            ImGui.Text($"(Current: {((GetRef<byte>(zoneStateAddr, 0xD2EA) == 0x1) ? "Hub" : "Combat")})");
            if (expand)
            {
                ref byte passiveFlag1 = ref player.GetRef<byte>(0x7626);
                ref byte passiveFlag2 = ref GetRef<byte>(player.Get<nint>(0x7D20), 0x9B8);
                bool passive = passiveFlag1 == 0x1 && passiveFlag2 == 0x1;
                if (saveSlotFallback)
                {
                    ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * disableAlphaFactor);
                }
                if (ImGui.Checkbox("Passive", ref passive))
                {
                    passiveFlag1 = ByteFlag(passive);
                    passiveFlag2 = ByteFlag(passive);
                }
                if (saveSlotFallback)
                {
                    ImGui.PopStyleVar();
                }
                if (ImGui.BeginItemTooltip())
                {
                    ImGui.Text("Passive: Calm camera and your hunter looks neutral/smiles.\nCombat: Intense camera and your hunter looks angry.\nIf you have Passive set and Combat Controls on, or vice versa, expect glitchy behavior.");
                    ImGui.EndTooltip();
                }
                nint controlsAddr = player.Get<nint>(0x12608);
                if (controlsAddr == 0x0) return;
                bool combatControls = Get<byte>(controlsAddr, 0xB18) == 0x80;
                zoneState = combatControls ? ZoneState.Combat : ZoneState.Hub;
#if QUARANTINED_FEATURES
                if (ImGui.Checkbox("Combat Controls", ref combatControls))
                {
                    passiveFlag1 = ByteFlag(!combatControls);
                    passiveFlag2 = ByteFlag(!combatControls);
                    setPlayerController1.Invoke(player.Instance);
                    setPlayerController5.Invoke(controlsAddr);
                }
                int zoneStateInt = (int)forceZoneState;
                ImGui.AlignTextToFramePadding();
                ImGui.Text($"Force:");
                if (ImGui.BeginItemTooltip())
                {
                    ImGui.Text("This will apply when moving to a different area.");
                    ImGui.EndTooltip();
                }
                ImGui.SameLine();
                ImGui.RadioButton("Off", ref zoneStateInt, 0);
                ImGui.SameLine();
                ImGui.RadioButton("Hub", ref zoneStateInt, 1);
                ImGui.SameLine();
                ImGui.RadioButton("Combat", ref zoneStateInt, 2);
                forceZoneState = (ZoneState)zoneStateInt;
                if (ImGui.Button("Run Change Zone State"))
                {
                    zoneStateManualInvoke = true;
                    setZoneStateFunc.Invoke(player.Instance, 0x000106C0); // or 0x00010780.
                }
                if (ImGui.BeginItemTooltip())
                {
                    ImGui.Text("You can use this to apply the value of Force Zone State.\nIf you're out in a map and Force Zone State is set to Off or Combat, this will send you back to camp.");
                    ImGui.EndTooltip();
                }
                if (ImGui.Checkbox("Force Passive in Combat Zones", ref forcePassiveInCombatZone))
                {
                    if (forcePassiveInCombatZone)
                    {
                        zoneStateForcePassive1.Enable();
                        zoneStateForcePassive2.Enable();
                    }
                    else
                    {
                        zoneStateForcePassive1.Disable();
                        zoneStateForcePassive2.Disable();
                    }
                }
                if (ImGui.Checkbox("Force Combat in Passive Zones", ref forceCombatInPassiveZone))
                {
                    if (forceCombatInPassiveZone)
                    {
                        zoneStateForceCombat1.Enable();
                        zoneStateForceCombat2.Enable();
                    }
                    else
                    {
                        zoneStateForceCombat1.Disable();
                        zoneStateForceCombat2.Disable();
                    }
                }
#endif
                if (zoneState != ZoneState.Combat)
                {
                    ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true);
                    ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * disableAlphaFactor);
                }
                if (ImGui.Checkbox("Force Crawl", ref enableCrawl))
                {
                    if (enableCrawl)
                    {
                        GetRef<float>(psuedoObject2, 0x70) = player.Position.X + player.Forward.X;
                        GetRef<float>(psuedoObject2, 0x74) = player.Position.Y + player.Forward.Y;
                        GetRef<float>(psuedoObject2, 0x78) = player.Position.Z + player.Forward.Z;

                        Quaternion playerRotation = new(player.Forward.X, player.Forward.Y, player.Forward.Z, 0.0f);
                        Quaternion objectRotation = getRight(playerRotation);

                        GetRef<float>(psuedoObject2, 0x40) = objectRotation.X;
                        GetRef<float>(psuedoObject2, 0x44) = objectRotation.Y;
                        GetRef<float>(psuedoObject2, 0x48) = objectRotation.Z;
                        GetRef<float>(psuedoObject2, 0x4C) = objectRotation.W;

                        GetRef<float>(psuedoObject2, 0x50) = 0.0f;
                        GetRef<float>(psuedoObject2, 0x54) = 1.0f;
                        GetRef<float>(psuedoObject2, 0x58) = 0.0f;
                        GetRef<float>(psuedoObject2, 0x5C) = 0.0f;

                        GetRef<float>(psuedoObject2, 0x60) = playerRotation.X;
                        GetRef<float>(psuedoObject2, 0x64) = playerRotation.Y;
                        GetRef<float>(psuedoObject2, 0x68) = playerRotation.Z;
                        GetRef<float>(psuedoObject2, 0x6C) = playerRotation.W;
                    }
                }
                if (zoneState != ZoneState.Combat)
                {
                    ImGui.PopStyleVar();
                    ImGui.PopItemFlag();
                    if (ImGui.BeginItemTooltip())
                    {
                        ImGui.Text("You have to be out in a map to use this.");
                        ImGui.EndTooltip();
                    }
                }
                else
                {
                    if (ImGui.BeginItemTooltip())
                    {
                        ImGui.Text("Simulate crawling under an object in the direction your character was facing when enabled.");
                        ImGui.EndTooltip();
                    }
                }

                if (zoneState != ZoneState.Hub)
                {
                    ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true);
                    ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * disableAlphaFactor);
                }
                if (ImGui.Button("Sit in Hot Springs"))
                {
                    // Run sit in hot springs action (top of function).
                    // MonsterHunterWorld.exe+17601F0 - mov [rsp+08],rbx
                    Models.runAction(player.ActionController, 0x1, 0x65);
                }
                if (zoneState != ZoneState.Hub)
                {
                    ImGui.PopStyleVar();
                    ImGui.PopItemFlag();
                    if (ImGui.BeginItemTooltip())
                    {
                        ImGui.Text("You have to be in a hub area to use this.");
                        ImGui.EndTooltip();
                    }
                }

                if (ImGui.Checkbox("Allow Hot Springs Anywhere", ref allowHotSpringsAnywhere))
                {
                    if (allowHotSpringsAnywhere)
                    {
                        jmpOverHotSpringsEval.Enable();
                    }
                    else
                    {
                        jmpOverHotSpringsEval.Disable();
                    }
                }
                if (ImGui.BeginItemTooltip())
                {
                    ImGui.Text("Disable check for being about halfway submerged in water to stay sitting.");
                    ImGui.EndTooltip();
                }

                if (ImGui.Checkbox("Disable Hot Springs Steam", ref disableHotSpringsSteam))
                {
                    if (disableHotSpringsSteam)
                    {
                        jmpOverHotSpringsSteam.Enable();
                    }
                    else
                    {
                        jmpOverHotSpringsSteam.Disable();
                    }
                }
                if (ImGui.BeginItemTooltip())
                {
                    ImGui.Text("It will take a couple of seconds to fade away.");
                    ImGui.EndTooltip();
                }
            }
        }
    }
}