Skip to content

Comments

Fix: SAM and Airfield stars now update when research completes#209

Merged
1brucben merged 5 commits intov0.2.3from
fix-structure-star-refresh-magic
Feb 20, 2026
Merged

Fix: SAM and Airfield stars now update when research completes#209
1brucben merged 5 commits intov0.2.3from
fix-structure-star-refresh-magic

Conversation

@El-Magico777
Copy link
Collaborator

@El-Magico777 El-Magico777 commented Feb 6, 2026

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

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
  • I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors

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

el_magico777

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
@El-Magico777 El-Magico777 added this to the v0.2.3 milestone Feb 6, 2026
@El-Magico777 El-Magico777 self-assigned this Feb 6, 2026
@El-Magico777 El-Magico777 added the bug Something isn't working label Feb 6, 2026
@El-Magico777 El-Magico777 requested a review from Copilot February 6, 2026 22:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.Player updates inside StructureLayer.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.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

… 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.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

- 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
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

@El-Magico777 El-Magico777 marked this pull request as ready for review February 6, 2026 23:02
@1brucben 1brucben merged commit 6459a53 into v0.2.3 Feb 20, 2026
13 checks passed
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
@El-Magico777 El-Magico777 modified the milestones: v0.2.3, v0.3.0 Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants