Screeps get all creeps with specific memory (role)

8.5k Views Asked by At

I'm trying to figure out how to get every creep with a specific memory or role like harvester in a variable... I can't seem to figure it out.

I already tried:

module.exports = function(){

    for(var i in Game.creeps){
        if(i.memory == 'Harvester'){
            var Harvesters = Game.creeps[i];

            if(Harvesters.index < 3){
                Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE],'Harvester'+ Harvesters.length, 'Harvester');
            }
        }
    }
}

But this obviously wouldn't work...

3

There are 3 best solutions below

2
On BEST ANSWER

You can create another array from the creeps with harvester role:

var harvesters = [];
for(var i in Game.creeps) {
    if(Game.creeps[i].memory== 'harvester') {
    harvesters.push(Game.creeps[i]);
}

if(harvesters.length < 3){
    Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE], null, 'Harvester');
}

Or using lodash:

var harvesters = _.filter(Game.creeps, {memory: 'harvester'});

if(_.size(harvesters) < 3){
    Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE], null, 'Harvester');
}
0
On

You can do something like this:

if(Number(Harvesters.name.slice(-1)) < 3)
    Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE],'Harvester'+ Harvesters.length, 'Harvester');

The thing to note here is that index is not a property and the deceptive id property is unique and therefore not what you're looking for. This code instead looks at the name of the current creep and gets the last character, then turns that into a number, and compares it to 3.

The way you're assigning to memory is fine though. You could do an object there if you really wanted, but that seems unnecessary with how you have it set up right now. It would look like this:

Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE],'Harvester'+ Harvesters.length, {role: 'Harvester'});

And you'd have to be sure to change your memory checks to memory.role.

Rereading and I think what I did is not really what you were asking. If I understand correctly this should work better (not pretty though)

module.exports = function() {

    var totalHarv = 0;
    for(var q in Game.creeps)
        if (Game.creeps[q].memory == 'Harvester')
            totalHarv++;

    for(var i in Game.creeps) {
        if(Game.creeps[i].memory == 'Harvester') {
            while(totalHarv < 3) {
                Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE], 'Harvester'+ totalHarv, 'Harvester');
                totalHarv++;
            }
        }
    }
}

This loops through the creeps and increments a counter if it finds a Harvester, then later when you're trying to spawn and stuff, it can check to see if there are enough of them and add more if necessary.

1
On

The room.find and position.findNearest methods both take optional parameters, one of which is filter. I use that to find all of a certain type.

First, I make sure that I assign a type to every creep I spawn:

var new_creep = spawn.createCreep(creepTypes[requirement.Type].Spec);
Memory.creeps[new_creep].Type = requirement.Type;

And then later I can use the filter:

function gatherHarvesters(creep) {
    var harvesters = creep.room.find(Game.MY_CREEPS, { "filter": selectHarvesters });
    _.sortBy(harvesters, function(harvester) {
        return (harvester.energy * harvester.energy) - creep.room.findPath(creep.pos, harvester.pos).length;
    });
    return harvesters;
}

function selectHarvesters(creep) {
    return creep.memory.Type == "Harvester";
}