user8521147
user8521147

Reputation:

Screeps autospawner not 'totally' working

So I tried to update my screeps script and well.. uhm.. something went wrong, because my autospawner code is stuck in a loop. I'm still fairly new to JavaScript so it's probably some stupid mistake I made. What I have tried to do is to get a Reference error for what is wrong by adding a console.log to the if statement... got nothing except for a 'harvester spawned' message that gets spammed into my console. I'm really struggling. Pls help me!

If you need any extra info pls say so, i'm happy to provide anything that you need!

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

Upvotes: 1

Views: 516

Answers (1)

Benjamin Cuningham
Benjamin Cuningham

Reputation: 886

Based on the timestamps from the console, your code is not "stuck in a loop" so much as it is in the main loop. Every tick where numberOf.Harvesters < 3 evaluates to true, the code in the if block will be executed. Assuming the creep counting part is working, this means it will keep executing until you have at least 3 harvesters.

Perhaps you should add some code to check if the spawn is already spawning another creep and if there is enough available energy before calling Spawn1.createCreep(). This will greatly reduce the chatter in your console.

If there are already 3 harvesters, try adding something like console.log('harvester count: ' + numberOf.Harvesters); right before the if statement.

Upvotes: 2

Related Questions