Skip to content

138 worked example input per player

Alex Diakovsky edited this page Oct 13, 2026 · 2 revisions

138. Worked example: input per player, the word, its decode and the vehicle choice

Question: step 2 of the two-player roadmap (issue #113): give each player a controller that produces the original's input word, so two people can drive, and drive the computer opponent through the same interface (#77). Document 137 traced the bindings, the defaults and what each vehicle's buttons do. What is left to read before building it: the decode the vehicle sees, which of its handlers want a press and which a hold, and how the vehicle-choice screen reads the word (document 137 left that open). Scripts and field names are in the tracing cheat sheet.

Short answer: every vehicle sees its player's word through one digital decode (FUN_0042ce40) that turns each of the three buttons into a press / hold / release triple. Most handlers act on a press or a hold; the Jeep's three (missile, swim, flag) act on a fresh press only, so the port's held-fire Jeep missile was wrong. The vehicle choice moves its cursor every tick a direction is held and needs a fresh press to confirm (A) or open the map (B or C). The port now builds one word per player from a binding set and feeds it through the same decode.

Step 1: the decode, FUN_0042ce40

DecompileMany.java 0x0042ce40, the whole function (word this tick, prev the last):

s = word << 16;                                         // the word's low half to the top (no control lives there)
if (word & 0x40000000) s |= 2;  else if (word & 0x80000000) s |= 4;  else s |= 1;   // forward / back / neither (friction)
if (word & 0x10000000) s |= 0x10;                       // left
if (word & 0x20000000) s |= 8;                          // right
if (word & 0x200000)   s |= 0x4000;                     // left shoulder
if (word & 0x400000)   s |= 0x8000;                     // right shoulder
A (0x8000000): down now, not before -> 0x20;  down both times -> 0x40;  up now, down before -> 0x80
B (0x4000000): the same with 0x100 / 0x200 / 0x400
C (0x2000000): the same with 0x800 / 0x1000 / 0x2000

FUN_0040b980, the vehicle's per-tick function, calls the record's A handler (+0x17c) when the state has any of 0xe0, the B handler (+0x180) for 0x700 with the state shifted right by 3, and the C handler (+0x184) for 0x3800 shifted by 6, so each handler sees its own button as 0x20 press, 0x40 hold, 0x80 release.

The port, game/input_word.gd (engine):

static func decode(word: int, prev: int) -> int:
	var s := (word << 16) & 0xFFFFFFFF
	if word & UP:
		s |= FORWARD
	elif word & DOWN:
		s |= BACK
	else:
		s |= NO_THROTTLE
	if word & LEFT:
		s |= TURN_LEFT
	if word & RIGHT:
		s |= TURN_RIGHT
	...
	s |= _triple(word & A, prev & A, A_PRESS)

static func button(state: int, name: String) -> int:   # FUN_0040b980's shift: the button's own triple as 0x20 / 0x40 / 0x80
	match name:
		"a": return state & 0xE0
		"b": return (state >> 3) & 0xE0
		"c": return (state >> 6) & 0xE0

From the keys to the vehicle, in the original and in the port (the names in brackets are the port's, after this document):

The input path: a player's key and joystick bindings are ORed into one input word, FUN_0042ce40 decodes it against the last tick's word into throttle, turn, shoulder bits and a press, hold and release triple per button, and FUN_0040b980 calls the vehicle record's A, B and C handlers

Mermaid source
flowchart TB
  K["keys of the player's binding set<br/>(input/controls.json: one_player,<br/>two_player_1, two_player_2)"] --> OR
  J["joystick codes, 24 kinds<br/>(FUN_0041cb00)"] --> OR
  OR["ORed per control into the word<br/>(PlayerControls)"] --> W[("input word this tick<br/>+ last tick's word")]
  AI["a computer player's word<br/>(AIControls, document 143)"] -.-> W
  W --> D["FUN_0042ce40<br/>(InputWord.decode)"]
  D --> M["forward / back / neither,<br/>left, right, shoulders"]
  D --> T["A: 0x20 / 0x40 / 0x80<br/>B: 0x100 / 0x200 / 0x400<br/>C: 0x800 / 0x1000 / 0x2000<br/>press / hold / release"]
  M --> V["FUN_0040b980, the vehicle's tick<br/>(Vehicle)"]
  T --> V
  V --> HA["A handler, record +0x17c<br/>(buttons.a: fire)"]
  V --> HB["B handler, +0x180, state shifted 3<br/>(buttons.b: raised / swim)"]
  V --> HC["C handler, +0x184, state shifted 6<br/>(buttons.c: flag / mine / weapon toggle)"]
Loading

Step 2: press or hold, per handler

The first test of each handler (DecompileMany.java of the twelve handlers document 137 listed):

Handler Button First test Acts on
0x40d240 Tank shell A, B if ((keys & 0x60) == 0) return press or hold
0x40d520 MSV rockets A, B & 0x60 press or hold
0x40d820 MSV mines C if ((keys & 0x60) != 0 && 1 < players) press or hold
0x40e600 Heli guns A, B & 0x60 press or hold
0x40df00 Jeep missile A if ((keys & 0x20) == 0) return 0, then the interval press only
0x40dfe0 Jeep swim B & 0x20 press only
0x40e070 Jeep flag C & 0x20, then FUN_00432e40 press only
0x40e7a0 Heli weapon switch C if ((keys & 0x20) != 0) toggle +0xc bit 0x10000000 press only

Translation: holding the fire button keeps the Tank, the MSV and the Heli firing at their own rates, but the Jeep fires one missile per press. The port had fired the Jeep's missile every 30 ticks while the button was held, a port choice nobody had marked.

Step 3: the vehicle choice, FUN_00417ad0

The choice loop reads the player's word and the previous one (**(player + 0x18), **(player + 0x1c)):

if ((word & 0xc0000000) != 0) { cursor = word & 0x40000000 ? up[cursor] : down[cursor]; ... }   // no previous-word test
if ((word & 0x30000000) != 0) { cursor = word & 0x10000000 ? left[cursor] : right[cursor]; ... }
if (cursor changed) play 0x44b640;
if (fade > 0xffff && ((word & B && !(prev & B)) || (word & C && !(prev & C)))) state = 0x4180d0;   // the map window
if ((word & A) && !(prev & A) && fade > 0xffff) { ... confirm ... }

Translation: a direction moves the cursor every tick it is held (up / down first, then left / right); the neighbour table stops it at the edges of the 2 x 2 grid, so holding a direction does not cycle. A fresh press of B or C opens the map window, a fresh press of A confirms, both only once the view has faded in. This answers document 137's open question: the choice reads the same per-player word as the vehicle.

Applied in the port (issue #113)

  • InputWord (engine, new): the word's bits, the decoded bits and decode above.
  • PlayerControls (engine, new): a player's binding set builds the word, a key and a joystick input live per control and ORed (document 137, step 5). A binding is an input action, a key, or joy:<code>, one of the original's 24 joystick input codes (FUN_0041cb00: stick past a quarter of the range, buttons 1-4, hat). The sets come from the pack's input/controls.json: one_player, two_player_1, two_player_2, the original's three; set_for(players, player) picks one, through GameSettings.control_slot for Swap Sides (document 136).
  • Defaults (tools/data/input_controls.json, both importers): two-player sets are the original's (W S A D, H J K, Q E; keypad 8 5 4 6, - + Enter, 7 9; joystick codes per document 137). The one-player set is a port choice (user, 2026-10-12): the port's own keys on the original's three-button scheme, arrows, A on Space / Enter, B on Z, C on X, shoulders Q / E.
  • The buttons per vehicle are pack data: each definition has "buttons" (Tank a: fire, b: raised; Jeep a: fire, b: swim, c: flag; MSV a: fire, b: raised, c: mine; Heli a: fire, b: raised, c: weapon_toggle; _record: +0x17c / +0x180 / +0x184). So the port's separate swim (B), flag (F), Heli weapon (X), mine (M) and recentre (R) keys are gone: each vehicle's three buttons do those jobs, as in the original. The recentre is C held (0x1000, document 64).
  • Vehicle: with controls, it polls its word once per _process and decodes it; its input seams read the decode:
func _wants_to_fire() -> bool:
	if controls != null:
		var t: Variant = weapons[0].get_script().get("TRIGGER") if not weapons.is_empty() else null
		var trigger: int = int(t) if t != null else (InputWord.PRESS | InputWord.HOLD)
		return _debug_fire or _action_bits("fire") & trigger != 0
	...

func _button_actions() -> void:   # after the weapons, as FUN_0040b980 calls the B and C handlers
	if _action_bits("swim") & InputWord.PRESS:
		toggle_swim()
	if _action_bits("flag") & InputWord.PRESS:
		flag_requested.emit(self)
	if _action_bits("weapon_toggle") & InputWord.PRESS:
		toggle_heli_slot()

lobbed_missile.gd declares const TRIGGER := 0x20 (Step 2). Docking takes a fresh press of any of the three (0x920, document 77).

  • MatchController: each PlayerSlot gets its controls and hands them to its vehicle; the choice runs from the slot's word as in Step 3 (_selection_input). The keyboard handler keeps only the port's own keys: Tab (a roster of more than 4), F1-F4 (debug vehicle swap), V (quick swap). A match start calls GameSettings.begin_game(players).
  • EnemyVehicle (a port feature, #77) now drives through its own controls: ai_word turns toward the target, drives forward and holds A when it is lined up (pulsing A for a press-only weapon), and the vehicle reads that word like a player's.

Checks: tests/player_controls_check.gd (29: the decode, the triples and their shift, bindings and actions, an absent joystick, the set by player count and Swap Sides, a pack layer's set, the match wiring, the definition's buttons, the press-only missile, a C press against a hold, directions and shoulders, the default buttons, the opponent's controls).

Not done

  • The Settings screen to edit the bindings is issue #121 (step 5).
  • The absolute-heading decoder (0x42cf30) is not built; the original's PC game never selects it (document 137, step 7).
  • Pads are looked up every frame and mapped to Godot's joypads by connection order (port choice); the original detects two joysticks once per game.

Next: back to the issues list.

Clone this wiki locally