Droping 0 drops all



  • When executing 'creep.drop(RESOURCE_ENERGY, 0);', instead of dropping nothing, the creep drops all carried energy. (Althought you would rarely explicitly drop 0, in my case 0 was the result of some formula. Took me a while to get while sometimes all energy was dropped...)

    On the other hand, it's good that you don't waste 0.2 CPU on dropping nothing, but still, the behavior is quite non-intuitive...

    The fix most likely would be to not use 'if(!arg)' to test whether the argument is undefined 😉



  • I would guess the behaviour comes from the amount being an optional argument, if you just pass a resource type to the creep.drop() function it will drop all of that resource. It appears that passing it 0 is a similar case from the function's perspective.



  • This is correct. At the time of writing the snippet from @screeps/engine/src/game/creeps.js#drop is:

      Creep.prototype.drop = register.wrapFn(function(resourceType, amount) {
            if(!this.my) {
                return C.ERR_NOT_OWNER;
            }
            if(this.spawning) {
                return C.ERR_BUSY;
            }
            if(!_.contains(C.RESOURCES_ALL, resourceType)) {
                return C.ERR_INVALID_ARGS;
            }
            if(!data(this.id)[resourceType]) {
                return C.ERR_NOT_ENOUGH_RESOURCES;
            }
            if(!amount) {
                amount = data(this.id)[resourceType];
            }
            if(data(this.id)[resourceType] < amount) {
                return C.ERR_NOT_ENOUGH_RESOURCES;
            }
    
            intents.set(this.id, 'drop', {amount, resourceType});
            return C.OK;
        });
    

    This could be resolved with by changing

    if(!amount) {
                amount = data(this.id)[resourceType];
            }
    

    to

    if(amount == undefined) // double equals important here for null or undefined
        amount = data(this.id)[resourceType];
    if(amount === 0)
        return C.OK;
    


  • easier fix:

    if(!amount && amount !== 0) {
        amount = data(this.id)[resourceType];
    }