diff --git a/src/roleHandlers/roleHandler.interface.ts b/src/roleHandlers/base.handler.interface.ts similarity index 100% rename from src/roleHandlers/roleHandler.interface.ts rename to src/roleHandlers/base.handler.interface.ts diff --git a/src/roleHandlers/harvesterHandler.ts b/src/roleHandlers/harvester.handler.ts similarity index 98% rename from src/roleHandlers/harvesterHandler.ts rename to src/roleHandlers/harvester.handler.ts index ca96c17..b7b368d 100644 --- a/src/roleHandlers/harvesterHandler.ts +++ b/src/roleHandlers/harvester.handler.ts @@ -1,5 +1,5 @@ import { getSourceById, getSpawnById } from "utils/funcs/get_by_id"; -import { RoleHandler } from "./roleHandler.interface"; +import { RoleHandler } from "./base.handler.interface"; import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus } from "types/source"; diff --git a/src/roleHandlers/index.ts b/src/roleHandlers/index.ts index 82cb4ab..a8fdb44 100644 --- a/src/roleHandlers/index.ts +++ b/src/roleHandlers/index.ts @@ -1,8 +1,9 @@ -import { RoleHandler } from "./roleHandler.interface"; +import { RoleHandler } from "./base.handler.interface"; -import HarvesterHandler from "./harvesterHandler"; +import HarvesterHandler from "./harvester.handler"; +import UpgraderHandler from "./upgrader.handler"; -export { RoleHandler, HarvesterHandler }; +export { RoleHandler, HarvesterHandler, UpgraderHandler }; diff --git a/src/roleHandlers/upgrader.handler.ts b/src/roleHandlers/upgrader.handler.ts new file mode 100644 index 0000000..b62b404 --- /dev/null +++ b/src/roleHandlers/upgrader.handler.ts @@ -0,0 +1,145 @@ +import { getControllerById, getSourceById } from "utils/funcs/get_by_id"; +import { RoleHandler } from "./base.handler.interface"; +import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus } from "types/source"; + + + +class UpgraderHandler extends RoleHandler { + public static run(creep: Creep, state: GameState): GameState { + switch (creep.memory.destination?.type) { + case "controller": + this.onControllerDestination(creep, state); + break; + case "source": + this.onSourceDestination(creep, state); + break; + default: + this.onFindNewSource(creep, state); + break; + } + + return state; + } + + private static onFindNewSource(creep: Creep, state: GameState) { + if (creep.memory.destination) { + console.log(`Creep ${creep.name} already has a destination set.`); + return; // Already has a destination, no need to find a new one + } + + const sources = this.findClosestSource(creep, state); + + for (const source of sources) { + const sourceState = state.sourcesStates[source.id]; + const emptySpot = getNextEmptySpot(sourceState.spots); + + if (emptySpot === null) { + continue; // No empty spots available, skip to next source + } + + setSpotStatus( + sourceState.spots, + emptySpot, + SourceSpotStatus.OCCUPIED + ); + creep.memory.destination = { + id: source.id, + type: "source", + sourceSpot: emptySpot + }; + return + } + + console.log(`Creep ${creep.name} could not find a valid source.`); + } + + private static onSourceDestination(creep: Creep, state: GameState) { + if (!creep.memory.destination || creep.memory.destination.type !== "source") { + console.log(`Creep ${creep.name} has no valid destination set.`); + delete creep.memory.destination; + 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}`); + return; + } + + if (creep.harvest(source) === ERR_NOT_IN_RANGE) { + const sourceSpotPosition = getPositionWithDelta( + source.pos, creep.memory.destination.sourceSpot + ) + creep.moveTo(sourceSpotPosition, { reusePath: 10, visualizePathStyle: { stroke: '#ffffff', lineStyle: 'dashed', strokeWidth: 0.1 } }); + } + } + + 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.`); + delete creep.memory.destination; + return; + } + creep.memory.destination = { + id: creep.room.controller.id, + type: "controller" + } + } + + 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}`); + return; + } + + if (creep.upgradeController(controller) === ERR_NOT_IN_RANGE) { + creep.moveTo(controller, { reusePath: 10, visualizePathStyle: { stroke: '#ffffff', lineStyle: 'dashed', strokeWidth: 0.1 } }); + } + } + + + + private static findClosestSource(creep: Creep, state: GameState): Source[] { + const sources = Object.keys(state.sourcesStates) + .map(sourceId => getSourceById(sourceId)) + .filter(source => source !== null) + .sort((a, b) => creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b)); + + return sources as Source[]; + } +} + + + + + +export default UpgraderHandler; diff --git a/src/types/creeps.ts b/src/types/creeps.ts index f0280d6..75579bb 100644 --- a/src/types/creeps.ts +++ b/src/types/creeps.ts @@ -1,4 +1,4 @@ -import { HarvesterHandler, RoleHandler } from "roleHandlers"; +import { HarvesterHandler, RoleHandler, UpgraderHandler } from "roleHandlers"; import { PositionDelta } from "./source"; export type RoleDefinition = { @@ -18,7 +18,7 @@ export const CreepRoles = { upgrader: { name: "upgrader", body: [WORK, CARRY, MOVE], - handler: HarvesterHandler, + handler: UpgraderHandler, priority: 2 }, builder: { @@ -47,6 +47,11 @@ export type SourceDestination = { sourceSpot: PositionDelta; // Position delta for the source spot } +export type ControllerDestination = { + id: string; // ID of the controller + type: "controller"; +} -export type CreepDestination = SpawnDestination | SourceDestination; + +export type CreepDestination = SpawnDestination | SourceDestination | ControllerDestination; diff --git a/src/types/gameConfig.ts b/src/types/gameConfig.ts index b8c4878..ecb00e8 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: 15, - upgrader: 0, + harvester: 5, + upgrader: 3, builder: 0 } }; diff --git a/src/utils/funcs/get_by_id.ts b/src/utils/funcs/get_by_id.ts index c01ce98..a14a7ee 100644 --- a/src/utils/funcs/get_by_id.ts +++ b/src/utils/funcs/get_by_id.ts @@ -28,3 +28,19 @@ export const getSpawnById = (spawnId: string): StructureSpawn | null => { return spawn; } + + +export const getControllerById = (controllerId: string): StructureController | null => { + if (!controllerId) { + console.log("getControllerById called with an empty or undefined controllerId."); + return null; + } + + const controller = Game.getObjectById(controllerId); + if (!controller) { + console.log(`No controller found with ID: ${controllerId}`); + return null; + } + + return controller; +}