2015-05-19 2 views
3

У меня есть часть скрипта, которая должна получить идентификатор источников и хранить их в памяти, но все равно не работает, пожалуйста, помогите мне.Screeps Memory добавляет, как?

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

также, источник1 [i] .id - должен быть исходным идентификатором в списке памяти, но это не значит, что эта часть является переменной, так почему? и что я могу сделать ?! –

+0

SO LOL, решил, что было легко, я просто stuid noob: D –

+0

, пожалуйста, отметьте законный ответ как ответ! – Gewure

ответ

5

Для тех, кто еще ищет ответ.

Вы можете легко добавить новые части памяти к любому объекту.

Большинство предметов должно быть специфичным для помещений, поэтому в большинстве случаев вы должны использовать память комнат для добавления объектов. Для этого примера позволяет добавить память для каждого источника в комнате:

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

0

Там же подобный вопрос хороший ответ; 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; 
    } 
}); 

Это позволяет избежать зацикливания через все комнаты, чтобы настроить ярлыки и т.д., как описано выше.

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){ 

    } 

 Смежные вопросы

  • Нет связанных вопросов^_^