I've tried to program healer creep. Simple task:
- find damaged creep.
- heal damaged creep.
- 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):
- Harvester1 (hits: 300, hitsMax: 300),
- Harvester2 (hits: 300, hitsMax: 300),
- Guard1 (hits: 190, hitsMax: 400),
- Healer1 (hits: 400, hitsMax: 400),
- 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:
- consol.log(chr) but nothing appeared in in-game Console.
- '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.
- 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?