use ho0ber's code for max, and parameteize it
This commit is contained in:
parent
acbffe5ed8
commit
68faf498b7
20
helpers.js
20
helpers.js
|
@ -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);
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user