Screeps get all creeps with specific memory (role)

I am trying to figure out how to get each creep with a specific memory or role similar to a harvester in a variable ... I can't 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 that obviously won't work ...

+4
source share
3 answers

You can create another array of creeps with a role harvester:

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');
}
+5
source

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

, , , , , . , 3.

, , . , , , . :

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

, memory memory.role.

, , , . , ( , )

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

, , , , , , .

+1

The room.find and position.findNearest methods use optional parameters, one of which is a filter. I use this to find all specific types.

First, I make sure that I assign a type to each creep that generates me:

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

And then 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";
}
0
source

All Articles