Can't make harvesters put into containers or storage by type or ID
-
Sorry for the messy code, i keep adding to it to try to make it work but can't get any of my creeps to consistantly interact with storage or containers... have max energy in all locations... creeps say "bye" but never move towards or put energy in storage.
thanks
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
if(creep.carry.energy>0){
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.structureType ==STRUCTURE_STORAGE ) &&
structure.energy < structure.energyCapacity;
}
});
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
}
}
else
{
creep.moveTo(sources[0]);
}
}
}
else {
//creep.say('hi')
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.structureType==STRUCTURE_STORAGE) &&
structure.energy < structure.energyCapacity;
}
});
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
}else
{
creep.say('bye');
var targets = Game.getObjectById('583cfbe2a4a358bb5cadb920');
creep.moveTo(targets);
if(targets.energy<targets.energyCapacity) {
if(creep.transfer(targets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
{
creep.moveTo(targets);
}else{
targets = Game.getObjectById('584062d3de55a3805b70708d');
if(targets.energy<targets.energyCapacity) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
{
creep.moveTo(targets[0]);
}
}
}
}
}
}
}
};module.exports = roleHarvester;
-
I had this exact same problem. The issue is that Containers and Storage don't have an energy property; instead, because they can hold more than energy, they have a "store" property. Take a look at the documentation for the details: http://support.screeps.com/hc/en-us/articles/208436805-StructureStorage . I split my filter conditions into two parts to account for this. Hope that helps.