Adds Upgraders
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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 };
|
||||
|
||||
145
src/roleHandlers/upgrader.handler.ts
Normal file
145
src/roleHandlers/upgrader.handler.ts
Normal file
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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<StructureController>(controllerId);
|
||||
if (!controller) {
|
||||
console.log(`No controller found with ID: ${controllerId}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user