Refactor Combat Logic for Efficiency - #133
Conversation
- Refactored `handleStallFiring` to pre-filter active stalls and optimize target selection. - Refactored `handleProjectiles` using an enemy lookup map to reduce lookup complexity from O(N) to O(1). - Optimized AoE collision detection in `handleProjectiles`. - Consolidated state updates and haptic triggers to reduce redundant iterations. - Fixed enemy death condition to correctly handle fractional health rounding. - Updated `fixes.md` with REF-003. Co-authored-by: candour <4670475+candour@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughMainViewModel's combat tick pipeline was refactored for performance: ChangesCombat Tick Optimization
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Build Successful! 🚀Note: This link will be removed when the PR is closed. |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
app/src/main/java/com/messark/hawker/MainViewModel.kt (2)
625-639: 💤 Low valueDuplicate KDoc above
handleStallFiring.The new KDoc (lines 632-639) is stacked on top of the existing one (lines 625-631) for the same function. Kotlin will only treat the closest block as the function’s doc, but both will render in source/IDE tooltips depending on tooling. Drop the older block (or merge them) to avoid drift.
♻️ Suggested cleanup
- /** - * Checks all stalls to see if they are ready to fire and creates projectiles/puddles. - * - * `@param` state Current game state. - * `@param` currentTimeMs Current game time. - * `@return` Updated state with new projectiles/puddles. - */ /** * Handles the firing logic for all stalls on the board. * Efficiently filters for active stalls and determines targets based on their targeting mode. * * `@param` state Current game state. * `@param` currentTimeMs Current game time in milliseconds. * `@return` Updated game state with new projectiles, puddles, and stall states. */ private fun handleStallFiring(state: GameState, currentTimeMs: Long): GameState {
857-866: ⚡ Quick winReuse
enemyLookupfor buff source resolution.The post-damage buff cleanup still does a linear
state.enemies.findper buff per surviving enemy (O(buffs · enemies) per tick), which goes against the O(1) lookup goal of this PR. The already-builtenemyLookupmap (line 740) gives the same result with a single map probe.♻️ Proposed refactor
}.map { e -> // Clean up buffs: source must be alive, still targeting this enemy, and not grabbed val validBuffs = e.buffs.filter { buff -> - val source = state.enemies.find { it.id == buff.sourceId } + val source = enemyLookup[buff.sourceId] source != null && source.buffingTargetId == e.id && !source.isGrabbed } if (validBuffs.size != e.buffs.size) { e.copy(buffs = validBuffs) } else e }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 5110c5a0-7239-4bc8-bc10-9d7477334a9b
📒 Files selected for processing (2)
app/src/main/java/com/messark/hawker/MainViewModel.ktfixes.md
|
Stall kill-credit threshold doesn’t match the new death threshold. The death/reward check at line 845 was updated to currentHealth < 1.0f (REF-003), but the per-projectile kill attribution at line 832 still uses currentHealth <= 0. When an enemy dies with fractional residual HP in (0, 1) — easily reachable via ARMOR buffs (line 814) or stallDef.applyDamageModifiers producing non-integer damage — the player is awarded gold/score and the enemy is removed, but the firing stall’s kills counter and uniqueTargetIds are never incremented. Stall stats will silently undercount kills for any non-integer damage path. Use the same threshold as the post-loop death check (or factor it into a shared isDead predicate so the two cannot drift again). |
|
Duplicate KDoc above handleStallFiring. The new KDoc (lines 632-639) is stacked on top of the existing one (lines 625-631) for the same function. Kotlin will only treat the closest block as the function’s doc, but both will render in source/IDE tooltips depending on tooling. Drop the older block (or merge them) to avoid drift. Reuse enemyLookup for buff source resolution. The post-damage buff cleanup still does a linear state.enemies.find per buff per surviving enemy (O(buffs · enemies) per tick), which goes against the O(1) lookup goal of this PR. The already-built enemyLookup map (line 740) gives the same result with a single map probe. |
Good catch! I've updated the kill-credit threshold in |
- Updated `handleProjectiles` to use `currentHealth < 1.0f` for stall kill attribution, matching the enemy death check. - This ensures stalls correctly receive credit for kills involving fractional damage. Co-authored-by: candour <4670475+candour@users.noreply.github.com>
Build Successful! 🚀Note: This link will be removed when the PR is closed. |
|
Duplicate KDoc above handleStallFiring. The new KDoc (lines 632-639) is stacked on top of the existing one (lines 625-631) for the same function. Kotlin will only treat the closest block as the function’s doc, but both will render in source/IDE tooltips depending on tooling. Drop the older block (or merge them) to avoid drift. Reuse enemyLookup for buff source resolution. The post-damage buff cleanup still does a linear state.enemies.find per buff per surviving enemy (O(buffs · enemies) per tick), which goes against the O(1) lookup goal of this PR. The already-built enemyLookup map (line 740) gives the same result with a single map probe. |
Fixed! I've removed the duplicate KDoc block above |
- Resolved duplicate KDoc above `handleStallFiring`. - Optimized buff cleanup in `handleProjectiles` to use `enemyLookup` map. - Ensured consistent $O(1)$ lookup performance throughout the combat tick. Co-authored-by: candour <4670475+candour@users.noreply.github.com>
Build Successful! 🚀Note: This link will be removed when the PR is closed. |
I have refactored the core combat logic in
MainViewModel.ktto improve efficiency and maintainability.Key improvements:
enemyLookupmap inhandleProjectiles, changing target enemy lookups fromhandleStallFiringby pre-filtering for stalls that are actually ready to fire, reducing iterations over the entire hex grid.Verified all changes with existing and updated unit tests. The project compiles successfully.
PR created automatically by Jules for task 10249510848776498304 started by @candour
Summary by CodeRabbit
Refactor
Chores