TsarBot/utils/energy.js

49 lines
1.9 KiB
JavaScript

var energyUtils = {
gatherEnergy : function(creep) {
var storage = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER) &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
}
});
/**
if(storage.length == 0){
storage = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN) &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
}
});
}
**/
if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
},
depositEnergy : function(creep) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_SPAWN &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) < 250;
}
});
if(targets.length == 0){
targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
}
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
}
return targets.length > 0;
}
}
module.exports = energyUtils;