diff --git a/src/main.ts b/src/main.ts index 432b335..b99177e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { runAsHarvester } from './creep/harvester'; import { runAsBuilder } from './creep/builder'; import { runAsUpgrader } from './creep/upgrader'; import { uniqId } from './utils/Identity'; +import { runTower } from './tower'; function makeName(): string { return uniqId(); @@ -125,49 +126,3 @@ export const loop = ErrorMapper.wrapLoop(() => { } } }); - -function runTower(tower: StructureTower) { - const room = tower.room; - const hostile = room.find(FIND_HOSTILE_CREEPS); - if (hostile.length > 0) { - const res = tower.attack(_.first(hostile)); - console.log('Attack', res); - return; - } - - const creeps = room.find(FIND_MY_CREEPS, { - filter: (c) => c.hits < c.hitsMax - }); - if (creeps.length > 0) { - const res = tower.heal(_.first(creeps)); - console.log('Heal', res); - 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 - }); - if (roads.length > 0) { - roads.sort((r1, r2) => r1.hits - r2.hits); - let road = _.first(roads); - const res = tower.repair(road); - 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); - const res = tower.repair(structure); - console.log('Repair', res); - return; - } -} diff --git a/src/tower.ts b/src/tower.ts new file mode 100644 index 0000000..4e0cdee --- /dev/null +++ b/src/tower.ts @@ -0,0 +1,47 @@ +const REPAIR_ENERGY_LIMIT = 500; + +export function runTower(tower: StructureTower) { + const room = tower.room; + const hostiles = room.find(FIND_HOSTILE_CREEPS); + if (hostiles.length > 0) { + const hostile = _.min(hostiles, (h) => h.hits); + const res = tower.attack(hostile); + console.log('Attack', res); + return; + } + + const creeps = room.find(FIND_MY_CREEPS, { + filter: (c) => c.hits < c.hitsMax + }); + if (creeps.length > 0) { + const creep = _.min(creeps, (c) => c.hits); + const res = tower.heal(creep); + console.log('Heal', res); + return; + } + + // Energy only for attack and heal + if (tower.store[RESOURCE_ENERGY] < REPAIR_ENERGY_LIMIT) { + return; + } + + const roads = room.find(FIND_STRUCTURES, { + filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax + }); + if (roads.length > 0) { + const road = _.min(roads, (r) => r.hits); + const res = tower.repair(road); + console.log('Repair', res); + return; + } + + const structures = room.find(FIND_STRUCTURES, { + filter: (s) => s.hits < s.hitsMax + }); + if (structures.length > 0) { + const structure = _.min(structures, (s) => s.hits); + const res = tower.repair(structure); + console.log('Repair', res); + return; + } +}