From ea07ba39b8044c9d823670d5c7648683bb648d48 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sat, 21 Nov 2026 00:54:27 +0100 Subject: [PATCH] reimplement: SHC_3BB0A8C1_0x00438BE0 100.0% Reimplemented BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtons, rewritten from the original's listing. gynt's drafts: 19176b882 at 39.09% ("bug fix, costs 0.6% match") on his fork's reimpl/UI-MenuItems, whose status line records 39.1%, and 05232650b at 40.73% ("if/else") on his fork's reimpl/UI-MenuItems-development, whose status line records 40.7%; the newest commit on the file, dd1a7c48c, is on that branch too. Our draft (72d2bc16b, then ae5a9208e and 07a33e2bb; 40.9% in the draft PR #328) was his code with a mechanical pass and a fix for button 376. The function decides for each unit action button whether it is drawn: one switch over the button's picture (DAT_CurrentButtonGmDataIndex, a GmDataIndex value), dispatched at 0x00438C35 through the jump table at 0x0043907C and the byte table at 0x004390AC. A hidden button stores TRUE in DAT_ButtonUnknownZero and returns; a shown one is rendered and stores FALSE. The drafts reached the shared render, the shared renderNumber call and the stance buttons' common end by goto (seven gotos to three labels), carried renderNumber's seven arguments there in seven locals, and reached part of the hides by break to a store behind the switch. Written without them, five things decide the match: - One render stands behind the switch and the shown paths leave by break. In the original 374, 216, 211 to 215 with 558, and default all jump to one render block (0x00438EC1); each path that falls into the render has a copy. - Three plain renders are written out all the same: at 218's end (0x00438CFA), in 219's ranged arm (0x00438EC1, the block the breaks land on) and at 219's end (0x00438EF3). With a break at 218's or 219's end the compiler keeps a copy of the hide there and jumps to the render, where the original jumps to the hide. With a break in the ranged arm the render block goes to the function's end and case 217 keeps code of its own (288 instructions against 277). - Every hide is written where it is decided (DAT_ButtonUnknownZero = TRUE; return;). The original keeps the 1 in esi in the cases that also compare with it, and the 0 in edi; its hides share two blocks, 0x00439048 for the register and 0x0043906C for the immediate. - The hides in front of a written-out render are one || test (218, 219's end), and so is the test of default and 217. As separate ifs the match is lost each time. Case 220's six conditions are one test as well; there one test and separate ifs compile the same. - case 217 has a body of its own, the same as default's. The original's jump table has twelve entries, one of them for 217 alone, and it leads to default's code (0x00439051). With `case 217:` in front of `default:` the table has eleven entries and the byte table other values, while the instructions stay the same. The two cases that draw a number each write their renderNumber call; the compiler shares the call's end (0x00438E95), as the original does. hasEngineerSelected is compared with TRUE in 376 and in 211 to 215 with 558, because the original compares with 1 there (0x00438C41, 0x0043902E); a truth test loses the match in both places. The case labels and the stores to DAT_CurrentButtonGmDataIndex use GmDataIndex's names; the siege engine unit types and the two colours use their enum names too. Measured: quick_diff 0.9964 with 277 instructions on both sides; the one difference is how the two listings print 376's tail call. All 45 local jumps land on the same instruction and both switch tables are the same, compared by a script because quick_diff prints every local jump alike. reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game. Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- ...erFunction_BuildMenu_UnitActionButtons.cpp | 279 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 280 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/MenuItems/BuildMenu/MenuItemRenderFunction_BuildMenu_UnitActionButtons.cpp diff --git a/src/OpenSHC/UI/MenuItems/BuildMenu/MenuItemRenderFunction_BuildMenu_UnitActionButtons.cpp b/src/OpenSHC/UI/MenuItems/BuildMenu/MenuItemRenderFunction_BuildMenu_UnitActionButtons.cpp new file mode 100644 index 0000000000..aa62116baa --- /dev/null +++ b/src/OpenSHC/UI/MenuItems/BuildMenu/MenuItemRenderFunction_BuildMenu_UnitActionButtons.cpp @@ -0,0 +1,279 @@ +#include "../BuildMenu.func.hpp" + +#include "OpenSHC/Map/MapPropertiesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Text/TextManager.func.hpp" +#include "OpenSHC/UI/MenuItems/General.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/Rendering/Colors/ColorsEnum.hpp" +#include "OpenSHC/Rendering/Enums/GmDataIndex.hpp" + +#include "OpenSHC/Globals/DAT_00ee1090.hpp" +#include "OpenSHC/Globals/DAT_00ee1094.hpp" +#include "OpenSHC/Globals/DAT_ButtonCurrentlyInteracting.hpp" +#include "OpenSHC/Globals/DAT_ButtonUnknownZero.hpp" +#include "OpenSHC/Globals/DAT_ButtonX.hpp" +#include "OpenSHC/Globals/DAT_ButtonY.hpp" +#include "OpenSHC/Globals/DAT_CurrentButtonGmDataIndex.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TextManagerObject.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace UI { + namespace MenuItems { + + // FUNCTION: STRONGHOLDCRUSADER 0x00438BE0 + void BuildMenu::MenuItemRenderFunction_BuildMenu_UnitActionButtons(int mapperValue, ...) + { + if (!MACRO_CALL_MEMBER(Map::MapPropertiesState_Func::isMapperAvailable, DAT_MapPropertiesState::ptr)( + (Commands::MappersEnum)mapperValue)) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + DAT_ButtonUnknownZero::instance = FALSE; + // A shown button breaks to the one render behind the switch, except where the render is written out. + switch ((int)DAT_CurrentButtonGmDataIndex::instance) { + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_376_PIC_325: + // == TRUE: a truth test loses the match + if (DAT_UnitsState::instance.hasEngineerSelected == TRUE) { + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + return; + } + DAT_ButtonUnknownZero::instance = TRUE; + return; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_374_PIC_315: + DAT_00ee1094::instance = 291; + if (DAT_GameCore::instance.gameMode_2 == Game::GM_EDITOR + || DAT_UnitsState::instance.hasEngineerSelected) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::returnFirstSelectedEngineer, DAT_UnitsState::ptr)() + != 0) { + break; + } + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::countSelectedCatapultsAndTrebuchets, DAT_UnitsState::ptr)() + == 0) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + DAT_00ee1094::instance = 308; + DAT_CurrentButtonGmDataIndex::instance = OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_377_PIC_414; + break; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_218_PIC_321: + if (DAT_GameCore::instance.gameMode_2 == Game::GM_EDITOR + || DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT + || DAT_UnitsState::instance.hasEngineerSelected + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getTunnelerIDOnlyIfFirstSelected, DAT_UnitsState::ptr)() + == 0) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + // written out behind one || test: a break or separate ifs lose the match + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + DAT_ButtonUnknownZero::instance = FALSE; + return; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_220_PIC_305: + if (DAT_GameCore::instance.gameMode_2 == Game::GM_EDITOR + || DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT + || (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + && DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SKIRMISH_SINGLE_PLAYER + && DAT_GameState::instance.mapAndTime.skirmishNoCowThrowing != 0) + || DAT_UnitsState::instance.hasEngineerSelected + || MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::getFirstSelectedUnitOfEitherType, + DAT_UnitsState::ptr)(Map::Units::UT_S_CATAPULT, Map::Units::UT_S_TREBUCHET) + == 0 + || DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .availableCows + <= 0) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + MACRO_CALL_MEMBER(OpenSHC::Text::TextManager_Func::renderNumber, DAT_TextManagerObject::ptr)( + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .availableCows, + DAT_ButtonX::instance + 2, DAT_ButtonY::instance + 2, OpenSHC::Rendering::Colors::COL_FFFFFF, + OpenSHC::Rendering::Colors::COL_000000, 19, FALSE); + DAT_ButtonUnknownZero::instance = FALSE; + return; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_219_PIC_317: + DAT_00ee1090::instance = 202; + if (DAT_GameCore::instance.gameMode_2 == Game::GM_EDITOR + || DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT + || DAT_UnitsState::instance.hasEngineerSelected) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsEngineersOnly, DAT_UnitsState::ptr)()) { + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getSelectedEngineerCarryingResource, DAT_UnitsState::ptr)() + == 0) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + DAT_CurrentButtonGmDataIndex::instance + = OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_210_PIC_319; + DAT_00ee1090::instance = 198; + break; + } + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsTunnelersOnly, DAT_UnitsState::ptr)()) { + DAT_CurrentButtonGmDataIndex::instance + = OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_218_PIC_321; + DAT_00ee1090::instance = 201; + break; + } + if (MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::getFirstSelectedUnitOfEitherType, + DAT_UnitsState::ptr)(Map::Units::UT_S_CATAPULT, Map::Units::UT_S_TREBUCHET) + != 0) { + DAT_CurrentButtonGmDataIndex::instance + = OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_375_PIC_323; + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + MACRO_CALL_MEMBER(OpenSHC::Text::TextManager_Func::renderNumber, DAT_TextManagerObject::ptr)( + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getMaxStoneAmmoInSelectedSiegeEngines, DAT_UnitsState::ptr)(), + DAT_ButtonX::instance + 2, DAT_ButtonY::instance + 2, OpenSHC::Rendering::Colors::COL_FFFFFF, + OpenSHC::Rendering::Colors::COL_000000, 19, FALSE); + DAT_ButtonUnknownZero::instance = FALSE; + return; + } + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsRangedOnlyUnits, DAT_UnitsState::ptr)()) { + DAT_CurrentButtonGmDataIndex::instance + = OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_375_PIC_323; + // written out, not a break: a break here loses the match + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + DAT_ButtonUnknownZero::instance = FALSE; + return; + } + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsLaddermenOnly, DAT_UnitsState::ptr)() + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsShieldmenOnly, DAT_UnitsState::ptr)()) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + // written out behind one || test, as in 218: a break or separate ifs lose the match + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + DAT_ButtonUnknownZero::instance = FALSE; + return; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_370_PIC_309: + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (DAT_TribesState::instance.tribes[DAT_TribesState::instance.DAT_CurrentTribeID].unitStance + == Map::Units::Behavior::USE_STAND_GROUND) { + DAT_ButtonY::instance -= 5; + DAT_ButtonCurrentlyInteracting::instance = TRUE; + } else { + DAT_ButtonCurrentlyInteracting::instance = FALSE; + } + break; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_371_PIC_311: + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (DAT_TribesState::instance.tribes[DAT_TribesState::instance.DAT_CurrentTribeID].unitStance + == Map::Units::Behavior::USE_DEFENSIVE) { + --DAT_ButtonY::instance; + DAT_ButtonCurrentlyInteracting::instance = TRUE; + } else { + DAT_ButtonCurrentlyInteracting::instance = FALSE; + } + break; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_372_PIC_313: + if (MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (DAT_TribesState::instance.tribes[DAT_TribesState::instance.DAT_CurrentTribeID].unitStance + == Map::Units::Behavior::USE_AGGRESSIVE) { + --DAT_ButtonY::instance; + DAT_ButtonCurrentlyInteracting::instance = TRUE; + } else { + DAT_ButtonCurrentlyInteracting::instance = FALSE; + } + break; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_216_PIC_301: + if (DAT_UnitsState::instance.hasEngineerSelected + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + if (DAT_TribesState::instance.patrolButtonPressed) { + DAT_ButtonCurrentlyInteracting::instance = TRUE; + } + break; + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_211_PIC_327: + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_212_PIC_329: + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_213_PIC_331: + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_214_PIC_333: + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_215_PIC_335: + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_558_PIC_465: + // != TRUE, as in 376: a truth test loses the match + if (DAT_UnitsState::instance.hasEngineerSelected != TRUE + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::returnFirstSelectedEngineer, DAT_UnitsState::ptr)() + == 0) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + break; + // default's body again: "case 217: default:" drops 217's own jump table entry + case OpenSHC::Rendering::Enums::GDI_INTERFACE_ICONS_2_217_PIC_303: + if (DAT_UnitsState::instance.hasEngineerSelected + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + break; + default: + // one || test: as two ifs the match is lost + if (DAT_UnitsState::instance.hasEngineerSelected + || MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getUnitTypeOfFirstSelectedUnit, DAT_UnitsState::ptr)() + == Map::Units::UT_INVALID) { + DAT_ButtonUnknownZero::instance = TRUE; + return; + } + break; + } + MACRO_CALL(General_Func:: + MenuItemRenderFunction_General_RenderCurrentButtonWithPossibleAlphaTexOnScreenMenuSurface)(); + DAT_ButtonUnknownZero::instance = FALSE; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b62540e14f..f9a60c20c4 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -12943,7 +12943,7 @@ SHC_3BB0A8C1_0x004387D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00438B60 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00438BE0 | 0.0% | Pending +SHC_3BB0A8C1_0x00438BE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0043907C | 0.0% | Pending