Skip to content

UI: the lobby's player list render function at 100% - #446

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round35-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. Seven behaviour fixes against the draft (below).

Function Address Before reccmp raw Normalised
MenuItems::LobbyMenu::MenuItemRenderFunction_LobbyMenu_PlayerListAndNpcButtons 0x00427810 58.8% (our draft in #328); gynt's best 56.5% 100.0% 100.0%

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

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).
  • Narrow build: this file (as committed but for its comments and the four pictures' names, which compile to the same listing line for line), together with five of main's files it calls, their resolvers on: AModalDialogIsActiveButIsNotQuitting (0x00427210), renderGMWithAlphaMask (0x004557B0), renderGMWithBlending (0x00455390), isAIPlayer (0x004010B0) and RenderCurrentButtonToScreenMenuWithBlendingUnk (0x00464370), which five paths tail-call. The function reads 100.0 raw and normalised, with no diff line. The first four stay at 100.0.
    • RenderCurrentButtonToScreenMenuWithBlendingUnk reads 16.9 in that narrow build. It is on main's /GL list, and its own callee is not in the build. In the full build it reads 100.0, as on main.
    • Without the five files in the build the function reads 99.4 (100.0 normalised): four of the five tail jumps then go to a trampoline.
  • Full build: an all-on build of the same code (12b9d934e): 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; 3458fea only adds files under tools/). The new function reads 100.0 raw and normalised there too, with no diff entry, and no function on main moves.
  • Instructions, jumps and switch tables: all 604 instructions are the original's (the alignment pad at 0x00427EEC aside), and all 70 local jumps land on the same instruction. The jump table (2 entries, 0x00428038) and the byte table (16 bytes, 0x00428040) are the same. We compared them with scripts.
  • Checks: our local style checker gives 0 ERROR and 1 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

The behaviour fixes

The function draws one part of the lobby's player list per call, chosen by the menu item's number. The draft had the number's ranges as separate ifs without else, and some without a return. So:

  • For the host, after the headings (0) it ran on into the code for -1 to -9, with the seat at index 0. The draft's own listing shows the jump; in a single player skirmish that code returns at once.
  • After a seat's name (1 to 10) it ran on into the same code with the seat at index -button, before the array, and cleared DAT_ButtonUnknownZero again, which the original leaves set. This is in the draft's listing too.
  • A seat's texts: above 10 it went on to the AI lord's text after a player's text, and drew it whenever the seat also had an AI type. From 1 to 10 it drew the AI lord's text, also for a seat without one, and then the name. The original draws one of the two: it returns after the player's text (0x00427BBE, 0x00427D65).
  • An AI lord's seat button (-1 to -9, and -200 and below) was drawn twice for the host. After the button the draft ran on to the function's last lines, which draw the current button. The original leaves through the first draw (tail jumps at 0x00427EB7 and 0x00428025).
  • In the last range a player's seat ran on after its button too: it then called isAIPlayer with the alpha value in place of the seat's number and could draw the button again.
  • -102 set DAT_ButtonUnknownZero only for someone who is not the host. The original sets it first (0x00427F0E), so for the host it stays set while another dialog is open.
  • -103 outside a single player skirmish set DAT_ButtonUnknownZero, cleared it again and went on to the button. The original returns with it set (0x00427F40 to 0x0042802A).

The seat's texts, -103 and the player's seat in the last range are read from the draft's source; the others are in its listing.

What got it to 100%

The ranges are: 0 the three headings; above 10 a seat's row (its group's icons, the help texts under the mouse, "Human" or the AI lord's kind); 1 to 10 a seat's picture and the player's or the AI lord's name; -1 to -9 a seat's button; -101 to -103 three single buttons; -200 and below a seat's button for the host. The last three leave through RenderCurrentButtonToScreenMenuWithBlendingUnk.

The function is written from the original's listing now: one if per range, each ending in a return, and no goto. Our draft read 573 instructions against the original's 604. What decided the match, each measured with one thing changed:

  • The two headings with an x offset take x through a local. Written in the call, x is loaded late: 96.2% (79.2% normalised).
  • Where calls differ in one argument, each arm has its own call. The compiler shares the call itself. The three help texts with their number in a local: 92.3% (82.7%). The two colours of a name with the colour in a local: 92.7% (83.4%).
  • A player's seat button takes its picture number, 206 or 207, by if / else. As the draft's 207 - (value != 0): 96.2% (82.7%).
  • The last range is if (isHost) { … return; }, with the code for everyone else after it. That code is also -103's exit (0x0042802A). With if (!isHost) { …; return; } first: 96.1% (81.8%).
  • -101 looks for an empty seat with a flag set in the loop. With the loop's index tested after it: 96.8% (85.9%).
  • Two more, measured after the review with our exact listing compare (differing lines of 604; the final text has 0): text 402's two colours take a call in each arm too (the colour in a local and one call: 328; set by if / else: 107), and the group array is read again after the help text's call (the group local there: 330).

Four choices gave the same code, and we took the plainer form: the flag in -101 where the draft had a goto, the AI type switched from the field where a local would do, the two icon rows in either order, and if / else for the picture number where a conditional expression would do (main's style bot flags those).

The single-change variants were built before the last two edits (the if / else for the picture number, one unused include removed), which do not change the code.

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

  • (strength - 32) * 32 / 32 + 32 at every call. The original computes it each time (shl eax, 5, then a signed division by 32); the two icon rows multiply by 22.
  • drawBufferChoiceValue is set to RT_SCREEN_MENU twice in a seat's row (0x00427958, 0x00427A40).
  • The icon rows read the border's width again in every round (0x004279BE).
  • The group array is read again after the help text's call (0x00427AFE), though group holds it; the local there loses the match.

Header findings (nothing changed here)

  • The addresses header has a label renderPlayerEntryInLobby at 0x00427CB2, without a type. The address lies inside this function: it is the operand of the instruction at 0x00427CB0.
  • TextManager::field_0x30 is set to 1 before a player's name is drawn (0x00427D07).
  • DAT_ButtonUnknownZero is TRUE here for the seats' rows and names (1 and up), for -101 without an empty seat, for -102 for someone who is not the host or behind another dialog, for -103 outside a single player skirmish, and in the last range for someone who is not the host or on their own seat; FALSE in the other cases. A lead for a name: the item takes no clicks.
  • DAT_PlayerSlotArraySomeValue[slot] picks the other picture (206 for 207) of a player's seat button. A lead: it may be the seat's ready state.

The checker's 1 CHECK

What Why it stays
the placeholder name field_0x30 Ghidra's name; see the header findings

🤖 Generated with Claude Code

LobbyMenu::MenuItemRenderFunction_LobbyMenu_PlayerListAndNpcButtons, rewritten from the original's listing. gynt's
draft: f0e6b48 at 56.46% ("Style pass, asm differs") on his fork's reimpl/UI-MenuItems, whose status line records
56.5%; his reimpl/UI-MenuItems-development records 56.5% too ("Reimplemented, asm differs"; the newest commits on
the file there are 8725ec5, a reimplementation commit, and 5084980, a style commit). Our draft (8c967b4, then
five style commits up to 07a33e2; 58.8% in the draft PR sourcehold#328) was his code with a mechanical pass.

The function draws the parts of the lobby's player list, one menu item per call, chosen by the item's number: 0 the
three headings; above 10 a seat's row (its group's icons, the help texts under the mouse, and text 402 for a player
or the AI lord's text); 1 to 10 a seat's picture, a second one for the host's seat, and the player's or the AI
lord's name; -1 to -9 a seat's button; -101 to -103 three single buttons; -200 and below a seat's button for the
host. Each range ends in a return; the last three leave through RenderCurrentButtonToScreenMenuWithBlendingUnk.

Seven behaviour fixes against the draft. The draft's ranges were separate ifs without an else and partly without a
return, so:

- For the host, after the headings (0) it ran on into the code for -1 to -9, with the seat at index 0 (visible in
  the draft's listing: a jmp to that code's single-player test, which returns in a single player skirmish).
- After a seat's name (1 to 10) it ran on into the same code with the seat at index -button, before the array, and
  cleared ButtonUnknownZero again, which the original leaves set.
- Above 10 it went on to the AI lord's text after a player's text, and drew it whenever the seat also had an AI
  type; 1 to 10 it drew the AI lord's text (for a seat without an AI lord too) and then the name. The original draws
  one of the two: it returns after the player's (0x00427BBE and 0x00427D65) and draws the AI lord's only for a seat
  that has one.
- An AI lord's seat button (-1 to -9, and -200 and below) was drawn twice for the host: after the button the draft
  ran on to the function's last lines, which draw the current button. The original leaves through the first draw
  (tail jumps at 0x00427EB7 and 0x00428025).
- In the last range a player's seat ran on after its button was drawn too: it then called isAIPlayer with the alpha
  value in place of the seat's number and could draw the button again.
- -102 set ButtonUnknownZero only for someone who is not the host. The original sets it first (0x00427F0E), so for
  the host it stays set while another dialog is open.
- -103 outside a single player skirmish set ButtonUnknownZero, cleared it again and went on to the button. The
  original returns with it set (0x00427F40 to 0x0042802A).

The first two, the AI lord's button and -102 are in the draft's listing; the others are read from its source.

What decided the match, each measured with that one thing changed (reccmp in the narrow build described below):

- The two headings with an x offset take x through a local. Written in the call, x is loaded late: 96.2% (79.2%
  normalized).
- Where two or three calls differ in one argument, each arm has its own call, and the compiler shares the call:
  the three help texts (their number in a local: 92.3%, 82.7%) and the two colours of a name (the colour in a
  local: 92.7%, 83.4%).
- A player's seat button takes its picture number, 206 or 207, by if / else. As the draft's `207 - (value != 0)`:
  96.2% (82.7%).
- The last range is `if (isHost) { ... return; }` with the code for everyone else after it; that code is also
  -103's exit. With `if (!isHost) { ...; return; }` first: 96.1% (81.8%).
- -101 looks for an empty seat with a flag set in the loop. With the loop's index tested after it: 96.8% (85.9%).

Measured later with our own exact listing compare against the original's 604 instructions (differing lines, the
final text has 0): text 402's two colours take a call in each arm too (the colour in a local and one call: 328; set
by if / else: 107), and the group array is read again after the help text's call where the original reads it again
(with the `group` local there: 330).

These compile to the same listing, and the plainer form stands: the flag in -101 where the draft had a goto into
-103's clear; the AI type switched from the field where a local would do; the two icon rows in either order; the
picture number by if / else where a conditional expression would do.

The text numbers carry their texts from the game's cr.tex (group 79) as comments, and the button pictures 206, 207,
626 and 627 are named from GmDataIndex. Neither changes the listing: this text compiles to the same instructions,
line for line, as the text the builds below measured (the tree of 12b9d934e).

Measured in a narrow build on main 3458fea of that text and the five finished functions it calls, their resolvers
on: AModalDialogIsActiveButIsNotQuitting (0x00427210), renderGMWithAlphaMask (0x004557B0), renderGMWithBlending
(0x00455390), isAIPlayer (0x004010B0) and RenderCurrentButtonToScreenMenuWithBlendingUnk (0x00464370), which five
paths tail-call. reccmp 100.0, normalized 100.0, no diff line. Without the callees' files in the list the function
reads 99.4% (100.0% normalized): the tail jumps then go to a trampoline, and four of the five read differently.

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