Skip to content

161 deterministic sim 6 vehicle movement

Alex Diakovsky edited this page Nov 21, 2026 · 3 revisions

161. Deterministic sim 6: vehicle movement in 16.16 (stage S5.1)

Update (document 175): the road-assist block, the last float step in the Jeep's drive, is on raw angles now.

Question: how does a ground vehicle's movement (position, heading, speed, acceleration, friction, turning) run on the original's integers instead of floats, and what changes when it does (document 155, issue #133)?

The deterministic simulation series: document 155 is the plan; document 156 is stage S0, the harness; document 157 is stage S1, one fixed tick; document 158 is stage S2, the sim's own random numbers; document 159 is stage S3, the fixed-point toolkit; document 160 is stage S4, sim state out of the scene nodes; this document is the first part of stage S5.

The drive itself was traced in document 45. This document re-reads FUN_0040c190 instruction by instruction for the exact integer order, and traces the direction table it moves along (FUN_0041ae50), which document 45 did not.

Glossary

Term Meaning
16.16 A 32-bit integer read as a number times 65536: 0x10000 is 1.0, 0x8000 is 0.5.
mul FUN_00410b70, the original's 16.16 multiply: (a * b) >> 16, rounding toward minus infinity (Fx.mul, document 159).
heading The vehicle's direction, obj+0x4c: 22 bits, 0x400000 a full turn, so 0x10000 is one of 64 steps of 5.625 degrees. 0 faces up the map (-y) and it grows clockwise.
direction step The heading's top 6 bits (heading >> 16): which of the 64 table directions the vehicle moves along.
terrain scale FUN_0040c390's factor on the speed limits: 1.0, 1.2 on a road, 0.75 in water, and so on.
view A float read of an integer: position, heading_deg and speed are now views of pos_fx, heading_fx and speed_fx.

The answer, up front

  1. The vehicle's movement state is three integers: pos_fx (a Vector2i in 16.16 world units), heading_fx (the original's 22-bit heading) and speed_fx (16.16 units a tick). The old float fields position, heading_deg and speed stay as properties that read and write those integers, so the views, the computer players and the code not yet converted keep working unchanged. Writing one rounds to the nearest integer.
  2. The ground drive is FUN_0040c190's integer arithmetic, step for step: accelerate or reverse by the record's acceleration, capped by mul(limit, terrain scale); turn by the record's turn rate and keep 22 bits; friction toward 0; move by mul(speed, direction).
  3. The vehicle moves along one of 64 directions, not its exact heading. The original's table at 0x481390 holds (sin, -cos) of each step's angle, and the drive looks it up with heading >> 16. The float port moved along the exact heading. The difference is visible: a Tank that has turned a quarter of a step keeps going straight.
  4. The terrain scale is a 16.16 number: 0x13333 on a road where the port used exactly 1.2, 0xc000, 0x4000, 0x28f.
  5. No pack-format change was needed. The pack's five drive numbers are already the record's integers divided by 65536 (1.04998779296875 is 0x10ccc), so roundi(x * 65536) gets each integer back exactly. A new check holds the pack to that.
  6. The Heli is not converted yet. Its own flight handler (rotor_drive.gd, FUN_0040e0e0) still steps floats, written into the integer state through the views. It is the next part of S5.1.

The input word's bits drive the integer speed and heading; the heading's step picks a direction from the 64-entry table; the position moves by speed times that direction; the float views read the integers

Mermaid source
flowchart TB
  IN["input word<br/>0x2 forward, 0x4 back,<br/>0x8 / 0x10 turn, 0x1 coast"] --> SP["speed_fx<br/>+- accel, friction,<br/>capped by mul(limit, scale)"]
  IN --> HD["heading_fx<br/>+- turn rate, & 0x3fffff"]
  TS["FUN_0040c390<br/>terrain scale (16.16)"] --> SP
  HD -- "heading >> 16" --> DT["direction table 0x481390<br/>64 x (sin, -cos)"]
  SP --> MV["pos_fx += mul(speed, dir)"]
  DT --> MV
  MV --> VW["views: position,<br/>heading_deg, speed (float)"]
  SP --> VW
  HD --> VW
Loading

Step 1: the drive, instruction by instruction

FUN_0040c190(out, obj, rec, state) returns the tick's displacement in out and stores the new speed and heading. [0x00480d2c] is the tick count since the last frame; the port steps one tick at a time (document 157), so it is 1. [EBP-0xc] holds the decoded input word, ESI the speed, [EBP-0x8] the heading.

The speed and heading are loaded, and the terrain scale is computed:

0040c1b5  MOV ESI,dword ptr [ECX + 0x54]      ; speed
0040c1b8  MOV EAX,dword ptr [ECX + 0x4c]      ; heading
...
0040c213  MOV CL,byte ptr [EDI + 0xb]         ; the state byte (0xff)
0040c229  MOV byte ptr [EBP + -0x4],CL
0040c22c  INC dword ptr [EBP + -0x4]          ; 0x100
0040c22f  SHL dword ptr [EBP + -0x4],0x8      ; 0x10000
0040c233  CALL 0x0040c390                     ; terrain scale
0040c240  CALL 0x00410b70                     ; scale = mul(terrain, 0x10000)

Accelerate (bit 0x2) and reverse (bit 0x4). Both use the record's +0x170. Each clamps only after the step:

0040c245  TEST byte ptr [EBP + -0xc],0x2
0040c24e  MOV EAX,dword ptr [EBX + 0x170]     ; accel
0040c257  IMUL EAX,dword ptr [0x00480d2c]
0040c25e  ADD ESI,EAX                         ; speed += accel
0040c260  MOV ECX,dword ptr [EBX + 0x168]     ; max forward
0040c266  CALL 0x00410b70                     ; limit = mul(max forward, scale)
0040c26b  CMP EAX,ESI
0040c26d  JGE 0x0040c271
0040c26f  MOV ESI,EAX                         ; speed = limit when above it
0040c271  TEST byte ptr [EBP + -0xc],0x4
0040c277  MOV EAX,dword ptr [EBX + 0x170]     ; the same accel
0040c287  SUB ESI,EAX                         ; speed -= accel
0040c289  MOV ECX,dword ptr [EBX + 0x16c]     ; max reverse (negative)
0040c28f  CALL 0x00410b70                     ; floor = mul(max reverse, scale)
0040c294  CMP EAX,ESI
0040c296  JLE 0x0040c29a
0040c298  MOV ESI,EAX                         ; speed = floor when below it

Turn. Both bits together are the joystick's steering (document 45), which the port does not have:

0040c29d  AND EAX,0x18
0040c2a0  CMP EAX,0x18                        ; both: FUN_004319e0 steers to state+0xb0
0040c2c1  CMP EAX,0x10
0040c2c9  MOV ECX,dword ptr [EBX + 0x178]     ; turn rate
0040c2d6  SUB EAX,ECX                         ; 0x10: heading -= turn
0040c2df  MOV EAX,dword ptr [EBX + 0x178]
0040c2ec  ADD EAX,dword ptr [EBP + -0x8]      ; 0x08: heading += turn
0040c2ef  AND EAX,0x3fffff                    ; keep 22 bits

Friction (bit 0x1, set when no throttle key is held), toward 0 from either side and stopping there:

0040c2fd  MOV EAX,dword ptr [EBX + 0x174]     ; friction
0040c303  TEST ESI,ESI
0040c305  JGE 0x0040c316
0040c30e  ADD ESI,EAX                         ; reversing: speed += friction
0040c310  TEST ESI,ESI
0040c312  JLE 0x0040c323
0040c314  JMP 0x0040c321                      ; went past 0: 0
0040c31d  SUB ESI,EAX                         ; forward: speed -= friction
0040c31f  JNS 0x0040c323
0040c321  XOR ESI,ESI                         ; went below 0: 0

The displacement: the heading's step indexes a table of 12-byte vectors, and each component is multiplied by the speed:

0040c329  SAR ECX,0x10                        ; heading >> 16: the step
0040c32c  LEA EAX,[ECX + ECX*0x2]
0040c32f  LEA ECX,[EAX*0x4 + 0x481390]        ; table + 12 * step
...
0040c348  IMUL EBX,dword ptr [0x00480d2c]     ; speed * ticks
0040c353  CALL 0x00410b70                     ; out.x = mul(speed, dir.x)
0040c35f  CALL 0x00410b70                     ; out.y = mul(speed, dir.y)
0040c36a  MOV dword ptr [EDI + 0x8],0x0       ; out.z = 0
0040c375  MOV dword ptr [ECX + 0x54],ESI      ; store speed
0040c378  MOV dword ptr [ECX + 0x4c],EAX      ; store heading

Translation:

  1. With a throttle key held, the speed moves by the acceleration and is then clamped to the terrain-scaled limit. If the vehicle drives into water faster than the water's limit, the clamp drops it there in one tick.
  2. The turn is a plain add of the turn rate (the Tank's 0x4000, a quarter of a step a tick), wrapped to a full turn.
  3. With no throttle, friction takes the speed toward 0 and never past it.
  4. The vehicle moves along its heading's step, not its exact heading.

Step 2: the direction table

FUN_0041ae50 builds the table once. For each step i from 0 to 63 it takes the angle a = i * 0x40000 (in the 2^24-a-turn units of Fx.sin), builds a rotation matrix from FUN_00410be0 (cos) and FUN_00410dc0 (sin), and transforms the vector (0, -0x10000, 0) with FUN_0041e770:

out.x = mul(v.z, M6) + mul(v.y, M3) + mul(v.x, M0)      ; M3 = -sin
out.y = mul(v.x, M1) + mul(v.y, M4) + mul(v.z, M7)      ; M4 = cos
out.z = mul(v.y, M5) + mul(v.z, M8) + mul(v.x, M2)

With v = (0, -0x10000, 0) only the v.y terms remain, and mul(-0x10000, k) is exactly -k. So entry i is (sin(a), -cos(a)), using the original's own truncated sine and cosine, which Fx.sin and Fx.cos reproduce exactly (document 159). Step 0 is (0, -65536), up the map; step 16 is (65536, 0), right; step 1 is (6423, -65220).

The port builds the same 64 entries on first use, in game/vehicle.gd:

## The direction step of a heading as the drive moves along it: FUN_0041ae50's table at 0x481390, 64 entries of (0, -1, 0) turned by
## step x 5.625 degrees (FUN_0041e770), which is (sin, -cos) of the step's angle exactly. `heading` is the 22-bit `heading_fx`.
static var _dirs: Array[Vector2i] = []
static func dir_fx(heading: int) -> Vector2i:
	if _dirs.is_empty():
		for i in 64:
			_dirs.append(Vector2i(Fx.sin(i << Fx.STEP_SHIFT), -Fx.cos(i << Fx.STEP_SHIFT)))
	return _dirs[(heading >> 16) & 63]

Translation: i << 18 is i * 0x40000, the step's angle. The lookup takes the heading's top 6 bits, as SAR ECX,0x10 does (the heading is never negative after the AND 0x3fffff).

Step 3: the integer state and its views

From game/vehicle.gd:

var pos_fx := Vector2i.ZERO   ## 16.16 world units (1/32 tile), x right, y down
var heading_fx := 0x100000    ## obj+0x4c: 22 bits, 0x400000 a turn, 0x10000 one of 64 steps; 0 faces -y (up), increasing clockwise
var speed_fx := 0             ## obj+0x54: 16.16 units a tick, negative in reverse

var position: Vector2:   ## world units: what Node2D.position was until stage S4
	get:
		return Vector2(pos_fx.x / 65536.0, pos_fx.y / 65536.0)
	set(value):
		pos_fx = Vector2i(roundi(value.x * 65536.0), roundi(value.y * 65536.0))
var heading_deg: float:   ## 0 = facing +X (screen right), increases clockwise: the original's heading less a quarter turn
	get:
		return fposmod(heading_fx * (360.0 / 0x400000) - 90.0, 360.0)
	set(value):
		heading_fx = roundi(fposmod(value + 90.0, 360.0) * (0x400000 / 360.0)) & 0x3fffff
var speed: float:   ## world units a second
	get:
		return speed_fx * (TICK_HZ / 65536.0)
	set(value):
		speed_fx = roundi(value * (65536.0 / TICK_HZ))

Translation:

  1. The port's heading_deg has always measured from screen right; the original's heading measures from up. The two differ by a quarter turn, so the default heading_fx is 0x100000, which reads back as 0 degrees.
  2. Everything that wrote a float (the spawn, the landing, the debug teleports, the mod tool's poses, the checks) still does, and lands on the nearest integer. Nothing that reads the floats had to change.
  3. The sim hash (document 156) skips the three views: it hashes the integers they are made from.

The record's five drive numbers are loaded next to the floats the computer players still read:

	# the pack keeps the record's numbers as units a tick; each is a whole number of 1/65536, so this gives back the record's integer
	max_forward_fx = roundi(float(t["max_forward_per_tick"]) * 65536.0)
	max_reverse_fx = roundi(float(t["max_reverse_per_tick"]) * 65536.0)
	accel_fx = roundi(float(t["accel_per_tick2"]) * 65536.0)
	friction_fx = roundi(float(t["friction_per_tick2"]) * 65536.0)
	turn_fx = roundi(float(t["turn_steps_per_tick"]) * 65536.0)

Why no pack-format change. Document 155 planned to store raw integers in the pack. For these five numbers the pack already holds integer / 65536 written out in full (1.04998779296875, -0.399993896484375). Every such number is exact in a double, and turning the same decimal text into a double gives the same result on every platform, so roundi(x * 65536) is the record's integer everywhere. A mod that writes 1.05 gets 68813, the same on every machine. Keeping the readable unit means no importer change, no rebuilt pack and no re-import for other sessions. The openfire check vehicle_definitions_check now fails if any of the five is not a whole number of 1/65536.

Step 4: the port's drive

From game/vehicle_modules/ground_drive.gd:

func tick(v: Vehicle) -> void:
	var controls := v._get_controls()
	if b("turn_accelerates") and controls.y == 0.0 and controls.x != 0.0:
		controls.y = 1.0   # FUN_0040db80 (document 62): turning without a throttle key accelerates
	var turn := int(signf(controls.x))
	var heading_before := v.heading_fx
	if turn != 0:
		v.heading_fx = (v.heading_fx + turn * v.turn_fx) & 0x3fffff
	if b("road_follow"):
		...
		if turn == 0 and v._road_mask != 0:
			v.heading_deg = RoadAssist.steer(v._road_mask, v.heading_deg, v.position, v.turn_rate_deg, Vehicle.TICK_SEC)

	var scale := v._terrain_scale_fx()   # times ((state+0xb) + 1) << 8, which is 0x10000 (1.0) while the byte is 0xff
	var spd := v.speed_fx
	if controls.y > 0.0:
		spd += v.accel_fx
		spd = mini(spd, Fx.mul(v.max_forward_fx, scale))
	elif controls.y < 0.0:
		spd -= v.accel_fx
		spd = maxi(spd, Fx.mul(v.max_reverse_fx, scale))
	elif spd < 0:
		spd = mini(spd + v.friction_fx, 0)
	else:
		spd = maxi(spd - v.friction_fx, 0)
	v.speed_fx = spd
	_move(v, heading_before)

and the movement step of FUN_0040b980 (document 54):

	var dir := Vehicle.dir_fx(v.heading_fx)
	var target := v.pos_fx + Vector2i(Fx.mul(v.speed_fx, dir.x), Fx.mul(v.speed_fx, dir.y))
	...
	var target_f := Vector2(target.x / 65536.0, target.y / 65536.0)
	if not v.blocked_test.call(v, target_f, v.heading_deg):
		v.pos_fx = target
		return
	if v.heading_fx != heading_before:
		v.heading_fx = heading_before
		if not v.blocked_test.call(v, target_f, v.heading_deg):
			v.pos_fx = target
			return
	v.speed_fx = -v.speed_fx >> 2

Translation:

  1. The order inside the tick does not matter for the result: the speed and the heading do not read each other, and the move uses both new values, as the original's does.
  2. The keyboard sends whole keys. A float axis (the debug drive, a test's scripted controls) counts only by its sign.
  3. The collision test still takes floats; it is converted with collision itself (S5.3). It is given the integer target's exact float.
  4. The bounce is now the original's -speed >> 2, which rounds toward minus infinity, where the port multiplied by 0.25.
  5. The fuel burn still subtracts a float (|speed| / 32 a tick); fuel is converted in S5.4.

The terrain scale, from game/vehicle.gd:

func _terrain_scale_fx() -> int:
	if z > 1.0:
		return Fx.ONE
	if water_class != 0:
		# FUN_0040c390: 0.75 in any water, 0.25 for a Jeep that has finished entering swim mode
		return 0x4000 if (_swims() and swim_amount >= 1.0) else 0xc000
	if _swims() and swim_amount >= 1.0:
		return 0x28f  # swim mode on dry land: nearly stuck (press the swim button to leave it)
	...
	return ROAD_SCALE_FX if RoadAssist.mask_for_tile(level.get_art_id(t.x, t.y), level.get_coastal_id(t.x, t.y)) != 0 else Fx.ONE

Translation: the tile under the vehicle (tile_under()) now comes from pos_fx >> 16, so a vehicle exactly on a tile edge is never put on the wrong side by float rounding. The Tank's limits per terrain:

Terrain scale forward limit reverse floor
ground 0x10000 0x10ccc (65.62 a second) -0x6666
road 0x13333 0x1428e (78.749 a second; the float port had 78.749 08) -0x7ae1
water 0xc000 0xc999 -0x4ccd

mul rounds toward minus infinity, so the reverse floors are one unit further from 0 than a float would give (mul(-0x6666, 0xc000) is -19661, not -19660.5).

What changed in behaviour

A golden run recorded every tick of the vehicle behaviour trace's scripted drive (600 ticks for each of the four vehicles: throttle, turns both ways, reverse, coasting) before and after the conversion. Every difference traces to the original:

  1. The direction step. After 60 ticks the Tank has turned 1.4 degrees (a quarter of a step). The float port moved it 0.031 units down the map; the integer drive keeps y at exactly 2224.0, since the step is still 0. Over the 600 ticks the paths drift apart by up to 16 units (Tank), 12 (Jeep) and 20 (MSV).
  2. The road limit. mul(0x10ccc, 0x13333) is 78.748 70 a second; the float port's max_speed * 1.2 was 78.749 08.
  3. Float rounding. The old position was a 32-bit float stepped by a float each tick. At x = 2224 a float32 is only accurate to 1/4096, so the old position was off by up to that much after each step (2224.150 15 where the exact sum is 2224.149 98). The integer position has no such error.

The Heli is unchanged: its trace matches the old one on every tick.

The trace check's recording was rewritten from the new code (vehicle_behaviour_trace_check -- --record), since the behaviour was meant to change.

Tests

  • Engine checks: all 55 pass. The determinism check passes (in-process, record against replay, second process), now hashing pos_fx, heading_fx and speed_fx; its dump test looks for slots[1].vehicle.pos_fx.
  • float_lint_check: baseline 2,238, 5 more than S4: the float views in vehicle.gd count 17 more floats and a vector, and ground_drive.gd uses 8 fewer floats and none of its 4 library maths calls (deg_to_rad, cos, sin, move_toward).
  • The AI's road cost (nav_grid.gd) now reads Vehicle.ROAD_SCALE_FX as a float, 1 / 1.199997 where it was 1 / 1.2.
  • Openfire checks: all pass. The vehicle behaviour trace was re-recorded; vehicle_definitions_check gained "the drive numbers are whole 16.16 values".

What is still float

  • The Heli's flight (rotor_drive.gd): the next part of S5.1.
  • The Jeep's road assist (RoadAssist.steer, document 94) works in degrees and writes the heading through the view.
  • The collision test the move calls (S5.3), fuel (S5.4), the wading test in hull_water.gd (it compares float views).
  • The computer players read the float views, as planned: they only produce input words.

Where the code is

In the engine (alexdia25/openfire-engine):

  • game/vehicle.gd: pos_fx, heading_fx, speed_fx, the views, the *_fx drive numbers, dir_fx, tile_under, _terrain_scale_fx
  • game/vehicle_modules/ground_drive.gd: the drive
  • game/sim/sim_hash.gd: the three views skipped
  • game/ai/nav_grid.gd: the road cost from ROAD_SCALE_FX

Engine alexdia25/openfire-engine@5a98c8b; openfire alexdia25/openfire@133cc47: tools/tests/vehicle_definitions_check.gd, tools/tests/data/vehicle_traces.json.

Next: the Heli's flight in 16.16 (the rest of S5.1), then S5.2, projectiles.

Clone this wiki locally