Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screeps get all creeps with specific memory (role)

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...

like image 217
Axenth Avatar asked Nov 21 '14 10:11

Axenth


2 Answers

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');
}
like image 163
artch Avatar answered Oct 26 '22 23:10

artch


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.

like image 39
dlkulp Avatar answered Oct 26 '22 23:10

dlkulp