diff --git a/main.js b/main.js index f01e025..41cf495 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,7 @@ var roleMiner2 = require('role.miner2'); var roleHauler = require('role.hauler'); var roleHauler2 = require('role.hauler2'); var roleWarrior = require('role.warrior'); +var roleRepairbot = require('role.repairbot') var runTower = require('tower'); var roleTowerrecharger = require('role.towerrecharger'); var purgetype="NONE" @@ -52,6 +53,7 @@ module.exports.loop = function () { roleHauler2.spawn() roleTowerrecharger.spawn() roleWarrior.spawn() + roleRepairbot.spawn() for(var name in Game.creeps) { var creep = Game.creeps[name]; if(creep.memory.role == 'harvester') { @@ -84,5 +86,8 @@ module.exports.loop = function () { if(creep.memory.role == 'towerrecharger'){ roleTowerrecharger.run(creep) } + if(creep.memory.role == 'repairbot'){ + roleRepairbot.run(creep) + } } } \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index 4b36d2b..e7c2129 100644 --- a/role.builder.js +++ b/role.builder.js @@ -2,7 +2,21 @@ var roleBuilder = { /** @param {Creep} creep **/ 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 if(ttl < 300) { console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role ) @@ -22,6 +36,7 @@ var roleBuilder = { if(creep.memory.building) { var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); if(target != undefined) { + if(storagepercent > .4){ if(creep.build(target) == ERR_NOT_IN_RANGE) { creep.moveTo(target); if(creep.fatigue<1){ @@ -33,6 +48,7 @@ var roleBuilder = { creep.say("MTF") creep.moveTo(Game.flags.Flag2); } + } } else { var containers = creep.room.find(FIND_STRUCTURES, { diff --git a/role.repairbot.js b/role.repairbot.js new file mode 100644 index 0000000..594cc73 --- /dev/null +++ b/role.repairbot.js @@ -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; \ No newline at end of file diff --git a/role.upgrader.js b/role.upgrader.js index 3933600..e83fafe 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -11,11 +11,25 @@ var roleUpgrader = { creep.memory.upgrading = true; 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.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.moveTo(creep.room.controller); - } + if(storagepercent > .3){ + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller); + } + } } else { var containers = creep.room.find(FIND_STRUCTURES, {