Repository navigation
114 worked example the gun turrets
Question: issue #33 asks what the original's enemy AI is. Document 113 surveys the enemy objects and finds that the main one on the ground is an object named Turret Gun (class 2) that no earlier document read. What is it, which tiles are turrets, how does one behave, how do you kill it, and how many does a level have? Scripts and field names are in the tracing cheat sheet.
A turret is a tile until an enemy vehicle looks at it. Two building ids (49 and 50) are turret tiles: 5 hit points, drawn as a red panel. When such a tile is drawn in the view of a vehicle of the other team, the draw code creates a Turret Gun object on it, linked to that vehicle, and swaps the tile to a placeholder id. The object waits a random 0-255 ticks, then slews at 70 degrees/s toward the vehicle, raises or lowers the gun within 33.75 degrees, and from 192 units fires a tracer (damage 1.0) and a missile (damage 1.5) once a second. If its tile has not been drawn for 301 ticks the object removes itself and the tile is a plain turret tile again. Shells damage the tile (5 hits for a 1.0 shell); when its hit points run out the tile is destroyed. Level 1 has none; 200 of the 204 levels have them, a median of 158 enemy turret tiles in a one-player level.
Every class table the program can create was found by scanning the data segment for the pattern (class number, pointer to a name, three code pointers) (document 113, Step 2). Two are named Turret Gun, class 2:
0x44e2d0 2 "Turret Gun" init 0x431b20 end 0x431be0 tick 0x431ba0 draw 0x44c0f8 first behaviour 0x431ce0 +0x30 = 0x32 hit 0x431c90
0x44e320 2 "Turret Gun" init 0x431b20 end 0x431be0 tick 0x431ba0 draw 0x44c158 first behaviour 0x431ce0 +0x30 = 0x32 hit 0x431c90
(slot meanings as in document 112, plus +0x3c = the hit handler, document 115 Step 1). They differ only in the draw descriptor. FindPointerRefsMulti.java 44c0f8 44c158 finds those two descriptors referenced from the coastal table (0x447030 + id * 0x38, document 93) at 0x447af0 and 0x447b28, which are field [2] of the entries for ids 49 (0x447030 + 49 * 0x38 = 0x447ae8) and 50 (0x447b20). DumpDwords.java confirms the entries: id 49 reads [0]=0x31 [2]=0x44c0f8 [4]=0x561 [7]=0x431a90 [10]=0x443e10, id 50 [0]=0x32 [2]=0x44c158 and the same tail ([0] is the id itself). Their entries in coastal_damage.json: hp 5, base_art 97 (the ground drawn under them, terrain.ground.red_panel.01), destroyed state id 60.
The raw tile bytes that produce those ids (tools/data/tile_lookup_tables.json, primary table) are 174 and 195 (team 0) and 214 and 235 (team 1): ids 49 / 50, with param4 0 or 1, which becomes bits 14-15 of the tile word, the tile's team (document 44).
FUN_004321b0(tile, vehicle), the only creator, decompiled:
if ((*tile & 0xe000000) == 0) return 0; // no hit points left: not a turret any more
FUN_0042e450(tile, &x, &y); // the tile's centre
desc = ((*tile & 0x3f80) == 0x1900) ? 0x44e320 : 0x44e2d0; // id 50 or any other id (49)
obj = FUN_0042c290(desc, (*tile & 0xc000) >> 14, x << 16, y << 16, 0, vehicle); // team = the tile's team
obj->+0x60 (byte) = (*tile >> 7) & 0x7f; // remember the tile's real id
obj->+0x61 (byte) = (*tile >> 25) & 7; // and its hit points
*tile = (*tile & 0xffffed7f) | 0x2d00; // the tile's id becomes 0x5a = 90 while the object lives
FUN_0042e150(tile, &obj->heading, &obj->pitch); // initial facing, from the per-tile block (Not traced)
obj->+0x6c = obj->heading; // the rest headingWho calls it is the interesting part. FindCallRel.java 4321b0 finds one call, 0x42e1fe, inside 0x42e180, a function Ghidra never made (DisasmForce.java 0x42e180 0x42e240). It is the init callback of the two draw descriptors (FindPointerRefsMulti.java 42e180 gives 0x44c11c and 0x44c17c, which are 0x44c0f8 + 0x24 and 0x44c158 + 0x24: cheat sheet, descriptor +0x24). So it runs while a turret is being drawn:
0042e18f MOV EDI,[ESI+0xc]
0042e192 JZ 0x42e1ba ; third argument 0: we are drawing the TILE, not an object
0042e194 MOV word ptr [ECX+0x62],0 ; third argument = the turret object: its age is reset to 0 (it was just drawn)
0042e19a MOV [ESI+0x2c],ECX ... ; and the draw object copies its heading and pitch
0042e1ba MOV EBX,[EBP+0xc] ; the tile word's address
0042e1bf TEST EAX,0xe000000 ; JNZ ; hit points left?
0042e1d7 AND EAX,0xc000 ; SHR EAX,0xe ; the tile's team
0042e1e2 MOV ECX,[0x48ca28] ; the view being drawn ...
0042e1e8 MOV ECX,[ECX+0x6c] ; ... and the vehicle it follows
0042e1ef CMP [ECX+0x10],EAX ; JZ 0x42e214 ; same team as the tile: no turret
0042e1f4 MOV EAX,[ECX+0x14] ; CMP [EAX],1 ; JNZ ; the viewer must be class 1, a Vehicle
0042e1fc PUSH ECX ; PUSH EBX ; CALL 0x4321b0 ; wake the turret, target = the viewing vehicle
Translation: a turret tile that is drawn in the view of a vehicle of the other team creates a Turret Gun that targets that vehicle. The same callback, run on the object each frame it is drawn, resets its age, which is Step 3's keep-alive. DAT_0048ca28 is the view FUN_00408d60 is drawing.
Consequences, all from that one callback: turrets exist only near a player's screen (a map with 3,000 turret tiles does not simulate 3,000); each is hostile to one specific vehicle (its +0x2c target); in a two-player game each player's view wakes the turrets of the other team; a vehicle can only wake turrets it can see.
The port code, built in document 126. The wake is
MatchController._wake_turrets, run for every vehicle that has a view. The port has no per-tile draw callback, so
it uses the view's area on the ground (or a square of the pack's wake.half_extent) in place of "this tile is
being drawn":
# game/match_controller.gd, _wake_turrets(viewer), for each turret tile in the viewer's area
var t := Vector2i(int(e["x"]), int(e["y"]))
var id := int(e.get("coastal_id", 0))
if not ids.has(str(id)) or _dead_turrets.has(t):
continue
if turrets.has(t):
(turrets[t] as Turret).seen() # drawn again: the age goes back to 0
continue
var side := int(e.get("variant", 0))
if side == viewer.player_index() or _tile_hp.get(t, _initial_tile_hp(t)) <= 0:
continue # no longer a turret tile (destroyed), the viewer's own side's, or no hit points left (FUN_004321b0's first test)
var tu := Turret.new(pack.turret, t, id, side, Vector2(cx, cy), level.facing_at(t.x, t.y), viewer) # target = the viewer
...
turrets[t] = tuInit FUN_00431b20 (param_2 the object):
if (param_3 != 0) FUN_0042cc50(obj, param_3); // link: target = the vehicle
obj->+0x62 = 0; // age
obj->+0x64 = (FUN_0041d3d0(300) & 0xff) + now; // wake-up time: 0..255 ticks from now
obj->+0x68 = 0; // next shot allowed: now
obj->+0x70 = (descriptor == 0x44c158) ? 0x44be38 : 0x44bbc0; // the constants (Step 5)
DAT_0045ae60[obj->team] += 1; // live turrets per teamTick FUN_00431ba0 (slot +0x10):
if (obj->+0x18 != 0 && (char)obj->+0x61 > 0 && (obj->+0x62 += dt) < 0x12d) // behaviour set, hit points, age < 301
(*obj->+0x18)(obj); // run the behaviour
else
FUN_0042c0f0(obj); // removeSo the object is removed when its age reaches 301 ticks (4.8 s) without being drawn, when it has no behaviour (it finished returning to rest), or when its stored hit points are 0.
End handler FUN_00431be0 (runs on removal):
tile->id = obj->+0x60; // give the tile its real id back
if ((*tile & 0xe000000) != 0) return 1; // still has hit points: it is a turret tile again
*tile = (*tile & 0xf3ffffff) | 0x2000000; // 1 hit point,
FUN_0042e8c0(0xff0000, obj, tile, &entry[obj->+0x60]); // then 255 damage: the tile's own destruction (document 93)and the per-team counter is decremented (and DAT_0044e2c8 reset when the last of a team goes).
The port code (game/turret.gd): the init's random delay, and the tick's keep-alive:
# game/turret.gd
func _init(config: Dictionary, at_tile: Vector2i, id: int, side: int, centre: Vector2, facing: Vector2, victim: Node2D) -> void:
...
rest_heading = facing.x # obj->+0x6c
target = victim # FUN_0042cc50: the link
var w: Dictionary = cfg.get("wake", {})
wake_at = float((randi() % int(w.get("delay_mod", 300))) & int(w.get("delay_mask", 255))) # (FUN_0041d3d0(300) & 0xff) + now
func seen() -> void:
age = 0.0
func tick(ticks: float) -> void:
if finished:
return
now += ticks
age += ticks
if state == State.DONE or age >= float(cfg.get("wake", {}).get("keepalive_ticks", 301)):
finished = true # FUN_00431ba0: no behaviour left, or not drawn for 301 ticks: FUN_0042c0f0
return
match state:
State.WAIT:
if wake_at < now:
state = State.TRACK
State.TRACK:
_track(ticks)
State.FIRE:
_fire()
State.RETURN:
_return(ticks)The whole life of a turret, from Steps 2 to 5. The four behaviours below are the inner states. Leaving the inner box (for any of the three reasons on the right) runs the end handler:
Mermaid source
stateDiagram-v2
direction TB
[*] --> Tile: ids 49 / 50,<br/>5 hit points
Tile --> Object: drawn in the view of<br/>an other-team vehicle (FUN_004321b0)
state Object {
[*] --> WAIT
WAIT --> TRACK: random 0-255 ticks over
TRACK --> FIRE: lined up, under 192 units,<br/>cooldown over
FIRE --> TRACK: projectiles 3 (+ 4),<br/>next shot in 60 ticks (30)
TRACK --> RETURN: target gone
RETURN --> [*]: at rest, behaviour = 0
}
Object --> EndHandler: behaviour 0, or 301 ticks undrawn,<br/>or hit points 0
EndHandler --> Tile: hit points left,<br/>a turret tile again
EndHandler --> Grass: none left, 255 damage,<br/>id 60 with an explosion
Grass --> [*]
Each frame the object is drawn, the same draw callback resets its age to 0, so the 301-tick limit only runs out once no view shows it.
The first behaviour (+0x1c) is WAIT, FUN_00431ce0:
if (obj->+0x64 < now) obj->+0x18 = FUN_00431d00; // the random delay is over: TRACK
return 1;TRACK, FUN_00431d00 (force-decompiled, trimmed):
target = obj->+0x2c;
if (target == 0) { obj->+0x18 = FUN_00432060; return 1; } // vehicle gone: RETURN
A = FUN_00410b80(obj->pos - target->pos) >> 2, a quarter turn back; // the exact heading toward the target (document 167; read here first as FUN_00422e70)
dist = FUN_0042cd70();
// bookkeeping: DAT_0044e2c8[team] = the smallest distance of any turret to a vehicle, DAT_0045ae30[team] = that turret's position
turn obj->heading toward A by 0x3333 * dt; // 1.125 degrees per tick
if (obj->heading == A && dist < 0xc00000) { // lined up and inside 192 units
e = FUN_00410b80(dist, type[+8] - target->z) >> 2; // the angle up or down to the target
if (0x60000 < e && e < 0x3a0000) e = (e <= 0x200000) ? 0x60000 : 0x3a0000; // snap into +/-33.75 degrees
fire_ready = 1;
}
turn obj->pitch toward e (0 while not lined up) by 0xa3d * dt; // 0.225 degrees per tick
if (fire_ready && obj->+0x68 < now) obj->+0x18 = FUN_00431f70; // FIRE
return 1;FIRE, FUN_00431f70:
FUN_00415480(&obj->pos, obj->+0x70, obj->heading, obj->pitch, 3, team, obj); // projectile type 3
if (DAT_0048c310 == 0) {
FUN_00415480(&obj->pos, obj->+0x70, obj->heading, obj->pitch, 4, team, obj); // projectile type 4
next = now + 0x3c; // 60 ticks
} else
next = now + 0x1e; // 30 ticks
obj->+0x68 = next; obj->+0x18 = FUN_00431d00; // back to TRACKRETURN, FUN_00432060 (no target): turn the heading back to the rest heading +0x6c at the same 0x3333; only once it is there, bring the gun to 0 at 0xa3d (the arrival tick falls through to it); then set +0x18 = 0 so the tick function removes the object.
Corrected by document 167 (stage S5.5b): this document first read TRACK's bearing as
FUN_00422e70(whole steps); the code callsFUN_00410b80itself, so the bearing is exact. It also read RETURN as turning the heading and the gun together; the gun waits for the heading. Both are fixed above, and the port follows them.
Translation, with the numbers worked out (the tick is 16 ms):
- Delay before the first move: 0-255 ticks, up to 4 s, uniform.
-
Traverse:
0x3333raw per tick = 1.125 degrees = 70 degrees/s; elevation0xa3d= 0.225 degrees per tick = 14 degrees/s, limited to plus or minus 33.75 degrees (0x60000). While the heading is not yet lined up the gun is lowered towards 0. -
Range:
0xc00000= 192 units (6 tiles). It does not shoot farther, and there is no minimum. -
Shots: projectile type 3 (class
TRACER, speed 6.0, damage 1.0) and type 4 (classMissle, speed 4.0, damage 1.5), both fromprojectile_types.json's 12-entry table, every 60 ticks (0.96 s), fired from the muzzle(0, -15, 24.5)units (+0x188000; the id-50 variant29.5) rotated by the heading (0x44bbc0/0x44be38, fields[1]and[2], passed toFUN_00415480as the offset). It fires as soon as it is lined up in range; the gun does not have to have finished raising. - No prediction, no cover, no leading. It aims at where the vehicle is now.
-
Visible evidence, weak. The reference footage (
Silent Software - Return Fire - 1996.mkv, one view, a one-player campaign) shows green gun emplacements set into walls and beside buildings in many of 20 frames sampled 48 s apart, with orange flashes at them in the frame at 780 s. A still frame cannot confirm the firing rules (the flashes may be hits or muzzle flashes); it only confirms that the emplacements exist and that they are the enemy's visible armament.
The port code, the three other behaviours. One change from this document's reading of FIRE: document 126
found that projectile type 3 is a tracer that probes the line of fire, not a shot. So the port fires only the
missile, and only when the probe finds no cover of the turret's own side in the way:
# game/turret.gd
func _track(ticks: float) -> void:
if not _alive_target():
state = State.RETURN # the link was cleared: FUN_00432060
return
var want := Soldier.heading_to(position, target.position) # FUN_00422e70: whole steps, rounded down
var dist := position.distance_to(target.position)
heading = turn_heading(heading, float(want), float(cfg.get("turn_steps_per_tick", 0.2)) * ticks) # 0x3333 * dt
var aim := 0.0
var lined_up := false
if heading == float(want) and dist < float(cfg.get("range", 192.0)): # 0xc00000
var limit := float(cfg.get("pitch_limit_steps", 6.0)) # +/-33.75 degrees
aim = clampf(rad_to_deg(atan2(muzzle_height() - float(target.get("z")), dist)) / STEP_DEG, -limit, limit) # FUN_00410b80: the angle down to the target
lined_up = true
pitch = move_toward(pitch, aim, float(cfg.get("pitch_steps_per_tick", 0.04)) * ticks) # 0xa3d * dt
if lined_up and next_shot < now:
state = State.FIRE
func _fire() -> void:
var blocked := 0
if probe.is_valid():
blocked = int(probe.call(self)) # the tracer (type 3): FUN_00415480 makes it and its init FUN_00414ef0 runs the whole line at once
if blocked == 0:
if fire.is_valid():
fire.call(self, int(cfg.get("missile", 4)))
next_shot = now + float(cfg.get("cooldown_ticks", 60))
else:
next_shot = now + float(cfg.get("cooldown_blocked_ticks", 30)) # own cover in the way: no missile, try again sooner
state = State.TRACK
func _return(ticks: float) -> void:
heading = turn_heading(heading, rest_heading, float(cfg.get("turn_steps_per_tick", 0.2)) * ticks)
pitch = move_toward(pitch, 0.0, float(cfg.get("pitch_steps_per_tick", 0.04)) * ticks)
if heading == rest_heading and pitch == 0.0:
state = State.DONE # +0x18 = 0: the tick removes itThe hit handler (slot +0x3c, 0x431c90, DisasmForce.java 0x431c90 0x431ce0):
00431c99 MOV CL,[ESI+0x60] ; the tile's real id
00431ca6 LEA EAX,[ECX*8 ... + 0x447030] ; its coastal entry
00431cb7 CALL 0x42e7f0 ; FUN_0042e7f0(damage, pos, tile, entry)
00431cc1 JZ ... ; PUSH ESI ; CALL 0x42c4d0 ; if it returned non-zero: remove the object
FUN_0042e7f0 is the tile damage function (document 93: armour, multiplier, whole hit points, at least 1) without the tile's state change: it returns 1 when the hit points reach 0. So a turret has the tile's 5 hit points: five 1.0 hits, or a 4.0 bomb (which leaves 1) and one more, and a hit that leaves exactly 1 hit point throws infantry if the entry asks (document 116; ids 49/50 do not). On the killing hit the object is removed, and its end handler (Step 3) does the real destruction: the tile becomes id 60 (grass) with the usual explosion.
decorations in level.json carries every tile's coastal id and variant (the team). Counting ids 49 and 50:
| LEVL | levels | median enemy turret tiles (team 1) | median own-team tiles (two-player only) |
|---|---|---|---|
| 0 | 3 | 0 | 1 |
| 1 | 24 | 18 | 16 |
| 2 | 20 | 35 | 33 |
| 3 | 21 | 69 | 64 |
| 4 | 27 | 116 | 106 |
| 5 | 24 | 123 | 93 |
| 6 | 28 | 174 | 137 |
| 7 | 25 | 231 | 209 |
| 8 | 32 | 373 | 325 |
200 of 204 levels have team-1 turrets; the largest (RFMAP204, two-player) has 3,074 turret tiles of both teams; level 1 (RFMAP001) has none, level 2 has 60. In a two-player level each team has its own turrets that wake only for the other player.
- Built since (issue #75): see document 126, which also traces the initial facing and the head's five quads. It corrects this document: the tracer (type 3) is a line-of-fire probe that is never drawn and does no damage; only the missile (type 4) hurts. What follows is the state before that build.
-
Applied in the port: nothing. No turret exists, which is invisible in level 1 and the first thing a player meets in level 2. Tiles 49/50 are drawn and damaged like any building (they have hit points in
coastal_damage.json) but never shoot. - To build it: a tile-wake hook in the terrain renderer ("a tile of id X is in the camera's view and the camera's vehicle is on another team": the engine already knows the camera and the tile list), a
Turretobject with the WAIT / TRACK / FIRE / RETURN states, a pack tableenemies/turret.json(ids 49/50 and their constants: delay, traverse, elevation limits, range, cooldown, muzzle offsets, projectile types, keep-alive), and the two projectile types, already extracted. The hit points are already in the tile damage code (MatchController._damage_tile_amount), so a turret hit costs nothing extra.
-
Initial facing.
FUN_0042e150reads the new object's heading (byte 3 & 0x3fsteps) and pitch (byte 0 << 14) from a 4-byte per-tile block (FUN_0042e4c0: array at0x45aa20). Where that block is filled is not traced, so what a turret looks like before it sees you (and which way it returns to) is open. Until it is, face it along the tile's team direction and mark that as a choice. -
DAT_0048c310(Step 4's FIRE) is read here, in the Drone and in projectile code and written nowhere found (document 115, Not traced). Treated as 0: the two-shot, 60-tick cycle above. -
The turret's draw callback and parts (
0x44c0f8/0x44c158: the red panel's turret top, barrel and muzzle ring). The registry has no names for them yet. -
What the nearest-distance bookkeeping (Nothing: document 167 found it written by TRACK and FIRE, reset by the end handlerDAT_0044e2c8,DAT_0045ae30) feeds.FUN_00431be0, and read nowhere. -
The size word at class
+0x30(0x32).
Return Fire, rebuilt
Elsewhere