diff --git a/src/creep/builder.ts b/src/creep/builder.ts index 84ba569..74498ca 100644 --- a/src/creep/builder.ts +++ b/src/creep/builder.ts @@ -22,18 +22,16 @@ export function runAsBuilder(creep: Creep) { } if (memory.building) { - const targets = creep.room.find(FIND_CONSTRUCTION_SITES); - if (targets.length > 0) { - if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) { - creep.say('🚧 build'); - creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } }); - } - } else { - const spawns = creep.room.find(FIND_MY_SPAWNS); - if (spawns.length > 0) { - creep.say('To spawn'); - creep.moveTo(spawns[0]); - } + if (build(creep)) { + return; + } + if (repair(creep)) { + return; + } + const spawns = creep.room.find(FIND_MY_SPAWNS); + if (spawns.length > 0) { + creep.say('To spawn'); + creep.moveTo(spawns[0]); } } else { const sources = creep.room.find(FIND_SOURCES); @@ -44,3 +42,27 @@ export function runAsBuilder(creep: Creep) { } } } + +function build(creep: Creep): boolean { + const targets = creep.room.find(FIND_MY_CONSTRUCTION_SITES); + if (targets.length === 0) { + return false; + } + if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) { + creep.say('🚧 build'); + creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } }); + } + return true; +} + +function repair(creep: Creep) { + const targets = creep.room.find(FIND_STRUCTURES).filter((t) => t.hits < t.hitsMax); + if (targets.length === 0) { + return false; + } + if (creep.repair(targets[0]) == ERR_NOT_IN_RANGE) { + creep.say('🚧 repair'); + creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } }); + } + return true; +} diff --git a/src/main.ts b/src/main.ts index 6fd7beb..301c3b0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,20 +16,20 @@ enum CreepRole { function makeCreep(role: CreepRole, count: number) { const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role); - console.log('Make creep ', role, 'need', count, 'has', creeps.length); + console.log(`Make creep "${role}"`, 'need', count, 'has', creeps.length); if (creeps.length < count) { const firstSpawn = _.first(Object.values(Game.spawns)); const name = makeName(role); const memory = { role: role } as CreepMemory; const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory }); - console.log(`Make creep "${role}" err`, err); + console.log(`Make creep "${role}"`, 'err', err); } } // When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change // This utility uses source maps to get the line numbers and file names of the original, TS source code export const loop = ErrorMapper.wrapLoop(() => { - console.log(`Current game tick is ${Game.time}`); + console.log(`\nCurrent game tick is ${Game.time}`); // Automatically delete memory of missing creeps for (const name in Memory.creeps) {