Fix: SAM and Airfield stars now update when research completes#209
Merged
Fix: SAM and Airfield stars now update when research completes#209
Conversation
Auto-refresh structure texture cache when player unlocks tech upgrades to immediately display updated star indicators on the map. Problem: - Commit dd427b0 added star level indicators to SAM/Airfield structures - Textures were cached with tech level in the key (-lvl1, -lvl2, -lvl3) - Cache was never invalidated when research completed - Build menu showed correct stars (recalculates each render) - Map showed stale stars until game reload or upgrade mode toggle Solution: - Listen for GameUpdateType.Player updates in tick() method - Detect when researchTreeTechs array is updated - Rebuild textures for SAM/Airfield structures owned by that player - Follows same pattern as TechUnlockNotification layer Implementation: - Import PlayerUpdate type from GameUpdates - Add player update handler before unit updates in tick() - Filter for SAM/Airfield structures matching updated player ID - Recreate texture using createTexture() (includes new tech level) - Set shouldRedraw flag to trigger render Performance: - Runs only when research completes (rare event, ~1-10 per game) - Iterates rendered structures (typically 10-100 items) - Rebuilds 1-20 textures per event (typical SAM/Airfield count) - Identical pattern to upgrade mode toggle (proven acceptable) - Negligible impact on frame rate Visual result: - Stars update immediately when research unlocks SAM Level 2/3 - Stars update immediately when research unlocks Bomber Level 2/3 - Map display now matches build menu star indicators - No reload or mode toggle required
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure SAM Launcher and Airfield star indicators on the map update immediately after a player completes relevant research, by refreshing the structure texture cache when player tech state changes.
Changes:
- Adds handling for
GameUpdateType.Playerupdates insideStructureLayer.tick(). - On detected research/tech change, recreates textures for SAM Launcher and Airfield sprites owned by the updated player and triggers a redraw.
Address performance regression and type errors from previous commit:
Type fixes:
- Import PlayerType from Game module
- Change lastPlayerTechLevels Map key from PlayerID (string) to number (smallID)
- Use smallID for player lookups (playerBySmallID returns PlayerView|TerraNullius)
- Check for hasUpgrade property instead of PlayerType to distinguish PlayerView from TerraNullius
- Use smallID for comparisons (owner().smallID() not owner().id())
Performance fix:
- Cache previous tech levels per player (samLevel, airfieldLevel)
- Only rebuild textures when tech level actually changes
- Prevents O(players × structures) texture rebuilds every tick
- Was rebuilding 60 times/second, now only rebuilds on research completion
Implementation:
- Track last known tech levels in lastPlayerTechLevels Map<number, {samLevel, airfieldLevel}>
- Compare current vs cached levels each tick
- Only recreate textures when levels differ
- Update cache after rebuilding
Result:
- No more per-tick texture recreation performance regression
- Textures still update immediately when research completes
- TypeScript compilation passes without errors
Reduce per-tick overhead by only checking tech level changes every 10 ticks instead of every tick. Research completion is rare and a ~0.1-0.2s delay before stars update is imperceptible.
… rebuilds When a player is first seen, seed the cache with current levels instead of rebuilding all their SAM/Airfield textures. Textures are already created with the correct tech level, so only subsequent changes need rebuilds.
- Use isPlayer() type guard instead of brittle 'hasUpgrade' in check - On first encounter: only rebuild if level > 1 (research already happened) This handles the edge case where textures were created before research completed but cache is seeded after - On subsequent checks: rebuild only if cached level differs from current - Simplifies logic by always updating cache and computing change flags
1brucben
pushed a commit
that referenced
this pull request
Feb 20, 2026
* Fix: SAM and Airfield stars now update when research completes Auto-refresh structure texture cache when player unlocks tech upgrades to immediately display updated star indicators on the map. Problem: - Commit dd427b0 added star level indicators to SAM/Airfield structures - Textures were cached with tech level in the key (-lvl1, -lvl2, -lvl3) - Cache was never invalidated when research completed - Build menu showed correct stars (recalculates each render) - Map showed stale stars until game reload or upgrade mode toggle Solution: - Listen for GameUpdateType.Player updates in tick() method - Detect when researchTreeTechs array is updated - Rebuild textures for SAM/Airfield structures owned by that player - Follows same pattern as TechUnlockNotification layer Implementation: - Import PlayerUpdate type from GameUpdates - Add player update handler before unit updates in tick() - Filter for SAM/Airfield structures matching updated player ID - Recreate texture using createTexture() (includes new tech level) - Set shouldRedraw flag to trigger render Performance: - Runs only when research completes (rare event, ~1-10 per game) - Iterates rendered structures (typically 10-100 items) - Rebuilds 1-20 textures per event (typical SAM/Airfield count) - Identical pattern to upgrade mode toggle (proven acceptable) - Negligible impact on frame rate Visual result: - Stars update immediately when research unlocks SAM Level 2/3 - Stars update immediately when research unlocks Bomber Level 2/3 - Map display now matches build menu star indicators - No reload or mode toggle required * Fix TypeScript errors and prevent per-tick texture rebuilds Address performance regression and type errors from previous commit: Type fixes: - Import PlayerType from Game module - Change lastPlayerTechLevels Map key from PlayerID (string) to number (smallID) - Use smallID for player lookups (playerBySmallID returns PlayerView|TerraNullius) - Check for hasUpgrade property instead of PlayerType to distinguish PlayerView from TerraNullius - Use smallID for comparisons (owner().smallID() not owner().id()) Performance fix: - Cache previous tech levels per player (samLevel, airfieldLevel) - Only rebuild textures when tech level actually changes - Prevents O(players × structures) texture rebuilds every tick - Was rebuilding 60 times/second, now only rebuilds on research completion Implementation: - Track last known tech levels in lastPlayerTechLevels Map<number, {samLevel, airfieldLevel}> - Compare current vs cached levels each tick - Only recreate textures when levels differ - Update cache after rebuilding Result: - No more per-tick texture recreation performance regression - Textures still update immediately when research completes - TypeScript compilation passes without errors * Throttle tech level checks to every 10 ticks Reduce per-tick overhead by only checking tech level changes every 10 ticks instead of every tick. Research completion is rare and a ~0.1-0.2s delay before stars update is imperceptible. * Seed tech level cache on first encounter to avoid unnecessary texture rebuilds When a player is first seen, seed the cache with current levels instead of rebuilding all their SAM/Airfield textures. Textures are already created with the correct tech level, so only subsequent changes need rebuilds. * Fix edge case and use proper type guard - Use isPlayer() type guard instead of brittle 'hasUpgrade' in check - On first encounter: only rebuild if level > 1 (research already happened) This handles the edge case where textures were created before research completed but cache is seeded after - On subsequent checks: rebuild only if cached level differs from current - Simplifies logic by always updating cache and computing change flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Auto-refresh structure texture cache when player unlocks tech upgrades to immediately display updated star indicators on the map.
Problem:
Solution:
Implementation:
Performance:
Visual result:
Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
el_magico777