Skip to content

130 worked example building the drone

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

130. Worked example: building the Drone, the art, the gun that can only point so far down, and what the port does

Question: document 115 traced what the Drone is and does, and left its art, its radar blip, the view counter's index and one number (T[11]) open. Issue #76 builds it. 157 of the 204 levels give each side Drones (difficulty 3 and up, or the level's unk4), so this is the object most levels have that the port did not. Scripts and field names are in the tracing cheat sheet; the headless Ghidra scripts are ForceDecompile.java (for code only reached through a function pointer), DumpDescriptorParts.java, DumpDwords.java, DumpShapes.java.

The answer in one paragraph

The Drone is a 15-quad aircraft, drawn the way the Heli is, whose first eight corners are a slab turned by the gun's pitch and whose last six are a spinning bar. Its art is descriptor 0x443740: a 16 x 16 x 2 slab (cels 610, 608, 612-624, each with a team variant), a fin, two forward barrels, and, drawn last, two quads of the "tint" cels 587 and 583 on a bar 16 wide and 32 long at 6 units that turns one heading step a tick. Its gun can only be pointed 5 steps (28 degrees) below the body's pitch: the constant document 115 read as 5 is 0x50000, 5.0 steps. A Drone 50 units up that shoots at a vehicle on the ground must therefore be pitched nose-down, and it is: banking while it flies fast tips the body down by up to 6 steps, so the shot is possible exactly while it closes on the vehicle (32 to 80 units out). The tracer's result DAT_0048c310 is the line-of-fire probe document 126 traced; the Drone shares it. Everything is built: game/drone.gd, game/drone_view_3d.gd, the section "The Drone" of game/match_controller.gd, the pack table enemies/drone.json, a radar blip, the hum, and the wreckage.

Step 1: the art

The draw callback is FUN_00409ef0 (no function was recognised there; ForceDecompile.java makes one). With param_1 the draw item, iVar1 its object (item+0x2c) and iVar2 the pool record (obj+0x6c):

*(int *)(iVar2 + 8) = DAT_00480d38;                                    // record +8: the tick it was last drawn
FUN_00410c60(&DAT_00443178, &DAT_004433b8, pitch_row(iVar2->+0x1c >> 16), 8);   // corners 0-7 := the box (corners 48-55) turned by the GUN's pitch
item->+0x20 = obj->+0x68;  item->+0x30 = obj->+0x58;                   // the body's pitch and roll
FUN_00410c60(&DAT_00443370, &DAT_00443418, heading_row((obj->+8 + DAT_00480d38) & 0xf), 6);   // corners 42-47 := the bar (56-61) turned by a row that steps every tick
FUN_0041b590(param_1, param_2, 0);                                     // the generic draw: pitch, roll, heading, camera

FUN_00410c60(dst, src, matrix, n) multiplies n corners by a 3 x 3 (row vectors). The two source blocks sit past the 50-corner array of the descriptor: corners 48-55 are the box (x and y +-8, z -2 and 0) and 56-61 the bar's six points. The part list (DumpDescriptorParts.java 443740):

part cel flags corners what
0 610 0x308 4 5 6 7 the slab's top (box corners, so it follows the gun's pitch)
1, 2 608 0x208 7 4 0 3 / 6 5 1 2 its two sides
3, 4 612 0x208 12-15 / 16-19 two panels at x -3 and +3
5 624 0x308 8 9 10 11 the deck at z 4
6 616 0x108 28-31 the back
7, 8, 9 618 0x108 20 21 26 27 / 21 22 25 26 / 22 23 24 25 the nose, three faces
10, 11 620 0x208 33 32 34 37 / 33 32 35 36 the two barrels
12 622 0x108 38-41 the barrels' ends
13, 14 587, 583 0 46 42 43 47 / 45 46 47 44 the bar, two halves

flags & 8 adds the team (obj+8, the side the Drone belongs to) to the cel, as in the turret's head; flags & 0x300 is the piece class the wreckage uses (Step 4). The painting order is the byte list of the heading octant (all fifteen parts, no part culled; art.orders), the pose the generic draw FUN_0041b590: the pitch about the lateral axis, the roll about the long one, then the heading row, then the camera. At full speed the pitch is +6 steps (nose down), which is why the barrels point at the ground it is flying over.

The shadow is a separate object (class 4, descriptor 0x4437f0, update FUN_00409bd0 as for every shot): one quad of cel 626 (effect.shadow.soft.018, 32 x 32, part flags 0x10, translucent) at the Drone's x + 0.332 z, y - 0.5 z, turned to its heading. It is the same object that casts the Heli's shadow, with the Drone's descriptor.

The radar blip is FUN_00413100(drone, 0x4438a0, 1000): a blip object (class 5, callback 0x40aca0 follows the Drone and removes itself when it is gone) whose shape 0x443888 is five points, a plus: (-1,0) (0,0) (0,1) (0,-1) (1,0), each with two colours, 205 for side 0 and 104 for side 1 (the vehicles' blips, descriptor 0x4404b0, do the same with their own colours). It draws every frame, not blinking.

Step 2: what document 115 had wrong

  • T[11] is 0x50000, not 5. In FUN_0040a280 the elevation to the vehicle is FUN_00410b80(dist, dz) >> 2 (an angle in 1/65536 steps, positive down), the wanted gun pitch is that minus the body's pitch, and:
uVar8 = (elev - obj->+0x68) & 0x3fffff;                    // against the BODY's pitch
if (uVar8 < 0x200001) {                                    // up to half a turn: needs the gun down
  bVar2 = true;  if (T[11] < uVar8) { bVar2 = false; uVar8 = T[11]; }   // more than 5.0 steps: it cannot, and the gun stops at 5
} else if ((int)uVar8 < (int)(0x40 - T[11])) {             // never true: 0x40 is a count, T[11] a 16.16 value
  bVar2 = false; uVar8 = 0x40 - T[11];
} else bVar2 = true;                                       // above the body's pitch: the gun just follows
gun_reached = FUN_004319e0(&record->+0x1c, uVar8, T[10]);  // 0.3 steps a tick
if (gun_reached && bVar2 && cooldown over && 32 < dist < 80 && |heading - A| < 5 degrees) fire

Document 115 read T[11] as 5 raw units, which would have made the second branch live and the first nearly never pass. With 5.0 steps the Drone fires while the vehicle is within about 28 degrees of where the body's nose points; the body's pitch (obj+0x68) is the banking look (Step 3), so the shot depends on it.

  • DAT_0048c310 is the tracer's answer (document 126, issue #79): FUN_00415480 with type 3 makes the invisible probe; when its line meets a tile of the Drone's own side first, the Drone shoots nothing and its interval restarts (10 ticks). It is only used against something above 2 units high (a Heli): a vehicle on the ground gets the shot without it.
  • The shot's heading and pitch are exact, not the Drone's: FUN_00415480(pos, muzzle, A, elev, type, team, shooter) flies the shot along A (the heading from the Drone to the vehicle, not rounded) and elev; only the muzzle offset uses the whole-step rows. The Drone does not have to point at the vehicle precisely, only within 5 degrees (0.8889 steps) of it.
  • The view counter's index (+4 or +8) remains unproven; with one player both are 0.

Corrected by document 169 (stage S5.5d): FUN_00410df0 is a whole square root: the magnitude is isqrt((vx >> 8)^2 + (vy >> 8)^2) << 8, so it moves in steps of 1/256 unit. The gains are used as T >> 16, so the pitch gain -3.5 is -4. T[0] is 0x3d7 (0.015, not 0.0149), and the acceleration, the climb and the gun's turn rate are 0x4ccc: full speed comes on the sixth tick. The bearing to the vehicle is exact (not a whole step), the distance whole units, and the shot leaves at that bearing and atan2(dz, d) >> 2. A shell meets only the Drone's body (16 units), not its 64-unit space box; the space box only bumps.

Step 3: the banking, and what it does to the aim

The movement FUN_0040a480 ends with the part document 115 skipped:

alpha = (heading - heading_of(velocity)) >> 16;                      // the angle between the way it points and the way it goes, whole steps
roll_target  = |v| * (T[5] >> 16) * sin_row[alpha];                  // T[5] = -6.0
pitch_target = |v| * (T[7] >> 16) * (-cos_row[alpha]);               // T[7] = -3.5, floored to -4
roll  := turn(roll,  roll_target,  T[6]);   // 0.5 steps a tick
pitch := turn(pitch, pitch_target, T[8]);   // 0.5 steps a tick

FUN_00410df0 hides its argument; it is taken here as the length of the velocity, so that << 8 gives it in 16.16 (an inference: with it the pitch at the speed cap is +6 steps, the magnitude of T[5], which reads as the intended look; a different scale would not change which way the Drone tilts, only how far). Flying straight at 1.5 units a tick: pitch = 1.5 x 4 = +6 steps, roll 0. A turn makes the velocity lag the heading, so alpha is not 0 and the Drone rolls by up to 6 x 1.5 = 9 steps. And with the nose 6 steps down the aim is easy: at 80 units out the vehicle is 5.7 steps below the horizon, 0.3 above the nose (bVar2 true by the "above" branch); at 32 units 10.2 steps, 4.2 below the nose (inside the 5.0 limit). A Drone that hovers or turns tightly (pitch near 0) cannot hit a vehicle on the ground (it needs more than 5.0 steps), which is why they come in passes.

The velocity chases speed * heading_row at T[0] = 0.0149 units a tick per tick, speed rises 0.3 a tick to 1.5. It takes about 100 ticks to turn its velocity round; the turn rates are 0.4, 0.2 or 1.0 steps a tick (Step 6 of document 115).

Step 4: dying

FUN_0040a010 (end handler) runs when the object is removed and record+0xe (killed) is set, by the hit handler 0x40a6e0 (damage 1.0 or more; a lighter shell returns 0 and flies on): the explosion 0x444c80 at its position and height, and FUN_0042b4e0 with the Heli's row 0x44aa90 (the "high" row: always, the Drone is above 10 units), the Drone's heading and body pitch, and the callback 0x40a120:

state->+0x34 += DAT_00457f98->+0x5c;     // the piece's drift word x  += the Drone's velocity x
state->+0x38 += DAT_00457f98->+0x60;     // ... and y

The pieces are the parts of class 1 and 2, ids 3 4 6 7 8 9 10 11 12 plus 1 2 (11 pieces: flags & 0x300 = 1 for 0x108, 2 for 0x208; the 0x308 slab top and deck stay), the corner positions as drawn (so the gun's pitch and the rotor's row apply). The drift words are real here: document 119 had drag (FUN_0042afa0) as a no-op because nothing set the drift; the Drone's callback does, and the piece's movement starts every tick from them (FUN_0042b290 copies +0x34/+0x38/+0x3c into the tick's movement). Records 0x44a850 / 0x44a864 carry drag of 0.015 a tick in x and y, so a piece keeps the Drone's speed for about 100 ticks and slows.

Applied in the port

  • The data is tools/data/drone.json, both importers write enemies/drone.json (_drone_table in importer/pack_builder.gd and the block in tools/build_pack.py, identical output: importer_parity_check), the radar's blip is drone_blip of tools/data/radar.json. The engine reads it as Pack.drone.
  • The object is Drone (game/drone.gd), pure logic, one tick at a time. The attack, as traced:
	var a := heading_steps_to(position, target.position)
	var off := fposmod(a - heading, 64.0)
	var dist := distance(position, target.position)
	var rate: float
	if off <= 16.0 or off > 48.0 - 1.0 / 65536.0:
		rate = float(fl["turn_ahead"])   # the vehicle within 90 degrees ahead
	elif dist > float(fl["far_distance"]):
		rate = float(fl["turn_behind_far"])   # behind and far: turn hard
	else:
		rate = float(fl["turn_behind_close"])   # behind and close: wide
	heading = Turret.turn_heading(heading, a, rate)
	var elev := rad_to_deg(atan2(z - float(target.get("z")), dist)) / STEP_DEG   # FUN_00410b80: the angle down to the vehicle
	var rel := fposmod(elev - pitch, 64.0)
	...
	if rel <= 32.0:
		if limit < rel:
			can_fire = false   # the gun cannot be pointed that far down against the body
			want = limit
	gun_pitch = Turret.turn_heading(gun_pitch, want, float(gn["pitch_rate"]))
	if gun_pitch == want and can_fire and next_shot < now and float(gn["min_distance"]) < dist and dist < float(gn["max_distance"]):
		var w := float(gn["aim_window_steps"])
		if fposmod(heading - a + w, 64.0) < 2.0 * w:
			_shoot(a, elev, gn)
  • The world is the "Drone" section of MatchController: the per-vehicle camp test (_camp_tick: a tile change restarts the timer, 360 ticks past it with fewer than 3 awake hostile turrets in view makes one), the spawn on one of the four borders of the rectangle round the vehicle (_spawn_drone; the original's refusal of a point inside another human player's rectangle has nobody to refuse it), the budget (_drone_cap: unk4, else the difficulty's table, at most 5, per side), the shots through _drone_fire (the pitched muzzle of FUN_00415480) and the probe (_probe_line, shared with the turrets), the hit (_shell_hits_drone: box +-8, z -4 to 6 about the Drone, damage below 1.0 passes), and the death (_kill_drone, _drone_debris: the explosion at 50 units through the new signal impact_effect_high, eleven pieces with drift = the Drone's velocity).
	var camp: Dictionary = pack.drone["camp"]
	if _camp_timer + float(camp["ticks"]) < _drone_clock:
		_camp_timer = _drone_clock
		if _hostile_turrets_in_view(v) < int(camp["hostile_turret_limit"]):
			_spawn_drone(v)
  • The view is DroneView3D: the parts as meshes in the descriptor's own axes, rebuilt when the gun's pitch or the rotor row changes, the body turned by Drone.pose_point (pitch, roll, heading), painted in the octant's order with no depth test (DecorationField3D.turret_head_material, priority 120 so a Drone is over a turret's head), the shadow, and the radar draws the blip. The hum is the loop Drone that TerrainView3D starts with the first Drone and stops with the last, following the Drone nearest the player.
  • Checked by the engine's tests/drone_check.gd (the maths, the flight and the gun on a fake world, the camp timer, the budget, the hum, a hit and the pieces, the turret gate) and openfire's tools/tests/drone_pack_check.gd (the real pack: the table's sprites, the budget by level, a 360-tick wait and a Drone on the Tank's border, strafing with type 11, the pool, a hit of 0.95 and of 1.0, the explosion at 50 units, eleven pieces carrying the velocity, three hostile turrets keeping it away, a Drone whose vehicle is gone leaving).
  • Debug: RF_DEBUG_DRONE=1 gives any level a budget of 2 and the first check at once; RF_DEBUG_DRONE=near also puts the Drone beside the vehicle, in view.

Follow-up: the bodies, the boxes and the Heli (issue #109)

The move FUN_0042c830 applies the step, then tests the new place (FUN_0042bd40 -> FUN_0042bb10), and undoes the step when the test says blocked. For each other object whose shape overlaps (FUN_0041e4c0: layers and masks, z, geometry) FUN_0042bb10 compares the class tables' priority words (+0x30: Vehicle 0x64, Drone 0x6e) and calls one handler (+0x38):

if (mover.prio < other.prio) {          // the other one answers; the mover is blocked only by an answer of exactly 4
    if (other.handler == 0) { if (mover.handler == 0) return blocked; if (mover.handler(mover, other) & 1) return blocked; }
    else if (other.handler(other, mover) == 4) return blocked;
} else {                                // the mover answers; blocked when bit 0 of the answer is set
    if (mover.handler == 0) { if (other.handler == 0) return blocked; if (other.handler(other, mover) == 4) return blocked; }
    else if (mover.handler(mover, other) & 1) return blocked;
}
  • A Drone's handler (0x40a760) stores the other object in the Drone's record (+0x18; for another Drone in both) and answers 5 (blocked) unless the shape it touched is the 64-unit box 0x4436c0, then 0. The Drone has two shapes: the 16-unit body (0x443700, layer 1, mask 0xff) and the 64-unit box (layer 0x20, mask 0x22), both z -4 to 6 about it.
  • A vehicle's handler (0x40c150) stores the other object in the vehicle's state (+0xa8) and answers 5. A vehicle moving into a Drone is the lower priority, so the Drone answers (not 4): a Heli is never blocked by a Drone, and the Heli's own handler is not called. A Drone moving into a Heli answers itself: blocked when its body touches the Heli's shape, only remembering it when just the 64-unit box does (the Heli's layer 2 is in the box's mask 0x22, and the box's layer 0x20 in the Heli's mask 0x27). The Heli cruises at exactly 50 units, the Drone's height, so this is common.
  • Two Drones have equal priority, so the mover's handler answers: the bodies block each other, the boxes only make both remember (the push of FUN_0040a480, 2 - distance / 16 away from the remembered object, now also from a Heli).
  • Applied in the port: Drone._move (the step, the test, the undo; a blocked climb is retried level, as FUN_0040a480 does) and MatchController._drone_collide (the other Drones' boxes, the vehicles' collision polygons at z 0 to 10). tests/drone_check.gd covers each case.

Port choices and what is not built

  • "Drawn" (the 60-tick removal of a Drone that has lost its vehicle) is a Drone within 288 units of the player's vehicle (flight.drift_view_half_extent), as for the turrets.
  • The camp test counts only the player's vehicle. The original applies it to every vehicle bound to a view (both human players); the port has one. The computer opponents (EnemyVehicle) do not attract Drones.
  • The camp clock does not run while the vehicle is frozen (the camera's swoop-in, the end of the match) or docked: the original's clock and the intro's interaction are untraced.
  • Terrain collision is not modelled: nothing but a Heli is at 50 units (see the follow-up section for the objects).
  • The roll's sign is the Heli's: both are drawn by FUN_0041b590, whose bank matrix [c 0 s; 0 1 0; -s 0 c] (document 63) raises the right side for a positive roll. The Drone's target roll is |v| * -6 * sin(heading - velocity heading), negative when it turns right, so it rolls into the turn like the Heli (drone_check covers it).
  • A shot at the Drone is checked against the box only for projectiles that pass _shell_hits_*: a shell must reach 50 units (a raised gun, a missile) to hit.
  • The shadow's soft cel draws as a ring with a silhouette (the sprite's own dither); it is the traced cel.

Next: Worked example: the gun turrets, the other object that fires at a vehicle.

Clone this wiki locally