screeps/role.guard.js

35 lines
1.2 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(){
return 4; // TODO. calculate me.
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;