Repository navigation
161 deterministic sim 6 vehicle movement
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.
| 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 vehicle's movement state is three integers:
pos_fx(aVector2iin 16.16 world units),heading_fx(the original's 22-bit heading) andspeed_fx(16.16 units a tick). The old float fieldsposition,heading_degandspeedstay 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. -
The ground drive is
FUN_0040c190's integer arithmetic, step for step: accelerate or reverse by the record's acceleration, capped bymul(limit, terrain scale); turn by the record's turn rate and keep 22 bits; friction toward 0; move bymul(speed, direction). -
The vehicle moves along one of 64 directions, not its exact heading. The original's table at
0x481390holds(sin, -cos)of each step's angle, and the drive looks it up withheading >> 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. -
The terrain scale is a 16.16 number:
0x13333on a road where the port used exactly 1.2,0xc000,0x4000,0x28f. -
No pack-format change was needed. The pack's five drive numbers are already the record's integers divided by 65536 (
1.04998779296875is0x10ccc), soroundi(x * 65536)gets each integer back exactly. A new check holds the pack to that. -
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.
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
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:
- 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.
- 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. - With no throttle, friction takes the speed toward 0 and never past it.
- The vehicle moves along its heading's step, not its exact heading.
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).
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:
- The port's
heading_deghas always measured from screen right; the original's heading measures from up. The two differ by a quarter turn, so the defaultheading_fxis0x100000, which reads back as 0 degrees. - 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.
- 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.
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 >> 2Translation:
- 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.
- The keyboard sends whole keys. A float axis (the debug drive, a test's scripted controls) counts only by its sign.
- The collision test still takes floats; it is converted with collision itself (S5.3). It is given the integer target's exact float.
- The bounce is now the original's
-speed >> 2, which rounds toward minus infinity, where the port multiplied by 0.25. - The fuel burn still subtracts a float (
|speed| / 32a 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.ONETranslation: 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).
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:
-
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
yat 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). -
The road limit.
mul(0x10ccc, 0x13333)is 78.748 70 a second; the float port'smax_speed * 1.2was 78.749 08. - 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.
-
Engine checks: all 55 pass. The determinism check passes (in-process, record against replay, second process), now hashing
pos_fx,heading_fxandspeed_fx; its dump test looks forslots[1].vehicle.pos_fx. -
float_lint_check: baseline 2,238, 5 more than S4: the float views invehicle.gdcount 17 more floats and a vector, andground_drive.gduses 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 readsVehicle.ROAD_SCALE_FXas a float,1 / 1.199997where it was1 / 1.2. -
Openfire checks: all pass. The vehicle behaviour trace was re-recorded;
vehicle_definitions_checkgained "the drive numbers are whole 16.16 values".
-
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.
In the engine (alexdia25/openfire-engine):
-
game/vehicle.gd:pos_fx,heading_fx,speed_fx, the views, the*_fxdrive 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 fromROAD_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.
Return Fire, rebuilt
Elsewhere