54 lines
3.1 KiB
JavaScript
54 lines
3.1 KiB
JavaScript
|
|
/* #######################################################
|
|
# Deliver energy to spawns, controller and extencions #
|
|
####################################################### */
|
|
|
|
module.exports = {
|
|
do: function(creep){
|
|
// Memory creation
|
|
if(creep.memory.restock == undefined) creep.memory.restock = true;
|
|
if(creep.memory.target == undefined) creep.memory.target = null;
|
|
// Job implementation
|
|
if(creep.memory.restock){
|
|
if(creep.memory.target == null){ // aquire target
|
|
var containers = creep.room.find(FIND_STRUCTURES,
|
|
{filter: (s)=>(s.structureType == STRUCTURE_CONTAINER
|
|
&& s.store.getUsedCapacity(RESOURCE_ENERGY) >= creep.store.getFreeCapacity(RESOURCE_ENERGY)/2)});
|
|
var container = containers[0];
|
|
for(var c in containers){
|
|
c = containers[c];
|
|
if(c.store.getUsedCapacity(RESOURCE_ENERGY) > container.store.getUsedCapacity(RESOURCE_ENERGY)) container = c;
|
|
}
|
|
if(container != null) creep.memory.target = container.id;
|
|
else creep.memory.target = creep.room.storage.id;
|
|
}
|
|
var target = Game.getObjectById(creep.memory.target);
|
|
if(creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
if(target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY)*0.4) creep.memory.target = null;
|
|
if(creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
|
creep.memory.restock = false;
|
|
creep.memory.target = null;
|
|
}
|
|
} else {
|
|
if(creep.memory.target == null){ // aquire target
|
|
var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
{filter: (s)=>(s.structureType == STRUCTURE_TOWER && s.store.getUsedCapacity(RESOURCE_ENERGY) < 900)});
|
|
if(structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
{filter: (s)=>(( s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION)
|
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > s.store.getCapacity(RESOURCE_ENERGY)*0.1)});
|
|
if(structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
{filter: (s)=>(( s.id == "5f3e44e1014017c66fc5df10") && s.store.getFreeCapacity() > 100)});
|
|
if(structure == null) structure = creep.room.storage;
|
|
if(structure == null) return;
|
|
creep.memory.target = structure.id;
|
|
}
|
|
var target = Game.getObjectById(creep.memory.target);
|
|
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
if(target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
|
if(creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
|
creep.memory.restock = true;
|
|
creep.memory.target = null;
|
|
}
|
|
}
|
|
}
|
|
}; |