我昨天开始玩Screeps>,并尝试制作自己的“修复者脚本”!但是我失败了,我不得不使用互联网上的一个脚本。问题是,当维修人员没有工作要做时,即使他们已满,他们也会留在提取能源的旁边。我试图修改代码但我不确定它是否会继续工作。所以我将代码粘贴在这里:
var roleRepairer = {
/** @param {Creep} creep **/
run: function(creep) {
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
}
// if creep is harvesting energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
}
// if creep is supposed to repair something
if (creep.memory.working == true) {
// find closest structure with less than max hits
// Exclude walls because they have way too many max hits and would keep
// our repairers busy forever. We have to find a solution for that later.
var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
});
// if we find one
if (structure != undefined) {
// try to repair it, if it is out of range
if (creep.repair(structure) == ERR_NOT_IN_RANGE) {
// move towards it
creep.moveTo(structure);
}
}
// if we can't fine one
else {
// look for construction sites
roleBuilder.run(creep);
}
}
// if creep is supposed to harvest energy from source
else {
// find closest source
var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards the source
creep.moveTo(source);
}
}
}
};
module.exports = roleRepairer;
我希望你能帮助我更多地了解这个精彩的游戏!非常赞赏!