Screeps: conditions won't work - ignorant

So, I tried this to automatically spawn creeps as a percentage of the total live creep. however, when I run this, it just keeps propagating the harvesters completely ignoring the conditions, even though the console.log is returning the expected results.

and now I donโ€™t know whatโ€™s going wrong.

//creepManager.creations() == counts total creeps and spawns creeps in function of total
    var spawnCreep = require('spawnCreep');
    var counter = require('counter');
    exports.creations=function(){

    if(  counter.guardCount()/counter.totalCount()<0.5 && counter.harvesterCount()>1){

        spawnCreep.guard();

    } else if (counter.harvesterCount()/counter.totalCount()<0.3){

        spawnCreep.harvester();

    } else if(counter.builderCount()/counter.totalCount()<0.2){

        spawnCreep.builder();
    } else {
        spawnCreep.guard(); //default
    }
};  // 5guards, 3harvesters, 2 builder per 10CREEPS`

      

(spawnCreep is another module that keeps track of how sliders are built)

0


source to share


1 answer


I was doing something like this in my old code:

function allocateResources() {
    var counts = {guard : 0, healer : 0}
    for (var name in Game.creeps) { 
        if (name.indexOf("guard") > -1) {
            counts["guard"]++;
        } else if (name.indexOf("builder") > -1) {
            counts["builder"]++;
        }
        // ...
        counts["total"]++;
    }

    if (counts["guard"] / (counts["total"] + 1) < 0.51) {
       spawnCreep("guard"); 
    } else if (counts["builder"] / (counts["total"] + 1) < 0.34) {
       spawnCreep("builder");
    } 
    // ...
}

      



You have to make sure that you avoid division by zero, which might be a mistake for you.

0


source







All Articles