87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
var energyUtils = {
|
|
gatherEnergy : function(creep) {
|
|
let storage;
|
|
if(creep.room.memory.hasContainer){
|
|
storage = creep.room.find(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, {visualizePathStyle: {stroke: '#ffaa00'}});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if(storage.length == 0) return;
|
|
|
|
if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}});
|
|
}
|
|
},
|
|
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;
|
|
|
|
}
|
|
,
|
|
depositEnergy : function(creep) {
|
|
var targets = creep.room.find(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return (structure.structureType == STRUCTURE_SPAWN ||
|
|
structure.structureType == STRUCTURE_EXTENSION) &&
|
|
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
if(targets.length == 0){
|
|
targets = creep.room.find(FIND_STRUCTURES, {
|
|
filter: (structure) => {
|
|
return (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 true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = energyUtils;
|