BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Input: from a key to a player action

BF22142Heroes

Between your hand and your soldier stands one small pipeline, and it is tick-shaped from the start: the keyboard and mouse are read once a frame, but their events are dealt out to the simulation one tick at a time by their timestamps, so that a frame that holds three ticks does not give all its mouse movement to the first. The port holds this pipeline to the original on recorded event sequences: 11,509 ticks without a difference.

From the devices to the wire the devices, once a frame buffered keyboard and mouse events with their timestamps; the controller polled a window a tick each tick takes the events of its own 33 ms: three ticks in a frame share the mouse's movement the control maps the default map, then the active one: infantry, land, air, helicopter or sea axes and triggers an axis from the mouse or two keys; a trigger by its mode: hold, once, double tap, repeat the player input 64 values, a mask of the analog ones; the game input of 74 beside it the player action six axes, 41 buttons, a flag: 138 bits to the server and to the prediction BF2.exe!0x006a4630 (update), 0x006ae5f0 (keyboard), 0x006d8570 (mouse), 0x006afad0 (a map's tick), 0x006c90c0 (the fill)
From the devices to the wire. Everything in the middle runs once per simulation tick.

Devices: read once, dealt out by time#

InputManager::update(ticks, step) (BF2.exe!0x006a4630) reads every device once, then for each tick of the frame hands each device a window of time:

  • Keyboard (0x006ae5f0): the buffered events since the last read are queued. Each tick applies the events whose timestamp falls within the next 33 ms from the first queued one. A press and a release inside one window leave no edge: a tap shorter than a tick between two reads can be lost, in the original too.
  • Mouse (0x006d8570): movement events are summed per tick window into X, Y and the wheel; button events set bits and remember how long ago the press was, up to one tick. Each axis is then multiplied by the active map's axis scale.
  • Controller (0x006d6d90): polled every tick; its axes scaled; each hat adds four buttons, and only the exact four directions count: a diagonal on the hat presses nothing.

The last tick of a frame gets a window of 10 seconds, so nothing queued is left behind.

The press age

On a fire press, the mouse button's press age goes with it: how far inside the tick the click fell. The lag compensation can add it to the shot's time, by an engine variable. A shot is timed more finely than the tick it is simulated in.

Control maps#

A control map is a named set of mappings, built by console commands in Controls.con. There are seven: one each for infantry, land, air, helicopter and sea, a default player map, and a game map for the keys that are not a player's movement.

MappingFromTo
axis from an axisa mouse or controller axis, whole or one halfan input value, analog
axis from two keys or buttonsa plus and a minus keya value that ramps between -1 and 1
trigger from a key or buttonone key, with a mode0 or 1
trigger from an axis directionmouse wheel up, say0 or 1

Every mapping has a primary and a secondary source: the two columns of the options screen. A secondary line for an input that has no primary does nothing.

A map's own settings, with the constructor's defaults:

SettingDefaultMeaning
axis scale0.03what one count of mouse movement is worth
mouse sensitivity1.0times that
yaw factor, pitch factor1.0, 1.0only their ratio is used
invert mouseoff
button rise time, fall time0.001 show fast a key-driven axis reaches full; at the default, within one tick
double tap time0.2 s
keyboard sensitivity0.1saved and loaded, and read by nothing

Which map is active#

On foot the infantry map; in a seat the map of the seat's controls category (land, sea, air, helicopter: seats and cameras). Activating a map copies its invert flag, sensitivities, factors and scales into the default player map, where the mouse look is bound (0x006c35f0). So the mouse feels different in each kind of vehicle because each kind's map carries its own sensitivity: 1.7 in aircraft and 3 in helicopters in the shipped Controls.con.

There is no commander map and no menu map: those screens read the game map and the system's cursor.

One tick of a map#

ControlMap::update(dt) (0x006afad0).

An axis from the mouse: the device's value, times the sensitivity, and the larger of yaw and pitch factor scales its axis by their ratio. A mouse axis is the tick's raw movement times 0.03 times the sensitivity. There is no smoothing, no acceleration and no dead zone anywhere in the input code.

An axis from keys: while the plus key is held the value rises by dt / rise time up to 1, the minus key takes it down to -1, neither lets it fall back to 0 by dt / fall time.

A trigger, by its mode:

ModeThe trigger is on
0 or below: push and holdwhile the key is down
10000 or above: oncein the tick the key goes down
1000 up to 10000: double tapon a second press within the double tap time, and then while held
between 0 and 1000: auto repeaton the press, then every mode seconds while held

Toggles (the camera mode, the weapon toggle) are "once" triggers whose consumers flip a state. The system's key repeat plays no part.

Six ticks, not seven

The double tap timer is a double in the source, but the client runs its arithmetic at 24-bit precision, so it accumulates like a float. A second press 6 ticks after the first still counts as a double tap (the timer reads exactly 0.2 as a float); 7 ticks does not. The port's timer does the same: the pipeline was compared with the original's on recorded event sequences.

The player input#

Each tick the maps fill a PlayerInput: 64 values, a mask of which came from a real analog axis, and a flag for the flight axes. First the default map, then the active one, which overrides what it maps (fillPlayerInput, 0x006c90c0). The throttle is clamped to -1 .. 1.

The same step fills a GameInput of 74 values for the game map.

Then the consumers, per pending tick: the engine and the HUD see the game input first and may swallow it; the player input goes to the game, which queues it for the tick. The HUD can ask for the player input to be zeroed for a while, which is how an open menu stops the soldier.

Onto the wire#

The network layer turns the tick's PlayerInput into a player action (packets, the control block): six axes, a button for each of the ids 8 to 48 that is above 0.5, and the flag. 138 bits. From here on the server and the client's prediction see the same thing, and the soldier's input handling takes over.

Rebinding#

The options screen edits copies of the seven maps and writes the profile's Controls.con (ControlSettings::save, 0x004b2b00). The writer has rules of its own (a map without mappings writes nothing; a trigger line carries the input's default mode, except double tap, which is kept), and the port's writer reproduces the game-written files byte for byte.

How it is tested#

WhatCheck
the control map languageall 35 shipped control map scripts parse; game-written files are reproduced byte for byte
the name tables of keys, axes and inputsextracted from the original and compared in tests
the pipeline: reads, windows, maps, trigger modes, the fillsin-process against the original's input manager on recorded event sequences: 11,509 ticks, no difference
controllers and hatsthe same with fake controllers: 11,428 ticks
the options screen's bindings3,010 bindings, no difference
raw mouse movement to the player action's axesend to end: 31,899 ticks
the haptic controller the original also supportsnot reimplemented

The port reads devices through the window system natively and through the page in a browser, and feeds the same per-tick windows.

In the code#

WhatWhere
ids, devices, control maps, the managercrates/bf2-input (ids, device, control_map, manager, input)
the script commands and the writercrates/bf2-input (script, writer)
the options screen's settingscrates/bf2-input (settings)
the name tablesre/data/input_names.tsv

Notes: notes/subsystems/input.md, notes/tracks/input.md.

Source: docs/dev/engine/input.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.