screeps/role.guard.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

var battle = require('battle')
2016-06-29 17:21:09 +00:00
var roleGuard = {
2016-06-30 00:52:07 +00:00
getLastFlag: function(){
2016-06-30 01:16:00 +00:00
let patrolFlags = _.filter(Game.flags, function(flag){
return flag.name.indexOf("patrol_") == 0;
});
let flagNumbers = _.map(patrolFlags, function(flag){
return parseInt(flag.name.substring("patrol_".length, flag.name.length));
});
let max = Math.max.apply(Math, flagNumbers);
return max;
2016-06-30 00:52:07 +00:00
},
2016-06-29 17:21:09 +00:00
run: function(creep){
console.log(creep, 'running Guard')
2016-06-30 00:56:10 +00:00
// Reset creep to 1
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 {
var destinationFlag = Game.flags['patrol_' + creep.memory.patrolDestination];
2016-06-30 00:52:07 +00:00
if (creep.pos.isEqualTo(destinationFlag)){
creep.memory.patrolDestination += 1;
}
if (creep.memory.patrolDestination > this.getLastFlag()){
2016-06-30 00:56:10 +00:00
creep.memory.patrolDestination = 1;
2016-06-30 00:52:07 +00:00
}
destinationFlag = Game.flags['patrol_' + creep.memory.patrolDestination];
console.log('\tMoving to Patrol leg:', creep.memory.patrolDestination);
2016-06-30 00:52:07 +00:00
creep.moveTo(destinationFlag);
2016-06-29 17:21:09 +00:00
}
}
}
module.exports = roleGuard;