3

I have script part that must get sources ID and store them to memory, but still don't work, please help me.

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

3 Answers3

6

For those still looking for an answer.

You can easily add new memory parts to any object.

Most items should be room specific, so in most cases you should use the rooms memory to add objects. For this example lets add a memory for each source in a room:

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

After this code all of your sources have memory.

Lets try it with a harvester: (creep is a variable from the module)

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.
     */
}
Duckdoom5
  • 716
  • 7
  • 27
1

There's a similar question with a good answer; https://stackoverflow.com/a/30150587/5857473.

I wanted it to be a property of the room, so I changed the code like this:

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

This avoids looping through all rooms to set up the shortcuts, etc. as above.

Community
  • 1
  • 1
Jomik
  • 38
  • 6
0

I used the following code to save all of the sources in the current room and a property that will allow me to later on assign a miner creep to the source.

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

        }
    }

And here is the code to loop through memory and view each source.

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

    }
JoBaxter
  • 710
  • 2
  • 12
  • 23