Transferring energy to storage



  • Why is the coding excessively cryptic? It seems that the command which works fine when transferring energy to STRUCTURE_EXTENSION does not also work for STRUCTURE_STORAGE or CONTAINER. Any help would be great, see my current code below:

    run: function(creep) {
        if(creep.carry.energy < creep.carryCapacity) {
            var sources = creep.room.find(FIND_SOURCES);
            if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
            }
        }
        else {
            var targets = creep.pos.findClosestByPath(FIND_STRUCTURES, {
                    filter: (s) => (s.structureType == STRUCTURE_STORAGE) &&
                            s.energy < s.energyCapacity
            });
            if(targets) {
                if(creep.transfer(targets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets, {visualizePathStyle: {stroke: '#ffffff'}});


  • energy structures vs store structures

    Terminal/Storage/Container/Tombstone have a "store" which works similar to a creep's "carry".

    s.energy < s.energyCapacity would work for an energy structure like an extension but not for a store structure like a container. In this case you need s.store.energy < s.storeCapacity.



  • Or better remember if there is some mineral in there too, maybe use lowdash container.storeCapacity - _.sum(container.store) for free space.



  • The reason that containers/storage/terminals are different is because they can store minerals. Extensions and spawns and towers can only store energy, so there's no need for the store object.

    For store, store[RESOURCE_ENERGY] is always defined, 0 if it's empty. If the other minerals/compounds aren't present, those values are undefined. So if there's no lemergium, then store[RESOURCE_LEMERGIUM] would be undefined



  • @wtfrank @Saruss @famine Thanks a lot for the explanation guys now I understand why it is different and the code is working all good also.