3

我有脚本部分必须获取源 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;
        }
    }
4

3 回答 3

6

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

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

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

//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.
     */
}

于 2015-05-22T14:30:33.480 回答
1

有一个类似的问题有一个很好的答案;https://stackoverflow.com/a/30150587/5857473

我希望它成为房间的属性,所以我改变了这样的代码:

Object.defineProperty(Source.prototype, 'memory', {
    get: function() {
        if(_.isUndefined(this.room.memory.sources)) {
            this.room.memory.sources = {};
        }
        if(!_.isObject(this.room.memory.sources)) {
            return undefined;
        }
        return this.room.memory.sources[this.id] = this.room.memory.sources[this.id] || {};
    },
    set: function(value) {
        if(_.isUndefined(this.room.memory.sources)) {
            Memory.sources = {};
        }
        if(!_.isObject(this.room.memory.sources)) {
            throw new Error('Could not set source memory');
        }
        this.room.memory.sources[this.id] = value;
    }
});

这样可以避免循环遍历所有房间来设置快捷方式等,如上所述。

于 2016-07-18T20:07:45.810 回答
0

我使用以下代码保存了当前房间中的所有源和一个属性,该属性将允许我稍后将矿工蠕变分配给源。

    if(!spawns.memory.roomSources){
        spawns.memory.roomSources=[];
        var energySources = spawns.room.find(FIND_SOURCES);
        for(var i in energySources){
            spawns.memory.roomSources[i] = {sourceId: energySources[i].id, currentMinerId: null};

        }
    }

这是遍历内存并查看每个源的代码。

    for(var x in spawns.memory.roomSources){

    }
于 2016-11-17T21:41:58.353 回答