Skip to content

fix(game): recalculate trade ship spawn rate on each level roll in PortExecution - #5015

Merged
evanpelle merged 1 commit into
openfrontio:mainfrom
berkelmali:fix/port-tradeship-spawn-pity-loop
Aug 17, 2026
Merged

evanpelle merged 1 commit into
openfrontio:mainfrom
berkelmali:fix/port-tradeship-spawn-pity-loop

Conversation

@berkelmali

Copy link
Copy Markdown
Contributor

Before opening a PR: discuss new features on Discord first, and file bugs or small improvements as issues. You must be assigned to an approved issue — unsolicited PRs will be auto-closed.

Add approved & assigned issue number here:

Resolves #5013

Description:

In PortExecution.ts, shouldSpawnTradeShip() rolls for trade ship spawns for each level of the port (for (let i = 0; i < this.port!.level(); i++)). When a roll fails, this.tradeShipSpawnRejections is incremented to track consecutive rejections.

Previously, spawnRate was computed once before the loop:

const spawnRate = this.mg
  .config()
  .tradeShipSpawnRate(this.tradeShipSpawnRejections, numTradeShips);
for (let i = 0; i < this.port!.level(); i++) {
  if (this.random.chance(spawnRate)) {
    this.tradeShipSpawnRejections = 0;
    return true;
  }
  this.tradeShipSpawnRejections++;
}

Because Config.tradeShipSpawnRate() implements a pity modifier (1 / (tradeShipSpawnRejections + 1)) that increases spawn chances as rejections accumulate, computing spawnRate once before the loop prevented higher-level ports (levels 2–5) from benefiting from the updated pity rate across consecutive failed rolls within the same tick.

This PR moves the tradeShipSpawnRate calculation inside the loop so that each roll evaluates against the up-to-date rejection count.

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

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

berkelmali

@coderabbitai

coderabbitai Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

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 Plus

Run ID: a165dc79-63c8-4d17-948f-4236016efeba

📥 Commits

Reviewing files that changed from the base of the PR and between b6c194e and 4ae5822.

📒 Files selected for processing (2)
  • src/core/execution/PortExecution.ts
  • tests/PortExecution.test.ts

Included review availability: Your plan includes up to 8 reviews per rolling hour; 5 remain after this review.


Walkthrough

shouldSpawnTradeShip() now recalculates the spawn rate for each port-level roll. A regression test verifies changing rejection counts, spawn outcomes, and reset behavior across invocations.

Changes

Trade ship spawning

Layer / File(s) Summary
Per-roll spawn-rate evaluation
src/core/execution/PortExecution.ts, tests/PortExecution.test.ts
The spawn logic recalculates the rate inside the level loop. The regression test verifies false, true, and false outcomes, records rejection-count arguments, and confirms reset behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 4ae58

This PR recalculates the pity-based trade-ship spawn rate for each port-level roll and adds focused tests; no actionable merge-blocking risk remains beyond normal checks.

Possibly related PRs

Suggested labels: auto-closed-needs-issue

Suggested reviewers: evanpelle, celant, jb940

Poem

Each port-level roll now checks anew,
With rejection counts kept in view.
Tests track the rate from start to end,
Then reset cleanly when calls begin.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: recalculating the trade ship spawn rate for each level roll.
Description check ✅ Passed The description explains the bug, the implementation change, and the added regression test, all related to the changeset.
Linked Issues check ✅ Passed The changes satisfy issue #5013 by evaluating tradeShipSpawnRate inside the level-roll loop and testing updated rejection behavior.
Out of Scope Changes check ✅ Passed The pull request contains only the requested PortExecution fix and its regression test, with no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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.

@github-actions github-actions Bot added the small-fix Small fix (≤ 50 lines) — auto-applied by PR gate label Aug 16, 2026
@github-project-automation github-project-automation Bot moved this from Triage to Final Review in OpenFront Release Management Aug 17, 2026
@evanpelle evanpelle added this to the v34 milestone Aug 17, 2026
@evanpelle
evanpelle merged commit 5ee20b8 into openfrontio:main Aug 17, 2026
12 of 13 checks passed
@github-project-automation github-project-automation Bot moved this from Final Review to Complete in OpenFront Release Management Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

small-fix Small fix (≤ 50 lines) — auto-applied by PR gate

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

PortExecution computes trade ship spawn rate once instead of updating per level roll in loop

2 participants