Screeps 自动生成器无法 'totally' 工作
Screeps autospawner not 'totally' working
所以我尝试更新我的 screeps 脚本,好吧.. 嗯.. 出了点问题,因为我的 autospawner 代码卡在了一个循环中。我对 JavaScript 还是很陌生,所以这可能是我犯的一些愚蠢的错误。我试图做的是通过在 if 语句中添加 console.log 来获取错误的参考错误...除了 'harvester spawned' 消息被垃圾邮件发送到我的控制台之外什么都没有。我真的很挣扎。请帮助我!
如果您需要任何额外信息,请说出来,我很乐意提供您需要的任何信息!
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);
}
}
}
根据控制台的时间戳,您的代码不像在主循环中那样 "stuck in a loop"。 numberOf.Harvesters < 3
的计算结果为 true
的每个刻度,将执行 if
块中的代码。假设 creep 计数部分在工作,这意味着它会一直执行,直到你有至少 3 个收割机。
也许您应该添加一些代码来检查 spawn 是否已经在生成另一个 creep,以及在调用 Spawn1.createCreep()
之前是否有足够的可用能量。这将大大减少您的控制台中的喋喋不休。
如果已经有 3 个收割机,请尝试在 if 语句之前添加类似 console.log('harvester count: ' + numberOf.Harvesters);
的内容。
所以我尝试更新我的 screeps 脚本,好吧.. 嗯.. 出了点问题,因为我的 autospawner 代码卡在了一个循环中。我对 JavaScript 还是很陌生,所以这可能是我犯的一些愚蠢的错误。我试图做的是通过在 if 语句中添加 console.log 来获取错误的参考错误...除了 'harvester spawned' 消息被垃圾邮件发送到我的控制台之外什么都没有。我真的很挣扎。请帮助我!
如果您需要任何额外信息,请说出来,我很乐意提供您需要的任何信息!
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);
}
}
}
根据控制台的时间戳,您的代码不像在主循环中那样 "stuck in a loop"。 numberOf.Harvesters < 3
的计算结果为 true
的每个刻度,将执行 if
块中的代码。假设 creep 计数部分在工作,这意味着它会一直执行,直到你有至少 3 个收割机。
也许您应该添加一些代码来检查 spawn 是否已经在生成另一个 creep,以及在调用 Spawn1.createCreep()
之前是否有足够的可用能量。这将大大减少您的控制台中的喋喋不休。
如果已经有 3 个收割机,请尝试在 if 语句之前添加类似 console.log('harvester count: ' + numberOf.Harvesters);
的内容。