因此,我有一个名为storer的蠕变角色,应该从容器中获取能量并将其带到存储中。但是,目前,它会找到距离Path最近的容器,并且其能量水平大于某个阈值,以使矿工每次填充容器时都不会在这里等待数小时。

我遇到的问题是,如果我降低阈值,则storer将在相同的容器中来回运行,而忽略房间中更远的任何容器并让它们充满。
而提高阈值将使他坐下来等待太久,而没有给他足够的时间来清空容器,因此,几乎所有时间存储空间都是空的。

我需要一种蠕变方法,以确定能量最高的容器并从那里填充。

这是它运行的代码:

if ((source = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] >= 150)}})) != undefined) {
    if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
        creep.moveTo(source);
    }
}

编辑:这是我尝试过的代码,但是我觉得它使用了过多的CPU能力,可以通过更好的方式来完成:
for (let i = 2000; i>=0; i=i-100) {
    source = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] >= i}});
        if (source != undefined) {
            break;
        }
    }
    if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
        creep.moveTo(source);
    }
}

最佳答案

您可以做的是使所有容器循环一次,获取其能量并选择最高的容器。在蠕变起作用时设置一个值,这样蠕变不会变高,也不会移动到另一个容器。

这是我为存储者角色编写的代码:

module.exports = {

  run: function( creep ) {

    // Setting the working variable so the creep focus
    // on getting the ressource or returning it.
    if ( creep.memory.working && creep.carry.energy == 0 ) {
        creep.memory.working = false;
    }

    if ( ! creep.memory.working && creep.carry.energy == creep.carryCapacity ) {
        creep.memory.working = true;
        creep.memory.targetContainer = false;
    }

    if ( creep.memory.working ) {

        // Bring the ressources to the storage.
        var theStorage = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
            filter: (structure) => {
                return (structure.structureType == STRUCTURE_STORAGE );
            }
        });

        if ( creep.transfer( theStorage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
            creep.moveTo( theStorage );
        }

    } else {

        // If the creep have a target.
        if ( creep.memory.targetContainer ) {

            // Go to the container.
            var theContainer = Game.getObjectById( creep.memory.targetContainer );

            if ( creep.withdraw( theContainer, RESOURCE_ENERGY ) == ERR_NOT_IN_RANGE ) {
                creep.moveTo( theContainer );
            }

        } else {

            // Find the container with the most energy.
            var target = creep.room.find( FIND_STRUCTURES, {
                filter: (structure) => {
                    return (structure.structureType == STRUCTURE_CONTAINER );
                }
            });

            if ( target.length ) {

                var allContainer = [];

                // Calculate the percentage of energy in each container.
                for ( var i = 0; i < target.length; i++ ) {

                    allContainer.push( { energyPercent: ( ( target[i].store.energy / target[i].storeCapacity ) * 100 ), id: target[i].id } );

                }

                // Get the container containing the most energy.
                var highestContainer = _.max( allContainer, function( container ){ return container.energyPercent; });

                console.log( 'Going for the container id "' + highestContainer.id + '" at ' + highestContainer.energyPercent + '% full.' );

                // set the target in memory so the creep dosen't
                // change target in the middle of the room.
                creep.memory.targetContainer = highestContainer.id;

            }
        }
    }
  }
};

09-28 05:49