From ed4c5eb67fb0663992e73b66b03186c111ad8164 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 29 Jun 2016 21:53:45 -0400 Subject: [PATCH] cleanup lastFlagCode --- helpers.js | 11 ++++------- main.js | 6 +++--- role.builder.js | 1 - role.guard.js | 9 +++++++-- role.upgrader.js | 4 ---- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/helpers.js b/helpers.js index d6879da..7011e0a 100644 --- a/helpers.js +++ b/helpers.js @@ -6,13 +6,10 @@ var helpers = { }); }, 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); + return _(Object.keys(Game.flags)) + .filter((f) => f.startsWith(flagName)) + .map((f) => f.slice(flagName.length)) + .max(); }, }; diff --git a/main.js b/main.js index 4fb7e17..12efb7a 100644 --- a/main.js +++ b/main.js @@ -17,8 +17,8 @@ var creepsConfig = { role: roleBuilder }, upgrader: { - bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE], - minimumCreeps: 4, + bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE], + minimumCreeps: 6, role: roleUpgrader }, guard: { @@ -35,7 +35,7 @@ module.exports.loop = function(){ for (var name in Memory.creeps){ if (!Game.creeps[name]){ delete Memory.creeps[name]; - console.log('Cleaning non-existing creep memory: ', name); + console.log('Cleaning non-existing creep memory:', name); } } diff --git a/role.builder.js b/role.builder.js index 41257eb..0aebba6 100644 --- a/role.builder.js +++ b/role.builder.js @@ -2,7 +2,6 @@ var BreakException= {}; var roleBuilder = { run: function(creep){ - console.log(creep, "running Builder"); if(creep.memory.building && creep.carry.energy == 0){ creep.memory.building = false; } diff --git a/role.guard.js b/role.guard.js index a75b2d2..9525038 100644 --- a/role.guard.js +++ b/role.guard.js @@ -9,13 +9,14 @@ var roleGuard = { if (this.creep.pos.isEqualTo(this.getDestinationFlag())){ this.creep.memory.patrolDestination += 1; } - if (this.creep.memory.patrolDestination > helpers.getLastFlag("patrol_")){ + if (this.creep.memory.patrolDestination > this.lastFlag){ this.creep.memory.patrolDestination = 1; } this.creep.moveTo(this.getDestinationFlag()); }, run: function(creep){ this.creep = creep; + this.lastFlag = helpers.getLastFlag("patrol_"); console.log(creep, 'running Guard') creep.memory.patrolDestination = creep.memory.patrolDestination || 1; var target = battle.findEnemy(creep); @@ -24,7 +25,11 @@ var roleGuard = { creep.moveTo(target); creep.attack(target); } else { - this.patrol(); + if (this.lastFlag > 0){ + this.patrol(); + } else { + this.creep.moveTo(creep.room.controller) + } } } diff --git a/role.upgrader.js b/role.upgrader.js index 8154654..77a7024 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -2,8 +2,6 @@ var roleUpgrader = { /** @param {Creep} creep **/ run: function(creep) { - console.log(creep, 'running Upgrade'); - if(creep.memory.upgrading && creep.carry.energy == 0) { creep.memory.upgrading = false; } @@ -13,14 +11,12 @@ var roleUpgrader = { if(creep.memory.upgrading) { if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - console.log('\tmoving to', creep.room.controller); creep.moveTo(creep.room.controller); } } else { var source = creep.pos.findClosestByPath(FIND_SOURCES); if(creep.harvest(source) == ERR_NOT_IN_RANGE) { - console.log('\tmoving to', source); creep.moveTo(source); } }