Skip to content

Optimize game engine loop and boost calculation - #188

Merged
candour merged 1 commit into
mainfrom
refactor/engine-optimization-6111875595712730203
Jun 15, 2026
Merged

Optimize game engine loop and boost calculation#188
candour merged 1 commit into
mainfrom
refactor/engine-optimization-6111875595712730203

Conversation

@candour

@candour candour commented Jun 15, 2026

Copy link
Copy Markdown
Owner

Refactored the core game engine in MainViewModel.kt and Registry.kt for improved efficiency.

  • Removed enemiesByMode sorting logic from the main update loop, saving three O(E log E) operations per tick.
  • Introduced stallBoosts cache to store Bak Kut Teh adjacency bonuses, reducing calculations from O(Stalls) per tick to O(1) by updating the cache only when the grid layout or wave status changes.
  • Simplified StallBehavior.selectTarget to handle all targeting modes (FIRST, CLOSEST, STRONGEST, WEAKEST) directly using collection operators on range-filtered enemies.

PR created automatically by Jules for task 6111875595712730203 started by @candour

Summary by CodeRabbit

  • Refactor
    • Optimized game loop performance by caching stall stat boost calculations, reducing redundant per-tick computations.
    • Streamlined stall targeting logic to directly select best targets from visible candidates, eliminating unnecessary sorting overhead.

Remove redundant sorted enemy lists from every game tick.
Implement stall boost caching to eliminate per-tick recalculations.
Update targeting logic to calculate best targets directly from visible candidates.

Co-authored-by: candour <4670475+candour@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Jun 15, 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: 5dc43a24-141b-4fd2-81d4-d5c98b1796af

📥 Commits

Reviewing files that changed from the base of the PR and between 574937e and 70d0b17.

📒 Files selected for processing (3)
  • app/src/main/java/com/messark/hawker/MainViewModel.kt
  • app/src/main/java/com/messark/hawker/registry/Registry.kt
  • fixes.md

📝 Walkthrough

Walkthrough

MainViewModel introduces a stallBoosts cache mapping stall coordinates to BoostResult values, recomputed via updateBoostCache on every state mutation that can affect BKT providers. The game loop drops per-tick boost precomputation and mode-sorted enemy lists; handleStallFiring now reads from the cache. StallBehavior.selectTarget drops its enemiesByMode parameter, with FIRST mode selection rewritten to use maxByOrNull { currentPathIndex } on visible enemies.

Changes

Engine Loop Boost Caching and selectTarget Simplification

Layer / File(s) Summary
selectTarget signature and FIRST-mode logic
app/src/main/java/com/messark/hawker/registry/Registry.kt
Removes enemiesByMode: Map<TargetMode, List<Enemy>> from StallBehavior.selectTarget and its DefaultStallBehavior override. FIRST mode selection changes from filtering a pre-sorted list by visible IDs to visibleEnemies.maxByOrNull { currentPathIndex }.
stallBoosts cache definition and handleStallFiring refactor
app/src/main/java/com/messark/hawker/MainViewModel.kt
Adds stallBoosts field and updateBoostCache(state) helper computing BoostResult per active BKT provider tile. Game loop drops per-tick preCalculatedBoosts and mode-sorted enemy lists. handleStallFiring signature removes those parameters and reads BoostResult from stallBoosts[coord] while calling selectTarget without enemiesByMode.
Cache refresh on all state mutations
app/src/main/java/com/messark/hawker/MainViewModel.kt, fixes.md
Every state-mutating operation—init, resume, reset, wave start, wave completion, build stall, sell stall, undo sell, pillar removal, stall upgrade—now constructs a newState local and calls updateBoostCache(newState) before returning from the update lambda. Changelog entry REF-016 added.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • candour/towerpower#164: Directly overlaps in the handleStallFiring and StallBehavior.selectTarget pipeline — that PR also reshapes selectTarget inputs and the boost-handling code path in the same functions.
  • candour/towerpower#176: Both modify DefaultStallBehavior.selectTarget in Registry.kt specifically for visibleEnemies-based target selection and spatial-index prefiltering.
  • candour/towerpower#156: Directly affects the same StallBehavior.selectTarget(...) contract in Registry.kt and the handleStallFiring call site in MainViewModel, introducing the enemiesByMode parameter that this PR removes.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Optimize game engine loop and boost calculation' directly and accurately summarizes the main changes: performance optimizations to the game engine loop and boost caching.
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.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/engine-optimization-6111875595712730203

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.

@github-actions

Copy link
Copy Markdown

Build Successful! 🚀

Download APK

Note: This link will be removed when the PR is closed.

@candour
candour merged commit a8756c0 into main Jun 15, 2026
3 checks passed
@candour
candour deleted the refactor/engine-optimization-6111875595712730203 branch June 15, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant