Task Prioritization and Efficiency
This document describes how creeps prioritize tasks and optimize their behavior for maximum efficiency.
Overview
The task prioritization system ensures creeps make optimal decisions about what action to perform each tick based on their current state, role, and room conditions.
Harvester Task Priority
Priority Hierarchy
HARVEST (Highest Priority when empty)
- Executed when:
store.getFreeCapacity(RESOURCE_ENERGY) > 0 - Goal: Fill carry capacity to maximum
- Efficiency target: 100% capacity utilization
- Executed when:
DELIVER (High Priority when full)
- Executed when:
store.getUsedCapacity(RESOURCE_ENERGY) > 0AND delivery targets exist - Goal: Supply spawn infrastructure
- Efficiency target: Minimize delivery travel time
- Executed when:
UPGRADE (Fallback Priority)
- Executed when: No delivery targets available
- Goal: Use surplus energy productively
- Efficiency target: Zero wasted energy
Target Selection Algorithm
Source Selection (HARVEST):
1 | 1. Find all active sources in room |
Efficiency Considerations:
- Closest path reduces travel overhead
- Active sources check prevents targeting depleted sources
- Fallback ensures creep always has a target
Delivery Target Selection (DELIVER):
1 | 1. Find spawns and extensions with free energy capacity |
Priority Order (implicit in filter):
- Spawns (enables new creep production)
- Extensions (enables larger creep bodies)
Efficiency Considerations:
- Closest target minimizes delivery time
- Free capacity check prevents wasted transfer attempts
- No explicit priority between spawns/extensions (proximity wins)
Task Transition Timing
Immediate Transitions: No delay between state changes
HARVEST → DELIVER: Triggered same tick when capacity fullDELIVER → HARVEST: Triggered same tick when energy depletedDELIVER → UPGRADE: Triggered same tick when no delivery targets
Benefits:
- Zero idle ticks
- Maximum creep utilization
- Responsive to changing conditions
Upgrader Task Priority
Priority Hierarchy
RECHARGE (Highest Priority when empty)
- Executed when:
store.getFreeCapacity(RESOURCE_ENERGY) > 0 - Goal: Acquire energy for upgrading
- Efficiency target: Minimize withdrawal travel time
- Executed when:
UPGRADE (High Priority when full)
- Executed when:
store.getUsedCapacity(RESOURCE_ENERGY) > 0 - Goal: Convert energy to controller points
- Efficiency target: 100% energy → controller point conversion
- Executed when:
Energy Source Selection
Source Priority Order (explicit in filter):
1 | 1. Spawns with stored energy |
Selection Algorithm:
1 | 1. Find all valid energy structures |
Efficiency Considerations:
- Harvests from storage rather than competing with harvesters for sources
- Closest source minimizes recharge cycle time
- Container support enables longer-distance energy collection
Task Transition Timing
Immediate Transitions:
RECHARGE → UPGRADE: Same tick when capacity fullUPGRADE → RECHARGE: Same tick when energy depleted
Pathfinding Optimization
Path Reuse Strategy
Configuration: reusePath: 5
Behavior:
- Path calculated once, reused for 5 ticks
- New path calculated on tick 6 or target change
- Significantly reduces CPU cost
Cost Analysis:
- Fresh pathfinding: ~0.5-2.0 CPU per creep
- Cached path reuse: ~0.05-0.1 CPU per creep
- Savings: 90-95% CPU reduction
Path Invalidation Triggers
Paths are recalculated when:
- TTL expires (5 ticks elapsed)
- Target changes (source/structure changes)
- ERR_NO_PATH returned (blocked path)
Range Optimization
Harvest/Withdraw/Transfer: range: 1
- Must be adjacent to interact
- Pathfinding targets 1-tile proximity
Upgrade Controller: range: 3
- Can upgrade from 3 tiles away
- Reduces repositioning overhead
- Allows multiple upgraders around controller
Efficiency Metrics
Harvester Efficiency
Ideal Cycle (no contention, close sources):
1 | Harvest Time: 25 ticks (50 energy / 2 per tick) |
Real-World Cycle (with contention):
1 | Harvest Time: 25 ticks |
Upgrader Efficiency
Ideal Cycle:
1 | Withdraw Time: 1 tick |
Real-World Cycle:
1 | Withdraw Time: 1 tick |
Task Switching Overhead
Zero-Latency Switching
The current implementation has no explicit overhead for task switches:
- Conditions checked every tick
- Transitions happen immediately
- No “waiting” or “idle” states
Implicit Overhead Sources
Pathfinding Recalculation
- Occurs when target changes
- Mitigated by path caching
- Cost: ~0.5 CPU one-time
Target Search
- Find operations executed every task switch
- Cost: ~0.1-0.3 CPU per search
- Cannot be cached (room state changes)
Movement Latency
- Creep must travel to new target
- Time: varies by distance
- Optimization: minimize unnecessary switches
Load Balancing
Source Distribution
Current Behavior: All harvesters use “closest source” logic
- Risk: Multiple harvesters on same source
- Contention: Reduced harvest efficiency
- Mitigation: None (implicit load balancing by spawn timing)
Optimization Opportunity:
- Assign harvesters to specific sources
- Prevent overcrowding
- Potential efficiency gain: 10-20%
Spawn Access
Current Behavior: All harvesters use “closest spawn/extension” logic
- Risk: Queue formation at spawns
- Contention: Harvesters wait for transfer access
- Mitigation: None (first-come-first-served)
Optimization Opportunity:
- Distribute deliveries across extensions
- Reserve spawns for critical transfers
- Potential efficiency gain: 5-15%
Energy Flow Balance
Steady State Requirements
For sustainable operation:
1 | Harvest Rate ≥ Spawn Cost Rate + Upgrade Cost Rate + Maintenance |
Minimum Viable Economy (RCL 1):
- 2 Harvesters: ~10 energy/tick income
- Spawn cost: ~1.4 energy/tick average (50 energy / 36 tick cycle)
- Upgrader consumption: ~0.8 energy/tick (1 upgrader fully utilized)
- Balance: 10 - 1.4 - 0.8 = 7.8 energy/tick surplus ✓
Bottleneck Identification
Common Bottlenecks:
Insufficient Harvesters
- Symptom: Spawns frequently empty
- Metric:
spawn.store.energy < 50for >50% of ticks - Solution: Add 1 harvester
Insufficient Upgraders
- Symptom: Spawns frequently at capacity
- Metric:
spawn.store.energy === spawn.store.getCapacity(RESOURCE_ENERGY)for >20% of ticks - Solution: Add 1 upgrader
Insufficient CPU
- Symptom: Bucket draining
- Metric:
Game.cpu.bucket < 1000trend downward - Solution: Reduce creep count or optimize pathfinding
Priority Inversion Scenarios
Harvester Stealing from Upgrader
Scenario: Harvester uses UPGRADE task as fallback
- Risk: Competes with upgraders for controller access
- Impact: Reduces upgrader efficiency (positioning conflicts)
- Frequency: Only when spawns/extensions full
- Mitigation: None needed (overflow handling)
Source Competition
Scenario: Multiple harvesters target same source
- Risk: Harvest slots fill up (max 5 adjacent positions)
- Impact: Some harvesters idle waiting for access
- Frequency: Common with >2 harvesters on 1 source
- Mitigation: Manual source assignment (not implemented)
Task Efficiency Improvements
Potential Optimizations
Source Assignment
- Assign harvesters to specific sources
- Prevents overcrowding
- Estimated gain: 10-20% harvest efficiency
Delivery Batching
- Wait for fuller capacity before delivering
- Reduces travel overhead
- Estimated gain: 5-10% delivery efficiency
- Trade-off: Increased response latency
Predictive Task Switching
- Start moving to next target before current task completes
- Reduces transition latency
- Estimated gain: 2-5% overall efficiency
- Complexity: High (requires precise timing)
Priority Delivery Targets
- Prioritize spawns over extensions explicitly
- Ensures spawn queue never starves
- Estimated gain: 3-7% spawn uptime
Related Documentation
- Creep Roles - Role definitions and behavior logic
- Scaling Strategies - Population scaling guidelines
- Performance Monitoring - Efficiency measurement
- Safe Refactoring - How to modify task logic safely