Skip to content

171 deterministic sim 16 height and water

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

171. Deterministic sim 16: the vehicle's height and water in 16.16 (stage S6.1)

Update (document 172): the Heli's take-off and automatic landing are on integers too.

Question: the vehicle's height (z) was the last float in its movement. It drives the water handlers, the Heli's climb, the dock sink and the rise out of the pad. How does the original step each of them on its integers, in what order within the tick, 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. It is split into sub-stages, one per area: S6.1 (this document) the height and water; S6.2 the Heli's take-off and automatic landing; then the weapons, gun mount and cooldowns; the explosion boxes, the road assist and the rest of the match controller; and last, the CI runs on Linux, Windows and the web.

The water handlers were traced in document 62 and document 123, the move step in document 54, the Heli's flight in document 63 and document 162, the dock in document 81 and the pad in document 89. This document re-reads them on the integers and finds four things the port had wrong: when the water handler runs, the move step's retry without a bounce, the Heli's climb being part of its move, and the dock and pad's exact tick conditions. Those documents are updated.

Glossary

Term Meaning
16.16, raw As in document 161: 0x10000 is one unit. A raw unit is 1/65536.
height obj+0x48, 16.16, the port's Vehicle.height_fx. Negative while sinking or under the pad.
water class FUN_0042f280's answer: 0 land, 1 shallow, 2 deep. Stored in state+0x70, the port's water_class.
water handler state+0x44: one of three functions, the dry handler FUN_0040cef0, the wading handler FUN_0040d150 and the sinking handler FUN_0040cf90. Each installs the next and swaps the draw descriptor (document 123).
splash counter state+0x48, the port's wade_counter_fx. The wading handler's spray frame; the sinking handler steps it too.
swim flag, swim amount The Jeep's state+0x84 (0 or 0x10000) and state+0x80, which follows it by 0x444 a tick (swim_target_fx, swim_amount_fx).
the drive's answer local_10 in FUN_0040b980: what the drive returned (the ground drive's new speed, 0 standing still; the Heli's 1), set to 0 when the move bounced. The water handler gets it. The port's Vehicle.drive_moved.
the mover FUN_0042c830(obj, delta): moves the object by a 3D delta if the new place is clear (document 164).
the place test FUN_0042bd40(obj): is the object clear where it stands?
view A float property that reads an integer one (z of height_fx), kept for drawing and the tests.

The answer, up front

  1. The height is an integer: Vehicle.height_fx is obj+0x48, and z is its view. The swim flag, swim amount and splash counter are integers too, and z_fx() returns the height itself.
  2. The water handler runs at the end of the tick, after the drive, the move, the dock test and the buttons (FUN_0040b980 at 0x40c029), with the drive's answer. The port ran it first, before the drive. So the water class is now taken where the vehicle is after its move, the drive reads the class from the end of the last tick, and a vehicle starts wading or sinking on the tick it reaches the water.
  3. The Jeep's swim ramp is its state handler FUN_0040d990, before the drive. The swim toggle (FUN_0040dfe0) asks FUN_0042f280 again itself, not the stored class.
  4. The three handlers are the original's, line for line. Two details the port missed: the sinking handler steps the splash counter (+0x3333 a tick, wrapping at 6.0), and back at the surface it wades only when bit 0 of the drive's answer is set.
  5. The water class is integer code: Water.class_at_fx, the object's first shape against the coast shape with the traced shape test. One whole-unit quirk comes with it: an object whose origin is less than a unit before a shore tile's centre on both axes never overlaps the coast shape (step 2).
  6. The move step has a retry the port lacked. A blocked move that turned is tried again with the old heading; if that is blocked too but the vehicle is clear where it stands, it just stays, keeping its speed: no bounce. Only a move that did not turn, or a vehicle blocked where it stands, bounces. A vehicle turning into a wall therefore stops dead while the keys are held.
  7. The Heli goes through the same move step. Its climb (0x8000 a tick up to 50.0) is the delta's third component, so a blocked move does not climb, and the Heli bounces like the ground vehicles.
  8. The dock sink fades from the tick after the height is below −0x100000 (the test is on the height before the fall) and ends at exactly −0x200000. The pad rise releases the vehicle the tick after its height reaches 0.

Step 1: where the water handler runs, FUN_0040b980

The vehicle update (document 54), from the state handler to the water handler:

// FUN_0040b980 (DecompileMany.java 40b980), reduced
if (state[3] != 0) {                         // the state handler: the Jeep's FUN_0040d990, the Heli's start-up
  if (state[3](...) != 0) return;            //   busy: nothing else runs, not even the water handler
}
heading0 = obj->heading;
moved = state[4](&delta, obj, rec, state);   // the drive: local_10
if (moved == 0) {
  if (obj->heading != heading0 && FUN_0042bd40(obj)) { obj->heading = heading0; obj->flags |= 0x4000000; }
} else {
  /* fuel: |speed| * dt >> 5 */
  if (FUN_0042c830(obj, &delta)) {           // blocked
    if (obj->heading != heading0) {
      obj->heading = heading0;
      if (!FUN_0042c830(obj, &delta) || !FUN_0042bd40(obj)) goto done;   // moved, or clear where it stands
      obj->heading = heading0;
    }
    moved = 0;
    obj->speed = -obj->speed >> 2;
    obj->flags |= 0x4000000;
  }
}
done:
/* fuel warning, the dock test (a dock goes straight to 0x40c029), the three button handlers */
LAB_0040c029:
if (state[0x11] != 0) {                      // the water handler, +0x44
  state[0x1c] = FUN_0042f280(obj);           // +0x70, the class, taken now
  state[0x11](obj, rec, state, moved);
}

The port, Vehicle.sim_step and the new move_step:

	if water != null:
		water.state_tick(self)          # FUN_0040d990: the swim ramp
	if drive.has_method("tick_startup") and drive.tick_startup(self):
		return   # (and FUN_0040b980 returns before the water handler too)
	drive_moved = 0
	...
	drive.tick(self)
	_process_fuel_warn()
	...
	if not _dock_pressed():   # a dock goes straight to 0x40c029, the water
		_weapons_tick()
		_button_actions()
	_update_water(drive_moved)


func move_step(d: Vector2i, dz: int, heading_before: int) -> void:
	var target := pos_fx + d
	var tz := height_fx + dz
	if not blocked_test.is_valid() or not blocked_test.call(self, target, heading_fx, tz):
		pos_fx = target
		height_fx = tz
		return
	if heading_fx != heading_before:
		heading_fx = heading_before
		if not blocked_test.call(self, target, heading_fx, tz):
			pos_fx = target
			height_fx = tz
			return
		if not blocked_test.call(self, pos_fx, heading_fx, height_fx):
			return
	drive_moved = 0
	speed_fx = -speed_fx >> 2

The retry keeps the same delta, worked out on the new heading; only the shape's heading goes back. vehicle_blocked takes the height to test at, so the Heli's climb is tested with the rest of the move.

Step 2: the water class, FUN_0042f280 and FUN_0042f410

// FUN_0042f280 (object), reduced
if (0x10000 < obj->z) return 0;
art = *cell & 0x7f; coastal = (*cell & 0x3f80) >> 7;
if (art > 0x33 || coastal == 0x4a || coastal == 0x4b) return 0;
if (art == 1) return 1;  if (art == 2) return 2;  if (art < 4) return 0;
if (art < 0x18) {
  shape = obj->desc->shape ? obj->desc->shape : &DAT_0044dbe8;        // the FIRST shape; none: a point
  e = &DAT_0044de10[art * 8];                                         // flag, (char)rot, coast shape
  hit = FUN_0041e060(&obj->pos, obj->heading, shape, &tile_centre, (char)e[1] << 16, e->shape);
  return (hit != 0) == (e[0] != 0) ? 1 : 0;
}
if (art < 0x28) return 1;
return FUN_0041d5b0(&obj->pos, &tile_centre, &DAT_0044ded0[(art - 0x28) * 0x10]) ? 1 : 2;

FUN_0042f410 is the same for a bare point: shape 0x44dbe8, heading 0. The three descriptors a water handler swaps in share one shape pointer (+8: 0x43e8f8 for the Tank's three, 0x43f6a8 for the Jeep's), so the class does not depend on which is drawn. The coast shapes 0x44dc70, 0x44dcf0, 0x44dd70 and 0x44ddf0 are polygons (type 3) with the box (−16, −16, 15, 15).

static func class_at_fx(level: LevelData, pack: Pack, pos_fx: Vector2i, heading_fx := 0, z_fx := 0, shape: Dictionary = {}) -> int:
	if z_fx > 0x10000:
		return 0
	...
	if art < 0x18:
		...
		var coast := _coast_shape_fx(tables, String(e["shape"]))
		var mine := shape if not shape.is_empty() else _point_shape_fx()
		var rot := int(e["rot_steps"]) & 0xff
		rot = rot - 0x100 if rot >= 0x80 else rot   # (char)
		var overlap := Collision.shapes_hit_fx(pos_fx, heading_fx, mine, centre, rot << 16, coast, false)
		return 1 if overlap == (int(e["flag"]) != 0) else 0
	if art < 0x28:
		return 1
	...
	return 1 if Collision.point_in_box_fx(pos_fx, centre, box) else 2

The test is the traced one (document 164), and it brings the traced segment test with it. Against a polygon, a point (and, for two polygons, the containment step) is tested by the segment from it to the polygon's origin, and FUN_0041d670 cuts every difference to whole units first. Within one unit of the tile centre (0 <= centre - pos < 1 on both axes) that segment is a point, the collinear branch reports a touch on the first edge whose x range covers it, and the answer is "no overlap". Across every shore tile of RFMAP002, the integer class agrees with the old float one at every point of an 8 × 8 grid, except:

  • the tile centres, by that rule;
  • points exactly on a diagonal edge, which the original's ends-included test counts as touching.

The float Water.class_at(level, pack, pos, shape, heading_fx, z) is now a wrapper round the integer one, for the editor's overlay, the computer players and the views.

Step 3: the three handlers

// FUN_0040cef0, dry
if (state->cls != 0) {
  if (state->cls == 2 && !(rec->type == JEEP && state->swim == 0x10000)) { install(sinking); state->counter = 0; return; }
  if (moved != 0 && (mul(FUN_0040c390(), rec->max_fwd) >> 1) <= obj->speed) { install(wading); state->counter = 0x40000; }
}
// FUN_0040cf90, sinking (param_4 is a byte: TEST EAX,ECX)
if (state->cls == 0) { install(dry); state->counter = 0; obj->z = 0; return; }
if (state->cls == 1 || swimming) {
  obj->z += 0x6666;
  if (obj->z >= 0) { obj->z = 0; if ((moved & 1) && fast) { install(wading); counter = 0x40000; return; } install(dry); counter = 0; obj->z = 0; return; }
} else {
  obj->z -= 0x6666;
  if (obj->z >> 16 <= -(rec->depth >> 16)) { obj->z = 0x10000 - (rec->depth & 0xffff0000); FUN_0042c4d0(obj); /* splash, the loss */ }
}
state->counter += 0x3333; if (state->counter > 0x5ffff) state->counter -= 0x60000;
// FUN_0040d150, wading
old = state->counter; state->counter += 0x3333;
if (state->cls == 2 && !swimming) { install(sinking); state->counter = 0; obj->z = 0; }     // and it goes on
keeps = state->cls == 1 && moved != 0 && obj->speed >= 1;
if (state->counter < 0xd0000) {
  if (!keeps && (old & 0xffff0000) <= 0x30000 && (state->counter & 0xffff0000) > 0x30000) { install(dry); state->counter = 0; }
  return;
}
state->counter -= keeps ? 0x50000 : 0xd0000;

The port, vehicle_modules/hull_water.gd:

func update(v: Vehicle, moved: int) -> void:
	v.water_class = v.water_class_now()
	var swimming := b("can_swim") and v.swim_target_fx == 0x10000
	if v.wading:
		_wade(v, moved, swimming)
	elif v._sinking:
		_sink(v, moved, swimming)
	else:
		_dry(v, moved, swimming)


func _sink(v: Vehicle, moved: int, swimming: bool) -> void:
	if v.water_class == 0:
		v._sinking = false
		v.wade_counter_fx = 0
		v.height_fx = 0
		return
	if v.water_class == 1 or swimming:
		v.height_fx += fx("sink_rate")
		if v.height_fx >= 0:
			v.height_fx = 0
			v._sinking = false
			if (moved & 1) != 0 and _fast_enough(v):
				v.wading = true
				v.wade_counter_fx = fx("wade_start")
			else:
				v.wade_counter_fx = 0
			return
	else:
		v.height_fx -= fx("sink_rate")
		if (v.height_fx >> 16) <= -(v.sink_depth_fx >> 16):
			v.height_fx = 0x10000 - (v.sink_depth_fx & ~0xffff)
			v.alive = false
			v.drowned.emit(v)
	v.wade_counter_fx += 0x3333
	if v.wade_counter_fx > 0x5ffff:
		v.wade_counter_fx -= 0x60000

_dry and _wade follow the C above the same way. The old port's wading test also needed a nonzero speed; the original asks the drive's answer instead (moved != 0). FUN_0040c390's height test is 0xffff < z (the port had z > 1.0); FUN_0042f280's is 0x10000 < z. Each is now its own.

Step 4: the Heli's climb, FUN_0040e0e0

// FUN_0040e0e0's tail (the Heli's drive)
param_1[0] = state->vel_x * dt;  param_1[1] = state->vel_y * dt;
if (obj->z < 0x320000) { param_1[2] = dt * 0x8000; if (0x320000 < param_1[2] + obj->z) param_1[2] = 0x320000 - obj->z; }
else param_1[2] = 0;
return 1;

The port climbed by itself before the blocked test. Now the climb is the move step's dz:

	var dz := 0
	var ceiling := fx("ceiling")
	if v.height_fx < ceiling:
		dz = mini(fx("climb"), ceiling - v.height_fx)
	...
	v.drive_moved = 1   # FUN_0040e0e0 always returns 1
	if v.burn_fuel(spd):
		return
	v.move_step(vel, dz, heading_before)

Step 5: the dock sink and the pad rise, FUN_0042efc0 and FUN_0042ef20

// FUN_0042efc0, the dock object's handler, each tick
if (-0x100001 < obj->z) { obj->z -= 0x4ccc; obj->timer = max(obj->timer - 1, 0); return 0; }   // no fade
obj->draw = fade;                                                                              // LAB_0042f0a0
obj->z -= 0x4ccc; obj->timer = max(obj->timer - 1, 0);
if (obj->z < -0x1fffff && obj->timer < 1) { obj->z = -0x200000; FUN_0042c4d0(obj); }            // removed: the choice opens
// FUN_0042ef20, the rising vehicle
if (-1 < obj->z) { obj->handler = 0; return 1; }          // at 0 or above: released
obj->z += 0x4ccc; if (0 < obj->z) obj->z = 0;
	if ps.pad_rising:
		ps.pad_age += 1
		if v.height_fx < 0:
			v.height_fx = mini(v.height_fx + DOCK_SINK_RATE_FX, 0)
		else:
			ps.pad_rising = false
			...
	# dock_state == 2
	var fading := v.height_fx <= DOCK_FADE_FX - 1
	v.height_fx -= DOCK_SINK_RATE_FX
	ps.dock_timer = maxi(ps.dock_timer - 1, 0)
	ps.pad_age = ps.dock_timer
	if fading:
		ps.view_fade = maxf(ps.view_fade - SelectorAnim.FADE_PER_TICK * ticks, 0.0)
	if fading and v.height_fx < DOCK_MIN_FX + 1 and ps.dock_timer < 1:
		v.height_fx = DOCK_MIN_FX

The pad leaves (FUN_0042ea10) are drawn while n * 0x4ccc < 0x120000, at ±(0x50000 + n × 0x4ccc): pad_leaf_offset now computes them that way from the integer age (the same numbers as before).

The golden check

53 scenes on RFMAP002, match seed fixed, no Drones, logging every tick the position, heading, height, speed, water class, the descriptor drawn, the splash counter and the swim numbers:

  • the Tank, Jeep and MSV driving south into the shallows at (35, 36), deep water at (89, 44) and the shore at (36, 37): full throttle, throttle released after 70 ticks, and turning right; the Jeep toggling swim mode in the water and driving on, or reversing out;
  • the three driving and turning (right and left) into the building at (40, 31); the Heli low into it, turning into it, and sitting at it;
  • each of the four docking, then the pad rise.

Before and after:

  1. Into the water: the same positions; the class, the wading and the sinking come one tick earlier, on the tick the vehicle gets there (step 1). The drive now reads the class from the end of the last tick, so leaving the water the speed cap stays 0.75 one tick longer (deep-type1-turn, tick 44).
  2. Sinking: the splash counter now runs while the vehicle sinks (it stayed 0).
  3. The Jeep swimming: the toggle asks the class itself, so it switches on a tick earlier, and the swim amount reaches 1.0 (the speed cap 0.25) a tick earlier.
  4. The shore at (36, 37): on ticks 31 and 32 the Tank's centre is less than a unit before the tile centre (step 2), the class is shallow for those two ticks, and the Tank wades a little. The same happens for the other two vehicles and the Jeep's scenes.
  5. Turning into the building or a shore object: where the old port bounced (wall-type1-turn tick 49, deep-type2-turn 139, coast-type0-turn 200, coast-type1-turn 90), the vehicle now stops and keeps its speed (step 1). The straight runs are unchanged: they bounce.
  6. The Heli turning into the building: at tick 24 the move is blocked, and the old Heli climbed on regardless. Now it stays at 12.0 units, with its heading and speed kept. Flown straight in, it is unchanged.
  7. The dock: the same heights; the fade starts a tick later (at tick 54, not 53), and the choice opens on the same tick at exactly −0x200000.
  8. The pad rise: the same heights; the vehicle is released a tick later (tick 107, not 106), so the Heli takes off a tick later. The leaf offsets are the same at every tick.

Tests

  • New openfire height_fx_check, on the real pack:
    • an art-5 shore tile's centre is land, 4 units inside its shape it is shallow, and above 0x10000 it is land;
    • a Tank into deep water is classed and wades on the tick it gets there, falls exactly 0x6666 a tick, and is lost at exactly 0x10000 − 14.0;
    • a Jeep turning into a building stops and keeps its speed; a Tank driven straight in bounces;
    • a blocked Heli move does not climb, and a clear one climbs exactly 0x8000;
    • the dock does not fade while the height was above −0x100001, and ends at −0x200000; the pad rise releases the vehicle the tick after its height reaches 0.
  • openfire water_view_check passes the drive's answer to the water handler.
  • openfire vehicle_behaviour_trace_check: the Jeep's trace is re-recorded. On RFMAP001 it leaves the shallows at tick 508, and the drive reads the class from the end of the last tick, so the water speed cap holds one tick longer (difference 1 above); the other three are unchanged.
  • float_lint_check: the baseline falls from 1,382 to 1,334.

Known differences from the original, kept

  • A frozen vehicle (the match over, docking, the pad) still runs its water handler with no move. In the original a docked vehicle is a separate dock object with no water handler. On the pad the class is 0, so nothing happens either way.
  • Off the map the class is 0 (the original's off-map pseudo-cell PTR_DAT_00449644 is not read).
  • The Heli's automatic landing (0x40eb40, 0x40ec30) still slides the position and heading by a float fraction of the height; it is S6.2, with the take-off (done in document 172).
  • The bounce flag 0x4000000 is not kept: nothing in the port reads it.

Where the code is

  • Engine game/vehicle.gd: height_fx and the views, water_class_now, sim_step, drive_moved, move_step, _update_water, _terrain_scale_fx, _road_mask_here.
  • Engine game/vehicle_modules/hull_water.gd: state_tick, toggle_swim, update, _dry, _sink, _wade.
  • Engine game/vehicle_modules/ground_drive.gd _move, game/vehicle_modules/rotor_drive.gd tick.
  • Engine game/water.gd: class_at_fx, class_at.
  • Engine game/match_controller.gd: vehicle_blocked, _update_slot_dock, _begin_pad_rise, pad_leaf_offset; game/player_slot.gd.
  • openfire tools/tests/height_fx_check.gd, tools/tests/water_view_check.gd.

Commits: engine 69ac7bd, openfire 55bd2d9.

S6.1 is done. Next: S6.2, the Heli's take-off and automatic landing on the original's integers (document 172).

Clone this wiki locally