From 5c5b9003adee66e24bd7ed47237fd74081442c50 Mon Sep 17 00:00:00 2001 From: Dan Hamik Date: Mon, 23 May 2022 15:59:00 -0500 Subject: [PATCH] mining/mover fixes --- Empire.js | 32 +++++ Room.Phase0.js | 4 +- Room.Phase1.js | 21 +++- main.js | 4 + proc.spawning.js | 6 +- role.mover.js | 181 +++++++++++++++------------- role.phase1worker.js | 277 ++++++++++++++++++++++--------------------- role.sourceminer.js | 32 ++--- 8 files changed, 312 insertions(+), 245 deletions(-) diff --git a/Empire.js b/Empire.js index 671e2fd..81f7a4f 100644 --- a/Empire.js +++ b/Empire.js @@ -164,5 +164,37 @@ module.exports = { } } return accessPoints; + }, + computeSpawnLevels: function(room){ + myroom=Game.rooms[room] + creepcounts=myroom.memory.creepcounts + let workerrolename = 'phase' + myroom.memory.phase +'worker' + if((myroom.memory.minablepositions >= 3 ||myroom.memory.minablepositions==undefined) && (creepcounts["mover"] == 0 || creepcounts["mover"]==undefined) ){ + if((((creepcounts[workerrolename]< (myroom.memory.minablepositions + 1) || creepcounts[workerrolename]==undefined) && myroom.energyAvailable >= myroom.energyCapacityAvailable) ) || ((creepcounts[workerrolename]==0 || creepcounts[workerrolename]==undefined ) && myroom.energyAvailable>100)) { + console.log('Spawning worker in ' + room) + } + } else { + if((((creepcounts['sourceminer']< myroom.find(FIND_SOURCES).length) || creepcounts['sourceminer']==undefined) ) || ((creepcounts['sourceminer']==0 || creepcounts['sourceminer']==undefined ) && myroom.energyAvailable>100)) { + console.log('Spawning sourceminer in ' + room) + } + if((((creepcounts[workerrolename]< (myroom.memory.minablepositions+1) || creepcounts[workerrolename]==undefined) && myroom.energyAvailable >= myroom.energyCapacityAvailable) ) || ((creepcounts[workerrolename]==0 || creepcounts[workerrolename]==undefined ) && myroom.energyAvailable>100)) { + console.log('Spawning worker in ' + room) + } + } + if((myroom.storage!=undefined || myroom.terminal!=undefined) && (creepcounts["mover"] < 2 || creepcounts["mover"]==undefined)){ + console.log("Spawning Mover in " + room) + } + if(((myroom.storage || myroom.terminal) && myroom.StructureExtractor) && (creepcounts["mineralmover"] < 1 || creepcounts["mineralmover"]==undefined)){ + console.log("Spawning MineralMover in " + room) + } + if((myroom.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_EXTRACTOR}}).length > 0)&&(creepcounts["miner"] < myroom.memory.mineralminablepositions || creepcounts["miner"]==undefined)&&(myroom.energyAvailable >= myroom.energyCapacityAvailable)){ + if(myroom.find(FIND_MINERALS)[0].ticksToRegeneration == undefined || myroom.find(FIND_MINERALS)[0].ticksToRegeneration < 1000 ){ + console.log("Spawning Miner in " + room) + } else { + if(Game.flags.debug && Game.flags.debug.room == myroom){ + console.log("Not spawning miner in " + room + ", waiting for regen") + } + } + } } }; \ No newline at end of file diff --git a/Room.Phase0.js b/Room.Phase0.js index 6c39877..28ad4e4 100644 --- a/Room.Phase0.js +++ b/Room.Phase0.js @@ -12,7 +12,7 @@ let Phase0 = { let newworkerrolename = 'phase' + Game.rooms[room].memory.phase + 'worker' for(let mycreep in creeps){ creep=creeps[mycreep] - if(creep.memory.role == workerrolename){ + if(creep.memory.role != newworkerrolename){ creep.memory.role=newworkerrolename } } @@ -30,7 +30,7 @@ let Phase0 = { require('proc.spawning').spawnworker(room) } } else { } - if(Game.rooms[room].controller.level>1){ + if(Game.rooms[room].controller.level>1 && Game.rooms[room].spawn){ Game.rooms[room].memory.phase++ let creeps=Game.rooms[room].find(FIND_MY_CREEPS) diff --git a/Room.Phase1.js b/Room.Phase1.js index ff38867..e5d969a 100644 --- a/Room.Phase1.js +++ b/Room.Phase1.js @@ -6,11 +6,25 @@ let Phase1 = { myroom.memory.creepcounts=creepcounts myroom.memory.movercount=creepcounts["mover"] let links=myroom.find(FIND_STRUCTURES, {filter: (s) =>{return ( s.structureType==STRUCTURE_LINK)}}) + let constructionSites=myroom.find(FIND_CONSTRUCTION_SITES) + if(constructionSites.length > 0){ + if(myroom.memory.currentconstruction!=undefined && myroom.memory.currentconstruction!=null){ + if(Game.getObjectById(myroom.memory.currentconstruction.id)){ + + } else { + myroom.memory.currentconstruction=constructionSites[0] + } + } else { + myroom.memory.currentconstruction=constructionSites[0] + } + } else { + console.log(myroom.name+" No constructions") + delete myroom.memory.currentconstruction + } if(Game.time % 10 === 0){ //console.log('processing spawn') let myspawns = myroom.find(FIND_MY_SPAWNS) - if(myspawns.length>0){ let myspawn = myspawns[0] let creepcount = myroom.find(FIND_MY_CREEPS).length @@ -45,7 +59,7 @@ let Phase1 = { require('proc.spawning').spawnworker(room) } } - if((myroom.storage || myroom.terminal) && (creepcounts["mover"] < 2 || creepcounts["mover"]==undefined)){ + if((myroom.storage!=undefined || myroom.terminal!=undefined) && (creepcounts["mover"] < 2 || creepcounts["mover"]==undefined)){ console.log("Spawning Mover in " + room) require('proc.spawning').spawnmover(room) } @@ -62,6 +76,7 @@ let Phase1 = { } } if(Game.time % 4 === 0){ + /* if(myroom.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TERMINAL}}).length > 0){ //console.log("starting market check for room " + room) try{ @@ -106,7 +121,7 @@ let Phase1 = { console.log("Myroom" + myroom) console.log(e) } - } + }*/ } /* if(links!=null && links!=undefined){ diff --git a/main.js b/main.js index 6320127..4cbf2ab 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,11 @@ module.exports.loop = function () { } if(myroom.controller!==undefined){ myroom.memory.tickssofar++ + try{ require('Room.Phase' + myroom.memory.phase).run(name) + } catch(e){ + console.log(e) + } } for(let pos in myroom.memory.maphits){ diff --git a/proc.spawning.js b/proc.spawning.js index e7bb31f..c54a9c6 100644 --- a/proc.spawning.js +++ b/proc.spawning.js @@ -31,11 +31,7 @@ let buildparts = { if(!spawn.spawning){ let tobuild=[] for(let totry of [ - [WORK,WORK,WORK,WORK,WORK,MOVE], - [WORK,WORK,WORK,WORK,MOVE], - [WORK,WORK,WORK,MOVE], - [WORK,WORK,MOVE], - [WORK,MOVE] + [WORK,WORK,WORK,WORK,WORK,MOVE] ]){ let ispossible = spawn.canCreateCreep(totry) if(ispossible==OK){ diff --git a/role.mover.js b/role.mover.js index eb943a7..acfe09a 100644 --- a/role.mover.js +++ b/role.mover.js @@ -8,6 +8,9 @@ let mover={ if(creep.memory.working==undefined){ creep.memory.working=false } + if(creep.memory.assignedroom==undefined){ + creep.memory.assignedroom=creep.room.name + } let lastpos = _.create(RoomPosition.prototype, creep.memory.lastpos) if(creep.pos.isEqualTo(lastpos)){ creep.memory.timeatpos = creep.memory.timeatpos+1 @@ -25,100 +28,108 @@ let mover={ creep.memory.working = true; creep.say('working'); } - if(creep.memory.working){ - //let terminaltarget = creep.room.terminal - let spawntarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_LAB].includes(s.structureType)) && s.energy < s.energyCapacity ))}}) - if(spawntarget){ - creep.say('ref sp') - if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(spawntarget,{ignoreCreeps:ignorecreeps}) - } - } else { - try{ - creep.say('finding storage') - let mystorage = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER) && s.store.getFreeCapacity()>0 ) ;}}); - creep.say('got storage') - if(creep.transfer(mystorage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.say('MvStor') - creep.moveTo(mystorage,{ignoreCreeps:ignorecreeps}) - } else { - creep.say("Dumped") - } - } catch(e){ - console.log(e) - } - } - /*else if(terminaltarget != creep.memory.pulledenergyfrom){ - if(creep.transfer(terminaltarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE ) { - creep.say('Putting Energy') - creep.travelTo(terminaltarget); - } - }*/ - } else if ((creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}})) ) { - if(creep.memory.renewto == undefined){ - creep.memory.renewto = 1200 - } else { - if(creep.ticksToLive >= creep.memory.renewto){ - delete creep.memory.renewto - } - } - //console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto) - creep.say('renewing') - let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS) - if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE) - { - creep.moveTo(spawn); - } + if(creep.room.name != creep.memory.assignedroom){ + creep.moveTo(Game.rooms[creep.memory.assignedroom].controller) + Game.notify(creep.name + " Not in assigned room") } else { - if(filllevel < creep.carryCapacity){ - let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER) && s.store.getUsedCapacity('energy') >= 1000) ;}}); - let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>10)}}); - let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}}); - let ruins=creep.pos.findClosestByRange(FIND_RUINS, {filter: ruin => ruin.store.getUsedCapacity(RESOURCE_ENERGY) > 0}); - console.log(creep.room.name + " " + creep.name + " " + creep.room.energyAvailable+":"+ creep.room.energyCapacityAvailable ) - if(creep.room.energyAvailable < creep.room.energyCapacityAvailable || ((droppedenergy == undefined) && (tombstone==undefined) && (ruins==undefined))){ - - if(storagetarget===undefined||storagetarget==null){ - creep.say('finding storage') - storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE) && s.store.getUsedCapacity('energy') >= 10) ;}}); - if((storagetarget==undefined ||storagetarget==null) && creep.room.terminal){ - storagetarget=creep.room.terminal - } - } - if(creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { - creep.say('GEST') - creep.travelTo(storagetarget); + if(creep.memory.working){ + //let terminaltarget = creep.room.terminal + let spawntarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_LAB].includes(s.structureType)) && s.energy < s.energyCapacity ))}}) + if(spawntarget){ + creep.say('ref sp') + if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntarget,{ignoreCreeps:ignorecreeps}) } } else { - if(ruins){ - if(creep.withdraw(ruins,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTRU"); - } - creep.moveTo(ruins,{ignoreCreeps:ignorecreeps}) - } - }else if(droppedenergy){ - if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTDE"); - } - creep.moveTo(droppedenergy,{ignoreCreeps:ignorecreeps}) - } - } else { - if(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTTS"); - } - creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps}) + try{ + creep.say('finding storage') + let mystorage = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER) && s.store.getFreeCapacity()>0 ) ;}}); + creep.say('got storage') + if(creep.transfer(mystorage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say('MvStor') + creep.moveTo(mystorage,{ignoreCreeps:ignorecreeps}) + } else { + creep.say("Dumped") } + } catch(e){ + console.log(e) } } - if(!storagetarget && (creep.carry > 0)){ - creep.memory.working=true + /*else if(terminaltarget != creep.memory.pulledenergyfrom){ + if(creep.transfer(terminaltarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE ) { + creep.say('Putting Energy') + creep.travelTo(terminaltarget); + } + }*/ + } else if ((creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}})) ) { + if(creep.memory.renewto == undefined){ + creep.memory.renewto = 1200 + } else { + if(creep.ticksToLive >= creep.memory.renewto){ + delete creep.memory.renewto + } + } + //console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto) + creep.say('renewing') + let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS) + if(creep.store.getUsedCapacity('energy') > 0){ + creep.transfer(spawn,RESOURCE_ENERGY) + } + if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE) + { + creep.moveTo(spawn); + } + } else { + if(filllevel < creep.carryCapacity){ + let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER) && s.store.getUsedCapacity('energy') >= 200) ;}}); + let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>10)}}); + let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}}); + let ruins=creep.pos.findClosestByRange(FIND_RUINS, {filter: ruin => ruin.store.getUsedCapacity(RESOURCE_ENERGY) > 0}); + console.log(creep.room.name + " " + creep.name + " " + creep.room.energyAvailable+":"+ creep.room.energyCapacityAvailable ) + if(creep.room.energyAvailable < creep.room.energyCapacityAvailable || ((droppedenergy == undefined) && (tombstone==undefined) && (ruins==undefined))){ + + if(storagetarget===undefined||storagetarget==null){ + creep.say('finding storage') + storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE) && s.store.getUsedCapacity('energy') >= 10) ;}}); + if((storagetarget==undefined ||storagetarget==null) && creep.room.terminal){ + storagetarget=creep.room.terminal + } + } + if(creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { + creep.say('GEST') + creep.travelTo(storagetarget); + } + } else { + if(ruins){ + if(creep.withdraw(ruins,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTRU"); + } + creep.moveTo(ruins,{ignoreCreeps:ignorecreeps}) + } + }else if(droppedenergy){ + if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTDE"); + } + creep.moveTo(droppedenergy,{ignoreCreeps:ignorecreeps}) + } + } else { + if(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTTS"); + } + creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps}) + } + } + } + if(!storagetarget && (creep.carry > 0)){ + creep.memory.working=true + } } } + creep.memory.lastpos=creep.pos } - creep.memory.lastpos=creep.pos } } module.exports = mover; \ No newline at end of file diff --git a/role.phase1worker.js b/role.phase1worker.js index a996052..9d10a67 100644 --- a/role.phase1worker.js +++ b/role.phase1worker.js @@ -21,162 +21,171 @@ let Phase1Worker = { let road = creep.pos.lookFor(LOOK_STRUCTURES); let filllevel = _.sum(creep.carry) - if(creep.memory.working && filllevel == 0) { - creep.memory.working = false; - creep.say('Gathering'); - } - if(!creep.memory.working && creep.carry.energy == creep.carryCapacity) { - creep.memory.working = true; - creep.say('working'); - } - - - if(creep.memory.working){ - let look=creep.pos.lookFor(LOOK_STRUCTURES) - let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_TERMINAL ) && _.sum(s.store) < s.store.getCapacity()) ;}}); - if(creep.room.memory.NeedsRecharge==1){ - if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.say(creep.room.controller.ticksToDowngrade + " of " + CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2) - creep.moveTo(creep.room.controller) - } - } else if((creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER,STRUCTURE_LINK].includes(s.structureType)) && s.energy < s.energyCapacity))}}))&& (creep.room.memory.movercount<1||creep.room.memory.movercount==undefined)){ - let spawntarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_TOWER) && s.energy < s.energyCapacity))}}); - if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(spawntarget,{ignoreCreeps:ignorecreeps}) - } - } else if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_RAMPART].includes(s.structureType)) && s.hits < 20000))}})){ - let ramparttarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_RAMPART) && s.hits < s.hitsMax))}}); - if(creep.repair(ramparttarget) == ERR_NOT_IN_RANGE) { - creep.moveTo(ramparttarget,{ignoreCreeps:ignorecreeps}) - } - } else if(creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)){ - target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES) - if(creep.build(target) == ERR_NOT_IN_RANGE) { - if (road.length > 0) {creep.repair(road);} - creep.moveTo(target,{ignoreCreeps:ignorecreeps}) - } - } else if(storagetargets && creep.room.storage&& creep.room.controller.level*125000> creep.room.storage.store.getUsedCapacity('energy')){ - if(creep.transfer(storagetargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - if (road.length > 0) {creep.repair(road);} - creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps}) - } - }else { - if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.say('MTRC') - if (road.length > 0) {creep.repair(road);} - creep.moveTo(Game.rooms[creep.memory.assignedroom].controller,{ignoreCreeps:ignorecreeps}) - } + if(creep.room.name == creep.memory.assignedroom){ + if(creep.memory.working && filllevel == 0) { + creep.memory.working = false; + creep.say('Gathering'); } - } else if (creep.pos.findClosestByRange(FIND_MY_SPAWNS) && (creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}})) ) { - if(creep.memory.renewto == undefined){ - creep.memory.renewto = 1200 - } else { - if(creep.ticksToLive >= creep.memory.renewto){ - delete creep.memory.renewto - } + if(!creep.memory.working && creep.carry.energy == creep.carryCapacity) { + creep.memory.working = true; + creep.say('working'); } - creep.say('renewing') - console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto) - let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS) - if(spawn != undefined && spawn!= null){ - if(creep.room.memory.NeedsRecharge==1 && creep.carry.energy>0){ + if(creep.memory.working){ + let look=creep.pos.lookFor(LOOK_STRUCTURES) + let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_TERMINAL ) && _.sum(s.store) < s.store.getCapacity()) ;}}); + if(creep.room.memory.NeedsRecharge==1){ if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.say(creep.room.controller.ticksToDowngrade + " of " + (CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2)) - creep.moveTo(creep.room.controller) + creep.say(creep.room.controller.ticksToDowngrade + " of " + CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2) + creep.moveTo(creep.room.controller) + } + } else if((creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER,STRUCTURE_LINK].includes(s.structureType)) && s.energy < s.energyCapacity))}}))&& (creep.room.memory.movercount<1||creep.room.memory.movercount==undefined)){ + let spawntarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_TOWER) && s.energy < s.energyCapacity))}}); + if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntarget,{ignoreCreeps:ignorecreeps}) + } + } else if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_RAMPART].includes(s.structureType)) && s.hits < 20000))}})){ + let ramparttarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_RAMPART) && s.hits < s.hitsMax))}}); + if(creep.repair(ramparttarget) == ERR_NOT_IN_RANGE) { + creep.moveTo(ramparttarget,{ignoreCreeps:ignorecreeps}) + } + } else if(creep.room.memory.currentconstruction){ + target = Game.getObjectById(creep.room.memory.currentconstruction.id) + creep.moveTo(target,{ignoreCreeps:ignorecreeps}) + if(creep.build(target) == ERR_NOT_IN_RANGE) { + if (road.length > 0) {creep.repair(road);} + creep.moveTo(target,{ignoreCreeps:ignorecreeps}) + } + } else if(storagetargets && creep.room.storage&& creep.room.controller.level*125000> creep.room.storage.store.getUsedCapacity('energy')){ + if(creep.transfer(storagetargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if (road.length > 0) {creep.repair(road);} + creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps}) + } + }else { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.say('MTRC') + if (road.length > 0) {creep.repair(road);} + creep.moveTo(Game.rooms[creep.memory.assignedroom].controller,{ignoreCreeps:ignorecreeps}) + } + } + } else if (creep.pos.findClosestByRange(FIND_MY_SPAWNS) && (creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}})) ) { + if(creep.memory.renewto == undefined){ + creep.memory.renewto = 1200 + } else { + if(creep.ticksToLive >= creep.memory.renewto){ + delete creep.memory.renewto + } + } + creep.say('renewing') + console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto) + let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS) + if(spawn != undefined && spawn!= null){ + if(creep.room.memory.NeedsRecharge==1 && creep.carry.energy>0){ + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.say(creep.room.controller.ticksToDowngrade + " of " + (CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2)) + creep.moveTo(creep.room.controller) + } + } else { + creep.transfer(spawn, RESOURCE_ENERGY) + if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE){ + creep.say('MVSP') + creep.moveTo(spawn); + } } } else { - - if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE){ - creep.say('MVSP') - creep.moveTo(spawn); - } + creep.memory.renewto=1 } } else { - creep.memory.renewto=1 - } - } else { - let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>200)}}); - let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}}); - let ruins=creep.pos.findClosestByRange(FIND_RUINS, {filter: ruin => ruin.store.getUsedCapacity(RESOURCE_ENERGY) > 0}); - let spawn= creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_SPAWN)}}); - //console.log(tombstone) - if((droppedenergy == undefined || droppedenergy==null) && (tombstone==undefined || tombstone==null) && (spawn!=undefined || spawn!=null) && (ruins==undefined || ruins==null)){ - creep.say("1") - if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} - let mysource=Game.getObjectById(creep.memory.destsource.id) - try{ - //console.log(creep.room.memory.creepcounts) - if((mysource.energy==0 || ((creep.room.memory.creepcounts.sourceminer!=null ||creep.room.memory.creepcounts.sourceminer!=undefined )))){ - creep.say("2.1") - //creep.say("No Energy Left to get") - let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_LINK) && s.store.getUsedCapacity('energy') >= 100) ;}}); - if(storagetarget===undefined || storagetarget==null){ - creep.say('2.2') - storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE) && s.store.getUsedCapacity('energy') >= creep.room.controller.level * 125000) ;}}); - if((storagetarget===undefined || storagetarget==null) && creep.room.terminal && creep.room.terminal.store.getUsedCapacity('energy') > 100000){ - creep.say('2.3') - storagetarget=creep.room.terminal + let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>200)}}); + let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}}); + let ruins=creep.pos.findClosestByRange(FIND_RUINS, {filter: ruin => ruin.store.getUsedCapacity(RESOURCE_ENERGY) > 0}); + let spawn= creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_SPAWN)}}); + //console.log(tombstone) + if((droppedenergy == undefined || droppedenergy==null) && (tombstone==undefined || tombstone==null) && (spawn!=undefined || spawn!=null) && (ruins==undefined || ruins==null)){ + creep.say("1") + if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} + let mysource=Game.getObjectById(creep.memory.destsource.id) + try{ + //console.log(creep.room.memory.creepcounts) + if((mysource.energy==0 || ((creep.room.memory.creepcounts.sourceminer!=null ||creep.room.memory.creepcounts.sourceminer!=undefined )))){ + creep.say("2.1") + //creep.say("No Energy Left to get") + let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_LINK) && s.store.getUsedCapacity('energy') >= 100) ;}}); + if(storagetarget===undefined || storagetarget==null){ + creep.say('2.2') + storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE) && s.store.getUsedCapacity('energy') >= creep.room.controller.level * 125000) ;}}); + if((storagetarget===undefined || storagetarget==null) && creep.room.terminal && creep.room.terminal.store.getUsedCapacity('energy') > 100000){ + creep.say('2.3') + storagetarget=creep.room.terminal + } } - } - //console.log(Game.getObjectById(storagetarget.id)) - if((storagetarget != undefined || storagetarget != null)){ - if( creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE){ - creep.say('3') - //creep.say('Getting Energy') - creep.travelTo(storagetarget); + //console.log(Game.getObjectById(storagetarget.id)) + if((storagetarget != undefined || storagetarget != null)){ + if( creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE){ + creep.say('3') + //creep.say('Getting Energy') + creep.travelTo(storagetarget); + } + } else { + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){ + creep.moveTo(mysource,{ignoreCreeps:ignorecreeps}) + } + creep.say("NSE-Ming") } - } else { + }else { if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){ creep.moveTo(mysource,{ignoreCreeps:ignorecreeps}) } - creep.say("NSE-Ming") - } - }else { - if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){ - creep.moveTo(mysource,{ignoreCreeps:ignorecreeps}) } + } catch(e){ + console.log(e) } - } catch(e){ - console.log(e) - } - } else { - if(droppedenergy){ - if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTDE"); + } else { + if(droppedenergy){ + if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTDE"); + } + creep.moveTo(droppedenergy,{ignoreCreeps:ignorecreeps}) } - creep.moveTo(droppedenergy,{ignoreCreeps:ignorecreeps}) - } - } else if (tombstone) { - if(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTTS"); - } - creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps}) - } - } else if (ruins){ - if(creep.withdraw(ruins,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTTS"); - } - creep.moveTo(ruins,{ignoreCreeps:ignorecreeps}) - } - } else if(spawn==null || spawn==undefined){ - storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE)) ;}}); - if(storagetarget){ - if(creep.withdraw(storagetarget,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + } else if (tombstone) { + if(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { if(global.verbosity>0){ creep.say("MTTS"); } - creep.moveTo(storagetarget,{ignoreCreeps:ignorecreeps}) + creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps}) } + } else if (ruins){ + if(creep.withdraw(ruins,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTTS"); + } + creep.moveTo(ruins,{ignoreCreeps:ignorecreeps}) + } + } else if(spawn==null || spawn==undefined){ + storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE)) ;}}); + if(storagetarget){ + if(creep.withdraw(storagetarget,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTTS"); + } + creep.moveTo(storagetarget,{ignoreCreeps:ignorecreeps}) + } + } else { + let mysource=Game.getObjectById(creep.memory.destsource.id) + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){ + creep.moveTo(mysource,{ignoreCreeps:ignorecreeps}) + } + } + + } else { + creep.say("IDK") } - } else { - creep.say("IDK") } } + creep.memory.lastpos=creep.pos + } else { + creep.moveTo(Game.rooms[creep.memory.assignedroom].controller) } - creep.memory.lastpos=creep.pos - } + } }; module.exports = Phase1Worker; diff --git a/role.sourceminer.js b/role.sourceminer.js index 27388ec..d31c8ed 100644 --- a/role.sourceminer.js +++ b/role.sourceminer.js @@ -29,23 +29,23 @@ let rolesourceMiner = { } else{ try{ creep.say('1') - destcontainer=Game.getObjectById(creep.memory.destsource.id).pos.findInRange(FIND_STRUCTURES,1,{filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER && s.store.getFreeCapacity() > 0) ;}}) - - if(destcontainer == undefined || destcontainer == null){ - creep.say('2') - console.log(creep.name + " " + destcontainer[0].id) - creep.travelTo(destcontainer[0]) - if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) { - //let mycontainer = creep.find(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER) ;}}); - creep.travelTo(destcontainer[0],{ignoreCreeps:ignorecreeps}); + let destcontainer=Game.getObjectById(creep.memory.destsource.id).pos.findInRange(FIND_STRUCTURES,1,{filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER && s.store.getFreeCapacity() > 0) ;}}) + //console.log(creep.room.name + " " + destcontainer) + if(destcontainer != undefined || destcontainer != null){ + creep.say('2') + console.log(creep.name + " " + destcontainer[0].id) + creep.travelTo(destcontainer[0]) + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE || creep.harvest(mysource)==ERR_NOT_ENOUGH_RESOURCES) { + //let mycontainer = creep.find(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER) ;}}); + creep.travelTo(destcontainer[0],{ignoreCreeps:ignorecreeps}); + } + } else { + creep.say('3') + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE|| creep.harvest(mysource)==ERR_NOT_ENOUGH_RESOURCES) { + //let mycontainer = creep.find(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER) ;}}); + creep.travelTo(mysource,{ignoreCreeps:ignorecreeps}); + } } - } else { - creep.say('3') - if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) { - //let mycontainer = creep.find(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER) ;}}); - creep.travelTo(mysource,{ignoreCreeps:ignorecreeps}); - } - } } catch(e){ console.log(e) }