room.energyCapacityAvailable returns null due to corrupted spawn store



  • Hi guys,

    Since the store object updates I've been seeing some strange things on my local server. At some stage my room starts return null for the room.energyCapacityAvailable while there actually is a spawn and extensions in the room.

    After diving a bit in the code I noticed that this triggered by my spawn having an empty store object, this is received from JSON.stringify(Game.getObjectById(spawnId)):

    {
      "id": "530636879f471b6",
      "room": {
        "name": "W1N7",
        "energyAvailable": null,
        "energyCapacityAvailable": null,
        "visual": {
          "roomName": "W1N7"
        }
      },
      "pos": {
        "x": 35,
        "y": 12,
        "roomName": "W1N7"
      },
      "name": "Spawn1",
      "energyCapacity": 0,
      "spawning": null,
      "store": {},
      "owner": {
        "username": "QzarSTB"
      },
      "my": true,
      "hits": 5000,
      "hitsMax": 5000,
      "structureType": "spawn"
    }
    

    I've had it several times already and used system.resetAllData() to wipe everything. When manually fixing the db.json file to set the store object to { "energy": 300 } it works until the issues starts again (usually takes a couple thousand of ticks).

    Edit: Please note that according to the screeps client there should be energy in the spawn. Also the auto-regen of energy doesn't work anymore while the total room energy is below 300.


  • Dev Team

    @qzarstb Thank you for the report. Any idea how this can be reproduced?



  • @o4kapuk I'm not sure how to exactly reproduce the issue. It might be related to very high tick rate (running at <100 ms) in combination with the simple json db storage or completion of extensions since I encounter it when doing early game testing of my code. It usually occurs within first 10k ticks.

    I have the issue both with running the private server through the screeps client and running the "standalone" server. Have not encountered it on MMO with same code base (however I don't see how code base could be breaking this, defenitely not overwriting any of the room properties anywhere in my code).



  • @o4kapuk

    I've looked further into the issue and I think I've found it. I think it's caused by providing the energyStructures towards the spawnCreep call, and one of the extensions that you pass has no energy and therefore an empty store object.

    In the "newEnergyHandling" method you have this code:

        let availableEnergy = _.sum(energyStructures, id => roomObjects[id].store.energy);
        if(availableEnergy < cost) {
            return false;
        }
    

    and a bit further:

            let energyChange = Math.min(cost, energyStructure.store.energy);
            energyStructure.store.energy -= energyChange;
            bulk.update(energyStructure, {store:{energy: energyStructure.store.energy}});
    

    The lodash _.sum seems to be very robust and handles null/undefined without a problem. A Math.min() with undefined in it will give NaN, so I think this is were the problem originates from.

    When not providing energy structures to the spawnCreep call I can't reproduce the issue, so If it's not what I described above there's definitely something else wrong in there.

    👍