I'm very new to screeps and a beginner programmer so I may be missing something obvious, but I can't get my creeps to store energy into my containers. My main goal here is to get extra energy stored into containers when my spawn & extensions are all full.
To clarify, unless i'm missing something else, my main problem here is the filter for var targets, it is returning everything EXCEPT containers. I'm just not understanding why.
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
if(harvesters.length < 2){
var energystructures = _.filter (creep.room.find(FIND_STRUCTURES), (structure) => structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_EXTENSION);
var totalenergy = 0;
for(var i in energystructures){
totalenergy = totalenergy + energystructures[i].energy;
}
if(totalenergy >= 500){
Game.spawns['Spawn1'].spawnCreep([WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE ], 'Harvester' + Game.time.toString(), {memory: {role: 'harvester'}});
}
}
if(!creep.memory.harvesting && creep.carry.energy == 0){
creep.memory.harvesting = true;
creep.say('Harvesting');
}
if(creep.memory.harvesting && creep.carry.energy == creep.carryCapacity){
creep.memory.harvesting = false;
creep.say('Transporting Energy');
}
if(creep.memory.harvesting) {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
else if(!creep.memory.harvesting){
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_CONTAINER) &&
structure.energy < structure.energyCapacity || structure.store < structure.storeCapacity;
}
});
console.log(targets);
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
}
}
}
};
Here is the whole script for any context needed, but the issue is in the bottommost "else if" statement.
This works exactly as intended for the Spawn and Extension structures, but it doesn't even return any Containers as a target (proved that to myself via that little console.log function in the middle). Probably some simple solution but I don't understand why this doesn't work since it worked perfectly for other structures. Any help would be appreciated, and thanks.