On this page
Input: from a key to a player action
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.
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.
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.
| Mapping | From | To |
|---|---|---|
| axis from an axis | a mouse or controller axis, whole or one half | an input value, analog |
| axis from two keys or buttons | a plus and a minus key | a value that ramps between -1 and 1 |
| trigger from a key or button | one key, with a mode | 0 or 1 |
| trigger from an axis direction | mouse wheel up, say | 0 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:
| Setting | Default | Meaning |
|---|---|---|
| axis scale | 0.03 | what one count of mouse movement is worth |
| mouse sensitivity | 1.0 | times that |
| yaw factor, pitch factor | 1.0, 1.0 | only their ratio is used |
| invert mouse | off | |
| button rise time, fall time | 0.001 s | how fast a key-driven axis reaches full; at the default, within one tick |
| double tap time | 0.2 s | |
| keyboard sensitivity | 0.1 | saved 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:
| Mode | The trigger is on |
|---|---|
| 0 or below: push and hold | while the key is down |
| 10000 or above: once | in the tick the key goes down |
| 1000 up to 10000: double tap | on a second press within the double tap time, and then while held |
| between 0 and 1000: auto repeat | on 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.
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#
| What | Check |
|---|---|
| the control map language | all 35 shipped control map scripts parse; game-written files are reproduced byte for byte |
| the name tables of keys, axes and inputs | extracted from the original and compared in tests |
| the pipeline: reads, windows, maps, trigger modes, the fills | in-process against the original's input manager on recorded event sequences: 11,509 ticks, no difference |
| controllers and hats | the same with fake controllers: 11,428 ticks |
| the options screen's bindings | 3,010 bindings, no difference |
| raw mouse movement to the player action's axes | end to end: 31,899 ticks |
| the haptic controller the original also supports | not 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#
| What | Where |
|---|---|
| ids, devices, control maps, the manager | crates/bf2-input (ids, device, control_map, manager, input) |
| the script commands and the writer | crates/bf2-input (script, writer) |
| the options screen's settings | crates/bf2-input (settings) |
| the name tables | re/data/input_names.tsv |
Notes: notes/subsystems/input.md, notes/tracks/input.md.
docs/dev/engine/input.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.