2016-07-28 28 views
2

我是Screeps的新手(喜欢它),我很难为房间中的所有源创建变量。 我想,以确保只有3个小兵相同的源上工作,所以我下面的代码片段为我的收获,我的主模块screeps - 无法在源代码中创建变量

主要

var sources = Game.spawns.Spawn1.room.find(FIND_SOURCES); 
for (var a in sources) { 
    var source = (sources[a]); 
    source.memory.numPeopleAt = 0; 
} 
module.exports.loop = function() { 
... 
} 

收割机

var sources = creep.room.find(FIND_SOURCES); 
for (var s in sources) { 
    if (creep.harvest(sources[s]) == ERR_NOT_IN_RANGE && sources[s].memory.numPeopleAt < 3) { 
     creep.moveTo(sources[s]); 
     sources[s].memory.numPeopleAt++; 
     break; 
    } 
} 

我知道我还是要做一个功能sources[s].memory.numPeopleAtt--

在此先感谢,

杰瑞凡Melckebeke

回答

1

源没有记忆特性,像爬行一样。但是,您可以将某些内容添加到主内存对象中。

var sources = Game.spawns.Spawn1.room.find(FIND_SOURCES); 
if (!Memory.sources) { 
    Memory.sources = {}; 
} 
_.each(sources, function(source) { 
    if (!Memory.sources[source.id]) { 
     Memory.sources[source.id] = { numPeopleAt: 0 }; 
    } 
}); 

有一点要注意的是,你的代码将运行每一场比赛剔,所以你只需要如果它尚未初始化,初始化的东西(这是什么如果,检查是)。

0

这将设置源到最近的来源,而不在它附近

var source = creep.pos.findClosestByPath(FIND_SOURCES, {filter: (s) => s.pos.findInRange(FIND_MY_CREEPS, 1, {filter: (c) => c.memory.role == 'harvester' && c.name != creep.name})[0] == undefined}); 

编辑另一个收获它来满足您的需求

相关问题