- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
实际上我想找到旁边没有源的容器,这样我的卡车就可以在基地周围更灵活地传输能量而不是拥有一个巨大的存储空间。 (我的 Controller 距离很远)
// find all containers in room
var containers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType == STRUCTURE_CONTAINER
});
// find all sources
var sources = creep.room.find(FIND_SOURCES);
// if there is no source next to the container
if (!(sources.length < 0)) {
}
这完成时没有错误,但我无法获取容器 ID旁边没有来源。
我相信我必须在过滤器中包含最有可能循环每个容器的逻辑?
我对循环/迭代的理解还不够深入,无法让它正常工作。我尝试了很多事情,但现在我感到无助试图解决这个难题。
最佳答案
我认为您可以更新您的第一个过滤器来检查附近的来源,如下所示:
let containers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType === STRUCTURE_CONTAINER && s.pos.findInRange(FIND_SOURCES, 2).length === 0
});
我使用 2 作为范围,但如果您的容器始终与源相邻(或非常远),您可以将其更改为 1。
如果将该结果存储在内存中,则可以节省 CPU 资源。如果您想知道如何做到这一点,我可以提供更多信息。
编辑:对于缓存,您可以在 Room 对象上创建一个属性,如下所示(可能需要对此进行一些调试 - 我编写了它,但没有测试它):
Object.defineProperty(Room.prototype, 'nonSourceContainers', {
get: function () {
if (this === Room.prototype || this === undefined) return undefined;
const room = this;
// If any containers have been constructed that don't have isNearSource defined, fix that first.
let newContainers = room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType === STRUCTURE_CONTAINER && s.memory.isNearSource === undefined
});
// Found some new containers?
if (newContainers.length) {
for (let i = 0; i < newContainers.length; ++i) {
newContainers[i].memory.isNearSource = newContainers[i].pos.findInRange(FIND_SOURCES, 2).length > 0;
}
// Set the list of non-source container ids in the room's memory. This lasts forever.
room.memory.nonSourceContainerIds = room.find(FIND_STRUCTURES, {filter: {isNearSource: false}});
// Reset the cached set of containers.
room._nonSourceContainers = undefined;
}
// If there is no cached list, create it. Caching will mean the following runs once per tick or less.
if (!room._nonSourceContainers) {
if (!room.memory.nonSourceContainerIds) {
room.memory.nonSourceContainerIds = [];
}
// Get the containers by their ids.
room._nonSourceContainers = room.memory.nonSourceContainerIds.map(id => Game.getObjectById(id));
}
return room._nonSourceContainers;
},
enumerable: false,
configurable: true
});
关于javascript - Screeps - 按 1 范围内的来源过滤容器(容器挖掘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513669/
当我使用 Creep.build() build 新墙时,它会创建具有 1 点生命值的墙。现在我正在尝试创建一个修复这些新 build 的墙壁的爬虫。但是我无法找到一种列出和迭代这些墙对象的好方法。
所以我可以攻击其他小兵而不是结构,我想知道你是怎么做到的,我到处寻找答案,但我发现的一切都是关于如何攻击另一个小兵。 最佳答案 这和攻击小兵一样。 creep.attack(structure); 文
我正在玩 Screeps ( http://screeps.com ),并且我正在尝试使用 lodash 模块来过滤我的收割机和其他 Creeps。下面的代码应该可以工作,但是当我运行它时,我得到 R
假设我在内存中保存了房间的名称 - “1-1”。我如何在无法访问该房间中的任何蠕变/结构的情况下访问该房间对象?像这样的东西: function(roomName){ var room = Gam
有什么方法可以从控制台调用模块或函数吗?例如,创建 worker 小兵的模块,从控制台使用 my_module.add_creep 调用? 最佳答案 对于给定的示例,请使用 require('my_m
我有一个用于创建房间布局的原型(prototype)。我正在尝试计算放置容器的位置: the closest point between the spawn and the controller th
我有脚本部分必须获取源ID并将它们存储到内存中,但仍然不起作用,请帮助我。 for(var name in Game.spawns) { var source1 = G
首先,我是这个游戏的新手,但我以前就知道 js。我遇到的问题是我在模拟器中玩耍的结果。 我试图生成一个 creep,我首先修改了 example in the tutorial : Game.spaw
我的内存有问题。它应该存储我的所有数据(而不是对象),但它会不时重置。这是示例: Memory.creeps_idx = 0; module.exports.loop = function ()
我是 Screeps 的新手(喜欢它),我很难为房间中的所有源创建一个变量。我试图确保只有 3 个 creep 在同一个源上工作,所以我有以下代码片段用于我的收割机和我的主模块 主要 var sour
我对 Screeps 还比较陌生,我一直在努力构建自己的基本 AI 系统。然而,我在最初的测试中遇到了障碍。我有一个脚本来确定要做的工作,然后小兵请求可用的工作。我已经能够将 Job 对象存储到 Cr
游戏中Screeps ,我对某些内置函数的返回类型有一些问题。采取以下代码: var creeps1 = Game.creeps var room = Game.rooms[myRoomName];
我在哪里可以找到 Game 对象的常量列表,例如 Game.HOSTILE_CREEPS、Game.MOVE 和 Game .攻击? 我在 http://screeps.com/docs/ 上找不到它
有没有办法让 screeps 代码将字符串打印到控制台(或任何地方)以进行简单调试? 最佳答案 您可以为此使用标准的 console.log 方法。 关于javascript - Screeps:将调
// Creep has Energy packed creep.say('E: ' + creep.carry.energy); if (creep.carry.en
在 Screeps 中,是否有可能不仅在控制台输出中看到错误,而且还能看到模块、行,也许还有堆栈跟踪? 更新:这是我得到的错误的样子。我没有看到行号或堆栈跟踪,或者可能没有看到启用它们的按钮? htt
在 Screeps 中,这段代码不起作用: var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE); 它是这样说的: Cannot read p
我试图编写一个代码来防止 2 个 creep(收割机)前往同一目的地并将它们绑定(bind)到该目的地,直到它满为止。 在第 55 行我得到一个可以理解的错误 55: creep.memory.tar
我有点想用 SO 来指出一些事情。 首先,在这个游戏中使用“脚本”有点奇怪,因为与“普通”Javascript 脚本不同,执行方式不同。我们都知道在脚本中,第一件事首先被执行。所以如果我有: Game
不知道“Screeps”吗?访问 screeps.com 主脚本中的第11行 错误: main:11 if (creep.transfer(Game.spawns.Spawn1, RESO
我是一名优秀的程序员,十分优秀!