diff --git a/src/client/controllers/BuildPreviewController.ts b/src/client/controllers/BuildPreviewController.ts index 3dfaa22ca..7a523a6cd 100644 --- a/src/client/controllers/BuildPreviewController.ts +++ b/src/client/controllers/BuildPreviewController.ts @@ -43,15 +43,21 @@ export function shouldPreserveGhostAfterBuild(unitType: UnitType): boolean { /** * Whether a SAM belongs in the nuke trajectory preview's threat set. - * Allied SAMs are excluded unless the strike would betray that ally — - * the alliance breaks at launch, so their SAMs will engage the nuke. + * Mirrors SAMLauncherExecution: a SAM ignores a nuke whose owner it's + * friendly with (same team OR allied). + * Teammates are excluded unconditionally — a strike can break an alliance + * but never a team relationship, so a teammate's SAM never engages. + * Allied SAMs are excluded unless the strike would betray that ally — the + * alliance breaks at launch, so their SAMs will engage the nuke. * (Own SAMs never threaten; the caller filters those out first.) */ export function samThreatensNukePreview( samOwnerSmallId: number, + teammateSmallIds: ReadonlySet, allySmallIds: ReadonlySet, betrayedSmallIds: ReadonlySet, ): boolean { + if (teammateSmallIds.has(samOwnerSmallId)) return false; return ( !allySmallIds.has(samOwnerSmallId) || betrayedSmallIds.has(samOwnerSmallId) ); @@ -331,10 +337,15 @@ export class BuildPreviewController implements Controller { const srcX = this.game.x(bestSilo.tile()); const srcY = this.game.y(bestSilo.tile()); - // Non-allied SAMs threaten the trajectory; own + allied SAMs don't — - // except allies this strike would betray: the alliance breaks at launch - // (NukeExecution.maybeBreakAlliances), so their SAMs will intercept. + // Non-friendly SAMs threaten the trajectory; own + teammate + allied SAMs + // don't — except allies this strike would betray: the alliance breaks at + // launch (NukeExecution.maybeBreakAlliances), so their SAMs will intercept. + // Teammates have no such exception (a strike never breaks a team). // listNukeBreakAlliance is the same function the sim uses there. + const teammateIds = new Set(); + for (const p of this.game.players()) { + if (myPlayer.isOnSameTeam(p)) teammateIds.add(p.smallID()); + } const allyIds = new Set(); for (const a of myPlayer.allies()) allyIds.add(a.smallID()); const betrayedIds: ReadonlySet = @@ -351,7 +362,14 @@ export class BuildPreviewController implements Controller { if (!s.isActive()) continue; const owner = s.owner(); if (owner === myPlayer) continue; - if (!samThreatensNukePreview(owner.smallID(), allyIds, betrayedIds)) { + if ( + !samThreatensNukePreview( + owner.smallID(), + teammateIds, + allyIds, + betrayedIds, + ) + ) { continue; } const r = this.game.config().samRange(s.level()); diff --git a/tests/client/controllers/BuildPreviewController.test.ts b/tests/client/controllers/BuildPreviewController.test.ts index b0d475b63..267997cb4 100644 --- a/tests/client/controllers/BuildPreviewController.test.ts +++ b/tests/client/controllers/BuildPreviewController.test.ts @@ -35,21 +35,40 @@ describe("BuildPreviewController ghost preservation (locked nuke / Enter confirm }); describe("samThreatensNukePreview (nuke trajectory threat set, #4226)", () => { + const teammates = new Set([7, 8]); const allies = new Set([2, 3]); - test("non-allied SAM threatens the trajectory", () => { - expect(samThreatensNukePreview(5, allies, new Set())).toBe(true); + test("non-friendly SAM threatens the trajectory", () => { + expect(samThreatensNukePreview(5, teammates, allies, new Set())).toBe(true); }); test("allied SAM does not threaten when the strike breaks no alliance", () => { - expect(samThreatensNukePreview(2, allies, new Set())).toBe(false); + expect(samThreatensNukePreview(2, teammates, allies, new Set())).toBe( + false, + ); }); test("would-be-betrayed ally's SAM threatens (alliance breaks at launch)", () => { - expect(samThreatensNukePreview(2, allies, new Set([2]))).toBe(true); + expect(samThreatensNukePreview(2, teammates, allies, new Set([2]))).toBe( + true, + ); }); test("other allies' SAMs still excluded when a different ally is betrayed", () => { - expect(samThreatensNukePreview(3, allies, new Set([2]))).toBe(false); + expect(samThreatensNukePreview(3, teammates, allies, new Set([2]))).toBe( + false, + ); + }); + + test("teammate SAM does not threaten the trajectory", () => { + expect(samThreatensNukePreview(7, teammates, new Set(), new Set())).toBe( + false, + ); + }); + + test("teammate SAM stays excluded even if listed as betrayed (a strike never breaks a team)", () => { + expect( + samThreatensNukePreview(7, teammates, new Set([7]), new Set([7])), + ).toBe(false); }); });