From c7eb577a63cd23186648d5fe183fd181a3cda31c Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 12 Jul 2025 18:53:39 -0300 Subject: [PATCH] Implements Handler Method For Cleanup --- src/roleHandlers/base.handler.interface.ts | 1 + src/roleHandlers/harvester.handler.ts | 32 +++++++++++++++++++++- src/roleHandlers/upgrader.handler.ts | 2 ++ src/spawnHandler.ts | 7 ++++- src/types/gameConfig.ts | 6 ++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/roleHandlers/base.handler.interface.ts b/src/roleHandlers/base.handler.interface.ts index 5378c6e..055e5c2 100644 --- a/src/roleHandlers/base.handler.interface.ts +++ b/src/roleHandlers/base.handler.interface.ts @@ -1,3 +1,4 @@ export abstract class RoleHandler { + static destroy(creepMemory: CreepMemory, state: GameState): void {}; static run(creep: Creep, state: GameState): GameState {}; } diff --git a/src/roleHandlers/harvester.handler.ts b/src/roleHandlers/harvester.handler.ts index f67d75a..55cde7b 100644 --- a/src/roleHandlers/harvester.handler.ts +++ b/src/roleHandlers/harvester.handler.ts @@ -1,10 +1,22 @@ import { getSourceById, getSpawnById } from "utils/funcs/get_by_id"; import { RoleHandler } from "./base.handler.interface"; import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus } from "types/source"; +import { SourceDestination } from "types/creeps"; class HarvesterHandler extends RoleHandler { + 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); @@ -24,7 +36,7 @@ class HarvesterHandler extends RoleHandler { } private static validateCreepMemory(creep: Creep, state: GameState) { - if (!!creep.memory.previousDestination && creep.memory.previousDestination.type === "source") { + if (creep.memory.previousDestination?.type === "source") { setSpotStatus( state.sourcesStates[creep.memory.previousDestination.id].spots, creep.memory.previousDestination.sourceSpot, @@ -134,6 +146,24 @@ class HarvesterHandler 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/roleHandlers/upgrader.handler.ts b/src/roleHandlers/upgrader.handler.ts index 99470b7..cd803b1 100644 --- a/src/roleHandlers/upgrader.handler.ts +++ b/src/roleHandlers/upgrader.handler.ts @@ -5,6 +5,8 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus class UpgraderHandler extends RoleHandler { + public static destroy(creepMemory: CreepMemory, state: GameState): void {} + public static run(creep: Creep, state: GameState): GameState { this.validateCreepMemory(creep, state); diff --git a/src/spawnHandler.ts b/src/spawnHandler.ts index cf431ac..4fe56fb 100644 --- a/src/spawnHandler.ts +++ b/src/spawnHandler.ts @@ -14,7 +14,12 @@ class SpawnHandler { public run(state: GameState): GameState { this.updateSpawnState(state); - for(const name in Game.creeps) { + for(const name in Memory.creeps) { + if (!Game.creeps[name]) { + 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 + } const creep = Game.creeps[name]; const roleDefinition = CreepRoles[creep.memory.role as CreepRole]; diff --git a/src/types/gameConfig.ts b/src/types/gameConfig.ts index f79262d..bd11699 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: 15, + maxCreeps: 100, minCreepsPerRole: { - harvester: 2, - upgrader: 5, + harvester: 3, + upgrader: 20, builder: 0 } };