Trying to get energy out of everything but spawns, extensions, and towers



  • let structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
                filter: (s) => s.structureType != STRUCTURE_ROAD || STRUCTURE_SPAWN || STRUCTURE_EXTENSION || STRUCTURE_TOWER
    && s.energy > 0
    });
    

    So this is what I have to figure out what structures that store energy are, and then I have my creeps try to withdraw the energy. But it doesn't work, and when I had it console.log(structure) it returned two roads and their IDs, and now it is showing a link AND a road. Does a road have energy storage..?



  • When you say s.structureType != STRUCTURE_ROAD || STRUCTURE_SPAWN || STRUCTURE_EXTENSION || STRUCTURE_TOWER, I assume you want Javascript to interpret it as "the structure type is neither STRUCTURE_ROAD, nor STRUCTURE_SPAWN, nor STRUCTURE_EXTENSION, nor STRUCTURE_TOWER". Javascript doesn't understand this code that way. If you want to say "the structure type is not equal to any of these objects", you have to specify them individually.

    s.structureType != STRUCTURE_ROAD && s.structureType != STRUCTURE_SPAWN && s.structureType != STRUCTURE_EXTENSION && s.structureType != STRUCTURE_TOWER



  • @jbyoshi pheonix: Its about priority /order of evaluation of the || and =! operators (like in Math, first "*", then "+" ).
    So in s.structureType != STRUCTURE_ROAD || STRUCTURE_SPAWN the != gets evaluated first, and then || STRUCTURE_SPAWN, which is always true (in JS) .



  • @jbyoshi Oh ok thank you



  • Also, it might be worth pointing out that && has a higher precedence than ||, which means that:

    A || B || C && D
    // equivalent to:
    A || B || (C && D)
    // NOT:
    (A || B || C) && D
    

    If you want to do all the ORs and then the AND, you'll need to add some brackets.