1

提醒一下,这更像是一个“告诉我这段代码是否有问题”的帖子。我道歉。我一直在努力让我的 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
4

1 回答 1

2

首先要尝试:添加console.log("Ping!");或类似的东西作为函数的第一行,以确保角色正在为你的cree执行。

run: function(creep) {
    console.log("Ping!");
    //all of the other stuff
}

需要注意的其他事项:

if (targets.length > 0) {
    //...Some stuff
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
} else {
    creep.transfer(targets[0], RESOURCE_ENERGY); //HERE!!!!
}

如果targets数组为空,则尝试访问targets[0]将导致错误。如果没有目标,则没有可转移的内容。整个else块可能应该被删除。

如果您仍然需要帮助,请尝试扩展“它似乎不起作用”的意思。收获部分有效,但填充部分无效?

于 2017-10-20T18:40:11.060 回答