Need help storing energy in containers



  • 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.



  • Hello @Navine

    Try...

    _.sum(structure.store) // If there's que chance u store multiple resources
    

    or...

    structure.store.energy //If you are sure you are going to store energy only

  • Dev Team

    @navine That's because containers can contain all types of resources (energy, minerals, power) so instead of .energy and .energyCapacity properties you should use .store and .storeCapacity properties.



  • @o4kapuk said in Need help storing energy in containers:

    @navine That's because containers can contain all types of resources (energy, minerals, power) so instead of .energy and .energyCapacity properties you should use .store and .storeCapacity properties.

    Thanks mate, but I actually came across this info when I was attempting to fix this issue myself, If you have a look at this part here:

    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; } });

    I do actually use the .store and .storeCapacity properties at the very end in an attempt to filter them into my "targets" variable. Unless I'm misunderstanding how to use it, which is very possible to be honest.



  • @calfa said in Need help storing energy in containers:

    Hello @Navine

    Try...

    _.sum(structure.store) // If there's que chance u store multiple resources
    

    or...

    structure.store.energy //If you are sure you are going to store energy only
    

    For now I'm just looking to store energy, I don't even know how to deal with anything else yet.

    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.energy < structure.storeCapacity; } });

    I gave structure.store.energy a shot, but that gave me this error:

    [8:50:47 PM] [shard0]role.harvester:37 structure.energy < structure.energyCapacity || structure.store. < structure.storeCapacity; ^

    SyntaxError: Unexpected token < at Object.module.exports.loop (main:2:25) at __mainLoop:1:52 at sigintHandlersWrap (vm.js:98:15)



  • try like this...

    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) && _.sum(structure.store) < structure.storeCapacity; } });
    

    EDIT: My answer is using _.sum(structure.store) < structure.storeCapacity for every type of structure, and as Mr. @o4kapuk said, that's not correct.


  • Dev Team

    @navine That's right, you're doing it wrong. Try:

    { filter: (structure) => { 
        return 
            ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && (structure.energy < structure.energyCapacity)) || 
            ((structure.structureType == STRUCTURE_CONTAINER) && (_.sum(structure.store) < structure.storeCapacity));
        }
    }
    


  • Thanks a lot @Calfa and @o4kapuk it seems to be working now.

    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 || _.sum(structure.store) < structure.storeCapacity; } });

    really appreciate the help.