From 2ab594344400a1b0e744b1443430bb6354f6ba16 Mon Sep 17 00:00:00 2001 From: paradizelost Date: Fri, 23 Sep 2016 00:12:42 -0500 Subject: [PATCH] Re-Write for multi-room support --- NOTES.js | 7 ++ main.js | 167 +++++++++++++++++++++++--------------------- role.builder.js | 89 +++++++++-------------- role.hauler.js | 76 +++++++++++--------- role.hauler2.js | 29 ++++---- role.miner.js | 31 ++++---- role.miner2.js | 31 ++++---- role.recharger.js | 51 ++++++++++++++ role.repairbot.js | 64 ++++++++--------- role.upgrader.js | 91 +++++++++++------------- sourceprocessing.js | 19 +++++ tower.js | 29 +++----- 12 files changed, 372 insertions(+), 312 deletions(-) create mode 100644 NOTES.js create mode 100644 role.recharger.js create mode 100644 sourceprocessing.js diff --git a/NOTES.js b/NOTES.js new file mode 100644 index 0000000..0250635 --- /dev/null +++ b/NOTES.js @@ -0,0 +1,7 @@ +serializePos: function(pos) { + return pos.x + '_' + pos.y + '_' + pos.roomName; + }, + deserializePos: function(str) { + var ar = str.split('_'); + return new RoomPosition(ar[0], ar[1], ar[2]); + }, \ No newline at end of file diff --git a/main.js b/main.js index 41cf495..10c40a4 100644 --- a/main.js +++ b/main.js @@ -1,93 +1,102 @@ -var roleHarvester = require('role.harvester'); -var roleHarvester2 = require('role.harvester2'); -var roleUpgrader = require('role.upgrader'); -var roleBuilder = require('role.builder'); -var roleMiner = require('role.miner'); -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 roleBuilder=require('role.builder'); +var roleMiner=require('role.miner'); +var roleMiner2=require('role.miner2'); +var roleHauler=require('role.hauler') +var roleHauler2=require('role.hauler2') +var roleUpgrader=require('role.upgrader') +var roleTowerrecharger=require('role.recharger') +var runSources=require('sourceprocessing') +var roleRepairbot=require('role.repairbot') var runTower = require('tower'); -var roleTowerrecharger = require('role.towerrecharger'); -var purgetype="NONE" -var clearflags=0 module.exports.loop = function () { - if(purgetype != "NONE"){ _(Game.creeps).filter(c=>c.memory.role == purgetype).forEach(c=>c.suicide()).value() } - - runTower.tick(); - if(clearflags>0){ - console.log("Clearing Flags"); - _.forEach(Game.flags, function(flag) {flag.remove()}); - } - console.log("--------") - for(var name in Game.rooms) { - if(Game.rooms[name].memory.maxbuilders == undefined){ Game.rooms[name].memory.maxbuilders =4 } - if(Game.rooms[name].memory.maxupgraders == undefined){ Game.rooms[name].memory.maxupgraders = 3 } - if(Game.rooms[name].memory.maxminers == undefined){ Game.rooms[name].memory.maxminers = 1 } - if(Game.rooms[name].memory.maxminer2s == undefined){ Game.rooms[name].memory.maxminer2s = 1 } - if(Game.rooms[name].memory.maxhaulers == undefined){ Game.rooms[name].memory.maxhaulers = 2 } - if(Game.rooms[name].memory.maxhauler2s == undefined){ Game.rooms[name].memory.maxhauler2s = 1 } - if(Game.rooms[name].memory.maxwarriors == undefined){ Game.rooms[name].memory.maxwarriors = 2 } - if(Game.rooms[name].memory.maxtowerrechargers == undefined){ Game.rooms[name].memory.maxtowerrechargers = 3 } - if(Game.rooms[name].memory.maxharvesters == undefined){ Game.rooms[name].memory.maxharvesters = 0 } - if(Game.rooms[name].memory.maxharvester2s == undefined){ Game.rooms[name].memory.maxharvester2s = 0 } - - console.log('Room "'+name+'" has ' + Game.rooms[name].energyAvailable + ' energy'); - } for(var name in Memory.creeps) { if(!Game.creeps[name]) { delete Memory.creeps[name]; console.log('Clearing non-existing creep memory:', name); } } - - roleMiner.spawn() - roleMiner2.spawn() - roleHarvester.spawn() - roleHarvester2.spawn() - roleBuilder.spawn() - roleUpgrader.spawn() - roleHauler.spawn() - roleHauler2.spawn() - roleTowerrecharger.spawn() - roleWarrior.spawn() - roleRepairbot.spawn() - for(var name in Game.creeps) { - var creep = Game.creeps[name]; - if(creep.memory.role == 'harvester') { - roleHarvester.run(creep); + for(var name in Game.rooms) { + runTower.tick(name); + var roomenergy = Game.rooms[name].energyAvailable + var allstorage = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_STORAGE)}}) + var allcontainers = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER)}}) + var usedstorage = 0 + var mycapacity=0 + for(var i=0; i < allstorage.length;i++){ + usedstorage+=_.sum(allstorage[i].store) + mycapacity+=allstorage[i].storeCapacity } - if(creep.memory.role == 'harvester2') { - roleHarvester2.run(creep); + Game.rooms[name].memory.storagepercent = usedstorage/mycapacity + + var containerusedstorage = 0 + var containercapacity=0 + for(var i=0; i < allcontainers.length;i++){ + containerusedstorage+=_.sum(allcontainers[i].store) + containercapacity+=allcontainers[i].storeCapacity } - if(creep.memory.role == 'upgrader') { - roleUpgrader.run(creep); - } - if(creep.memory.role == 'builder') { - roleBuilder.run(creep); - } - if(creep.memory.role == 'miner') { - roleMiner.run(creep); - } - if(creep.memory.role == 'miner2') { - roleMiner2.run(creep); - } - if(creep.memory.role == 'hauler'){ - roleHauler.run(creep) - } - if(creep.memory.role == 'hauler2'){ - roleHauler2.run(creep) - } - if(creep.memory.role == 'warrior'){ - roleWarrior.run(creep) - } - if(creep.memory.role == 'towerrecharger'){ - roleTowerrecharger.run(creep) - } - if(creep.memory.role == 'repairbot'){ - roleRepairbot.run(creep) + Game.rooms[name].memory.containerstoragepercent = containerusedstorage/containercapacity + console.log('-------') + console.log('Room "'+name+'" has '+roomenergy+' energy : Using ' + usedstorage + ' of ' + mycapacity + ', ' + Game.rooms[name].memory.storagepercent * 100 + '% Storage, ' + containerusedstorage + ' of ' + containercapacity + ':' + Game.rooms[name].memory.containerstoragepercent *100+ '% in containers') + if(Game.rooms[name].memory.maxbuilders == undefined){ Game.rooms[name].memory.maxbuilders =0 } + if(Game.rooms[name].memory.maxupgraders == undefined){ Game.rooms[name].memory.maxupgraders = 0 } + if(Game.rooms[name].memory.maxminers == undefined){ Game.rooms[name].memory.maxminers = 0 } + if(Game.rooms[name].memory.maxminer2s == undefined){ Game.rooms[name].memory.maxminer2s = 0 } + if(Game.rooms[name].memory.maxhaulers == undefined){ Game.rooms[name].memory.maxhaulers = 0 } + if(Game.rooms[name].memory.maxhauler2s == undefined){ Game.rooms[name].memory.maxhauler2s = 0 } + if(Game.rooms[name].memory.maxwarriors == undefined){ Game.rooms[name].memory.maxwarriors = 0 } + if(Game.rooms[name].memory.maxtowerrechargers == undefined){ Game.rooms[name].memory.maxtowerrechargers = 0 } + if(Game.rooms[name].memory.maxharvesters == undefined){ Game.rooms[name].memory.maxharvesters = 0 } + if(Game.rooms[name].memory.maxharvester2s == undefined){ Game.rooms[name].memory.maxharvester2s = 0 } + if(Game.rooms[name].memory.maxrepairbots == undefined){ Game.rooms[name].memory.maxrepairbots = 0 } + if(Game.rooms[name].memory.minworkenergypct == undefined){ Game.rooms[name].memory.minworkenergypct = 0.3 } + if(Game.rooms[name].memory.builderparkx==undefined){Game.rooms[name].memory.builderparkx=24} + if(Game.rooms[name].memory.builderparky==undefined){Game.rooms[name].memory.builderparky=24} + if(Game.rooms[name].memory.hauler2parkx==undefined){Game.rooms[name].memory.hauler2parkx=24} + if(Game.rooms[name].memory.hauler2parky==undefined){Game.rooms[name].memory.hauler2parky=24} + if(Game.rooms[name].memory.upgraderparkx==undefined){Game.rooms[name].memory.upgraderparkx=24} + if(Game.rooms[name].memory.upgraderparky==undefined){Game.rooms[name].memory.upgraderparky=24} + runSources.tick(name) + roleBuilder.spawn(name) + roleMiner.spawn(name) + roleHauler.spawn(name) + roleHauler2.spawn(name) + roleUpgrader.spawn(name) + roleMiner2.spawn(name) + roleTowerrecharger.spawn(name) + roleRepairbot.spawn(name) + var mycreeps = Game.rooms[name].find(FIND_MY_CREEPS) + for(var mycreep in mycreeps){ + try{ + var creep = mycreeps[mycreep] + if(creep.memory.role == 'miner'){ + roleMiner.run(creep) + } + if(creep.memory.role == 'miner2'){ + roleMiner2.run(creep) + } + if(creep.memory.role == 'hauler'){ + roleHauler.run(creep) + } + if(creep.memory.role == 'hauler2'){ + roleHauler2.run(creep) + } + if(creep.memory.role == 'builder'){ + roleBuilder.run(creep) + } + if(creep.memory.role == 'upgrader'){ + roleUpgrader.run(creep) + } + if(creep.memory.role=='towerrecharger'){ + roleTowerrecharger.run(creep) + } + if(creep.memory.role == 'repairbot'){ + roleRepairbot.run(creep) + } + } catch (e) { + //console.log(e.toString) + } } } + console.log('-------') } \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index a2b5a4e..4ff46c8 100644 --- a/role.builder.js +++ b/role.builder.js @@ -1,27 +1,5 @@ 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 - } - var storagepercent = usedstorage/mycapacity - - var ttl = creep.ticksToLive - if(ttl < 300) { - console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role ) - } - if(ttl < 20) { - roleBuilder.spawn("true") - } + run: function(creep) { if(creep.memory.building && creep.carry.energy == 0) { creep.memory.building = false; creep.say('Gathering'); @@ -33,59 +11,58 @@ var roleBuilder = { if(creep.memory.building) { var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); + //Check next room as well + if(target != undefined) { - if(storagepercent > .4){ + if(creep.build(target) == ERR_NOT_IN_RANGE) { creep.moveTo(target); if(creep.fatigue<1){ creep.say("MTCS"); } else { creep.say("Tired")} - } - } else { - - creep.say("MTF") - creep.moveTo(Game.flags.Flag2); - } + } + + } else { + //console.log(creep.room.memory.builderpark) + creep.moveTo(creep.room.memory.builderparkx,creep.room.memory.builderparky,creep.room.roomName) } } else { var containers = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; - }}); - var allcontainers = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => {return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;}}); + var allcontainers = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER ) ; }}); var sources = creep.room.find(FIND_SOURCES); - var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); - - - if(allcontainers.length<1){ - if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) { + var droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); + if(containers==0){ + if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { creep.say("MTDE"); - creep.moveTo(droppedenergy[0]); - } - } else{ - if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(droppedenergy); + } + } else { + if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.say("MTSC"); creep.moveTo(containers[0]); } + } } - }, - spawn: function(dyingcreep="false"){ - - var myrole='builder'; - var nummyrole=4; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole || dyingcreep=="true" ) { - if(dyingcreep=="true"){console.log(myrole + " dying. preventative spawn")} - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='builder'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([WORK,WORK,CARRY,MOVE,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - } + } }; - module.exports = roleBuilder; \ No newline at end of file diff --git a/role.hauler.js b/role.hauler.js index f0cadae..902e6ef 100644 --- a/role.hauler.js +++ b/role.hauler.js @@ -1,7 +1,8 @@ var roleHauler = { - run: function(creep) { + run: function(creep) { if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling && creep.carry.energy == 0) { + creep.memory.destsource=undefined creep.memory.hauling = false; creep.say('gathering'); } @@ -9,51 +10,62 @@ var roleHauler = { creep.memory.hauling = true; creep.say('hauling'); } - var sources = creep.room.find(FIND_DROPPED_ENERGY ); + var sources = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); if(creep.memory.hauling==false){ - if(creep.pickup(sources[0]) == ERR_NOT_IN_RANGE && creep.carryCapacity > creep.carry.energy) { - creep.moveTo(sources[0]); - } + if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} + var mysource=Game.getObjectById(creep.memory.destsource.id) + + if(creep.pickup(mysource) == ERR_NOT_IN_RANGE && creep.carryCapacity > creep.carry.energy) { + creep.moveTo(mysource); + } }else { if(sources != undefined ) - { var spawntargets = creep.room.find(FIND_STRUCTURES, { + { var spawntargets = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity) } }); - var containertargets = creep.room.find(FIND_STRUCTURES, { + var containertargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { - return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE) && _.sum(structure.store) < structure.storeCapacity) ; + return ((structure.structureType == STRUCTURE_CONTAINER ) && _.sum(structure.store) < structure.storeCapacity) ; } }); - if(spawntargets.length > 0) { - if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(spawntargets[0]); + var storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_STORAGE ) && _.sum(structure.store) < structure.storeCapacity) ; + } + + }); + if(spawntargets) { + if(creep.transfer(spawntargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntargets); } - } else if (containertargets.length > 0) { - if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(containertargets[0]); + } else if (containertargets) { + if(creep.transfer(containertargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(containertargets); + } + } else { + if(creep.transfer(storagetargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(storagetargets); } - }else { - creep.say("") - creep.moveTo(Game.flags.Flag1); } - }else{ - creep.say("NS,NT") - creep.moveTo(Game.flags.Flag1); - } - } - }, - spawn: function(){ - var myrole='hauler'; - var nummyrole=2; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); } - } + } + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='hauler'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } + } }; module.exports = roleHauler; \ No newline at end of file diff --git a/role.hauler2.js b/role.hauler2.js index 0fb71a9..032c614 100644 --- a/role.hauler2.js +++ b/role.hauler2.js @@ -2,6 +2,7 @@ /** @param {Creep} creep **/ run: function(creep) { + if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling && creep.carry.energy == 0) { creep.memory.hauling = false; @@ -21,6 +22,7 @@ creep.say("MTSC"); creep.moveTo(container); } + } } else { var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { @@ -33,21 +35,24 @@ creep.moveTo(spawntarget); } } else { - creep.say('MTF') - creep.moveTo(Game.flags.Flag1); + creep.say('NTD'); + creep.moveTo(creep.room.memory.hauler2parkx,creep.room.memory.hauler2parky,creep.room.roomName) } } - } }, - spawn: function(){ - var myrole='hauler2'; - var nummyrole=1; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([MOVE,CARRY], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='hauler2'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - } + } }; module.exports = roleHauler2; \ No newline at end of file diff --git a/role.miner.js b/role.miner.js index f5c4fb8..579ec4e 100644 --- a/role.miner.js +++ b/role.miner.js @@ -1,24 +1,23 @@ var roleMiner = { - - /** @param {Creep} creep **/ - run: function(creep,mysource) { + run: function(creep) { var sources = creep.room.find(FIND_SOURCES); if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[0]); } - }, - spawn: function(){ - var myrole='miner'; - 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,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='miner'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - } - + } }; - module.exports = roleMiner; \ No newline at end of file diff --git a/role.miner2.js b/role.miner2.js index 1765a58..654638e 100644 --- a/role.miner2.js +++ b/role.miner2.js @@ -1,22 +1,23 @@ var roleMiner2 = { - - /** @param {Creep} creep **/ - run: function(creep) { + run: function(creep) { var sources = creep.room.find(FIND_SOURCES); if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[1]); + } + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='miner2'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - }, - spawn: function(){ - var myrole='miner2'; - 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,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } + } }; - module.exports = roleMiner2; \ No newline at end of file diff --git a/role.recharger.js b/role.recharger.js new file mode 100644 index 0000000..874ca32 --- /dev/null +++ b/role.recharger.js @@ -0,0 +1,51 @@ + var roleTowerrecharger = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.memory.recharging && creep.carry.energy == 0) { + creep.memory.recharging = false; + creep.say('refilling'); + } + if(!creep.memory.recharging && creep.carry.energy == creep.carryCapacity) { + creep.memory.recharging = true; + creep.say('building'); + } + + if(!creep.memory.recharging) { + var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 1000) ; + }}); + if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say("MTSC"); + creep.moveTo(container); + } + } else { + var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_TOWER && structure.energy < structure.energyCapacity) + } + }); + if(spawntarget != undefined) { + if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntarget); + } + } + } + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='towerrecharger'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } + } + }; + module.exports = roleTowerrecharger; \ No newline at end of file diff --git a/role.repairbot.js b/role.repairbot.js index e26bb9f..2ae3ee2 100644 --- a/role.repairbot.js +++ b/role.repairbot.js @@ -1,7 +1,7 @@ var roleRepairbot = { /** @param {Creep} creep **/ - run: function(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; @@ -21,42 +21,40 @@ var roleRepairbot = { 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)} + var importantstructures = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; + }}); + importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax) + if(importantstructures.length > 0){ + if(creep.repair(importantstructures[0]) == ERR_NOT_IN_RANGE){ + creep.moveTo(importantstructures[0]) } + } else { + var damagedstructures = creep.room.find(FIND_STRUCTURES,{filter: (s) => s.hits < s.hitsMax}); + damagedstructures = _.sortBy(damagedstructures, (s)=>s.hits / s.hitsMax) + if(damagedstructures.length>0){ + if(creep.repair(damagedstructures[0]) == ERR_NOT_IN_RANGE){ + creep.moveTo(damagedstructures[0]) + } + } + } } }, - 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); + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='repairbot'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - } + } }; diff --git a/role.upgrader.js b/role.upgrader.js index 70d8a9e..811008a 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -1,9 +1,8 @@ var roleUpgrader = { - - /** @param {Creep} creep **/ - run: function(creep) { - - if(creep.memory.upgrading && creep.carry.energy == 0) { + run: function(creep) { + + if(creep.memory.upgrading==undefined){creep.memory.upgrading=true} + if(creep.memory.upgrading && creep.carry.energy == 0) { creep.memory.upgrading = false; creep.say('harvesting'); } @@ -11,57 +10,47 @@ 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(storagepercent > .3){ - if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.moveTo(creep.room.controller); + if(creep.room.memory.containerstoragepercent>.6){ + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.say('MTRC') + creep.moveTo(creep.room.controller); } - } - } - else { - var containers = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; - }}); - var allcontainers = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER) ; - }}); - var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); - if(allcontainers.length<0){ + } else { creep.moveTo(creep.room.memory.upgraderparkx,creep.room.memory.upgraderparky,creep.room.roomName) } + } else { + + var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; + }}); + if(container){ + + if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say("MTSC"); + creep.moveTo(container); + } + }else{ + var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(droppedenergy[0]); - } - } else{ - if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(containers[0]); - } + creep.say("MTDE"); + creep.moveTo(droppedenergy[0]); + } } } - }, - spawn: function(){ - var myrole='upgrader'; - var nummyrole=3; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); + }, + spawn: function(roomname){ + var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + var myroom = Game.rooms[roomname] + for(var thisspawn in myspawns){ + var spawn = myspawns[thisspawn] + var myrole='upgrader'; + var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); + if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { + var newName = spawn.createCreep([MOVE,MOVE,WORK,WORK,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } } - } + } }; - module.exports = roleUpgrader; \ No newline at end of file diff --git a/sourceprocessing.js b/sourceprocessing.js new file mode 100644 index 0000000..fa28f9c --- /dev/null +++ b/sourceprocessing.js @@ -0,0 +1,19 @@ +var runSources = { + tick: function(roomname) { + var sources = Game.rooms[roomname].find(FIND_DROPPED_ENERGY ); + for(var source of sources){ + var allhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role=='hauler' ); + var unassignedhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource == undefined && creep.memory.role=='hauler')); + var assignedhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource != undefined && creep.memory.role=='hauler')); + var myhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource != undefined && creep.memory.destsource.id==source.id && creep.memory.role=='hauler')); + var sourcecount = sources.length + var persrc = allhaulers.length / sourcecount + if(myhaulers.length >=persrc){break;} else{ + if(unassignedhaulers.length > 0){ + unassignedhaulers[0].memory.destsource = source + } + } + } + } +}; +module.exports = runSources; \ No newline at end of file diff --git a/tower.js b/tower.js index 4871a14..6f97f49 100644 --- a/tower.js +++ b/tower.js @@ -1,16 +1,16 @@ var towers = { /** @param {Game} game **/ - tick: function() { + tick: function(roomname) { - towers = Game.spawns.Spawn1.room.find(FIND_MY_STRUCTURES, { + towers = Game.rooms[roomname].find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_TOWER } }) _.forEach(towers, function(tower){ var myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, { - filter: (structure) => structure.hits < structure.hitsMax + filter: (structure) => structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART && structure.hits < structure.hitsMax }); var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); @@ -19,28 +19,21 @@ var towers = { 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 - var importantstructures = tower.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; - }}); - importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax) if(tower.energy > tower.energyCapacity * .7 ){ + var importantstructures = tower.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; + }}); + importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax) if(importantstructures.length > 0){ tower.repair(importantstructures[0]); } else { if(closestDamagedStructure) { - if(storagepercent > .8){ - console.log("tower repairing. Currently at: " + storagepercent) + if(Game.rooms[roomname].memory.containerstoragepercent > .8){ + console.log("tower repairing. Currently at: " + Game.rooms[roomname].memory.containerstoragepercent) tower.repair(closestDamagedStructure); - } else { console.log("tower waiting for more storage. Currently at: " + storagepercent)} + } else { console.log("tower waiting for more storage. Currently at: " + Game.rooms[roomname].memory.containerstoragepercent)} } } var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);