Skip to main content

Minimap Hooks API Reference

Files:
  • packages/client/src/game/hud/useMinimapTerrainCache.ts
  • packages/client/src/game/hud/useMinimapEntityPips.ts
  • packages/client/src/game/hud/useMinimapWorldCaches.ts
Added: March 2026 (PR #1067)

Overview

Modular React hooks for minimap rendering, extracted from 772 lines of inline logic in Minimap.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 requestIdleCallback for non-blocking terrain generation
  • Processes terrain in chunks to avoid blocking main thread
  • Cancellable via version ref
Biome Coloring:
  • LRU cache for biome colors (max 256 entries)
  • Automatic eviction when cache exceeds limit
  • Consistent colors across terrain tiles
Zoom-Aware Detail:
  • Adjusts terrain detail based on zoom level
  • Higher zoom = more detail
  • Lower zoom = less detail for performance
Version-Based Cache Invalidation:
  • 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:
Biome Color Cache:
Chunked Generation:

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 OffscreenCanvas to cache entity icons
  • Prevents redundant image loading
  • Shared cache across all entity instances
Quest Status Indicators:
  • Quest available (yellow exclamation mark)
  • Quest in progress (blue question mark)
  • Quest completed (green checkmark)
  • Detects quest status from world state
Spectator Target Highlighting:
  • Highlights spectated player with distinct styling
  • Larger pip size for visibility
  • Different color for spectator target
Extent Culling:
  • Only renders entities within minimap bounds
  • Improves performance with many entities
  • Automatic culling based on camera position and zoom
Entity Cache Pruning:
  • Tracks lastSeenTick for each entity
  • Prunes stale entries (not seen in 100 ticks)
  • Prevents unbounded cache growth

Usage Example

Implementation Details

RAF-Throttled Updates:
Icon Caching:
Quest Status Detection:

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:generated event
  • Listens for towns:generated event
  • Automatically refreshes caches when world data changes
Proper Cleanup:
  • 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 requestIdleCallback to avoid blocking
  • Cancellable when camera moves
Biome Color Cache:
  • LRU eviction prevents unbounded growth
  • Max 256 entries (sufficient for most use cases)
  • Shared across all terrain tiles
Version-Based Invalidation:
  • 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
Icon Caching:
  • Prevents redundant image loading
  • Shared cache across all entity instances
  • Cleared on unmount
Extent Culling:
  • Only renders entities within minimap bounds
  • Improves performance with many entities
  • Automatic culling based on camera position
Entity Cache Pruning:
  • Tracks lastSeenTick for each entity
  • Prunes entries not seen in 100 ticks
  • Prevents unbounded cache growth

World Caches

Event-Driven Updates:
  • Only updates when roads:generated or towns:generated events fire
  • No polling or continuous updates
  • Minimal CPU usage

Migration from Old Pattern

Before (Inline Logic)

After (Modular Hooks)

Benefits

  1. Modular Architecture: Each hook has single responsibility
  2. Reusable Logic: Hooks can be used independently
  3. Better Testing: Easier to test individual hooks
  4. Proper Cleanup: All resources cleaned up on unmount
  5. Performance: Optimized with caching, throttling, and culling
  6. Type Safety: Strongly typed parameters and returns
  7. Maintainability: Easier to understand and modify

Known Issues

None - All known issues from PR review have been addressed:
  • Resize listeners properly cleaned up
  • towns:generated event listener added
  • .catch() added to terrain generation promises
  • Biome color cache has LRU eviction
  • Entity cache has pruning logic

Future Improvements

Potential Enhancements:
  1. Extract icon caching to shared utility
  2. Add configurable cache sizes
  3. Support custom entity pip renderers
  4. Add minimap overlay layers (fog of war, etc.)
  5. Support minimap zoom animation
  • usePlayerData - Centralized player data subscription
  • useModalPanels - Centralized modal panel state

See Also