1

我的矿工代码:

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 个来源和运输者一次从多个容器中撤出。

4

3 回答 3

2

A creep can only perform one action of the same type each tick. If your creep perform withdraw twice, only the last of those calls will be evaluated by the server. It is technically the same with move. You can only move once per tick and it is only the last call to move that will go through to the server.

When your code makes a call to withdraw and the method returns OK it doesn't mean the action was performed. It means that the action was successfully registered as an "intent". A creep can register only one intent for each action. When your code has finished execution, these intents are sent to the server for evaluation. If you have a container with 100 energy and two creeps try to withdraw this energy, both creeps will get OK in response, but when the server executes your intents, only one of them will actually be performed.

Another thing to remember is the simultaneous execution rules. A creep can perform both a harvest and a move in the same tick, but if it tries to do both build and repair, only build will be performed.

More about this here: http://docs.screeps.com/simultaneous-actions.html

于 2017-06-28T07:46:32.993 回答
2

有些人使用所有可用资源的简单随机选择。这很简单,但并不可取。我所做的是,当一个cree 准备好收割时,我使用一种算法来选择一个源并将源ID 保存到cree 的内存中。

我用来选择源的算法考虑了已经瞄准该源的其他小兵的数量、到源的距离以及未被阻挡的相邻方格的数量(被墙之类的东西)。

var source;

if (creep.memory.sourceId) {
    source = Game.getObjectById(creep.memory.sourceId);
} else {
    //Find all active sources
    //Count the number of creeps targeting that source
    //Count the accessible adjacent squares using something like room.lookForAt(...)
    //source = ... the source you selected
}

if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
        creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}});
}

也可以为运输者做类似的事情。

于 2017-10-20T18:20:14.357 回答
0

我相信这根本不可能。虽然没有具体说明,但我认为情况与以下情况相同transfer

虽然transfer可能会work随着 drop 一起下降,但您不能在transfer每个滴答声中执行两次或更多次(以将能量传输到多个对象)。对于所有类似的方法也是如此。

于 2017-07-21T20:13:42.940 回答