gpt4 book ai didi

memory - Screeps 内存如何添加?

转载 作者:行者123 更新时间:2023-12-02 08:18:40 27 4
gpt4 key购买 nike

我有脚本部分必须获取源ID并将它们存储到内存中,但仍然不起作用,请帮助我。

    for(var name in Game.spawns)
{
var source1 = Game.spawns[name].room.find(FIND_SOURCES)
for(var i in source1)
{
Memory[source1[i].id] ={};
Memory[source1[i].id].workers = 0;
}
}

最佳答案

对于那些仍在寻找答案的人。

您可以轻松地将新的内存部分添加到任何对象。

大多数项目应该是特定于房间的,因此在大多数情况下您应该使用房间内存来添加对象。对于此示例,让我们为房间中的每个源添加内存:

//Lets first add a shortcut prototype to the sources memory:
Source.prototype.memory = undefined;

for(var roomName in Game.rooms){//Loop through all rooms your creeps/structures are in
var room = Game.rooms[roomName];
if(!room.memory.sources){//If this room has no sources memory yet
room.memory.sources = {}; //Add it
var sources = room.find(FIND_SOURCES);//Find all sources in the current room
for(var i in sources){
var source = sources[i];
source.memory = room.memory.sources[source.id] = {}; //Create a new empty memory object for this source
//Now you can do anything you want to do with this source
//for example you could add a worker counter:
source.memory.workers = 0;
}
}else{ //The memory already exists so lets add a shortcut to the sources its memory
var sources = room.find(FIND_SOURCES);//Find all sources in the current room
for(var i in sources){
var source = sources[i];
source.memory = this.memory.sources[source.id]; //Set the shortcut
}
}
}

在此代码之后,您的所有源都有内存。

让我们用收割机尝试一下:(creep 是模块中的变量)

var source = creep.pos.findClosest(FIND_SOURCES, {
filter: function(source){
return source.memory.workers < 2; //Access this sources memory and if this source has less then 2 workers return this source
}
});
if(source){ //If a source was found
creep.moveTo(source);
creep.harvest(source);

/* You should also increment the sources workers amount somehow,
* so the code above will know that another worker is working here.
* Be aware of the fact that it should only be increased once!
* But I will leave that to the reader.
*/
}

关于memory - Screeps 内存如何添加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30324353/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com