Added repairbot and limits on energy usage

This commit is contained in:
paradizelost 2016-09-15 01:02:53 -05:00
parent 9f9811e831
commit 52d492863d
4 changed files with 103 additions and 4 deletions

View File

@ -7,6 +7,7 @@ var roleMiner2 = require('role.miner2');
var roleHauler = require('role.hauler'); var roleHauler = require('role.hauler');
var roleHauler2 = require('role.hauler2'); var roleHauler2 = require('role.hauler2');
var roleWarrior = require('role.warrior'); var roleWarrior = require('role.warrior');
var roleRepairbot = require('role.repairbot')
var runTower = require('tower'); var runTower = require('tower');
var roleTowerrecharger = require('role.towerrecharger'); var roleTowerrecharger = require('role.towerrecharger');
var purgetype="NONE" var purgetype="NONE"
@ -52,6 +53,7 @@ module.exports.loop = function () {
roleHauler2.spawn() roleHauler2.spawn()
roleTowerrecharger.spawn() roleTowerrecharger.spawn()
roleWarrior.spawn() roleWarrior.spawn()
roleRepairbot.spawn()
for(var name in Game.creeps) { for(var name in Game.creeps) {
var creep = Game.creeps[name]; var creep = Game.creeps[name];
if(creep.memory.role == 'harvester') { if(creep.memory.role == 'harvester') {
@ -84,5 +86,8 @@ module.exports.loop = function () {
if(creep.memory.role == 'towerrecharger'){ if(creep.memory.role == 'towerrecharger'){
roleTowerrecharger.run(creep) roleTowerrecharger.run(creep)
} }
if(creep.memory.role == 'repairbot'){
roleRepairbot.run(creep)
}
} }
} }

View File

@ -2,7 +2,21 @@ var roleBuilder = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep,mysource) { run: function(creep,mysource) {
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return ( s.structureType == STRUCTURE_CONTAINER)
}
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
console.log(usedstorage + " " + mycapacity)
var storagepercent = usedstorage/mycapacity
var ttl = creep.ticksToLive var ttl = creep.ticksToLive
if(ttl < 300) { if(ttl < 300) {
console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role ) console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role )
@ -22,6 +36,7 @@ var roleBuilder = {
if(creep.memory.building) { if(creep.memory.building) {
var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if(target != undefined) { if(target != undefined) {
if(storagepercent > .4){
if(creep.build(target) == ERR_NOT_IN_RANGE) { if(creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target); creep.moveTo(target);
if(creep.fatigue<1){ if(creep.fatigue<1){
@ -33,6 +48,7 @@ var roleBuilder = {
creep.say("MTF") creep.say("MTF")
creep.moveTo(Game.flags.Flag2); creep.moveTo(Game.flags.Flag2);
} }
}
} }
else { else {
var containers = creep.room.find(FIND_STRUCTURES, { var containers = creep.room.find(FIND_STRUCTURES, {

64
role.repairbot.js Normal file
View File

@ -0,0 +1,64 @@
var roleRepairbot = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.repairing == undefined){creep.memory.repairing=true}
if(creep.memory.repairing && creep.carry.energy == 0) {
creep.memory.repairing = false;
creep.say('gathering');
}
if(!creep.memory.repairing && creep.carry.energy == creep.carryCapacity) {
creep.memory.repairing = true;
creep.say('repairing');
}
console.log(creep.name)
if(creep.memory.repairing==false){
var container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0) ;
}});
if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.say("MTSC");
creep.moveTo(container);
}
} else {
var myclosestDamagedStructure = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax
});
var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax));
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return ( s.structureType == STRUCTURE_CONTAINER)
}
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
var storagepercent = usedstorage/mycapacity
if(closestDamagedStructure) {
if(storagepercent > .1){
console.log(creep.name + " repairing. Currently at: " + storagepercent)
if(creep.repair(closestDamagedStructure) == ERR_NOT_IN_RANGE){ creep.moveTo(closestDamagedStructure)}
} else { console.log(creep.name + " waiting for more storage. Currently at: " + storagepercent)}
}
}
},
spawn: function(){
var myrole='repairbot';
var nummyrole=1;
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole);
if(myroles.length < nummyrole) {
console.log('Miners: ' + myroles.length + ' Needed: ' + nummyrole);
var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,CARRY,MOVE], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName);
}
}
};
module.exports = roleRepairbot;

View File

@ -11,11 +11,25 @@ var roleUpgrader = {
creep.memory.upgrading = true; creep.memory.upgrading = true;
creep.say('upgrading'); creep.say('upgrading');
} }
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return ( s.structureType == STRUCTURE_CONTAINER)
}
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
var storagepercent = usedstorage/mycapacity
if(creep.memory.upgrading) { if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { if(storagepercent > .3){
creep.moveTo(creep.room.controller); if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
} creep.moveTo(creep.room.controller);
}
}
} }
else { else {
var containers = creep.room.find(FIND_STRUCTURES, { var containers = creep.room.find(FIND_STRUCTURES, {