screeps/role.repairbot.js

54 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-06-06 13:17:26 -05:00
let buildparts=require('bodypartbuilder')
2016-09-28 08:37:28 -05:00
let roleRepairbot = {
2016-09-23 00:12:42 -05:00
run: function(creep) {
2016-09-28 08:37:28 -05:00
if(creep.memory.originroom === undefined){
creep.memory.originroom = creep.room.name
}
if(creep.memory.repairing == undefined){creep.memory.repairing=true}
if(creep.memory.repairing && creep.carry.energy == 0) {
creep.memory.repairing = false;
creep.say('gathering');
}
if(!creep.memory.repairing && creep.carry.energy == creep.carryCapacity) {
creep.memory.repairing = true;
creep.say('repairing');
}
if(creep.memory.repairing==false){
2016-09-28 08:37:28 -05:00
let container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
2016-09-15 01:56:03 -05:00
return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;
}});
if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.say("MTSC");
creep.moveTo(container);
}
} else {
2017-06-06 13:17:26 -05:00
if(Game.rooms[creep.memory.originroom].memory.containerstoragepercent > .7 || !(Game.rooms[creep.memory.originroom].memory.containerstoragepercent)){
2016-09-28 08:37:28 -05:00
let importantstructures = creep.room.find(FIND_STRUCTURES, {
2016-09-23 00:12:42 -05:00
filter: (structure) => {
2017-06-06 13:17:26 -05:00
return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType==STRUCTURE_RAMPART) && structure.hits < structure.hitsMax) ;
2016-09-23 00:12:42 -05:00
}});
importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax)
if(importantstructures.length > 0){
2017-06-06 13:17:26 -05:00
if(Game.rooms[creep.memory.originroom].memory.containerstoragepercent > .5 || !(Game.rooms[creep.memory.originroom].memory.containerstoragepercent)){
if(creep.repair(importantstructures[0]) == ERR_NOT_IN_RANGE){
creep.moveTo(importantstructures[0])
}
2016-09-23 00:12:42 -05:00
}
} else {
2017-06-06 13:17:26 -05:00
if(Game.rooms[creep.memory.originroom].memory.containerstoragepercent > .7 || !(Game.rooms[creep.memory.originroom].memory.containerstoragepercent)){
2016-09-28 08:37:28 -05:00
let damagedstructures = creep.room.find(FIND_STRUCTURES,{filter: (s) => s.hits < s.hitsMax});
2016-09-23 00:12:42 -05:00
damagedstructures = _.sortBy(damagedstructures, (s)=>s.hits / s.hitsMax)
if(damagedstructures.length>0){
if(creep.repair(damagedstructures[0]) == ERR_NOT_IN_RANGE){
creep.moveTo(damagedstructures[0])
}
}
2017-06-06 13:17:26 -05:00
}
2016-09-23 00:12:42 -05:00
}
}
}
2017-06-06 13:17:26 -05:00
}
};
module.exports = roleRepairbot;