Initial Working Creep Handler

This commit is contained in:
2025-07-06 16:00:36 -03:00
parent 2a7b74542c
commit bb3f797fc3
8 changed files with 41 additions and 117 deletions

View File

@@ -1,5 +1,4 @@
import SpawnHandler from "spawnHandler"; import SpawnHandler from "spawnHandler";
import SpawnStorage from "spawnStorage";
declare global { declare global {
/* /*
@@ -33,22 +32,8 @@ declare global {
export const loop = () => { export const loop = () => {
const spawnStorage = new SpawnStorage(); for (const spawnName of Object.keys(Game.spawns)) {
Memory.spawnHandlers = Memory.spawnHandlers || {};
// Check if spawn still exists
const activeSpawns = Object.keys(Game.spawns);
spawnStorage.clearDeadHandlers(activeSpawns);
for (const spawnName of activeSpawns) {
// Create a handler for each spawn
var currentHandler = spawnStorage.getHandler(spawnName);
if (!currentHandler) {
currentHandler = spawnStorage.addHandler(spawnName, new SpawnHandler(Game.spawns[spawnName]));
}
// Run the handler // Run the handler
currentHandler.run(); new SpawnHandler(Game.spawns[spawnName]).run();
} }
}; };

View File

@@ -1,12 +1,10 @@
import { CreepRole } from "types/creeps"; import { RoleHandler } from "./roleHandler.interface";
import { registerRoleHandler, RoleHandler } from "./roleHandler.interface";
class HarvesterHandler implements RoleHandler { class HarvesterHandler extends RoleHandler {
public readonly mappedRole: CreepRole = "harvester"; public static run(creep: Creep): void {
console.log(`Running HarvesterHandler for creep: ${creep.name}`);
public run(creep: Creep): void {
if (creep.store.getFreeCapacity() > 0) { if (creep.store.getFreeCapacity() > 0) {
const sources = creep.room.find(FIND_SOURCES); const sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) { if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
@@ -22,7 +20,6 @@ class HarvesterHandler implements RoleHandler {
} }
registerRoleHandler("harvester", HarvesterHandler);

View File

@@ -0,0 +1,8 @@
import { RoleHandler } from "./roleHandler.interface";
import HarvesterHandler from "./harvesterHandler";
export { RoleHandler, HarvesterHandler };

View File

@@ -1,19 +1,3 @@
import { CreepRole } from "types/creeps"; export abstract class RoleHandler {
static run(creep: Creep): void {};
export interface RoleHandler {
mappedRole: CreepRole;
run(creep: Creep): void;
}
export const ImplementationRegistry: { [roleName: string]: RoleHandler } = {};
export const registerRoleHandler = (roleName: string, handler: typeof RoleHandler) => {
if (ImplementationRegistry[roleName]) {
console.warn(`Role handler for ${roleName} is already registered. Overwriting.`);
}
ImplementationRegistry[roleName] = handler;
} }

View File

@@ -5,23 +5,23 @@ import { get_role_const as get_role_cost } from "utils/funcs/get_role_const";
class SpawnHandler { class SpawnHandler {
constructor(private spawn: StructureSpawn) {} constructor(private spawn: StructureSpawn) {}
public get spawnName(): string {
return this.spawn.name;
}
public run(): void { public run(): void {
this.validateSpawnState(); this.validateSpawnState();
for(var name in Game.creeps) { for(const name in Game.creeps) {
var creep = Game.creeps[name]; const creep = Game.creeps[name];
if(creep.store.getFreeCapacity() > 0) { const roleDefinition = CreepRoles[creep.memory.role as CreepRole];
var sources = creep.room.find(FIND_SOURCES); if (!roleDefinition) {
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { console.warn(`Creep ${creep.name} has an unknown role: ${creep.memory.role}`);
creep.moveTo(sources[0]); continue;
}
}
else {
if(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1']);
}
} }
roleDefinition.handler.run(creep);
} }
} }
@@ -87,6 +87,10 @@ class SpawnHandler {
} }
const roleType = role as CreepRole; const roleType = role as CreepRole;
requisition[roleType] = DEFAULT_GAME_CONFIG.minCreepsPerRole[roleType] - (creepCounts[role] || 0); requisition[roleType] = DEFAULT_GAME_CONFIG.minCreepsPerRole[roleType] - (creepCounts[role] || 0);
if (requisition[roleType] < 0) {
requisition[roleType] = 0; // Ensure we don't have negative requisitions
}
} }
return requisition; return requisition;

View File

@@ -1,60 +0,0 @@
import SpawnHandler from "spawnHandler";
class SpawnStorage {
constructor() {
// Initialize the spawn storage
if (!Memory.spawnHandlers) {
Memory.spawnHandlers = {};
}
}
get allHandlers(): { [name: string]: SpawnHandler } {
const handlers: { [name: string]: SpawnHandler } = {};
for (const spawnName in Memory.spawnHandlers) {
const instance = this.getHandler(spawnName);
if (!instance) {
console.log(`No handler found for spawn: ${spawnName}`);
continue;
}
handlers[spawnName] = instance;
}
return handlers;
}
// Method to get a spawn handler by name
getHandler(spawnName: string): SpawnHandler | null {
if (!Memory.spawnHandlers[spawnName]) {
console.log(`No handler found for spawn: ${spawnName}`);
return null;
}
const data = JSON.parse(Memory.spawnHandlers[spawnName]) as SpawnHandler;
return Object.assign(new SpawnHandler(Game.spawns[spawnName]), data);
}
// Method to add or update a spawn handler
addHandler(spawnName: string, handler: SpawnHandler): SpawnHandler {
Memory.spawnHandlers[spawnName] = JSON.stringify(handler);
return handler;
}
// Method to remove a spawn handler
removeHandler(spawnName: string) {
delete Memory.spawnHandlers[spawnName];
}
checkHandlerExists(spawnName: string): boolean {
return !!Memory.spawnHandlers[spawnName];
}
clearDeadHandlers(activeSpawns: string[]) {
for (const spawnName of Object.keys(this.allHandlers)) {
if (!(spawnName in activeSpawns)) {
continue;
}
this.removeHandler(spawnName);
}
}
}
export default SpawnStorage;

View File

@@ -1,6 +1,9 @@
import { HarvesterHandler, RoleHandler } from "roleHandlers";
export type RoleDefinition = { export type RoleDefinition = {
name: string; name: string;
body: BodyPartConstant[]; body: BodyPartConstant[];
handler: RoleHandler;
priority: number; priority: number;
}; };
@@ -8,16 +11,19 @@ export const CreepRoles = {
harvester: { harvester: {
name: "harvester", name: "harvester",
body: [WORK, CARRY, MOVE], body: [WORK, CARRY, MOVE],
handler: HarvesterHandler,
priority: 1 priority: 1
}, },
upgrader: { upgrader: {
name: "upgrader", name: "upgrader",
body: [WORK, CARRY, MOVE], body: [WORK, CARRY, MOVE],
handler: HarvesterHandler,
priority: 2 priority: 2
}, },
builder: { builder: {
name: "builder", name: "builder",
body: [WORK, CARRY, MOVE], body: [WORK, CARRY, MOVE],
handler: HarvesterHandler,
priority: 3 priority: 3
} }
} satisfies Record<string, RoleDefinition>; } satisfies Record<string, RoleDefinition>;

View File

@@ -22,10 +22,10 @@ export type GameConfig = {
* @type {GameConfig} * @type {GameConfig}
*/ */
export const DEFAULT_GAME_CONFIG: GameConfig = { export const DEFAULT_GAME_CONFIG: GameConfig = {
maxCreeps: 50, maxCreeps: 15,
minCreepsPerRole: { minCreepsPerRole: {
harvester: 5, harvester: 5,
upgrader: 5, upgrader: 0,
builder: 5 builder: 0
} }
}; };