Minimap Hooks API Reference
Files:packages/client/src/game/hud/useMinimapTerrainCache.tspackages/client/src/game/hud/useMinimapEntityPips.tspackages/client/src/game/hud/useMinimapWorldCaches.ts
Overview
Modular React hooks for minimap rendering, extracted from 772 lines of inline logic inMinimap.tsx. Provides terrain rendering, entity markers, and world caches (roads/towns) with proper cleanup and performance optimizations.
useMinimapTerrainCache
File:packages/client/src/game/hud/useMinimapTerrainCache.ts
Purpose
Handles terrain rendering with biome coloring, chunked generation, and zoom-aware detail levels.API
Parameters
Returns
Features
Chunked Generation:- Uses
requestIdleCallbackfor non-blocking terrain generation - Processes terrain in chunks to avoid blocking main thread
- Cancellable via version ref
- LRU cache for biome colors (max 256 entries)
- Automatic eviction when cache exceeds limit
- Consistent colors across terrain tiles
- Adjusts terrain detail based on zoom level
- Higher zoom = more detail
- Lower zoom = less detail for performance
- Increments version on camera move or zoom change
- Cancels in-flight generation when version changes
- Prevents stale terrain from rendering
Usage Example
Implementation Details
OffscreenCanvas:Cleanup
useMinimapEntityPips
File:packages/client/src/game/hud/useMinimapEntityPips.ts
Purpose
Handles entity marker rendering (players, NPCs, resources) with icon caching and quest status indicators.API
Parameters
Returns
Features
Icon Caching:- Uses
OffscreenCanvasto cache entity icons - Prevents redundant image loading
- Shared cache across all entity instances
- Quest available (yellow exclamation mark)
- Quest in progress (blue question mark)
- Quest completed (green checkmark)
- Detects quest status from world state
- Highlights spectated player with distinct styling
- Larger pip size for visibility
- Different color for spectator target
- Only renders entities within minimap bounds
- Improves performance with many entities
- Automatic culling based on camera position and zoom
- Tracks
lastSeenTickfor each entity - Prunes stale entries (not seen in 100 ticks)
- Prevents unbounded cache growth
Usage Example
Implementation Details
RAF-Throttled Updates:Cleanup
useMinimapWorldCaches
File:packages/client/src/game/hud/useMinimapWorldCaches.ts
Purpose
Handles road and town network caching with event-driven updates.API
Parameters
Returns
Features
Event-Driven Updates:- Listens for
roads:generatedevent - Listens for
towns:generatedevent - Automatically refreshes caches when world data changes
- Removes event listeners on unmount
- Prevents memory leaks
Usage Example
Implementation Details
Event Listeners:Cleanup
Common Patterns
Combining All Minimap Hooks
Clearing All Caches
Performance Considerations
Terrain Cache
Chunked Generation:- Processes terrain in small chunks
- Uses
requestIdleCallbackto avoid blocking - Cancellable when camera moves
- LRU eviction prevents unbounded growth
- Max 256 entries (sufficient for most use cases)
- Shared across all terrain tiles
- Increments version on camera move
- Cancels in-flight generation
- Prevents rendering stale terrain
Entity Pips
RAF-Throttled Updates:- Updates at ~30fps (requestAnimationFrame)
- Only when minimap is visible
- Automatic pause when hidden
- Prevents redundant image loading
- Shared cache across all entity instances
- Cleared on unmount
- Only renders entities within minimap bounds
- Improves performance with many entities
- Automatic culling based on camera position
- Tracks
lastSeenTickfor each entity - Prunes entries not seen in 100 ticks
- Prevents unbounded cache growth
World Caches
Event-Driven Updates:- Only updates when
roads:generatedortowns:generatedevents fire - No polling or continuous updates
- Minimal CPU usage
Migration from Old Pattern
Before (Inline Logic)
After (Modular Hooks)
Benefits
- Modular Architecture: Each hook has single responsibility
- Reusable Logic: Hooks can be used independently
- Better Testing: Easier to test individual hooks
- Proper Cleanup: All resources cleaned up on unmount
- Performance: Optimized with caching, throttling, and culling
- Type Safety: Strongly typed parameters and returns
- Maintainability: Easier to understand and modify
Known Issues
None - All known issues from PR review have been addressed:- Resize listeners properly cleaned up
towns:generatedevent listener added.catch()added to terrain generation promises- Biome color cache has LRU eviction
- Entity cache has pruning logic
Future Improvements
Potential Enhancements:- Extract icon caching to shared utility
- Add configurable cache sizes
- Support custom entity pip renderers
- Add minimap overlay layers (fog of war, etc.)
- Support minimap zoom animation
Related Hooks
- usePlayerData - Centralized player data subscription
- useModalPanels - Centralized modal panel state
See Also
- UI Modernization Guide - Complete UI modernization details
- usePlayerData API - Player data hook reference
- useModalPanels API - Modal panel hook reference