Skip to content

163 deterministic sim 8 projectiles

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

163. Deterministic sim 8: projectiles in 16.16 (stage S5.2)

Update (document 177): a shot's launch (Projectile.aim) reads only the spec's integer keys (z_fx, pitch_fx / bonus_fx, heading_fx, position_fx). The computer players' turned copy of a shot writes them too (AIGunner.pointed), rounded as the old float setters rounded.

Update (document 173): the shot specs now carry heading_fx, position_fx, z_fx and pitch_fx, which Projectile.aim takes before the float views.

Question: how does a shot fly on the original's integers (its launch, its tick, the lobbed Jeep missile and the homing submarine missile), and what changes when the port flies it that way (document 155, issue #133)?

The deterministic simulation series: document 155 is the plan; documents 156 to 160 are stages S0 to S4; documents 161 and 162 are S5.1, vehicle movement; this document is S5.2, projectiles.

Shots were first traced in document 52 (the straight shell), document 61 (the lob), document 63 (the Heli's pitched guns and bombs) and document 112 (the homing missile). This document re-reads each handler for the exact integer form.

Glossary

Term Meaning
16.16, mul, heading, step, view As in document 161.
raw angle An object's heading (obj+0x4c) or pitch (obj+0x60): 22 bits, 0x400000 a turn, 0x10000 one of 64 steps. The tables are indexed by angle >> 16, so a shot always flies on a whole step.
pitch Positive is downward. The Heli's gun is 0x71c71 (40 degrees down); the raised guns are near 0x355555 (60 degrees up).
heading matrix H, pitch matrix P The 64 rotation matrices FUN_0041ae50 builds at start-up: H[i] at 0x48a4b0 and P[i] at 0x481710, 0x24 bytes each.
pitch-moves flag obj+0xc bit 0x2000000: the shot was launched with a pitch and its type has a pitch rate, so its tick turns the pitch.
ballistic Projectile-type flag bit 1 (type 6, the Heli's bomb): its pitch grows every tick instead of easing back to level.

The answer, up front

  1. A shot is now integers: position, height, heading, pitch, speed and velocity are the original's 16.16 and 22-bit numbers. The old float fields (position, z, heading_deg, speed, pitch_steps, spin_deg, lob_start, homing_vel) are views, so the matches, the computer players and the checks read them as before.
  2. The velocity comes from the original's tables: (0, -speed, 0) times P[pitch >> 16] x H[heading >> 16], or H alone at pitch 0. So a shot flies on a whole heading step. The float port flew at any angle; a shot fired at 12.65 degrees now flies at 11.25, as in the original.
  3. Only a shot launched with a pitch and a pitch rate turns its pitch (the flag above). Every other shot keeps its launch velocity.
  4. The Heli's level-button bomb flies level. It has pitch 0, so the flag is never set and it does not arc. The float port bent every bomb down. From the down button the bomb starts at pitch 0x3fffff, one raw unit below level, so the flag is set and it arcs.
  5. The Heli's toe-in is 2 steps (11.25 degrees), not half a step. FUN_0040e600 adds +-0x20000 to the heading. Document 63 read it as 0.5 step and has been corrected; the module's toe_steps is now the integer 2.
  6. The ceiling (55 units) turns the pitch only for a shot whose tick did not already turn it. The float port turned it twice in the same tick.
  7. The lob and the homing missile are exact integer forms of the formulas in documents 61 and 112. They fly within 0.0013 and 0.5 units of the float port.
  8. Still float, and why: the muzzle point (rounded once at launch), the damage (stage S5.4) and the collision test (stage S5.3).

A shot spec is aimed; the first step launches it (the velocity from P x H, the flag if pitched with a rate); each tick the flag turns the pitch and rebuilds the velocity, the ceiling clips the climb, and the shot moves; the lob and the homing missile have their own ticks

Mermaid source
flowchart TB
  SP["shot spec<br/>heading_fx, pitch_fx, bonus_fx"] --> AIM["aim(): pitch, speed += launcher speed"]
  AIM --> K{"kind"}
  K -- "lob" --> LB["lob: FUN_00415730<br/>z: parabola in t<br/>pos: start + dir x r(t)"]
  K -- "homing" --> HM["homing: FUN_00415100<br/>turn toward target<br/>vel approaches"]
  K -- "shot" --> LA["first step: FUN_004148f0<br/>vel = (0,-speed,0) x P x H<br/>flag if pitch and rate"]
  LA --> FL{"flag 0x2000000?"}
  FL -- "yes, not ballistic" --> T1["pitch toward 0 by rate<br/>rebuild velocity"]
  FL -- "yes, ballistic" --> T2["pitch += rate<br/>rebuild velocity"]
  FL -- "no" --> CE
  T1 --> CE{"z + vz > 0x370000?"}
  T2 --> CE
  CE -- "yes" --> CL["vz clipped;<br/>no flag: pitch toward 0"]
  CE -- "no" --> MV["pos += vel; z < 0 is the ground"]
  CL --> MV
Loading

Step 1: the tables

FUN_0041ae50 fills both tables at start-up from the trig helpers, for each step i at angle i << 18 (the helpers take 2^24 to a turn):

H[i] = { c,  s, 0,   -s, c, 0,   0, 0, 1 };   // 0x48a4b0
P[i] = { 1,  0, 0,    0, c, s,   0, -s, c };  // 0x481710

FUN_00410d00(out, a, b) multiplies two of them and FUN_0041e770(out, v, m) multiplies a row vector by one. The port has both already (document 159): Fx.mat_mul and Fx.transform. The direction table at 0x481390 that the vehicles use is (0, -1, 0) x H, so the two agree.

# addons/openfire_engine/game/projectile.gd
static func _tables() -> void:
	if not _heading_m.is_empty():
		return
	for i in 64:
		var c := Fx.cos(i << Fx.STEP_SHIFT)
		var s := Fx.sin(i << Fx.STEP_SHIFT)
		_heading_m.append(PackedInt32Array([c, s, 0, -s, c, 0, 0, 0, Fx.ONE]))
		_pitch_m.append(PackedInt32Array([Fx.ONE, 0, 0, 0, c, s, 0, -s, c]))


func _velocity(pitch: int, exact := false) -> Vector3i:
	var m := heading_matrix(heading_fx)
	if pitch != 0:
		m = Fx.mat_mul(exact_pitch_matrix(pitch) if exact else pitch_matrix(pitch), m)
	var v := Fx.transform(PackedInt32Array([0, -speed_fx, 0]), m)
	return Vector3i(v[0], v[1], v[2])

Translation: each table entry is built once from the integer sine and cosine. A velocity is the vector "speed straight ahead" turned first by the pitch's matrix, then by the heading's. At pitch 0 only the heading's matrix is used, as in the original.

Step 2: the launch, FUN_004148f0

obj.speed = type[+0xc];  obj.heading = spec[2];  obj.pitch = spec[3];  obj.life = type[+0x38];
if (spec[3] != 0 && type[+0x28] != 0) obj[+0xc] |= 0x2000000;      // pitch-moves
vel = (0, -speed, 0);
if (pitch == 0)                      m = H[heading >> 16];
else if (!(type[+8] & 2))            m = P[pitch >> 16] x H[heading >> 16];
else { FUN_0041ae10(tmp, pitch);     m = tmp x H[heading >> 16]; }      // ballistic
vel = vel x m;                                                            // FUN_0041e770

FUN_004155a0 then adds the launcher's speed, when it is moving, and rebuilds the velocity the same way:

0040e6a3  CMP EAX,0x1                         ; EAX = obj[+0xc] & 0x8000000 (which mount)
0040e6a6  SBB EDX,EDX                         ; -1 if the bit is clear, else 0
0040e6a8  AND EDX,0xfffc0000                  ; -0x40000 or 0
0040e6ae  ADD EDX,0x20000                     ; -0x20000 or +0x20000: two steps
0040e6b4  ADD EDX,dword ptr [EDI + 0x4c]      ; + the Heli's heading
0040e6b7  AND EDX,0x3fffff

That is the Heli's fire handler FUN_0040e600 building the heading it passes to the launch, only for the down button (param4 != 0). Two steps from each mount, inward: 0x10000 is a step, so 0x20000 is 11.25 degrees. Document 63 read it as half a step.

FUN_0041ae10, used only for a ballistic launch, builds a pitch matrix from the trig helpers directly. It is handed the 22-bit pitch but the helpers read 2^24 to a turn, so it builds a quarter of the angle. For the down-button bomb (0x3fffff, one unit below level) this makes no difference; the port keeps the quirk (exact_pitch_matrix).

# addons/openfire_engine/game/projectile.gd
func _launch() -> void:
	launched = true
	pitch_moves = pitch_fx != 0 and pitch_rate_fx != 0
	vel_fx = _velocity(pitch_fx, ballistic)


# addons/openfire_engine/game/vehicle_modules/heli_guns.gd
	if down:
		# 0040e6a3: CMP/SBB/AND 0xfffc0000/ADD 0x20000 -> +0x20000 from the left mount (bit 0x8000000, 0x440f20), -0x20000 from the right
		var toe_steps := int(params["toe_steps"])
		toe_fx = toe_steps * 0x10000 if v._heli_mount_left else -toe_steps * 0x10000
		pitch = f("down_pitch_steps") * 5.625 if slot == 0 else 0.0
	var h_fx := (v.heading_fx + toe_fx) & 0x3fffff
	...
	spec["pitch_fx"] = roundi(pitch / 5.625 * 65536.0) if slot == 0 else (0x3fffff if down else 0)

Translation: the shot's velocity is built once, on its first step, from the heading, pitch and speed set by then (the launcher's speed is already added). It is marked to turn its pitch only if it has both a pitch and a rate. The Heli passes its exact heading plus two steps toward the middle, and its pitch argument: the gun's 40 degrees, the bomb's "one unit down" from the first button, or 0 from the second.

Step 3: the tick, FUN_00414b10

if (obj.life <= dt) { remove; return; }   obj.life -= dt;
if (obj[+0xc] & 0x2000000) {                                        // pitch-moves
  if (!(type[+8] & 2)) {                                            // not ballistic
    old = pitch;  FUN_004319e0(&pitch, 0, type[+0x28]);             // toward level
    if (old != 0) vel = (0, -speed, 0) x (pitch ? P[pitch>>16] x H : H);
  } else {
    pitch = (pitch + type[+0x28] * dt) & 0x3fffff;                  // ever further down
    vel = (0, -speed, 0) x (pitch ? P[pitch>>16] x H : H);
  }
  turned = true;
}
d = vel * dt;
if (z + d.z > 0x370000) { d.z = 0x370000 - z;  if (!turned) FUN_004319e0(&pitch, 0, type[+0x28]); }
FUN_0042c830(obj, &d);                     // move
if (z < 0) { z = 0; impact by the ground under it (document 61) }

FUN_004319e0 turns an angle the short way by a step and lands on the target when within it (the port's turn_toward_fx, already used for the vehicles). No projectile type has a per-tick callback (obj+0x18).

# addons/openfire_engine/game/projectile.gd
func advance() -> bool:
	if lob:
		return _lob_step()
	if homing:
		_homing_step()
		return true
	if not launched:
		_launch()
	var turned := false
	if pitch_moves:
		if not ballistic:
			var before := pitch_fx
			pitch_fx = turn_toward_fx(pitch_fx, 0, pitch_rate_fx)
			if before != 0:
				vel_fx = _velocity(pitch_fx)
		else:
			pitch_fx = (pitch_fx + pitch_rate_fx) & 0x3fffff
			vel_fx = _velocity(pitch_fx)
		turned = true
	var d := vel_fx
	if Fx.wrap32(z_fx + d.z) > Z_CEILING_FX:
		d.z = Z_CEILING_FX - z_fx
		if not turned:
			pitch_fx = turn_toward_fx(pitch_fx, 0, pitch_rate_fx)
	pos_fx = Vector2i(Fx.wrap32(pos_fx.x + d.x), Fx.wrap32(pos_fx.y + d.y))
	z_fx = Fx.wrap32(z_fx + d.z)
	_age_ticks += 1
	return _age_ticks < lifetime_ticks

Translation: a shot marked to turn its pitch eases it back to level (or, for a bomb, tips it further down) and rebuilds its velocity from the tables. A shot about to rise above 55 is held at 55, and only a shot that did not already turn its pitch this tick has it pulled toward level. Then it moves.

The lifetime is counted as the port always has (lifetime_ticks moves). The original's life byte is decremented before the move and the shot is removed when it reaches 1, so whether it makes 79 or 80 moves depends on the tick of the frame it was spawned in, which is not traced yet. The port keeps 80.

Step 4: the lob, FUN_004159a0 and FUN_00415730

// launch
d   = FUN_0042cd70(start, target) >> 16;        // whole units
v   = FUN_sqrt(FUN_div(3276 * d, 0xb504));
dir = FUN_00422e70(start, target);              // bearing
t = 0; spin = 0; frame = 0;
// tick
t += 1;
z   = (mul(v, 0xec83) - 0x666 * t) * t + z0;
r   = mul(v, t * 0x61f7);
pos = start + mul(dirtable[dir >> 16], r);
spin += 0x8888;  frame += 0x2aaa;  if (frame > 0xbffff) frame %= 0xc0000;

FUN_0042cd70 subtracts then shifts: isqrt(((b.x - a.x) >> 16)^2 + ((b.y - a.y) >> 16)^2) << 16. FUN_00422e70 is ((atan2(a.y - b.y, a.x - b.x) >> 2) - 0x100000) & 0x3fffff.

# addons/openfire_engine/game/projectile.gd
static func distance_fx(a: Vector2i, b: Vector2i) -> int:
	var dx := Fx.wrap32(b.x - a.x) >> 16
	var dy := Fx.wrap32(b.y - a.y) >> 16
	return Fx.wrap32(Fx.isqrt(Fx.wrap32(dx * dx + dy * dy)) << 16)


static func bearing_fx(a: Vector2i, b: Vector2i) -> int:
	return ((Fx.atan2(Fx.wrap32(a.y - b.y), Fx.wrap32(a.x - b.x)) >> 2) - 0x100000) & 0x3fffff


func _lob_step() -> bool:
	lob_t += 1
	var dz := Fx.mul(lob_v_fx, 0xec83)
	z_fx = Fx.wrap32(Fx.wrap32((dz + lob_t * -0x666) * lob_t) + lob_z0_fx)
	var r := Fx.mul(lob_v_fx, Fx.wrap32(lob_t * 0x61f7))
	var dir := Vehicle.dir_fx(lob_heading_fx)
	pos_fx = Vector2i(Fx.wrap32(Fx.mul(dir.x, r) + lob_start_fx.x), Fx.wrap32(Fx.mul(dir.y, r) + lob_start_fx.y))
	spin_fx = (spin_fx + 0x8888) & 0x3fffff
	frame_fx += 0x2aaa
	if frame_fx > 0xbffff:
		frame_fx %= 0xc0000
	return lob_t <= 1000

Translation: the lob's speed comes from the whole distance to the target; every tick its height is a parabola in the tick count and its ground position is the start plus the direction step times a distance that grows with the count. It spins 3 degrees and a sixth of a frame each tick.

Step 5: the homing missile, FUN_00415100

heading = FUN_004319e0(heading, FUN_00422e70(self, target), 0x6666);            // DAT_00448914
want = atan2(z - tz, FUN_0042cd70(self, target)) >> 2;
if (want > 0xaaaaa && want < 0x355556) want = want > 0x200000 ? 0x355556 : 0xaaaaa;   // 60 degrees either way
pitch = FUN_004319e0(pitch, want, 0x3333);                                       // DAT_00448918
if (pitch != want || rand(4) == 1) smoke;
w = (0, -speed, 0) x (P[pitch >> 16] x H[heading >> 16]);
vel.x = approach(vel.x, w.x, 0x7ae);  vel.y = approach(vel.y, w.y, 0x7ae);  vel.z = approach(vel.z, w.z, 0x1999);
move by vel;

The port's _homing_step is this, line for line (its numbers come from the projectile type's homing table). The target's position is read from its pos_fx when it has one, so a vehicle target gives the exact integers.

Translation: each tick the missile turns its heading toward the target and its pitch toward the target's elevation (never steeper than 60 degrees), and its velocity eases toward "speed along that direction", so it swings wide in a turn.

What changed in behaviour

A temporary dump flew a fixed set of shots for up to 300 ticks with the float port and then with this one (tools/tests/zz_s5p_dump.gd, not committed). Every difference was traced to the original:

Case Largest difference Why
Straight shell at 0 degrees 0.00003 none (rounding)
Straight shell at 12.65 degrees 5.76 after 80 ticks the original flies on the step: 11.25 degrees
Rocket raised 40 degrees at -30 degrees 10.1 after 90 ticks the same heading rounding
Raised shell at 45 degrees 0.26 the float port turned the pitch twice a tick at the ceiling; before it reached the ceiling (tick 36) within 0.0006
Heli gun the shot now leaves 8.4 degrees further inward the toe-in is 2 steps, not half a step
Heli bomb, level button flies level for its 100 ticks (the float port arced it to the ground in 30) pitch 0: no flag, no arc
Heli bomb, down button 4.5 at impact the pitch moves on whole steps from 0x3fffff
Jeep missile (lob) 0.0013 none (rounding)
Homing missile 0.5 the tables' truncated values, through 150 ticks of turning

Tests

  • The engine checks pass (55). The determinism check hashes the projectiles' integers (the views are skipped in sim_hash.gd). edge_guard_check calls the homing step without a time argument.
  • The openfire checks pass, with two notes:
    • vehicle_behaviour_trace_check: the Heli's golden was re-recorded. Diffing the old and new traces line by line, the only change is the muzzle point of the four down-button bombs, from the 2-step toe-in.
    • ai_jeep_gun_check was already flaky: its match seed and the computer player's draws are random each run. With the seed pinned to 1 to 4, the old engine passes 1 of the 4 seeds and this one 2 (seed 2 fails on both), so S5.2 does not make it worse.

What is still float

  • The muzzle point: each weapon module still computes it in floats; aim() rounds it to 16.16 once, at launch. It moves to integers with the vehicles' mounts.
  • Damage (stage S5.4) and the collision test (stage S5.3): the match tests the segment each shot flew, from the views.

Where the code is

  • Engine game/projectile.gd: the state, the tables, _launch, advance, _lob_step, _homing_step, aim.
  • Engine game/vehicle_modules/heli_guns.gd: the exact heading with the 2-step toe-in, and the pitch argument.
  • Engine game/match_controller.gd and game/ai/ai_gunner.gd: a shot spec is applied with aim().
  • Engine game/sim/sim_hash.gd: the projectile views are left out of the hash.

Commits: engine 99ccb21, openfire 458ab9d.

Next: S5.3, collision.

Clone this wiki locally