var idler = require('idle.base'); var wallRepairer = require('role.wallrepairer'); var harvester = require('energy.harvest'); var collector = require('energy.storage'); var get_target = function(creep) { var result; var repairTarget; result = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: (structure) => structure.hits < structure.hitsMax && structure.hits < 10000 && !Memory.rooms[creep.room.name].repairs.includes(structure.id) }); if(result){ repairTarget = Math.min(11000, result.hitsMax); } if(!result) { result = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => structure.hits < structure.hitsMax && (structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE) && structure.hits < 10000 && !Memory.rooms[creep.room.name].repairs.includes(structure.id) }); if(result){ repairTarget = Math.min(11000, result.hitsMax); } } if(!result) { result = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => structure.hits < structure.hitsMax && structure.structureType != STRUCTURE_WALL && structure.hits < 10000 && !Memory.rooms[creep.room.name].repairs.includes(structure.id) }); if(result){ repairTarget = Math.min(11000, result.hitsMax); } } if(!result) { result = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => structure.hits < structure.hitsMax && (structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE) && !Memory.rooms[creep.room.name].repairs.includes(structure.id) }); if(result){ repairTarget = result.hitsMax; } } if(!result) { result = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => structure.hits < structure.hitsMax && structure.structureType != STRUCTURE_WALL && !Memory.rooms[creep.room.name].repairs.includes(structure.id) }); if(result){ repairTarget = result.hitsMax; } } // If no result can be found, go repair a wall or something if(!result){ result = wallRepairer.get_target(creep); repairTarget = -1; } if(result){ creep.memory.repairTarget = repairTarget; creep.memory.repairId = result.id Memory.rooms[creep.room.name].repairs.push(result.id); } return result } var roleRepairer = { /** @param {Creep} creep @param {int} behaviourLevel **/ run: function(creep, behaviourLevel) { switch (creep.memory.state) { case "idle": idler.do_idle(creep); if (get_target(creep)){ // If there are any buildings to repair, switch back to the repair state idler.clear_idle(creep); creep.memory.state = "repair_structure"; creep.say("repairing"); } if (creep.store.getFreeCapacity() > 0) { // If we are not full with energy, switch to the get_energy state idler.clear_idle(creep); creep.memory.state = "get_energy"; creep.say('Reloading...'); } break; case "repair_structure": if(creep.memory.repairId){ var struct = Game.getObjectById(creep.memory.repairId); if (struct.hits >= creep.memory.repairTarget) { _.remove(Memory.rooms[creep.room.name].repairs, (n) => n == creep.memory.repairId); delete creep.memory.repairId; delete creep.memory.repairTarget; } } if (!creep.memory.repairId) { creep.memory.repairId = get_target(creep).id; } var closestDamagedStructure = Game.getObjectById(creep.memory.repairId); if(closestDamagedStructure) { if (creep.repair(closestDamagedStructure) == ERR_NOT_IN_RANGE) { creep.moveTo(closestDamagedStructure, {visualizePathStyle: {stroke: '#FF9999'}}); } } else { // Else, go idle creep.memory.state = "idle"; creep.say('idling'); } if (creep.store.getUsedCapacity() == 0) { // If we are out of energy, get more! creep.memory.state = "get_energy"; creep.say('Reloading...'); // Forget what we were doing, so we can pick the best repair option when we are done recharging _.remove(Memory.rooms[creep.room.name].repairs, (n) => n == creep.memory.repairId); delete creep.memory.repairId; delete creep.memory.repairTarget; } break; case "get_energy": var energy_source; if(behaviourLevel >= 2) { energy_source = collector; } else { energy_source = harvester; } energy_source.do_collect(creep, "repair_structure", "Repairing..."); break; default: creep.memory.state = "repair_structure"; creep.say('repairing'); } } }; module.exports = roleRepairer;