From 3e73445a76e56ac6489ff98a9e8b4d93a3b6eb49 Mon Sep 17 00:00:00 2001 From: Dan Hamik Date: Thu, 28 Apr 2022 14:21:40 -0500 Subject: [PATCH] get market working for all mineral types --- Room.Phase1.js | 62 ++++++++++++++++---- proc.market.js | 30 +++++++--- proc.spawning.js | 27 +++++++++ role.miner.js | 2 +- role.mineralmover.js | 132 +++++++++++++++++++++++++++++++++++++++++++ role.mover.js | 60 +++++++++++++++++--- 6 files changed, 283 insertions(+), 30 deletions(-) create mode 100644 role.mineralmover.js diff --git a/Room.Phase1.js b/Room.Phase1.js index 3eb7ebc..1b893a8 100644 --- a/Room.Phase1.js +++ b/Room.Phase1.js @@ -2,7 +2,7 @@ let Phase1 = { run: function(room) { let myroom=Game.rooms[room] require('assignsources').tick(room) - if(Game.time % 1 === 0){ + if(Game.time % 10 === 0){ //console.log('processing spawn') let myspawns = myroom.find(FIND_MY_SPAWNS) @@ -45,25 +45,63 @@ let Phase1 = { console.log("Spawning Mover in " + room) require('proc.spawning').spawnmover(room) } + if((myroom.storage || myroom.terminal) && (creepcounts["mineralmover"] < 1 || creepcounts["mineralmover"]==undefined)){ + console.log("Spawning MineralMover in " + room) + require('proc.spawning').spawnmineralmover(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) require('proc.spawning').spawnminer(room) } else { if(Game.flags.debug && Game.flags.debug.room == myroom){console.log("Not spawning miner in " + room + ", waiting for regen")}} } - if(myroom.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TERMINAL}}).length > 0){ - console.log("starting market check for room " + room) - try{ - let availtosell = myroom.terminal.store.getUsedCapacity([RESOURCE_ENERGY]) - 200000 - if(availtosell > 1000){ - require('proc.market').sellEnergy(room) - } - }catch{ - console.log("Myroom" + myroom) - console.log("Room" + room) + } + } + 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{ + if(myroom.terminal){ + ///console.log("1") + if(myroom.terminal.store.getUsedCapacity() == 0) { + //console.log("terminal is empty") + }else{ + if(myroom.terminal.store.getUsedCapacity('energy') > 10000){ + console.log("Starting market loop for "+ myroom) + for(mat in myroom.terminal.store){ + let availtosell=0 + //console.log(mat) + if(mat == 'energy'){ + try{ + availtosell = myroom.terminal.store.getUsedCapacity(mat) - 200000 + } catch { + //console.log("error getting energy") + availtosell=0 + } + } else { + try{ + //console.log("2") + availtosell = myroom.terminal.store.getUsedCapacity(mat) + } catch { + //console.log("Couldn't get available " + mat) + availtosell=0 + } + } + if(availtosell){ + //console.log(availtosell) + if(availtosell > 20000){ + //console.log("processing market") + require('proc.market').sellEnergy(room,mat) + } + } + } + } + } } + }catch(e){ + console.log("Myroom" + myroom) + console.log(e) } - //require('proc.market').sellEnergy(room) } } if(myroom.controller.level>2){ } diff --git a/proc.market.js b/proc.market.js index f234586..c3d71ac 100644 --- a/proc.market.js +++ b/proc.market.js @@ -7,18 +7,18 @@ * mod.thing == 'a thing'; // true */ let gomarket={ - sellEnergy: function(myroom) { + sellEnergy: function(myroom,mat) { try{ - let availtosell= Game.rooms[myroom].terminal.storage[RESOURCE_ENERGY] - - if(availtosell>200000){ - console.log(myroom + ":" + availtosell) + let availtosell= Game.rooms[myroom].terminal.store.getUsedCapacity(mat) + if(availtosell>0){ + console.log(myroom + ":" + mat +":" + availtosell) //console.log(JSON.stringify(Game.market.orders)) // if(Game.market.orders.length > 9){ - let myorders = Game.market.getAllOrders(order=>order.resourceType == RESOURCE_ENERGY && order.type == ORDER_BUY) + let myorders = Game.market.getAllOrders(order=>order.resourceType == mat && order.type == ORDER_BUY) + //console.log("Open orders: " + myorders.length) //console.log(myorders.length) for(let i=0; i {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_TERMINAL || s.structureType == STRUCTURE_FACTORY ) && _.sum(s.store) < s.storeCapacity) ;}}); + let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_TERMINAL || s.structureType == STRUCTURE_FACTORY ) && _.sum(s.store) < s.storeCapacity) ;}}); if(storagetargets){ if(this.transferAll(creep,storagetargets) == ERR_NOT_IN_RANGE) { creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps}) diff --git a/role.mineralmover.js b/role.mineralmover.js new file mode 100644 index 0000000..680ab5e --- /dev/null +++ b/role.mineralmover.js @@ -0,0 +1,132 @@ +let mover={ + run: function(creep) { + //creep.say("Moving Minerals") + let ignorecreeps=true + if(creep.memory.lastpos==undefined || creep.memory.timeatpos==undefined){ + 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 + if(creep.memory.timeatpos>2){ + //console.log(creep.name + " " + creep.memory.timeatpos) + ignorecreeps=false + } else { } + } else { creep.memory.timeatpos=0} + let filllevel = _.sum(creep.carry) + if(creep.memory.working && filllevel == 0) { + creep.memory.working = false; + creep.say('Gathering'); + } + if(!creep.memory.working && filllevel == creep.carryCapacity) { + creep.memory.working = true; + creep.say('working'); + } + if(creep.memory.working){ + let terminaltarget = creep.room.terminal + if(creep.room.terminal){ + if(terminaltarget.store.getUsedCapacity()<200000){ + if(this.transferAll(creep,terminaltarget) == ERR_NOT_IN_RANGE) { + creep.moveTo(terminaltarget,{ignoreCreeps:ignorecreeps}) + } + } + } + } 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); + } + } else { + if(filllevel < creep.carryCapacity){ + /* + let nearestcontainer = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER ) && s.store.getUsedCapacity('energy') >= 10) ;}}); + if(creep.room.terminal){ + let terminal = creep.room.terminal + let terminalenergy= terminal.store.getUsedCapacity('energy') + } + if(creep.room.storage){ + let storage = creep.room.storage + let storageenergy=storage.store.getUsedCapacity('energy') + } + //console.log("Storage: " + storage.store.getUsedCapacity('energy')) + //console.log("Container: " + nearestcontainer.store.getUsedCapacity('energy') + try{ + if(nearestcontainer == null){ + + }else { + storagetarget = nearestcontainer + } + }catch{ + if(storageenergy > 0){ + storagetarget = storage + } else if ( terminalenergy > 0) { + storagetarget = terminal + } else { + console.log("we're out of energy!") + storagetarget=storage + } + }*/ + let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER ) && (s.store.getUsedCapacity() > s.store.getUsedCapacity('energy') || s.store.getUsedCapacity('energy') > 200000)) ;}}); + 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)}}); + if((droppedenergy == undefined) && (tombstone==undefined)){ + try{ + // console.log(storagetarget.store.getUsedCapacity('energy')/storagetarget.store.getCapacity()) + if(storagetarget.store.getUsedCapacity('energy')< 1000 && (storagetarget.store.getUsedCapacity('energy')/storagetarget.store.getCapacity() < .5 )){ + //console.log("too much crap") + //console.log(storagetarget.store.getUsedCapacity()) + //creep.travelTo(storagetarget); + } + } catch(e) { + //console.log("oops" + e) + } + try{ + for(mat in storagetarget.store){ + if(mat=='energy' && storagetarget.store.getUsedCapacity('energy') > 200000){ + if(creep.withdraw(storagetarget,mat)== ERR_NOT_IN_RANGE) { + creep.say('Getting ' + mat ) + creep.travelTo(storagetarget); + } + } else { + if(creep.withdraw(storagetarget,mat)== ERR_NOT_IN_RANGE) { + creep.say('Getting ' + mat ) + creep.travelTo(storagetarget); + } + } + } + } catch (e){ + //console.log(e) + } + } + if(!storagetarget && (creep.carry > 0)){ + creep.memory.working=true + } + } + } + creep.memory.lastpos=creep.pos + }, + 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 = mover; \ No newline at end of file diff --git a/role.mover.js b/role.mover.js index a5247c3..a003d0f 100644 --- a/role.mover.js +++ b/role.mover.js @@ -26,16 +26,18 @@ let mover={ creep.say('working'); } if(creep.memory.working){ - let terminaltarget = creep.room.terminal - if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_LAB].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.structureType == STRUCTURE_LAB) && s.energy < s.energyCapacity))}}); + //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){ 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(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 @@ -53,14 +55,56 @@ let mover={ } } 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) >= 10) ;}}); + /* + let nearestcontainer = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER ) && s.store.getUsedCapacity('energy') >= 10) ;}}); + if(creep.room.terminal){ + let terminal = creep.room.terminal + let terminalenergy= terminal.store.getUsedCapacity('energy') + } + if(creep.room.storage){ + let storage = creep.room.storage + let storageenergy=storage.store.getUsedCapacity('energy') + } + //console.log("Storage: " + storage.store.getUsedCapacity('energy')) + //console.log("Container: " + nearestcontainer.store.getUsedCapacity('energy') + try{ + if(nearestcontainer == null){ + + }else { + storagetarget = nearestcontainer + } + }catch{ + if(storageenergy > 0){ + storagetarget = storage + } else if ( terminalenergy > 0) { + storagetarget = terminal + } else { + console.log("we're out of energy!") + storagetarget=storage + } + }*/ + let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER) && s.store.getUsedCapacity('energy') >= 10) ;}}); + if(storagetarget===undefined){ + storagetarget==creep.room.terminal + } 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)}}); if((droppedenergy == undefined) && (tombstone==undefined)){ + /*try{ + // console.log(storagetarget.store.getUsedCapacity('energy')/storagetarget.store.getCapacity()) + if(storagetarget.store.getUsedCapacity('energy')< 1000 && (storagetarget.store.getUsedCapacity('energy')/storagetarget.store.getCapacity() < .5 )){ + //console.log("too much crap") + //console.log(storagetarget.store.getUsedCapacity()) + //creep.travelTo(storagetarget); + } + } catch(e) { + console.log("oops" + e) + }*/ if(creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) { creep.say('Getting Energy') creep.travelTo(storagetarget); } + creep.memory.pulledenergyfrom=storagetarget } else { if(droppedenergy){ if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) {