Search code examples
screeps

Screeps Extension Filler


Just as a heads up, this is more of a "tell me if there is something wrong with this code" post. I apologize. I have been working on making my Screeps colony more efficient, which brings the need for higher quality units, which brings the need for extensions. I wrote a script for a unit to harvest energy and bring it/transfer it to the extensions, however it does not seem to be working. It does not give me any errors, and I am beginning to think that it may be simply a problem with my main script, and my the process of transferring the role function to the creeps, so if someone would be kind enough to simply look over my code and tell me if there is a issue with it, it would be much appreciated. Here is my code (I call the role "Filler"):

var roleFiller = {
    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 {
                creep.harvest(sources[1]);
            }

        } else {
            var targets = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return (structure.structureType == STRUCTURE_EXTENSION ||
                            structure.structureType == STRUCTURE_TOWER) &&
                        structure.energy < structure.energyCapacity;
                }
            });

            if (targets.length > 0) {

                if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {
                        visualizePathStyle: {
                            stroke: '#ffffff'
                        }
                    });

                }
            } else {
                creep.transfer(targets[0], RESOURCE_ENERGY);
            }
        }
    }
};

module.exports = roleFiller

Solution

  • First thing to try: add console.log("Ping!"); or something like that as the first line of the function to be sure that the role is being executed for your creep.

    run: function(creep) {
        console.log("Ping!");
        //all of the other stuff
    }
    

    Additional thing to note:

    if (targets.length > 0) {
        //...Some stuff
    if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
    } else {
        creep.transfer(targets[0], RESOURCE_ENERGY); //HERE!!!!
    }
    

    If the targets array is empty, then trying to access targets[0] will result in an error. If there are no targets, then there is nothing to transfer to. The entire else block should probably be removed.

    If you still need help, try expanding on what you mean by "it does not seem to be working." Does the harvesting part work, but not the filling part?