screeps-scripts/role.builder.js

44 lines
1.6 KiB
JavaScript

var roleBuilder = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.building = false;
creep.say('🔄 harvest');
}
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
creep.memory.building = true;
creep.say('🚧 build');
}
if(creep.memory.building) {
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
if(targets.length) {
var nearestBuild = creep.pos.findClosestByRange(targets)
if(creep.build(nearestBuild) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearestBuild, {visualizePathStyle: {stroke: '#ffffff'}});
}
} else {
const repairs = creep.room.find(FIND_STRUCTURES, {
filter:(struc)=>{
return(struc.hits < (struc.hitsMax*0.75))
}
});
var nearestRepair = creep.pos.findClosestByRange(repairs);
if(creep.repair(nearestRepair) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearestRepair, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
} else {
var sources = creep.room.find(FIND_SOURCES);
var nearestSource = creep.pos.findClosestByRange(sources)
var nearestSource = sources[1]
if(creep.harvest(nearestSource) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearestSource, {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
};
module.exports = roleBuilder;