Skip to content

157 deterministic sim 2 one fixed tick

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

157. Deterministic sim 2: one fixed tick (stage S1)

Question: how does the simulation stop depending on how fast the machine draws frames, and on the wall clock, so that the same input words give the same match (document 155, issue #133)?

The deterministic simulation series: document 155 is the plan; document 156 is stage S0, the harness; this document is stage S1.

A port change, checked against the original's clock. The original steps its objects once per tick of timeGetTime() >> 4 (16 ms, 62.5 a second; document 45). The port used to step by each frame's time instead; S1 brings it back to whole ticks. The update order inside a tick is the port's own, written down below; the original's object order is not traced.

Glossary

Term Meaning
tick One 16 ms step of the simulation. Everything in the sim moves by exactly one tick at a time.
frame One picture drawn on screen. On a 144 Hz screen there are about 2.3 frames per tick; on a slow machine a frame may need several ticks.
delta Godot's frame time in seconds. Before S1 the sim moved by it; now only the tick clock reads it.
tick clock (SimClock) Turns frame time into whole ticks and keeps the leftover.
sim_tick The match's tick count: the game clock that every timer now reads.
drawing between ticks (TickDraw) The views draw a vehicle or shot part of the way between where it was at the start of the last tick and where it is now, so motion stays smooth faster than 62.5 Hz.

The answer, up front

  1. SimClock adds each frame's time, in whole microseconds, and hands back how many 16 ms ticks have passed. If a frame falls far behind, it runs at most 8 ticks and drops the rest of the time: the game slows down instead of jumping ahead.
  2. MatchController.step() runs one tick of the whole match in a fixed order: the vehicles, then the shots already in flight, then the world, then the hits. Vehicles and shots in a match no longer move from their own _process.
  3. No delta and no wall clock in the sim. Every delta * TICK_HZ is one tick. Cooldowns, the hit flash, shot lifetimes, the radar ping and the radar's enemy bearing count ticks instead of seconds or milliseconds.
  4. The views draw between ticks (TickDraw): vehicles, their shadows, shots and the camera.
  5. Result: all three comparisons in determinism_check agree for all 1,500 ticks (and 8,000 in a longer run). The check now fails on any divergence. The float lint's count went from 2,412 to 2,271: no clock reads left, and delta down from 130 to 6.
  6. What it doesn't prove yet: the test fight never draws from the global random generator (adding an extra randi() every tick changes nothing), and every run is on one machine with one build. S2 widens the fight so randomness shows, and gives the sim its own generator.

One frame: the tick clock turns the frame's time into whole ticks, each tick runs the match's fixed step, then the views draw between the last two ticks

Mermaid source
flowchart TB
  F["a frame: Godot calls MatchController._process(delta)"] --> C["SimClock.advance(delta): whole 16 ms ticks, at most 8"]
  C --> S
  subgraph S["step(), once per tick"]
    direction TB
    V["1. vehicles, slot order: poll controls, drive, fire"] --> P["2. shots already in flight"]
    P --> W["3. world: soldiers, turrets, drones, wrecks, death, dock, flags, mines, zones, gates"]
    W --> H["4. hits, then the radar bearing"]
  end
  S --> A["SimClock.alpha(): how far into the next tick"]
  A --> D["views draw between the last two ticks (TickDraw)"]
Loading

Step 1: the tick clock

const TICK_USEC := 16000   ## 16 ms: the original's `timeGetTime() >> 4`
const MAX_CATCHUP := 8     ## at most this many ticks for one frame (128 ms); the rest of a longer frame is dropped

var _acc_usec := 0

func advance(delta: float) -> int:
	_acc_usec += maxi(int(round(delta * 1000000.0)), 0)
	var n := _acc_usec / TICK_USEC
	_acc_usec -= n * TICK_USEC
	if n > MAX_CATCHUP:
		n = MAX_CATCHUP
		_acc_usec = 0
	return n

Translation: the frame's time is added in whole microseconds, an integer, so the leftover never drifts. Each whole 16,000 µs is one tick to run. A frame worth more than 8 ticks (a hitch, a breakpoint, a background browser tab) runs 8 and forgets the rest.

The clock is not simulation state: two machines cut the same second into different frames and still run the same ticks. SimHash skips it.

Step 2: one step, in a fixed order

func _process(delta: float) -> void:
	for i in clock.advance(delta):
		step()
	var a := clock.alpha()
	for v in all_vehicles():
		if v != null and is_instance_valid(v):
			v.draw_alpha = a
	...

func step() -> void:
	sim_tick += 1
	var flying: Array = _projectiles.filter(func(p): return is_instance_valid(p) and not p.is_queued_for_deletion())
	for v in all_vehicles():
		if v != null and is_instance_valid(v) and not v.is_queued_for_deletion():
			v.sim_driven = true
			v.game_tick = sim_tick - 1
			v.sim_step()
	for p in flying:
		if is_instance_valid(p) and not p.is_queued_for_deletion():
			p.sim_driven = true
			if not p.sim_step():
				p.queue_free()
	_update_edge_guard()
	_update_soldiers()
	...

Translation: the order of one tick is:

  1. Every vehicle: slots first, in slot order, then any vehicle no slot owns. Each polls its controls, drives, and fires.
  2. Every shot that was already flying before the vehicles moved, in the order they were fired. A shot fired this tick waits for the next one; that is the same timing the engine's tests have always used.
  3. The world: edge guard, soldiers, turrets, drones, wrecks, debris, death and respawn, docking, flags, mines, the zones under the vehicles, gates.
  4. The hits: the segment each shot flew this tick, then the radar's enemy bearing.

Before S1 each vehicle and shot was a node that moved in its own _process, so Godot's tree order decided who went first, and each moved by its frame's delta. Now the match marks every vehicle and shot put in its world (_adopt, on the world's child_entered_tree) as sim_driven, and steps them itself.

A vehicle or shot outside a match (a test, the mod tool's preview) still steps itself, with its own SimClock:

func _process(delta: float) -> void:
	if sim_driven:
		return
	if _own_clock == null:
		_own_clock = SimClock.new()
	for i in _own_clock.advance(delta):
		sim_step()
	draw_alpha = _own_clock.alpha()

Step 3: no delta, no wall clock

Inside the sim, every function that took delta now steps exactly one tick:

Before After
_update_soldiers(delta), var ticks := delta * Vehicle.TICK_HZ _update_soldiers(), var ticks := 1.0
v.rearm(delta): _rearm_acc += delta * TICK_HZ v.rearm(): _rearm_acc += 1.0
_fire_cooldown_remaining in seconds, - delta _fire_cooldown_ticks in ticks, - 1.0
hit_flash_remaining = 10.0 / TICK_HZ hit_flash_ticks = 10
lifetime_sec = lifetime_ticks / TICK_HZ, _age_sec += delta lifetime_ticks, _age_ticks += 1 (integers)
ps.hit_at_ms = Time.get_ticks_msec() ps.hit_tick = sim_tick
ripple_frame() from Time.get_ticks_msec() from game_tick, which the match sets from sim_tick

The cooldowns in ticks also fix a small rounding fault: a 10-tick cooldown stored as 0.16 seconds and counted down by 0.016 a tick could be left at a tiny positive number after 10 steps and fire a tick late.

Speeds are still per second for now. A vehicle's speed is in pixels per second, and each tick moves it by speed * Vehicle.TICK_SEC (1/62.5 s). That is a constant, not frame time, so it is deterministic on one machine. Converting the units to pixels per tick in Q16.16, as the original stores them, is S5.

The radar. The ping (document 107) now reads sim_tick - hit_tick:

func _register_hit(v: Vehicle) -> void:
	var ps := slot_of(v)
	if ps != null:
		ps.hit_tick = sim_tick

The enemy bearing used to be recomputed when the radar drew, every 60 ticks by the wall clock. That put a view's timing into the sim's state: a machine whose radar wasn't drawing would have a different bearing. Now step() recomputes it every 60 ticks, whether or not anything draws, and the radar only reads it.

Step 4: drawing between ticks

The sim now moves 62.5 times a second. On a faster screen, a vehicle drawn only where the sim left it would hold still for two or three frames and then jump. So each sim_step first keeps where the vehicle or shot was (prev_position, prev_heading_deg, prev_z), and the views draw part of the way between that and where it is now:

static func position(o: Node2D) -> Vector2:
	var from: Vector2 = o.prev_position
	if from.distance_squared_to(o.position) > SNAP_PX * SNAP_PX:
		return o.position
	return from.lerp(o.position, o.draw_alpha)

Translation: draw_alpha is how far this frame is into the next tick, from the tick clock. A jump of more than 64 px in one tick (a respawn, a teleport, a shot that hasn't flown yet) is drawn at once instead of being slid across the map. The vehicle's model and shadow, the shells and missiles, and the camera's follow point all use it, so the camera and the vehicle never drift apart. TickDraw lives in the view code and only reads; draw_alpha is skipped by SimHash because it differs on every machine.

Soldiers, drones, turrets, wrecks and debris aren't drawn between ticks yet. They move slowly enough that the 62.5 Hz steps are hard to see; they can be added the same way.

What the player notices

  • Game speed no longer depends on the frame rate. Before, a long frame became one big step of every vehicle, timer and animation at once.
  • On a slow or stalled machine, the game slows down rather than skipping ahead (more than 8 ticks behind in one frame).
  • Input is read once per tick, as in the original: up to 16 ms later than once per frame.
  • Timers stop while the game is paused, because they count ticks, not milliseconds.

Tests

  • determinism_check: EXPECT_DETERMINISTIC is now true. All three comparisons agree for 1,500 ticks; with OF_DETERMINISM_TICKS=8000 they agree for 8,000. Its tick is now just mc.step().
  • float_lint_check: baseline lowered to 2,271 (float 1,655, vector 354, math 211, random 45, clock 0, delta 6). The six deltas left are the tick clocks' own inputs (the match's, and a lone vehicle's or shot's). Two files went up by one Vector2 each, the new prev_position fields, which are sim state until S5 converts them.
  • The engine and openfire checks that called the old delta functions now call the one-tick versions. radar_ping_check counts ticks and checks the window's last tick exactly, instead of allowing for the real clock's drift.
  • ai_strategy_check sometimes fails its "every plan comes up" count, because it draws from the unseeded global generator. That is not new and not caused by S1; S2 fixes it.

Where the code is

In the engine (alexdia25/openfire-engine):

  • game/sim/sim_clock.gd: SimClock
  • game/match_controller.gd: _process, step, _adopt, sim_tick, _update_direction_sector
  • game/vehicle.gd, game/projectile.gd: sim_step, sim_driven, prev_*, ticks for cooldowns and lifetimes
  • game/tick_draw.gd: TickDraw
  • game/vehicle_modules/*.gd, game/gate.gd, game/flag_marker.gd: one tick per call

Next: S2, the sim's own random numbers (document 158). That means widening the test fight so soldiers, turrets and debris roll dice, then moving every gameplay roll to a seeded CrtRand and every cosmetic one to a separate effects generator.

Clone this wiki locally