screeps/role.guard.js

34 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-06-29 17:21:09 +00:00
var battle = require("battle")
var roleGuard = {
2016-06-30 00:52:07 +00:00
getLastFlag: function(){
2016-06-30 00:56:10 +00:00
return 2; // 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 {
2016-06-30 00:52:07 +00:00
var destinationFlag = Game.flags["patrol_" + creep.memory.patrolDestination];
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);
creep.moveTo(destinationFlag);
2016-06-29 17:21:09 +00:00
}
}
}
module.exports = roleGuard;