diff --git a/src/builder.ts b/src/builder.ts index 65d23a9..a8aa13d 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -1,33 +1,49 @@ +interface BuilderMemory extends CreepMemory { + building: boolean | undefined; + sourceNum: number | undefined; +} + +function randomIntInRange(min: number, max: number): number { + const delta = max - min; + return Math.floor(Math.random() * delta) + min; +} + export function runAsBuilder(creep: Creep) { - const memory = creep.memory as CreepMemory & { building: boolean | undefined }; + const memory = creep.memory as BuilderMemory; if (memory.building && creep.store[RESOURCE_ENERGY] === 0) { memory.building = false; - creep.say('🔄 harvest'); } if (!memory.building && creep.store.getFreeCapacity() === 0) { memory.building = true; - creep.say('🚧 build'); + } + + if (memory.sourceNum === undefined) { + const sources = creep.room.find(FIND_SOURCES); + memory.sourceNum = randomIntInRange(0, sources.length); } 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.say('To spawn'); creep.moveTo(spawns[0]); } } } else { const sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); + const sourceNum = memory.sourceNum; + if (creep.harvest(sources[sourceNum]) == ERR_NOT_IN_RANGE) { + creep.say('🔄 harvest ' + sourceNum); + creep.moveTo(sources[sourceNum], { visualizePathStyle: { stroke: '#ffaa00' } }); } } } diff --git a/src/main.ts b/src/main.ts index 307b6b4..9beede6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,7 +39,7 @@ export const loop = ErrorMapper.wrapLoop(() => { makeCreep(CreepRole.HARVESTER, 1); makeCreep(CreepRole.UPGRADER, 3); - makeCreep(CreepRole.BUILDER, 2); + makeCreep(CreepRole.BUILDER, 4); // Process current creeps for (let name in Game.creeps) {