105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
var energyUtils = {
|
|
|
|
// get energy from a container (if available) otherwise just mine for it at
|
|
// an energy source
|
|
gatherEnergy : function(creep) {
|
|
let storage;
|
|
if(creep.room.memory.containers > 0){
|
|
storage = creep.room.findClosestByRange(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return (structure.structureType == STRUCTURE_CONTAINER) &&
|
|
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
}
|
|
else{
|
|
if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep);
|
|
let source = Game.getObjectById(creep.memory.assignedSource);
|
|
if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(source);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if(storage == null) return;
|
|
|
|
if(creep.withdraw(storage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(storage);
|
|
}
|
|
},
|
|
|
|
//assign a creep to source in the room the creep is in
|
|
sourceDelegate : function(creep){
|
|
const room = creep.room;
|
|
|
|
const sources = creep.room.find(FIND_SOURCES);
|
|
|
|
let minSource = sources[0].id;
|
|
let minSourceCount = 0;
|
|
|
|
if(typeof room.memory[sources[0].id] !== 'undefined')
|
|
minSourceCount = room.memory[sources[0].id].length;
|
|
|
|
sources.forEach(function(source){
|
|
if(typeof room.memory[source.id] === 'undefined')
|
|
room.memory[source.id] = [];
|
|
|
|
room.memory[source.id].forEach(function(assignedCreep, index){
|
|
if(!Game.creeps[assignedCreep]){
|
|
room.memory[source.id].splice(index, 1);
|
|
}
|
|
});
|
|
|
|
if(room.memory[source.id].length < minSourceCount){
|
|
minSource = source.id;
|
|
minSourceCount = room.memory[source.id].length;
|
|
}
|
|
});
|
|
|
|
room.memory[minSource].push(creep.name);
|
|
creep.memory.assignedSource = minSource;
|
|
|
|
},
|
|
|
|
// put energy into storage, first if possible into a spawner / extension
|
|
// otherwise put it into a container
|
|
depositEnergy : function(creep) {
|
|
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return (structure.structureType == STRUCTURE_SPAWN ||
|
|
structure.structureType == STRUCTURE_EXTENSION) &&
|
|
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
if(target == null){
|
|
target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return (structure.structureType == STRUCTURE_CONTAINER) &&
|
|
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
}
|
|
if(target != null) {
|
|
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// if no storage anywhere, then just build something
|
|
// in order to speed up early game, since no storage
|
|
if(creep.room.memory.containers == 0){
|
|
target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
|
|
if(target != null){
|
|
if(creep.build(target) == ERR_NOT_IN_RANGE){
|
|
creep.moveTo(target);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = energyUtils;
|