Add tower module

This commit is contained in:
Anton Vakhrushev 2020-07-23 18:36:24 +03:00
parent 8979e42150
commit 29f653f77a
2 changed files with 48 additions and 46 deletions

View File

@ -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;
}
}

47
src/tower.ts Normal file
View File

@ -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;
}
}