Repository navigation
162 deterministic sim 7 heli flight
Update (document 172): the Heli's buttons run after its move, the brake and the descent use the flight drive, and "moving" has no drift threshold.
Update (document 171): the height is now 16.16 too, and the climb is part of the move delta, so a blocked move does not climb.
Question: how does the Heli's flight (speed, spin, strafe, hover drift, velocity, bank, and the push when it is hit) run on the original's integers, and what changes when it does (document 155, issue #133)?
The deterministic simulation series: document 155 is the plan; documents 156 to 160 are stages S0 to S4; document 161 is the first part of S5.1, the ground vehicles; this document finishes S5.1 with the Heli.
The Heli's flight was traced in document 63, its hover drift in issue #26 and its hit reaction in issue #70. This document re-reads both handlers instruction by instruction for the exact integer form.
| Term | Meaning |
|---|---|
16.16, mul, heading, step, direction step, view
|
As in document 161. |
| spin |
state+0x8c, heli_omega_fx: how much the heading changes each tick, in heading units (0x10000 is one step). |
| approach |
FUN_0042cdd0(value, target, rate): value moves toward target by rate and stops on it. Every eased number in the flight goes through it. |
| hover drift |
state+0x90 / +0x94, heli_wobble_fx: a small random walk the velocity follows while the Heli is asked for no velocity at all. |
| velocity |
state+0x98 / +0x9c, heli_vel_fx: what the Heli actually moves by each tick. It follows the velocity wanted, so the Heli drifts on after the keys are let go. |
-
Every flight number is now an integer: the spin, the velocity, the hover drift and the bank, plus the speed, heading and position from document 161. The old float fields (
heli_omega,heli_vel,heli_wobble,bank_steps) are views of them, so the views, the checks and the mod tool's poses are unchanged. -
FUN_0042cdd0is an exact integer move-toward, so each eased value is the port'smove_towardon integers (RotorDrive.approach). -
The flight's direction comes from the same 64-entry table as the ground drive (
Vehicle.dir_fx). The float port computed it withcosandsinof the step's angle; the table holds the original's truncated values. -
One detail the float port had wrong: the strafe direction is taken from the heading the Heli had at the start of the tick (
obj+0x4c, stored only at the end), not the heading after this tick's spin. The two differ only while the spin is still dying away after a turn key is let go. -
The hit reaction is integer too: spin
(rand(0x180) + rand(0x180) - 0x180) << 7; pushmul(direction, (rand(0x100) + 0x80) << 7). - The Heli flies almost exactly as before: over the 600-tick scripted flight its path differs by at most 0.0044 units, all of it from the table's truncated sines. The ground vehicles are unchanged on every tick.
-
Still float: the height
z(the climb is0x8000a tick, so it stays exact) and the rotor's start-up and landing ramps, which no movement reads.
Mermaid source
flowchart TB
IN["input word"] --> SP["speed_fx<br/>accel / reverse / friction"]
IN --> OT["spin target<br/>+- turn rate, or 0"]
OT --> OM["spin (state+0x8c)<br/>approach at turn_up / turn_down"]
OM --> HD["heading_fx += spin"]
HD --> FW["mul(speed, dir(new heading))"]
SP --> FW
IN -- "strafe keys" --> ST["mul(0xcccc, dir(old heading +- 16 steps))"]
FW --> WA["velocity wanted"]
ST --> WA
WA -- "zero" --> WB["hover drift: wanted = old drift,<br/>drift += rand(0x28f) - 0x147"]
WB --> VE
WA -- "not zero" --> VE["velocity (state+0x98/+0x9c)<br/>approach at 0x7ae"]
VE --> PO["pos_fx += velocity"]
FUN_0042cdd0(value, target, rate):
0042cdd1 MOV EAX,dword ptr [ESP + 0xc] ; target
0042cdd7 MOV ECX,dword ptr [ESP + 0x8] ; value
0042cddb CMP EAX,ECX
0042cddd JLE 0x0042cde8
0042cddf ADD ECX,dword ptr [EBP + 0x10] ; target above: value + rate
0042cde2 CMP EAX,ECX
0042cde4 JGE 0x0042cdf1 ; not past it: value + rate
0042cde6 POP EBP
0042cde7 RET ; past it: target
0042cde8 JGE 0x0042cdf1 ; equal: value
0042cdea SUB ECX,dword ptr [EBP + 0x10] ; target below: value - rate
0042cded CMP EAX,ECX
0042cdef JG 0x0042cdf3 ; past it: target
0042cdf1 MOV EAX,ECX
From game/vehicle_modules/rotor_drive.gd:
## FUN_0042cdd0(value, target, rate): `value` moves toward `target` by `rate` and stops on it.
static func approach(value: int, target: int, rate: int) -> int:
if target > value:
return mini(value + rate, target)
if target < value:
return maxi(value - rate, target)
return valueTranslation: the rate is not multiplied by the tick count inside the helper; the callers that need it do so (0x7ae is built as 1966 * dt, below). With the port's one tick per step, that makes no difference.
The speed is the ground drive's (document 161), with ((byte)(state+0xb) + 1) << 8 as the only scale, which is 1.0. There is no terrain factor:
0040e19f XOR EAX,EAX
0040e1a1 MOV AL,byte ptr [ESI + 0xb]
0040e1a4 INC EAX
0040e1a5 SHL EAX,0x8 ; 0x10000
...
0040e1bd MOV EAX,dword ptr [EDI + 0x170] ; accel
0040e1d3 ADD dword ptr [EBP + -0x4],EAX ; speed += accel
0040e1cd MOV ECX,dword ptr [EDI + 0x168]
0040e1d6 CALL 0x00410b70 ; limit = mul(max forward, 1.0)
The spin's target and the bank's target. The second scale, from state+0xa, is also 1.0:
0040e2bd CMP EAX,0x10 ; left
0040e2c2 MOV EDX,dword ptr [EDI + 0x178]
0040e2cb NEG EDX ; target = -turn rate
0040e2d2 MOV dword ptr [EBP + -0x8],0x30000 ; bank target +3 steps
0040e2db TEST byte ptr [EBP + -0x10],0x8 ; right
0040e2e1 MOV EDX,dword ptr [EDI + 0x178] ; target = +turn rate
0040e316 MOV dword ptr [EBP + -0x8],0xfffd0000 ; bank target -3 steps
0040e2f3 TEST byte ptr [EBP + -0xf],0x40 ; strafe one way (input bit 0x4000)
0040e2f9 MOV dword ptr [EBP + -0x20],0xfff00000 ; -16 steps
0040e309 TEST byte ptr [EBP + -0xf],0x80 ; the other (0x8000)
0040e30f MOV dword ptr [EBP + -0x20],0x100000 ; +16 steps
The spin. The rate is [0x44549c] (turn_down, 0x1eb8) when the spin is slowing or reversing, otherwise [0x445498] (turn_up, 0x7ae):
0040e31d TEST EAX,EAX ; target
0040e31f JL 0x0040e32e
0040e327 CMP dword ptr [EDI],0x0 ; target >= 0: spin <= 0?
0040e32a JLE 0x0040e33b ; -> turn_down
0040e32e JG 0x0040e343 ; target > 0 and spin > 0 -> turn_up
0040e336 CMP dword ptr [EDI],0x0 ; target <= 0: spin <= 0?
0040e339 JLE 0x0040e343 ; -> turn_up
0040e33b MOV ECX,dword ptr [0x0044549c] ; turn_down
0040e349 MOV ECX,dword ptr [0x00445498] ; turn_up
0040e354 CALL 0x0042cdd0 ; spin = approach(spin, target, rate)
0040e364 IMUL EAX,dword ptr [0x00480d2c]
0040e36b ADD EAX,dword ptr [EBP + -0x1c] ; heading += spin
0040e36e AND EAX,0x3fffff
The velocity wanted. Forward along the new heading; strafing adds 0xcccc along the direction 16 steps to the side of [ECX + 0x4c], the object's stored heading, which is still the one from before this tick:
0040e37c SAR ECX,0x10 ; new heading's step
0040e3a3 CALL 0x00410b70 ; want.x = mul(speed, dir.x)
0040e3b1 CALL 0x00410b70 ; want.y = mul(speed, dir.y)
0040e3c6 MOV EAX,dword ptr [ECX + 0x4c] ; the heading at the start of the tick
0040e3c9 ADD EAX,dword ptr [EBP + -0x20] ; +- 16 steps
0040e3cc AND EAX,0x3f0000
0040e3c1 MOV EDX,0xcccc
0040e3e0 CALL 0x00410b70 ; want.x += mul(side.x, 0xcccc)
0040e3f0 CALL 0x00410b70 ; want.y += mul(side.y, 0xcccc)
The hover drift, only when both components wanted are 0. The old drift becomes the target, and the drift takes a step:
0040e409 MOV ECX,dword ptr [ESI + 0x90]
0040e40f PUSH 0x28f
0040e414 MOV dword ptr [EBP + -0x18],ECX ; want.x = drift.x
0040e417 CALL 0x0041d3d0 ; rand(0x28f)
0040e422 LEA ECX,[ECX + EAX*0x1 + 0xfffffeb9] ; drift.x + rand - 0x147
0040e42f ... ; kept within -0x7ae .. 0x7ae, then the same for y
0040e491 MOV dword ptr [ESI + 0x94],EAX ; otherwise the drift is cleared
0040e497 MOV dword ptr [ESI + 0x90],EAX
The velocity and the bank:
0040e4a4 LEA EDX,[EAX + EAX*0x4] ; dt * 1966 = 0x7ae
...
0040e4c5 CALL 0x0042cdd0 ; vel.x = approach(vel.x, want.x, 0x7ae)
0040e4dc CALL 0x0042cdd0 ; vel.y likewise
0040e4f0 CMP dword ptr [EBP + -0x20],0x0 ; not strafing,
0040e4f6 CMP dword ptr [EBP + -0x4],0xffff0000 ; and -1.0 < speed < 1.0:
0040e50e CALL 0x00410b70 ; bank target = mul(speed, bank target)
0040e563 CALL 0x0042cdd0 ; bank = approach(bank, target, 0x147a), or toward 0 at 0x28f4
0040e573 ...
0040e579 CALL 0x00410b70 ; obj+0x70 (nose pitch) = mul(speed, 0x18000)
0040e58d IMUL EAX,dword ptr [0x00480d2c] ; out = velocity * dt
0040e5a6 CMP dword ptr [ECX + 0x48],0x320000 ; below 50: climb 0x8000 a tick, to 50
The port, from game/vehicle_modules/rotor_drive.gd:
var om := v.heli_omega_fx
var slowing := (target_omega >= 0 and om <= 0) or (target_omega <= 0 and om > 0)
om = approach(om, target_omega, fx("turn_down") if slowing else fx("turn_up"))
v.heli_omega_fx = om
var heading_before := v.heading_fx
if om != 0:
v.heading_fx = (v.heading_fx + om) & 0x3fffff
# the velocity wanted: forward speed plus strafe
var dir := Vehicle.dir_fx(v.heading_fx)
var want := Vector2i(Fx.mul(spd, dir.x), Fx.mul(spd, dir.y))
if strafe_turn != 0:
var side := Vehicle.dir_fx((heading_before + strafe_turn) & 0x3f0000)
want += Vector2i(Fx.mul(side.x, fx("strafe")), Fx.mul(side.y, fx("strafe")))
want = hover_wobble(v, want)
var rate := fx("velocity_rate")
v.heli_vel_fx = Vector2i(approach(v.heli_vel_fx.x, want.x, rate), approach(v.heli_vel_fx.y, want.y, rate))
# bank: a turn's bank scales with the speed below 1.0
if bank_target != 0 and strafe_turn == 0 and spd > -Fx.ONE and spd < Fx.ONE:
bank_target = Fx.mul(spd, bank_target)
if bank_target != 0:
v.bank_fx = approach(v.bank_fx, bank_target, fx("bank_rise"))
else:
v.bank_fx = approach(v.bank_fx, 0, fx("bank_fall"))
## The hover drift (issue #26; FUN_0040e0e0 at 0x40e3f7): with no velocity wanted at all, the drift as it stood is wanted instead, and the drift
## takes a step of rand(0x28f) - 0x147 on each axis, kept within +-0x7ae (0.03 units a tick); any velocity wanted clears it. Returns what is wanted.
func hover_wobble(v: Vehicle, want: Vector2i) -> Vector2i:
if want != Vector2i.ZERO:
v.heli_wobble_fx = Vector2i.ZERO
return want
var w := v.heli_wobble_fx
var old := w
w.x = clampi(w.x + v.rng.below(0x28f) - 0x147, -0x7ae, 0x7ae)
w.y = clampi(w.y + v.rng.below(0x28f) - 0x147, -0x7ae, 0x7ae)
v.heli_wobble_fx = w
return oldTranslation:
- The rule for choosing the spin's rate reads: if the spin is pointing away from where the keys want it, or there is no key and the spin is still turning one way, it changes at the faster turn_down rate. Otherwise it builds up at turn_up. The float port had the same rule, written with
1 / 65536tolerances. - The module's parameters stay in the pack as units (
0x7ae / 65536.0and so on). The newVehicleModule.fx(name)reads one back as its 16.16 integer, as the vehicle does with its drive numbers (document 161). - The fixed tick removes the float port's fractional-tick counter for the drift (
heli_wobble_acc): the drift takes exactly one step a tick. - The "is it moving?" test, a port choice (document 63), compares the squared velocity with
0xb85squared (1.5 ×0x7ae) in integers.
0040e836 PUSH 0x180
0040e83b CALL 0x0041d3d0 ; rand(0x180)
0040e84a CALL 0x0041d3d0 ; + rand(0x180)
0040e854 SUB ESI,0x180
0040e85d SHL ESI,0x7
0040e863 MOV dword ptr [EDI + 0x8c],ESI ; spin
0040e869 TEST EBX,EBX ; a hitter?
0040e872 CALL 0x0041d3d0 ; rand(0x100)
0040e87a LEA ESI,[EAX + 0x80]
0040e880 SHL ESI,0x7 ; push
0040e883 MOV EAX,dword ptr [EBX + 0x4c] ; the hitter's heading
0040e886 SAR EAX,0x10
0040e88e LEA EBX,[ECX*0x4 + 0x481390] ; its direction step
0040e897 CALL 0x00410b70 ; vel.x = mul(dir.x, push)
0040e8a7 CALL 0x00410b70 ; vel.y = mul(dir.y, push)
func on_hit(v: Vehicle, hit_heading_deg: float) -> void:
v.heli_omega_fx = (v.rng.below(0x180) + v.rng.below(0x180) - 0x180) << 7
if not is_nan(hit_heading_deg):
var push := (v.rng.below(0x100) + 0x80) << 7
# the hitter's heading is still in the port's degrees (projectiles are S5.2): its step, as the original's heading >> 16
var dir := Vehicle.dir_fx(floori(fposmod(hit_heading_deg + 90.0, 360.0) / 5.625) << 16)
v.heli_vel_fx = Vector2i(Fx.mul(dir.x, push), Fx.mul(dir.y, push))Translation: the hitter's heading arrives in degrees because projectiles are still float. Its step is computed exactly as the float port did, and when projectiles are converted (S5.2) it will be their heading_fx >> 16.
The same golden run as in document 161, before and after:
- Tank, Jeep, MSV: identical on every tick.
-
Heli: the same speed, heading, height, rotor and bank on every tick. The position differs by at most 0.0044 units over 600 ticks. The integer table's entries are truncated (
Fx.sin), where the float port used exactcosandsin; on a long straight run the two paths separate by a few thousandths. - The scripted flight does strafe while the spin dies away: the turn key is let go at tick 310 with a strafe key held, and the heading moves on until tick 315. It still stays within 0.002 units. The spin is at most a quarter step a tick, so the old and the new heading fall in different steps only on a tick that crosses a step boundary, and the velocity takes many ticks to follow what is wanted (
0x7aea tick against0xccccof strafe).
The vehicle behaviour trace check's Heli recording was rewritten from the new code.
-
Engine checks: 55 of 55 pass, including
wreck_check, whose hover-drift test now stepshover_wobblein integers. -
Openfire checks: 72 of 73 pass.
ai_jeep_gun_checkfails on some runs, before and after this change alike (one of four runs on the previous engine). It plays RFMAP156, which has no Heli, with a random match seed and the computer player on the global random generator. -
float_lint_check: the baseline drops to 2,185.
-
The height
zand the climb: whole multiples of 1/65536 that add exactly in a float. They go with the rest of the height code. -
The rotor's start-up and landing ramps (
heli_spinup_stage,rotor_speed_steps): only the drawing, the sound cues and the order of the start-up read them. - The hitter's heading in the hit reaction, until projectiles are converted (S5.2).
In the engine (alexdia25/openfire-engine):
-
game/vehicle_modules/rotor_drive.gd:tick,hover_wobble,on_hit,approach -
game/vehicle_modules/vehicle_module.gd:fx(name) -
game/vehicle.gd:heli_omega_fx,heli_vel_fx,heli_wobble_fx,bank_fxand their views -
game/sim/sim_hash.gd: the four views skipped
Commits: engine aec5e4a, openfire 6c7cb8d.
Next: S5.2, projectiles (document 163).
Return Fire, rebuilt
Elsewhere