Skip to content
Merged
67 changes: 52 additions & 15 deletions specs/weapons.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,19 +406,47 @@ For each cell, in *z* then *x* order, for the cell's `unit` and then its `unit2`

For the cell's features, when the weapon does not have bit 14 of `flags` set: a cell whose `feature`
is 0xfffe is first replaced by its origin cell, read with `los_radar.GetMapCell` at (*x* − its
`offsetX`, *z* − its `offsetY`); a `feature` of 0xfffb or above is skipped. The distance from the
`offsetX`, *z* − its `offsetY`); a `feature` of 0xfffb or above is skipped, tested on the cell that results
from that replacement (so an origin cell whose own `feature` is 0xfffb or above is skipped). A `feature`
below 0xfffb is an index below `Game.featureCount` in every valid block, and an origin cell that does
not exist is a case no valid block has: both are outside the contract, and no recording holds
either. The distance from the
centre to the feature is, when the visited cell's `flags` bit 0 is set, the length of the centre minus
`Game.spots[cell.spot].pos`; otherwise `features.GetFootprintCentre` of the visited cell's coordinates
and the feature, and the length of the gap to the centre it writes. A distance below the radius adds
the origin cell to the list (once) and publishes the features' DAMAGE (kind 34) with the origin cell's
*x* and *z* and the weapon's index in `Game.weapons`.

When the weapon has `interceptor` set, every projectile in the table that has its `dead` bit clear and
is not this weapon, and whose squared distance from its position to the centre (each difference taken in
signed 64 bits, squared, the high 32 bits summed) is below `areaOfEffect²`, is detonated with no unit,
and `network.SendProjectileDetonated` is called twice: with the attacker and that projectile, then with
the attacker and this weapon. The sweep has no limit on how deep it nests: a detonated interceptor
projectile whose own detonation reaches the area damage sweeps in turn, to any depth.
is not the projectile being detonated, and whose squared distance from its position to the centre (each
difference taken as a signed 32-bit value that wraps, squared in signed 64 bits, the high 32 bits of
each square summed as signed 32-bit numbers) is below `areaOfEffect²` (the 16-bit `areaOfEffect` squared as a signed 32-bit product, so a
value of 46341 or more squares to a negative number), is detonated with no unit, and
`network.SendProjectileDetonated` is called twice: with the attacker and that projectile, then with the
attacker and the projectile being detonated (the one whose sweep this is). A detonation that an
AREA_DAMAGE or DETONATE event starts has no projectile of the table to pass: the call's second
argument is the event's own record, which is no block address, so a module passes 0 there (the call
acts only in a network game, and no recording holds one). A detonated interceptor projectile whose own detonation reaches the area
damage sweeps in turn, inside the sweep that detonated it. The two
announcements of a projectile follow everything its detonation does, the sweeps nested in it included:
in `ai-antinuke-pair` at tick 5926, interceptor projectile 3 (attacker `Unit#252`) detonates projectile
4, whose own sweep detonates projectile 5, and the calls come in the order (`Unit#253`, 5), (`Unit#253`,
4), (`Unit#252`, 4), (`Unit#252`, 3).

A projectile whose weapon does not have `noExplode` set has its `dead` bit set by its detonation before
its area damage runs (step 2 of "A detonation"), so no later sweep detonates it again, and a chain of
such projectiles has at most as many levels as the table has projectiles (300). A projectile whose
weapon has `noExplode` set keeps its `dead` bit clear, unless step 3 of "A detonation" ends its
detonation (which sets the bit and runs no area damage), so each later sweep that reaches one whose
detonation ran the area damage detonates it again, and a chain through one can have more levels than
the table has entries. Two interceptor projectiles, of the same weapon or of two, that are both
`noExplode` and in each other's reach (directly, or through the sweeps of others) detonate each other without end:
the reference has no result for that cycle, and it is outside the contract. So is a chain of more
than 300 levels, which only a `noExplode` interceptor can make (no weapon of the game's data is both),
and no recording or vector holds one. The contract is every chain of up to 300 levels, which the
sweep detonates to its end, in the depth-first order above. The work a module keeps for the nested
sweeps outside its stack is empty when the outermost sweep starts (a sweep started while none runs)
and when it ends (`docs/module-abi.md`, section 12).

Finally, when the weapon's attacker is not null, HIT is published on it with the enemy and then the
friendly total.
Expand Down Expand Up @@ -458,10 +486,12 @@ Given a projectile *p* and its weapon, at its position:
7. Features: *cx* = the whole *x* ÷ 16 and *cz* = the whole *z* ÷ 16, both as signed 16-bit. The cell's
`feature` *f* is resolved: *f* below 0xfffb is `Game.features[f]` when *f* is below
`Game.featureCount`, else none; 0xfffe means the cell's origin (the cell at `offsetX` and `offsetY`
back, whose feature is used), and any other value of 0xfffb and above none. When a feature remains and
its `height` plus the cell's `low` is above the whole *y*: when the projectile's `cellX` and `cellZ`
already equal *cx* and *cz* the feature is dropped (the projectile already hit this cell), else they
become *cx* and *cz*. A surviving feature detonates *p* with no unit and the rule ends.
back, whose feature is used), and any other value of 0xfffb and above none. A feature whose `height`
plus the cell's `low` is not above the whole *y* is dropped, and a feature that is above it is dropped
too when the projectile's `cellX` and `cellZ` already equal *cx* and *cz* (the projectile already hit
this cell); otherwise `cellX` and `cellZ` become *cx* and *cz*, and that feature survives (`cellX` and
`cellZ` change only in this case). A surviving feature detonates *p* with no unit
and the rule ends.
8. When the cell's `low` is above the whole *y*: a weapon with `groundBounce` set gets
`direction.y = −(direction.y >> 2)` (arithmetic shift) and the rule ends without a detonation; any
other weapon detonates *p* with no unit and the rule ends.
Expand Down Expand Up @@ -497,6 +527,11 @@ weapon, and no scenario reaches one (#25).
| SET_STOCKPILES 267 | each slot that has a weapon gets the stockpile `stockpiles[w]` (its low 8 bits) | 1 |
| ADD_STOCKPILE 269 | the slot's stockpile rises by one (8 bits) | 1 |

**The `source` of DAMAGE and REMOTE_DAMAGE** is 0 (no attacker) or the block offset of a slot of
`Game.units` inside the pool (the first slot included), by the test the table above gives for `unit`.
The reference does no such test; any other value is outside the contract: the reference has no result for it, and no recording holds
one. The event is not refused for it.

**A unit's slots set up** (INIT_SLOTS): for each slot *w*: `reloadTimer` 0; `flags` keeps bits 5 to 7,
its bit 0 becomes 0, bits 2 and 3 become *w*, its bit 4 becomes 1 and its bit 1 becomes 1 when the
definition's `weapons[w]` is not weapon 0; `weapon` becomes `u.def.weapons[w]`; `stockpile` 0; `muzzleAimFromDeltaZ` becomes
Expand Down Expand Up @@ -759,7 +794,7 @@ For each slot *w* from 0 to 2, in order:
- With the slot's weapon `ballistic`: `scripts.GetAimFromPosition` gives the aim point; the differences
to *pos* give the heading (`heading(dx, dz) − u.heading`, 16 bits) and the pitch (the launch angle
rule of (dx, dy, dz, speed, pitch)); a pitch of 0x8000 means no aim. With `lineOfSight`: the heading
is `heading` of the differences, and the pitch is `heading` of minus *dy*'s whole part and of the
is `heading` of the differences minus `u.heading` (16 bits), and the pitch is `heading` of minus *dy*'s whole part and of the
truncated `distance(dx, dz)` cut to 16 bits; any other weapon has no aim.
- With no aim: the slot's bit 0 is cleared, OUT_OF_REACH is published and the handler returns 0. When
the aim tolerance test (below) fails: the slot's bit 0 is cleared and it returns 0.
Expand Down Expand Up @@ -1070,12 +1105,14 @@ Not recorded in the kit's recordings (`docs/replay-format.md`, "Not recorded"),
reason:

- An interceptor shot: the recorded interceptor is the anti-nuke of `orders-weapon-build`, which
searches and finds no projectile, so `FindTargetableProjectile`'s hit, a projectile's chase of its
`interceptedProjectile` and `SendProjectileDetonated` are unrecorded.
searches and finds no projectile, so `FindTargetableProjectile`'s hit and a projectile's chase of its
`interceptedProjectile` are unrecorded. `ai-antinuke-pair` (ticks 5920 to 5930) holds the
detonation of interceptor projectiles and its two `SendProjectileDetonated` calls each, in a chain of
two levels.
- `network.SendWeaponFire`, `network.SendUnitDamage`, `network.SendWeaponFireAt` and
`network.SendProjectileDetonated`: they act only in a network game. The kit's stated limits
(`docs/replay-format.md`, "Not recorded", weapons) also leave unjudged the two points' addresses,
interceptor chains deeper than four, a moved interceptor with a dead target, writes through a corrupt
interceptor chains deeper than two levels, a moved interceptor with a dead target, writes through a corrupt
pointer of the block and a damage table above 65536 entries.
- A hit of a paralyser weapon (a DAMAGE event of kind 2), PARALYZE's effect, damage of kind 11 and the
recorded kinds' other values: the recorded DAMAGE events are of kinds 3 and 10.
Expand Down Expand Up @@ -1122,5 +1159,5 @@ Questions the kit leaves open, which this spec does not answer:

## 8. Kit version

Kit `interfaces-v92` and `reference-traces-v60`, the versions the gate judges against. Interface
Kit `interfaces-v102` and `reference-traces-v64`, the versions the gate judges against. Interface
`weapons` 4.0.
40 changes: 39 additions & 1 deletion weapons/IMPLEMENTATION-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Verdict of the third-review build (req22): `equal`, all 9 recordings (module 669
## Fourth review: answers

- 1 fixed: `area_damage` is iterative. The levels of nested interceptor sweeps live in a static `FRAME` array (304
levels of 10 words; every level detonates a distinct live projectile, so the 300-entry table bounds the depth),
levels of 10 words; a chain of up to 300 levels is the contract; a noExplode interceptor could chain deeper, which is out of contract, and the extra level takes the plain hit branch),
each frame zeroed when popped, so the statics are unchanged after a tick. `detonate` is split into `detonate_pre`
(up to the area damage, which it returns) and the loop runs it, keeping the depth-first order of events and
`SendProjectileDetonated` calls. The old `DEPTH` static and the cap of 12 are gone; the stack no longer grows with depth.
Expand All @@ -112,3 +112,41 @@ Verdict of the third-review build (req22): `equal`, all 9 recordings (module 669
area-damage text is silent; this is the safe reading, unjudged).
- 4 kept: `g_extTdf` is in the manifest's data list. 5 fixed: stale notes corrected here. 6 fixed: `U_PITCH`.
- Verdict of the fourth-review build: req1 `equal`, all 9 recordings (module 69006 bytes).

## Spec change (interfaces-v102, reference-traces-v64): interceptor chains

- Spec diff read: the sweep excludes the projectile being detonated (not every projectile of the same weapon); each
coordinate difference wraps as signed 32-bit before squaring; `areaOfEffect²` is a signed 32-bit product; the two
`SendProjectileDetonated` calls are (attacker, swept projectile) then (attacker, projectile being detonated), after
everything the detonation does; an event-started detonation passes the event record. `FRAME` has 11 words now (the
projectile being detonated is the new one); the bit-14 / 0xfffb feature rules and the DAMAGE `source` text already
matched the code.
- New recording `ai-antinuke-pair` exposed a collision bug that no earlier recording reached: a feature in the cell was
detonating the projectile even when `feature.height + cell.low` was not above the whole y (the spec's step 7 only
lets a feature survive when that test holds). Fixed in `collision`.
- Verdicts in order: req1 differs (ai-antinuke-pair, tick 5920, extra cell lookup); req2/req3 were probes
(detonations disabled) to locate the cause; req4 `equal` on all 10 recordings.
- Unclear: step 7's wording ("when a feature remains and its height plus low is above y: ... A surviving feature
detonates") reads as if a feature below that height still survives; the recording says it does not.

## Fifth review and spec merge (interfaces-v102)

- Merged `spec-weapons-1338`. The step 7 feature rule (a feature not above the cell is dropped) was already what the code does; the spec now says it.
- DETONATE 258 went through `detonate_as` (first passing the event record; superseded below: it now passes 0).
- The area radius squared uses `wrapping_mul`, so it does not depend on the overflow-checks profile.
- Chain depth comment/notes corrected: 300 levels is the contract, not a guarantee for noExplode chains.

## Spec change: event detonation argument and aim heading

- Merged the new `spec-weapons-1338` commit. An AREA_DAMAGE or DETONATE event now passes 0 (not the event record) as the
second `SendProjectileDetonated` argument; a `lineOfSight` aim heading is `heading` minus `u.heading` (code already did).
This supersedes the earlier "passes the event record" note and the review's risks 2 and 3.
- Review risk 1 fixed: the interceptor chase test in `collision` wraps each difference to 32 bits and squares
`areaOfEffect` as a signed 32-bit product, like the sweep.
- Verdict of this build (req1): `equal` on all 10 recordings (module 69052 bytes).
- Left as is: the table anchor and `g_extTdf` (needed by the manifest), and the raw frame array (nits 5, 6).

## Sixth review

- Nit fixed: stale notes corrected (`FRAME` has 11 words; event-started detonations pass 0). No code change; the
last submission (req1, `equal` on all 10 recordings) is of the current source.
60 changes: 35 additions & 25 deletions weapons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,13 @@ fn weapon_index(wd: u32) -> i32 {
}

fn detonate(p: u32, hit: u32) {
detonate_as(p, hit, p);
}

/// `detonate` with `proj` as the record the area damage announces (an event's record for events).
fn detonate_as(p: u32, hit: u32, proj: u32) {
if let Some((wd, pos, owner, att)) = detonate_pre(p, hit) {
area_damage(wd, &pos, owner, att);
area_damage(wd, &pos, owner, att, proj);
}
}

Expand Down Expand Up @@ -1241,20 +1246,20 @@ fn clampi(v: i32, lo: i32, hi: i32) -> i32 {
}

/// One level of an interceptor sweep: the weapon, its centre, the attacker, the damage totals, the
/// next projectile index, the count and the projectile whose detonation is running.
const FRAME_WORDS: usize = 10;
/// Every level detonates a distinct live projectile of the table, so 300 levels are the most.
/// next projectile index, the count, the projectile being detonated and the one whose announcement is due.
const FRAME_WORDS: usize = 11;
/// Room for every chain of up to 300 levels (the contract); a `noExplode` chain can be deeper, and is cut here.
const FRAMES: usize = 304;
static mut FRAME: [u32; FRAMES * FRAME_WORDS] = [0; FRAMES * FRAME_WORDS];

/// The area damage of a detonation, and the sweeps of the interceptor projectiles it detonates in
/// turn, depth first: the levels live in `FRAME`, which is all zero again when this returns.
fn area_damage(wd: u32, c: &V3, owner: i32, att: u32) {
fn area_damage(wd: u32, c: &V3, owner: i32, att: u32, proj: u32) {
let frame = unsafe { &mut *core::ptr::addr_of_mut!(FRAME) };
let mut sp = 0usize;
let mut cur = Some((wd, *c, owner, att));
let mut cur = Some((wd, *c, owner, att, proj));
loop {
if let Some((wd, c, owner, att)) = cur.take() {
if let Some((wd, c, owner, att, pj)) = cur.take() {
let (enemy, friendly) = area_sweep(wd, &c, owner, att);
if wflags(wd) & WF_INTERCEPTOR != 0 && sp < FRAMES {
let f = &mut frame[sp * FRAME_WORDS..(sp + 1) * FRAME_WORDS];
Expand All @@ -1268,6 +1273,7 @@ fn area_damage(wd: u32, c: &V3, owner: i32, att: u32) {
f[7] = 0;
f[8] = r32(game(G_PROJ_COUNT)) as u32;
f[9] = 0;
f[10] = pj;
sp += 1;
} else if att != 0 {
hit_effect(att, enemy, friendly);
Expand All @@ -1281,24 +1287,24 @@ fn area_damage(wd: u32, c: &V3, owner: i32, att: u32) {
if f[9] != 0 {
unsafe {
net_projectile_detonated(att, f[9]);
net_projectile_detonated(att, wd);
net_projectile_detonated(att, f[10]);
}
f[9] = 0;
}
let c = [f[1] as i32, f[2] as i32, f[3] as i32];
let r2 = (r16u(wd + W_AOE) * r16u(wd + W_AOE)) as i32;
let r2 = r16u(wd + W_AOE).wrapping_mul(r16u(wd + W_AOE));
while f[7] < f[8] {
let q = proj_at(f[7] as i32);
f[7] += 1;
if is_dead(q) || r32(q + P_TYPE) == boff(wd) {
if is_dead(q) || q == f[10] {
continue;
}
let s = sq_hi64(r32(q + P_POS) as i64 - c[0] as i64)
.wrapping_add(sq_hi64(r32(q + P_POS + 4) as i64 - c[1] as i64))
.wrapping_add(sq_hi64(r32(q + P_POS + 8) as i64 - c[2] as i64));
let s = sq_hi64(r32(q + P_POS).wrapping_sub(c[0]) as i64)
.wrapping_add(sq_hi64(r32(q + P_POS + 4).wrapping_sub(c[1]) as i64))
.wrapping_add(sq_hi64(r32(q + P_POS + 8).wrapping_sub(c[2]) as i64));
if s < r2 {
f[9] = q;
cur = detonate_pre(q, 0);
cur = detonate_pre(q, 0).map(|(w, p, o, a)| (w, p, o, a, q));
break;
}
}
Expand Down Expand Up @@ -1483,11 +1489,11 @@ fn collision(p: u32) {
let inter = r32(p + P_INTERCEPT);
if inter != 0 {
let q = lin(inter) + P_POS;
let s = sq_hi64(r32(p + P_POS) as i64 - r32(q) as i64)
.wrapping_add(sq_hi64(r32(p + P_POS + 4) as i64 - r32(q + 4) as i64))
.wrapping_add(sq_hi64(r32(p + P_POS + 8) as i64 - r32(q + 8) as i64));
let s = sq_hi64(r32(p + P_POS).wrapping_sub(r32(q)) as i64)
.wrapping_add(sq_hi64(r32(p + P_POS + 4).wrapping_sub(r32(q + 4)) as i64))
.wrapping_add(sq_hi64(r32(p + P_POS + 8).wrapping_sub(r32(q + 8)) as i64));
let aoe = r16u(wd + W_AOE);
if s < aoe * aoe {
if s < (aoe as i32).wrapping_mul(aoe as i32) {
detonate(p, 0);
}
}
Expand Down Expand Up @@ -1522,12 +1528,16 @@ fn collision(p: u32) {
if f < 0xfffb && f < r32(game(G_FEATURE_COUNT)) {
feature = lin(r32(game(G_FEATURES))) + f as u32 * 256;
}
if feature != 0 && r8(feature + 250) + low > ywhole {
if r16(p + P_CELL_X) == cx && r16(p + P_CELL_Z) == cz {
feature = 0;
if feature != 0 {
if r8(feature + 250) + low > ywhole {
if r16(p + P_CELL_X) == cx && r16(p + P_CELL_Z) == cz {
feature = 0;
} else {
w16(p + P_CELL_X, cx);
w16(p + P_CELL_Z, cz);
}
} else {
w16(p + P_CELL_X, cx);
w16(p + P_CELL_Z, cz);
feature = 0;
}
}
if feature != 0 {
Expand Down Expand Up @@ -2800,7 +2810,7 @@ pub extern "C" fn Apply(ev: u32, size: u32) -> i32 {
w32(p + P_TYPE, boff(wd));
wr3(p + P_POS, &rd3(u + U_POS));
w8(p + P_OWNER, r8(u + U_PLAYER_INDEX));
detonate(p, 0);
detonate_as(p, 0, 0);
}
1
}
Expand Down Expand Up @@ -2869,7 +2879,7 @@ pub extern "C" fn Apply(ev: u32, size: u32) -> i32 {
return 0;
}
let wd = weapons_base() + widx as u32 * W_SIZE;
area_damage(wd, &rd3(ev + 12), 0, 0);
area_damage(wd, &rd3(ev + 12), 0, 0, 0);
1
}
266 => {
Expand Down
Loading