use ho0ber's code for max, and parameteize it

This commit is contained in:
Tyrel Souza 2016-06-29 21:37:58 -04:00
parent acbffe5ed8
commit 68faf498b7
2 changed files with 11 additions and 16 deletions

View File

@ -1,7 +1,3 @@
/**
* Created by tsouza on 6/28/2016.
*/
var helpers = {
generateName: function(role){
return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) {
@ -9,14 +5,14 @@ var helpers = {
return v.toString(16);
});
},
getLastPatrolFlag: function(){
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));
});
return Math.max.apply(Math, flagNumbers);
getLastFlag: function(flagName){
return Object.keys(Game.flags).reduce(function(m,f) {
if (f.startsWith(flagName)){
m = Math.max(m, f.slice(flagName.length));
}
return m;
},
-1);
},
};

View File

@ -5,12 +5,11 @@ var roleGuard = {
getDestinationFlag: function(){
return Game.flags['patrol_' + this.creep.memory.patrolDestination];
},
moveToPatrolDestination: function(){
patrol: function(){
if (this.creep.pos.isEqualTo(this.getDestinationFlag())){
this.creep.memory.patrolDestination += 1;
}
if (this.creep.memory.patrolDestination > helpers.getLastPatrolFlag()){
if (this.creep.memory.patrolDestination > helpers.getLastFlag("patrol_")){
this.creep.memory.patrolDestination = 1;
}
this.creep.moveTo(this.getDestinationFlag());
@ -25,7 +24,7 @@ var roleGuard = {
creep.moveTo(target);
creep.attack(target);
} else {
this.moveToPatrolDestination();
this.patrol();
}
}