提醒一下,这更像是一个“告诉我这段代码是否有问题”的帖子。我道歉。我一直在努力让我的 Screeps 殖民地更有效率,这带来了对更高质量单位的需求,这带来了对扩展的需求。我为一个单元编写了一个脚本来收集能量并将其带入/传输到扩展,但是它似乎不起作用。它没有给我任何错误,我开始认为这可能只是我的主脚本的问题,以及我将角色功能转移给毛骨悚然的过程,所以如果有人愿意简单地看一下我的代码并告诉我它是否有问题,将不胜感激。这是我的代码(我称角色为“填充者”):
var roleFiller = {
run: function(creep) {
if (creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[1], {
visualizePathStyle: {
stroke: '#ffaa00'
}
});
} else {
creep.harvest(sources[1]);
}
} else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
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'
}
});
}
} else {
creep.transfer(targets[0], RESOURCE_ENERGY);
}
}
}
};
module.exports = roleFiller