40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
var energyUtils = require('../utils/energy');
|
|
var roleBase = require('base');
|
|
|
|
class roleRepairer extends roleBase {
|
|
constructor(){
|
|
super();
|
|
this.name = 'Repairer';
|
|
}
|
|
/** @param {Creep} creep **/
|
|
run(creep) {
|
|
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
|
|
creep.memory.building = false;
|
|
}
|
|
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
|
|
creep.memory.building = true;
|
|
}
|
|
|
|
if(creep.memory.building) {
|
|
var targets = creep.room.find(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return structure.hitsMax - structure.hits > 0;
|
|
}
|
|
});
|
|
if(targets.length > 0) {
|
|
if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
|
|
}
|
|
}
|
|
else{
|
|
creep.say("IDLE")
|
|
}
|
|
}
|
|
else {
|
|
energyUtils.gatherEnergy(creep);
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = roleRepairer;
|