Для тех, кто еще ищет ответ.
Вы можете легко добавить новые части памяти к любому объекту.
Большинство предметов должно быть специфичным для помещений, поэтому в большинстве случаев вы должны использовать память комнат для добавления объектов. Для этого примера позволяет добавить память для каждого источника в комнате:
//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.
*/
}
также, источник1 [i] .id - должен быть исходным идентификатором в списке памяти, но это не значит, что эта часть является переменной, так почему? и что я могу сделать ?! –
SO LOL, решил, что было легко, я просто stuid noob: D –
, пожалуйста, отметьте законный ответ как ответ! – Gewure