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