Screeps: World

Screeps: World

34 ratings
Container mining, logistics and market video guides
By goto64
A couple of video guides on Youtube, demonstrating how to use containers for more efficient resource gathering and how to create logistic creeps that can transport energy between structures.
2
   
Award
Favorite
Favorited
Unfavorite
Container Mining video
Code for the miner
var roleMiner = { /** @param {Creep} creep **/ run: function(creep) { var targets = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER) && (structure.store[RESOURCE_ENERGY] < structure.storeCapacity); } }); if(targets.length > 0) { if(creep.pos.getRangeTo(targets[0]) == 0) { var source = creep.pos.findClosestByPath(FIND_SOURCES); creep.harvest(source); } else { creep.moveTo(targets[0]); } } } }; module.exports = roleMiner;
Code for picking up from container
var containers = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER) && (structure.store[RESOURCE_ENERGY] > 0); } }); var source = creep.pos.findClosestByPath(containers); if (source) { if(creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(source); } }
Storage and Logistics video
Code for logistic creep (not CPU optimized)
var roleLogistic = { /** @param {Creep} creep **/ run: function(creep) { if(creep.memory.supplying && creep.carry.energy == 0) { creep.memory.supplying = false; creep.say('fetching'); } if(!creep.memory.supplying && creep.carry.energy == creep.carryCapacity) { creep.memory.supplying = true; creep.say('supplying'); } if (creep.memory.supplying) { var stores = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_STORAGE) && (structure.store[RESOURCE_ENERGY] < structure.storeCapacity); } }); if (stores && stores.length > 0) { if(creep.transfer(stores[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(stores[0]); } } } else { var containers = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER) && (structure.store[RESOURCE_ENERGY] > 0); } }); var source = creep.pos.findClosestByPath(containers); if (source) { if(creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(source); } } } } }; module.exports = roleLogistic;

Note: After making the video, I found that you can get the storage structure with
creep.room.storage
That will be more efficient than the find operation.
Mineral mining and selling to the market
Code for the extractor creep
var roleExtractor = { /** @param {Creep} creep **/ run: function(creep) { if (creep.memory.extracting && creep.carryCapacity == _.sum(creep.carry)) { creep.memory.extracting = false; } if (!creep.memory.extracting && 0 == _.sum(creep.carry)) { creep.memory.extracting = true; if (creep.ticksToLive < 200) { creep.suicide(); } } if (creep.memory.extracting) { var target; if (creep.memory.depositId) { target = Game.getObjectById(creep.memory.depositId); } else { var targets = creep.room.find(FIND_MINERALS); target = targets[0]; creep.memory.depositId = target.id; creep.memory.mineralType = target.mineralType; } if (creep.harvest(target) == ERR_NOT_IN_RANGE) { creep.moveTo(target); } } else { if (creep.room.terminal) { if (creep.transfer(creep.room.terminal, creep.memory.mineralType) == ERR_NOT_IN_RANGE) { creep.moveTo(creep.room.terminal); } } else if (creep.room.storage) { if (creep.transfer(creep.room.storage, creep.memory.mineralType) == ERR_NOT_IN_RANGE) { creep.moveTo(creep.room.storage); } } } } }; module.exports = roleExtractor;
Code for selling to a buy order
// Terminal trade execution if (spawn.room.terminal && (Game.time % 10 == 0)) { if (spawn.room.terminal.store[RESOURCE_ENERGY] >= 2000 && spawn.room.terminal.store[RESOURCE_HYDROGEN] >= 2000) { var orders = Game.market.getAllOrders(order => order.resourceType == RESOURCE_HYDROGEN && order.type == ORDER_BUY && Game.market.calcTransactionCost(200, spawn.room.name, order.roomName) < 400); console.log('Hydrogen buy orders found: ' + orders.length); orders.sort(function(a,b){return b.price - a.price;}); console.log('Best price: ' + orders[0].price); if (orders[0].price > 0.7) { var result = Game.market.deal(orders[0].id, 200, spawn.room.name); if (result == 0) { console.log('Order completed successfully'); } } } }
5 Comments
stormwing0 16 Mar, 2020 @ 9:46am 
My only issue with this tutorial is it stops too soon. Other than that good work and keep going. :) Already made a general case for this code but still stumped on many things about the market. That said keep up the good work.
ProlificPlague 2 Jul, 2019 @ 3:00pm 
great share. thank you.
GlaivePrime 27 May, 2019 @ 7:42pm 
Thank you! its really helpful!
AstroCryptic 7 Nov, 2018 @ 4:54pm 
this was a big help thanks! :steamhappy:
Eiskalt 14 Jan, 2017 @ 6:16pm 
Thank you!