From 7bee8550029119c0bfd9af0037e04b587e001464 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 12 Jul 2025 11:15:20 -0300 Subject: [PATCH] Better Harvester Handler --- src/roleHandlers/harvester.handler.ts | 53 ++++++++++++++++----------- src/types/creeps.ts | 12 +++++- src/types/gameConfig.ts | 4 +- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/roleHandlers/harvester.handler.ts b/src/roleHandlers/harvester.handler.ts index b7b368d..60f1561 100644 --- a/src/roleHandlers/harvester.handler.ts +++ b/src/roleHandlers/harvester.handler.ts @@ -6,6 +6,8 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus class HarvesterHandler extends RoleHandler { public static run(creep: Creep, state: GameState): GameState { + this.validateCreepMemory(creep, state); + switch (creep.memory.destination?.type) { case "spawn": this.onSpawnDestination(creep, state); @@ -21,6 +23,35 @@ class HarvesterHandler 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; + creep.memory.destination = { + id: creep.memory.spawnId, + type: "spawn" + }; + return; + } + + if (creep.memory.destination.type === "spawn" && 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,15 +91,6 @@ class HarvesterHandler extends RoleHandler { return; } - if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) { - creep.memory.previousDestination = creep.memory.destination; - creep.memory.destination = { - id: creep.memory.spawnId, - type: "spawn" - }; - return; - } - const source = getSourceById(creep.memory.destination.id); if (source === null) { console.log(`Source not found for creep: ${creep.name}`); @@ -84,14 +106,6 @@ class HarvesterHandler extends RoleHandler { } private static onSpawnDestination(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) { creep.memory.destination = { id: creep.memory.spawnId, @@ -99,11 +113,6 @@ class HarvesterHandler extends RoleHandler { } } - if (creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) { - delete creep.memory.destination; - return; - } - const spawn = getSpawnById(creep.memory.destination.id); if (!spawn) { console.log(`Spawn not found for creep: ${creep.name}`); diff --git a/src/types/creeps.ts b/src/types/creeps.ts index 75579bb..30f3a6b 100644 --- a/src/types/creeps.ts +++ b/src/types/creeps.ts @@ -52,6 +52,16 @@ export type ControllerDestination = { type: "controller"; } +export type ConstructionSiteDestination = { + id: string; // ID of the construction site + type: "constructionSite"; +} -export type CreepDestination = SpawnDestination | SourceDestination | ControllerDestination; + +export type CreepDestination = ( + SpawnDestination | + SourceDestination | + ControllerDestination | + ConstructionSiteDestination +); diff --git a/src/types/gameConfig.ts b/src/types/gameConfig.ts index ecb00e8..f79262d 100644 --- a/src/types/gameConfig.ts +++ b/src/types/gameConfig.ts @@ -24,8 +24,8 @@ export type GameConfig = { export const DEFAULT_GAME_CONFIG: GameConfig = { maxCreeps: 15, minCreepsPerRole: { - harvester: 5, - upgrader: 3, + harvester: 2, + upgrader: 5, builder: 0 } };