Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/core/execution/nation/NationStructureBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,30 @@
private _hasHighStartingGold: boolean | null = null;
private _postSaveUpStartTick: number | null = null;

private attackStressMeter = 0;
private lastAttackCheckTick = 0;
constructor(
private random: PseudoRandom,
private game: Game,
private player: Player,
) {}
private isThreatenedByAttack(ratio: number): boolean {
const currentTick = this.game.ticks();

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.defensePostNeeded > returns true when ratio is above threshold

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.defensePostNeeded src/core/execution/nation/NationStructureBehavior.ts:259:17 ❯ call tests/NationStructureBehavior.test.ts:555:30 ❯ tests/NationStructureBehavior.test.ts:579:12

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.defensePostNeeded > returns true when ratio is exactly at threshold (0.35)

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.defensePostNeeded src/core/execution/nation/NationStructureBehavior.ts:259:17 ❯ call tests/NationStructureBehavior.test.ts:555:30 ❯ tests/NationStructureBehavior.test.ts:575:12

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.defensePostNeeded > returns false when ratio is below threshold (0.35)

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.defensePostNeeded src/core/execution/nation/NationStructureBehavior.ts:259:17 ❯ call tests/NationStructureBehavior.test.ts:555:30 ❯ tests/NationStructureBehavior.test.ts:571:12

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > returns false when no sampled tile passes canBuild

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:517:30

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > returns false when player.gold() is below cost

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:497:30

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > Hard: dispatches a ConstructionExecution for DefensePost on successful build

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:477:30

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > Hard: returns false once countDefensePostsNearFront exceeds the allowed cap

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:454:30

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > Hard: returns false once countDefensePostsNearFront reaches the allowed cap

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:443:30

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > Hard: skips chance gate (no chance(2) consumed)

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ tests/NationStructureBehavior.test.ts:429:23

Check failure on line 145 in src/core/execution/nation/NationStructureBehavior.ts

View workflow job for this annotation

GitHub Actions / 🔬 Test

tests/NationStructureBehavior.test.ts > NationStructureBehavior.tryBuildDefensePost > returns false when land-attack ratio is below 0.35

TypeError: this.game.ticks is not a function ❯ NationStructureBehavior.isThreatenedByAttack src/core/execution/nation/NationStructureBehavior.ts:145:35 ❯ NationStructureBehavior.tryBuildDefensePost src/core/execution/nation/NationStructureBehavior.ts:218:15 ❯ callTryBuild tests/NationStructureBehavior.test.ts:366:30 ❯ tests/NationStructureBehavior.test.ts:386:12
// Cap elapsed at 100 ticks (10s) to prevent stress spikes after long peace
const elapsed = this.lastAttackCheckTick === 0 ? 0 : Math.min(100, currentTick - this.lastAttackCheckTick);
this.lastAttackCheckTick = currentTick;

// Convert to Basis Points (whole numbers) for multiplayer safety (desync prevention)
const ratioBps = Math.floor(ratio * 10000);
const thresholdBps = Math.floor(UNDER_ATTACK_THREAT_RATIO * 10000);

// Cooldown by 1x. If attacked (>5%), heat up by 3x
this.attackStressMeter = Math.max(0, this.attackStressMeter - elapsed);
if (ratioBps > 500) this.attackStressMeter += elapsed * 3;

// Trigger if over 35% threshold OR sustained stress over 200 (approx 10s)
return ratioBps >= thresholdBps || (this.attackStressMeter > 200 && ratioBps > 0);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

handleStructures(): boolean {
// Defense posts are handled outside the normal pacing/counter system:
Expand Down Expand Up @@ -195,15 +214,15 @@

const incomingTroops = landAttacks.reduce((sum, a) => sum + a.troops(), 0);
const ratio = incomingTroops / ourTroops;
if (ratio < UNDER_ATTACK_THREAT_RATIO) return false;

if (!this.isThreatenedByAttack(ratio)) return false;

let allowed: number;
if (difficulty === Difficulty.Medium) {
allowed = 1;
} else {
allowed = Math.ceil(ratio / DEFENSE_POST_RATIO_PER_POST);
allowed = Math.max(1, Math.ceil(ratio / DEFENSE_POST_RATIO_PER_POST));
}

const frontTiles = this.getAttackFrontTiles(landAttacks);
if (this.countDefensePostsNearFront(frontTiles, allowed) >= allowed)
return false;
Expand Down Expand Up @@ -236,7 +255,8 @@
const ourTroops = this.player.troops();
if (ourTroops <= 0) return false;
const incomingTroops = landAttacks.reduce((sum, a) => sum + a.troops(), 0);
return incomingTroops / ourTroops >= UNDER_ATTACK_THREAT_RATIO;
const ratio = incomingTroops / ourTroops;
return this.isThreatenedByAttack(ratio);
}

/**
Expand Down
Loading