Overview
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.
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’); } } } }