diff --git a/helpers.js b/helpers.js index 4a9952a..8a2dc2a 100644 --- a/helpers.js +++ b/helpers.js @@ -8,7 +8,16 @@ var helpers = { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); 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); + }, }; module.exports = helpers; \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index c563137..41257eb 100644 --- a/role.builder.js +++ b/role.builder.js @@ -10,16 +10,13 @@ var roleBuilder = { creep.memory.building = true; } if (creep.memory.building){ - console.log("\t is building"); var targets = creep.room.find(FIND_CONSTRUCTION_SITES); var transferErrorCode = creep.build(targets[0]); if(transferErrorCode == ERR_NOT_IN_RANGE) { - console.log("\t moving to", targets[0]); - creep.moveTo(targets[0]); + creep.moveTo(targets[0]); } } else { - console.log("\t","isFindingSources"); var sources = creep.room.find(FIND_SOURCES); if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ creep.moveTo(sources[0]); diff --git a/role.guard.js b/role.guard.js index c9b7841..a0d250a 100644 --- a/role.guard.js +++ b/role.guard.js @@ -1,40 +1,31 @@ var battle = require('battle') +var helpers = require('helpers'); var roleGuard = { - getLastFlag: 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); - }, getDestinationFlag: function(){ return Game.flags['patrol_' + this.creep.memory.patrolDestination]; }, + moveToPatrolDestination: function(){ + if (this.creep.pos.isEqualTo(this.getDestinationFlag())){ + this.creep.memory.patrolDestination += 1; + } + + if (this.creep.memory.patrolDestination > helpers.getLastPatrolFlag()){ + this.creep.memory.patrolDestination = 1; + } + this.creep.moveTo(this.getDestinationFlag()); + }, run: function(creep){ this.creep = creep; - console.log(creep, 'running Guard') - // Reset creep to 1 creep.memory.patrolDestination = creep.memory.patrolDestination || 1; - var target = battle.findEnemy(creep); if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) { console.log('\t', creep, 'attacking:',target) creep.moveTo(target); creep.attack(target); } else { - if (creep.pos.isEqualTo(this.getDestinationFlag())){ - creep.memory.patrolDestination += 1; - } - - if (creep.memory.patrolDestination > this.getLastFlag()){ - creep.memory.patrolDestination = 1; - } - console.log('\tMoving to Patrol leg:', creep.memory.patrolDestination); - creep.moveTo(this.getDestinationFlag()); + this.moveToPatrolDestination(); } }