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 = { var helpers = {
generateName: function(role){ generateName: function(role){
return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) { return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) {
@ -9,14 +5,14 @@ var helpers = {
return v.toString(16); return v.toString(16);
}); });
}, },
getLastPatrolFlag: function(){ getLastFlag: function(flagName){
let patrolFlags = _.filter(Game.flags, function(flag){ return Object.keys(Game.flags).reduce(function(m,f) {
return flag.name.indexOf("patrol_") == 0; if (f.startsWith(flagName)){
}); m = Math.max(m, f.slice(flagName.length));
let flagNumbers = _.map(patrolFlags, function(flag){ }
return parseInt(flag.name.substring("patrol_".length, flag.name.length)); return m;
}); },
return Math.max.apply(Math, flagNumbers); -1);
}, },
}; };

View File

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