screeps/role.guard.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

var battle = require('battle')
2016-06-30 01:26:39 +00:00
var helpers = require('helpers');
2016-06-29 17:21:09 +00:00
var roleGuard = {
getDestinationFlag: function(){
return Game.flags['patrol_' + this.creep.memory.patrolDestination];
2016-06-30 00:52:07 +00:00
},
patrol: function(){
2016-06-30 01:26:39 +00:00
if (this.creep.pos.isEqualTo(this.getDestinationFlag())){
this.creep.memory.patrolDestination += 1;
}
2016-06-30 01:53:45 +00:00
if (this.creep.memory.patrolDestination > this.lastFlag){
2016-06-30 01:26:39 +00:00
this.creep.memory.patrolDestination = 1;
}
this.creep.moveTo(this.getDestinationFlag());
},
2016-06-29 17:21:09 +00:00
run: function(creep){
this.creep = creep;
2016-06-30 01:53:45 +00:00
this.lastFlag = helpers.getLastFlag("patrol_");
console.log(creep, 'running Guard')
2016-06-30 00:56:10 +00:00
creep.memory.patrolDestination = creep.memory.patrolDestination || 1;
2016-06-29 17:21:09 +00:00
var target = battle.findEnemy(creep);
2016-06-30 00:52:07 +00:00
if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) {
console.log('\t', creep, 'attacking:',target)
2016-06-29 17:21:09 +00:00
creep.moveTo(target);
creep.attack(target);
} else {
2016-06-30 01:53:45 +00:00
if (this.lastFlag > 0){
this.patrol();
} else {
this.creep.moveTo(creep.room.controller)
}
2016-06-29 17:21:09 +00:00
}
}
}
module.exports = roleGuard;