From 68faf498b71c0c7b7e6b232eadf6d9044bff0f5c Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 29 Jun 2016 21:37:58 -0400 Subject: [PATCH] use ho0ber's code for max, and parameteize it --- helpers.js | 20 ++++++++------------ role.guard.js | 7 +++---- 2 files changed, 11 insertions(+), 16 deletions(-) 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(); } }