Add repair tasks to builder

This commit is contained in:
Anton Vakhrushev 2020-07-21 11:36:14 +03:00
parent 7214f6d408
commit 950681a0ed
2 changed files with 37 additions and 15 deletions

View File

@ -22,18 +22,16 @@ export function runAsBuilder(creep: Creep) {
}
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.moveTo(spawns[0]);
}
if (build(creep)) {
return;
}
if (repair(creep)) {
return;
}
const spawns = creep.room.find(FIND_MY_SPAWNS);
if (spawns.length > 0) {
creep.say('To spawn');
creep.moveTo(spawns[0]);
}
} else {
const sources = creep.room.find(FIND_SOURCES);
@ -44,3 +42,27 @@ export function runAsBuilder(creep: Creep) {
}
}
}
function build(creep: Creep): boolean {
const targets = creep.room.find(FIND_MY_CONSTRUCTION_SITES);
if (targets.length === 0) {
return false;
}
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
creep.say('🚧 build');
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
}
return true;
}
function repair(creep: Creep) {
const targets = creep.room.find(FIND_STRUCTURES).filter((t) => t.hits < t.hitsMax);
if (targets.length === 0) {
return false;
}
if (creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
creep.say('🚧 repair');
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
}
return true;
}

View File

@ -16,20 +16,20 @@ enum CreepRole {
function makeCreep(role: CreepRole, count: number) {
const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role);
console.log('Make creep ', role, 'need', count, 'has', creeps.length);
console.log(`Make creep "${role}"`, 'need', count, 'has', creeps.length);
if (creeps.length < count) {
const firstSpawn = _.first(Object.values(Game.spawns));
const name = makeName(role);
const memory = { role: role } as CreepMemory;
const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory });
console.log(`Make creep "${role}" err`, err);
console.log(`Make creep "${role}"`, 'err', err);
}
}
// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change
// 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(`Current game tick is ${Game.time}`);
console.log(`\nCurrent game tick is ${Game.time}`);
// Automatically delete memory of missing creeps
for (const name in Memory.creeps) {