forked from nipkownix/re4_tweaks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Trainer.cpp
2174 lines (1716 loc) · 70.1 KB
/
Trainer.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "dllmain.h"
#include "Game.h"
#include "Settings.h"
#include "input.hpp"
#include "imgui.h"
#include <FAhashes.h>
#include "UI_DebugWindows.h"
#include "UI_Utility.h"
#include "Trainer.h"
#include <DirectXMath.h>
int AshleyStateOverride;
int last_weaponId;
float fFreeCam_direction;
float fFreeCam_depression;
bool bRequestedPlayerTPtoCam;
bool bRequestedAshleyTPtoCam;
uint16_t pl_atariInfoFlagBackup = 0;
bool pl_atariInfoFlagSet = false;
uint16_t ash_atariInfoFlagBackup = 0;
bool ash_atariInfoFlagSet = false;
// Trainer.cpp: checks certain game flags & patches code in response to them
// Ideally we would hook each piece of code instead and add a flag check there
// Unfortunately, most existing trainer-like-mods for the game are done as code patches
// So to make it easier on us, this will act as a manager to tie code patches with game flags
// (maybe over time these patches can be converted to work as hooks instead)
// Holds data about a patch, which flag it should be affected by, address to patch, undo data...
struct FlagPatch
{
uint32_t* flagValuesPtr;
uint32_t flagIndex;
uint8_t* patchAddress;
std::vector<uint8_t> patchData;
bool prevFlagValue = false;
std::vector<uint8_t> origData;
FlagPatch(uint32_t* FlagValuesPtr, uint32_t FlagIndex) : flagValuesPtr(FlagValuesPtr), flagIndex(FlagIndex) {}
void SetPatch(uint8_t* address, std::initializer_list<uint8_t> data)
{
patchAddress = address;
patchData = data;
if (!patchData.size())
return;
origData.resize(patchData.size());
memcpy(origData.data(), patchAddress, patchData.size());
}
void Update()
{
bool value = CurValue();
if (value == prevFlagValue)
return; // flag not changed, no need to do anything
auto& data = value ? patchData : origData;
DWORD dwProtect;
VirtualProtect((void*)patchAddress, data.size(), PAGE_EXECUTE_READWRITE, &dwProtect);
memcpy(patchAddress, data.data(), data.size());
VirtualProtect((void*)patchAddress, data.size(), dwProtect, &dwProtect);
prevFlagValue = value;
}
bool CurValue()
{
return FlagIsSet(flagValuesPtr, flagIndex);
}
};
void MoveAshleyToPlayer()
{
cPlayer* ashley = AshleyPtr();
cPlayer* player = PlayerPtr();
if (player && ashley)
{
// Backup original collision flags and then unset collision-enabled bit
if (!ash_atariInfoFlagSet)
{
ash_atariInfoFlagBackup = ashley->atari_2B4.m_flag_1A;
ash_atariInfoFlagSet = true;
}
ashley->atari_2B4.m_flag_1A &= ~(SAT_SCA_ENABLE | SAT_OBA_ENABLE); // map collision | em collision
ashley->pos_94 = player->pos_94;
ashley->pos_old_110 = player->pos_old_110;
}
}
std::vector<uint32_t> KeyComboNoclipToggle;
std::vector<uint32_t> KeyComboFreeCamToggle;
std::vector<uint32_t> KeyComboSpeedOverride;
std::vector<uint32_t> KeyComboAshToPlayer;
std::vector<uint32_t> KeyComboDebugTrg;
std::vector<uint32_t> KeyComboWeaponHotkey[5];
std::vector<uint32_t> KeyComboLastWeaponHotkey;
void HotkeySlotPressed(int slotIdx, bool forceUseWepID = false)
{
if (!pConfig->bWeaponHotkeysEnable)
return;
if (ImGuiShouldAcceptInput())
return; // don't apply hotkeys while in ImGui
if (forceUseWepID && slotIdx < 1)
return;
cPlayer* player = PlayerPtr();
if (!player)
return;
// Routine 4 = reloading, 2 = shooting
if (player->r_no_2_FE == 4 || player->r_no_2_FE == 2)
return;
// Function used by game to determine whether inventory/subscreens can be opened
// Swapping weapons would normally require going through inventory, so we should probably make use of this too
// (fixes issues with swapping weapons during certain actions like jumping, which would interrupt the actions anim and usually place player outside of collision)
if (!player->subScrCheck())
return;
// If forceUseWepID is true, we are probably just trying to equip the last wep id, so we can skip this part
if (SubScreenWk && !forceUseWepID)
{
// If subscreen is open and we're on inventory menu...
if (SubScreenWk->open_flag_2C != 0 && SubScreenWk->menu_no_260 == 1)
{
// Treat the key-press as weapon binding request
if (SubScreenWk->puzzlePlayer_2AC)
{
// Fetch current highlighted item piece
itemPiece* ItemPiece = (itemPiece*)SubScreenWk->puzzlePlayer_2AC->ptrPiece(SubScreenWk->puzzlePlayer_2AC->m_p_active_board_30);
if (ItemPiece && ItemPiece->be_flag_0 == 1)
{
if (ItemPiece->pItem_24)
{
pConfig->iWeaponHotkeyWepIds[slotIdx] = ItemPiece->pItem_24->id_0;
pConfig->WriteSettings();
// fall-thru to weapon switch code below, acts as an indicator that binding was set
}
}
}
}
}
// Not binding weapon, must be trying to switch to weapon instead
int weaponId = pConfig->iWeaponHotkeyWepIds[slotIdx];
// slotIdx is the actual weaponId if forceUseWepID is true
if (!forceUseWepID)
weaponId = pConfig->iWeaponHotkeyWepIds[slotIdx];
else
weaponId = slotIdx;
if (!weaponId)
{
static size_t weaponCycleIndex[5] = { 0 };
std::vector<int>& weaponCycle = pConfig->iWeaponHotkeyCycle[slotIdx];
size_t desiredIndex = weaponCycleIndex[slotIdx];
// Search cycle for the current equipped wep, if it contains it, then pick the weapon that comes after it
// else if it doesn't contain current equipped wep, our desiredIndex will be whatever the previously selected index for this slot was
if (ItemMgr->m_pWep_C)
{
for (size_t i = 0; i < weaponCycle.size(); i++)
if (ItemMgr->m_pWep_C->id_0 == weaponCycle[i])
{
desiredIndex = i + 1;
break;
}
}
// Check that we have the desired weapon in inventory, continue through all possible weaponCycle values until we find one
size_t cycleIdx = desiredIndex;
for (size_t i = 0; i < weaponCycle.size(); i++)
{
cycleIdx = (desiredIndex + i) % weaponCycle.size();
cItem* item = ItemMgr->search(weaponCycle[cycleIdx]);
if (item)
{
weaponId = weaponCycle[cycleIdx];
break;
}
}
// Save our new index in the cycle
weaponCycleIndex[slotIdx] = cycleIdx;
}
if (weaponId)
{
if (ItemMgr->m_pWep_C)
last_weaponId = ItemMgr->m_pWep_C->id_0;
cItem* item = ItemMgr->search(weaponId);
if (ItemMgr->arm(item))
{
RequestWeaponChange();
}
}
}
void Trainer_ParseKeyCombos()
{
// NoClip
{
KeyComboNoclipToggle.clear();
KeyComboNoclipToggle = ParseKeyCombo(pConfig->sTrainerNoclipKeyCombo);
pInput->RegisterHotkey({ []() {
bool playerCollisionDisabled = FlagIsSet(GlobalPtr()->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_PL_NOHIT));
FlagSet(GlobalPtr()->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_PL_NOHIT), !playerCollisionDisabled);
}, &KeyComboNoclipToggle });
}
// Free cam
{
KeyComboFreeCamToggle.clear();
KeyComboFreeCamToggle = ParseKeyCombo(pConfig->sTrainerFreeCamKeyCombo);
pInput->RegisterHotkey({ []() {
pConfig->bTrainerEnableFreeCam = !pConfig->bTrainerEnableFreeCam;
}, &KeyComboFreeCamToggle });
}
// SpeedOverride
{
KeyComboSpeedOverride.clear();
KeyComboSpeedOverride = ParseKeyCombo(pConfig->sTrainerSpeedOverrideKeyCombo);
pInput->RegisterHotkey({ []() {
pConfig->bTrainerPlayerSpeedOverride = !pConfig->bTrainerPlayerSpeedOverride;
}, &KeyComboSpeedOverride });
}
// Move Ash to player
{
KeyComboAshToPlayer.clear();
KeyComboAshToPlayer = ParseKeyCombo(pConfig->sTrainerMoveAshToPlayerKeyCombo);
pInput->RegisterHotkey({ []() {
MoveAshleyToPlayer();
}, &KeyComboAshToPlayer });
}
// DebugTrg
{
KeyComboDebugTrg.clear();
KeyComboDebugTrg = ParseKeyCombo(pConfig->sTrainerDebugTrgKeyCombo);
}
// WeaponHotkey
{
for (int i = 0; i < 5; i++)
{
KeyComboWeaponHotkey[i].clear();
KeyComboWeaponHotkey[i] = ParseKeyCombo(pConfig->sWeaponHotkeys[i]);
}
{
KeyComboLastWeaponHotkey.clear();
KeyComboLastWeaponHotkey = ParseKeyCombo(pConfig->sLastWeaponHotkey);
}
// TODO: make RegisterHotkey use std::function so that we could include `i` in the capture and handle this in the loop above...
pInput->RegisterHotkey({ []() { HotkeySlotPressed(0); }, &KeyComboWeaponHotkey[0] });
pInput->RegisterHotkey({ []() { HotkeySlotPressed(1); }, &KeyComboWeaponHotkey[1] });
pInput->RegisterHotkey({ []() { HotkeySlotPressed(2); }, &KeyComboWeaponHotkey[2] });
pInput->RegisterHotkey({ []() { HotkeySlotPressed(3); }, &KeyComboWeaponHotkey[3] });
pInput->RegisterHotkey({ []() { HotkeySlotPressed(4); }, &KeyComboWeaponHotkey[4] });
pInput->RegisterHotkey({ []() { HotkeySlotPressed(last_weaponId, true); }, &KeyComboLastWeaponHotkey });
}
}
bool(__cdecl* cameraHitCheck_orig)(Vec *pCross, Vec *pNorm, Vec p0, Vec p1);
bool __cdecl cameraHitCheck_hook(Vec *pCross, Vec *pNorm, Vec p0, Vec p1)
{
if (pConfig->bTrainerEnableFreeCam)
return false;
else
return cameraHitCheck_orig(pCross, pNorm, p0, p1);
}
// DebugTrg reimpl based on 2007 port, thank you SourceNext!
// (no other publicly available builds appear to include this, even the debug builds...)
// Gamepad combo is LB + B + X
bool __cdecl DebugTrg_hook(uint32_t flag)
{
if (!pConfig->bTrainerEnableDebugTrg)
return false;
if (FlagIsSet(GlobalPtr()->Flags_SYSTEM_0_54, uint32_t(Flags_SYSTEM::SYS_PUBLICITY_VER)))
return false; // disallowed if SYS_PUBLICITY_VER set, probably set on E3/TGS/trade show builds
// if flag is set, then caller func seems to allow checking for previously held buttons
// otherwise, buttons must have only been pressed this frame
if (flag && pInput->is_combo_down(&KeyComboDebugTrg))
return true;
if (!flag && pInput->is_combo_pressed(&KeyComboDebugTrg))
return true;
JOY* Joy = JoyPtr();
if ((Joy[0].on_14 & JOY_START) != 0)
return false; // if start pressed, always return false
if (flag)
{
if ((Joy[0].on_14 & JOY_LB) == 0 || (Joy[0].on_14 & JOY_B) == 0)
return false; // LB or B not pressed/held, return false
return ((Joy[0].on_14 & JOY_X) != 0); // return true if X also pressed/held
}
// If flag == 0, the same LB/B checks are used as above, but the X check is performed on Joy.trg instead of Joy.on
// Seems trg shows buttons that were newly pressed on this frame, then gets cleared on subsequent frame even if held down
// I guess to help ensure the function only runs once, on the frame when button was first pressed
// (and not on any subsequent frames where it might still be held down)
if ((Joy[0].on_14 & JOY_LB) == 0 || (Joy[0].on_14 & JOY_B) == 0)
return false; // LB or B not pressed/held, return false
return ((Joy[0].trg_18 & JOY_X) != 0); // return true if X was newly pressed
}
std::vector<FlagPatch> flagPatches;
void Trainer_Init()
{
GLOBAL_WK* globals = GlobalPtr();
if (!globals)
return;
if (!GameVersionIsDebug())
{
// Remove bzero call that clears debug flags every frame for some reason
auto pattern = hook::pattern("83 C0 60 6A 10 50 E8");
injector::MakeNOP(pattern.count(1).get(0).get<uint32_t>(6), 5, true);
}
// Game seems to set DBG_NO_DEATH & DBG_INF_BULLET2 flags during gameRoomInit
// Ordinarilly those get cleared by the bzero mentioned above, but we had to disable that to allow using DBG flags...
// For now we'll just patch out the code that sets up those flags
{
auto pattern = hook::pattern("81 48 68 00 00 80 00");
injector::MakeNOP(pattern.count(1).get(0).get<uint8_t>(0), 7, true);
pattern = hook::pattern("B9 00 00 00 80 09 48 6C");
injector::MakeNOP(pattern.count(1).get(0).get<uint8_t>(0), 8, true);
}
// gameRoomMemInit clears most DBG flags when loading new room
// patch out the code responsible so players trainer settings won't get reset between rooms
{
auto pattern = hook::pattern("8B 15 ? ? ? ? 89 72 60");
injector::MakeNOP(pattern.count(1).get(0).get<uint8_t>(0), 9, true);
pattern = hook::pattern("A1 ? ? ? ? 89 70 64");
injector::MakeNOP(pattern.count(1).get(0).get<uint8_t>(0), 8, true);
}
// DBG_NO_DEATH2
{
// EmAtkHitCk patch
auto pattern = hook::pattern("05 88 00 00 00 50 53 57 E8");
auto EmAtkHitCk_thunk = injector::GetBranchDestination(pattern.count(1).get(0).get<uint32_t>(8));
FlagPatch patch_EmAtkHitCk(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_NO_DEATH2));
patch_EmAtkHitCk.SetPatch((uint8_t*)EmAtkHitCk_thunk.as_int(), { 0x33, 0xC0, 0xC3 });
flagPatches.push_back(patch_EmAtkHitCk);
// LifeDownSet2 patch
pattern = hook::pattern("0F 85 ? ? ? ? A1 ? ? ? ? 66 83 B8 B4 4F 00 00 00 7F");
auto LifeDownSet2_jg = pattern.count(1).get(0).get<uint8_t>(0x13);
FlagPatch patch_LifeDownSet2(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_NO_DEATH2));
patch_LifeDownSet2.SetPatch(LifeDownSet2_jg, { 0x90, 0x90 });
flagPatches.push_back(patch_LifeDownSet2);
// PlGachaGet patch
pattern = hook::pattern("A1 ? ? ? ? 8B 15 ? ? ? ? 80 BA 98 4F 00 00 03");
auto PlGachaGet = pattern.count(1).get(0).get<uint8_t>(0);
FlagPatch patch_PlGachaGet(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_NO_DEATH2));
patch_PlGachaGet.SetPatch(PlGachaGet, { 0xB8, 0x7F, 0x00, 0x00, 0x00, 0xC3 });
flagPatches.push_back(patch_PlGachaGet);
// PlGachaMove patch (prevents button prompt when grabbed)
pattern = hook::pattern("57 8B 3D ? ? ? ? 6A 00 6A 00 6A 11 6A 0B");
auto PlGachaMove = pattern.count(1).get(0).get<uint8_t>(0);
FlagPatch patch_PlGachaMove(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_NO_DEATH2));
patch_PlGachaMove.SetPatch(PlGachaMove, { 0xC3 });
flagPatches.push_back(patch_PlGachaMove);
}
// DBG_EM_WEAK
{
// LifeDownSet2 patch
auto pattern = hook::pattern("0F BF 86 24 03 00 00 3B C7 7D");
auto LifeDownSet2_jg = pattern.count(1).get(0).get<uint8_t>(9);
FlagPatch patch_LifeDownSet2(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_EM_WEAK));
patch_LifeDownSet2.SetPatch(LifeDownSet2_jg, { 0x90, 0x90 });
flagPatches.push_back(patch_LifeDownSet2);
}
// DBG_INF_BULLET
{
auto pattern = hook::pattern("0B D1 66 89 57 08");
auto cItemMgr__trigger_0_mov = pattern.count(1).get(0).get<uint8_t>(2);
FlagPatch patch_cItemMgr__trigger_0(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_INF_BULLET));
patch_cItemMgr__trigger_0.SetPatch(cItemMgr__trigger_0_mov, { 0x90, 0x90, 0x90, 0x90 });
flagPatches.push_back(patch_cItemMgr__trigger_0);
pattern = hook::pattern("4A 66 89 50 02");
auto cItemMgr__dump_0_mov = pattern.count(1).get(0).get<uint8_t>(1);
FlagPatch patch_cItemMgr__dump_0(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_INF_BULLET));
patch_cItemMgr__dump_0.SetPatch(cItemMgr__dump_0_mov, { 0x90, 0x90, 0x90, 0x90 });
flagPatches.push_back(patch_cItemMgr__dump_0);
}
// DBG_EM_NO_ATK
{
// TODO: this only affects em10FindCk right now, other Ems also have their own FindCk funcs though, might need to patch those too
// (unless we can find a func used by all of them - haven't had any luck with that yet...)
// RouteCk / em10RouteCk also seem involved with searching/routing to player, but patching those can make Em movement buggy...
auto pattern = hook::pattern("33 D2 8B C6 E8 ? ? ? ? 85 C0 75");
auto em10FindCk = injector::GetBranchDestination(pattern.count(1).get(0).get<uint8_t>(4));
FlagPatch patch_em10FindCk(globals->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_EM_NO_ATK));
patch_em10FindCk.SetPatch((uint8_t*)em10FindCk.as_int(), { 0x31, 0xC0, 0xC3 });
flagPatches.push_back(patch_em10FindCk);
}
// Ashley presence
{
auto pattern = hook::pattern("F7 80 ? ? ? ? ? ? ? ? 74 22 D9 80 ? ? ? ? 51");
struct ScenarioRoomInit_hook
{
void operator()(injector::reg_pack& regs)
{
switch ((AshleyState)AshleyStateOverride) {
case AshleyState::Present:
FlagSet(GlobalPtr()->flags_STATUS_0_501C, uint32_t(Flags_STATUS::STA_SUB_ASHLEY), true);
break;
case AshleyState::NotPresent:
FlagSet(GlobalPtr()->flags_STATUS_0_501C, uint32_t(Flags_STATUS::STA_SUB_ASHLEY), false);
break;
case AshleyState::Default:
break;
}
// Code we replaced
if (FlagIsSet(GlobalPtr()->flags_STATUS_0_501C, uint32_t(Flags_STATUS::STA_SUB_ASHLEY)))
regs.ef &= ~(1 << regs.zero_flag);
else
regs.ef |= (1 << regs.zero_flag);
}
}; injector::MakeInline<ScenarioRoomInit_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(10));
}
// Enemy HP multiplier
{
auto pattern = hook::pattern("66 89 88 ? ? ? ? 0F BF 56 ? 89 55 ? DB 45 ? D8 C9 D9 98");
struct EmSetFromList_hook
{
void operator()(injector::reg_pack& regs)
{
// Code we repalced
*(int16_t*)(regs.eax + 0x326) = (int16_t)regs.ecx;
// Get pointer to cEm
cEm* Em_ptr = (cEm*)(regs.eax);
if (pConfig->bTrainerEnemyHPMultiplier)
{
float multi = 0.0f;
if (pConfig->bTrainerRandomHPMultiplier)
multi = GetRandomFloat(pConfig->fTrainerRandomHPMultiMin, pConfig->fTrainerRandomHPMultiMax);
else
multi = pConfig->fTrainerEnemyHPMultiplier;
Em_ptr->hp_324 = (int16_t)(Em_ptr->hp_324 * multi);
Em_ptr->hp_max_326 = (int16_t)(Em_ptr->hp_max_326 * multi);
}
}
}; injector::MakeInline<EmSetFromList_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(7));
pattern = hook::pattern("66 89 86 ? ? ? ? 0F BF 57 12 89 55 08 DB 45 08 D8 C9 D9");
struct EmSetFromList2_hook
{
void operator()(injector::reg_pack& regs)
{
// Code we repalced
*(int16_t*)(regs.esi + 0x326) = (int16_t)regs.eax;
// Get pointer to cEm
cEm* Em_ptr = (cEm*)(regs.esi);
if (pConfig->bTrainerEnemyHPMultiplier)
{
float multi = 0.0f;
if (pConfig->bTrainerRandomHPMultiplier)
multi = GetRandomFloat(pConfig->fTrainerRandomHPMultiMin, pConfig->fTrainerRandomHPMultiMax);
else
multi = pConfig->fTrainerEnemyHPMultiplier;
Em_ptr->hp_324 = (int16_t)(Em_ptr->hp_324 * multi);
Em_ptr->hp_max_326 = (int16_t)(Em_ptr->hp_max_326 * multi);
}
}
}; injector::MakeInline<EmSetFromList2_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(7));
pattern = hook::pattern("66 89 86 ? ? ? ? 0F BF 4F ? 89 4D ? DB 45 ? D8 C9");
struct EmSetEvent_hook
{
void operator()(injector::reg_pack& regs)
{
// Code we repalced
*(int16_t*)(regs.esi + 0x326) = (int16_t)regs.eax;
// Get pointer to cEm
cEm* Em_ptr = (cEm*)(regs.esi);
if (pConfig->bTrainerEnemyHPMultiplier)
{
float multi = 0.0f;
if (pConfig->bTrainerRandomHPMultiplier)
multi = GetRandomFloat(pConfig->fTrainerRandomHPMultiMin, pConfig->fTrainerRandomHPMultiMax);
else
multi = pConfig->fTrainerEnemyHPMultiplier;
Em_ptr->hp_324 = (int16_t)(Em_ptr->hp_324 * multi);
Em_ptr->hp_max_326 = (int16_t)(Em_ptr->hp_max_326 * multi);
}
}
}; injector::MakeInline<EmSetEvent_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(7));
}
// Disable enemy spawn
{
auto pattern = hook::pattern("38 8F ? ? ? ? 0F 85 ? ? ? ? 38 87 ? ? ? ? 0F 85 ? ? ? ? 53");
struct EmSetFromList_hook
{
void operator()(injector::reg_pack& regs)
{
// Original cmp we replaced
bool isEqual = (*(uint8_t*)(regs.edi + 0x4FAD) == (uint8_t)regs.ecx);
if (!pConfig->bTrainerDisableEnemySpawn && isEqual)
regs.ef |= (1 << regs.zero_flag);
else
regs.ef &= ~(1 << regs.zero_flag);
}
}; injector::MakeInline<EmSetFromList_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("8B 15 ? ? ? ? 85 C0 75 22 0F B7 47 18");
struct EmSetFromList2_hook1
{
void operator()(injector::reg_pack& regs)
{
// Code we replaced
regs.edx = (uint32_t)GlobalPtr();
if (pConfig->bTrainerDisableEnemySpawn)
regs.eax = 0;
}
}; injector::MakeInline<EmSetFromList2_hook1>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("38 8a ? ? ? ? 75 ? 38 82 ? ? ? ? 74 ? a1 ? ? ? ? 5f 5b");
struct EmSetFromList2_hook2
{
void operator()(injector::reg_pack& regs)
{
// Original cmp we replaced
bool isEqual = (*(uint8_t*)(regs.edx + 0x4FAD) == (uint8_t)regs.ecx);
if (!pConfig->bTrainerDisableEnemySpawn && isEqual)
regs.ef |= (1 << regs.zero_flag);
else
regs.ef &= ~(1 << regs.zero_flag);
}
}; injector::MakeInline<EmSetFromList2_hook2>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
}
// Dead bodies never disappear
{
auto pattern = hook::pattern("8b 8e ? ? ? ? f7 c1 ? ? ? ? 74 ? b8");
struct em10_R1_Die_Cramp_hook
{
void operator()(injector::reg_pack& regs)
{
// Code we replaced
regs.ecx = *(uint32_t*)(regs.esi + 0x3D0);
// Dead bodies have to disappear in room 11C, otherwise the game won't advance. Something about enemy recycling?
bool is11C = (GlobalPtr()->curRoomId_4FAC == 0x11C);
if (pConfig->bTrainerDeadBodiesNeverDisappear && !is11C)
regs.eax = 1;
}
}; injector::MakeInline<em10_R1_Die_Cramp_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
}
// AllowEnterDoorsWithoutAsh
{
auto pattern = hook::pattern("A1 ? ? ? ? 85 C0 74 7A 05 ? ? ? ? 50 A1 ? ? ? ? 05 ? ? ? ? 50 E8");
struct CheckAshleyActive_hook
{
void operator()(injector::reg_pack& regs)
{
// Code we replaced
regs.eax = (uint32_t)AshleyPtr();
// Make game check for Leon's position against itself, instead of checking against Ashley's position.
if (pConfig->bTrainerAllowEnterDoorsWithoutAsh)
regs.eax = (uint32_t)PlayerPtr();
}
}; injector::MakeInline<CheckAshleyActive_hook>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(5));
}
// Free cam
{
// Hook cameraHitCheck to disable hit detection
auto pattern = hook::pattern("E8 ? ? ? ? 83 C4 20 84 C0 74 2A");
ReadCall(injector::GetBranchDestination(pattern.count(1).get(0).get<uint32_t>(0)).as_int(), cameraHitCheck_orig);
InjectHook(injector::GetBranchDestination(pattern.count(1).get(0).get<uint32_t>(0)).as_int(), cameraHitCheck_hook, PATCH_JUMP);
// Redirect m_direction_ratio_208 and m_depression_ratio_204 when using free cam
pattern = hook::pattern("D9 83 ? ? ? ? DA E9 89 85 ? ? ? ? DF E0");
struct calcOffset_direction
{
void operator()(injector::reg_pack& regs)
{
float direction;
if (pConfig->bTrainerEnableFreeCam)
direction = fFreeCam_direction;
else
direction = CamCtrl->m_QuasiFPS_278.m_direction_ratio_208;
__asm {fld direction}
}
}; injector::MakeInline<calcOffset_direction>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? 51 D9 1C ? 8D 85 ? ? ? ? 6A ? 50");
injector::MakeInline<calcOffset_direction>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? D9 EE D9 C0 DD EA DF E0 DD D9 F6 C4 ? 7A ? 8B 75");
struct calcOffset_depression
{
void operator()(injector::reg_pack& regs)
{
float depression;
if (pConfig->bTrainerEnableFreeCam)
depression = fFreeCam_depression;
else
depression = CamCtrl->m_QuasiFPS_278.m_depression_ratio_204;
__asm {fld depression}
}
}; injector::MakeInline<calcOffset_depression>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? D8 D9 DF E0 F6 C4 ? 0F 85");
injector::MakeInline<calcOffset_depression>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? 52 D9 9D ? ? ? ? 51 D9 85 ? ? ? ? 8D 45");
injector::MakeInline<calcOffset_depression>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? D8 D9 DF E0 F6 C4 ? 0F 8A ? ? ? ? DD D8 8D 95");
injector::MakeInline<calcOffset_depression>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
pattern = hook::pattern("D9 83 ? ? ? ? 52 DC 0D ? ? ? ? 51 8D 45 ? D9 9D");
injector::MakeInline<calcOffset_depression>(pattern.count(1).get(0).get<uint32_t>(0), pattern.count(1).get(0).get<uint32_t>(6));
}
// Hook DebugTrg stub to use our reimplemented version
{
auto pattern = hook::pattern("83 C4 18 6A 01 E8 ? ? ? ? 83 C4 04 3C 01");
InjectHook(injector::GetBranchDestination(pattern.count(1).get(0).get<uint32_t>(0x5)).as_int(), DebugTrg_hook, PATCH_JUMP);
}
}
void Trainer_Update()
{
for (auto& flagPatch : flagPatches)
{
flagPatch.Update();
}
cPlayer* player = PlayerPtr();
if (!player)
return;
// Player speed override handling
{
static bool prev_bPlayerSpeedOverride = false;
static float prev_PlayerSpeed = 0;
if (prev_bPlayerSpeedOverride != pConfig->bTrainerPlayerSpeedOverride)
{
if (pConfig->bTrainerPlayerSpeedOverride)
{
// changed from disabled -> enabled, backup previous speed value
prev_PlayerSpeed = player->Motion_1D8.Seq_speed_C0;
}
else if (!pConfig->bTrainerPlayerSpeedOverride)
{
// changed from enabled -> disabled, restore previous speed value
player->Motion_1D8.Seq_speed_C0 = prev_PlayerSpeed;
}
prev_bPlayerSpeedOverride = pConfig->bTrainerPlayerSpeedOverride;
}
if (pConfig->bTrainerPlayerSpeedOverride)
player->Motion_1D8.Seq_speed_C0 = pConfig->fTrainerPlayerSpeedOverride;
}
// Handle the player's collision
if (pl_atariInfoFlagSet &&
!FlagIsSet(GlobalPtr()->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_PL_NOHIT)) &&
!pConfig->bTrainerEnableFreeCam)
{
// Restore collision flags
player->atari_2B4.m_flag_1A = pl_atariInfoFlagBackup;
pl_atariInfoFlagSet = false;
}
// Handle Ashley's collision
cPlayer* ashley = AshleyPtr();
if (ashley && !pConfig->bTrainerEnableFreeCam)
{
// Restore collision flags
if (ash_atariInfoFlagSet)
{
ashley->atari_2B4.m_flag_1A = ash_atariInfoFlagBackup;
ash_atariInfoFlagSet = false;
}
}
if (FlagIsSet(GlobalPtr()->flags_DEBUG_0_60, uint32_t(Flags_DEBUG::DBG_PL_NOHIT)))
{
// Backup original collision flags and then unset collision-enabled bit
if (!pl_atariInfoFlagSet)
{
pl_atariInfoFlagBackup = player->atari_2B4.m_flag_1A;
pl_atariInfoFlagSet = true;
}
player->atari_2B4.m_flag_1A &= ~(SAT_SCA_ENABLE | SAT_OBA_ENABLE); // map collision | em collision
// Handle numpad/mouse wheel movement
if (pConfig->bTrainerUseNumpadMovement && !pConfig->bTrainerEnableFreeCam)
{
// Speed calculation
float movSpeed = 140.0f * pConfig->fTrainerNumMoveSpeed;
// Cam directions
float Sine = CamCtrl->Camera_60.mat_0[2][0];
float Cosine = CamCtrl->Camera_60.mat_0[0][0];
float Pitch = fFreeCam_depression;
Sine *= movSpeed;
Cosine *= movSpeed;
Pitch *= movSpeed;
// Forwards
if (pInput->is_key_down(0x68)) //VK_NUMPAD_8
{
player->pos_94.x += Sine;
player->pos_94.z -= Cosine;
}
// Backwards
if (pInput->is_key_down(0x62)) //VK_NUMPAD_2
{
player->pos_94.x -= Sine;
player->pos_94.z += Cosine;
}
// Left
if (pInput->is_key_down(0x64)) //VK_NUMPAD_4
{
player->pos_94.x -= Cosine;
player->pos_94.z -= Sine;
}
// Right
if (pInput->is_key_down(0x66)) //VK_NUMPAD_6
{
player->pos_94.x += Cosine;
player->pos_94.z += Sine;
}
// Up
if (pInput->is_key_down(0x67)) //VK_NUMPAD_7
{
player->pos_94.y += movSpeed;
}
// Down
if (pInput->is_key_down(0x61)) //VK_NUMPAD_1
{
player->pos_94.y -= movSpeed;
}
// Handle mouse wheel
if (pConfig->bTrainerUseMouseWheelUpDown && !ImGuiShouldAcceptInput())
{
// Up
if (pInput->mouse_wheel_delta() > 0)
{
movSpeed += pInput->mouse_wheel_delta() * 200.0f;
player->pos_94.y += movSpeed;
}
// Down
if ((pInput->mouse_wheel_delta() < 0))
{
movSpeed -= pInput->mouse_wheel_delta() * 200.0f;
player->pos_94.y -= movSpeed;
}
}
}
}
// Handle free camera
static bool stopPlFlagSet = false;
static float CamXYZbackup[3] = {0, 0, 0};
if (pConfig->bTrainerEnableFreeCam)
{
// Camera rotation
if (isController())
{
fFreeCam_direction -= *AnalogRX_8 / 800.0f;
fFreeCam_depression += *AnalogRY_9 / 800.0f;
}
else
{
fFreeCam_direction -= *MouseDeltaX / 600.0f;
fFreeCam_depression += *MouseDeltaY / 600.0f;
}
fFreeCam_depression = std::clamp(fFreeCam_depression, -2.33f, 2.33f);
// Flag and backup
bool isFlagSet = FlagIsSet(GlobalPtr()->flags_STOP_0_170, uint32_t(Flags_STOP::SPF_PL));
if (!isFlagSet)
{
// Backup cam pos
CamXYZbackup[0] = CamCtrl->m_QuasiFPS_278.m_pl_mat_178[0][3];
CamXYZbackup[1] = CamCtrl->m_QuasiFPS_278.m_pl_mat_178[1][3];
CamXYZbackup[2] = CamCtrl->m_QuasiFPS_278.m_pl_mat_178[2][3];
// Backup original collision flags and then unset collision-enabled bit
if (!pl_atariInfoFlagSet)
{
pl_atariInfoFlagBackup = player->atari_2B4.m_flag_1A;
pl_atariInfoFlagSet = true;
}
player->atari_2B4.m_flag_1A &= ~(SAT_SCA_ENABLE | SAT_OBA_ENABLE); // map collision | em collision
if (ashley)
{
if (!ash_atariInfoFlagSet)
{
ash_atariInfoFlagBackup = ashley->atari_2B4.m_flag_1A;
ash_atariInfoFlagSet = true;
}
ashley->atari_2B4.m_flag_1A &= ~(SAT_SCA_ENABLE | SAT_OBA_ENABLE); // map collision | em collision
}
// Set pl stop flag
FlagSet(GlobalPtr()->flags_STOP_0_170, uint32_t(Flags_STOP::SPF_PL), true);
stopPlFlagSet = true;
}
// Input handling
bool isPressingFwd = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_FORWARD) == (uint64_t)KEY_BTN::KEY_FORWARD);
bool isPressingBack = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_BACK) == (uint64_t)KEY_BTN::KEY_BACK);
bool isPressingLeft = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_LEFT) == (uint64_t)KEY_BTN::KEY_LEFT);
bool isPressingRight = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_RIGHT) == (uint64_t)KEY_BTN::KEY_RIGHT);
bool isPressingRun = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_RUN) == (uint64_t)KEY_BTN::KEY_RUN);
bool isPressingUp = false;
bool isPressingDown = false;
if (isController())
isPressingUp = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_KAMAE_KNIFE) == (uint64_t)KEY_BTN::KEY_KAMAE_KNIFE);
else
{
if (!ImGuiShouldAcceptInput())
isPressingUp = (pInput->mouse_wheel_delta() > 0); // Mouse wheel up
}
if (isController())
isPressingDown = ((Key_btn_on() & (uint64_t)KEY_BTN::KEY_KAMAE) == (uint64_t)KEY_BTN::KEY_KAMAE);
else
{
if (!ImGuiShouldAcceptInput())
isPressingDown = (pInput->mouse_wheel_delta() < 0); // Mouse wheel down
}
// Speed calculation
float movSpeed = 140.0f * pConfig->fTrainerFreeCamSpeed;
if (isPressingRun)
movSpeed *= 2.0f;
// Directions
float Sine = CamCtrl->Camera_60.mat_0[2][0];
float Cosine = CamCtrl->Camera_60.mat_0[0][0];
float Pitch = fFreeCam_depression;
Sine *= movSpeed;
Cosine *= movSpeed;
Pitch *= movSpeed;
float *X = &CamCtrl->m_QuasiFPS_278.m_pl_mat_178[0][3];
float *Y = &CamCtrl->m_QuasiFPS_278.m_pl_mat_178[1][3];
float *Z = &CamCtrl->m_QuasiFPS_278.m_pl_mat_178[2][3];
// Forwards
if (isPressingFwd)
{
*X += Sine;
*Y += Pitch;
*Z -= Cosine;
}
// Backwards
if (isPressingBack)
{
*X -= Sine;
*Y -= Pitch;
*Z += Cosine;
}
// Left
if (isPressingLeft)
{
*X -= Cosine;
*Z -= Sine;
}
// Right
if (isPressingRight)
{
*X += Cosine;
*Z += Sine;
}
// Up
if (isPressingUp)
{
if (isKeyboardMouse())
{
movSpeed += pInput->mouse_wheel_delta() * 200.0f;
if (isPressingRun)
movSpeed *= 3.0f;
}
*Y += movSpeed;
}
// Down
if (isPressingDown)
{
if (isKeyboardMouse())
{
movSpeed -= pInput->mouse_wheel_delta() * 200.0f;
if (isPressingRun)
movSpeed *= 3.0f;
}
*Y -= movSpeed;
}
// TP if needed
if (bRequestedPlayerTPtoCam)
{
player->pos_94.x = *X;
player->pos_94.y = *Y;
player->pos_94.z = *Z;
// Has to be set to false, or else the game won't read the new position.
// Will be automatically be set to true again once the loop reaches this function again.