Skip to content

175 deterministic sim 20 road assist

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

175. Deterministic sim 20: the Jeep's road assist in 16.16 (stage S6.4b)

Question: the Jeep's road-following steering was the last float step in its drive: the port turned the 22-bit heading into degrees, worked out a target from the float position, and wrote the result back. What does the Jeep's drive handler do on its integers, and what changes when the port does the same (document 155, issue #133)?

The deterministic simulation series: document 155 is the plan. Documents 156 to 160 are stages S0 to S4, and documents 161 to 170 are S5. S6 takes the last floats out of the simulation files before the check goes into CI: S6.1 the height and water (document 171); S6.2 the Heli's take-off and landing (document 172); S6.3 the weapons (document 173); S6.4 the rest, in four parts: S6.4a the explosion damage boxes (document 174), S6.4b the road assist (this document), S6.4c the dock test and the rest of the match controller, S6.4d the view code fenced off from the lint; then S6.5 the CI runs.

The road assist was traced in document 94: the road mask, its table, and the six instructions that read it. The Jeep's drive was put on integers in document 161, except for this block. This document reads the block itself, FUN_0040db80 from 0x40dd50 to 0x40de52, and FUN_004319e0.

Glossary

Term Meaning
16.16, raw As in document 161: 0x10000 is one unit.
raw angle 22 bits: 0x400000 is a full turn, 0x10000 one 5.625-degree step. 0 is north, clockwise.
road mask DAT_0048c7b0, written by FUN_0040c390 from the tile under the vehicle: bit 1 a road running north, 2 south, 4 east, 8 west. The port's _road_mask.
lateral offset ((p & 0x1ffffc) − 0x100000) >> 2 for the vehicle's x or y: its distance from the cell's centre line, a quarter of it, as a raw angle. ±0x40000 (22.5 degrees) at the cell's edge.
FUN_004319e0 Turns a raw angle toward a target by a step the short way round, and stops on it. The port's Projectile.turn_toward_fx.
FUN_00410b70 The 16.16 multiply.

The answer, up front

  1. The target is a raw angle from the lateral offset. North (mask bit 1, heading above 0x380000 or below 0x80000): −lx. South (bit 2, between 0x180000 and 0x280000): lx + 0x200000. East (bit 4, between 0x80000 and 0x180000): 0x100000 − ly. West (bit 8, between 0x280000 and 0x380000): ly − 0x100000. The windows are open at both ends and asked in that order.
  2. The offset is cut to 4 raw units (& 0x1ffffc) and shifted arithmetically.
  3. The step is the Jeep's turn rate (record+0x178, the port's turn_fx) times the turn-key scale (FUN_00410b70, 1.0 from the keyboard), by FUN_004319e0: the short way, landing on the target.
  4. The tile is the vehicle's cell (obj+0x1c), the position >> 21. The port divided the float position by the pack's tile size.

The golden check: the headings differ by at most 2 raw units (a two-millionth of a turn) in 10 of 29 runs; the positions and speeds are the same.

Step 1: the block, FUN_0040db80 at 0x40dd50

// FUN_0040db80 (ForceDecompile.java 40db80), the block at 0x40dd50, names added
// reached with no turn key (keys & 0x18 == 0); scale = the turn-key scale, never 0
if (road_mask != 0 && scale != 0) {
    int h = obj->heading;                                  // obj+0x4c
    int lx = (int)((obj->x & 0x1ffffc) - 0x100000) >> 2;
    int ly = (int)((obj->y & 0x1ffffc) - 0x100000) >> 2;
    int target;
    if ((road_mask & 1) && (h > 0x380000 || h < 0x80000))        target = -lx;
    else if ((road_mask & 2) && h > 0x180000 && h < 0x280000)    target = lx + 0x200000;
    else if ((road_mask & 4) && h > 0x80000 && h < 0x180000)     target = 0x100000 - ly;
    else if ((road_mask & 8) && h > 0x280000 && h < 0x380000)    target = ly - 0x100000;
    else goto no_turn;
    FUN_004319e0(&heading, target & 0x3fffff, FUN_00410b70(scale, record->turn));
}

The decompiler writes the windows as the negations (h < 0x380001 && 0x7ffff < h skips the north rule); they are the same open windows. The mask is only refreshed by FUN_0040c390 while a throttle key is held, as document 94 found; the port already did that.

static func target_fx(mask: int, heading: int, pos_fx: Vector2i) -> int:
	var lx := ((pos_fx.x & 0x1ffffc) - 0x100000) >> 2
	var ly := ((pos_fx.y & 0x1ffffc) - 0x100000) >> 2
	if (mask & 1) != 0 and (heading > 0x380000 or heading < 0x80000):
		return (-lx) & 0x3fffff
	if (mask & 2) != 0 and heading > 0x180000 and heading < 0x280000:
		return (lx + 0x200000) & 0x3fffff
	if (mask & 4) != 0 and heading > 0x80000 and heading < 0x180000:
		return (0x100000 - ly) & 0x3fffff
	if (mask & 8) != 0 and heading > 0x280000 and heading < 0x380000:
		return (ly - 0x100000) & 0x3fffff
	return -1

Step 2: the turn, FUN_004319e0

// FUN_004319e0 (ForceDecompile.java 4319e0)
bool turn_toward(uint *angle, uint target, int step)
{
    uint a = *angle, d = (target - a) & 0x3fffff;
    if (d == 0) return true;
    if ((-d & 0x3fffff) < d) {                    // the short way is down
        a = (a - step * dt) & 0x3fffff;
        d = (target - a) & 0x3fffff;
        if ((-d & 0x3fffff) <= d) goto done;      // not past it yet
    } else {
        a = (a + step * dt) & 0x3fffff;
        d = (target - a) & 0x3fffff;
        if (d <= (-d & 0x3fffff)) goto done;
    }
    a = target;                                   // past it: land on it
done:
    *angle = a;
    return a == target;
}

This is the port's Projectile.turn_toward_fx (document 163), which the Drone and the gun mount already use.

static func steer_fx(mask: int, heading: int, pos_fx: Vector2i, turn_fx: int) -> int:
	var target := target_fx(mask, heading, pos_fx)
	if target < 0:
		return heading
	return Projectile.turn_toward_fx(heading, target, turn_fx)

The ground drive calls it with the vehicle's own numbers:

		if controls.y != 0.0:
			v._road_mask = v._road_mask_here()
		if turn == 0 and v._road_mask != 0:
			v.heading_fx = RoadAssist.steer_fx(v._road_mask, v.heading_fx, v.pos_fx, v.turn_fx)

and the tile under the vehicle is its cell:

func tile_under() -> Vector2i:
	return Vector2i(pos_fx.x >> 21, pos_fx.y >> 21)

The golden check

A dump on RFMAP001 (the level with roads), match seed 1996: a Jeep on each of its 29 road tiles, at one of five x offsets and four y offsets in the cell, with one of eleven headings (including 0x7ffff and 0x380000, at the windows' ends), and one of three inputs: forward held; forward for 20 ticks, then coasting; forward with the right key held for ticks 11 to 15. 40 ticks each, logging the position, heading, speed and road mask.

Before and after: 19 runs are identical. In 10 the heading differs by 1 or 2 raw units on some ticks: the float version turned the target into degrees, and the step from degrees per second, and rounded back. The positions, speeds and masks are identical in all 29: the move uses the heading's whole step (heading >> 16), which a raw unit or two never changes here.

Tests

  • openfire road_assist_check: the target rule on integers: on the centre line, 8 units off it for each of the four directions, the open window ends (0x80000 is neither north nor east), outside the window, a mask without the direction, the 4-raw-unit cut, one step of the turn rate and the landing on the target. The driving checks on level 1 are unchanged.

Known differences from the original, kept

  • Both turn keys (keys & 0x18 == 0x18) make the original turn toward the controller's own heading (state+0xb0), or the road's when that is within 22.5 degrees. The port's input has no controller heading; its drive treats both keys as no turn key, so the road assist runs.
  • The turn-key scale is 1.0: the port has no analogue input.

Where the code is

  • Engine game/road_assist.gd: target_fx, steer_fx, mask_for_tile.
  • Engine game/vehicle_modules/ground_drive.gd: tick; game/vehicle.gd: tile_under, _road_mask_here.
  • openfire tools/tests/road_assist_check.gd.

Commits: engine 7340d90, openfire d33c169.

S6.4b is done. Next: S6.4c, the dock test and the rest of the match controller (document 176).

Clone this wiki locally