Repository navigation
One more UI function at 100%: Actions::PlaceUnit - #458
Merged
TheRedDaemon merged 7 commits intoNov 24, 2026
Merged
Conversation
Actions::PlaceUnit, from Gynt's draft on his fork's reimpl/UI-Core-development (10280b1 at 39.4%, from after his closed sourcehold#256, and e584164 at 36.40% before it; sourcehold#256's reimpl/UI-Core has it at 36.4% in a05c452 and 39.0% in 1b11452). The draft has the cases' values (the pictures, the tribe types and the unit types), the calls and their arguments. His 3d17527 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 3458fea; main's two commits since (02fab42, 812d149) add cmake files only, with the new option off. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
TheRedDaemon
requested changes
Nov 22, 2026
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 sourcehold#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 <noreply@anthropic.com>
…e variation's two halves From TheRedDaemon's review of sourcehold#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 <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sourcehold#486 named the flag bits of a floater's variation, so PlaceUnit's eight createFloatingLayerElement calls now spell them: 13 is `FVF_SECOND_PASS | FVF_SET_RENDERED_OWNER | FVF_USE_TOTAL_HEIGHT` and 29 the same with `FVF_RENDERED_OWNER_0`. The four blended calls keep the blend strength in the high half as the number `(24 << 16)`, as sourcehold#486's text writes it. The quick listing is the same as before (376 instructions). Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TheRedDaemon
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One more function at 100%, with one header change (
TileMapState::unitPlacementCountis anint, below; the PR waits for it to reach main) and no line forcmake/compiler-flags-gl.txt. One behaviour fix against the draft (below).Actions::PlaceUnithandles the mapper commands that place units (270 to 286 and the Arab units 350 to 357): while the mouse is dragged it puts the unit's pictures on the floating layer, and once the drag has stopped it spawns the units, paying for them in a siege. It is from gynt's draft on his fork'sreimpl/UI-Core-development(10280b1, 39.4%, from after he closed #256; #256's reimpl/UI-Core has the function at 39.0%, 1b11452), and the commit credits him as co-author.Actions::PlaceUnit/GLlistMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie./GLlist (this file is not on it) and with 0x004314E0 (theMapEditorLandscaping_GeneralButtonshandler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.58 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; since then main has only added files undertools/(3458fea) and the optional function-visibility units (cmake: optional wrapper units built from function-visibility.txt #399, cmake: function-visibility.txt, which callers must see which callees' bodies #398), off by default).PlaceUnitreads 100.0 raw and normalised there too, with no diff entry. WithunitPlacementCountas anint, main's two other users of the field,InitializeBasicMapandMenuItemActionHandler_General_ToolbarButtonPressed, read 100.0 raw and normalised as before. No main function reads differently: every one reads exactly what it read before (unrounded 99.5761 against 99.5759 raw, 99.5494 against 99.5492 normalised; 2,323 against 2,322 functions at 100.0 raw)./GLlist that exist on main, andPlaceUnit.cpp, off the list: 144 functions, average 99.56.PlaceUnitreads 100.0% raw and normalised, with no diff entries. The same build without the file has 143 functions, and each of them reads the same. That build, and the one with the file on the list (below), had an earlier text of this file: the same statements with the masks and the tribe types as numbers, the flag as anint, 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 were main's at 3458fea; main's two commits since (02fab42, 812d149) add cmake files only, with the new option off.InitializeBasicMap,MenuItemActionHandler_General_ToolbarButtonPressed): all three read 100.0% raw and normalised,PlaceUnitoff the list. quick_diff gives the same 376 instructions as before forPlaceUnit, and the same listings for the two users with the header change and without it.PlaceUnitat 100.0% raw and normalised.jmpandlea ecx, [ecx]at 0x004386AB / 0x004386AD), in its order; reccmp's diff for the function is empty. The case labels pair with the original's jump table (26 entries) and index table (88 bytes), case for case.The behaviour fix
cmp [draggingStopped], 0at 0x004384AF). In gynt's drafts the drag branch has no return. On a free tile they drew the allowed pictures and then the refused ones as well (a siege tower all four pairs), and then went on into the placement, so the units were paid for in a siege and spawned while the drag was still going.Read case by case against the jump table, the draft's values (pictures, tribe types, unit types, masks) are the original's.
What got it to 100%
The draft has the cases' values (the pictures, the tribe types and the unit types), the calls and their arguments. Their order and the control flow are rewritten against the original instructions, in three steps on our dev branch (not public): 98.4% and 98.6% (both read with the file on the
/GLlist; quick_diff, which compiles it off the list, gave 0.9774 and 0.9801), then 100.0% off the list.break, since the catapult case follows it.gold >the player's gold, the flag is a dword (BOOLEnum), and the loop over the engineers with pots counts with a signed compare.gmIDfrom the stack slot that the siege cost'sgolduses (mov edi, [esp + 0x24]at 0x00438456): the variable is never set on that path. Withstoneandgolddeclared in each of the two cost blocks, not once at the top, our build puts the unsetgmIDon that same slot (the draft writes that read out as_gmID = local_8,local_8being its gold).int const tile = …stands betweenint imageID = 1;andBOOLEnum canAfford = TRUE;(98.6% to 100.0%). The two share their constant. With the tile's computation between them the constant has to stay alive across it, andeax,ecxandedxare taken there, so the compiler keeps it inedi; its two stores then sink behind the switch's range compare, with the tile's store between them. That is the original:mov edi, 1at 0x00438223, and the stores ofimageID, the tile andcanAffordat 0x00438234, 0x00438238 and 0x0043823C. With both= 1on one side of the computation the constant is ineaxand has to be stored before the command is loaded. The order of the original's three stores is what gave the order of the three statements away.quick_diff reads 0.9987: 376 instructions against 377. The one left is an alignment
jmpin front of the pots loop (0x004386AB), which quick_diff does not lay out; the real build has it.No line for
cmake/compiler-flags-gl.txtedxinstead ofecxat 0x004381E3), andecxandedxtrade roles from there on: it is the/GLbuild's register choice, not a callee's body.getBuildingCostandprocessResourceLoss, do not have to be visible: quick_diff gives the same instructions with and without their bodies in the unit. So it needs no line incmake/function-visibility.txt(cmake: function-visibility.txt, which callers must see which callees' bodies #398) either.Header change
TileMapState::unitPlacementCountis anint(it wasundefined4; main has it since the Ghidra sync), in a commit of its own (e0ade4b). The original uses it as a signed 32-bit count in all six places that touch it:InitializeBasicMap(0x0042F0CD),MenuItemActionHandler_MapEditorProperties_MainButtons(0x0044309F) andMenuItemActionHandler_General_ToolbarButtonPressed(0x00444647) store 1 into it;PlaceUnitpasses it tospawnUnitsIntoNewTribeas that function'sintunit count (0x00438659) and loops over it with signed compares (jleat 0x004386A9,jlat 0x004386D1). With it the cast in the pots loop is gone. Its two other users on main read 100.0% with it (above).Main's declaration check fails on any change to a header under
src/OpenSHCand labels the PR, so this PR waits for the line to reach main through the Ghidra sync, as the other declaration-change PRs do. Then we merge main in, and the check passes.Kept as they are
fixme:ongmID: for a command outside the switch the original leaves it unset and still compares it and passes it on (above). The code keeps that for the match. The only caller (0x004451C0) jumps here only for 270 to 286 and 350 to 357 (0x00445B1D, 0x00445B34), so the path is never taken in the game.int stone; int gold;without a value, in two blocks (the fourSG 24CHECKs): they are the out-parameters ofgetBuildingCost, and they are declared in both cost blocks for the stack slot above. A comment in the code says so.(UnitType)-1: the valueunitTypestarts with (or esi, ebpat 0x0043822F).UnitTypehas no name for it;UT_NONEis 0. Every case sets the type.int tribeType = -1;: the cases assignAITribeTypenames; -1, for the siege engines that join no tribe, has no name, andspawnUnitsIntoNewTribetakes anint.MapConstants:MC_MAP_XY_LIMIT_MIN_1for the bounds test andMC_MAP_XY_LIMITfor the row length of the tile index.Logic1flags: sea, rocky, building, tree, river, crenel and moat (0x40501481), and for the knight and the siege engines also wall or gatehouse and keep (0x50501581).FloaterVariationFlags(783b497): 13 isFVF_SECOND_PASS | FVF_SET_RENDERED_OWNER | FVF_USE_TOTAL_HEIGHT, and 29 addsFVF_RENDERED_OWNER_0. Avariationhas two halves. The low 16 bits are those flags, and the high 16 bits a signed number: without flag 0x20 and when not 0, the blend strength thatrenderMap's two floater loops (0x004E9EC0, 0x004EC080) pass torenderGMWithBlending. So a placement that is not allowed is the allowed one's pictures drawn blended at 24. The blend strength stays(24 << 16), a number, as Map::Floaters: an enum for the flag bits of a floater's variation #486 writes it; the constants are 0x18001d and 0x18000d.Proposed, not changed here
Floater::variation(andcreateFloatingLayerElement's parameter): flags in the low half, a signed blend strength or mask-image offset in the high half. The flags have names since Map::Floaters: an enum for the flag bits of a floater's variation #486, used here; two shorts would touch every caller ofcreateFloatingLayerElement, so the parameter stays anint.AITT_HOPS_FARMER_UNK(12) is also the fighting monks' tribe type: PlaceUnit's monk case (0x00438317) spawnsUT_E_MONKinto a tribe of type 12. A name that fits both needs a header commit.Other PRs
reimpl/UI-Core, closed) holdsPlaceUnit.cppat 39.0%, and his fork's UI branches carry it at 36.4% to 39.4%. We know of no open PR that holds the file. A branch that does will have an add/add conflict on it and a different status line at 0x004381D0, and can take main's version once this lands.🤖 Generated with Claude Code