【问题标题】:screeps - can't make variable in sourcescreeps - 不能在源代码中进行变量
【发布时间】:2023-03-09 10:36:01
【问题描述】:

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

主要

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

提前致谢,

贾里·范梅尔克贝克

【问题讨论】:

    标签: javascript screeps


    【解决方案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 的用途)。

    【讨论】:

      【解决方案2】:

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

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

      编辑它以满足您的需要

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-03
        • 2014-07-13
        • 2011-12-15
        • 1970-01-01
        相关资源
        最近更新 更多