moved some stuff to helpers

This commit is contained in:
Tyrel Souza 2016-06-29 21:26:39 -04:00
parent b4be39510a
commit acbffe5ed8
3 changed files with 23 additions and 26 deletions

View File

@ -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;

View File

@ -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]);
}
} 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]);

View File

@ -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();
}
}