diff --git a/docs/REVERSE_ENGINEERING.md b/docs/REVERSE_ENGINEERING.md index 71bcebe4..450243bd 100644 --- a/docs/REVERSE_ENGINEERING.md +++ b/docs/REVERSE_ENGINEERING.md @@ -336,6 +336,22 @@ Multipliers: high `3.0` (light-count group `10.0`), mid `1.0`, low `0.5` (water **Not done:** a forced-2P tier as a middle setting for 3P/4P (deliberately deferred — same 5 bytes, would need mutual exclusion with the 1P patch). Open: a per-game Performance-preset capture (low/mid tier values are currently derived, not measured); frame-cost measurement; portability to other Halo titles. +## Per-player HUD (Halo CE, 2, 3, ODST, Reach) + +**Known — mechanism.** MCC's host object (`CGameManager`) has three HUD methods the games call while drawing: `+0x2F8` `get_hud_element_anchor(element, int* anchor)`, `+0x300` `get_hud_element_transform(element, float* dx, float* dy, float* scale)` and `+0x308` `transform_hud_color(user, argb)`; MCC's own versions back a feature-flagged HUD editor and the colour-blind filter. Halo CE, 2, 3, ODST and Reach ask for a transform right before placing each HUD element (MCC element ids: 1 motion sensor, 2-5 grenades, 6 shield/health, 7 weapon, 9 crosshair, 11 equipment, 12 messages) and keep the player being drawn in a global; Halo 3, ODST and Reach also pass every HUD colour, with its user, through `+0x308`. `src/mcc/hud` answers for the drawing player, on top of MCC's answer. + +| Game | Drawing player | Offset units | +| --- | --- | --- | +| Halo CE | `0x29AF2B8` (int16) | host offsets go to x only, so offsets are added by a hook on `hud_calculate_point` (`0xB56A58`) to each placed point, in the view being drawn (`0x29AF2F0`); scale and hiding still go through the host | +| Halo 2 | `0x165C398` | offset × unit scale (`0xE14F28`) × the element's scale = pixels, in the view's bounds (`0x165C298`, int16 t, l, b, r) | +| Halo 3 | `0xAD317C` | the HUD's virtual canvas (`0xAD3148`, float width, height) | +| ODST | `0xB1AA04` | canvas `0xB1A9D8` | +| Reach | `0xD1F71C` | canvas `0xD1F6F0` | + +- **Reach asks for the motion sensor twice.** `UpdatePlayerHudView` (`0x2D94BC`) asks once per view (call at `0x2D96B7`) and bakes the answer into the radar centre the dish and blips draw from (`0xD1F6FC`...); the per-widget lookup `0x2D93A4` (widget anchor string_id → element: `0x80630` motion sensor → 1, `0x80631` → 2 grenades, `0x80632` → 6 shield, `0x80633` → 7 weapon, `0x80634` → 9 crosshair, `0x80635` → 11 equipment) asks again, returning to `0x2D9443`. That second ask is left to MCC, or a dish widget would get its offset and scale twice. +- **The motion sensor is asked for before its view's canvas is set.** Halo 3, ODST and Reach ask for element 1 while they set each view's HUD up, before they store that view's canvas (Halo 3 `0x2ED267`, canvas `0x2ED40C`; ODST `0x3287F5`, `0x328991`; Reach `0x2D96B7`, `0x2D984E`), so the canvas at that ask is still the previous view's - wrong wherever the views differ in size (3 players). The other elements are asked for while the view's widgets are placed (Halo 3 `0x2EDD80`, ODST `0x3292E4`, Reach `0x2D93A4`), after it. `Hud.cpp` keeps each user's canvas from those asks and gives the motion sensor's ask its own view's, as of the frame before. Halo 2 reads its view bounds right after each ask (`0x829490`), so it doesn't have this. +- **Halo 2 and "HUD anchor: Centered"** (the profiles' `LockMaxAspectRatio`, a global at `0x197EE40`, the last loaded profile's). On a screen wider than 16:9, `0x954DD0` insets the whole-screen HUD frame to a centred 16:9 box and `0x7E0A40` splits that box among the players, so every split-screen HUD bunches toward the middle of the screen and the crosshairs leave their views' centres. The embedded patch "HUD at screen edges" (`0x954DF2` `74` → `EB`, off by default) skips the inset - MCC's "HUD anchor: Edge" - with any number of players. Halo 3's `chud_draw_begin` (`0x2ED0D4`) insets each player's own view instead, so it doesn't bunch. + ## Other durable findings ### `CPatch` backup-capture idempotency (generic, not Reach-specific) diff --git a/lib/game/inc/1.3528.0.0/offset_halo3.h b/lib/game/inc/1.3528.0.0/offset_halo3.h index 10156bf2..09236337 100644 --- a/lib/game/inc/1.3528.0.0/offset_halo3.h +++ b/lib/game/inc/1.3528.0.0/offset_halo3.h @@ -56,6 +56,10 @@ DefOffset(OFFSET_HALO3_PF_VIEW_SETUP, 0x282EC4) // void (view*, int slot, int pl DefOffset(OFFSET_HALO3_PF_TITLE_SAFE, 0x272010) // void (short rect[4] {top, left, bottom, right}) - the screen's title-safe box, 5% in from each edge DefOffset(OFFSET_HALO3_VIEW_SETUP_TITLE_SAFE_RETURN, 0x282F10) // where the view setup's call to it returns +// Per-player HUD (mcc/hud) +DefOffset(OFFSET_HALO3_PV_HUD_DRAWING_PLAYER, 0xAD317C) // int user whose HUD is being drawn +DefOffset(OFFSET_HALO3_PV_HUD_CANVAS, 0xAD3148) // float width, height of that user's HUD virtual canvas + struct halo3_teb_data_definition { void* zero; void* actor; diff --git a/lib/game/inc/1.3528.0.0/offset_halo3odst.h b/lib/game/inc/1.3528.0.0/offset_halo3odst.h index aef32efd..01af1a48 100644 --- a/lib/game/inc/1.3528.0.0/offset_halo3odst.h +++ b/lib/game/inc/1.3528.0.0/offset_halo3odst.h @@ -32,3 +32,7 @@ DefOffset(OFFSET_HALO3ODST_PF_HUD_LAYOUT, 0x3284B4) // record* - +0x94 int canva DefOffset(OFFSET_HALO3ODST_PF_VIEW_SETUP, 0x2ABAC8) // void (view*, int slot, int players, int, int, void*) - as Halo 3's DefOffset(OFFSET_HALO3ODST_PF_TITLE_SAFE, 0x29F634) // void (short rect[4]) - the screen's title-safe box, 5% in from each edge DefOffset(OFFSET_HALO3ODST_VIEW_SETUP_TITLE_SAFE_RETURN, 0x2ABB14) // where the view setup's call to it returns + +// Per-player HUD (mcc/hud); see offset_halo3.h +DefOffset(OFFSET_HALO3ODST_PV_HUD_DRAWING_PLAYER, 0xB1AA04) +DefOffset(OFFSET_HALO3ODST_PV_HUD_CANVAS, 0xB1A9D8) diff --git a/lib/game/inc/1.3528.0.0/offset_haloreach.h b/lib/game/inc/1.3528.0.0/offset_haloreach.h index c7428153..ef9789e1 100644 --- a/lib/game/inc/1.3528.0.0/offset_haloreach.h +++ b/lib/game/inc/1.3528.0.0/offset_haloreach.h @@ -139,6 +139,12 @@ DefOffset(OFFSET_HALOREACH_PV_HUD_CANVAS, 0xD1F6F0) // float width, height of th DefOffset(OFFSET_HALOREACH_DAT_HUD_BASIS, 0xD1F740) // float4[5], ending where the next begins DefOffset(OFFSET_HALOREACH_DAT_HUD_SCREEN_SCALE_AND_OFFSET, 0xD1F790) // chud_screen_scale_and_offset: float xInset, yInset, halfW, halfH +// Per-player HUD (mcc/hud; the player is DAT_CURRENT_HUD_PLAYER, the canvas PV_HUD_CANVAS). Where the per-widget +// get_hud_element_transform call (0x2D93A4, widget anchor -> element) returns. UpdatePlayerHudView already asks for the +// motion sensor (element 1) once per view and bakes the answer into the radar centre the dish and blips draw from, so a +// dish widget asking again here would get it twice. +DefOffset(OFFSET_HALOREACH_V_HUD_WIDGET_TRANSFORM_RETURN, 0x2D9443) + // Dev Tools' resolution/FOV table dump (mcc/module/Module.cpp) DefOffset(OFFSET_HALOREACH_PF_GET_SPLITSCREEN_SLOT_TOKEN, 0x53EC8) // unsigned (int slot) DefOffset(OFFSET_HALOREACH_PF_RESOLVE_HUD_PROFILE, 0x2C2F60) // int (unsigned token) diff --git a/lib/game/inc/1.3528.0.0/patterns_halo3.inc b/lib/game/inc/1.3528.0.0/patterns_halo3.inc index 4028b749..a498ea55 100644 --- a/lib/game/inc/1.3528.0.0/patterns_halo3.inc +++ b/lib/game/inc/1.3528.0.0/patterns_halo3.inc @@ -115,4 +115,10 @@ const OffsetPatterns kHalo3Patterns[] = { Rel("E8 ?? ?? ?? ?? 66 83 85 10 09 00 00 50 BA 04 00 00 00 48 8D 4D 70", 0x1, 0x5, 0x0)}}, {&OFFSET_HALO3_VIEW_SETUP_TITLE_SAFE_RETURN, 0x282f10, Code, { At("E8 ?? ?? ?? ?? 48 8D 15 ?? ?? ?? ?? 49 8B EF 49 8D 47 FC 4A 8D 04 B0 48 8D 0C 80", 0x5)}}, + {&OFFSET_HALO3_PV_HUD_DRAWING_PLAYER, 0xad317c, Data, { + Rel("8B 15 ?? ?? ?? ?? F3 0F 59 C1 F3 4C 0F 2C C8 F3 41 0F 10 00", 0x2, 0x6, 0x0), + Rel("48 63 05 ?? ?? ?? ?? 48 69 C8 C8 28 00 00 4A 8B 04 02 8B 94 08 60 14 00 00", 0x3, 0x7, 0x0)}}, + {&OFFSET_HALO3_PV_HUD_CANVAS, 0xad3148, Data, { + Rel("F3 0F 11 1D ?? ?? ?? ?? 66 0F 6E 43 14 0F 5B C0 F3 0F 11 35", 0x4, 0x8, 0x0), + Rel("F3 0F 10 05 ?? ?? ?? ?? F3 0F 11 44 24 30 F3 0F 11 4C 24 34", 0x4, 0x8, 0x0)}}, }; diff --git a/lib/game/inc/1.3528.0.0/patterns_halo3odst.inc b/lib/game/inc/1.3528.0.0/patterns_halo3odst.inc index a69482f9..0cab920a 100644 --- a/lib/game/inc/1.3528.0.0/patterns_halo3odst.inc +++ b/lib/game/inc/1.3528.0.0/patterns_halo3odst.inc @@ -55,4 +55,10 @@ const OffsetPatterns kHalo3odstPatterns[] = { Rel("E8 ?? ?? ?? ?? 66 83 85 10 09 00 00 50 BA 04 00 00 00 48 8D 4D 70", 0x1, 0x5, 0x0)}}, {&OFFSET_HALO3ODST_VIEW_SETUP_TITLE_SAFE_RETURN, 0x2abb14, Code, { At("E8 ?? ?? ?? ?? 48 8D 15 ?? ?? ?? ?? 49 8B EF 49 8D 47 FC 4A 8D 04 B0 48 8D 0C 80", 0x5)}}, + {&OFFSET_HALO3ODST_PV_HUD_DRAWING_PLAYER, 0xb1aa04, Data, { + Rel("44 89 3D ?? ?? ?? ?? E8 ?? ?? ?? ?? 89 05 ?? ?? ?? ?? 41 8B CF 8B 94 3E 68 15 00 00", 0x3, 0x7, 0x0), + Rel("8B 15 ?? ?? ?? ?? 0F 29 74 24 20 F3 0F 10 35 ?? ?? ?? ?? F3 0F 59 C6 F3 4C 0F 2C C8", 0x2, 0x6, 0x0)}}, + {&OFFSET_HALO3ODST_PV_HUD_CANVAS, 0xb1a9d8, Data, { + Rel("F3 0F 10 0D ?? ?? ?? ?? F3 0F 11 4D F8 66 0F 6E C0 0F 5B C0", 0x4, 0x8, 0x0), + Rel("F3 0F 10 05 ?? ?? ?? ?? F3 0F 11 44 24 38 F3 0F 11 4C 24 3C", 0x4, 0x8, 0x0)}}, }; diff --git a/lib/game/inc/1.3528.0.0/patterns_haloreach.inc b/lib/game/inc/1.3528.0.0/patterns_haloreach.inc index 2b0b7a00..d196491e 100644 --- a/lib/game/inc/1.3528.0.0/patterns_haloreach.inc +++ b/lib/game/inc/1.3528.0.0/patterns_haloreach.inc @@ -229,6 +229,8 @@ const OffsetPatterns kHaloreachPatterns[] = { {&OFFSET_HALOREACH_DAT_HUD_SCREEN_SCALE_AND_OFFSET, 0xd1f790, Data, { Rel("F3 0F 58 05 ?? ?? ?? ?? F3 44 0F 10 65 97 45 33 D2 F3 44 0F 10 55 9B", 0x4, 0x8, 0x0), Rel("F3 0F 10 05 ?? ?? ?? ?? 48 8D 55 A0 F3 0F 10 0D ?? ?? ?? ?? 41 B8 10 00 00 00", 0x4, 0x8, 0x0)}}, + {&OFFSET_HALOREACH_V_HUD_WIDGET_TRANSFORM_RETURN, 0x2d9443, Code, { + At("41 FF D2 84 C0 74 51 48 8B 0D ?? ?? ?? ?? 48 85 C9 74 ?? 48 8B 01", 0x3)}}, {&OFFSET_HALOREACH_PF_GET_SPLITSCREEN_SLOT_TOKEN, 0x53ec8, Code, { At("83 C8 FF 83 F9 03 77 27 8B 15 ?? ?? ?? ?? 65 48 8B 04 25 58 00 00 00 41 B8 58 01 00 00 48 63 C9 48 8B 04 D0 4A 8B 04 00 8B 84 88 B8 00 00 00", 0x0), Rel("E8 ?? ?? ?? ?? 48 8D 4F 04 8B D8 33 D2 41 B8 9C 01 00 00 E8", 0x1, 0x5, 0x0)}}, diff --git a/src/mcc/CGameManager.cpp b/src/mcc/CGameManager.cpp index 139172e6..5a691106 100644 --- a/src/mcc/CGameManager.cpp +++ b/src/mcc/CGameManager.cpp @@ -87,6 +87,9 @@ bool CGameManager::Initialize(CGameManager* mng) { {pGameManager->table->set_state, set_state, (void**)&ppOriginal.set_state}, {pGameManager->table->game_restart, game_restart, (void**)&ppOriginal.game_restart}, {pGameManager->table->game_setup, game_setup, (void**)&ppOriginal.game_setup}, + {pGameManager->table->get_hud_element_anchor, get_hud_element_anchor, (void**)&ppOriginal.get_hud_element_anchor}, + {pGameManager->table->get_hud_element_transform, get_hud_element_transform, (void**)&ppOriginal.get_hud_element_transform}, + {pGameManager->table->transform_hud_color, transform_hud_color, (void**)&ppOriginal.transform_hud_color}, }); } diff --git a/src/mcc/CGameManager.h b/src/mcc/CGameManager.h index 43406596..a51a03b6 100644 --- a/src/mcc/CGameManager.h +++ b/src/mcc/CGameManager.h @@ -7,6 +7,8 @@ #include "CGameEngine.h" +#include + struct CGameManager { public: struct Profile_t { @@ -49,6 +51,11 @@ static bool __fastcall get_key_state(CGameManager* self, DWORD index, input_data_t* p_input); static CGamepadMapping* __fastcall retrive_gamepad_mapping(CGameManager* self, __int64 xid); + // CGameManagerHud.cpp + static bool __fastcall get_hud_element_anchor(CGameManager* self, int element, int* anchor); + static bool __fastcall get_hud_element_transform(CGameManager* self, int element, float* dx, float* dy, float* scale); + static unsigned __fastcall transform_hud_color(CGameManager* self, int user, unsigned argb); + static void __fastcall set_state(CGameManager* self, eState state); static void* __fastcall game_restart(CGameManager* self, int type, const char* reason); static char __fastcall game_setup(CGameManager* self, void* a2); @@ -93,7 +100,15 @@ // 0x2C0i64 get_xbox_user_id bool (__fastcall* get_xbox_user_id)(CGameManager* self, __int64* pId, wchar_t* pName, int size, int index); - char pad5[0xD8]; + char pad5[0x30]; + + // 0x2F8..0x308: the games ask MCC for an override of each HUD element before placing it, and + // Halo 3, ODST and Reach pass every HUD colour, with the user it's drawn for, through +0x308. + bool (__fastcall* get_hud_element_anchor)(CGameManager* self, int element, int* anchor); + bool (__fastcall* get_hud_element_transform)(CGameManager* self, int element, float* dx, float* dy, float* scale); + unsigned (__fastcall* transform_hud_color)(CGameManager* self, int user, unsigned argb); + + char pad6[0x90]; // 0x3A0i64 retrieve gamepad mapping CGamepadMapping* (__fastcall* retrive_gamepad_mapping)(CGameManager* self, __int64 xid); @@ -105,5 +120,10 @@ Microsoft::Xbox::Telemetry::CELL* cell; }; +static_assert(offsetof(CGameManager::FunctionTable, get_hud_element_anchor) == 0x2F8); +static_assert(offsetof(CGameManager::FunctionTable, get_hud_element_transform) == 0x300); +static_assert(offsetof(CGameManager::FunctionTable, transform_hud_color) == 0x308); +static_assert(offsetof(CGameManager::FunctionTable, retrive_gamepad_mapping) == 0x3A0); + extern CGameManager* pGameManager; inline CGameManager* GameManager() { return pGameManager; } diff --git a/src/mcc/CGameManagerHud.cpp b/src/mcc/CGameManagerHud.cpp new file mode 100644 index 00000000..8af5da14 --- /dev/null +++ b/src/mcc/CGameManagerHud.cpp @@ -0,0 +1,33 @@ +#include "CGameManager.h" + +#include "mcc/hud/Hud.h" + +#include + +// The games call these host methods from their HUD drawing (MCC's own versions back a feature-flagged HUD editor +// and the colour-blind filter). MCC's answer is kept and the drawing player's HUD settings are added on top - see +// mcc/hud/Hud.h. With a player's settings at their defaults the game gets MCC's answer as it was. + +bool CGameManager::get_hud_element_transform(CGameManager* self, int element, float* dx, float* dy, float* scale) { + if (ppOriginal.get_hud_element_transform(self, element, dx, dy, scale)) { + MCC::Hud::Transform(element, dx, dy, scale, _ReturnAddress()); + return true; + } + // MCC has none: ours alone, written only when there is one + float x = 0.0f, y = 0.0f, s = 1.0f; + if (!MCC::Hud::Transform(element, &x, &y, &s, _ReturnAddress())) return false; + *dx = x; + *dy = y; + *scale = s; + return true; +} + +// Only asked after get_hud_element_transform answered true. +bool CGameManager::get_hud_element_anchor(CGameManager* self, int element, int* anchor) { + if (ppOriginal.get_hud_element_anchor(self, element, anchor)) return true; + return MCC::Hud::Anchor(element, anchor); +} + +unsigned CGameManager::transform_hud_color(CGameManager* self, int user, unsigned argb) { + return MCC::Hud::Color(user, ppOriginal.transform_hud_color(self, user, argb)); +} diff --git a/src/mcc/hud/Hud.cpp b/src/mcc/hud/Hud.cpp new file mode 100644 index 00000000..ae0cbf9d --- /dev/null +++ b/src/mcc/hud/Hud.cpp @@ -0,0 +1,406 @@ +#include "Hud.h" + +#include "common.h" + +#include "mcc/CGameGlobal.h" +#include "mcc/module/Module.h" +#include "mcc/splitscreen/LeftRight.h" + +#include +#include +#include +#include +#include + +#include "imgui.h" + +#include +#include +#include +#include +#include + +// Halo CE's placement hook (module/entry/halo1/hud.cpp) +namespace Halo1::Entry::Hud { const AlphaRing::Feature& Hooks(); } + +namespace MCC::Hud { + // The settings the games read. A player's are replaced whole under the exclusive lock and copied whole under + // the shared one, so a HUD callback on a game thread never sees half of an edit. An SRW lock rather than + // std::mutex, as in SplitscreenConfigStore: no dependence on the msvcp140 MCC loads. + static SRWLOCK s_lock = SRWLOCK_INIT; + static PlayerHud s_players[kPlayers]; + + PlayerHud Settings(int player) { + PlayerHud settings; // the defaults for no player, which change nothing + if (player < 0 || player >= kPlayers) return settings; + AcquireSRWLockShared(&s_lock); + settings = s_players[player]; + ReleaseSRWLockShared(&s_lock); + return settings; + } + + void SetSettings(int player, const PlayerHud& settings) { + if (player < 0 || player >= kPlayers) return; + AcquireSRWLockExclusive(&s_lock); + s_players[player] = settings; + ReleaseSRWLockExclusive(&s_lock); + } + + // --- the games ----------------------------------------------------------------------------- + + // How a game's host offsets are measured. + enum class Units { + Canvas, // the HUD's virtual canvas (Halo 3, ODST, Reach): size = float width, height + Pixels, // screen pixels / unit scale (Halo 2): size = int16 view rect {t, l, b, r} + None, // offsets come from elsewhere (Halo CE: OffsetCE) + }; + + // What each game's answers read. In an MCC build without one of them, that game's HUD is left as it is. Halo CE's + // also need its placement hook, which moves CE's elements (OffsetCE), and Reach's the return address that tells + // its widgets' motion-sensor ask apart (without it the radar would move twice). + static AlphaRing::Feature s_halo1{"Halo CE per-player HUD", {OFFSET_HALO1_PV_HUD_DRAWING_PLAYER}}; + const bool s_halo1_hook = (s_halo1.Requires(Halo1::Entry::Hud::Hooks()), true); + static AlphaRing::Feature s_halo2{"Halo 2 per-player HUD", {OFFSET_HALO2_PV_HUD_DRAWING_PLAYER, + OFFSET_HALO2_PV_HUD_VIEW_BOUNDS, OFFSET_HALO2_PV_HUD_UNIT_SCALE}}; + static AlphaRing::Feature s_halo3{"Halo 3 per-player HUD", {OFFSET_HALO3_PV_HUD_DRAWING_PLAYER, + OFFSET_HALO3_PV_HUD_CANVAS}}; + static AlphaRing::Feature s_halo3odst{"ODST per-player HUD", {OFFSET_HALO3ODST_PV_HUD_DRAWING_PLAYER, + OFFSET_HALO3ODST_PV_HUD_CANVAS}}; + static AlphaRing::Feature s_haloreach{"Reach per-player HUD", {OFFSET_HALOREACH_DAT_CURRENT_HUD_PLAYER, + OFFSET_HALOREACH_PV_HUD_CANVAS, + OFFSET_HALOREACH_V_HUD_WIDGET_TRANSFORM_RETURN}}; + + // Per game: where it keeps the player whose HUD is being drawn, how MCC's element ids map to + // ours (1 motion sensor, 2-5 grenade kinds, 6 shield/health, 7 weapon, 9 crosshair, + // 11 equipment, 12 messages; Halo 2 has no separate shield element - its shield sits with + // the motion tracker), the units of its host offsets, and for the HUD area presets the shape + // of the box the game lays its HUD out in (0: the whole view; Halo CE: a centered 4:3 box) + // and which side of it each of our elements sits on (-1 left, 1 right, 0 centered). + struct Game { + int id; + const AlphaRing::Feature* offsets; // every offset below + const AlphaRing::Offset* drawing_user; + bool user_is_short; + const signed char* elements; // [16], by MCC element id + Units units; + const AlphaRing::Offset *size, *unit_scale; // or none + float area; + signed char sides[kElementCount]; // motion sensor, shield, weapon, grenades, crosshair, equipment, messages + bool (*box_is_area)(__int64 module) = nullptr; // whether the HUD box really is `area` right now + const AlphaRing::Offset* widget_motion_sensor = nullptr; // a widget asking for the motion sensor the view moved + }; + + // Halo 2 lays its HUD out in the whole view unless the profiles' "HUD anchor: Centered" is on + // and our fix for it (the "HUD at screen edges" patch) has been switched off. In a build where + // that can't be told, the area presets leave Halo 2's HUD alone. + static bool Halo2HudInView(__int64 module) { + if (!AlphaRing::Found({OFFSET_HALO2_PV_HUD_ASPECT_LOCK, OFFSET_HALO2_PF_HUD_ASPECT_LOCK})) return false; + return !*(const bool*)(module + OFFSET_HALO2_PV_HUD_ASPECT_LOCK) || + *(const unsigned char*)(module + OFFSET_HALO2_PF_HUD_ASPECT_LOCK) == 0xEB; + } + + static constexpr signed char kHalo1Elements[16] = { + -1, MotionSensor, Grenades, -1, -1, -1, Shield, Weapon, -1, Crosshair, -1, -1, Messages, -1, -1, -1}; + static constexpr signed char kHalo2Elements[16] = { + -1, MotionSensor, Grenades, -1, -1, -1, -1, Weapon, -1, Crosshair, -1, -1, Messages, -1, -1, -1}; + static constexpr signed char kGen3Elements[16] = { + -1, MotionSensor, Grenades, Grenades, Grenades, Grenades, Shield, Weapon, -1, Crosshair, -1, Equipment, -1, -1, -1, -1}; + + static constexpr Game kGames[] = { + {CGameGlobal::Halo1, &s_halo1, &OFFSET_HALO1_PV_HUD_DRAWING_PLAYER, true, kHalo1Elements, Units::None, nullptr, + nullptr, 4.0f / 3.0f, {-1, 1, -1, -1, 0, 0, -1}}, + {CGameGlobal::Halo2, &s_halo2, &OFFSET_HALO2_PV_HUD_DRAWING_PLAYER, false, kHalo2Elements, Units::Pixels, + &OFFSET_HALO2_PV_HUD_VIEW_BOUNDS, &OFFSET_HALO2_PV_HUD_UNIT_SCALE, 0.0f, {-1, 0, 1, -1, 0, 0, -1}, Halo2HudInView}, + {CGameGlobal::Halo3, &s_halo3, &OFFSET_HALO3_PV_HUD_DRAWING_PLAYER, false, kGen3Elements, Units::Canvas, + &OFFSET_HALO3_PV_HUD_CANVAS, nullptr, 0.0f, {-1, 0, 1, -1, 0, -1, 0}}, + {CGameGlobal::Halo3ODST, &s_halo3odst, &OFFSET_HALO3ODST_PV_HUD_DRAWING_PLAYER, false, kGen3Elements, + Units::Canvas, &OFFSET_HALO3ODST_PV_HUD_CANVAS, nullptr, 0.0f, {-1, -1, 1, 1, 0, 1, 0}}, + {CGameGlobal::HaloReach, &s_haloreach, &OFFSET_HALOREACH_DAT_CURRENT_HUD_PLAYER, false, kGen3Elements, + Units::Canvas, &OFFSET_HALOREACH_PV_HUD_CANVAS, nullptr, 0.0f, {-1, 0, 1, -1, 0, -1, 0}, nullptr, + &OFFSET_HALOREACH_V_HUD_WIDGET_TRANSFORM_RETURN}, + }; + static_assert(kGames[0].id == CGameGlobal::Halo1); // OffsetCE's + + static const Game* CurrentGame(__int64& module) { + auto p_global = GameGlobal(); + if (p_global == nullptr) return nullptr; + for (auto& game : kGames) { + if (game.id != p_global->current_game) continue; + module = MCC::Module::GetSubModule(game.id)->info().hModule; + if (module == 0) return nullptr; + if (auto missing = game.offsets->Missing()) { // an MCC build without it (src/offsets) + static std::atomic logged[std::size(kGames)]; // the game threads ask + auto& once = logged[&game - kGames]; + if (!once.load(std::memory_order_relaxed) && !once.exchange(true)) + LOG_WARNING("[Offsets] {} stays off: {} isn't in this build", game.offsets->Name(), missing->name); + return nullptr; + } + return &game; + } + return nullptr; + } + + static int ElementOf(const Game& game, int game_element) { + return game_element >= 0 && game_element < 16 ? game.elements[game_element] : -1; + } + + // Halo 3, ODST and Reach ask for the motion sensor while they set each view's HUD up, before they store that + // view's canvas (Halo 3 0x2ED267, then 0x2ED40C; ODST 0x3287F5, then 0x328991; Reach 0x2D96B7, then 0x2D984E): + // the canvas then is still the previous view's. Every other element is asked for after, so each user's canvas + // is kept from those asks, and the motion sensor's ask gets its own view's, as of the frame before. + struct Canvas { float width, height; }; + static std::atomic s_view_canvas[std::size(kGames)][kPlayers]; // {0, 0}: not seen yet + + static void ViewCanvas(const Game& game, int user, int game_element, float* width, float* height) { + if (user < 0 || user >= kPlayers) return; + auto& kept = s_view_canvas[&game - kGames][user]; + if (ElementOf(game, game_element) != MotionSensor) { + kept.store({*width, *height}, std::memory_order_relaxed); + return; + } + Canvas canvas = kept.load(std::memory_order_relaxed); + if (canvas.width <= 0.0f) return; // first frame: the game's + *width = canvas.width; + *height = canvas.height; + } + + // What `settings` do to one element of `game` in a view `aspect` wide per unit of height + // (0: no area presets); false when nothing. + static bool Effective(const Game& game, const PlayerHud& settings, int game_element, float aspect, float* x, + float* y, float* scale) { + int element = ElementOf(game, game_element); + if (element < 0) return false; + auto& layout = settings.elements[element]; + *x = layout.x; + *y = layout.y; + *scale = layout.hidden ? 0.0f : layout.scale * settings.scale; + // the area preset moves the sides of the game's HUD box to those of a centered box of that + // shape (no wider than the view) + if (settings.area > 0 && aspect > 0.0f) { + float from = game.area > 0.0f ? std::min(game.area, aspect) : aspect; + *x += game.sides[element] * 0.5f * (std::min(kAreas[settings.area], aspect) - from) / aspect; + } + return *x != 0.0f || *y != 0.0f || *scale != 1.0f; + } + + bool Transform(int game_element, float* dx, float* dy, float* scale, const void* caller) { + __int64 module; + auto game = CurrentGame(module); + if (game == nullptr) return false; + if (game->widget_motion_sensor && caller == (const void*)(module + *game->widget_motion_sensor) && + ElementOf(*game, game_element) == MotionSensor) + return false; + + auto user_at = module + *game->drawing_user; + int user = game->user_is_short ? *(short*)user_at : *(int*)user_at; + + // the size of the drawing player's view: the HUD's virtual canvas, or pixels (Halo 2); + // Halo CE takes only sizes from here (its offsets: OffsetCE) + float width = 0.0f, height = 1.0f; + if (game->units == Units::Canvas) { + auto canvas = (const float*)(module + *game->size); // width, height + width = canvas[0]; + height = canvas[1]; + ViewCanvas(*game, user, game_element, &width, &height); + } else if (game->units == Units::Pixels) { + auto& rect = *(const Splitscreen::LeftRight::Rect*)(module + *game->size); + width = rect.right - rect.left; + height = rect.bottom - rect.top; + } + + float aspect = height > 0.0f && (!game->box_is_area || game->box_is_area(module)) ? width / height : 0.0f; + float x, y, s; + if (!Effective(*game, Settings(user), game_element, aspect, &x, &y, &s)) return false; + *scale *= s; + if (s == 0.0f || game->units == Units::None || height <= 0.0f) return true; + if (game->units == Units::Pixels) { + // Halo 2 moves an element by offset x unit scale x its final scale pixels + float unit = *(const float*)(module + *game->unit_scale) * *scale; + if (unit <= 0.0f) return true; + width /= unit; + height /= unit; + } + *dx += x * width; + *dy -= y * height; // our y points up, the screen's down + return true; + } + + bool Anchor(int game_element, int* anchor) { + float dx = 0.0f, dy = 0.0f, scale = 1.0f; + if (!Transform(game_element, &dx, &dy, &scale)) return false; + *anchor = 1; // "unchanged": our offsets are relative to where the element already is + return true; + } + + bool OffsetCE(int player, int game_element, float view_width, float view_height, float* dx, float* dy) { + float x, y, s; + if (!s_halo1.Available() || view_height <= 0.0f || // with CE's other settings only + !Effective(kGames[0], Settings(player), game_element, view_width / view_height, &x, &y, &s) || + (x == 0.0f && y == 0.0f)) + return false; + *dx = x * view_width; + *dy = -y * view_height; + return true; + } + + // --- colours --------------------------------------------------------------------------------- + + static float Wrap(float degrees) { return degrees - 360.0f * std::floor(degrees / 360.0f); } + + // Moves a colour's hue toward `hue`, keeping its brightness, saturation and alpha. Greys and + // whites stay as they are, and so do strong reds: the HUD uses them for enemies and danger. + static unsigned Recolor(unsigned argb, float hue, float strength) { + float r = ((argb >> 16) & 0xFF) / 255.0f, g = ((argb >> 8) & 0xFF) / 255.0f, b = (argb & 0xFF) / 255.0f; + float max = std::max({r, g, b}), min = std::min({r, g, b}), delta = max - min; + if (max <= 0.0f || delta / max < 0.15f) return argb; + + float h = max == r ? 60.0f * std::fmod((g - b) / delta, 6.0f) : max == g ? 60.0f * ((b - r) / delta + 2.0f) + : 60.0f * ((r - g) / delta + 4.0f); + h = Wrap(h); + if ((h < 18.0f || h > 342.0f) && delta / max > 0.45f) return argb; + + float shift = Wrap(hue - h + 180.0f) - 180.0f; // shortest way around + h = Wrap(h + shift * strength); + + float s = delta / max, v = max, c = v * s, x = c * (1.0f - std::fabs(std::fmod(h / 60.0f, 2.0f) - 1.0f)), m = v - c; + float rgb[3]; + switch ((int)(h / 60.0f) % 6) { + case 0: rgb[0] = c; rgb[1] = x; rgb[2] = 0; break; + case 1: rgb[0] = x; rgb[1] = c; rgb[2] = 0; break; + case 2: rgb[0] = 0; rgb[1] = c; rgb[2] = x; break; + case 3: rgb[0] = 0; rgb[1] = x; rgb[2] = c; break; + case 4: rgb[0] = x; rgb[1] = 0; rgb[2] = c; break; + default: rgb[0] = c; rgb[1] = 0; rgb[2] = x; break; + } + auto byte = [](float f) { return (unsigned)std::lround(std::clamp(f, 0.0f, 1.0f) * 255.0f); }; + return (argb & 0xFF000000u) | byte(rgb[0] + m) << 16 | byte(rgb[1] + m) << 8 | byte(rgb[2] + m); + } + + unsigned Color(int user, unsigned argb) { + auto settings = Settings(user); + return settings.recolor ? Recolor(argb, settings.hue, settings.strength) : argb; + } + + // --- the HUD window --------------------------------------------------------------------------- + + static const char* kElementNames[kElementCount] = {"Motion tracker", "Shield / health", "Weapon / ammo", + "Grenades", "Crosshair", "Equipment", "Messages"}; + + static const char* const kAreaNames[kAreaCount] = {"Game default", "Screen edges", "21:9 box", "16:9 box", + "4:3 box"}; + + // Edits a copy of the player's settings, published whole when something changed. + static void PlayerTab(int p) { + PlayerHud player = Settings(p); + bool changed = false; + constexpr ImGuiSliderFlags kClamp = ImGuiSliderFlags_AlwaysClamp; + + ImGui::SeparatorText("Size"); + ImGui::SetNextItemWidth(260); + changed |= ImGui::SliderFloat("Whole HUD", &player.scale, 0.25f, 3.0f, "%.2fx", kClamp); + + // presets by the shape of screen the player looks at, for views wider than their eyes follow + ImGui::SetNextItemWidth(260); + changed |= ImGui::Combo("HUD area", &player.area, kAreaNames, kAreaCount); + ImGui::SameLine(); + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("Lays the HUD out in a box of this shape in the middle of the player's view: pull it\n" + "in from the far edges of an ultrawide (21:9) or multi-monitor view to a TV-shaped\n" + "(16:9) or classic (4:3) box, or spread Halo CE's HUD (always a 4:3 box) out to the\n" + "screen edges."); + + ImGui::SeparatorText("Colour (Halo 3, ODST, Reach)"); + changed |= ImGui::Checkbox("Recolour this player's HUD", &player.recolor); + ImGui::BeginDisabled(!player.recolor); + ImGui::ColorButton("##preview", ImColor::HSV(player.hue / 360.0f, 0.75f, 1.0f), ImGuiColorEditFlags_NoTooltip, + ImVec2(ImGui::GetFrameHeight() * 2, ImGui::GetFrameHeight())); + ImGui::SameLine(); + ImGui::SetNextItemWidth(260); + changed |= ImGui::SliderFloat("Hue", &player.hue, 0.0f, 360.0f, "%.0f", kClamp); + ImGui::SetNextItemWidth(260); + changed |= ImGui::SliderFloat("Strength", &player.strength, 0.0f, 1.0f, "%.2f", kClamp); + ImGui::EndDisabled(); + ImGui::TextDisabled("Whites and enemy reds keep their colour."); + + ImGui::SeparatorText("Elements"); + if (ImGui::BeginTable("elements", 5, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg)) { + ImGui::TableSetupColumn("Element"); + ImGui::TableSetupColumn("Left / right"); + ImGui::TableSetupColumn("Down / up"); + ImGui::TableSetupColumn("Size"); + ImGui::TableSetupColumn("Hide"); + ImGui::TableHeadersRow(); + for (int e = 0; e < kElementCount; ++e) { + auto& l = player.elements[e]; + ImGui::PushID(e); + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + ImGui::TextUnformatted(kElementNames[e]); + // the aim point doesn't move with the crosshair's picture, so it can only be resized or hidden + ImGui::BeginDisabled(e == Crosshair); + ImGui::TableSetColumnIndex(1); + ImGui::SetNextItemWidth(150); + changed |= ImGui::SliderFloat("##x", &l.x, -0.5f, 0.5f, "%+.2f", kClamp); + ImGui::TableSetColumnIndex(2); + ImGui::SetNextItemWidth(150); + changed |= ImGui::SliderFloat("##y", &l.y, -0.5f, 0.5f, "%+.2f", kClamp); + ImGui::EndDisabled(); + ImGui::TableSetColumnIndex(3); + ImGui::SetNextItemWidth(130); + changed |= ImGui::SliderFloat("##scale", &l.scale, 0.25f, 3.0f, "%.2fx", kClamp); + ImGui::TableSetColumnIndex(4); + changed |= ImGui::Checkbox("##hide", &l.hidden); + ImGui::PopID(); + } + ImGui::EndTable(); + } + ImGui::TextDisabled("Offsets are fractions of the player's view. Messages move in Halo CE and Halo 2, equipment"); + ImGui::TextDisabled("in Halo 3, ODST and Reach; Halo 2's shield moves with its motion tracker."); + + ImGui::Spacing(); + if (ImGui::Button("Reset this player")) { + player = PlayerHud(); + changed = true; + } + ImGui::SameLine(); + bool copy = ImGui::Button("Copy to all players"); + + if (changed) SetSettings(p, player); + if (copy) + for (int other = 0; other < kPlayers; ++other) SetSettings(other, player); + } + + static void Window() { + ImGui::TextWrapped("Move, resize, hide or recolour each player's HUD - in Halo CE, Halo 2, Halo 3, ODST and " + "Halo Reach. Changes show up right away and last until MCC closes; they aren't saved."); + if (ImGui::BeginTabBar("players")) { + for (int p = 0; p < kPlayers; ++p) { + char label[16]; + snprintf(label, sizeof(label), "Player %d", p + 1); + if (ImGui::BeginTabItem(label)) { + PlayerTab(p); + ImGui::EndTabItem(); + } + } + ImGui::EndTabBar(); + } + } + + void ImGuiContext() { + static bool show_hud; + + if (ImGui::BeginMainMenuBar()) { + ImGui::MenuItem("HUD", nullptr, &show_hud); + ImGui::EndMainMenuBar(); + } + + if (show_hud) { + // nothing of it is saved, its place in imgui.ini included + ImGui::SetNextWindowSize(ImVec2(760, 0), ImGuiCond_FirstUseEver); + if (ImGui::Begin("HUD", &show_hud, ImGuiWindowFlags_NoSavedSettings)) + Window(); + ImGui::End(); + } + } +} diff --git a/src/mcc/hud/Hud.h b/src/mcc/hud/Hud.h new file mode 100644 index 00000000..0a7098eb --- /dev/null +++ b/src/mcc/hud/Hud.h @@ -0,0 +1,57 @@ +#pragma once + +// Per-player HUD customization: move, scale or hide HUD elements and recolour the HUD, separately +// for each local player. +// +// Halo CE, 2, 3, ODST and Reach ask MCC's host (CGameManager) for a per-element override right before +// placing each HUD element (see CGameManagerHud.cpp); Halo 3, ODST and Reach also pass every HUD colour, +// with the drawing player, through the host. Answering those calls for the player being drawn is +// the whole trick: the games apply the override to that element and put it back after drawing. +// Halo CE applies host offsets wrongly (both to X), so it gets its offsets from a placement hook +// instead (module/entry/halo1/hud.cpp). +// +// The settings are kept in memory for the session; nothing is saved yet. +namespace MCC::Hud { + enum Element : int { MotionSensor, Shield, Weapon, Grenades, Crosshair, Equipment, Messages, kElementCount }; + + struct ElementLayout { + float x = 0.0f, y = 0.0f; // offset, as a fraction of the HUD canvas width/height + float scale = 1.0f; + bool hidden = false; + }; + + // Screen shapes (width / height) a player's HUD can be laid out in, centered in their view: + // the game's own layout, the view's full width (its edges), or a box of that shape. + constexpr float kFullWidth = 100.0f; + constexpr float kAreas[] = {0.0f, kFullWidth, 21.0f / 9.0f, 16.0f / 9.0f, 4.0f / 3.0f}; + constexpr int kAreaCount = sizeof(kAreas) / sizeof(*kAreas); + + struct PlayerHud { + float scale = 1.0f; // every element + int area = 0; // index in kAreas + ElementLayout elements[kElementCount]; + bool recolor = false; + float hue = 200.0f; // degrees + float strength = 1.0f; // 0 = game colours, 1 = fully the chosen hue + }; + + constexpr int kPlayers = 4; + + // A player's settings, as the games' HUD drawing reads them: a copy, and a whole new set put in + // place at once (the HUD window publishes its edits so). What a profile would save and load. A + // default PlayerHud changes nothing. + PlayerHud Settings(int player); + void SetSettings(int player, const PlayerHud& settings); + + // Host callbacks (CGameManagerHud.cpp): true when `element` of the player whose HUD is being + // drawn has an override, composed onto MCC's own values. `caller`: the game's return address. + bool Transform(int game_element, float* dx, float* dy, float* scale, const void* caller = nullptr); + bool Anchor(int game_element, int* anchor); + unsigned Color(int user, unsigned argb); + + // Halo CE: pixels to move `element` of `player`'s HUD within a view of the given size. + bool OffsetCE(int player, int game_element, float view_width, float view_height, float* dx, float* dy); + + // The HUD window (overlay menu bar). + void ImGuiContext(); +} diff --git a/src/mcc/module/CModule.cpp b/src/mcc/module/CModule.cpp index 2624c195..6de3ae22 100644 --- a/src/mcc/module/CModule.cpp +++ b/src/mcc/module/CModule.cpp @@ -186,6 +186,13 @@ static struct { {"splitscreen_patch2", "", OFFSET_HALO2_PF_PLAYER_COUNT1, "\x04", true, &Halo2FourPlayers}, {"splitscreen_patch3", "", OFFSET_HALO2_PF_PLAYER_COUNT2, "\x04", true, &Halo2FourPlayers}, {"splitscreen_patch4", "force making splitscreen works with more than 2 players", OFFSET_HALO2_TWO_PLAYERS_TEST_HAND_OVER, "\x83\xF8\x01\x74\x04", true, &Halo2FourPlayers}, + // With MCC's default "HUD anchor: Centered", Halo 2 fits the whole HUD frame into a centred 16:9 box + // on wider screens and splits that box between the players, so on ultrawide or multi-monitor + // screens every split-screen player's HUD bunches up toward the middle of the screen. On, it's + // "Edge" whatever the profile says, with one player too, so it's off by default. + {"HUD at screen edges", "split screen on a screen wider than 16:9 (ultrawide, several monitors): keep every " + "player's HUD at the edges of their own view, not squeezed into a centred 16:9 box - MCC's " + "'HUD anchor: Edge', with one player too", OFFSET_HALO2_PF_HUD_ASPECT_LOCK, "\xEB", false}, // Save & Quit from a modded campaign with 2+ players hung on the loading screen (reported by SR388): Halo 2 // decided the game came from its own lobby and went back to it by loading its main-menu map, which MCC // doesn't have. MCC is the lobby, so quitting always goes back to it. diff --git a/src/mcc/module/entry/halo1/hud.cpp b/src/mcc/module/entry/halo1/hud.cpp new file mode 100644 index 00000000..db4ac58d --- /dev/null +++ b/src/mcc/module/entry/halo1/hud.cpp @@ -0,0 +1,34 @@ +#include "halo1.h" + +#include "mcc/hud/Hud.h" +#include "mcc/splitscreen/LeftRight.h" + +#include + +// Per-player HUD element offsets for Halo CE (mcc/hud/Hud.h). MCC's own host offsets are added +// to X only in CE, so offsets are applied here instead, to every placed HUD point. +namespace Halo1::Entry::Hud { + // hud_calculate_point(local player, placement, element header, unused, use scale override, + // scale override, out point (int16 x, y relative to the viewport), MCC element id). The scale + // override is a float on the stack, so the original is called through this exact type. + using calculate_point_t = __int64(__fastcall*)(short, void*, void*, void*, bool, float, short*, int); + + EntryFeature("Halo CE per-player HUD", OFFSET_HALO1_PV_HUD_VIEWPORT_BOUNDS); + // CE's settings require it (mcc/hud/Hud.cpp): without this hook none of them apply. + const ::AlphaRing::Feature& Hooks() { return *entry_feature; } + + Halo1Entry(entry, OFFSET_HALO1_PF_HUD_CALCULATE_POINT, __int64, detour, short player, void* placement, void* header, + void* unused, bool use_scale, float scale, short* out, int element) { + auto result = ((calculate_point_t)entry.m_pOriginal)(player, placement, header, unused, use_scale, scale, out, element); + + auto& viewport = *(const MCC::Splitscreen::LeftRight::Rect*)(entry.m_target - entry.m_offset + + OFFSET_HALO1_PV_HUD_VIEWPORT_BOUNDS); + float dx, dy; + if (out && MCC::Hud::OffsetCE(player, element, viewport.right - viewport.left, viewport.bottom - viewport.top, + &dx, &dy)) { + out[0] = (short)(out[0] + std::lround(dx)); + out[1] = (short)(out[1] + std::lround(dy)); + } + return result; + } +} diff --git a/src/mcc/module/entry/halo4/halo4.h b/src/mcc/module/entry/halo4/halo4.h index 446a3ded..ec10ae60 100644 --- a/src/mcc/module/entry/halo4/halo4.h +++ b/src/mcc/module/entry/halo4/halo4.h @@ -22,9 +22,12 @@ namespace Halo4::Entry { constexpr int kFullHudLayout = 0x80076, kHalfHudLayout = 0x80077, kQuarterHudLayout = 0x80078; namespace HudFit { - void Refresh(__int64 module); // each user's fit, once a frame (hud_fit.cpp) const ::AlphaRing::Feature& Hooks(); // its hooks and offsets, which Left/Right requires } + + namespace Splitscreen { + void Frame(__int64 module); // Left/Right's work before each frame, from hud_fit.cpp's hook (splitscreen.cpp) + } } #define Halo4Entry(name, offset, returnType, pDetour, ...) \ diff --git a/src/mcc/module/entry/halo4/hud_fit.cpp b/src/mcc/module/entry/halo4/hud_fit.cpp index 3dd24ff2..aaf92ea7 100644 --- a/src/mcc/module/entry/halo4/hud_fit.cpp +++ b/src/mcc/module/entry/halo4/hud_fit.cpp @@ -1,24 +1,25 @@ -// Halo 4's HUD in a Left/Right half. A half is 960x1080 for the quarter layout's 640x360 units (HUD units: -// pixels over min(width / 1280, height / 720) of the screen), and the layout sits in the view's corner, so the -// HUD kept to the top half of the view, the motion tracker at mid-height. +// Halo 4's HUD in a split view shaped unlike its HUD layout. With the black bars removed a two-player view is +// 1920x540 for the two-player layout's 960x360 units, and a Left/Right half is 960x1080 for the quarter +// layout's 640x360 (HUD units: pixels over min(width / 1280, height / 720) of the screen). The layout sits in +// the view's corner, so the bars-removed HUD kept to the left three quarters and the Left/Right one to the +// top half. // // Halo 4 already fits its 1280x720 layout to screens of another shape (ultrawide): four helpers give the // screen's shape against 16:9 - wider or not, the aspect over 16:9's, the extra width or height either side // - and the UI uses them to centre each screen, keep edge-anchored elements at the screen's edges and place -// what it projects from the world (nametags, waypoints). While a half's HUD is updated or drawn, the helpers -// answer for that view and its layout instead (the other split views keep the game's own answers). The -// crosshair sits at the view's centre already, so the screen's centring would move it: its group is moved back. +// what it projects from the world (nametags, waypoints). While a split view's HUD is updated or drawn, the +// helpers answer for that view and its layout instead (a view its layout fits, as every stock one on a 16:9 +// screen, keeps the game's own answers). The crosshair sits at the view's centre already, so the screen's +// centring would move it: its group is moved back. #include "halo4.h" #include "mcc/module/entry/PreservingThunk.h" #include "mcc/module/patch/SplitscreenConfigStore.h" -#include "mcc/splitscreen/LeftRight.h" #include #include namespace Halo4::Entry::HudFit { - namespace LeftRight = MCC::Splitscreen::LeftRight; namespace Store = AlphaRing::SplitscreenConfigStore; EntryFeature("Halo 4 split-screen HUD fit", OFFSET_HALO4_PV_SPLITSCREEN_TABLE, OFFSET_HALO4_PV_SCREEN_SIZE, @@ -40,8 +41,6 @@ namespace Halo4::Entry::HudFit { Fit Compute(__int64 module, int players, int user) { Fit fit; if (players < 2 || players > 4 || user >= players) return fit; - // a Left/Right half (both two-player views, the three-player left one) - if (!LeftRight::Active(players) || Store::LeftRightRegion(players, user).resolution != 3) return fit; auto& view = ((const Store::LayoutEntry*)(module + OFFSET_HALO4_PV_SPLITSCREEN_TABLE)) [players * Store::SLOT_COUNT + user]; auto screen = (const int*)(module + OFFSET_HALO4_PV_SCREEN_SIZE); @@ -55,6 +54,8 @@ namespace Halo4::Entry::HudFit { if (view_h < 1 || (std::abs(view_w - layout_w) < 1 && std::abs(view_h - layout_h) < 1)) return fit; fit.on = true; fit.aspect = (view_w / view_h) / (layout_w / layout_h); + // One extra, Wide()'s: the world-marker projection (0x3F2783-0x3F27C5) cancels only that one, so a view with + // room both ways (a Left/Right half wider than 16:9, a bars-removed view on 4:3) is centred one way only. if (fit.aspect > 1.0f) fit.extra_x = (view_w - layout_w) * 0.5f; else fit.extra_y = (view_h - layout_h) * 0.5f; return fit; @@ -62,7 +63,6 @@ namespace Halo4::Entry::HudFit { void Refresh(__int64 module) { s_any_fit = false; - if (!entry_feature->Available()) return; // from splitscreen.cpp's render hook: only with the hooks below int players = ((int (*)())(module + OFFSET_HALO4_PF_SPLITSCREEN_PLAYER_COUNT))(); for (int user = 0; user < 4; ++user) { s_fits[user] = Compute(module, players, user); @@ -72,6 +72,14 @@ namespace Halo4::Entry::HudFit { } const bool s_reset = (Halo4SplitscreenEntrySet()->on_remove([] { s_ever_fit = false; }), true); + // Before each frame is drawn: Left/Right's work (splitscreen.cpp, when its hooks are in), then each user's fit. + Halo4SplitscreenEntry(entry_frame, OFFSET_HALO4_PF_RENDER, void, frame) { + __int64 module = entry_frame.m_target - entry_frame.m_offset; + Splitscreen::Frame(module); + Refresh(module); + ((frame_t)entry_frame.m_pOriginal)(); + } + // The fit of the user whose HUD is being worked on, when it's on. const Fit* Current() { return s_any_fit && t_user >= 0 && t_user < 4 && s_fits[t_user].on ? &s_fits[t_user] : nullptr; diff --git a/src/mcc/module/entry/halo4/splitscreen.cpp b/src/mcc/module/entry/halo4/splitscreen.cpp index dc04f856..9ec40b69 100644 --- a/src/mcc/module/entry/halo4/splitscreen.cpp +++ b/src/mcc/module/entry/halo4/splitscreen.cpp @@ -13,8 +13,9 @@ namespace Halo4::Entry::Splitscreen { EntryFeature("Halo 4 Left/Right split screen", OFFSET_HALO4_PV_SPLITSCREEN_TABLE, OFFSET_HALO4_PV_SCREEN_SIZE, OFFSET_HALO4_PF_SPLITSCREEN_PLAYER_COUNT, OFFSET_HALO4_PF_FILL_RECT, OFFSET_HALO4_PF_RT_POOL_RELEASE, OFFSET_HALO4_PF_RT_POOL_INIT, OFFSET_HALO4_PF_RENDER_SETUP_1, OFFSET_HALO4_PF_RENDER_SETUP_2); - // A half's HUD only fits it with hud_fit.cpp's hooks (without them it keeps to the half's top half): on a build - // without one of them, none of these go in either and Halo 4 keeps its stock split. + // A half's HUD only fits it with hud_fit.cpp's hooks (without them it keeps to the half's top half), and Frame + // below runs from one of them: on a build without one of them, none of these go in either and Halo 4 keeps its + // stock split. const bool s_requires_hud_fit = (entry_feature->Requires(HudFit::Hooks()), true); // The bar painter sets the render state up itself before drawing (as Reach's does, blackbars.cpp). @@ -71,12 +72,10 @@ namespace Halo4::Entry::Splitscreen { s_viewmodel_divide = divide + 3; } - Halo4SplitscreenEntry(entry_render, OFFSET_HALO4_PF_RENDER, void, render) { - __int64 module = entry_render.m_target - entry_render.m_offset; + void Frame(__int64 module) { + if (!entry_feature->Available()) return; // only with the hooks above and below LeftRight::Frame(kGame, module, s_state); - HudFit::Refresh(module); ViewmodelAspect(module, LeftRight::Active(LeftRight::Players(kGame, module))); - ((render_t)entry_render.m_pOriginal)(); } Halo4SplitscreenEntry(entry_bars, OFFSET_HALO4_PF_DRAW_SPLITSCREEN_BARS, void, draw_bars) { diff --git a/src/render/imgui/game/mcc/CMCCContext.cpp b/src/render/imgui/game/mcc/CMCCContext.cpp index 041d43ba..057d82df 100644 --- a/src/render/imgui/game/mcc/CMCCContext.cpp +++ b/src/render/imgui/game/mcc/CMCCContext.cpp @@ -4,6 +4,7 @@ #include "mcc/network/Network.h" #include "mcc/splitscreen/Splitscreen.h" +#include "mcc/hud/Hud.h" #include "mcc/module/Module.h" static const char* tutorial_message = R"(TUTORIAL: @@ -57,6 +58,8 @@ void CMCCContext::render() { MCC::Splitscreen::ImGuiContext(); + MCC::Hud::ImGuiContext(); + MCC::Network::ImGuiContext(); if (ImGui::BeginMainMenuBar()) { diff --git a/tools/offsets/gen_patterns.py b/tools/offsets/gen_patterns.py index b973db59..7208a564 100644 --- a/tools/offsets/gen_patterns.py +++ b/tools/offsets/gen_patterns.py @@ -49,6 +49,7 @@ "OFFSET_HALO2_PF_PLAYER_COUNT2": 1, "OFFSET_HALO2_TWO_PLAYERS_TEST_HAND_OVER": 5, "OFFSET_HALO2_PF_QUIT_TO_LOBBY_TEST": 9, + "OFFSET_HALO2_PF_HUD_ASPECT_LOCK": 2, "OFFSET_HALO4_PF_COOP_PLAYER_LIMIT": 6, "OFFSET_HALO4_PF_COOP_REJOIN": 2, "OFFSET_HALO4_VIEWMODEL_ASPECT_DIVIDE": 4,