From 56e626e5751d7fe29d721fc528d22f2751e849a6 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 12 Jul 2025 19:24:31 -0300 Subject: [PATCH] Implements Upgrader Handler Method For Cleanup --- src/roleHandlers/upgrader.handler.ts | 29 +++++++++++++++++++++++++++- src/spawnHandler.ts | 4 ++++ src/types/gameConfig.ts | 4 ++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/roleHandlers/upgrader.handler.ts b/src/roleHandlers/upgrader.handler.ts index cd803b1..a7c7fc6 100644 --- a/src/roleHandlers/upgrader.handler.ts +++ b/src/roleHandlers/upgrader.handler.ts @@ -5,7 +5,16 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus class UpgraderHandler extends RoleHandler { - public static destroy(creepMemory: CreepMemory, state: GameState): void {} + public static destroy(creepMemory: CreepMemory, state: GameState): void { + if (creepMemory.destination?.type === "source") { + this.releaseSourceSpot(creepMemory.destination, state); + delete creepMemory.destination; // Clear destination after releasing the spot + } + if (creepMemory.previousDestination?.type === "source") { + this.releaseSourceSpot(creepMemory.previousDestination, state); + delete creepMemory.previousDestination; // Clear previous destination after releasing the spot + } + } public static run(creep: Creep, state: GameState): GameState { this.validateCreepMemory(creep, state); @@ -148,6 +157,24 @@ class UpgraderHandler extends RoleHandler { return sources as Source[]; } + + private static releaseSourceSpot(destination: SourceDestination, state: GameState) { + if (!destination || destination.type !== "source") { + return; // Not a source destination, nothing to release + } + + const sourceState = state.sourcesStates[destination.id]; + if (!sourceState) { + console.log(`Source state not found for ID: ${destination.id}`); + return; + } + + setSpotStatus( + sourceState.spots, + destination.sourceSpot, + SourceSpotStatus.EMPTY + ); + } } diff --git a/src/spawnHandler.ts b/src/spawnHandler.ts index 4fe56fb..3d06794 100644 --- a/src/spawnHandler.ts +++ b/src/spawnHandler.ts @@ -16,9 +16,13 @@ class SpawnHandler { for(const name in Memory.creeps) { if (!Game.creeps[name]) { + console.log(`Creep ${name} is dead, cleaning up memory.`); + const roleDefinition = CreepRoles[Memory.creeps[name].role as CreepRole]; roleDefinition.handler.destroy(Memory.creeps[name], state); delete Memory.creeps[name]; // Clean up memory for dead creeps + + continue; // Skip to the next creep } const creep = Game.creeps[name]; diff --git a/src/types/gameConfig.ts b/src/types/gameConfig.ts index bd11699..61c8e78 100644 --- a/src/types/gameConfig.ts +++ b/src/types/gameConfig.ts @@ -22,10 +22,10 @@ export type GameConfig = { * @type {GameConfig} */ export const DEFAULT_GAME_CONFIG: GameConfig = { - maxCreeps: 100, + maxCreeps: 10, minCreepsPerRole: { harvester: 3, - upgrader: 20, + upgrader: 7, builder: 0 } };