feat: better code formatting
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import { getSourceById, getSpawnById } from "utils/funcs/getById";
|
||||
import { RoleHandler } from "./BaseHandler.interface";
|
||||
import { SourceDestination } from "types/creeps";
|
||||
import { getNextEmptySpot, getPositionWithDelta, PositionSpotStatus, setSpotStatus } from "utils/positions";
|
||||
import { RoleHandler } from './BaseHandler.interface';
|
||||
import { SourceDestination } from 'types/creeps';
|
||||
import { getSourceById, getSpawnById } from 'utils/funcs/getById';
|
||||
import {
|
||||
getNextEmptySpot,
|
||||
getPositionWithDelta,
|
||||
PositionSpotStatus,
|
||||
setSpotStatus,
|
||||
} from 'utils/positions';
|
||||
|
||||
class HarvesterHandler extends RoleHandler {
|
||||
public static destroy(creepMemory: CreepMemory, state: GameState): void {
|
||||
if (creepMemory.destination?.type === "source") {
|
||||
if (creepMemory.destination?.type === 'source') {
|
||||
this.releaseSourceSpot(creepMemory.destination, state);
|
||||
delete creepMemory.destination; // Clear destination after releasing the spot
|
||||
}
|
||||
if (creepMemory.previousDestination?.type === "source") {
|
||||
if (creepMemory.previousDestination?.type === 'source') {
|
||||
this.releaseSourceSpot(creepMemory.previousDestination, state);
|
||||
delete creepMemory.previousDestination; // Clear previous destination after releasing the spot
|
||||
}
|
||||
@@ -19,10 +24,10 @@ class HarvesterHandler extends RoleHandler {
|
||||
this.validateCreepMemory(creep, state);
|
||||
|
||||
switch (creep.memory.destination?.type) {
|
||||
case "spawn":
|
||||
case 'spawn':
|
||||
this.onSpawnDestination(creep, state);
|
||||
break;
|
||||
case "source":
|
||||
case 'source':
|
||||
this.onSourceDestination(creep, state);
|
||||
break;
|
||||
default:
|
||||
@@ -34,7 +39,7 @@ class HarvesterHandler extends RoleHandler {
|
||||
}
|
||||
|
||||
private static validateCreepMemory(creep: Creep, state: GameState) {
|
||||
if (creep.memory.previousDestination?.type === "source") {
|
||||
if (creep.memory.previousDestination?.type === 'source') {
|
||||
setSpotStatus(
|
||||
state.sourcesStates[creep.memory.previousDestination.id].spots,
|
||||
creep.memory.previousDestination.sourceSpot,
|
||||
@@ -47,16 +52,22 @@ class HarvesterHandler extends RoleHandler {
|
||||
return; // No destination set, nothing to validate
|
||||
}
|
||||
|
||||
if (creep.memory.destination.type === "source" && !creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
|
||||
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"
|
||||
type: 'spawn',
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
if (creep.memory.destination.type === "spawn" && creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) {
|
||||
if (
|
||||
creep.memory.destination.type === 'spawn' &&
|
||||
creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0
|
||||
) {
|
||||
delete creep.memory.destination; // Clear destination if no energy is available
|
||||
return;
|
||||
}
|
||||
@@ -78,11 +89,15 @@ class HarvesterHandler extends RoleHandler {
|
||||
continue; // No empty spots available, skip to next source
|
||||
}
|
||||
|
||||
setSpotStatus(sourceState.spots, emptySpot, PositionSpotStatus.OCCUPIED);
|
||||
setSpotStatus(
|
||||
sourceState.spots,
|
||||
emptySpot,
|
||||
PositionSpotStatus.OCCUPIED
|
||||
);
|
||||
creep.memory.destination = {
|
||||
id: source.id,
|
||||
type: "source",
|
||||
sourceSpot: emptySpot
|
||||
type: 'source',
|
||||
sourceSpot: emptySpot,
|
||||
};
|
||||
return;
|
||||
}
|
||||
@@ -91,7 +106,10 @@ class HarvesterHandler extends RoleHandler {
|
||||
}
|
||||
|
||||
private static onSourceDestination(creep: Creep, _state: GameState) {
|
||||
if (!creep.memory.destination || creep.memory.destination.type !== "source") {
|
||||
if (
|
||||
!creep.memory.destination ||
|
||||
creep.memory.destination.type !== 'source'
|
||||
) {
|
||||
console.log(`Creep ${creep.name} has no valid destination set.`);
|
||||
delete creep.memory.destination;
|
||||
return;
|
||||
@@ -104,10 +122,17 @@ class HarvesterHandler extends RoleHandler {
|
||||
}
|
||||
|
||||
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
|
||||
const sourceSpotPosition = getPositionWithDelta(source.pos, creep.memory.destination.sourceSpot);
|
||||
const sourceSpotPosition = getPositionWithDelta(
|
||||
source.pos,
|
||||
creep.memory.destination.sourceSpot
|
||||
);
|
||||
creep.moveTo(sourceSpotPosition, {
|
||||
reusePath: 10,
|
||||
visualizePathStyle: { stroke: "#ffffff", lineStyle: "dashed", strokeWidth: 0.1 }
|
||||
visualizePathStyle: {
|
||||
stroke: '#ffffff',
|
||||
lineStyle: 'dashed',
|
||||
strokeWidth: 0.1,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -116,7 +141,7 @@ class HarvesterHandler extends RoleHandler {
|
||||
if (creep.memory.destination === undefined) {
|
||||
creep.memory.destination = {
|
||||
id: creep.memory.spawnId,
|
||||
type: "spawn"
|
||||
type: 'spawn',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -129,22 +154,29 @@ class HarvesterHandler extends RoleHandler {
|
||||
if (creep.transfer(spawn, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(spawn, {
|
||||
reusePath: 10,
|
||||
visualizePathStyle: { stroke: "#ffffff", lineStyle: "dashed", strokeWidth: 0.1 }
|
||||
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)
|
||||
.map((sourceId) => getSourceById(sourceId))
|
||||
.filter((source) => source !== null)
|
||||
.sort((a, b) => creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b));
|
||||
|
||||
return sources as Source[];
|
||||
}
|
||||
|
||||
private static releaseSourceSpot(destination: SourceDestination, state: GameState) {
|
||||
if (!destination || destination.type !== "source") {
|
||||
private static releaseSourceSpot(
|
||||
destination: SourceDestination,
|
||||
state: GameState
|
||||
) {
|
||||
if (!destination || destination.type !== 'source') {
|
||||
return; // Not a source destination, nothing to release
|
||||
}
|
||||
|
||||
@@ -154,7 +186,11 @@ class HarvesterHandler extends RoleHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
setSpotStatus(sourceState.spots, destination.sourceSpot, PositionSpotStatus.EMPTY);
|
||||
setSpotStatus(
|
||||
sourceState.spots,
|
||||
destination.sourceSpot,
|
||||
PositionSpotStatus.EMPTY
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user