Some fixes

This commit is contained in:
Anton Vakhrushev 2020-07-22 21:07:29 +03:00
parent 3811e4fc56
commit ea5ff7a63a
3 changed files with 50 additions and 17 deletions

View File

@ -53,16 +53,31 @@ function build(creep: Creep): boolean {
} }
function repair(creep: Creep) { function repair(creep: Creep) {
const targets = creep.room.find(FIND_STRUCTURES, { const target = findRepairTarget(creep);
filter: (t) => t.hits < t.hitsMax if (!target) {
});
if (targets.length === 0) {
return false; return false;
} }
targets.sort((t1, t2) => t1.hits - t2.hits); if (creep.repair(target) == ERR_NOT_IN_RANGE) {
if (creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
creep.say('🚧 repair'); creep.say('🚧 repair');
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } }); creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } });
} }
return true; 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;
}

View File

@ -1,6 +1,6 @@
import { selectSource } from './common'; import { selectSource } from './common';
const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_SPAWN]; const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_SPAWN, STRUCTURE_TOWER];
enum Action { enum Action {
Harvest = 'harvest', Harvest = 'harvest',
@ -36,7 +36,7 @@ export function runAsHarvester(creep: Creep) {
const source = Game.getObjectById(memory.sourceId); const source = Game.getObjectById(memory.sourceId);
if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) { if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.say('🔄 harvest'); creep.say('🔄 harvest');
creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } }); creep.moveTo(source, { reusePath: 2, visualizePathStyle: { stroke: '#ffaa00' } });
} }
break; break;
case Action.Charge: case Action.Charge:
@ -52,13 +52,13 @@ export function runAsHarvester(creep: Creep) {
function findTarget(creep: Creep): Structure | undefined { function findTarget(creep: Creep): Structure | undefined {
for (let type of TARGET_TYPES) { for (let type of TARGET_TYPES) {
const targets = creep.room.find(FIND_STRUCTURES, { const target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return structure.structureType === type && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; return structure.structureType === type && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
} }
}); });
if (targets.length) { if (target) {
return targets[0]; return target;
} }
} }
return undefined; return undefined;
@ -67,7 +67,7 @@ function findTarget(creep: Creep): Structure | undefined {
function moveEnergyToTarget(creep: Creep, target: Structure) { function moveEnergyToTarget(creep: Creep, target: Structure) {
if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.say('🚛 transfer'); creep.say('🚛 transfer');
creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } }); creep.moveTo(target, { reusePath: 2, visualizePathStyle: { stroke: '#ffffff' } });
} }
} }

View File

@ -53,9 +53,10 @@ function calcBodyCost(body: Array<BodyPartConstant>): number {
function callHarvestersFromOthers(minHarvCount: number) { function callHarvestersFromOthers(minHarvCount: number) {
const harvesters = Object.values(Game.creeps).filter((c) => c.memory.role === CreepRole.HARVESTER); 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 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); console.log('Call harvesters', required);
let count = 0; let count = 0;
for (let creep of others) { 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 // This utility uses source maps to get the line numbers and file names of the original, TS source code
export const loop = ErrorMapper.wrapLoop(() => { export const loop = ErrorMapper.wrapLoop(() => {
console.log(''); 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)); const room = _.first(Object.values(Game.rooms));
if (room) { if (room) {
@ -86,7 +87,7 @@ export const loop = ErrorMapper.wrapLoop(() => {
} }
} }
callHarvestersFromOthers(2); callHarvestersFromOthers(4);
makeCreep(CreepRole.HARVESTER, 4); makeCreep(CreepRole.HARVESTER, 4);
makeCreep(CreepRole.UPGRADER, 4); makeCreep(CreepRole.UPGRADER, 4);
@ -135,6 +136,11 @@ function runTower(tower: StructureTower) {
return; return;
} }
// Energy only for attack and heal
if (tower.store[RESOURCE_ENERGY] < 500) {
return;
}
const roads = room.find(FIND_STRUCTURES, { const roads = room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax
}); });
@ -146,4 +152,16 @@ function runTower(tower: StructureTower) {
console.log('Repair', res); console.log('Repair', res);
return; 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;
}
} }