Skip to content

136 worked example swap sides

Alex Diakovsky edited this page Oct 12, 2026 · 3 revisions

136. Worked example: Swap Sides, the Options menu item that trades the two players' controls

Question: document 122 listed an Options item "Swap Sides, Alt+3" (command 0x9c42) and left it untraced. What does it swap: the bases, the colours or the controls? (Issue #86.) Scripts and field names are in the tracing cheat sheet.

The answer in one paragraph

It swaps which control set drives which player, and the two player names go with it. The command flips one byte, DAT_00448d04, and swaps the two name strings; the flag has exactly one reader, the function that builds a player's input word, which reads the control set of the other player when the flag is on. Bases, colours, vehicles and views do not change. It only works in a two-player game, and the flag is a per-run switch that is never written out.

Step 1: the handler

FindDispOps.java 0x401000 0x440000 "0x9c4" finds the id in seven places: the command switch (CMP EAX,0x9c42, 0x41fe60, in the window procedure 0x41fe50, JZ 0x420d3c) and six EnableMenuItem / CheckMenuItem calls. The handler (DisasmForce.java 0x420d3c 0x420d90):

00420d3c  CMP byte ptr [0x00442fbc],0x2     ; the number of players: only with two
00420d43  JNZ 0x00420c9b                     ; else nothing happens
00420d49  PUSH 0x48adb0                      ; the game-info struct: name 1 at +0, name 2 at +0x29 (document 133)
00420d4e  LEA EAX,[EBP + 0xffffff00]         ; a 256-byte stack buffer
00420d55  MOV ESI,dword ptr [0x0048e5dc]     ; an imported string copy (lstrcpyA)
00420d5b  XOR byte ptr [0x00448d04],0x1      ; THE FLAG: toggled
00420d62  CALL ESI                           ; copy(buffer, name1)
00420d64  PUSH 0x48add9 ; PUSH 0x48adb0 ; CALL ESI    ; copy(name1, name2)
00420d70  LEA ECX,[EBP + 0xffffff00] ; PUSH ECX ; PUSH 0x48add9 ; CALL ESI   ; copy(name2, buffer)
00420d7e  ...                                ; then CheckMenuItem(menu, 0x9c42, 8 if the flag is on else 0)

Translation: toggle the flag, and swap name1 and name2 through a temporary. (The two copies of the struct's strings are the ones FUN_00401470, the Hall of Fame updater, reads: document 133.) The menu item is ticked while the flag is on. 0x442fbc is the player count: 0x41a8e2 writes 2 to it, and the rest of the game compares it with 1 and 2 (0x4092eb, 0x40d833 ...).

The menu is kept in step by the code at 0x41eb06-0x41eb64 and 0x420da3: for a one-player game it clears the flag (MOV byte ptr [0x00448d04],0 at 0x41eb1f) and greys the item (EnableMenuItem(..., 0x9c42, 1)); for a two-player game it enables it and sets the tick from the flag.

Step 2: the one reader

FindDispOps.java 0x401000 0x440000 "0x00448d04" lists six places: the handler, the menu code above and one more, in 0x41b890, the function that reads a player's controls:

0041b8ae  MOV EDI,dword ptr [EBP + 0x10]    ; argument 3: the player number, 1-based
0041b8b1  DEC EDI                           ; 0-based
0041b8b2  CMP byte ptr [0x00448d04],0x0
0041b8b9  JZ 0x0041b8be
0041b8bb  XOR EDI,0x1                       ; swapped: use the OTHER player's control set

and everything after it indexes by EDI: the key bindings at 0x480db0 + EDI * 0x34 (each key tested with FUN_0041cb00, a key-down test, and turned into one bit of the input word: 0x8000000, 0x4000000, 0x2000000 ... the buttons and directions of document 113), the joystick slot 0x480ee0 + EDI * 4 (read with joyGetPosEx; a failed read clears it), and the per-device axis block at 0x480ef0 + EDI * 0x3c. Its only caller (FindCallRel.java 41b890: 0x42d300) is the per-tick input poll 0x42d290, which loops over the players 1 .. DAT_00442fbc and stores each result as that player's input word. The loop's own counter and the vehicle that word drives are untouched.

Translation: player p's input word is built from control set (p - 1) xor swap. With the flag on, the Tan player (player 1) is driven by the second set of keys (or the second joystick) and the Green player by the first. Nothing about the players themselves changes. The names are swapped in the same breath so that a name stays with the person holding those controls: the Hall of Fame (document 133) then records a win under the right person.

Step 3: the accelerator, and what happens to the names

Alt+3. The window's accelerator table is resource RT_ACCELERATOR 103 (pefile, entries of flags, key, command). The row flags 0x13, key 0x33, command 0x9c42 is virtual-key '3' with the Alt modifier (0x13 = FVIRTKEY | FNOINVERT | FALT): Alt+3 posts the same command as the menu item. Its neighbours are Alt+0 (0x402), Alt+1 (0x3f9, 640x480) and Alt+2 (0x3fa, 320x240): the Options menu's number keys.

The names are never saved. They live in the game-info struct 0x48adb0 (0x99 dwords: name 1 at +0, name 2 at +0x29, the mode byte at +0x25b). FUN_00424d40 creates it at start-up: it zeroes the struct and fills name 1 from the Windows user (WNetGetUserA(0, param_2, 0x29)). The Player Selector dialog procedure FUN_00425d00 shows them (SetDlgItemTextA(dlg, 1000, struct), SetDlgItemTextA(dlg, 0x3e9, struct + 0x29)) and on OK copies the edit boxes back (GetDlgItemTextA(dlg, 1000, struct, 0x29) and ... + 0x29); its registry code writes only the mode's level record (Levels\<1Player|2Player>\..., document 128), never a name. FUN_00424c80, which opens the dialogs, copies the whole struct first and puts it back if the player cancels. So after Swap Sides the swapped names are what the selector shows next time, they stay until the player edits them or the program ends, and the flag stays with them across games.

What a new game does with the flag. The code at 0x41eae0 (after the world is accepted: CALL 0x414130, TEST EAX,EAX): CMP byte [0x442fbc],2; JNZ 0x41eb1f. With two players it enables the item and ticks it from the flag (the flag carries over from the last two-player game); with one it clears the flag (0x41eb1f) and greys the item. The names are not touched either way, so a swap made in a two-player game leaves the swapped names in a later one-player game.

Applied in the port

GameSettings (engine, game/game_settings.gd) has the flag, the command and the control-set lookup:

static var swap_sides := false

static func toggle_swap_sides(players: int) -> bool:
	if players != 2:
		return false
	swap_sides = not swap_sides
	var t := player_name_1
	player_name_1 = player_name_2
	player_name_2 = t
	return true

static func begin_game(players: int) -> void:
	if players != 2:
		swap_sides = false

static func control_slot(player: int) -> int:
	return player ^ 1 if swap_sides else player

tests/swap_sides_check.gd covers the one-player refusal, the toggle both ways, the control sets, the names, and begin_game (a one-player game clears the flag but not the names). Not wired to anything yet: the port has one human player. The two-player input controller (issue #113) is to read its bindings through GameSettings.control_slot(player), and the Options menu entry (a toggle, greyed out in a one-player game, ticked when on) belongs to the front-end menus (document 127).

Closed

Nothing about Swap Sides is untraced now. What is still to do is wiring, with the two-player work: the input controller reads through control_slot (#113), a match start calls begin_game(players), and the Options menu gets its toggle (document 127).

Clone this wiki locally