screeps/role.guard.js
2016-06-29 21:37:58 -04:00

32 lines
1.1 KiB
JavaScript

var battle = require('battle')
var helpers = require('helpers');
var roleGuard = {
getDestinationFlag: function(){
return Game.flags['patrol_' + this.creep.memory.patrolDestination];
},
patrol: function(){
if (this.creep.pos.isEqualTo(this.getDestinationFlag())){
this.creep.memory.patrolDestination += 1;
}
if (this.creep.memory.patrolDestination > helpers.getLastFlag("patrol_")){
this.creep.memory.patrolDestination = 1;
}
this.creep.moveTo(this.getDestinationFlag());
},
run: function(creep){
this.creep = creep;
console.log(creep, 'running Guard')
creep.memory.patrolDestination = creep.memory.patrolDestination || 1;
var target = battle.findEnemy(creep);
if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) {
console.log('\t', creep, 'attacking:',target)
creep.moveTo(target);
creep.attack(target);
} else {
this.patrol();
}
}
}
module.exports = roleGuard;