我的矿工代码:
var roleMiner = {
run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES_ACTIVE);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
} else {
var containers = creep.room.find(FIND_STRUCTURES, {filter: (s) => (s.structureType == STRUCTURE_CONTAINER && s.store.energy< s.storeCapacity)});
if(containers != undefined){
creep.moveTo(containers[0]);
creep.transfer(containers[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE
}
}
}
};
module.exports = roleMiner;
和我的转运代码:
var roleTransporter = {
run: function(creep) {
if(creep.carry.energy == 0) {
var containers = creep.room.find(FIND_STRUCTURES, {filter: (s) => (s.structureType == STRUCTURE_CONTAINER && s.store.energy <= s.storeCapacity)});
if (creep.withdraw(containers[1], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(containers, {visualizePathStyle: {stroke: '#ffaa00'}});
}
} else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
}
});
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
}
}
}
};
module.exports = roleTransporter;
关键是,在我有容器 [0] 或源 [0] 的地方,数字是容器或源对象。
我怎样才能让矿工一次收获多个来源,因为我有 2 个来源和运输者一次从多个容器中撤出。