可以't在源中生成变量

can't make variable in source

本文关键字:变量 可以      更新时间:2023-09-26

我是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--

提前感谢

Jari Van Melckebeke

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检查的目的)。

这将把源设置为最近的源,而附近没有其他采集器

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});

编辑它以满足您的需求