Picking Up Dropped Energy



  • I've written this code for my creep to pick up dropped energy but when it finds the dropped energy it just becomes paralyzed. Have I written anything wrong?

    Code:

    var roleHarvester = require('role.harvester');

    var roleLabourer = {
        run: function(creep){
            var dropenergy = creep.pos.findClosestByPath(FIND_DROPPED_ENERGY)
            if(dropenergy.length != 0){
                if(creep.pickup(dropenergy[0]) == ERR_NOT_IN_RANGE){
                    creep.moveTo(dropenergy[0].pos)
                }
                if(creep.pickup(dropenergy[0]) == ERR_FULL){
                    var storages = Game.spawns.Spawn1.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_STORAGE }});
                    for (i = 0; i < storages.length; i++) {
                        if(storages[i].store[RESOURCE_ENERGY] < storages[i].storeCapacity){
                            if(creep.transfer(storages[i],RESOURCE_ENERGY,creep.carry.energy) == ERR_NOT_IN_RANGE){
                                creep.moveTo(storages[i])
                            }
                        }
                    }
                }
            }
            else{
                roleHarvester.run(creep)
            }
        }
    }

    module.exports = roleLabourer


  • YP

    You have a serious error in your code.. you have a whole section with a loop and moving in an if section that is only once executed when you pick up the energy.

     

    you should join the slack chat to get better help



  • findClosestByPath returns a single value not an array - so dont use dropenergy[0] but just dropenergy

     

    as for the storage finding loop, do the same - use findclosest rather than find