Skip to content

Update NationStructureBehavior.ts#3875

Open
MrDev-lang wants to merge 3 commits intoopenfrontio:mainfrom
MrDev-lang:main
Open

Update NationStructureBehavior.ts#3875
MrDev-lang wants to merge 3 commits intoopenfrontio:mainfrom
MrDev-lang:main

Conversation

@MrDev-lang
Copy link
Copy Markdown

@MrDev-lang MrDev-lang commented May 6, 2026

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:

  • I have added screenshots for all UI updates
  • I process any text displayed to the user through translateText() and I've added it to the en.json file
  • I have added relevant tests to the test directory
  • I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced

Please put your Discord username so you can be contacted if a bug or regression is found:

DISCORD_USERNAME
Noarze

Added a leaky bucket algorithm using stress meter to hopefully make the defence posts more consistent.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 6, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2c29f362-494e-47f4-a23e-9a96b1796912

📥 Commits

Reviewing files that changed from the base of the PR and between 365d87a and f5f8344.

📒 Files selected for processing (1)
  • src/core/execution/nation/NationStructureBehavior.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/core/execution/nation/NationStructureBehavior.ts

Walkthrough

Defense-post threat detection was refactored to use a sustained-stress accumulation model. A new attackStressMeter state and isThreatenedByAttack() helper with cooldown/heat-up logic replace direct threat-ratio comparisons in tryBuildDefensePost() and defensePostNeeded().

Changes

Attack Stress Meter Integration

Layer / File(s) Summary
State Tracking
src/core/execution/nation/NationStructureBehavior.ts (lines 134–143)
New fields attackStressMeter and lastAttackCheckTick track cumulative attack stress over elapsed ticks.
Core Threat Logic
src/core/execution/nation/NationStructureBehavior.ts (lines 144–156)
isThreatenedByAttack(ratio) implements cooldown/heat-up model—stress increases when ratio exceeds baseline, decreases otherwise, with threat decision based on accumulated stress and tick elapsed.
Defense Post Evaluation
src/core/execution/nation/NationStructureBehavior.ts (lines 212–220, 253–254)
tryBuildDefensePost() and defensePostNeeded() now compute attack ratio and delegate threshold decision to isThreatenedByAttack() instead of direct ratio comparison.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

Defense posts now feel the battle's slow burn—
Not instant panic, but stress that returns.
Cooldown and heat-up, a meter that grows,
Threats build like thunder where the fight goes. ⚔️✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Update NationStructureBehavior.ts' is vague and generic, using only a filename without describing the actual change or improvement made. Use a more descriptive title that explains the primary change, such as 'Add stress meter to defense post threat evaluation' or 'Refactor defense post threat detection with decay model'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description relates to the changeset by mentioning stress meter implementation and the hard-coded threshold fix, though it contains informal language and incomplete checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 win

Add 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

📥 Commits

Reviewing files that changed from the base of the PR and between 005e1b6 and 365d87a.

📒 Files selected for processing (1)
  • src/core/execution/nation/NationStructureBehavior.ts

Comment thread src/core/execution/nation/NationStructureBehavior.ts
Comment thread src/core/execution/nation/NationStructureBehavior.ts Outdated
@github-project-automation github-project-automation Bot moved this from Triage to Development in OpenFront Release Management May 6, 2026
@evanpelle evanpelle requested a review from FloPinguin May 7, 2026 02:28
Fix code rabbit issues, transfered over from decimals to whole numbers.
@MrDev-lang
Copy link
Copy Markdown
Author

@FloPinguin I have completed the change requested by coderabbit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Development

Development

Successfully merging this pull request may close these issues.

2 participants