screeps-scripts/role.harvester.js

37 lines
1.4 KiB
JavaScript

var roleBuilder = require('role.builder');
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.store.getFreeCapacity() > 0) {
var sources = creep.room.find(FIND_SOURCES);
var t = creep.pos.findClosestByRange(sources)
if(creep.harvest(t) == ERR_NOT_IN_RANGE) {
creep.moveTo(t, {visualizePathStyle: {stroke: '#aaff00'}});
}
} else {
// TODO spawn should be priotized
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
if(targets.length > 0) {
var t = creep.pos.findClosestByRange(targets)
if(creep.transfer(t, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(t, {visualizePathStyle: {stroke: '#ffffff'}});
}
} else { // no sturctures with free storage
roleBuilder.run(creep);
}
}
}
};
module.exports = roleHarvester;