Refactors SpawnHandler to RoomRunner
This commit is contained in:
@@ -4,15 +4,9 @@ import { createSourcePositionMatrix, forEachMatrixSpot, getPositionWithDelta, se
|
|||||||
import { checkPositionWalkable } from "utils/funcs/check_position";
|
import { checkPositionWalkable } from "utils/funcs/check_position";
|
||||||
import { get_role_const as get_role_cost } from "utils/funcs/get_role_const";
|
import { get_role_const as get_role_cost } from "utils/funcs/get_role_const";
|
||||||
|
|
||||||
class SpawnHandler {
|
class RoomRunner {
|
||||||
constructor(private spawn: StructureSpawn) {}
|
public static run(room: Room, state: GameState): GameState {
|
||||||
|
this.updateSpawnState(room, state);
|
||||||
public get spawnName(): string {
|
|
||||||
return this.spawn.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public run(state: GameState): GameState {
|
|
||||||
this.updateSpawnState(state);
|
|
||||||
|
|
||||||
for(const name in Memory.creeps) {
|
for(const name in Memory.creeps) {
|
||||||
if (!Game.creeps[name]) {
|
if (!Game.creeps[name]) {
|
||||||
@@ -35,13 +29,15 @@ class SpawnHandler {
|
|||||||
state = roleDefinition.handler.run(creep, state);
|
state = roleDefinition.handler.run(creep, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.validateSpawnState();
|
for (const spawn of room.find(FIND_MY_SPAWNS)) {
|
||||||
|
this.validateSpawnState(spawn, state);
|
||||||
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateSpawnState(state: GameState) {
|
private static updateSpawnState(room: Room, state: GameState) {
|
||||||
const sources = this.spawn.room.find(FIND_SOURCES);
|
const sources = room.find(FIND_SOURCES);
|
||||||
if (!state.sourcesStates) {
|
if (!state.sourcesStates) {
|
||||||
state.sourcesStates = {};
|
state.sourcesStates = {};
|
||||||
state.maxHarvesters = 0
|
state.maxHarvesters = 0
|
||||||
@@ -71,8 +67,8 @@ class SpawnHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private validateSpawnState() {
|
private static validateSpawnState(spawn: StructureSpawn, state: GameState) {
|
||||||
if (this.spawn.spawning) {
|
if (spawn.spawning) {
|
||||||
// console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`);
|
// console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -84,38 +80,36 @@ class SpawnHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const totalCreeps = Object.values(Game.creeps).length;
|
const totalCreeps = Object.values(Game.creeps).length;
|
||||||
if (totalCreeps >= DEFAULT_GAME_CONFIG.maxCreeps) {
|
if (totalCreeps >= state.maxHarvesters) {
|
||||||
// console.log(`Spawn ${this.spawn.name} cannot spawn more creeps, limit reached.`);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const rolesToSpawn = this.sortCreepRolesByPriority(creepRequisition);
|
const rolesToSpawn = this.sortCreepRolesByPriority(creepRequisition);
|
||||||
|
|
||||||
for (const role of rolesToSpawn) {
|
for (const role of rolesToSpawn) {
|
||||||
if (this.spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) {
|
if (spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) {
|
||||||
// console.log(`Spawn ${this.spawn.name} does not have enough energy to spawn a ${role.name}.`);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newName = `${role.name}_${Game.time}`;
|
const newName = `${role.name}_${Game.time}`;
|
||||||
const spawnResult = this.spawn.spawnCreep(role.body, newName, {
|
const spawnResult = spawn.spawnCreep(role.body, newName, {
|
||||||
memory: {
|
memory: {
|
||||||
role: role.name,
|
role: role.name,
|
||||||
room: this.spawn.room.name,
|
room: spawn.room.name,
|
||||||
spawnId: this.spawn.id,
|
spawnId: spawn.id,
|
||||||
working: false
|
working: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (spawnResult === OK) {
|
if (spawnResult === OK) {
|
||||||
console.log(`Spawn ${this.spawn.name} successfully spawned a new ${role.name}: ${newName}.`);
|
console.log(`Spawn ${spawn.name} successfully spawned a new ${role.name}: ${newName}.`);
|
||||||
return; // Exit after spawning one creep
|
return; // Exit after spawning one creep
|
||||||
} else {
|
} else {
|
||||||
console.error(`Spawn ${this.spawn.name} failed to spawn a new ${role.name}: ${spawnResult}`);
|
console.error(`Spawn ${spawn.name} failed to spawn a new ${role.name}: ${spawnResult}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private checksNeedsCreeps(): CreepRequisition {
|
private static checksNeedsCreeps(): CreepRequisition {
|
||||||
const creepCounts: Record<string, number> = {};
|
const creepCounts: Record<string, number> = {};
|
||||||
for (const creep of Object.values(Game.creeps)) {
|
for (const creep of Object.values(Game.creeps)) {
|
||||||
const role = creep.memory.role;
|
const role = creep.memory.role;
|
||||||
@@ -143,7 +137,7 @@ class SpawnHandler {
|
|||||||
return requisition;
|
return requisition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private sortCreepRolesByPriority(requisition: CreepRequisition): RoleDefinition[] {
|
private static sortCreepRolesByPriority(requisition: CreepRequisition): RoleDefinition[] {
|
||||||
return Object.keys(requisition)
|
return Object.keys(requisition)
|
||||||
.filter(role => requisition[role as CreepRole] > 0)
|
.filter(role => requisition[role as CreepRole] > 0)
|
||||||
.map(role => CreepRoles[role as CreepRole])
|
.map(role => CreepRoles[role as CreepRole])
|
||||||
@@ -151,4 +145,4 @@ class SpawnHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SpawnHandler;
|
export default RoomRunner;
|
||||||
16
src/main.ts
16
src/main.ts
@@ -1,6 +1,6 @@
|
|||||||
import { CreepDestination } from "types/creeps";
|
import { CreepDestination } from "types/creeps";
|
||||||
import { SourcePositionMatrix, SourceSpotStatus } from "./types/source";
|
import { SourcePositionMatrix, SourceSpotStatus } from "./types/source";
|
||||||
import SpawnHandler from "spawnHandler";
|
import RoomRunner from "RoomRunner";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
/*
|
/*
|
||||||
@@ -26,7 +26,7 @@ declare global {
|
|||||||
interface Memory {
|
interface Memory {
|
||||||
uuid: number;
|
uuid: number;
|
||||||
log: any;
|
log: any;
|
||||||
spawnStates: { [name: string]: GameState };
|
roomStateRegistry: { [name: string]: GameState };
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CreepMemory {
|
interface CreepMemory {
|
||||||
@@ -48,13 +48,11 @@ declare global {
|
|||||||
|
|
||||||
|
|
||||||
export const loop = () => {
|
export const loop = () => {
|
||||||
Memory.spawnStates = Memory.spawnStates || {};
|
Memory.roomStateRegistry = Memory.roomStateRegistry || {};
|
||||||
|
|
||||||
for (const spawnName of Object.keys(Game.spawns)) {
|
for (const roomName of Object.keys(Game.rooms)) {
|
||||||
const spawnState = Memory.spawnStates[spawnName] || {};
|
Memory.roomStateRegistry[roomName] = RoomRunner.run(
|
||||||
|
Game.rooms[roomName], Memory.roomStateRegistry[roomName] || {}
|
||||||
const spawnHandler = new SpawnHandler(Game.spawns[spawnName]);
|
);
|
||||||
|
|
||||||
Memory.spawnStates[spawnName] = spawnHandler.run(spawnState);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES6",
|
"target": "ES6",
|
||||||
"lib": ["ES2015", "DOM"],
|
"lib": ["ES2017", "DOM"],
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
|
|||||||
Reference in New Issue
Block a user