diff --git a/src/creep/builder.ts b/src/creep/builder.ts index 9fc8c58..21d2744 100644 --- a/src/creep/builder.ts +++ b/src/creep/builder.ts @@ -53,16 +53,31 @@ function build(creep: Creep): boolean { } function repair(creep: Creep) { - const targets = creep.room.find(FIND_STRUCTURES, { - filter: (t) => t.hits < t.hitsMax - }); - if (targets.length === 0) { + const target = findRepairTarget(creep); + if (!target) { return false; } - targets.sort((t1, t2) => t1.hits - t2.hits); - if (creep.repair(targets[0]) == ERR_NOT_IN_RANGE) { + if (creep.repair(target) == ERR_NOT_IN_RANGE) { creep.say('🚧 repair'); - creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } }); + creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } }); } return true; } + +function findRepairTarget(creep: Creep): Structure | undefined { + const nearest = creep.pos.findInRange(FIND_STRUCTURES, 3, { + filter: (t) => t.hits < t.hitsMax && t.structureType !== STRUCTURE_ROAD + }); + if (nearest.length > 0) { + nearest.sort((t1, t2) => t1.hits - t2.hits); + return nearest[0]; + } + const targets = creep.room.find(FIND_STRUCTURES, { + filter: (t) => t.hits < t.hitsMax && t.structureType !== STRUCTURE_ROAD + }); + if (targets.length > 0) { + targets.sort((t1, t2) => t1.hits - t2.hits); + return targets[0]; + } + return undefined; +} diff --git a/src/creep/harvester.ts b/src/creep/harvester.ts index dc2cd26..c7aa4f1 100644 --- a/src/creep/harvester.ts +++ b/src/creep/harvester.ts @@ -1,6 +1,6 @@ import { selectSource } from './common'; -const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_SPAWN]; +const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_SPAWN, STRUCTURE_TOWER]; enum Action { Harvest = 'harvest', @@ -36,7 +36,7 @@ export function runAsHarvester(creep: Creep) { const source = Game.getObjectById(memory.sourceId); if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) { creep.say('🔄 harvest'); - creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } }); + creep.moveTo(source, { reusePath: 2, visualizePathStyle: { stroke: '#ffaa00' } }); } break; case Action.Charge: @@ -52,13 +52,13 @@ export function runAsHarvester(creep: Creep) { function findTarget(creep: Creep): Structure | undefined { for (let type of TARGET_TYPES) { - const targets = creep.room.find(FIND_STRUCTURES, { + const target = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (structure) => { return structure.structureType === type && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } }); - if (targets.length) { - return targets[0]; + if (target) { + return target; } } return undefined; @@ -67,7 +67,7 @@ function findTarget(creep: Creep): Structure | undefined { function moveEnergyToTarget(creep: Creep, target: Structure) { if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { creep.say('🚛 transfer'); - creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } }); + creep.moveTo(target, { reusePath: 2, visualizePathStyle: { stroke: '#ffffff' } }); } } diff --git a/src/main.ts b/src/main.ts index cad072b..c95f605 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,9 +53,10 @@ function calcBodyCost(body: Array): number { function callHarvestersFromOthers(minHarvCount: number) { const harvesters = Object.values(Game.creeps).filter((c) => c.memory.role === CreepRole.HARVESTER); - if (harvesters.length < minHarvCount) { + const curHarvCount = harvesters.length; + if (curHarvCount < minHarvCount) { const others = Object.values(Game.creeps).filter((c) => c.memory.role !== CreepRole.HARVESTER); - const required = Math.min(minHarvCount, others.length); + const required = Math.min(minHarvCount - curHarvCount, others.length); console.log('Call harvesters', required); let count = 0; for (let creep of others) { @@ -72,7 +73,7 @@ function callHarvestersFromOthers(minHarvCount: number) { // 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(''); - console.log(`Current game tick is ${Game.time}`); + console.log(`Current game tick is ${Game.time}, ${Game.cpu.bucket}`); const room = _.first(Object.values(Game.rooms)); if (room) { @@ -86,7 +87,7 @@ export const loop = ErrorMapper.wrapLoop(() => { } } - callHarvestersFromOthers(2); + callHarvestersFromOthers(4); makeCreep(CreepRole.HARVESTER, 4); makeCreep(CreepRole.UPGRADER, 4); @@ -135,6 +136,11 @@ function runTower(tower: StructureTower) { return; } + // Energy only for attack and heal + if (tower.store[RESOURCE_ENERGY] < 500) { + return; + } + const roads = room.find(FIND_STRUCTURES, { filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax }); @@ -146,4 +152,16 @@ function runTower(tower: StructureTower) { console.log('Repair', res); return; } + + const structures = room.find(FIND_STRUCTURES, { + filter: (s) => s.hits < s.hitsMax + }); + if (structures.length > 0) { + structures.sort((r1, r2) => r1.hits - r2.hits); + let structure = _.first(structures); + console.log('Broken structure', structure.hits); + const res = tower.repair(structure); + console.log('Repair', res); + return; + } }