Find empty spawn in room with full extensions.



  • How do you work out if a spawn can accept more energy in a room with extensions?

    Say you have ten filled extensions and one or more empty spawns. If you query the spawn, it tells you its energyCapacity is 0. Presumably to stop it gaining one energy per turn.

    You can ask the room if it's room.energy < room.energyCapacity. If the room is not at capacity, you know that at least one of the spawns is not at capacity, but not which one. Can't see how you could tell which without visiting each spawn in turn and seeing if anything happens if you transfer. (edit: Although I seem to looking at a room with energy 300 and energycapacity 0 so maybe that won't work.)

    I feel sure that I have to be missing something here, but not sure what.

    So how, in a room with a mixture of spawns and extensions, can you guarantee finding the one that will accept an energy transfer?

    This seems a total fail.

    const nextSourceContainer = this.creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: function(structure) { return (structure.structureType === STRUCTURE_EXTENSION || structure.structureType === STRUCTURE_SPAWN ) && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } });



  • room.find(FIND_MY_SPAWNS).filter((s:StructureSpawn) => s.store.getUsedCapacity(RESOURCE_ENERGY) === 0)



  • @gadjung said in Find empty spawn in room with full extensions.:

    room.find(FIND_MY_SPAWNS).filter((s:StructureSpawn) => s.store.getUsedCapacity(RESOURCE_ENERGY) === 0)

    Thanks for replying, but this answer does not find any extensions unless you are saying that you should never fill extensions?!?

    Anyway, I am still really confused. I have a room with:

    room.energyAvailable 317

    room.energyCapacityAvailable null

    I don't understand how "Total amount of energyCapacity of all spawns and extensions in the room." is null here? https://docs.screeps.com/api/#Room.energyCapacityAvailable

    The 317 is 50 from each of 5 extensions and 67 from the spawn. Querying the spawn gives.

    spawn store {"energy":67}

    spawn getCapacity null

    spawn getFreeCapacity null

    spawn getUsedCapacity 67

    Which seems to be saying that the spawn which has 67 energy, has no energy capcity.

    Code I used to get this.

    const home = Game.rooms["W7N7"];

    console.log("home energyAvailable", home.energyAvailable, "energyCapacityAvailable",home.energyCapacityAvailable);

    const spawn = Game.spawns["Spawn1"];

    console.log("spawn store", JSON.stringify(spawn.store), "spawn getCapacity", spawn.store.getCapacity(RESOURCE_ENERGY), "spawn getFreeCapacity", spawn.store.getFreeCapacity(RESOURCE_ENERGY), "spawn getUsedCapacity", spawn.store.getUsedCapacity(RESOURCE_ENERGY));

    With result:

    home energyAvailable 317 energyCapacityAvailable null spawn store {"energy":67} spawn getCapacity null spawn getFreeCapacity null spawn getUsedCapacity 67



  • So this is the first time I have looked at the screeps engine, so I am probably missing stuff. However

    https://github.com/screeps/engine/blob/master/src/game/game.js#L316

    register.rooms[object.room].energyAvailable += object.store.energy;

     register.rooms[object.room].energyCapacityAvailable += object.storeCapacityResource.energy;
    

    We are += on something that could be null, in which case it would just turn null and stay that way forever!

    https://github.com/screeps/engine/blob/master/src/game/store.js#L28

    Here stuff in the store is set to "null" rather than "0" if the resource is missing.

    I am just guessing, but I suspect there might be an issue here. The bit in game.js was only done 11 months ago. https://github.com/screeps/engine/commit/879b4ca215f1e0b7fd92c7b32c7e59bdc4722cfc#diff-47818c887f19982321abd70c2e741871



  • const nextSourceContainer = this.creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: function(structure) { return (structure.structureType === STRUCTURE_EXTENSION || structure.structureType === STRUCTURE_SPAWN ) && structure.store.energy > 0; } });

    structure.store.energy > 0; Being key - this will return all structures that have energy.

    If you're looking for spawn/extensions that need energy filled you can replace "structure.store.energy > 0" for "!structure.store.energy || structure.store.energy < structure.store.getCapacity(RESOURCE_ENERGY) " - !structure.store.energy will detect null/undefined and 0.

    I would recommend getting on Slack, and the help channel. People are a bit more active than in these forums.



  • @likeafox

    Thanks, I'll try your suggestion.

    I am also using room.energyCapacity to work out how big to build my creeps. I guess I will have to replace that with something that explicitly counts spawns and extensions. Plus my spawn no longer accepts energy despite having only 67 energy.

    Might be an idea to move this post to the bugs forum.



  • @ayrtep I would use room.energyCapacityAvailable to figure out how big you can build you're creeps, that way you don't have to count you're spawns/extensions.

    Your spawn should always accept energy unless the RCL isn't high enough, do you know what error is coming back after you try a transfer?



  • @likeafox said in Find empty spawn in room with full extensions.:

    @ayrtep I would use room.energyCapacityAvailable to figure out how big you can build you're creeps, that way you don't have to count you're spawns/extensions.

    If you read what I said above

    room.energyCapacityAvailable returns null

    so I just created a wrapper function to do it the long way until the 'bug?' gets fixed and room.energyCapacityAvailable returns the room's energyCapacityAvailable.

    I checked the creep.transfer. The transfer is successful, but as far as I can tell no energy is transferred to the spawn. On the GUI the spawn's energy status stays around 50-80. Sometimes it goes up in ones, I think after I use the energy in the extensions to make a creep.

    I am not sure if this is relevant, but it's happening on a steam private server.



  • You man have corrupted your api or your DB entries might be corrupt. Try these:

    Game.spawns.Spawn1.store.getCapacity()
    Game.spawns.Spawn1.store.getCapacity(RESOURCE_ENERGY)
    Game.spawns.Spawn1.store.getCapacity('energy')
    Game.spawns.Spawn1.energyCapacity
    

    If this is on MMO it is very unlikely to be a DB error. If this is a private server, you may have found a problem when db upgraded to use the new Store schema.



  • @deft-code

    Thanks, I'll try that the next time I reproduce.

    I have reset my private server with a system.resetAllData(), and the new game does not have this problem.

    It could easily be some form of corruption. My development cycle involves deleting and replacing the files in my .../127_0_0_1___21025/default directory every few minutes.



  • Spawn will stop generating energy if the combined energy of spawn + extensions in the room is 300 or above. Does that make any difference for you?



  • @deft-code said in Find empty spawn in room with full extensions.:

    Game.spawns.Spawn1.energyCapacity

    Ok, It happened again. The trigger this time might have been stopping the server for eight hours then restarting.

    I tried

        console.log("Game.spawns.Spawn1.store.getCapacity()", Game.spawns.Spawn1.store.getCapacity());
        console.log("Game.spawns.Spawn1.store.getCapacity(RESOURCE_ENERGY)", Game.spawns.Spawn1.store.getCapacity(RESOURCE_ENERGY));
        console.log("Game.spawns.Spawn1.store.getCapacity('energy')", Game.spawns.Spawn1.store.getCapacity('energy'));
        console.log("Game.spawns.Spawn1.energyCapacity", Game.spawns.Spawn1.energyCapacity);
    

    and got

    Game.spawns.Spawn1.store.getCapacity() null
    Game.spawns.Spawn1.store.getCapacity(RESOURCE_ENERGY) null
    Game.spawns.Spawn1.store.getCapacity('energy') null
    Game.spawns.Spawn1.energyCapacity 0



  • @ayrtep

    Happened again. Looks like restarting a steam private server is very likely to set the spawn energy and hence room energy to null.

    🤔


  • Having the same issue





  • got the exact same issue here, after rebooting vm machine and restarting private server.

    spawn.store and spawn.storeCapacityResource become an empty object.

    if put energy into the spawn, spawn.store["energy"] will reappear, but spawn.storeCapacityResource still empty.



  • @ayrtep said in Find empty spawn in room with full extensions.:

    So this is the first time I have looked at the screeps engine, so I am probably missing stuff. However

    https://github.com/screeps/engine/blob/master/src/game/game.js#L316

    register.rooms[object.room].energyAvailable += object.store.energy;

    register.rooms[object.room].energyCapacityAvailable += object.storeCapacityResource.energy;

    We are += on something that could be null, in which case it would just turn null and stay that way forever!

    https://github.com/screeps/engine/blob/master/src/game/store.js#L28

    Here stuff in the store is set to "null" rather than "0" if the resource is missing.

    I am just guessing, but I suspect there might be an issue here. The bit in game.js was only done 11 months ago. https://github.com/screeps/engine/commit/879b4ca215f1e0b7fd92c7b32c7e59bdc4722cfc#diff-47818c887f19982321abd70c2e741871 https://www.worktime.com/

    I am not sure if this is relevant, but it's happening on a steam private server.