diff --git a/helpers.js b/helpers.js index 8a2dc2a..d6879da 100644 --- a/helpers.js +++ b/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); }, }; diff --git a/role.guard.js b/role.guard.js index a0d250a..a75b2d2 100644 --- a/role.guard.js +++ b/role.guard.js @@ -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(); } }