From 2507c0a9eacbf43878ebfa1235c053fa64278740 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sun, 22 Nov 2026 04:05:25 +0100 Subject: [PATCH 1/4] reimplement: SHC_3BB0A8C1_0x004381D0 100.0% Reimplemented Actions::PlaceUnit, from Gynt's draft on his fork's reimpl/UI-Core-development (10280b1610 at 39.4%, from after his closed #256, and e584164c31 at 36.40% before it; #256's reimpl/UI-Core has it at 36.4% in a05c4529c5 and 39.0% in 1b114523b6). The draft has the cases' values (the pictures, the tribe types and the unit types), the calls and their arguments. His 3d17527d65 on reimpl/UI-MenuItems-development is another line of this file (36.4% there); this text does not build on it. Rewritten against the original instructions are the order and the control flow: - the cases are in the original's jump-table order: 270 to 280, the Arab units 350 to 357, then 281, 282, 284, 283, 285, 286; the Arab ballista case, last in the draft, gets a break, since the catapult case follows it now; - the default case leaves the body graphic unset, as the original does. The original reads it from the stack slot of the siege cost's gold there (`mov edi, [esp + 0x24]` at 0x00438456), so stone and gold are declared in each of the two cost blocks and not once at the top; - the four floating-layer pairs are alternatives that each return (allowed or not, siege tower or not), where the draft ran them one after another, and then went on into the placement, so it paid for and spawned the units while the drag was still going. That is the one behaviour fix against the draft; - the gold check is gold > the player's gold, the flag a dword (BOOLEnum), and the pots loop counts with a signed compare; - `int const tile = ...` stands between `int imageID = 1;` and `BOOLEnum canAfford = TRUE;`. The two share their constant, which then has to live across the tile's computation, where eax, ecx and edx are taken. So it is in edi, and its two stores sink behind the switch's range compare with the tile's store between them, as in the original (`mov edi, 1` at 0x00438223; the stores at 0x00438234, 0x00438238 and 0x0043823C). With both `= 1` on one side of the computation the constant is in eax and is stored before the command is loaded (98.6%). The decompiler's names became tribeType, secondGmID, secondImageID, imageID, blockingLogicMask, canAfford, stone and gold. The blocking masks are written as main's Logic1 flags and the tribe types as AITribeType names (12, the monks', is AITT_HOPS_FARMER_UNK there). The variation of a refused placement is written 29 + (24 << 16) and 13 + (24 << 16): the allowed one's with 24 in the third byte. No line for cmake/compiler-flags-gl.txt, and it must not get one: the file reads 100.0% off the /GL list and 90.0% (normalised 80.9) on it, in the same build otherwise. quick_diff gives the same instructions with and without the bodies of getBuildingCost and processResourceLoss, its two callees on main's list, in the unit. reccmp in a build of main's whole /GL list (the 133 files that exist) and the file, off the list, with every resolver of those files on: 144 functions, average 99.56. PlaceUnit 100.0% raw and normalised, no differing line. The same build without the file has 143 functions, and each of them reads the same. Both builds (off and on the list) had an earlier text of this file: the same statements with the masks and the tribe types as numbers, the flag as an int, the bounds test around the body, and without most comments. quick_diff gives the same 376 instructions, line for line, for that text and for this one. The sources are main's at 3458fea34; main's two commits since (02fab423c, 812d149696) add cmake files only, with the new option off. Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- src/OpenSHC/UI/Actions/PlaceUnit.cpp | 305 +++++++++++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/Actions/PlaceUnit.cpp diff --git a/src/OpenSHC/UI/Actions/PlaceUnit.cpp b/src/OpenSHC/UI/Actions/PlaceUnit.cpp new file mode 100644 index 0000000000..fbaba7e41d --- /dev/null +++ b/src/OpenSHC/UI/Actions/PlaceUnit.cpp @@ -0,0 +1,305 @@ +#include "../Actions.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Resources/ResourceType.hpp" +#include "OpenSHC/IO/Graphics/GmID.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MouseState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace UI { + + using OpenSHC::IO::Graphics::GmID; + using OpenSHC::Map::Units::UnitType; + using OpenSHC::WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x004381D0 + void Actions::PlaceUnit() + { + if (DAT_TileMapState::instance.DAT_ClickedTileX > 399 || DAT_TileMapState::instance.DAT_ClickedTileY > 399 + || DAT_ViewportRenderState::instance + .DAT_BinaryTileMap400x400[DAT_TileMapState::instance.DAT_ClickedTileY * 400 + + DAT_TileMapState::instance.DAT_ClickedTileX] + == 0) { + return; + } + int secondImageID = -1; + int tribeType = -1; + UnitType unitType = (UnitType)-1; + int imageID = 1; + // declared between the two that start at 1: with both on one side of this line the match is lost + int const tile + = DAT_ViewportRenderState::instance.translationMatrix[DAT_TileMapState::instance.DAT_ClickedTileY] + .addXgetTile + + DAT_TileMapState::instance.DAT_ClickedTileX; + BOOLEnum canAfford = TRUE; + uint blockingLogicMask = OpenSHC::Map::LogicHelpers::L_SEA | OpenSHC::Map::LogicHelpers::L_ROCKY + | OpenSHC::Map::LogicHelpers::L_BUILDING | OpenSHC::Map::LogicHelpers::L_TREE + | OpenSHC::Map::LogicHelpers::L_RIVER | OpenSHC::Map::LogicHelpers::L_CRENEL_VARIATIONUnk + | OpenSHC::Map::LogicHelpers::L_MOAT; + // fixme: for a command outside the switch (the one caller sends none) the original leaves gmID unset and + // still reads it below + GmID gmID; + GmID secondGmID; + // the cases in the order of the original's code: the Arab units stand between 280 and 281 + switch (DAT_TileMapState::instance.currentMapperCommand) { + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARCHERS: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARCHER; + tribeType = OpenSHC::AI::Tribes::AITT_ARCHERS; + unitType = OpenSHC::Map::Units::UT_E_ARCHER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_SPEARMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_SPEARMAN; + tribeType = OpenSHC::AI::Tribes::AITT_SPEARMEN; + unitType = OpenSHC::Map::Units::UT_E_SPEAR; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_PIKEMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_PIKEMAN; + tribeType = OpenSHC::AI::Tribes::AITT_PIKEMEN; + unitType = OpenSHC::Map::Units::UT_E_PIKE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_MACEMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_MACEMAN; + tribeType = OpenSHC::AI::Tribes::AITT_MACEMEN; + unitType = OpenSHC::Map::Units::UT_E_MACE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_XBOWMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_CROSSBOWMAN; + tribeType = OpenSHC::AI::Tribes::AITT_CROSSBOWMEN; + unitType = OpenSHC::Map::Units::UT_E_XBOW; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_SWORDSMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_SWORDSMAN; + tribeType = OpenSHC::AI::Tribes::AITT_SWORDSMEN; + unitType = OpenSHC::Map::Units::UT_E_SWORD; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_KNIGHTS: + gmID = OpenSHC::IO::Graphics::GID_BODY_KNIGHT; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_KNIGHT_TOP; + tribeType = OpenSHC::AI::Tribes::AITT_KNIGHTS; + unitType = OpenSHC::Map::Units::UT_E_KNIGHT; + secondImageID = 1; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_LADDERMEN: + gmID = OpenSHC::IO::Graphics::GID_BODY_LADDER_BEARER; + tribeType = OpenSHC::AI::Tribes::AITT_LADDERMEN; + unitType = OpenSHC::Map::Units::UT_E_LADDER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS: + gmID = OpenSHC::IO::Graphics::GID_BODY_SIEGE_ENGINEER; + tribeType = OpenSHC::AI::Tribes::AITT_ENGINEERS; + unitType = OpenSHC::Map::Units::UT_E_ENGINEER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS_POTS: + gmID = OpenSHC::IO::Graphics::GID_BODY_SIEGE_ENGINEER; + tribeType = OpenSHC::AI::Tribes::AITT_ENGINEERS; + unitType = OpenSHC::Map::Units::UT_E_ENGINEER; + imageID = 629; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_MONKS: + gmID = OpenSHC::IO::Graphics::GID_BODY_FIGHTING_MONK; + tribeType = OpenSHC::AI::Tribes::AITT_HOPS_FARMER_UNK; + unitType = OpenSHC::Map::Units::UT_E_MONK; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_BOW: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_SHORTBOW; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_ARCHER; + unitType = OpenSHC::Map::Units::UT_A_ARCHER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SLAVE: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_SLAVE; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_SLAVE; + unitType = OpenSHC::Map::Units::UT_A_SLAVE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SLINGER: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_SLINGER; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_SLINGER; + unitType = OpenSHC::Map::Units::UT_A_SLINGER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_ASSASIN: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_ASSASIN; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_ASSASSIN; + unitType = OpenSHC::Map::Units::UT_A_ASSASSIN; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_HORSEMAN: + gmID = OpenSHC::IO::Graphics::GID_BODY_HORSE_ARCHER; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_HORSE_ARCHER; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_HORSE_ARCHER_TOP; + unitType = OpenSHC::Map::Units::UT_A_HARCHER; + secondImageID = 1; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_SWORDSMAN: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_SWORDSMAN; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_SWORDSMAN; + unitType = OpenSHC::Map::Units::UT_A_SWORDSMAN; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_GRENADIER: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_GRENADIER; + tribeType = OpenSHC::AI::Tribes::AITT_ARAB_FIRETHROWER; + unitType = OpenSHC::Map::Units::UT_A_FIRETHROWER; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_ARAB_BALLISTA: + gmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_BALLISTA; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_ARAB_BALLISTA; + secondImageID = 169; + tribeType = OpenSHC::AI::Tribes::AITT_FIRE_BALLISTA; + unitType = OpenSHC::Map::Units::UT_S_FBALLISTA; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_CATAPULTS: + gmID = OpenSHC::IO::Graphics::GID_BODY_CATAPULT; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_CATAPULT; + unitType = OpenSHC::Map::Units::UT_S_CATAPULT; + secondImageID = 225; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_TREBUCHETS: + gmID = OpenSHC::IO::Graphics::GID_BODY_TREBUTCHET; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_TREBUTCHET; + unitType = OpenSHC::Map::Units::UT_S_TREBUCHET; + secondImageID = 9; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_SIEGE_TOWERS: + gmID = OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER; + secondImageID = 9; + unitType = OpenSHC::Map::Units::UT_S_TOWER; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_BATTERING_RAMS: + gmID = OpenSHC::IO::Graphics::GID_BODY_BATTERING_RAM; + secondGmID = OpenSHC::IO::Graphics::GID_BODY_BATTERING_RAM; + secondImageID = 77; + unitType = OpenSHC::Map::Units::UT_S_BATTERINGRAM; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_PORTABLE_SHIELDS: + gmID = OpenSHC::IO::Graphics::GID_BODY_SHIELD; + unitType = OpenSHC::Map::Units::UT_S_SHIELD; + blockingLogicMask + |= OpenSHC::Map::LogicHelpers::L_WALL_OR_GATEHOUSE | OpenSHC::Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + break; + case OpenSHC::Commands::M_MAPPER_PEOPLE_TUNNELERS: + gmID = OpenSHC::IO::Graphics::GID_BODY_TUNNELOR; + tribeType = OpenSHC::AI::Tribes::AITT_TUNNELERS; + unitType = OpenSHC::Map::Units::UT_TUNNELER; + break; + } + // in a siege the units cost gold + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT) { + // declared in both cost blocks: the unset gmID then shares gold's stack slot, as in the original + int stone; + int gold; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingCost, DAT_BuildingsState::ptr)( + DAT_TileMapState::instance.currentMapperCommand, &stone, &gold); + if (gold > DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .startResources[OpenSHC::Game::Resources::RT_GOLD]) { + canAfford = FALSE; + } + } + // while the drag goes on: only the pictures on the floating layer + if (!DAT_MouseState::instance.draggingStopped) { + if ((DAT_TileMapState::instance.LogicLayer[tile] & blockingLogicMask) == 0 && canAfford) { + if (gmID == OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER) { + if (secondImageID > 0) { + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 71, tile, 29); + } + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)( + OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER, imageID, 0, 0, tile, 29); + return; + } + if (secondImageID > 0) { + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 0, tile, 13); + } + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(gmID, imageID, 0, 0, tile, 13); + return; + } + // not allowed here: the same pictures with 24 in the third byte of the variation + if (gmID == OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER) { + if (secondImageID > 0) { + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 71, tile, 29 + (24 << 16)); + } + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)( + OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER, imageID, 0, 0, tile, 29 + (24 << 16)); + return; + } + if (secondImageID > 0) { + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 0, tile, 13 + (24 << 16)); + } + MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(gmID, imageID, 0, 0, tile, 13 + (24 << 16)); + return; + } + // the drag has stopped: pay and place + if ((DAT_TileMapState::instance.LogicLayer[tile] & blockingLogicMask) == 0 && canAfford) { + if (DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_SIEGE_THAT) { + int stone; + int gold; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::getBuildingCost, + DAT_BuildingsState::ptr)(DAT_TileMapState::instance.currentMapperCommand, &stone, &gold); + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::processResourceLoss, DAT_BuildingsState::ptr)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, OpenSHC::Game::Resources::RT_GOLD, gold, 0); + } + if (tribeType >= 0) { + int const tribeID = MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::spawnUnitsIntoNewTribe, + DAT_TribesState::ptr)(-1, tribeType, DAT_TileMapState::instance.DAT_ClickedTileX, + DAT_TileMapState::instance.DAT_ClickedTileY, DAT_GameSynchronyState::instance.currentPlayerSlotID, + unitType, OpenSHC::Map::Units::UT_NONE, DAT_TileMapState::instance.unitPlacementCount, 0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = OpenSHC::Map::Units::STBT_1; + DAT_TribesState::instance.tribes[tribeID].initialMoveOrderGiven = 1; + if (DAT_TileMapState::instance.currentMapperCommand + == OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS_POTS) { + // the engineers with pots start with oil in them + for (int i = 0; i < (int)DAT_TileMapState::instance.unitPlacementCount; ++i) { + DAT_UnitsState::instance + .units[MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, + DAT_TribesState::ptr)(tribeID, i)] + .resourceToDeposit = 1; + } + } + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, + DAT_GameSynchronyState::instance.currentPlayerSlotID, + DAT_TileMapState::instance.DAT_ClickedTileX * 8, DAT_TileMapState::instance.DAT_ClickedTileY * 8, + DAT_TileMapState::instance.HeightLayer[tile], unitType); + } + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b62540e14f..2f5b45f5da 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -12707,7 +12707,7 @@ SHC_3BB0A8C1_0x004381C4 | 0.0% | Pending SHC_3BB0A8C1_0x004381C8 | 0.0% | Pending -SHC_3BB0A8C1_0x004381D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004381D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004386B0 | 0.0% | Pending From e0ade4b9389ce35ec15aa0b37fa4077546ff2b45 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sun, 22 Nov 2026 22:25:30 +0100 Subject: [PATCH 2/4] headers: TileMapState::unitPlacementCount is an int The original uses it as a signed 32-bit count in all six places that touch it. InitializeBasicMap (0x0042F0CD), MenuItemActionHandler_MapEditorProperties_MainButtons (0x0044309F) and MenuItemActionHandler_General_ToolbarButtonPressed (0x00444647) store 1 into it. PlaceUnit passes it to spawnUnitsIntoNewTribe as its int unit count (0x00438659) and loops over it with signed compares: `jle` when it is not above 0 (0x004386A9) and `jl` for the loop bound (0x004386D1). The header had it as undefined4, so PlaceUnit's loop needed an (int) cast (TheRedDaemon's review of #458). The two users on main, InitializeBasicMap and ToolbarButtonPressed, compile to the same listing with it (quick_diff, ours against ours). Co-Authored-By: Claude Opus 5.5 --- src/OpenSHC/Map/TileMapState.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/TileMapState.hpp b/src/OpenSHC/Map/TileMapState.hpp index 757b20e55c..6405f13c6b 100644 --- a/src/OpenSHC/Map/TileMapState.hpp +++ b/src/OpenSHC/Map/TileMapState.hpp @@ -144,7 +144,7 @@ namespace Map { int editorActiveBrush; // 0x005548E0 length: 4 MappersEnum currentMapperCommand; // 0x005548E4 length: 4 undefined4 DAT_BuildingSize; // 0x005548E8 length: 4 - undefined4 unitPlacementCount; // 0x005548EC length: 4 + int unitPlacementCount; // 0x005548EC length: 4 BOOLEnum mapperMax; // 0x005548F0 length: 4 undefined4 rockFlagStartNumber; // 0x005548F4 length: 4 int unknownBrushRelated; // 0x005548F8 length: 4 From add0b814bbe15499cf9cff2ad1f5393bc18ddabd Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sun, 22 Nov 2026 22:25:30 +0100 Subject: [PATCH 3/4] PlaceUnit: the map-size constants, no cast on the placement count, the variation's two halves From TheRedDaemon's review of #458: - The bounds test and the tile index use Map::MC_MAP_XY_LIMIT_MIN_1 and Map::MC_MAP_XY_LIMIT in place of 399 and 400. - With unitPlacementCount an int (the commit before), the loop drops its (int) cast. - The variation's high half is a blend strength, not a third byte: the comment says so, and the variations are written 13 | (24 << 16) and 29 | (24 << 16). createFloatingLayerElement stores the dword whole (0x004E64BC). renderMap's two floater loops (0x004E9EC0, 0x004EC080) test bits of its low half and take the high half as a signed number (sar 16): with flag 0x20 an offset added to the image ID for renderGMWithAlphaMask, otherwise, when not 0, the blend strength for renderGMWithBlending. The listing is unchanged (quick_diff, ours against ours: the same 376 instructions), and a real build of main's /GL list with this file off the list reads 100.0% raw and normalised. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- src/OpenSHC/UI/Actions/PlaceUnit.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/UI/Actions/PlaceUnit.cpp b/src/OpenSHC/UI/Actions/PlaceUnit.cpp index fbaba7e41d..26c8e84497 100644 --- a/src/OpenSHC/UI/Actions/PlaceUnit.cpp +++ b/src/OpenSHC/UI/Actions/PlaceUnit.cpp @@ -10,6 +10,7 @@ #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/IO/Graphics/GmID.hpp" #include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" @@ -34,10 +35,11 @@ namespace UI { // FUNCTION: STRONGHOLDCRUSADER 0x004381D0 void Actions::PlaceUnit() { - if (DAT_TileMapState::instance.DAT_ClickedTileX > 399 || DAT_TileMapState::instance.DAT_ClickedTileY > 399 - || DAT_ViewportRenderState::instance - .DAT_BinaryTileMap400x400[DAT_TileMapState::instance.DAT_ClickedTileY * 400 - + DAT_TileMapState::instance.DAT_ClickedTileX] + if (DAT_TileMapState::instance.DAT_ClickedTileX > OpenSHC::Map::MC_MAP_XY_LIMIT_MIN_1 + || DAT_TileMapState::instance.DAT_ClickedTileY > OpenSHC::Map::MC_MAP_XY_LIMIT_MIN_1 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[DAT_TileMapState::instance.DAT_ClickedTileY + * OpenSHC::Map::MC_MAP_XY_LIMIT + + DAT_TileMapState::instance.DAT_ClickedTileX] == 0) { return; } @@ -244,23 +246,23 @@ namespace UI { DAT_ViewportRenderState::ptr)(gmID, imageID, 0, 0, tile, 13); return; } - // not allowed here: the same pictures with 24 in the third byte of the variation + // not allowed here: the same pictures drawn blended (the high half of the variation is a blend strength) if (gmID == OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER) { if (secondImageID > 0) { MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 71, tile, 29 + (24 << 16)); + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 71, tile, 29 | (24 << 16)); } MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)( - OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER, imageID, 0, 0, tile, 29 + (24 << 16)); + OpenSHC::IO::Graphics::GID_BODY_SIEGE_TOWER, imageID, 0, 0, tile, 29 | (24 << 16)); return; } if (secondImageID > 0) { MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 0, tile, 13 + (24 << 16)); + DAT_ViewportRenderState::ptr)(secondGmID, secondImageID, 0, 0, tile, 13 | (24 << 16)); } MACRO_CALL_MEMBER(OpenSHC::Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(gmID, imageID, 0, 0, tile, 13 + (24 << 16)); + DAT_ViewportRenderState::ptr)(gmID, imageID, 0, 0, tile, 13 | (24 << 16)); return; } // the drag has stopped: pay and place @@ -284,7 +286,7 @@ namespace UI { if (DAT_TileMapState::instance.currentMapperCommand == OpenSHC::Commands::M_MAPPER_PEOPLE_ENGINEERS_POTS) { // the engineers with pots start with oil in them - for (int i = 0; i < (int)DAT_TileMapState::instance.unitPlacementCount; ++i) { + for (int i = 0; i < DAT_TileMapState::instance.unitPlacementCount; ++i) { DAT_UnitsState::instance .units[MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, DAT_TribesState::ptr)(tribeID, i)] From 12ec6c6ac2fe2fb234883f11fd4ce337e5268797 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Mon, 23 Nov 2026 06:20:31 +0100 Subject: [PATCH 4/4] style: PlaceUnit starts its unit type at UnitType's UT_INVALID The local unit type started as `(UnitType)-1`; main's UnitType names -1 UT_INVALID, so it is the name now. No other change. Measured: with the precompiled header, quick_diff gives the same listing for the pushed head's text and this one. A narrow real build (main's /GL list and this file, with this PR's header change, the struct resolvers and the file's function resolver on, sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie) reads 100.0% raw and normalised with no differing line, and every other function reads as in the same build without the file. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- src/OpenSHC/UI/Actions/PlaceUnit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/UI/Actions/PlaceUnit.cpp b/src/OpenSHC/UI/Actions/PlaceUnit.cpp index 26c8e84497..979a93f6e4 100644 --- a/src/OpenSHC/UI/Actions/PlaceUnit.cpp +++ b/src/OpenSHC/UI/Actions/PlaceUnit.cpp @@ -45,7 +45,7 @@ namespace UI { } int secondImageID = -1; int tribeType = -1; - UnitType unitType = (UnitType)-1; + UnitType unitType = OpenSHC::Map::Units::UT_INVALID; int imageID = 1; // declared between the two that start at 1: with both on one side of this line the match is lost int const tile