From c5d8f4afb732f82ce2d7bbef144a4fe72932f90d Mon Sep 17 00:00:00 2001 From: John P <7864917+kirklandsig@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:08:21 -0400 Subject: [PATCH 1/3] feat(splitscreen): the Players window and hot join The Players window: every controller presses A to join as the next player, B to leave and Y to pick Spartan or Elite. Hot join (experimental) lets players join in the middle of a mission: a controller nobody plays with presses A. Halo 3, ODST, Reach and Halo 4 sign the player in; Halo CE and Halo 2 make their players as a mission loads, so their modules (halo1/hotjoin.cpp, halo2/hotjoin.cpp) start a hot-join mission with all four and hold back the ones who haven't joined. Nine new offsets with byte patterns (gen_patterns.py) for those modules. On the shared menu state (mcc/settings/MenuSettings): a join or a leave changes the players and their controllers there, as the Splitscreen window's player count does, so both menus show it, a game start applies it, and a menu saving writes it. Every game start keeps split screen on while hot join is chosen (ApplySplitscreen). In a hot-join Halo CE or Halo 2 game the player count changes only by joining and leaving: the dashboard's count and the Splitscreen window's -/+ are held. The rest, as in my fork (kirklandsig/AlphaRing v2.0): Halo 2 checkpoints stay safe with players held back, and a player who joined comes back beside a teammate after a checkpoint revert in Halo CE and Halo 2; a join waits for the next tick; the press a player joined with doesn't reach the game; the joined count is published with a release store and read with acquire loads; Bind in the Splitscreen window has the controllers while its player's page waits, and a press either side acted on isn't taken by the other. Co-Authored-By: Claude Opus 5.5 --- lib/game/inc/1.3528.0.0/offset_halo1.h | 11 + lib/game/inc/1.3528.0.0/offset_halo2.h | 1 + lib/game/inc/1.3528.0.0/patterns_halo1.inc | 27 ++ lib/game/inc/1.3528.0.0/patterns_halo2.inc | 3 + src/mcc/CGameManager.cpp | 2 + src/mcc/CGameManager.h | 5 +- src/mcc/CGameManagerSplitscreen.cpp | 23 +- src/mcc/module/entry/halo1/graphics.cpp | 1 + src/mcc/module/entry/halo1/halo1.h | 4 + src/mcc/module/entry/halo1/hotjoin.cpp | 260 +++++++++++++++ src/mcc/module/entry/halo2/graphics.cpp | 1 + src/mcc/module/entry/halo2/halo2.h | 16 +- src/mcc/module/entry/halo2/hotjoin.cpp | 120 +++++++ src/mcc/module/entry/halo2/players.cpp | 11 +- src/mcc/settings/MenuSettings.cpp | 4 +- src/mcc/splitscreen/Join.cpp | 334 +++++++++++++++++++ src/mcc/splitscreen/Splitscreen.cpp | 104 +++++- src/mcc/splitscreen/Splitscreen.h | 55 ++- src/render/imgui/ImGui.cpp | 2 + src/render/imgui/game/xbox/CStateMachine.cpp | 7 +- 20 files changed, 971 insertions(+), 20 deletions(-) create mode 100644 src/mcc/module/entry/halo1/hotjoin.cpp create mode 100644 src/mcc/module/entry/halo2/hotjoin.cpp create mode 100644 src/mcc/splitscreen/Join.cpp diff --git a/lib/game/inc/1.3528.0.0/offset_halo1.h b/lib/game/inc/1.3528.0.0/offset_halo1.h index bf799f1e..0150ad5b 100644 --- a/lib/game/inc/1.3528.0.0/offset_halo1.h +++ b/lib/game/inc/1.3528.0.0/offset_halo1.h @@ -24,6 +24,17 @@ DefOffset(OFFSET_HALO1_PF_OBJECT_TRY_AND_GET, 0xB389A4) // void* (int object, un DefOffset(OFFSET_HALO1_PF_ACTOR_CUSTOMIZE_UNIT, 0xC01DD0) // (int actor_variant, int unit) - weapon, grenades, colors DefOffset(OFFSET_HALO1_PF_AI_ATTACH_FREE, 0xBFF120) // (int unit, int actor_variant) - creates an encounterless actor +// hot join (module/entry/halo1/hotjoin.cpp) +DefOffset(OFFSET_HALO1_PF_PLAYERS_UPDATE, 0xAD0720) // void (state*) - each tick: the players' respawns (calls the below) +DefOffset(OFFSET_HALO1_PF_PLAYER_SPAWN, 0xAD4184) // void (int player) - a start location or the co-op respawn; callers check the unit +DefOffset(OFFSET_HALO1_PF_CHOOSE_START_LOCATION, 0xAD39AC) // int16 (int player) - the best free starting location, -1: none +DefOffset(OFFSET_HALO1_PF_START_LOCATION, 0xAD3940) // entry* (int16 index) - a starting location, 0x34: point, facing +0xC +DefOffset(OFFSET_HALO1_PF_OBJECT_DELETE, 0xC579D4) // void (int object) - object_destroy's; skips a player's unit +DefOffset(OFFSET_HALO1_PF_PLAYER_SET_UNIT, 0xAD2404) // void (int player, int unit) - -1 takes the player out of theirs +DefOffset(OFFSET_HALO1_PF_OBJECT_SET_POSITION, 0xB359E8) // void (int object, point3*, vector3* forward, vector3* up) - null keeps +DefOffset(OFFSET_HALO1_PF_OBJECT_TELEPORTED, 0xBA83EC) // void (int object) - what object_teleport resets after the move +DefOffset(OFFSET_HALO1_PF_PLAYER_MOVE_BESIDE, 0xAD2FBC) // bool (int player, int other, void*) - moves a just-spawned player's unit beside another player (the co-op respawn, players_update 0xAD058F); true when it did + // Left/Right split screen (mcc/splitscreen/LeftRight) DefOffset(OFFSET_HALO1_PF_SPLIT_GRID, 0xAC4108) // void (int views, int* columns, int* rows) - the window grid, rows first DefOffset(OFFSET_HALO1_PF_SPLIT_WINDOW, 0xAC4154) // void (int view, int views, short rect[4], short copy[4]) - a view's window diff --git a/lib/game/inc/1.3528.0.0/offset_halo2.h b/lib/game/inc/1.3528.0.0/offset_halo2.h index 21e66175..fd89af39 100644 --- a/lib/game/inc/1.3528.0.0/offset_halo2.h +++ b/lib/game/inc/1.3528.0.0/offset_halo2.h @@ -30,6 +30,7 @@ DefOffset(OFFSET_HALO2_PF_PLAYERS_UPDATE, 0x6A3910) DefOffset(OFFSET_HALO2_PF_COOP_CAMPAIGN, 0x6A6320) // bool () - a co-op campaign: session [0xE80A78] mode +8 == 1, byte +0x2C8 DefOffset(OFFSET_HALO2_PF_COOP_RESPAWN_ALLOWED, 0x6A5D40) // bool () - not with the Iron skull (11), not on Legendary DefOffset(OFFSET_HALO2_PV_SCENARIO, 0xE6F768) // the loaded scenario*: int starting locations +0x100 +DefOffset(OFFSET_HALO2_PF_PLAYER_SPAWN, 0x69E580) // bool (int player) - a new unit at the best starting location (hot join) // spawning (mcc/spawn) DefOffset(OFFSET_HALO2_PV_TAG_INSTANCES, 0x15E4B30) // {u32 group, u32 datum, u32 address, u32 size}[tag count] diff --git a/lib/game/inc/1.3528.0.0/patterns_halo1.inc b/lib/game/inc/1.3528.0.0/patterns_halo1.inc index bed06e32..808f0578 100644 --- a/lib/game/inc/1.3528.0.0/patterns_halo1.inc +++ b/lib/game/inc/1.3528.0.0/patterns_halo1.inc @@ -48,6 +48,33 @@ const OffsetPatterns kHalo1Patterns[] = { Rel("E8 ?? ?? ?? ?? 8B 06 B9 02 00 00 00 44 88 7C 24 58 45 8B CE", 0x1, 0x5, 0x0)}}, {&OFFSET_HALO1_PF_AI_ATTACH_FREE, 0xbff120, Code, { At("48 83 EC 68 48 8B 05 ?? ?? ?? ?? 41 BB 02 00 00 00 44 8B C2", 0x0)}}, + {&OFFSET_HALO1_PF_PLAYERS_UPDATE, 0xad0720, Code, { + At("48 8B C4 48 89 58 10 48 89 70 18 48 89 78 20 55 41 54 41 55 41 56 41 57 48 8D A8 D8 FB FF FF", 0x0), + Rel("48 8D 4C 24 30 E8 ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 38 58 03 74 ?? F3 0F 10 05", 0x6, 0xa, 0x0)}}, + {&OFFSET_HALO1_PF_PLAYER_SPAWN, 0xad4184, Code, { + At("48 8B C4 48 89 58 10 48 89 70 18 48 89 78 20 55 41 54 41 55 41 56 41 57 48 8D 68 A1 48 81 EC 00 01 00 00 0F 29 70 C8 48 8B 05", 0x0), + Rel("E8 ?? ?? ?? ?? 39 6F 64 74 ?? 8B 4C 24 2C 41 0F B7 D6 4C 8D 04 52", 0x1, 0x5, 0x0)}}, + {&OFFSET_HALO1_PF_CHOOSE_START_LOCATION, 0xad39ac, Code, { + At("48 8B C4 48 89 58 08 48 89 68 10 48 89 70 18 57 48 83 EC 50 0F 29 70 E8 83 CF FF", 0x0), + Rel("E8 ?? ?? ?? ?? 45 33 DB 66 41 3B C5 0F 84 ?? ?? ?? ?? 48 8B 35 ?? ?? ?? ?? 41 8B CB", 0x1, 0x5, 0x0)}}, + {&OFFSET_HALO1_PF_START_LOCATION, 0xad3940, Code, { + At("4C 8B 05 ?? ?? ?? ?? 33 D2 66 85 C9 78 ?? 0F BF C1 41 3B 80 54 03 00 00", 0x0), + Rel("E8 ?? ?? ?? ?? 48 8B F8 66 39 68 10 0F 87 ?? ?? ?? ?? 4C 8D 40 14 8B CD", 0x1, 0x5, 0x0)}}, + {&OFFSET_HALO1_PF_OBJECT_DELETE, 0xc579d4, Code, { + At("83 F9 FF 74 1C 53 48 83 EC 20 8B D9 E8 ?? ?? ?? ?? 84 C0 75", 0x0), + Rel("48 0F BF C9 8B 0C 88 EB ?? 8B CA 3B CA 74 ?? E8 ?? ?? ?? ?? 48 83 C4 28", 0x10, 0x14, 0x0)}}, + {&OFFSET_HALO1_PF_PLAYER_SET_UNIT, 0xad2404, Code, { + At("48 89 6C 24 08 48 89 74 24 10 48 89 7C 24 18 41 54", 0x0), + Rel("E8 ?? ?? ?? ?? 8B 4D C8 89 4B 4C 8A 4D C8 88 8B CE 00 00 00", 0x1, 0x5, 0x0)}}, + {&OFFSET_HALO1_PF_OBJECT_SET_POSITION, 0xb359e8, Code, { + At("48 8B C4 48 89 58 08 48 89 68 10 48 89 70 18 48 89 78 20 41 55 41 56 41 57 48 83 EC 60 4C 8B 3D", 0x0), + Rel("E8 ?? ?? ?? ?? B8 D4 01 00 00 49 8D 9F 08 02 00 00 48 03 DF", 0x1, 0x5, 0x0)}}, + {&OFFSET_HALO1_PF_OBJECT_TELEPORTED, 0xba83ec, Code, { + At("83 F9 FF 74 1F 80 3D ?? ?? ?? ?? 00 74 ?? 48 8B 05 ?? ?? ?? ?? 0F B7 C9 48 69 D1 4C 0D 00 00", 0x0), + Rel("8B CB E8 ?? ?? ?? ?? 33 D2 8B CF E8 ?? ?? ?? ?? 48 8B 5C 24 30 48 83 C4 20", 0x3, 0x7, 0x0)}}, + {&OFFSET_HALO1_PF_PLAYER_MOVE_BESIDE, 0xad2fbc, Code, { + At("48 89 5C 24 08 48 89 6C 24 10 48 89 74 24 18 57 48 83 EC 20 48 8B 05 ?? ?? ?? ?? 8B EA", 0x0), + Rel("49 81 C0 90 00 00 00 4D 03 C1 44 3B CD 4C 0F 44 C0 E8 ?? ?? ?? ??", 0x12, 0x16, 0x0)}}, {&OFFSET_HALO1_PF_SPLIT_GRID, 0xac4108, Code, { At("48 89 5C 24 08 BB 01 00 00 00 4D 8B D8 44 8B CB", 0x0), Rel("E8 ?? ?? ?? ?? 44 8B 5C 24 58 41 8B C3 0F AF 44 24 50 3B C3", 0x1, 0x5, 0x0)}}, diff --git a/lib/game/inc/1.3528.0.0/patterns_halo2.inc b/lib/game/inc/1.3528.0.0/patterns_halo2.inc index b8c873bc..4adcbdb3 100644 --- a/lib/game/inc/1.3528.0.0/patterns_halo2.inc +++ b/lib/game/inc/1.3528.0.0/patterns_halo2.inc @@ -47,6 +47,9 @@ const OffsetPatterns kHalo2Patterns[] = { {&OFFSET_HALO2_PV_SCENARIO, 0xe6f768, Data, { Rel("48 8B 05 ?? ?? ?? ?? 8B DE 48 89 7C 24 68 39 98 10 02 00 00", 0x3, 0x7, 0x0), Rel("48 8B 05 ?? ?? ?? ?? 33 D2 4C 63 80 6C 01 00 00 41 83 F8 FF", 0x3, 0x7, 0x0)}}, + {&OFFSET_HALO2_PF_PLAYER_SPAWN, 0x69e580, Code, { + At("40 55 53 56 57 41 54 41 56 41 57 48 8D AC 24 60 EF FF FF", 0x0), + Rel("E8 ?? ?? ?? ?? B0 01 EB ?? 32 C0 48 8B 5C 24 30 48 8B 74 24 38 48 8B 7C 24 40", 0x1, 0x5, 0x0)}}, {&OFFSET_HALO2_PV_TAG_INSTANCES, 0x15e4b30, Data, { Rel("48 8B 05 ?? ?? ?? ?? 48 03 C9 4C 89 7C 24 50 4C 63 74 C8 08", 0x3, 0x7, 0x0), Rel("66 90 0F B7 C8 48 8B 05 ?? ?? ?? ?? 48 03 C9 48 63 5C C8 08", 0x8, 0xc, 0x0)}}, diff --git a/src/mcc/CGameManager.cpp b/src/mcc/CGameManager.cpp index 5a691106..6b707dcd 100644 --- a/src/mcc/CGameManager.cpp +++ b/src/mcc/CGameManager.cpp @@ -4,6 +4,7 @@ #include "mcc/mcc.h" #include "mcc/settings/MenuSettings.h" #include "log/MatchLog.h" +#include "mcc/splitscreen/Splitscreen.h" #include #include @@ -164,6 +165,7 @@ void CGameManager::apply_profiles() { if (profile) { memcpy(&profile->profile, base_profile, sizeof(CUserProfile)); memcpy(&profile->mapping, base_mapping, sizeof(CGamepadMapping)); + MCC::Splitscreen::ApplySpecies(i, profile->profile); } } } diff --git a/src/mcc/CGameManager.h b/src/mcc/CGameManager.h index c96fcff1..ab058997 100644 --- a/src/mcc/CGameManager.h +++ b/src/mcc/CGameManager.h @@ -41,8 +41,11 @@ static void apply_profiles(); // Increments every time a map starts loading. static unsigned load_generation(); - // Local players in the game right now (see CGameManagerSplitscreen.cpp: Halo 4 starts with two). + // Local players in the game right now (see CGameManagerSplitscreen.cpp: Halo 4 starts with two; a hot-join + // Halo CE or Halo 2 mission is told of all four and holds back the others, Splitscreen::HotJoinSlots). static int active_player_count(); + // The current map runs: from its Running state until a map loads or the session exits. + static bool running(); private: static void track_state(eState state); diff --git a/src/mcc/CGameManagerSplitscreen.cpp b/src/mcc/CGameManagerSplitscreen.cpp index 3322c453..e9f8349e 100644 --- a/src/mcc/CGameManagerSplitscreen.cpp +++ b/src/mcc/CGameManagerSplitscreen.cpp @@ -12,6 +12,7 @@ using AlphaRing::MatchLog::Describe; using AlphaRing::MatchLog::FirstThisMatch; #include "input/MenuConfig.h" #include "mcc/spawn/Spawn.h" +#include "mcc/splitscreen/Splitscreen.h" #include @@ -43,14 +44,19 @@ static constexpr ULONGLONG kDeferredJoinDelayMs = 3000; static std::atomic s_running_since; // 0 until the current session first runs static std::atomic s_load_generation; +static std::atomic s_map_running; // from the Running state until a map loads or the session exits unsigned CGameManager::load_generation() { return s_load_generation; } +bool CGameManager::running() { return s_map_running; } + void CGameManager::track_state(eState state) { if (state == Loading) ++s_load_generation; if (state == Running && !s_running_since) // also re-sent after level transitions s_running_since = GetTickCount64(); + if (state == Running || state == Loading || state == Exit || state == Exiting) + s_map_running = state == Running; } // MCC ends every session with a restart after the exit states. The clock is cleared there, @@ -58,6 +64,7 @@ void CGameManager::track_state(eState state) { // it, and the next session queries its players before its own loading state. void CGameManager::end_session() { s_running_since = 0; + s_map_running = false; } static int deferred_player_count(int count) { @@ -78,7 +85,7 @@ static int deferred_player_count(int count) { int CGameManager::active_player_count() { static std::atomic s_last_logged = -1; - int active = deferred_player_count(AlphaRing::Global::MCC::Splitscreen()->player_count); + int active = deferred_player_count(MCC::Splitscreen::PlayerCount()); if (active != s_last_logged) { s_last_logged = active; @@ -100,7 +107,8 @@ static bool get_xbox_user_id_inner(CGameManager *self, __int64 *pId, wchar_t *pN return CGameManager::ppOriginal.get_xbox_user_id(self, pId, pName, size, index); } - if (index >= CGameManager::active_player_count()) { + // MCC counts the local players it asks for here (all four slots in a Halo CE or Halo 2 hot-join mission) + if (index >= MCC::Splitscreen::HotJoinSlots(CGameManager::active_player_count())) { *path = "beyond player_count"; return false; } @@ -212,9 +220,11 @@ bool CGameManager::get_key_state(CGameManager *self, DWORD index, input_data_t * MATCH_LOG("input player={} first press buttons={:#06x} (input_user={})", index, p_device->state.Gamepad.wButtons, p_device->input_user); - // While the player's spawn menu is open it has the pad, which the game sees released; after it closes, what - // was held then is taken out of the pad until released. - if (MCC::Spawn::HandlePlayerInput(index, p_device->state.Gamepad)) + // Until what a player who just joined pressed is let go (Players window, hot join), and while the player's + // spawn menu is open, which then has the pad, the game sees it released; after a menu closes, what was held + // then is taken out of the pad until released. + if (MCC::Splitscreen::JoinPressHeld(p_device->input_user, p_device->state.Gamepad) || + MCC::Spawn::HandlePlayerInput(index, p_device->state.Gamepad)) memset(&p_device->state.Gamepad, 0, sizeof(p_device->state.Gamepad)); } @@ -265,7 +275,7 @@ CUserProfile* CGameManager::get_player_profile(CGameManager *self, __int64 xid) // Reach initializes armor into the player-0 profile after game_setup fires, so the // snapshot taken by apply_profiles() is stale. Sync armor from the live player-0 // profile on every call while preserving the per-player custom colors and look - // settings we applied. + // settings we applied, and a species chosen in the Players window. auto* gg = GameGlobal(); if (gg && gg->current_game == CGameGlobal::HaloReach) { auto* base = ppOriginal.get_player_profile(self, get_xuid(0)); @@ -293,6 +303,7 @@ CUserProfile* CGameManager::get_player_profile(CGameManager *self, __int64 xid) dest->profile.HorizontalLookSensitivity = horizSens; // and the armor picked for this player in the advanced menu AlphaRing::MenuSettings::ApplyArmor(get_index(xid), dest->profile); + MCC::Splitscreen::ApplySpecies(index, dest->profile); } } diff --git a/src/mcc/module/entry/halo1/graphics.cpp b/src/mcc/module/entry/halo1/graphics.cpp index ae0eab3d..bce17bbc 100644 --- a/src/mcc/module/entry/halo1/graphics.cpp +++ b/src/mcc/module/entry/halo1/graphics.cpp @@ -6,6 +6,7 @@ namespace Halo1::Entry::Graphics { Halo1Entry(entry, OFFSET_HALO1_PF_GAME_START, __int64, detour, void* self, void* manager, unsigned char* game_options, void* a4) { MCC::Splitscreen::ClassicGraphicsScope classic(game_options, CGameGlobal::Halo1); + HotJoin::Start(MCC::Splitscreen::HotJoinOn()); return ((detour_t)entry.m_pOriginal)(self, manager, game_options, a4); } } diff --git a/src/mcc/module/entry/halo1/halo1.h b/src/mcc/module/entry/halo1/halo1.h index c87074f5..6bcf3723 100644 --- a/src/mcc/module/entry/halo1/halo1.h +++ b/src/mcc/module/entry/halo1/halo1.h @@ -22,4 +22,8 @@ namespace Halo1::Entry { namespace Anniversary { bool Quad(__int64 module); // 3-4 players drawn in quarters, Anniversary graphics (anniversary.cpp) } + + namespace HotJoin { + void Start(bool on); // as a mission starts (graphics.cpp): whether it's a hot-join one (hotjoin.cpp) + } } diff --git a/src/mcc/module/entry/halo1/hotjoin.cpp b/src/mcc/module/entry/halo1/hotjoin.cpp new file mode 100644 index 00000000..3b96f334 --- /dev/null +++ b/src/mcc/module/entry/halo1/hotjoin.cpp @@ -0,0 +1,260 @@ +// Halo CE hot join (mcc/splitscreen/Join.cpp): Halo CE only makes its local players as a mission loads, so a hot-join +// mission starts with all four (MCC is told of four, CGameManagerSplitscreen.cpp) and the game's local player count - +// which its views, HUD, input and cameras follow, live - is held at the player count; the others don't spawn and have +// no body. Halo CE spawns players only at the mission's starting places, which a player joining later could be far +// behind or find taken (the built-in missions have two, where the others may still stand), so a player who joins +// comes in on a spot a teammate walked through lately - or, while there's none, at a starting place and is moved to +// one as soon as there is. +#include "halo1.h" + +#include "global/Global.h" +#include "mcc/splitscreen/Splitscreen.h" + +#include +#include +#include +#include + +namespace Halo1::Entry::HotJoin { + EntryFeature("Halo CE hot join", OFFSET_HALO1_PF_GAME_START, OFFSET_HALO1_PV_LOCAL_PLAYERS, + OFFSET_HALO1_PV_PLAYER_COUNT, OFFSET_HALO1_PF_OBJECT_DELETE, OFFSET_HALO1_PF_PLAYER_SET_UNIT, + OFFSET_HALO1_PF_OBJECT_GET_ORIGIN, OFFSET_HALO1_PF_OBJECT_SET_POSITION, + OFFSET_HALO1_PF_OBJECT_TELEPORTED, OFFSET_HALO1_PV_SABER_SPLIT, OFFSET_HALO1_PV_SABER_SPLIT_STATE, + OFFSET_HALO1_PF_SABER_SET_SPLIT); + + constexpr int kSlots = 4; + + std::atomic s_active = false; // the running mission is a hot-join one + int s_slots = 0; // local players it was started with (read on its first update) + + // Where each player walked lately on foot: every half second (the game ticks 30 times a second), a spot a step + // from the one before at about the same height - not driving, jumping or falling - and a stride from the last one + // kept, so a player standing still keeps where they walked before. And for how many more ticks a player is to be + // brought beside a teammate once they have a body: set while they're out of the mission, so it's there when they + // join. + struct Point { + float x, y, z; + }; + struct Spawn { // a starting place's first 0x10 bytes + Point at; + float facing; + }; + constexpr int kTrail = 8, kTrailTicks = 15, kPlaceTicks = 300; + constexpr float kStep = 2.0f, kRise = 0.25f; // world units in half a second + constexpr float kStride = 0.8f; // between the spots kept + constexpr float kClear = 0.6f; // from every player now (a Spartan is about 0.2 across) + constexpr float kNear = 10.0f; // from the teammate now: spots left behind by a teleport, a + // checkpoint or a drive aren't used + Point s_trail[kSlots][kTrail]; // newest first + int s_trail_count[kSlots] = {}; + Point s_sampled[kSlots]; // the last half-second sample, on foot or not + bool s_has_sample[kSlots] = {}; + int s_place[kSlots] = {}; + int s_tick = 0; + // Held back in this mission at some point, until the next one starts. Players and their globals are game state + // (allocated through 0xACD75C), so going back to a checkpoint from before such a player joined brings back their + // record without a unit, to spawn at a starting place (0xAD0B63 -> 0xAD4184 -> 0xAD39AC): whenever they have no + // unit they're to be brought beside a teammate again. + bool s_held_back[kSlots] = {}; + + // players globals: int16 local player count +0xB4, player handle[4] +0xB8, unit[4] +0xC8 + char* Globals(__int64 module) { return *(char**)(module + OFFSET_HALO1_PV_LOCAL_PLAYERS); } + int Handle(char* globals, int local) { return *(int*)(globals + 0xB8 + 4 * local); } + int Unit(char* globals, int local) { return *(int*)(globals + 0xC8 + 4 * local); } + + int Joined() { return std::clamp(MCC::Splitscreen::PlayerCount(), 1, kSlots); } + + int LocalIndex(char* globals, int player) { + for (int j = 0; j < kSlots; ++j) + if (Handle(globals, j) == player) return j; + return -1; + } + + Point Origin(__int64 module, int object) { + Point at{}; + ((void (*)(int, Point*))(module + OFFSET_HALO1_PF_OBJECT_GET_ORIGIN))(object, &at); + return at; + } + + float Distance2(Point a, Point b) { + float dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z; + return dx * dx + dy * dy + dz * dz; + } + + void Sample(__int64 module, int local, int unit) { + Point at = Origin(module, unit), before = s_sampled[local]; + bool on_foot = s_has_sample[local] && std::abs(at.z - before.z) <= kRise && + Distance2({at.x, at.y, 0}, {before.x, before.y, 0}) <= kStep * kStep; + s_sampled[local] = at; + s_has_sample[local] = true; + if (!on_foot || (s_trail_count[local] > 0 && Distance2(at, s_trail[local][0]) < kStride * kStride)) return; + std::copy_backward(s_trail[local], s_trail[local] + kTrail - 1, s_trail[local] + kTrail); + s_trail[local][0] = at; + s_trail_count[local] = (std::min)(s_trail_count[local] + 1, kTrail); + } + + void Forget(int local) { + s_trail_count[local] = 0; + s_has_sample[local] = false; + } + + // The newest spot a teammate walked through, near where they are now, that no player is on now, and which way to + // face there (toward that teammate); false while there's none (nobody has walked anywhere near yet). + bool Spot(__int64 module, char* globals, int joiner, int joined, Spawn* spawn) { + Point now[kSlots]; + bool here[kSlots] = {}; + for (int k = 0; k < joined; ++k) + if (k != joiner && Unit(globals, k) != -1) { + here[k] = true; + now[k] = Origin(module, Unit(globals, k)); + } + for (int t = 0; t < joined; ++t) { + if (!here[t]) continue; + for (int n = 0; n < s_trail_count[t]; ++n) { + Point at = s_trail[t][n]; + if (Distance2(at, now[t]) > kNear * kNear) continue; + bool clear = true; + for (int k = 0; k < joined && clear; ++k) clear = !here[k] || Distance2(at, now[k]) >= kClear * kClear; + if (!clear) continue; + *spawn = {at, std::atan2(now[t].y - at.y, now[t].x - at.x)}; + return true; + } + } + return false; + } + + // A joiner who spawned at a starting place, moved to a teammate's spot. + bool Place(__int64 module, char* globals, int joiner, int joined) { + Spawn spawn; + if (!Spot(module, globals, joiner, joined, &spawn)) return false; + int unit = Unit(globals, joiner); + ((void (*)(int, Point*, void*, void*))(module + OFFSET_HALO1_PF_OBJECT_SET_POSITION))(unit, &spawn.at, nullptr, + nullptr); + ((void (*)(int))(module + OFFSET_HALO1_PF_OBJECT_TELEPORTED))(unit); + return true; + } + + // Halo CE sets its Anniversary renderer's split screen only while the map loads, from the local player count + // (0x6762A) - four in a hot-join mission, which left one player split in two. It follows the players in the + // mission instead, applied as the game applies it: set_split adds or drops the second camera and lays the views + // out (halo1/anniversary.cpp's detour of it makes and drops the cameras for players 3 and 4). + void FollowSplit(__int64 module, int joined) { + char* state = *(char**)(module + OFFSET_HALO1_PV_SABER_SPLIT_STATE); + if (state == nullptr) return; + bool split = joined > 1; + *(bool*)(module + OFFSET_HALO1_PV_SABER_SPLIT) = split; + if ((state[0x41] != 0) != split) ((void (*)(char*))(module + OFFSET_HALO1_PF_SABER_SET_SPLIT))(state); + } + + // Each tick: the bodies of the players who aren't in the mission, the game's local player count, and bringing the + // players who joined to the others. + void Update(__int64 module) { + char* globals = Globals(module); + if (globals == nullptr) return; + if (s_slots == 0) s_slots = std::clamp((int)*(short*)(globals + 0xB4), 1, kSlots); + int joined = (std::min)(Joined(), s_slots); + for (int j = joined; j < s_slots; ++j) { + s_held_back[j] = true; + s_place[j] = kPlaceTicks; + Forget(j); + int unit = Unit(globals, j); + if (unit == -1) continue; + // the game won't delete a player's unit: the player leaves it first + ((void (*)(int, int))(module + OFFSET_HALO1_PF_PLAYER_SET_UNIT))(Handle(globals, j), -1); + ((void (*)(int))(module + OFFSET_HALO1_PF_OBJECT_DELETE))(unit); + } + *(short*)(globals + 0xB4) = (short)joined; + *(short*)(module + OFFSET_HALO1_PV_PLAYER_COUNT) = (short)joined; + FollowSplit(module, joined); + + bool sample = ++s_tick % kTrailTicks == 0; + for (int j = 0; j < joined; ++j) { + int unit = Unit(globals, j); + if (unit == -1) { + Forget(j); // the next body starts a new trail + if (s_held_back[j]) s_place[j] = kPlaceTicks; + } else if (s_place[j] > 0) { + if (Place(module, globals, j, joined) || --s_place[j] == 0) s_place[j] = 0; + } else if (sample) { + Sample(module, j, unit); + } + } + } + + Halo1Entry(entry_update, OFFSET_HALO1_PF_PLAYERS_UPDATE, void, players_update, void* state) { + if (s_active) Update(entry_update.m_target - entry_update.m_offset); + ((players_update_t)entry_update.m_pOriginal)(state); + } + + // A joiner's first body at a teammate's spot: while they spawn, the starting place the game picks is that spot + // (the game thread's). + thread_local const Spawn* t_spawn = nullptr; + thread_local bool t_spot_taken = false; // the spawn going on took that spot + char s_start[0x34]; + + Halo1Entry(entry_choose_start, OFFSET_HALO1_PF_CHOOSE_START_LOCATION, short, choose_start, int player) { + return t_spawn ? 0 : ((choose_start_t)entry_choose_start.m_pOriginal)(player); + } + + Halo1Entry(entry_start, OFFSET_HALO1_PF_START_LOCATION, char*, start_location, short index) { + char* entry = ((start_location_t)entry_start.m_pOriginal)(index); + if (!t_spawn || entry == nullptr) return entry; + memcpy(s_start, entry, sizeof s_start); + memcpy(s_start, t_spawn, sizeof *t_spawn); + t_spot_taken = true; + return s_start; + } + + // The co-op respawn (players_update, 0xAD0546) spawns a waiting player - at a starting place, through the two + // above - and then moves them beside a teammate, true when it could: moved, they're where Place would bring them. + Halo1Entry(entry_move_beside, OFFSET_HALO1_PF_PLAYER_MOVE_BESIDE, bool, move_beside, int player, int other, + void* where) { + bool moved = ((move_beside_t)entry_move_beside.m_pOriginal)(player, other, where); + if (moved && s_active) { + char* globals = Globals(entry_move_beside.m_target - entry_move_beside.m_offset); + int local = globals ? LocalIndex(globals, player) : -1; + if (local >= 0) s_place[local] = 0; + } + return moved; + } + + // Players who aren't in the mission don't spawn, and one who joined comes in at a teammate's spot if there is one. + Halo1Entry(entry_spawn, OFFSET_HALO1_PF_PLAYER_SPAWN, void, player_spawn, int player) { + auto original = (player_spawn_t)entry_spawn.m_pOriginal; + if (!s_active) return original(player); + __int64 module = entry_spawn.m_target - entry_spawn.m_offset; + char* globals = Globals(module); + int local = globals ? LocalIndex(globals, player) : -1; + int joined = Joined(); + if (local >= joined) return; + Spawn spawn; + bool at_spot = local >= 0 && s_place[local] > 0 && Spot(module, globals, local, joined, &spawn); + t_spawn = at_spot ? &spawn : nullptr; + t_spot_taken = false; + original(player); + t_spawn = nullptr; + if (t_spot_taken && Unit(globals, local) != -1) s_place[local] = 0; // where Place would have moved them + } + + bool Available() { return entry_feature->Available(); } + + void Reset() { + s_active = false; + s_slots = s_tick = 0; + for (int j = 0; j < kSlots; ++j) { + s_place[j] = 0; + s_held_back[j] = false; + Forget(j); + } + } + const bool s_reset = (Halo1EntrySet()->on_remove(&Reset), true); + + // As a mission starts (halo1/graphics.cpp). + void Start(bool on) { + Reset(); + s_active = on && Available(); + if (s_active) LOG_INFO("Halo CE hot join: this mission holds back the players who haven't joined"); + } + + bool Active() { return s_active; } +} diff --git a/src/mcc/module/entry/halo2/graphics.cpp b/src/mcc/module/entry/halo2/graphics.cpp index f668ab85..a8830ed4 100644 --- a/src/mcc/module/entry/halo2/graphics.cpp +++ b/src/mcc/module/entry/halo2/graphics.cpp @@ -6,6 +6,7 @@ namespace Halo2::Entry::Graphics { Halo2Entry(entry, OFFSET_HALO2_PF_COPY_GAME_OPTIONS, __int64, detour, unsigned char* game_options, void* a2, void* a3, void* a4) { MCC::Splitscreen::ClassicGraphicsScope classic(game_options, CGameGlobal::Halo2); + HotJoin::Start(MCC::Splitscreen::HotJoinOn()); return ((detour_t)entry.m_pOriginal)(game_options, a2, a3, a4); } } diff --git a/src/mcc/module/entry/halo2/halo2.h b/src/mcc/module/entry/halo2/halo2.h index d49d1d38..851af127 100644 --- a/src/mcc/module/entry/halo2/halo2.h +++ b/src/mcc/module/entry/halo2/halo2.h @@ -16,10 +16,24 @@ inline EntrySet* Halo2EntrySet() {return &g_pHalo2EntrySet;}; namespace Halo2::Entry { namespace Players { - int Handle(__int64 module, int local); // local player `local`'s player handle, -1: none (players.cpp) + const ::AlphaRing::Feature& Hooks(); // its hook and offsets, which hot join requires (players.cpp) + int Handle(__int64 module, int local); // local player `local`'s player handle, -1: none char* Player(__int64 module, int handle); // that player's data, or null } + namespace HotJoin { + void Start(bool on); // as a mission starts (graphics.cpp): whether it's a hot-join one (hotjoin.cpp) + // Each tick (players.cpp), before the game's: in a hot-join mission, the game's local player count held at the + // players who joined, and that number; 4 otherwise. + int Hold(__int64 module); + // Local player `local` was held back in this hot-join mission at some point (with the players who haven't + // joined; past the count, the caller tells them apart). + bool JoinedLate(int local); + // After the game's tick: in a hot-join mission, "every player is dead" and "a player is", over the ones who + // joined. + void Account(__int64 module); + } + namespace Splitscreen { bool ClassicShown(__int64 module); // Classic graphics are on screen (Back switches them in a mission) } diff --git a/src/mcc/module/entry/halo2/hotjoin.cpp b/src/mcc/module/entry/halo2/hotjoin.cpp new file mode 100644 index 00000000..ee2c9eba --- /dev/null +++ b/src/mcc/module/entry/halo2/hotjoin.cpp @@ -0,0 +1,120 @@ +// Halo 2 hot join (mcc/splitscreen/Join.cpp), as Halo CE's (halo1/hotjoin.cpp): Halo 2 only makes its local players as +// a mission loads, so a hot-join mission starts with all four (MCC is told of four, CGameManagerSplitscreen.cpp) and +// the game's local player count - which its views, HUD and input follow - is held at the player count; the others +// don't spawn. A player who joins comes in beside a teammate through the co-op respawn (players.cpp), where the game +// gives co-op respawns (not on Legendary or with the Iron skull: there, at a starting place). +// +// The players held back keep waiting for a starting place, as players do before their first spawn: a player without a +// unit who isn't waiting for one counts as a dead teammate (0x6A16F0 sets players globals +5), which keeps every +// checkpoint from being saved (game_safe_to_save, 0x7005C0, at 0x70067E). Waiting so, though, they count as alive in +// the game's "every player is dead" (+4), so that's worked out over the players who joined (Account). +#include "halo2.h" + +#include "global/Global.h" +#include "mcc/splitscreen/Splitscreen.h" + +#include +#include +#include + +namespace Halo2::Entry::HotJoin { + // players.cpp holds the count each tick, from its players_update hook + EntryFeature("Halo 2 hot join", OFFSET_HALO2_PF_COPY_GAME_OPTIONS, OFFSET_HALO2_PV_RESPAWN); + const bool s_requires_players = (entry_feature->Requires(Players::Hooks()), true); + + constexpr int kSlots = 4; + + std::atomic s_active = false; // the running mission is a hot-join one + // Held back in it at some point, until the next mission starts (game thread). A checkpoint saved before such a + // player joined brings back, as it's restored, their record waiting for a starting place without a unit (players + // and their globals are game state, allocated through 0x15A0AF0's allocator): they're still sent to the co-op + // respawn, as whenever they have no unit (players.cpp). + bool s_held_back[kSlots] = {}; + // The players it lets into the mission, taken once a tick, as players_update starts (Hold, game thread): one who + // joins in the middle of a tick waits for the next, so that their record is sent to the co-op respawn before they + // can spawn - spawning first, they would take the mission's starting place (0x6A3B93). + std::atomic s_admitted = kSlots; + + // players globals: int16 local player count +8, player handle[4] +0xC (both kept by 0x69E4A0, which maps a player + // to a local index; Players::Handle reads the handles), bytes +4 every player dead and +5 one is (0x6A16F0) + char* Globals(__int64 module) { return *(char**)(module + OFFSET_HALO2_PV_RESPAWN); } + + int Joined() { return std::clamp(MCC::Splitscreen::PlayerCount(), 1, kSlots); } + + int LocalIndex(__int64 module, int player) { + for (int j = 0; j < kSlots; ++j) + if (Players::Handle(module, j) == player) return j; + return -1; + } + + // A local player's record (unit +0x2C, flags +6), or null. + char* Record(__int64 module, int local) { + int handle = Players::Handle(module, local); + return handle != -1 ? Players::Player(module, handle) : nullptr; + } + + bool HasUnit(const char* record) { return *(const int*)(record + 0x2C) != -1; } + + // The count the game's local players are held at: the ones who joined, once they're all mapped; until then 0, and + // the game keeps its own count. The game counts players as it maps them (in no set order over a fresh mission's + // first ticks) and unmaps them, so a count past the ones mapped would run on past four. + int Held(__int64 module, int joined) { + for (int j = 0; j < joined; ++j) + if (Players::Handle(module, j) == -1) return 0; + return joined; + } + + int Hold(__int64 module) { + if (!s_active) return kSlots; + int joined = s_admitted = Joined(); + for (int j = joined; j < kSlots; ++j) s_held_back[j] = true; + if (char* globals = Globals(module)) + if (int held = Held(module, joined)) *(short*)(globals + 8) = (short)held; + return joined; + } + + bool JoinedLate(int local) { return s_active && local >= 0 && local < kSlots && s_held_back[local]; } + + void Account(__int64 module) { + char* globals = s_active ? Globals(module) : nullptr; + int joined = s_admitted; + if (globals == nullptr || !Held(module, joined)) return; // until they're all mapped, the game's own + bool all_dead = true, dead = false; + for (int j = 0; j < joined; ++j) { + char* record = Record(module, j); + if (record == nullptr) return; + if (HasUnit(record) || (*(unsigned short*)(record + 6) & 8)) all_dead = false; // or waiting to spawn + else dead = true; + } + globals[4] = all_dead; + globals[5] = dead; + } + + // Players who aren't in the mission don't spawn (a starting place, or the co-op respawn beside a teammate). + Halo2Entry(entry_spawn, OFFSET_HALO2_PF_PLAYER_SPAWN, bool, player_spawn, int player) { + if (s_active) { + int local = LocalIndex(entry_spawn.m_target - entry_spawn.m_offset, player); // -1 without the globals + if (local >= s_admitted) return false; + } + return ((player_spawn_t)entry_spawn.m_pOriginal)(player); + } + + bool Available() { return entry_feature->Available(); } + + void Reset() { + s_active = false; + s_admitted = kSlots; + std::fill(std::begin(s_held_back), std::end(s_held_back), false); + } + const bool s_reset = (Halo2EntrySet()->on_remove(&Reset), true); + + // As a mission starts (halo2/graphics.cpp). + void Start(bool on) { + Reset(); + s_admitted = Joined(); + s_active = on && Available(); + if (s_active) LOG_INFO("Halo 2 hot join: this mission holds back the players who haven't joined"); + } + + bool Active() { return s_active; } +} diff --git a/src/mcc/module/entry/halo2/players.cpp b/src/mcc/module/entry/halo2/players.cpp index e921257c..dbe9a0c4 100644 --- a/src/mcc/module/entry/halo2/players.cpp +++ b/src/mcc/module/entry/halo2/players.cpp @@ -5,10 +5,14 @@ // there. Local players past the mission's starting locations take the co-op respawn instead, which brings them in // beside a teammate (once that teammate is out of combat, as for any co-op respawn) - only where the game gives co-op // respawns. On Legendary or with the Iron skull it doesn't, and counts a co-op player waiting for one as dead, which -// reverts to the checkpoint: there they keep waiting for a starting location, as before. +// reverts to the checkpoint: there they keep waiting for a starting location, as before. In a hot-join mission +// (hotjoin.cpp) the players who haven't joined keep waiting for a starting location (one waiting for the co-op respawn +// counts as a dead teammate, and no checkpoint would be saved), and one who has joined takes the co-op respawn whenever +// they have no unit - a checkpoint from before they joined included - so they come in beside a teammate. namespace Halo2::Entry::Players { EntryFeature("Halo 2 co-op respawn", OFFSET_HALO2_PV_RESPAWN, OFFSET_HALO2_PV_PLAYERS, OFFSET_HALO2_PV_SCENARIO, OFFSET_HALO2_PF_COOP_CAMPAIGN, OFFSET_HALO2_PF_COOP_RESPAWN_ALLOWED); + const ::AlphaRing::Feature& Hooks() { return *entry_feature; } // players globals: player handle per local player, int[4] at +0xC (-1: none) int Handle(__int64 module, int local) { @@ -36,13 +40,16 @@ namespace Halo2::Entry::Players { Halo2Entry(entry, OFFSET_HALO2_PF_PLAYERS_UPDATE, void, detour, void* state) { __int64 module = entry.m_target - entry.m_offset; + int joined = HotJoin::Hold(module); // every slot outside a hot-join mission int places = CoopRespawn(module) ? StartingLocations(module) : 0; - for (int local = places; places > 0 && local < 4; ++local) { + for (int local = 0; places > 0 && local < 4; ++local) { + if (local >= joined || (local < places && !HotJoin::JoinedLate(local))) continue; int handle = Handle(module, local); char* player = handle != -1 ? Player(module, handle) : nullptr; if (player && *(int*)(player + 0x2C) == -1) // still waiting for a unit *(unsigned short*)(player + 6) &= ~8; } ((detour_t)entry.m_pOriginal)(state); + HotJoin::Account(module); } } diff --git a/src/mcc/settings/MenuSettings.cpp b/src/mcc/settings/MenuSettings.cpp index bf6bacd3..c4a1e31c 100644 --- a/src/mcc/settings/MenuSettings.cpp +++ b/src/mcc/settings/MenuSettings.cpp @@ -6,6 +6,7 @@ #include "filesystem/Filesystem.h" #include "mcc/CGameManager.h" #include "mcc/CGameGlobal.h" +#include "mcc/splitscreen/Splitscreen.h" #include "render/imgui/game/xbox/CXboxColorMapping.h" #include @@ -589,8 +590,9 @@ namespace AlphaRing::MenuSettings { auto p_setting = AlphaRing::Global::MCC::Splitscreen(); if (!p_setting) return; auto& ms = State(); + // hot join brings players in through AlphaRing's split screen, a lone player 1 as the first of several if (atGameStart) - p_setting->b_override = ms.playerCount > 1; + p_setting->b_override = ms.playerCount > 1 || MCC::Splitscreen::HotJoinChosen(); else if (ms.playerCount > 1) p_setting->b_override = true; p_setting->player_count = ms.playerCount; diff --git a/src/mcc/splitscreen/Join.cpp b/src/mcc/splitscreen/Join.cpp new file mode 100644 index 00000000..04182476 --- /dev/null +++ b/src/mcc/splitscreen/Join.cpp @@ -0,0 +1,334 @@ +// The Players window: couch-style joining. Every controller answers here directly: A joins as the next player, B +// leaves, Y picks Spartan or Elite. At MCC's menus the players and their controllers can still change before a +// mission. In a game it shows the lineup: the game reads the controllers live, so players only join there with hot +// join (a controller no player uses presses A), and only in Halo CE does the last player leave (B). +// +// The players and their controllers are the menus' (mcc/settings/MenuSettings, which the dashboard and the overlay +// share): a change goes into that state and in place at once, as the Splitscreen window's player count does, and is +// written with the rest when a menu saves (the overlay closing, for one). A game start applies the state, joins +// included. On a running map the controllers are the ones the players have then (State). +#include "Splitscreen.h" + +#include "common.h" + +#include "global/Global.h" +#include "input/Input.h" +#include "mcc/CGameManager.h" +#include "mcc/mcc.h" +#include "mcc/settings/MenuSettings.h" +#include "render/imgui/game/xbox/CXboxContext.h" + +#include "imgui.h" + +#include +#include +#include +#include + +namespace MCC::Splitscreen { + static constexpr int kSlots = 4; + static constexpr ImU32 kColors[kSlots] = {IM_COL32(85, 164, 242, 255), IM_COL32(242, 85, 85, 255), + IM_COL32(85, 242, 85, 255), IM_COL32(242, 216, 85, 255)}; // the cards + + // ---- the lineup: how many play, with which controllers, and player 1 on keyboard and mouse or not + + namespace Settings = AlphaRing::MenuSettings; + + // The menus' state, under Settings::Mutex(). On a running map the game reads each player's controller and keyboard + // and mouse for player 1 live (CGameManager::get_input_device), as that game's start put them in place and the + // Splitscreen window's Input, Bind and "Enable K/M for player1" may have changed them since: those are the + // players', so a controller rebound to a player isn't a free one, and a join or a leave keeps them. + static MenuState& State() { + auto& state = Settings::State(); + if (MCC::IsInGame() && CGameManager::running()) { + for (int i = 0; i < kSlots; ++i) + state.controllerIndex[i] = CGameManager::get_profile(i)->controller_index; + state.useKM = AlphaRing::Global::MCC::Splitscreen()->b_player0_use_km; + } + return state; + } + + // The player count, published last with a release store and read on the game thread with an acquire load, so that + // whoever reads a new count sees the controllers put in place before it. It's the dashboard's plain int (Global.h), + // so through Windows' interlocked-ordered accessors for it (winnt.h). + static_assert(sizeof(LONG) == sizeof(int)); + static void PublishPlayerCount(int count) { + WriteRelease(reinterpret_cast(&AlphaRing::Global::MCC::Splitscreen()->player_count), count); + } + + int PlayerCount() { + return ReadAcquire(reinterpret_cast(&AlphaRing::Global::MCC::Splitscreen()->player_count)); + } + + // A change goes in place as the Splitscreen window's player count puts its own (Settings::ApplySplitscreen): the + // controllers, keyboard and mouse for player 1, split screen on above one player, and the count. + static void PutInPlace(const MenuState& state) { + auto setting = AlphaRing::Global::MCC::Splitscreen(); + for (int i = 0; i < kSlots; ++i) CGameManager::get_profile(i)->controller_index = state.controllerIndex[i]; + setting->b_player0_use_km = state.useKM; + if (state.playerCount > 1) setting->b_override = true; + PublishPlayerCount(state.playerCount); // last: a new player's controller before the count makes them one + } + + // The player `pad` plays as, or -1 (player 1 on keyboard and mouse has no pad). + static int SlotOf(int pad) { + auto& state = State(); + for (int i = state.useKM ? 1 : 0; i < state.playerCount; ++i) + if (state.controllerIndex[i] == pad) return i; + return -1; + } + + // A pad that just joined gives the game nothing until what it joined with is let go: the A would jump, and anything + // else held would act at once (JoinPressHeld, from the game's input poll). + static std::atomic s_join_held[kSlots] = {}; + + static bool Held(const XINPUT_GAMEPAD& gamepad) { + return gamepad.wButtons || gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD || + gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD; + } + + bool JoinPressHeld(int pad, const XINPUT_GAMEPAD& gamepad) { + if (pad < 0 || pad >= kSlots || !s_join_held[pad]) return false; + if (Held(gamepad)) return true; + s_join_held[pad] = false; + return false; + } + + // A press hot join or the window acted on (a join, a leave, a species), which the Splitscreen window's Bind doesn't + // also take until it's let go (PressTaken): Bind can take a press in the same frame, after them (BindPending). + static bool s_taken[kSlots] = {}; + + bool PressTaken(int pad, const XINPUT_GAMEPAD& gamepad) { + if (pad < 0 || pad >= kSlots || !s_taken[pad]) return false; + if (Held(gamepad)) return true; + s_taken[pad] = false; + return false; + } + + // Each frame (HotJoinPoll), whether Bind is drawn or not, and whether the game reads the controller or not (not + // while the overlay is shown, by default): a press acted on, a join's included, is let go once its controller holds + // nothing. + static void LetGoTaken() { + for (int pad = 0; pad < kSlots; ++pad) { + XINPUT_STATE state{}; + if ((s_taken[pad] || s_join_held[pad]) && + (!AlphaRing::Input::GetXInputGetState(pad, &state) || !Held(state.Gamepad))) { + s_taken[pad] = false; + s_join_held[pad] = false; + } + } + } + + // A new player with `pad`: their slot, or -1. The game reads these live in a mission (hot join), so the pad is held + // back before the count makes it a player's. + static int Join(int pad) { + auto& state = State(); + int slot = state.playerCount; + if (slot >= kSlots || SlotOf(pad) >= 0) return -1; + s_join_held[pad] = true; + s_taken[pad] = true; + state.controllerIndex[slot] = pad; + state.playerCount = slot + 1; + PutInPlace(state); + return slot; + } + + // Everyone after the leaving player moves up a slot, keeping their controller; the leaver's goes to the slot left + // free, so each of the dashboard's player pages keeps a controller of its own. + static bool Leave(int slot) { + auto& state = State(); + if (slot < 0 || slot >= state.playerCount || state.playerCount <= 1) return false; + std::rotate(state.controllerIndex + slot, state.controllerIndex + slot + 1, + state.controllerIndex + state.playerCount); + --state.playerCount; + PutInPlace(state); + return true; + } + + static bool DashboardOpen() { return g_pXboxContext && g_pXboxContext->isOpen(); } + + // ---- the pads, read at most once a frame, by hot join or the window, whichever needs them first: which are + // connected, and the buttons newly down on each. Buttons already held when reading starts aren't presses. + + static unsigned s_frame = 0, s_read_frame = ~0u; // frames counted by HotJoinPoll; not read yet + static WORD s_buttons[kSlots] = {}, s_pressed[kSlots] = {}; + static bool s_connected[kSlots] = {}; + + static void ReadPads() { + if (s_read_frame == s_frame) return; + bool continuing = s_read_frame + 1 == s_frame; + s_read_frame = s_frame; + for (int pad = 0; pad < kSlots; ++pad) { + XINPUT_STATE state{}; + s_connected[pad] = AlphaRing::Input::GetXInputGetState(pad, &state); + s_pressed[pad] = continuing ? state.Gamepad.wButtons & ~s_buttons[pad] : 0; + s_buttons[pad] = state.Gamepad.wButtons; + } + } + + // The Splitscreen window's Bind has the controllers while it waits for one: nothing here acts on them, and reading + // starts again from what's held then, so the press Bind took is no press here until it's let go. + static bool BindHasPads() { + if (!BindPending()) return false; + s_read_frame = ~0u; + return true; + } + + // ---- species: each player's Spartan or Elite (CUserProfile::UseEliteModel), which Halo 2, Halo 3 and Reach read + // as a game loads. -1: the profile's own (player 1's, which AlphaRing copies to every player's profile). + + static std::atomic s_species[kSlots] = {-1, -1, -1, -1}; + + void ApplySpecies(int index, CUserProfile& profile) { + int species = index >= 0 && index < kSlots ? s_species[index].load() : -1; + if (species >= 0) profile.UseEliteModel = species != 0; + } + + // The game takes an AlphaRing profile of the player's own (CGameManager::get_player_profile): only with + // AlphaRing's split screen on, player 1's while "Override profile" is, the others' unless they share player 1's. + static bool OwnsProfile(int slot) { + auto setting = AlphaRing::Global::MCC::Splitscreen(); + if (!setting->b_override) return false; + return slot == 0 ? setting->b_override_profile : !setting->b_use_player0_profile; + } + + static bool Elite(int slot) { + int species = s_species[slot]; + return species >= 0 ? species != 0 : CGameManager::get_profile(slot)->profile.UseEliteModel; + } + + static void ToggleSpecies(int slot) { + if (!OwnsProfile(slot)) return; + bool elite = !Elite(slot); + s_species[slot] = elite; + CGameManager::get_profile(slot)->profile.UseEliteModel = elite; + } + + // ---- hot join (Splitscreen::HotJoinActive): the lineup changes during a mission + + void HotJoinPoll() { + std::lock_guard lock(Settings::Mutex()); + ++s_frame; + LetGoTaken(); + // the dashboard has the controllers while it's open + if (BindHasPads() || !HotJoinActive() || DashboardOpen() || State().playerCount >= kSlots) return; + ReadPads(); + for (int pad = 0; pad < kSlots; ++pad) { + int slot = s_pressed[pad] & XINPUT_GAMEPAD_A && SlotOf(pad) < 0 ? Join(pad) : -1; + if (slot >= 0) LOG_INFO("Hot join: controller {} joins as player {}", pad + 1, slot + 1); + } + } + + // A Halo CE hot-join mission: the last player leaves. + static void HotJoinLeave(int slot) { + if (!HotJoinLeaves() || slot <= 0 || slot != State().playerCount - 1 || !Leave(slot)) return; + LOG_INFO("Hot join: player {} leaves", slot + 1); + } + + // ---- the window (the overlay's menu bar) + + static bool s_show = false; + + static void PollPads(bool changes) { + ReadPads(); + for (int pad = 0; pad < kSlots; ++pad) { + WORD pressed = s_pressed[pad]; + int slot = SlotOf(pad); + if (changes && (pressed & XINPUT_GAMEPAD_A)) { + Join(pad); + } else if (pressed & XINPUT_GAMEPAD_B) { + s_taken[pad] = true; + if (changes) Leave(slot); + else HotJoinLeave(slot); + } else if ((pressed & XINPUT_GAMEPAD_Y) && slot >= 0) { + s_taken[pad] = true; + ToggleSpecies(slot); + } + } + } + + static void Card(int slot, bool changes) { + auto& state = State(); + bool joined = slot < state.playerCount; + float unit = ImGui::GetFontSize(); + ImVec2 size(unit * 13.0f, unit * 8.0f); + ImVec2 at = ImGui::GetCursorScreenPos(); + auto draw = ImGui::GetWindowDrawList(); + ImU32 color = kColors[slot]; + draw->AddRectFilled(at, ImVec2(at.x + size.x, at.y + size.y), + joined ? (color & 0x60FFFFFF) : IM_COL32(40, 40, 40, 160), unit * 0.5f); + draw->AddRect(at, ImVec2(at.x + size.x, at.y + size.y), joined ? color : IM_COL32(90, 90, 90, 255), + unit * 0.5f, 0, 3.0f); + + float x = at.x + unit * 0.8f; + char line[64]; + std::snprintf(line, sizeof(line), "PLAYER %d", slot + 1); + draw->AddText(ImVec2(x, at.y + unit * 0.7f), joined ? color : IM_COL32(160, 160, 160, 255), line); + const char* status; + if (!joined) { + status = changes ? "Press A to join" : "Free"; + } else if (slot == 0 && state.useKM) { + status = "Keyboard & mouse"; + } else if (int pad = state.controllerIndex[slot]; pad < 0 || pad >= kSlots) { + status = "No controller"; // the Splitscreen window's NONE + } else { + bool connected = s_connected[pad]; + std::snprintf(line, sizeof(line), "Controller %d%s", pad + 1, connected ? "" : " (disconnected)"); + status = line; + } + draw->AddText(ImVec2(x, at.y + unit * 2.4f), IM_COL32(230, 230, 230, 255), status); + + if (joined && OwnsProfile(slot)) { // Spartan or Elite, also with Y on the player's controller + ImGui::SetCursorScreenPos(ImVec2(x, at.y + unit * 4.1f)); + std::snprintf(line, sizeof(line), "%s##species%d", Elite(slot) ? "Elite" : "Spartan", slot); + if (ImGui::SmallButton(line)) ToggleSpecies(slot); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("Spartan or Elite (Y on the player's controller): Halo 2, Halo 3 and Reach, from " + "the next game"); + } + if (changes && joined && !(slot == 0 && state.useKM)) + draw->AddText(ImVec2(x, at.y + unit * 6.1f), IM_COL32(170, 170, 170, 255), "B to leave"); + ImGui::SetCursorScreenPos(at); + ImGui::Dummy(size); + } + + void JoinContext() { + std::lock_guard lock(Settings::Mutex()); + bool bind = BindHasPads(); // every overlay frame, before the Splitscreen window can take a press (ImGuiContext) + if (ImGui::BeginMainMenuBar()) { + ImGui::MenuItem("Players", nullptr, &s_show); + ImGui::EndMainMenuBar(); + } + if (!s_show || DashboardOpen()) return; // one open at a time: the dashboard has the controllers + + bool changes = !MCC::IsInGame(); + if (!bind) PollPads(changes); + if (ImGui::Begin("Players", &s_show, + ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize | + ImGuiWindowFlags_NoSavedSettings)) { + for (int slot = 0; slot < kSlots; ++slot) { + if (slot) ImGui::SameLine(); + Card(slot, changes); + } + if (bind) ImGui::TextDisabled("Bind (Splitscreen window) is waiting for a controller"); + else if (changes) ImGui::TextDisabled("A: join B: leave Y: Spartan / Elite"); + else if (HotJoinLeaves()) + ImGui::TextDisabled("Hot join: A on a free controller joins, B leaves (the last player) Y: Spartan / Elite"); + else if (HotJoinActive()) + ImGui::TextDisabled("Hot join: A on a free controller joins; players leave at MCC's menus Y: Spartan / Elite"); + else ImGui::TextDisabled("In a game: players join and leave at MCC's menus Y: Spartan / Elite"); + + bool hot_join = HotJoinChosen(); + ImGui::BeginDisabled(HotJoinLocked()); + if (ImGui::Checkbox("Hot join (experimental)", &hot_join)) ChooseHotJoin(hot_join); + ImGui::EndDisabled(); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) + ImGui::SetTooltip("Players join in the middle of a mission: a controller nobody is playing with presses " + "A to join, and comes in beside a teammate. In Halo CE the last player can also leave, " + "with B here; in the other games players leave at MCC's menus. Halo CE and Halo 2 take " + "it as a mission starts, so in their games it's changed at MCC's menus. On until MCC " + "closes."); + } + ImGui::End(); + } +} diff --git a/src/mcc/splitscreen/Splitscreen.cpp b/src/mcc/splitscreen/Splitscreen.cpp index a2e47c2e..46e250b1 100644 --- a/src/mcc/splitscreen/Splitscreen.cpp +++ b/src/mcc/splitscreen/Splitscreen.cpp @@ -9,6 +9,7 @@ #include "../CGameManager.h" #include "log/MatchLog.h" #include "mcc/CGameGlobal.h" +#include "mcc/mcc.h" #include "mcc/module/patch/SplitscreenConfigStore.h" #include "LeftRight.h" @@ -16,6 +17,8 @@ namespace Halo1::Entry::Anniversary { bool Available(); bool QuadShown(); } namespace Halo2::Entry::Anniversary { bool Available(); } +namespace Halo1::Entry::HotJoin { bool Available(); bool Active(); } +namespace Halo2::Entry::HotJoin { bool Available(); bool Active(); } namespace MCC::Splitscreen { DefDetourFunction(__int64, __fastcall, get_index_by_xuid, void* a1, __int64 xuid) { @@ -68,23 +71,28 @@ namespace MCC::Splitscreen { auto p_setting = AlphaRing::Global::MCC::Splitscreen(); bool split = p_setting->b_override && game_options != nullptr; // AlphaRing's split screen sets this mission up - bool left_right = split && LeftRight::Chosen() && LeftRight::Supports(game, p_setting->player_count); + // A hot-join mission can have any number of players: it's set up as one for four, and side by side for when + // two or three are in. + bool hot_join = split && HotJoinReserves(game); + bool left_right = split && LeftRight::Chosen() && + LeftRight::Supports(game, hot_join ? 2 : p_setting->player_count); LeftRight::StartClassicMission(game, left_right); // Chosen, the 3-4 player mode is set up for the whole mission, whichever graphics it starts in: Back // switches Halo CE and Halo 2 to Anniversary in the middle of one, side by side included. bool quad = split && AnniversaryQuadChosen() && AnniversaryQuadAvailable(game); s_anniversary_quad_game = quad ? game : -1; if (!split) return; - if (p_setting->player_count <= 2 && !left_right) return; + if (p_setting->player_count <= 2 && !hot_join && !left_right) return; bool anniversary = game_options[0] & 1; if (!anniversary) return; if (quad && !left_right) { - LOG_INFO("Splitscreen: {} players in Anniversary graphics (experimental)", p_setting->player_count); + LOG_INFO("Splitscreen: {} players{} in Anniversary graphics (experimental)", p_setting->player_count, + hot_join ? " and hot join" : ""); return; } - LOG_INFO("Splitscreen: {} players{}, starting in Classic graphics", p_setting->player_count, - left_right ? " side by side" : ""); + LOG_INFO("Splitscreen: {} players{}{}, starting in Classic graphics", p_setting->player_count, + left_right ? " side by side" : "", hot_join ? " and hot join" : ""); m_options = game_options; m_saved = game_options[0]; game_options[0] &= ~1; @@ -93,6 +101,70 @@ namespace MCC::Splitscreen { ClassicGraphicsScope::~ClassicGraphicsScope() { if (m_options) m_options[0] = m_saved; } + + static std::atomic s_hot_join{false}; + + bool HotJoinChosen() { return s_hot_join; } + + // The slots are AlphaRing's, a lone player 1 the first of several; every game start keeps split screen on for it + // too (AlphaRing::MenuSettings::ApplySplitscreen). + void ChooseHotJoin(bool on) { + s_hot_join = on; + if (on) AlphaRing::Global::MCC::Splitscreen()->b_override = true; + } + + bool HotJoinOn() { return AlphaRing::Global::MCC::Splitscreen()->b_override && s_hot_join; } + + // How a game takes a player joining in the middle of a mission. Halo 3, ODST, Reach and Halo 4 make the player MCC + // signs in. Halo CE and Halo 2 make their players as a mission loads, so their modules keep all four and hold back + // the ones who haven't joined (module/entry/halo1/hotjoin.cpp, halo2/hotjoin.cpp), where this build has all the + // module's addresses; the module notes whether the running mission started so (Active). + bool HotJoinReserves(int game) { + return HotJoinOn() && (game == CGameGlobal::Halo1 ? Halo1::Entry::HotJoin::Available() + : game == CGameGlobal::Halo2 && Halo2::Entry::HotJoin::Available()); + } + + static bool SignsIn(int game) { + return game == CGameGlobal::Halo3 || game == CGameGlobal::Halo3ODST || game == CGameGlobal::HaloReach || + game == CGameGlobal::Halo4; + } + + // A running map takes joins (not one that is still loading). + bool HotJoinActive() { + if (!HotJoinOn() || !MCC::IsInGame() || !CGameManager::running()) return false; + auto p_global = GameGlobal(); + if (p_global == nullptr) return false; + int game = p_global->current_game; + return game == CGameGlobal::Halo1 ? Halo1::Entry::HotJoin::Active() + : game == CGameGlobal::Halo2 ? Halo2::Entry::HotJoin::Active() + : SignsIn(game); + } + + // Players leave in the middle of a Halo CE mission only: its module takes the leaver's Spartan away, and the games + // that sign players in end the mission when MCC signs one out. + bool HotJoinLeaves() { + auto p_global = GameGlobal(); + return HotJoinActive() && p_global && p_global->current_game == CGameGlobal::Halo1; + } + + int HotJoinSlots(int player_count) { + auto p_global = GameGlobal(); + return p_global && HotJoinReserves(p_global->current_game) ? 4 : player_count; + } + + // Halo CE and Halo 2 take the choice as a mission starts, and MCC ends one whose held-back players go away. + bool HotJoinLocked() { + auto p_global = GameGlobal(); + return MCC::IsInGame() && p_global && + (p_global->current_game == CGameGlobal::Halo1 || p_global->current_game == CGameGlobal::Halo2); + } + + // Its missions start as hot-join ones (with split screen on, as every game start puts it with hot join chosen). + bool HotJoinHeld() { + if (!HotJoinChosen() || !HotJoinLocked()) return false; + return GameGlobal()->current_game == CGameGlobal::Halo1 ? Halo1::Entry::HotJoin::Available() + : Halo2::Entry::HotJoin::Available(); + } } #include "imgui.h" @@ -124,6 +196,7 @@ namespace MCC::Splitscreen { for (int i = 0; i < 4; i++) { XINPUT_STATE state; if (AlphaRing::Input::GetXInputGetState(i, &state)) { + if (PressTaken(i, state.Gamepad)) continue; // the Players window or hot join acted on it (Join.cpp) if (state.Gamepad.wButtons != 0 || state.Gamepad.bLeftTrigger > 30 || state.Gamepad.bRightTrigger > 30) { @@ -137,9 +210,21 @@ namespace MCC::Splitscreen { // Binding state: which player slot is waiting for controller input (-1 = none) static int s_binding_player = -1; + // Bind takes a controller only on its player's page as it's drawn, with the overlay shown: so only while that page + // was drawn waiting the frame before (or this one: before a frame starts the count is still the last one's). A Bind + // whose player has gone is dropped. + static int s_binding_frame = -2; // the frame the page last drew the Bind waiting + bool BindPending() { + if (s_binding_player >= PlayerCount()) s_binding_player = -1; + return s_binding_player >= 0 && AlphaRing::Global::Global()->show_imgui && + s_binding_frame >= ImGui::GetFrameCount() - 1; + } + void ImGuiContext() { static bool show_splitscreen; + JoinContext(); + if (ImGui::BeginMainMenuBar()) { ImGui::MenuItem("Splitscreen", nullptr, &show_splitscreen); ImGui::EndMainMenuBar(); @@ -455,6 +540,7 @@ namespace MCC::Splitscreen { s_binding_player = index; } } + if (s_binding_player == index) s_binding_frame = ImGui::GetFrameCount(); // BindPending if (p_profile->controller_index != controller_index) { ms.controllerIndex[index] = p_profile->controller_index; @@ -560,7 +646,9 @@ namespace MCC::Splitscreen { ImGui::AlignTextToFramePadding(); ImGui::TextUnformatted("Players"); ImGui::SameLine(); - ImGui::BeginDisabled(player_count <= 1); + // in a hot-join Halo CE or Halo 2 game the count changes only by joining and leaving + bool held = HotJoinHeld(); + ImGui::BeginDisabled(held || player_count <= 1); if (ImGui::Button("-", ImVec2(button_size, button_size))) --player_count; ImGui::EndDisabled(); @@ -572,10 +660,12 @@ namespace MCC::Splitscreen { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (button_size - text_width) * 0.5f); ImGui::TextUnformatted(count_text); ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.x + (button_size - text_width) * 0.5f); - ImGui::BeginDisabled(player_count >= 4); + ImGui::BeginDisabled(held || player_count >= 4); if (ImGui::Button("+", ImVec2(button_size, button_size))) ++player_count; ImGui::EndDisabled(); + if (held && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) + ImGui::SetTooltip("Players join and leave in the Players window until this hot-join game ends"); if (player_count != ms.playerCount) { ms.playerCount = player_count; Settings::ApplySplitscreen(false); diff --git a/src/mcc/splitscreen/Splitscreen.h b/src/mcc/splitscreen/Splitscreen.h index 0466c51f..ebfe21d3 100644 --- a/src/mcc/splitscreen/Splitscreen.h +++ b/src/mcc/splitscreen/Splitscreen.h @@ -1,17 +1,46 @@ #pragma once +struct CUserProfile; +struct _XINPUT_GAMEPAD; + namespace MCC::Splitscreen { bool Initialize(); void ImGuiContext(); } +namespace MCC::Splitscreen { + // The Players window (Join.cpp): each controller presses A to join and B to leave. It changes the menus' player + // state (mcc/settings/MenuSettings) - the players and their controllers - as the Splitscreen window's player count + // does, and a menu saving writes it with the rest. + void JoinContext(); + // From the game's input poll (CGameManagerSplitscreen.cpp): true while controller `pad` still holds what it joined + // with, when the game must see it released - until its buttons and triggers are all let go. + bool JoinPressHeld(int pad, const _XINPUT_GAMEPAD& gamepad); + // From the Splitscreen window's Bind: true while controller `pad` still holds a press the Players window or hot join + // acted on (a join, a leave, a species), which Bind doesn't also take - until it's let go. + bool PressTaken(int pad, const _XINPUT_GAMEPAD& gamepad); + // The Splitscreen window's Bind is waiting for a controller on its player's page, drawn with the overlay shown + // (Splitscreen.cpp): until it has one it has the controllers, as the dashboard has while it's open, so the Players + // window and hot join don't act on them. Once the page isn't drawn they're theirs again. + bool BindPending(); + // The player count (Global::MCC::Splitscreen), read with an acquire load: the Players window and hot join put a new + // player's controller in place before the count, which they publish with a release store, so a reader on the game + // thread that sees the count sees the controller too. + int PlayerCount(); + // A player's species (Spartan or Elite) chosen in the Players window, back on their AlphaRing profile after it was + // copied over from player 1's (CGameManager::apply_profiles, Reach's armour sync); nothing if none was chosen. + void ApplySpecies(int index, CUserProfile& profile); +} + namespace MCC::Splitscreen { // Halo CE and Halo 2's Anniversary renderer only ever drew two views, stacked, so missions with // three or four players, or side by side (LeftRight.h, which takes the layout choice here), start in // Classic: bit 0 of the first game-options byte selects Anniversary visuals in both engines, and it // is cleared only while the engine copies the options, leaving MCC's own setting untouched. With 3-4 // players (not side by side) both can keep Anniversary instead, an experimental choice - // (module/entry/halo1/anniversary.cpp, module/entry/halo2/anniversary.cpp). + // (module/entry/halo1/anniversary.cpp, module/entry/halo2/anniversary.cpp). A hot-join mission + // starts as one for four players would, side by side for when two or three are in: a player can + // join it at any time. class ClassicGraphicsScope { public: // `game`: CGameGlobal::Halo1 or Halo2 @@ -36,3 +65,27 @@ namespace MCC::Splitscreen { // Halo 2's keeps its own grid, player 1 on the top half. bool AnniversaryQuartersShown(int game); } + +namespace MCC::Splitscreen { + // Hot join (experimental): players join in the middle of a mission, and in Halo CE leave (Join.cpp). The choice + // is kept for the session (off as MCC starts) and needs AlphaRing's split screen, which it turns on: whether it's + // chosen, and on; whether the running mission takes joins, and leaves; each frame, a controller no player uses + // joining with A. + bool HotJoinChosen(); + void ChooseHotJoin(bool on); + bool HotJoinOn(); + bool HotJoinActive(); + bool HotJoinLeaves(); + void HotJoinPoll(); + // Local players MCC is told of: all four slots in a hot-join Halo CE or Halo 2 mission, else the player count. + int HotJoinSlots(int player_count); + // A Halo CE or Halo 2 mission of `game` starting now is a hot-join one: hot join is on and the game's module, which + // holds back the players who haven't joined, has its addresses (module/entry/halo1/hotjoin.cpp, halo2/hotjoin.cpp). + bool HotJoinReserves(int game); + // In a Halo CE or Halo 2 game, where the choice takes effect as a mission starts, it's changed at MCC's menus. + bool HotJoinLocked(); + // In a Halo CE or Halo 2 game with hot join (its module there), MCC has all four local players from the mission's + // start until the session exits, and ends it if they go away; the held-back ones come in only by joining. So until + // then the player count changes only by joining and leaving, not with the dashboard's or the Splitscreen window's. + bool HotJoinHeld(); +} diff --git a/src/render/imgui/ImGui.cpp b/src/render/imgui/ImGui.cpp index 98e276d8..2e5151ee 100644 --- a/src/render/imgui/ImGui.cpp +++ b/src/render/imgui/ImGui.cpp @@ -21,6 +21,7 @@ #include "mcc/CGameGlobal.h" #include "mcc/settings/MenuSettings.h" #include "mcc/spawn/Spawn.h" +#include "mcc/splitscreen/Splitscreen.h" static ICContext* pages[7] { nullptr, @@ -128,6 +129,7 @@ namespace AlphaRing::Render::ImGui { } void Render() { + MCC::Splitscreen::HotJoinPoll(); AlphaRing::Input::Update(); bool xboxOpen = g_pXboxContext && g_pXboxContext->isOpen(); diff --git a/src/render/imgui/game/xbox/CStateMachine.cpp b/src/render/imgui/game/xbox/CStateMachine.cpp index 3d49991a..93329f97 100644 --- a/src/render/imgui/game/xbox/CStateMachine.cpp +++ b/src/render/imgui/game/xbox/CStateMachine.cpp @@ -2,6 +2,7 @@ #include "CXboxRender.hpp" #include "CStateMachine.h" +#include "mcc/splitscreen/Splitscreen.h" #include #include @@ -266,7 +267,11 @@ void StateMachine::handleOption() { auto& opt = currentState.menu.pages[currentState.pageIndex] .options[currentState.optionIndex]; - Mix_PlayChannel(-1, currentState.sounds[4], 0); + // in a hot-join Halo CE or Halo 2 game the player count changes only by joining and leaving + bool held = currentState.pageIndex == 0 && MCC::Splitscreen::HotJoinHeld() && + (opt.type == OptionType::Increment || opt.type == OptionType::Decrement); + Mix_PlayChannel(-1, currentState.sounds[held ? 5 : 4], 0); + if (held) return; if (currentState.pageIndex == 0) { if (opt.type == OptionType::Increment && currentState.menuState.playerCount < 4) From 4fd18908d7ff2efb6b8f8d23b7aa033b1b141747 Mon Sep 17 00:00:00 2001 From: John P <7864917+kirklandsig@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:23:02 -0400 Subject: [PATCH 2/3] fix(halo2): a hot-join mission's death accounting counts every player but the held-back ones Account replaced the game's "every player is dead" and "one is dead" flags (players globals +4/+5) with values worked out over the local players who joined only. The game's own routine (0x6A16F0) walks the whole players array, so in network co-op a remote teammate's death or life was dropped: everyone could count as dead while a remote teammate lived (a revert), or a remote teammate's death could stop vetoing checkpoints. It now walks the array as the game's iterator (0x67B050) reads it - count +0x3C, in-use bits, element size +0x24 - and skips only the local players held back. Found by a Codex adversarial review. Co-Authored-By: Claude Opus 5.5 --- src/mcc/module/entry/halo2/hotjoin.cpp | 36 +++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/mcc/module/entry/halo2/hotjoin.cpp b/src/mcc/module/entry/halo2/hotjoin.cpp index ee2c9eba..9c275425 100644 --- a/src/mcc/module/entry/halo2/hotjoin.cpp +++ b/src/mcc/module/entry/halo2/hotjoin.cpp @@ -7,7 +7,7 @@ // The players held back keep waiting for a starting place, as players do before their first spawn: a player without a // unit who isn't waiting for one counts as a dead teammate (0x6A16F0 sets players globals +5), which keeps every // checkpoint from being saved (game_safe_to_save, 0x7005C0, at 0x70067E). Waiting so, though, they count as alive in -// the game's "every player is dead" (+4), so that's worked out over the players who joined (Account). +// the game's "every player is dead" (+4), so that's worked out over every player but them (Account). #include "halo2.h" #include "global/Global.h" @@ -19,7 +19,7 @@ namespace Halo2::Entry::HotJoin { // players.cpp holds the count each tick, from its players_update hook - EntryFeature("Halo 2 hot join", OFFSET_HALO2_PF_COPY_GAME_OPTIONS, OFFSET_HALO2_PV_RESPAWN); + EntryFeature("Halo 2 hot join", OFFSET_HALO2_PF_COPY_GAME_OPTIONS, OFFSET_HALO2_PV_RESPAWN, OFFSET_HALO2_PV_PLAYERS); const bool s_requires_players = (entry_feature->Requires(Players::Hooks()), true); constexpr int kSlots = 4; @@ -47,12 +47,7 @@ namespace Halo2::Entry::HotJoin { return -1; } - // A local player's record (unit +0x2C, flags +6), or null. - char* Record(__int64 module, int local) { - int handle = Players::Handle(module, local); - return handle != -1 ? Players::Player(module, handle) : nullptr; - } - + // A player's record: unit +0x2C, flags +6. bool HasUnit(const char* record) { return *(const int*)(record + 0x2C) != -1; } // The count the game's local players are held at: the ones who joined, once they're all mapped; until then 0, and @@ -75,15 +70,32 @@ namespace Halo2::Entry::HotJoin { bool JoinedLate(int local) { return s_active && local >= 0 && local < kSlots && s_held_back[local]; } + // Over every player, as 0x6A16F0 walks them (network co-op teammates included), but the local ones held back. The + // players data array, as its iterator (0x67B050) reads it: int count +0x3C, in-use bits at +*(+0x50), elements of + // int size +0x24 at +*(+0x48). void Account(__int64 module) { char* globals = s_active ? Globals(module) : nullptr; int joined = s_admitted; if (globals == nullptr || !Held(module, joined)) return; // until they're all mapped, the game's own + char* players = *(char**)(module + OFFSET_HALO2_PV_PLAYERS); + if (players == nullptr) return; + __int64 bits_at = *(__int64*)(players + 0x50), data_at = *(__int64*)(players + 0x48); + if (bits_at == 0 || data_at == 0) return; + auto bits = (const unsigned*)(players + bits_at); + int count = *(int*)(players + 0x3C), size = *(int*)(players + 0x24); + + int held_back[kSlots]; + for (int j = 0; j < kSlots; ++j) { + int handle = j >= joined ? Players::Handle(module, j) : -1; + held_back[j] = handle != -1 ? handle & 0xFFFF : -1; + } + bool all_dead = true, dead = false; - for (int j = 0; j < joined; ++j) { - char* record = Record(module, j); - if (record == nullptr) return; - if (HasUnit(record) || (*(unsigned short*)(record + 6) & 8)) all_dead = false; // or waiting to spawn + for (int i = 0; i < count; ++i) { + if (!(bits[i >> 5] >> (i & 31) & 1) || std::find(held_back, held_back + kSlots, i) != held_back + kSlots) + continue; + const char* record = players + data_at + (__int64)i * size; + if (HasUnit(record) || (*(const unsigned short*)(record + 6) & 8)) all_dead = false; // or waiting to spawn else dead = true; } globals[4] = all_dead; From b6791aca3a8d867bdafc2a66b036a338f86f5a64 Mon Sep 17 00:00:00 2001 From: John P <7864917+kirklandsig@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:34:17 -0500 Subject: [PATCH 3/3] fix(splitscreen): wrap the Hot join checkbox's tooltip It was one long line and ran off a 1080p screen (seen on the test box). It now wraps at 35 font sizes, as a tooltip paragraph. Co-Authored-By: Claude Opus 5.5 --- src/mcc/splitscreen/Join.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mcc/splitscreen/Join.cpp b/src/mcc/splitscreen/Join.cpp index 04182476..b1b714be 100644 --- a/src/mcc/splitscreen/Join.cpp +++ b/src/mcc/splitscreen/Join.cpp @@ -322,12 +322,17 @@ namespace MCC::Splitscreen { ImGui::BeginDisabled(HotJoinLocked()); if (ImGui::Checkbox("Hot join (experimental)", &hot_join)) ChooseHotJoin(hot_join); ImGui::EndDisabled(); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) - ImGui::SetTooltip("Players join in the middle of a mission: a controller nobody is playing with presses " - "A to join, and comes in beside a teammate. In Halo CE the last player can also leave, " - "with B here; in the other games players leave at MCC's menus. Halo CE and Halo 2 take " - "it as a mission starts, so in their games it's changed at MCC's menus. On until MCC " - "closes."); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { + ImGui::BeginTooltip(); // wrapped: one line would run off a 1080p screen + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted("Players join in the middle of a mission: a controller nobody is playing with " + "presses A to join, and comes in beside a teammate. In Halo CE the last player can " + "also leave, with B here; in the other games players leave at MCC's menus. Halo CE " + "and Halo 2 take it as a mission starts, so in their games it's changed at MCC's " + "menus. On until MCC closes."); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } } ImGui::End(); }