2

我是 Screeps 的新手(喜欢它),我很难为房间里的所有来源创建一个变量。我试图确保只有 3 个小兵在同一个源上工作,所以我为我的收割机和我的主模块提供了以下代码片段

主要的

var sources = Game.spawns.Spawn1.room.find(FIND_SOURCES);
for (var a in sources) {
  var source = (sources[a]);
  source.memory.numPeopleAt = 0;
}
module.exports.loop = function () {
...
}

收割机

var sources = creep.room.find(FIND_SOURCES);
for (var s in sources) {
    if (creep.harvest(sources[s]) == ERR_NOT_IN_RANGE && sources[s].memory.numPeopleAt < 3) {
        creep.moveTo(sources[s]);
        sources[s].memory.numPeopleAt++;
        break;
     }
}

我知道我仍然必须创建一个功能sources[s].memory.numPeopleAtt--

提前致谢,

贾里·范·梅尔克贝克

4

2 回答 2

1

Source 没有 Creep 那样的内存属性。但是,您可以向主内存对象添加一些内容。

var sources = Game.spawns.Spawn1.room.find(FIND_SOURCES);
if (!Memory.sources) {
    Memory.sources = {};
}
_.each(sources, function(source) {
    if (!Memory.sources[source.id]) {
        Memory.sources[source.id] = { numPeopleAt: 0 };
    }
});

需要注意的一点是,您的代码将运行每个游戏时间,因此您只需要在尚未初始化的情况下初始化某些内容(这就是 if-checks 的用途)。

于 2016-07-30T17:48:52.460 回答
0

这会将源设置为最近的源,而附近没有其他收割机

var source = creep.pos.findClosestByPath(FIND_SOURCES, {filter: (s) => s.pos.findInRange(FIND_MY_CREEPS, 1, {filter: (c) => c.memory.role == 'harvester' && c.name != creep.name})[0] == undefined});

编辑它以满足您的需求

于 2016-10-20T22:57:01.383 回答