Javascript Screeps-按范围为1的源过滤容器(容器挖掘)
事实上我想找到旁边没有来源的容器, 所以我的卡车可以更灵活地在基地周围传输能量 而不是拥有一个巨大的存储空间。(我的控制器在远处)Javascript Screeps-按范围为1的源过滤容器(容器挖掘),javascript,screeps,Javascript,Screeps,事实上我想找到旁边没有来源的容器, 所以我的卡车可以更灵活地在基地周围传输能量 而不是拥有一个巨大的存储空间。(我的控制器在远处) //查找房间中的所有容器 var containers=screw.room.find(find_结构{ 过滤器:(s)=>s.structureType==结构容器 }); //查找所有来源 var sources=screw.room.find(查找源); //如果容器旁边没有源 如果(!(sources.lengths.structureType==结构容器&
//查找房间中的所有容器
var containers=screw.room.find(find_结构{
过滤器:(s)=>s.structureType==结构容器
});
//查找所有来源
var sources=screw.room.find(查找源);
//如果容器旁边没有源
如果(!(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资源。如果你想知道怎么做,我可以提供更多的信息
编辑:对于缓存,您可以像这样在房间对象上创建属性(可能需要对此进行一些调试-我编写了它,但没有测试它):
Object.defineProperty(Room.prototype,'nonSourceContainers'{
get:function(){
if(this==Room.prototype | | this==undefined)返回undefined;
const room=这个;
//如果构造的任何容器没有定义isNearSource,请首先修复该问题。
让newContainers=room.find(find_结构{
筛选器:(s)=>s.structureType==结构容器&&s.memory.isNearSource==未定义
});
//找到一些新的容器了吗?
if(newContainers.length){
for(设i=0;i0;
}
//设置文件室内存中的非源容器ID列表。这将永远持续。
room.memory.nonSourceContainerIds=room.find(find_结构,{filter:{isNearSource:false}});
//重置缓存的容器集。
房间。非资源容器=未定义;
}
//如果没有缓存列表,请创建它。缓存将意味着以下每勾选一次或更少的运行。
如果(!房间._非资源容器){
if(!room.memory.nonSourceContainerIds){
room.memory.nonSourceContainerIds=[];
}
//通过容器ID获取容器。
room.\u nonSourceContainers=room.memory.nonSourceContainerIds.map(id=>Game.getObjectById(id));
}
返回室。_非资源容器;
},
可枚举:false,
可配置:true
});
这太棒了-我在摆弄过滤器的时候想到了内存,打算使用docs.screeps中关于存储内存的示例,但我相信这会运行每一个滴答声吗?(编辑:我还没完成,想开始新的一行:()抱歉-编程新手-我不明白这一点。这进入了一个新的原型?它不创建任何内存,但也没有错误。我还没有设置任何与内存相关的东西,但有一个基本的派生behavior@Bergzwerg这可能是您希望稍后再来讨论的问题,因为您可能需要调试和修复它n、 不过,这在Screw原型上定义了一个新属性。您可以将它放在主代码指定为“require”d的任何js文件中。然后您应该能够像这样引用它:screw.room.nonSourceContainers
let containers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType === STRUCTURE_CONTAINER && s.pos.findInRange(FIND_SOURCES, 2).length === 0
});
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
});