Skip to content

UI: the allies screen's render function at 100% - #447

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round36-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round36-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, moved out of our open draft #328, from gynt's draft. No header changes. Behaviour fixes against the draft (below).

Function Address Before reccmp raw Normalised
MenuItems::Allies::MenuItemRenderFunction_Allies_Main 0x004AC7A0 52.8% (our draft in #328); gynt's best 52.3% 100.0% 100.0%

Our draft was gynt's code with a mechanical pass; gynt's best, 52.3%, is 1f983fe on his fork's reimpl/UI-MenuItems-development.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed). In the full build 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) is left out and its resolver stays off, as in main's baseline.
  • Narrow build: this file (as committed but for comments and two forms that compile to the same listing line for line), together with eight of main's files it calls, their resolvers on: renderGM (0x00455300), drawBitmapFace (0x0044CD40), renderGMWithBlending (0x00455390), renderGMWithAlphaMask (0x004557B0), renderFacesSmallUnk (0x0044CE00), isAIPlayer (0x004010B0), RenderCurrentNotActiveButtonWithPossibleAlphaTexOnCurrentSurfaceUnk (0x004640D0) and RenderButtonImageWithBlending (0x004641A0), which 110 and 111 tail-call. The function reads 100.0 raw and normalised, with no diff line. The first six stay at 100.0.
    • The last two read 54.8 and 58.3 in that narrow build. Both are on main's /GL list, and their own callees are not in the build. In the full build both read 100.0, as on main.
    • With only this file, as committed, in the build and the callees' resolvers off, the function reads 100.0 (100.0 normalised) too.
  • Full build: an all-on build of the same code on main 3458fea: 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). The new function reads 100.0 raw and normalised there too, with no diff entry.
  • Instructions and jumps: all 702 instructions are the original's, and all 74 local jumps land on the same instruction. We compared them with scripts. The function has no switch table.
  • Checks: our local style checker gives 0 ERROR and 11 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

The behaviour fixes

The function draws the allies screen, one menu item per call: 1 to 6 an ally's portrait with its signs, 10 the panel with the selected ally's state, 200 the help line, 110 and 111 the selected ally's button, the rest plain buttons. Most of the draft's ranges had no return, so items ran on into the code for others:

  • Without allies the draft set the two button flags and went on to draw the item. The original returns (0x004AC7D6).
  • 110 and 111 ran on after their button into the code for 200 and the rest, which drew the plain button too. The original leaves through the first (a tail jump at 0x004AD0FC).
  • 200 ran on after the help line, cleared DAT_ButtonUnknownZero and drew the plain button. The original returns after the text (0x004AC841).
  • 7 to 19 other than 10, and 22 while the mouse is on it, drew the panel. The original returns for them (jne at 0x004ACB92 to the return at 0x004ACB89, and 0x004AD0AA).
  • A portrait item past the number of allies cleared DAT_ButtonUnknownZero and went on with that index. The original returns (jge at 0x004AC86C).
  • An unselected ally's portrait returned after its picture when it showed lord icon 0 or 1 or an AI lord, so the signs on it (the resource it asks for, help needed, its orders) were not drawn. The original goes on to them (0x004ACA42 falls into 0x004ACA47).
  • The selected ally's portrait returned after its frame: no signs either (0x004ACA77 falls into 0x004ACA7C).
  • The ally's request that you attack: with a player as the target the draft drew the face and its frame, then the target's picture a second time and an AI lord's picture over them. The original returns after the frame (0x004AD012).

Each shows in the draft's own listing. Read block by block against the new code, nothing else differs.

What got it to 100%

The function is written from the original's listing now. Our draft read 704 instructions against the original's 702, with 624 lines differing in our compare. What decided the match, each measured with one thing changed (differing lines of that compare; the final code has 0):

  • The parameter is reused. The portrait steps it to the ally's index, copies that, and then stores the ally's player ID in it; the panel stores the selected ally's player ID in it too. The code reuses button for both, as AlliesOrder_Main on main does. A variable of its own for the player: 281; for the index: 169; for the panel's player: 13.
  • The resource asked for is counted from wood on, in a statement of its own (resource -= RT_WOOD), in both places. Inside the test: 155 and 123.
  • The code for the items above 19 stands after the portraits' and the panel's (before them: 62), and the panel's after the portraits' (before them: 577).
  • The portrait's blend by if. As a conditional expression: 88.
  • An unselected portrait's picture with a call in each arm. With its number in a local and one call: 80.
  • Smaller: the panel's resource test < 0 with the "no request" text first (the other way: 8), the player's ID read once into a local (twice: 12), the target picture's x before its y (20), new variables for the last picture's position rather than changing x and y (4), the frame's row number with no first value (7 as its first value: 36), the target's picture called in each arm of its test, in both places where a target is drawn (called once before the test: 73 and 41).

These gave the same code, and we took the plainer form: 110 and 111 first with a return (the original has their code at the end), their flag by an early return or by if / else, the AI lord's text number's operands in either order, the frame's row number by if / else or by conditional expressions (main's style bot flags those), the range 0 to 19 as two compares or as one unsigned compare, the step to the index as button-- or -= 1, the frame's middle columns skipped by an else-if or by a nested if, button written directly or named through a reference, and 20 to 22 as an else-if chain or as ifs that each return.

Things in the file that look odd and are the original's

  • button holds the ally's index and then its player ID. The original keeps both in the parameter's register and stack slot (see above), and the code reuses button for them, as AlliesOrder_Main on main does.
  • The selected ally is tested three times (index == DAT_LastTeamMemberIndex): for the plain portrait, for its frame, and for the signs' blend. The original tests it three times too (0x004AC88E, 0x004ACA47, 0x004ACA7E).
  • Pitch is shown as partial pitch (RT_PITCH becomes RT_PARTIALPITCH) before the resource's picture and name are chosen, and the numbers count from RT_WOOD.
  • The panel's middle rows draw only their first and last tile (continue for the others); the inside is the blended black box drawn after the frame.

Header findings (nothing changed here)

  • DAT_00df42b0 is the help sign's blink frame: MenuModalRenderFunction_Allies steps it between 0 and 1, and this function draws picture 578 or 579 with it.
  • DAT_00df51f4 says the help line is shown and DAT_00df51f0 is its text number in TEXT_ALLIES (5, 6 or 7: "Set Orders", "Send Goods", "Request Goods"): items 20 to 22 set both while the mouse is on them, item 1 clears the first. Every reference to either in the whole image is in this function (one read and four stores each), and DAT_00df51f4 only ever holds 0 or 1, so it can be proposed as a BOOLEnum.
  • PlayerData::isNotNervousByEnemyTroopValue: when it is set, the panel writes text 4 of TEXT_ALLIES, "Help Needed!" in the game's cr.tex, and the portrait gets the help sign. A lead: the name may say the opposite of what the field means.
  • PlayerData's request fields: requestedGoodsArray1Unk / 2Unk (own player's, by the ally's ID): the resource the ally asks for and its amount; requestStateUnk: 0 none, 1 attack, 2 defend; playerID_askerUnk (own player's): the ally that asked you to attack.

The checker's 11 CHECKs

What Why it stays
the placeholder names DAT_00df51f4 (5), DAT_00df51f0 (4), DAT_00df42b0 (1) Ghidra's names; see the header findings
int row; with no first value needed for the match: with 7 as its first value, 36 lines differ

🤖 Generated with Claude Code

Allies::MenuItemRenderFunction_Allies_Main, rewritten from the original's listing. gynt's draft: 1f983fe at 52.27%
("reordered stores") on his fork's reimpl/UI-MenuItems-development, whose status line records 52.3% (the newest
commit on the file there is dd1a7c4, a style commit); his reimpl/UI-MenuItems has 8533b17 at 47.09%, status line
47.1% ("Style pass, asm differs"). Our draft (89a680f, then f8721e5 and 07a33e2; 52.8% in the draft PR sourcehold#328)
was his code with a mechanical pass.

The function draws the allies screen, one menu item per call, chosen by the item's number: 1 to 6 an ally's portrait
with its signs (the resource it asks for, help needed, its orders); 10 the panel, its frame tile by tile and the
selected ally's name, request, orders and help text; 200 the help line, whose text items 20 to 22 choose while the
mouse is on them; the items above 19 and -1 a plain button; 110 and 111 the selected ally's button, shown while it
needs help or has asked you to attack. Without allies every item but -1 is off.

Behaviour fixes against the draft, each in the draft's own listing. Most of its ranges were separate ifs without a
return, so:

- Without allies it set the two flags and went on to draw the item. The original returns (0x004AC7D6).
- 110 and 111 ran on after their button into the code for 200 and the rest, which drew the plain button too. The
  original leaves through the first (a tail jump at 0x004AD0FC).
- 200 ran on after the help line, cleared ButtonUnknownZero and drew the plain button. The original returns after
  the text (0x004AC841).
- 7 to 19 other than 10, and 22 while the mouse is on it, drew the panel. The original returns for them (jne at
  0x004ACB92 to the return at 0x004ACB89, 0x004AD0AA).
- A portrait item past the number of allies cleared ButtonUnknownZero and went on with that index. The original
  returns (jge at 0x004AC86C).
- An unselected ally's portrait returned after its picture when it showed lord icon 0 or 1 or an AI lord, so the
  signs on it were not drawn. The original goes on to the signs (0x004ACA42 falls into 0x004ACA47).
- The selected ally's portrait returned after its frame: no signs either (0x004ACA77 falls into 0x004ACA7C).
- The ally's request that you attack: with a player as the target the draft drew the face and its frame, then the
  target's picture a second time and an AI lord's picture over them. The original returns after the frame
  (0x004AD012).

What decided the match. The counts are differing lines of our own exact listing compare against the original's 702
instructions (operands with the original's addresses and every jump's landing), each with that one thing changed
from the final code, which has 0:

- The parameter is reused: the portrait steps it to the ally's index, copies that, and then stores the ally's player
  ID in it; the panel stores the selected ally's player ID in it too. A variable of its own for the player: 281; for
  the index: 169; for the panel's player: 13. The code reuses `button` for both, as AlliesOrder_Main on main does.
- The resource asked for is counted from wood on in a statement of its own (`resource -= RT_WOOD`), in both places.
  Inside the test: 155 and 123.
- The code for the items above 19 stands after the portraits' and the panel's (before them: 62), and the panel's
  after the portraits' (before them: 577).
- The portrait's blend (8 for the unselected ones) by if. As a conditional expression: 88.
- An unselected portrait's picture with a call in each arm. Its number in a local and one call: 80.
- Smaller: the panel's resource test `< 0` with the "no request" text first (the other way: 8), the player's ID read
  once into a local (twice: 12), the target picture's x before its y (20), new variables for the last picture's
  position rather than changing x and y (4), the frame's row number with no first value (7 as its first value: 36),
  the target's picture called in each arm of its test, in both places (called once before the test: 73 and 41).

These gave the same code, and the plainer form stands: 110 and 111 first with a return (the original has their code
at the end), their flag by an early return or by if / else, the AI lord's text number's operands in either order,
the frame's row number by if / else or by conditional expressions, the range test 0 to 19 as two compares or as one
unsigned compare, the step to the index as `button--` or `-= 1`, the frame's middle columns skipped by an else-if or
by a nested if, `button` written directly or named through a reference, and 20 to 22 as an else-if chain or as ifs
that each return.

The text numbers carry their texts from the game's cr.tex (TEXT_ALLIES) as comments. Neither they nor the other
comments change the listing: this code compiles to the same instructions, line for line, as the code the narrow
build with the callees and the full build measured (0aae52a5f's).

Measured in a narrow build on main 3458fea of that code and the eight finished functions it calls, struct
resolvers and their resolvers on: renderGM (0x00455300), drawBitmapFace (0x0044CD40), renderGMWithBlending
(0x00455390), renderGMWithAlphaMask (0x004557B0), renderFacesSmallUnk (0x0044CE00), isAIPlayer (0x004010B0),
RenderCurrentNotActiveButtonWithPossibleAlphaTexOnCurrentSurfaceUnk (0x004640D0) and RenderButtonImageWithBlending
(0x004641A0), which 110 and 111 tail-call. reccmp 100.0, normalized 100.0, no diff line. The two of them on main's
/GL list read lower in that build (RenderCurrentNotActiveButton... 54.8, RenderButtonImageWithBlending 58.3), as
their own callees are not in it. With only this file, as committed, in the list, the callees' resolvers off, the
function reads 100.0 (100.0 normalized) too. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant