Skip to content

174 deterministic sim 19 explosion damage boxes

Alex Diakovsky edited this page Nov 21, 2026 · 1 revision

174. Deterministic sim 19: the explosion damage boxes in 16.16 (stage S6.4a)

Question: a mine's explosion owns a damage box that hurts what it overlaps. The port stepped it with float progress, tested it against float polygons, and walked every tile under the box. What does the original's explosion step do on its integers, which cells and objects does the box meet, and what changes when the port does the same (document 155, issue #133)?

The deterministic simulation series: document 155 is the plan. Documents 156 to 160 are stages S0 to S4, and documents 161 to 170 are S5. S6 takes the last floats out of the simulation files before the check goes into CI. It is split into sub-stages, one per area: S6.1 the height and water (document 171); S6.2 the Heli's take-off and automatic landing (document 172); S6.3 the weapons (document 173); S6.4 the rest, itself in four parts: S6.4a the explosion damage boxes (this document), S6.4b the road assist (document 175), S6.4c the dock test and the rest of the match controller, S6.4d the view code fenced off from the lint; then S6.5 the CI runs.

The explosion records and their script were traced in document 60 (the mine and its explosion record 0x445058) and document 50; the box's damage rule in document 165; the cell scan and the chains in document 53 and document 164. This document reads the explosion's own step, FUN_0042dbe0, and its box ops.

Glossary

Term Meaning
16.16, raw As in document 161: 0x10000 is one unit.
explosion object The object an explosion record makes (class 4). obj+0x58 its box, obj+0x64 the record, obj+0x5c the script pointer, obj+0x64 (dword 0x19) the progress, obj+0x68 (dword 0x1a) the rate.
record An explosion record: +0xc the duration, then the script. The mine's is 0x445058: duration 25.0, rate 0x2aaa.
flag 0x40 Set on a new explosion object (obj+0xc); its first step clears it.
box A 0xb0-byte struct made by the script op 13 (FUN_0042d640). It is the object's descriptor (obj+0x3c) and holds one shape: type 2, layer 0x20.
chain The shapes of an object, tested in step by height (document 164). The port's Collision.chains_hit_fx.
cell scan FUN_0042bd40: the cells a mover tests: its own row (its cell, left, right), then the row above or below. The port's _scan_cells.
scan reach The descriptor's +0xc. The box's is 0.
dt DAT_00480d2c, the ticks this frame: always 1 in the port.

The answer, up front

  1. The first step sets the progress to exactly 1.0 (flag 0x40). Every later step adds the rate. The explosion ends on the step whose progress reaches the duration. The mine's explosion lives 145 steps, not the port's 150.
  2. WAIT n waits for the progress to reach n << 16. The mine's box grows to ±25 on step 20, shrinks to ±12 on step 21, then ±16 on step 44 and ±20 on step 68. The port had these on steps 25, 26, 49 and 73.
  3. DAMAGE_BOX z_lo z_hi w h mask damage makes the box from w × −0x8000 to its negation in x (the same from h in y), z from z_lo << 16 to z_hi << 16, layer 0x20, the op's mask, damage damage << 16. BOX_EXTENT n sets all three axes to −n << 16 to n << 16.
  4. The box is tested inside the explosion's own step, after the script, as a mover with scan reach 0. From its own row it scans its cell and both neighbours (reach 0 is not negative, so left and right are always scanned). It scans the row above only in the top half of its cell, and the row below only in the bottom half. A ±25 box can cover three rows; it meets only two.
  5. In each cell, the first tile shape the box meets gets the tile callback (FUN_0042dcd0 → FUN_0042e8c0). Then each object there gets the hit callback FUN_0042dd20, which calls that object's own hit handler with the damage. Only the box's mask is asked (0x43: layers 1, 2 and 0x40), not the other shape's. Edges that touch count.
  6. The damage is damage < 0 ? −(damage × dt) : damage: the mine's −1 is 1.0 a step, as before.

Step 1: the step, FUN_0042dbe0

// FUN_0042dbe0 (ForceDecompile.java 42dbe0), names added
void explosion_step(int unused, int *obj)
{
    int duration = *(int *)(obj[0x18] + 0xc);            // the record's duration
    if (duration <= obj[0x19]) { free_object(obj); return; }   // FUN_0042c0f0
    if ((obj[3] & 0x40) == 0) {
        obj[0x19] += obj[0x1a] * dt;                       // progress += rate
        if (duration <= obj[0x19]) { free_object(obj); return; }
    } else {
        obj[0x19] = 0x10000;                               // the first step: exactly 1.0
        obj[3] &= ~0x40;
    }
    char *pc = (char *)obj[0x17];
    if (*pc != 0) {
        while (*pc != 0) {                                 // op 0: END
            if (*pc == 1) { pc++; break; }                 // op 1: STOP, steps past and ends this step
            if (*pc == 2) {                                // op 2: WAIT n
                if (obj[0x19] < pc[1] * 0x10000) break;
                pc += 2;
            } else
                pc = op_table_0x44bae0[*pc](obj, pc + 1, 0);
        }
        obj[0x17] = (int)pc;
    }
    if (obj[0xb] != 0) FUN_0042daf0(obj, obj[0xb]);        // the drawn parts
    if (obj[0x16] != 0) FUN_0042bd40(obj);                 // the box: the cell scan, as a mover
}

The free (FUN_0042c0f0) runs the class's free callback FUN_0042dca0, which frees the box.

## FUN_0042dbe0, one tick (dt 1): false once the explosion is over (FUN_0042c0f0 frees it, and FUN_0042dca0 its box).
func advance() -> bool:
	if finished:
		return false
	if _progress_fx >= _duration_fx:
		return _finish()
	if _first:
		_progress_fx = 0x10000
		_first = false
	else:
		_progress_fx += _rate_fx
		if _progress_fx >= _duration_fx:
			return _finish()
	var script: Array = _record.get("script", [])
	while not _script_done and _pc < script.size():
		var op: Array = script[_pc]
		match op[0]:
			"END":
				_script_done = true
			"STOP":
				_pc += 1
				break
			"WAIT":
				if _progress_fx < int(op[1]) << 16:
					break
				_pc += 1
			...

With the mine's rate 0x2aaa, the progress after step s is 0x10000 + (s − 1) × 0x2aaa. It reaches 25.0 (0x190000) when (s − 1) × 0x2aaa ≥ 0x180000: s − 1 = 145, so step 146 frees it and the box lives 145 steps. The port started at 0 and added 0.16666 a step: 150 steps.

Step 2: the box ops, FUN_0042d640 and FUN_0042d710

// FUN_0042d640: op 13, DAMAGE_BOX z_lo z_hi w h mask damage (ForceDecompile.java 42d640)
if (obj->box == 0) obj->box = alloc(0xb0);
memcpy(obj->box, template_0x44bb40, 11 * 4);   // [0] 0x42dd90, [3] 0 (scan reach), [9] 0x42dd60
box[2]    = &box[0xb];                          // the descriptor's shape list: the box's own shape
box[0xb]  = 2;                                  // type 2, an axis box
box[0xe]  = op[0] << 16;  box[0xf] = op[1] << 16;          // z0, z1
box[0x12] = op[2] * -0x8000;  box[0x13] = op[3] * -0x8000; // x0, y0
box[0x14] = -box[0x12];  box[0x15] = -box[0x13];           // x1, y1
byte(box + 0x36) = 0x20;  byte(box + 0x37) = op[4];        // layer 0x20, mask
box[0x1a] = op[5] << 16;                                   // the damage
obj->descriptor = box;                                     // obj+0x3c
return op + 6;

// FUN_0042d710: op 14, BOX_EXTENT n
box->x0 = box->y0 = box->z0 = n * -0x10000;
box->x1 = box->y1 = box->z1 = n * 0x10000;
return op + 1;
			"DAMAGE_BOX":
				z0_fx = int(op[1]) << 16
				z1_fx = int(op[2]) << 16
				x0_fx = int(op[3]) * -0x8000
				y0_fx = int(op[4]) * -0x8000
				x1_fx = -x0_fx
				y1_fx = -y0_fx
				mask = int(op[5])
				damage_fx = int(op[6]) << 16
				active = true
				_chain = []
				_pc += 1
			"BOX_EXTENT":
				x0_fx = int(op[1]) * -0x10000
				y0_fx = x0_fx
				z0_fx = x0_fx
				x1_fx = int(op[1]) * 0x10000
				y1_fx = x1_fx
				z1_fx = x1_fx
				_chain = []
				_pc += 1

The box is a one-shape chain in Collision.shape_fx's form:

func chain_fx() -> Array:
	if _chain.is_empty():
		_chain = [{"type": 2, "layer": 0x20, "mask": mask, "b8": 0, "b9": 0, "z0": z0_fx, "z1": z1_fx, "off": Vector2i.ZERO,
			"box": PackedInt32Array([x0_fx, y0_fx, x1_fx, y1_fx]), "verts": PackedInt32Array(), "src": {}}]
	return _chain

The mine's script is DAMAGE_BOX -4 4 8 8 67 -1, SOUND 14, WAIT 4, BOX_EXTENT 25, DESC_TBL 1, STOP, BOX_EXTENT 12, DESC_DEFAULT, WAIT 8, BOX_EXTENT 16, WAIT 12, BOX_EXTENT 20, END. So the box is ±4 by ±4, z −4 to 4, on steps 1 to 19; ±25 on step 20; ±12 from step 21; ±16 from step 44; ±20 from step 68.

Step 3: the box as a mover, FUN_0042bd40

FUN_0042bd40 is the cell scan every mover uses (document 164), already ported as _scan_cells. With the box's reach 0:

// FUN_0042bd40, the parts that decide the cells (reduced)
reach = descriptor[3];                                    // the box: 0
scan_row(own row);                                        // FUN_0042bf30: own cell, then left and right (reach >= 0: always)
fy = obj->y & 0x1f0000;
if (fy - reach < 0x100000 || reach > 0) scan_row(row above);   // top half only
if (fy + reach > 0xfffff  || reach > 0) scan_row(row below);   // bottom half only

In each cell, FUN_0042bb10 walks the tile's chain against the mover's and gives the first shape that meets to the coastal callback, then the mover class's tile callback (FUN_0042dcd0 for an explosion), then each object in the cell to the mover's object callback (FUN_0042dd20). Both callbacks answer 0, so the scan goes on.

// FUN_0042dd20: the explosion meets an object (ForceDecompile.java 42dd20)
if (obj->box == 0) return 0;
int dmg = obj->box[0x1a];
if (dmg < 0) dmg = -(dmg * dt);
if (other->class->hit) other->class->hit(other, obj, dmg);   // class +0x3c
return 0;

// FUN_0042dcd0: the explosion meets a tile shape
if (obj->box == 0) return 0;
int dmg = obj->box[0x1a];
if (dmg < 0) dmg = -(dmg * dt);
FUN_0042e8c0(dmg, obj, tile, &coastal_table[id]);
return 0;
func _update_boxes() -> void:
	for entry in _boxes.duplicate():
		var b: ExplosionBox = entry["box"]
		if not b.advance():
			_boxes.erase(entry)
			continue
		if not b.active:
			continue
		var dmg := b.damage_for()
		var chain := b.chain_fx()
		var at := b.pos_fx
		var destroyed: Dictionary = entry["destroyed"]
		_scan_cells(at, 0, func(t: Vector2i) -> bool:
			if _in_level(t) and not destroyed.has(t):
				var th := _tile_shape_hit_fx(chain, at, 0, 0, t)
				if not th.is_empty() and _damage_tile_fx(t, int(th["id"]), dmg):
					destroyed[t] = true
			var g := _gate_in_cell(t)
			if g != null and not destroyed.has(g.tile) and not Collision.chains_hit_fx(at, 0, 0, chain, g.centre_fx, 0, 0, g.bars_fx()).is_empty():
				if _damage_tile_fx(g.tile, g.coastal_id, dmg):
					destroyed[g.tile] = true
			for v in _vehicles_in_cell(t):
				if not Collision.chains_hit_fx(at, 0, 0, chain, v.pos_fx, v.z_fx(), v.heading_fx, v.hit_chain_fx).is_empty():
					if v.take_damage_fx(dmg, -90.0):
						_register_hit(v)
			for s in soldiers.duplicate():
				...   # the soldier's chain: it dies
			for w in _wrecks_in_cell(t):
				...   # the wreck's chain: w.hit(dmg)
			for d in drones.duplicate():
				...   # the Drone's chain: d.hit(dmg, 0)
			return false)

The objects' hit handlers are the ones a shot calls (document 163): FUN_0040c460 for a vehicle, the soldier's death, FUN_0040cbe0 for a wreck, FUN_0040a6e0 for a Drone, and a gate's tile damage. The port had only vehicles, soldiers and tiles. The explosion's height is 0 and its heading 0.

The mine's detonation now hands the mine's own 16.16 place to the box (m.pos_fx).

The golden check

A dump on RFMAP002, match seed 1996: every seventh tile with collision shapes, 150 in all. A mine goes off at the tile's centre or at one of four offsets. A Tank stands 3 to 12 units away and a soldier near the mine. The dump logs, every step for 160 steps, the Tank's hit points, the soldier, the tile's hit points and the live boxes, and every tile destroyed.

Before and after, every case differs, and every difference is explained:

  1. The box lives 145 steps, not 150. Every Tank takes 0xb334 (1.0 less its armour) a step: 144 × 0xb334 after, 149 × 0xb334 before, from step 1.
  2. The box grows sooner. Tiles that fall when it reaches ±25 fall on step 20, not 25; at ±16 on step 44, not 49 and so on.
  3. The Tank is tested by its own chain, with the whole-unit segment test. In 37 cases the Tank stands 12 units east of the mine, its hull's near edge at 4.5 units, half a unit beyond the ±4 box. The integer test cuts differences to whole units, so parallel edges less than a unit apart touch (document 164, answer 4): the box hurts the Tank from step 1. The float polygon test did not, and the Tank was hurt only from step 24, when the box grew.
  4. Only two rows of cells. 42 tiles the float box destroyed are outside the cells the original scans: the row above when the mine is in the bottom half of its cell, or the row below when it is in the top half.
  5. Touching edges and the mask. 6 tiles are destroyed only now. In five, a tree's canopy or trunk touches the box exactly at its edge (the integer test counts the edge, as in document 164). In one, building 15's third shape has mask 2: the port asked the tile's mask for 0x20, the original asks only the box's mask.

The soldiers die in the same cases before and after.

Tests

  • Engine damage_fx_check: the mine's record step by step: the first step's progress 1.0, the box's numbers, 145 steps, the extents on steps 20, 21, 44 and 68.
  • Engine soldier_check: the box kills a soldier in it; from the bottom half of its cell, it kills a soldier in the row below and spares one in the row above, both inside the box.

Known differences from the original, kept

  • A destroyed tile is not hit again by the same box. The scene changes a destroyed tile's id when it applies tile_destroyed; until then the port's level still has the old id. The original changes the id at once. The destroyed set stands in for that.
  • The order. The original steps each explosion in the object list's order with every other object. The port steps the boxes after the mines, at the end of _update_mines, as before.
  • The rest of the record's script (sounds, the drawn parts, DESC_TBL) is the view's (document 50).

Where the code is

  • Engine game/explosion_box.gd: advance, chain_fx, damage_for, pos_fx.
  • Engine game/match_controller.gd: _update_boxes, _detonate_mine.
  • Engine tests/damage_fx_check.gd (_box_script), tests/soldier_check.gd.

Commits: engine 430116e, openfire d33c169.

S6.4a is done. Next: S6.4b, the road assist (document 175).

Clone this wiki locally