diff --git a/Empire.js b/Empire.js index 3a1ca5b..a408a34 100644 --- a/Empire.js +++ b/Empire.js @@ -53,6 +53,13 @@ module.exports = { } return 'Spawning initiated' }, + spawnminer: function(){ + for(let myroom of _.filter(Game.rooms, 'controller.my')) { + let name = myroom.name + require('proc.spawning').spawnminer(name) + } + return 'Spawning initiated' + }, spawnclaimer: function(){ for(let myroom of _.filter(Game.rooms, 'controller.my')) { let name = myroom.name diff --git a/Room.Phase1.js b/Room.Phase1.js index cf1f080..a418ad8 100644 --- a/Room.Phase1.js +++ b/Room.Phase1.js @@ -8,6 +8,7 @@ let Phase1 = { let myspawn = myspawns[0] let creepcount = Game.rooms[room].find(FIND_MY_CREEPS).length creepcounts = _.countBy(Game.rooms[room].find(FIND_MY_CREEPS), c => c.memory.role) + Game.rooms[room].memory.movercount=creepcounts["mover"] let workerrolename = 'phase' + Game.rooms[room].memory.phase +'worker' let sources = Game.rooms[room].find(FIND_SOURCES ) if(Game.rooms[room].controller.ticksToDowngrade < CONTROLLER_DOWNGRADE[Game.rooms[room].controller.level] * .2 ){ @@ -24,14 +25,18 @@ let Phase1 = { console.log(creepcounts[workerrolename]) console.log(Game.rooms[room].energyAvailable + " of " + Game.rooms[room].energyCapacityAvailable) } - if((((creepcounts[workerrolename]< (sources.length * 3) || creepcounts[workerrolename]==undefined) && Game.rooms[room].energyAvailable >= Game.rooms[room].energyCapacityAvailable) ) || ((creepcounts[workerrolename]==0 || creepcounts[workerrolename]==undefined ) && Game.rooms[room].energyAvailable>100)) { + console.log('Spawning worker in ' + room) require('proc.spawning').spawnworker(room) } - if(Game.rooms[room].storage && Game.rooms[room].terminal && (creepcounts["mover"] < 1 || creepcounts["mover"]==undefined) && Game.rooms[room].energyAvailable >= Game.rooms[room].energyCapacityAvailable){ - console.log("Spawning Mover") + if((Game.rooms[room].storage || Game.rooms[room].terminal) && (creepcounts["mover"] < 2 || creepcounts["mover"]==undefined)){ + console.log("Spawning Mover in " + room) require('proc.spawning').spawnmover(room) } + if(Game.rooms[room].find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_EXTRACTOR}}) && (creepcounts["miner"] < 1 || creepcounts["miner"]==undefined) && Game.rooms[room].energyAvailable >= Game.rooms[room].energyCapacityAvailable){ + console.log("Spawning Miner in " + room) + require('proc.spawning').spawnminer(room) + } //require('proc.market').sellEnergy(room) } } diff --git a/RoomInit.js b/RoomInit.js index 7e5a1e8..6672e0a 100644 --- a/RoomInit.js +++ b/RoomInit.js @@ -9,6 +9,23 @@ let initroom = { Game.rooms[name].memory.hasbeeninited=1 Game.rooms[name].memory.tickssofar=0 Game.rooms[name].memory.maphits={} + //Game.rooms[name].memory.minablepositions= this.checkminablepositions(name) + }, + checkminablepositions: function(name){ + Source.prototype.analyzeFreeSpaces = function() { + let x = this.pos.x; + let y = this.pos.y; + let walkable = this.room.lookForAtArea( + LOOK_TERRAIN, + y - 1, // top + x - 1, // left + y + 1, // bottom + x + 1, // right + true // asArray + ).filter(o => o[LOOK_TERRAIN] !== 'wall'); + return walkable.length; + }; + } } module.exports = initroom \ No newline at end of file diff --git a/proc.spawning.js b/proc.spawning.js index 86c5e10..72a5539 100644 --- a/proc.spawning.js +++ b/proc.spawning.js @@ -2,15 +2,15 @@ let buildparts = { spawnminer: function(roomname) { let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) Game.rooms[roomname].memory.CreepNum++ - let CreepName = roomname + "_" + (Game.rooms[roomname].memory.CreepNum) + let CreepName = 'miner_' + roomname + "_" + (Game.rooms[roomname].memory.CreepNum) for(let spawn of myspawns){ 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,WORK,WORK,WORK,CARRY,MOVE], + [WORK,WORK,WORK,CARRY,MOVE], + [WORK,WORK,CARRY,MOVE], + [WORK,CARRY,MOVE], [WORK,MOVE] ]){ let ispossible = spawn.canCreateCreep(totry) @@ -26,7 +26,7 @@ let buildparts = { spawnwarrior: function(roomname) { let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) Game.rooms[roomname].memory.CreepNum++ - let CreepName = roomname + "_" + (Game.rooms[roomname].memory.CreepNum) + let CreepName = 'warrior_' + roomname + "_" + (Game.rooms[roomname].memory.CreepNum) for(let spawn of myspawns){ if(!spawn.spawning){ let tobuild=[] @@ -50,7 +50,7 @@ let buildparts = { spawnworker: function(roomname) { let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) Game.rooms[roomname].memory.CreepNum++ - let CreepName = roomname + "_" + (Game.rooms[roomname].memory.CreepNum) + let CreepName = 'worker_' + roomname + "_" + (Game.rooms[roomname].memory.CreepNum) for(let spawn of myspawns){ if(!spawn.spawning){ for(let totry of [ @@ -77,7 +77,7 @@ let buildparts = { spawnmover: function(roomname) { let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) Game.rooms[roomname].memory.CreepNum++ - let CreepName = roomname + "_" + (Game.rooms[roomname].memory.CreepNum) + let CreepName = 'Mover_' + roomname + "_" + (Game.rooms[roomname].memory.CreepNum) for(let spawn of myspawns){ if(!spawn.spawning){ for(let totry of [ diff --git a/role.miner.js b/role.miner.js index 47d5056..ecd7fc7 100644 --- a/role.miner.js +++ b/role.miner.js @@ -1,12 +1,38 @@ let roleMiner = { run: function(creep) { - if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} - let mysource=Game.getObjectById(creep.memory.destsource.id) - { - if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) { - creep.travelTo(mysource); - } - } - } + //if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} + let ignorecreeps=true + let filllevel = _.sum(creep.carry) + if(creep.memory.working && filllevel == 0) { + creep.memory.working = false; + creep.say('Gathering'); + } + if(!creep.memory.working && _.sum(creep.carry) == creep.carryCapacity) { + creep.memory.working = true; + creep.say('working'); + } + if(!creep.memory.working){ + let mysource=Game.rooms[creep.room.name].find(FIND_MINERALS)[0] + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) { + creep.travelTo(mysource); + } + } else { + let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_TERMINAL ) && _.sum(s.store) < s.storeCapacity) ;}}); + if(storagetargets){ + if(this.transferAll(creep,storagetargets) == ERR_NOT_IN_RANGE) { + creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps}) + } + } + } + }, + transferAll: function(creep,targetStorage){ + let result = 0; + for(mat in creep.store) + { + let tempResult = creep.transfer(targetStorage,mat); + if(tempResult !== ERR_INVALID_ARGS) {result = tempResult;} + } + return result; + } }; module.exports = roleMiner; \ No newline at end of file diff --git a/role.mover.js b/role.mover.js index 7945f7f..617526e 100644 --- a/role.mover.js +++ b/role.mover.js @@ -5,6 +5,9 @@ let mover={ creep.memory.lastpos = creep.pos creep.memory.timeatpos = 0 } + if(creep.memory.working==undefined){ + creep.memory.working=false + } let lastpos = _.create(RoomPosition.prototype, creep.memory.lastpos) if(creep.pos.isEqualTo(lastpos)){ creep.memory.timeatpos = creep.memory.timeatpos+1 @@ -24,13 +27,18 @@ let mover={ } if(creep.memory.working){ let terminaltarget = creep.room.terminal - if(creep.transfer(terminaltarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { - creep.say('Putting Energy') - creep.travelTo(terminaltarget); + if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER].includes(s.structureType)) && s.energy < s.energyCapacity))}})){ + 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.transfer(terminaltarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { + creep.say('Putting Energy') + creep.travelTo(terminaltarget); + } } else { if(filllevel < creep.carryCapacity){ - let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER ) && _.sum(s.store) > 5000) ;}}); + let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER ) && _.sum(s.store) >= 500) ;}}); if(creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { creep.say('Getting Energy') creep.travelTo(storagetarget); diff --git a/role.phase1worker.js b/role.phase1worker.js index 53a8103..c947ce2 100644 --- a/role.phase1worker.js +++ b/role.phase1worker.js @@ -28,7 +28,9 @@ let Phase1Worker = { 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_CONTAINER || s.structureType == STRUCTURE_TERMINAL ) && _.sum(s.store) < s.storeCapacity) ;}}); @@ -38,7 +40,7 @@ let Phase1Worker = { creep.say(creep.room.controller.ticksToDowngrade) creep.moveTo(creep.room.controller) } - } else if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER].includes(s.structureType)) && s.energy < s.energyCapacity))}})){ + } else if((creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER].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}) @@ -68,21 +70,30 @@ let Phase1Worker = { } } else { let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>200)}}); - if(droppedenergy == undefined){ + let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}}); + //console.log(tombstones) + if((droppedenergy == undefined) && (tombstone==undefined)){ if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} let mysource=Game.getObjectById(creep.memory.destsource.id) if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){ creep.moveTo(mysource,{ignoreCreeps:ignorecreeps}) } } else { - if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { - if(global.verbosity>0){ - creep.say("MTDE"); + 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(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + if(global.verbosity>0){ + creep.say("MTTS"); + } + creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps}) + } + } } } creep.memory.lastpos=creep.pos