Better Harvester Handler

This commit is contained in:
2025-07-12 11:15:20 -03:00
parent 23d9739167
commit 7bee855002
3 changed files with 44 additions and 25 deletions

View File

@@ -6,6 +6,8 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus
class HarvesterHandler extends RoleHandler { class HarvesterHandler extends RoleHandler {
public static run(creep: Creep, state: GameState): GameState { public static run(creep: Creep, state: GameState): GameState {
this.validateCreepMemory(creep, state);
switch (creep.memory.destination?.type) { switch (creep.memory.destination?.type) {
case "spawn": case "spawn":
this.onSpawnDestination(creep, state); this.onSpawnDestination(creep, state);
@@ -21,6 +23,35 @@ class HarvesterHandler extends RoleHandler {
return state; 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) { private static onFindNewSource(creep: Creep, state: GameState) {
if (creep.memory.destination) { if (creep.memory.destination) {
console.log(`Creep ${creep.name} already has a destination set.`); console.log(`Creep ${creep.name} already has a destination set.`);
@@ -60,15 +91,6 @@ class HarvesterHandler extends RoleHandler {
return; 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); const source = getSourceById(creep.memory.destination.id);
if (source === null) { if (source === null) {
console.log(`Source not found for creep: ${creep.name}`); console.log(`Source not found for creep: ${creep.name}`);
@@ -84,14 +106,6 @@ class HarvesterHandler extends RoleHandler {
} }
private static onSpawnDestination(creep: Creep, state: GameState) { 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) { if (creep.memory.destination === undefined) {
creep.memory.destination = { creep.memory.destination = {
id: creep.memory.spawnId, 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); const spawn = getSpawnById(creep.memory.destination.id);
if (!spawn) { if (!spawn) {
console.log(`Spawn not found for creep: ${creep.name}`); console.log(`Spawn not found for creep: ${creep.name}`);

View File

@@ -52,6 +52,16 @@ export type ControllerDestination = {
type: "controller"; 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
);

View File

@@ -24,8 +24,8 @@ export type GameConfig = {
export const DEFAULT_GAME_CONFIG: GameConfig = { export const DEFAULT_GAME_CONFIG: GameConfig = {
maxCreeps: 15, maxCreeps: 15,
minCreepsPerRole: { minCreepsPerRole: {
harvester: 5, harvester: 2,
upgrader: 3, upgrader: 5,
builder: 0 builder: 0
} }
}; };