2

I've tried to program healer creep. Simple task:

  1. find damaged creep.
  2. heal damaged creep.
  3. if no damaged creeps were found, return home (hardcoded Spawn1)

Here is my code (I also tried option with Game.CREEPS but it gave same effect):

module.exports = function (creep) {
    var damagedCreeps = creep.room.find(Game.MY_CREEPS, function(chr){return chr.hits < chr.hitsMax;});
    if (damagedCreeps.length > 0){
        creep.moveTo(damagedCreeps[0]);
        creep.heal(damagedCreeps[0]);
    } else {
        creep.moveTo(Game.spawns.Spawn1);
    }
};

Here are my creeps (in order of creation):

  1. Harvester1 (hits: 300, hitsMax: 300),
  2. Harvester2 (hits: 300, hitsMax: 300),
  3. Guard1 (hits: 190, hitsMax: 400),
  4. Healer1 (hits: 400, hitsMax: 400),
  5. Harvester3 (hits: 300, hitsMax: 300).

Harvesters are doing their thing, guard is doing his thing and "Healer1" follows "Harvester1".

I thought that I've misspelled hits and hitsMax and failed to notice it but in console I got:

> Game.creeps.Harvester1.hits
< 300
> Game.creeps.Harvester1.hitsMax
< 300

The only thing that comes to my mind is that 'chr' parameter contains something else than creep object.

I tried:

  1. consol.log(chr) but nothing appeared in in-game Console.
  2. 'Game.creeps.Healer1.memory.a=chr; Game.creeps.Healer1.a=chr;' and in console typed 'Game.creeps.Healer1.memory.a'/'Game.creeps.Healer1.a' but got undefined.
  3. In Chrome's js console: var a='Healer1'; a.hits < a.hitsMax;. Got 'false' on the second one.

Is it a game bug or have I missed something?

4

1 回答 1

5

詹姆斯是正确的。你可以找到你受伤的士兵使用

var wounded = creep.room.find( Game.MY_CREEPS, {
    filter: function(object) {
        return ( object.hits < object.hitsMax );
    } } );

现在你可以检查你是否有任何东西并遍历列表,比如

if( wounded.length ) {
    // Care for any wounded soldiers
}

for( var i = 0; i < wounded.length; ++i ) {
    // Do something to wounded[i]
}
于 2014-11-21T22:40:06.953 回答