Skip to content

UI: the credits scroll's fade and the win/defeat window at 100% - #437

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round26-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round26-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Two render functions at 100%, moved out of our open draft #360, both from gynt's drafts. No header changes.

Function Address Before reccmp raw Normalised
MenuModals::CreditsScroll::MenuModalRenderFunction_CreditsScroll 0x004AAB50 41.0% in the draft PR (be9edd1, gynt's code, not reworked); gynt's own 40.8%, "allocator only" (the status line of his fork's reimpl/UI-MenuModals-development, from 934219c at 40.84%; a4ce8c2 at 37.8% is on sourcehold's reimpl/UI-MenuModals; the newest commit on the file, 1eaeddd, is on his fork's reimpl/UI-MenuItems-development) 100.0 100.0
DisplayElements::RenderInGameWinDefeatWindowDisplayElement 0x004AFB30 82.0% in the draft PR (d0fbcd5, gynt's code); gynt's own 81.0%, "security buffer, reg naming" (the status line of his fork's reimpl/UI-DisplayElements-pr-development, from 7a4656b at 80.97%; before it 3c85538 at 65.16% and 7648a6d at 76.62%; 158fe8d at 87.80% was reverted in 94926b6; the newest commit on the file there is ff0d33f) 100.0 100.0

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 (MapEditorLandscaping_GeneralButtons handler) is left out and its resolver stays off, as in main's baseline.
  • Narrow build: the two files alone read 100.0 raw and normalised each.
  • Full build: an all-on build of this head's tree on main 3458fea: 0 failed of 2,425 compile steps, 99.58 over 2,441 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). Both functions read 100.0 raw and normalised, with no diff entry.
    • Two functions on main rise to 100.0 raw and normalised, because they store the two functions' addresses and those now resolve: Global::Init::Constructor_MenuModal_CreditsScroll (0x0059BDA0, 93.8 / 92.3 before) and Global::Init::CreateInGameWinDefeatWindowDisplayElement (0x0059C540, 88.9 / 88.9 before).
    • Two functions on main read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) reads 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all. In this build's layout a symbol of our DLL lies at exactly the address 0x11000000, so reccmp prints the constant as that symbol's name on our side. The instructions are the same on both sides. UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435's text describes the same thing for its build.
    • The average: unrounded it is 99.5819 here against 99.5759 for main.
    • Every other main function reads exactly what it read before.
  • Checks: openshc-pr-check gives 0 ERROR and 5 CHECK (below). openshc-pr-fix finds nothing to change.
  • Not run in game.

What got them to 100%

Both drafts still had the decompiler's text: counters stepped by hand and pointers walking arrays. Tuning that text had stopped paying. Both functions are now written from the original's listing, with the loop variable as the only variable, and the compiler makes the counters and the pointers itself.

The credits scroll's fade (0x004AAB50)

The scroll fades to black towards its top and its bottom edge, over 30 rows of four pixels, and each row over its 20 leftmost columns. The draft had 21 counters in locals for that, each stepped by hand, and all of it twice.

  • The formula. The counters' start values and steps give the blend of column c in row r: (r - 30) * (c + 1) / 20 + 32. Written that way, the compiler makes the same counters, down to the one it does not make: column 3's product 4 * (r - 30) comes from the row's y, which also steps by four (mov edx, [esp + 0x60]; lea ecx, [edx + esi] at 0x004AACAA).
  • Each column is its own call. As a loop over the columns the compiler keeps the loop (102 instructions against the original's 709), so the 20 calls are written out.
  • The first column divides the other way round: 32 - (30 - r) / 20. The original keeps 30 - r as a counter of its own for it (0x004AAC27, used at 0x004AAC38) and leaves the loop by the second column's counter (0x004AAFD5). With the first column in the others' form, the compiler has no such counter and ends the loop by counting down (700 instructions).
  • One row variable for both loops. The original has both loops' row counter in [esp + 0x10]. With a counter of its own in each loop, the second loop's 20 counters land one slot off (91.4%).

The win/defeat window (0x004AFB30)

The draft walked three pointers through the players' arrays and kept the window's y in a second variable.

  • The window's y is the parameter, counted on. posY += -28 - height / 2, with posY used from there on, gives the original's mov edx, -28; sub edx, eax; add esi, edx (0x004AFC25). With the sum in a variable of its own the compiler folded it into two subtractions, in every form tried.
  • The arrays are indexed by the player. The compiler makes the draft's three pointers itself, in the original's registers. The loop ends at the array's length, countof(playerIsAlive), which is 9 (the jl at 0x004AFD7D follows a compare with the array's end, 0x0117EF52).
  • The count of the players alive is a loop too. The compiler unrolls it into the original's eight tests.
  • "Wins" behind each name: its colour and its x are const locals in front of the call, the colour first. The original loads the colour and works out x where the call's pushes begin (0x004AFCE1 to 0x004AFCF8). Written in the call, x was finished behind the pushes and in another register.
  • The security cookie. The original has one, and an 8-byte frame (the cookie and the players' pointer), so its source had a buffer whose address was taken. Nothing reads it, so char unused[8]; char* unusedPointer = unused; is a guess at its type and length, and the comment in the file says so. An unused array alone gets no cookie. gynt's 7a4656b found the cookie first, with a ushort[3] whose address went into the loop's pointer; our draft d0fbcd5 had made that a short[3].

No behaviour differs from the drafts, as far as we can tell: they computed the same values the decompiler's way.

The checker's 5 CHECKs

All five are in the credits scroll's file.

What Why it stays
int row; declared without a value, and the two for loops without their counter in the header (3 CHECKs) one row variable for both loops is what the original's frame has; with int row in each for the second loop's counters land one slot off and the match drops to 91.4%. The comment above the declaration says so
the 19 calls for columns 1 to 19, which differ only in numbers stepping evenly, in each of the two loops (2 CHECKs) as a loop over the columns the compiler keeps a loop where the original has the calls written out (102 instructions against 709). The comment above the calls says so

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 20, 2026 13:00
CreditsScroll::MenuModalRenderFunction_CreditsScroll, rewritten from the original's listing. gynt's drafts: a4ce8c2
at 37.8% on sourcehold's reimpl/UI-MenuModals and 934219c at 40.84% ("allocator only") on his fork's
reimpl/UI-MenuModals-development; the newest commit on the file, 1eaeddd, is on his fork's
reimpl/UI-MenuItems-development.
Our draft be9edd1 (41.0%, in the draft PR sourcehold#360) was his code, not reworked.

The drafts had the decompiler's text: 21 counters in locals, each stepped by hand, and all of it twice. The listing
shows what they are. The scroll fades to black towards its top and bottom edge over 30 rows of four pixels, each
row over its 20 leftmost columns, and the blend of column c in row r is (r - 30) * (c + 1) / 20 + 32. Written that
way, with the row as the only variable, the compiler makes the same counters by itself, down to the one it does not
make: column 3's product 4 * (r - 30) comes from the row's y, which also steps by four (mov edx, [esp + 0x60];
lea ecx, [edx + esi] at 0x004AACAA).

Three things the match needs:

- Each column is its own call. As a loop over the columns the compiler keeps the loop (102 instructions against
  709), so the 20 calls are written out.

- The first column is 32 - (30 - r) / 20, not (r - 30) / 20 + 32. The original keeps 30 - r as a counter of its own
  for it (0x004AAC27, used at 0x004AAC38) and leaves the loop by the second column's counter (0x004AAFD5). With the
  first column in the others' form the compiler has no such counter and ends the loop by counting down (700
  instructions).

- One row variable for both loops. The original has both loops' row counter in [esp + 0x10]; with a counter of
  its own in each loop, the second loop's 20 counters land one slot off (reccmp 91.4%).

Measured: quick_diff 1.0000 with 709 instructions on both sides, also with the stack slots kept; reccmp 100.0%
(normalized 100.0%) alone, data resolvers and its own resolver on. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
DisplayElements::RenderInGameWinDefeatWindowDisplayElement, rewritten from the original's listing. gynt's drafts are
on his fork's reimpl/UI-DisplayElements-pr-development: 3c85538 at 65.16%, 7648a6d at 76.62% and 7a4656b at
80.97%, which the branch's status line records as 81.0% (158fe8d at 87.80% was reverted in 94926b6; ff0d33f is
the newest commit on the file there). Our draft d0fbcd5 (82.0%, in the draft PR sourcehold#360) was his code.

The drafts walked three pointers through the players' arrays and kept the window's y in a second variable, as the
decompiler shows it. Written with the arrays indexed by the player and the y parameter itself counted on, the
compiler makes the same three pointers, and four differences go:

- The window's y. posY += -28 - height / 2, with posY used from there on, gives the original's mov edx, -28;
  sub edx, eax; add esi, edx (0x004AFC25). With the sum in a variable of its own the compiler folded it into two
  subtractions, in every form.

- The loop over the players ends at the array's length, player < countof(playerIsAlive), which is 9 (the jl at
  0x004AFD7D, behind a compare with the array's end, 0x0117EF52).

- The count of the players alive is a loop as well; the compiler unrolls it into the original's eight tests.

- "Wins" behind each name: its colour and its x are locals in front of the call, the colour first. The original
  loads the colour and works out x where the call's pushes begin (0x004AFCE1 to 0x004AFCF8). Written in the call,
  x was finished behind the pushes and in another register.

The security cookie: the original has one, and an 8-byte frame (the cookie and the players' pointer), so its source
had a buffer whose address was taken. Nothing reads it, so `char unused[8]; char* unusedPointer = unused;` is a
guess at its type and length. An unused array alone gets no cookie. gynt's 7a4656b found the cookie first, with a
ushort[3] whose address went into the loop's pointer; our draft d0fbcd5 had made that a short[3].

Measured: quick_diff 0.9893 with 187 instructions on both sides (what differs is how the two listings print the
cookie's address and an import call); reccmp 100.0% (normalized 100.0%) alone, data resolvers and its own resolver
on. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <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