30 lines
975 B
JavaScript
30 lines
975 B
JavaScript
// Finds the closest container that has something in it
|
|
var find_container = function(creep) {
|
|
return creep.pos.findClosestByPath(FIND_STRUCTURES, {
|
|
filter: (s) => {
|
|
return (s.structureType == STRUCTURE_CONTAINER ||
|
|
s.structureType == STRUCTURE_STORAGE) &&
|
|
s.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
}
|
|
|
|
var do_collect = function(creep, done_state, done_state_message) {
|
|
var container = find_container(creep);
|
|
if (container) {
|
|
if(creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(container, {visualizePathStyle: {stroke: '#ffaa00'}});
|
|
}
|
|
}
|
|
if (creep.store.getFreeCapacity() == 0) {
|
|
// If we are full with energy, switch to the next state
|
|
creep.memory.state = done_state;
|
|
creep.say(done_state_message);
|
|
}
|
|
}
|
|
|
|
var baseCollector = {
|
|
do_collect: do_collect,
|
|
}
|
|
|
|
module.exports = baseCollector; |