Update NationStructureBehavior.ts#3875
Conversation
Added a leaky bucket algorithm using stress meter to hopefully make the defence posts more consistent.
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughDefense-post threat detection was refactored to use a sustained-stress accumulation model. A new ChangesAttack Stress Meter Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/core/execution/nation/NationStructureBehavior.ts (1)
144-255:⚠️ Potential issue | 🟠 Major | ⚡ Quick winAdd tests for the new stress-meter defense behavior
This PR changes core simulation behavior, but no tests are included for the new stress/cooldown path. Please add focused tests (heat-up, cooldown, threshold crossing, and low-ratio sustained attack cases) before merge.
As per coding guidelines, "All changes to
src/core/must include tests".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/core/execution/nation/NationStructureBehavior.ts` around lines 144 - 255, Add unit tests covering the new attack stress-meter logic: write tests that exercise isThreatenedByAttack via tryBuildDefensePost and defensePostNeeded by simulating incoming landAttacks and ourTroops to verify (1) heat-up: repeated high-ratio ticks increase attackStressMeter and trigger defensePostNeeded/tryBuildDefensePost, (2) cooldown: without attacks attackStressMeter decreases over elapsed ticks and eventually clears the threat, (3) threshold crossing: a single spike above UNDER_ATTACK_THREAT_RATIO immediately triggers threat behavior, and (4) sustained low-ratio attacks: multiple ticks with ratio < threshold but accumulating stress eventually trigger the behavior. In each test assert attackStressMeter changes and the public outcomes (defensePostNeeded() return value or that tryBuildDefensePost enqueues a ConstructionExecution / returns true) using mocks/fakes for player.incomingAttacks(), game.ticks(), and random where needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/core/execution/nation/NationStructureBehavior.ts`:
- Around line 144-155: The threat math in isThreatenedByAttack uses
floating-point ratios (parameters and literals like 0.05) inside the
deterministic core; change to integer fixed-point (e.g., basis points) by:
accept or convert the incoming ratio to an integer basis-points value, replace
literals (0.05, any 0.35/0.4 uses) with integer constants (e.g.,
UNDER_ATTACK_THREAT_BPS), and perform all comparisons and attackStressMeter
arithmetic using integers (adjust attackStressMeter scaling and the elapsed*3
multiplier to integer equivalents). Update related members lastAttackCheckTick,
attackStressMeter, and the constant UNDER_ATTACK_THREAT_RATIO to integer-based
names and semantics so all math in isThreatenedByAttack and the other reported
locations uses only integer operations. Ensure callers that pass a float convert
to the same fixed-point basis.
- Around line 149-151: The attackStressMeter can spike because the heating logic
in isThreatenedByAttack(elapsed) is skipped when callers return early on no land
attacks; fix by invoking isThreatenedByAttack(0) (so cooldown/decay runs but
heating is zero) before returning in the two call sites that short-circuit on no
land attacks (the branches that check landAttacks.length === 0 around where
isThreatenedByAttack is normally used, at the two early-return locations
referenced in the diff), ensuring attackStressMeter is cooled each tick even
when there are no attacks.
---
Outside diff comments:
In `@src/core/execution/nation/NationStructureBehavior.ts`:
- Around line 144-255: Add unit tests covering the new attack stress-meter
logic: write tests that exercise isThreatenedByAttack via tryBuildDefensePost
and defensePostNeeded by simulating incoming landAttacks and ourTroops to verify
(1) heat-up: repeated high-ratio ticks increase attackStressMeter and trigger
defensePostNeeded/tryBuildDefensePost, (2) cooldown: without attacks
attackStressMeter decreases over elapsed ticks and eventually clears the threat,
(3) threshold crossing: a single spike above UNDER_ATTACK_THREAT_RATIO
immediately triggers threat behavior, and (4) sustained low-ratio attacks:
multiple ticks with ratio < threshold but accumulating stress eventually trigger
the behavior. In each test assert attackStressMeter changes and the public
outcomes (defensePostNeeded() return value or that tryBuildDefensePost enqueues
a ConstructionExecution / returns true) using mocks/fakes for
player.incomingAttacks(), game.ticks(), and random where needed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3ef0798c-e844-4dcb-b264-a18a6c3516b8
📒 Files selected for processing (1)
src/core/execution/nation/NationStructureBehavior.ts
Fix code rabbit issues, transfered over from decimals to whole numbers.
|
@FloPinguin I have completed the change requested by coderabbit |
Added a simple, stable compact change using stress meter to hopefully make the defence posts more consistent.
If this PR fixes an issue, link it below. If not, delete these two lines.
Resolves #(issue number)
Description:
Describe the PR. It's a pull request to fix an custom issue I'm not sure suggested yet. I suggested it in dev discord and was told to make pr this is mainly for others to test. Fixes hard coded 0.35 threshold for defence post that was originally a simple solution.
Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
DISCORD_USERNAME
Noarze