Search code examples
screeps

Nested filter doesn't work


// Build structures
var structs = curRoom.find(FIND_CONSTRUCTION_SITES, {
    filter: (structure) => {
        return
            _.filter(Memory.jobs.worker.normal,
                (job) => {
                    return job.id == structure.id;
                }).length == 0 // Test if this structure is already in the queue
            && _.filter(Game.creeps,
                (creep) => {
                    return creep.memory.curJob != undefined && creep.memory.curJob.id == structure.id;
                }).length == 0; // Test if a creep is already working on this structure
    }
});

The code above returns 0 construction sites (out of the 40 that should pass the tests), but every construction site passes the (outer) filter.

I have also tested the inner filters (with the .length == 0):

console.log(<filter1>); // true

console.log(<filter2>); // true

console.log(<filter1>, <filter2>) // true true

console.log(<filter1> && <filter2>

Is there something I am missing or have I done something completely wrong?


Solution

  • I solved this problem by performing the inner filters before (and outside of) the outer filter. This also saves some time because the filters, which are mostly the same, won't have to be performed multiple times (for other actions like repairing etc)