Fixes Creeps Memory Access per Room

This commit is contained in:
2025-07-13 21:43:43 -03:00
parent 6e8c8ae428
commit 2c93b65b3d

View File

@@ -8,7 +8,7 @@ class RoomRunner {
public static run(room: Room, state: GameState): GameState {
this.updateSpawnState(room, state);
for(const name in Memory.creeps) {
for(const name in this.getRoomCreeps(room)) {
if (!Game.creeps[name]) {
console.log(`Creep ${name} is dead, cleaning up memory.`);
@@ -32,10 +32,19 @@ class RoomRunner {
for (const spawn of room.find(FIND_MY_SPAWNS)) {
this.validateSpawnState(spawn, state);
}
return state;
}
private static getRoomCreeps(room: Room): Record<string, CreepMemory> {
return Object.keys(Memory.creeps)
.filter(name => Memory.creeps[name].room === room.name)
.reduce((creeps: Record<string, CreepMemory>, creepName: string) => {
creeps[creepName] = Memory.creeps[creepName];
return creeps;
}, {});
}
private static updateSpawnState(room: Room, state: GameState) {
const sources = room.find(FIND_SOURCES);
if (!state.sourcesStates) {
@@ -73,7 +82,7 @@ class RoomRunner {
return;
}
const creepRequisition = this.checksNeedsCreeps();
const creepRequisition = this.checksNeedsCreeps(spawn.room);
if (Object.values(creepRequisition).every(count => count <= 0)) {
// console.log(`Spawn ${this.spawn.name} has no creep needs.`);
return;
@@ -109,10 +118,10 @@ class RoomRunner {
}
}
private static checksNeedsCreeps(): CreepRequisition {
private static checksNeedsCreeps(room: Room): CreepRequisition {
const creepCounts: Record<string, number> = {};
for (const creep of Object.values(Game.creeps)) {
const role = creep.memory.role;
for (const creepMemory of Object.values(this.getRoomCreeps(room))) {
const role = creepMemory.role;
creepCounts[role] = (creepCounts[role] || 0) + 1;
}