From 293aaef0d4ec0fd74236fd5a99c9270ff208d049 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 12 Jul 2025 11:19:08 -0300 Subject: [PATCH] Better Upgrader Handler --- src/roleHandlers/upgrader.handler.ts | 66 ++++++++++++++++------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/roleHandlers/upgrader.handler.ts b/src/roleHandlers/upgrader.handler.ts index b62b404..69db62f 100644 --- a/src/roleHandlers/upgrader.handler.ts +++ b/src/roleHandlers/upgrader.handler.ts @@ -6,6 +6,8 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus class UpgraderHandler extends RoleHandler { public static run(creep: Creep, state: GameState): GameState { + this.validateCreepMemory(creep, state); + switch (creep.memory.destination?.type) { case "controller": this.onControllerDestination(creep, state); @@ -21,6 +23,42 @@ class UpgraderHandler extends RoleHandler { return state; } + private static validateCreepMemory(creep: Creep, state: GameState) { + if (!creep.memory.destination) { + return; // No destination set, nothing to validate + } + + if (creep.memory.destination.type === "source" && !!creep.memory.previousDestination && creep.memory.previousDestination.type === "source") { + setSpotStatus( + state.sourcesStates[creep.memory.previousDestination.id].spots, + creep.memory.previousDestination.sourceSpot, + SourceSpotStatus.EMPTY + ); + delete creep.memory.previousDestination; + } + + if (creep.memory.destination.type === "source" && !creep.store.getFreeCapacity(RESOURCE_ENERGY)) { + creep.memory.previousDestination = creep.memory.destination; + + if (!creep.room.controller) { + console.log(`Creep ${creep.name} has no valid controller to upgrade.`); + delete creep.memory.destination; + return; + } + + creep.memory.destination = { + id: creep.room.controller.id, + type: "controller" + }; + return; + } + + if (creep.memory.destination.type === "controller" && creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) { + delete creep.memory.destination; // Clear destination if no energy is available + return; + } + } + private static onFindNewSource(creep: Creep, state: GameState) { if (creep.memory.destination) { console.log(`Creep ${creep.name} already has a destination set.`); @@ -60,21 +98,6 @@ class UpgraderHandler extends RoleHandler { return; } - if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) { - creep.memory.previousDestination = creep.memory.destination; - - if (!creep.room.controller) { - console.log(`Creep ${creep.name} has no valid controller to upgrade.`); - delete creep.memory.destination; - return; - } - creep.memory.destination = { - id: creep.room.controller.id, - type: "controller" - }; - return; - } - const source = getSourceById(creep.memory.destination.id); if (source === null) { console.log(`Source not found for creep: ${creep.name}`); @@ -90,14 +113,6 @@ class UpgraderHandler extends RoleHandler { } private static onControllerDestination(creep: Creep, state: GameState) { - if (!!creep.memory.previousDestination && creep.memory.previousDestination.type === "source") { - setSpotStatus( - state.sourcesStates[creep.memory.previousDestination.id].spots, - creep.memory.previousDestination.sourceSpot, - SourceSpotStatus.EMPTY - ); - delete creep.memory.previousDestination; // Clear previous destination if it exists - } if (creep.memory.destination === undefined) { if (!creep.room.controller) { console.log(`Creep ${creep.name} has no valid controller to upgrade.`); @@ -110,11 +125,6 @@ class UpgraderHandler extends RoleHandler { } } - if (creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) { - delete creep.memory.destination; - return; - } - const controller = getControllerById(creep.memory.destination.id); if (!controller) { console.log(`Spawn not found for creep: ${creep.name}`);