Better Upgrader Handler
This commit is contained in:
@@ -6,6 +6,8 @@ import { getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus
|
|||||||
|
|
||||||
class UpgraderHandler extends RoleHandler {
|
class UpgraderHandler 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 "controller":
|
case "controller":
|
||||||
this.onControllerDestination(creep, state);
|
this.onControllerDestination(creep, state);
|
||||||
@@ -21,6 +23,42 @@ class UpgraderHandler 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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (creep.memory.destination.type === "controller" && 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,21 +98,6 @@ class UpgraderHandler extends RoleHandler {
|
|||||||
return;
|
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);
|
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}`);
|
||||||
@@ -90,14 +113,6 @@ class UpgraderHandler extends RoleHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static onControllerDestination(creep: Creep, state: GameState) {
|
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.memory.destination === undefined) {
|
||||||
if (!creep.room.controller) {
|
if (!creep.room.controller) {
|
||||||
console.log(`Creep ${creep.name} has no valid controller to upgrade.`);
|
console.log(`Creep ${creep.name} has no valid controller to upgrade.`);
|
||||||
@@ -110,11 +125,6 @@ class UpgraderHandler extends RoleHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) {
|
|
||||||
delete creep.memory.destination;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const controller = getControllerById(creep.memory.destination.id);
|
const controller = getControllerById(creep.memory.destination.id);
|
||||||
if (!controller) {
|
if (!controller) {
|
||||||
console.log(`Spawn not found for creep: ${creep.name}`);
|
console.log(`Spawn not found for creep: ${creep.name}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user