克服自动车手并非“完全”工作

时间:2018-02-07 19:54:37

标签: javascript if-statement screeps

所以我试着更新我的screeps脚本......好吧......出了问题,因为我的autospawner代码卡在一个循环中。我仍然是相当新的JavaScript,所以这可能是我犯的一些愚蠢的错误。我试图做的是通过向if语句添加console.log来获取引发错误的引用错误...除了收集垃圾邮件进入我的控制台的“收集器生成”消息之外什么都没有。我真的很挣扎。请帮助我!

如果您需要任何额外信息,请说明,我很乐意提供您需要的任何内容!

const roleHarvester = require('role.harvester');
const roleUpgrader = require('role.upgrader');
const roleBuilder = require('role.builder');

module.exports.loop = function () {

for(let name in Memory.creeps) {
    if(!Game.creeps[name]) {
        delete Memory.creeps[name];
    }
}

//change to object
let minimum = {
    NumberOfHarvesters: 3,
    NumberOfUpgraders: 3,
    NumberOfBuilders: 3
}
let numberOf = {
    Harvesters: _.sum(Game.creeps, (c) => c.memory.role == 'harvester'),
    Upgraders: _.sum(Game.creeps, (c) => c.memory.role == 'upgraders'),
    Builders: _.sum(Game.creeps, (c) => c.memory.role == 'builders')
}

if (numberOf.Harvesters < 3) {
    Game.spawns.Spawn1.createCreep([WORK, CARRY, CARRY, CARRY, MOVE], {role: 'harvester'})
    console.log('harvester spawned');
} 
else if (numberOf.Upgraders < 3) {
    Game.spawns.Spawn1.createCreep([WORK, CARRY, CARRY, CARRY, MOVE], {role: 'upgrader'})
    console.log('upgrader spawned');
} 
else if (numberOf.Builders < 3) {
    Game.spawns.Spawn1.createCreep([WORK, WORK, CARRY, MOVE], {role: 'builder'})
    console.log('builder spawned');
}
else {
    Game.spawns.Spawn1.createCreep([WORK, CARRY, MOVE], {role: 'upgrader'})
}   

for(let name in Game.creeps) {
    let creep = Game.creeps[name];
    if(creep.memory.role == 'harvester') {
        roleHarvester.run(creep);
    }
    if(creep.memory.role == 'upgrader') {
        roleUpgrader.run(creep);
    }
    if(creep.memory.role == 'builder') {
        roleBuilder.run(creep);
    } 
}
}

enter image description here

1 个答案:

答案 0 :(得分:1)

根据控制台的时间戳,您的代码不会被卡在一个循环中#34;就像它在主循环中一样。 numberOf.Harvesters < 3评估为true的每个标记都将执行if块中的代码。假设蠕变计数部分正在工作,这意味着它将继续执行,直到你有至少3个收割机。

也许你应该添加一些代码来检查spawn是否已经产生了另一个creep,并且在调用Spawn1.createCreep()之前是否有足够的可用能量。这将大大减少控制台中的喋喋不休。

如果已经有3台收割机,请尝试在if语句之前添加类似console.log('harvester count: ' + numberOf.Harvesters);的内容。