Issue with the target for the method creep.reserveController(target).



  • Preconditions:

    "shard" is 3.

    "creep" is the my creep that has a body [MOVE, CLAIM, CLAIM].

    "controller" is a neutral controller in the room next to my main room.

    My "creep" isNearTo "controller".

    My code is:

            let controller_1 = Game.getObjectById('5bbcaabd9099fc012e6321b9');
            let controller_2 = creep.room.lookForAt(LOOK_STRUCTURES, flag.pos.x, flag.pos.y)
            console.log("Controller 1 is " + controller_1)
            console.log("Controller 2 is " + controller_2)
            let result_1 = creep.reserveController(controller_1);
            let result_2 = creep.reserveController(controller_2);
            console.log("Result 1 = " + result_1)
            console.log("Result 2 = " + result_2)
    

    Actual output in the console is:

    [21:15:21][shard3]Controller 1 is [structure (controller) #5bbcaabd9099fc012e6321b9]
    [21:15:21][shard3]Controller 2 is [structure (controller) #5bbcaabd9099fc012e6321b9]
    [21:15:21][shard3]Result 1 = 0
    [21:15:21][shard3]Result 2 = -7
    

    But I expect both methods to work. (Or, at least, both methods not to work)

    I don't know what tools you have to reproduce that and I'm not sure how are you supposed to reproduce that... It doesn't reproduced on the [sim] room(



  • From the API Documentation: https://docs.screeps.com/api/#Creep.reserveController

    ERR_INVALID_TARGET -7 The target is not a valid neutral controller object.

    The error code in your example makes sense if the first intent claimed it, and the second failed because it is claimed.

    Can you try other variations of your example? Reversing the order of the .reserveController calls?



  • I've added the loop

            let val;
            for(val in controller_X) {
                console.log(controller_1[val])
            }
    

    after both controllders were defined.

    For the controller_1 that loop returns that:

    [22:03:55][shard3]5bbcaabd9099fc012e6321b9
    [22:03:55][shard3][room W42S38]
    [22:03:55][shard3][room W42S38 pos 18,39]
    [22:03:55][shard3]undefined
    [22:03:55][shard3]function (id) {
            OwnedStructure.call(this, id);
        }
    [22:03:55][shard3]undefined
    [22:03:55][shard3][object Object]
    [22:03:55][shard3]0
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]0
    [22:03:55][shard3]undefined
    [22:03:55][shard3]false
    [22:03:55][shard3]function() {
            var result = {};
            for(var i in this) {
                if(i[0] == '_' || _.contains(['toJSON','toString'],i)) {
                    continue;
                }
                result[i] = this[i];
            }
            return result;
        }
    [22:03:55][shard3]function() {
    
            if(!this.my) {
                return C.ERR_NOT_OWNER;
            }
    
            intents.set(this.id, 'unclaim', {});
            return C.OK;
        }
    [22:03:55][shard3]function() {
    
            if(!this.my) {
                return C.ERR_NOT_OWNER;
            }
            if(this.safeModeAvailable <= 0) {
                return C.ERR_NOT_ENOUGH_RESOURCES;
            }
            if(this.safeModeCooldown || this.upgradeBlocked > 0 ||
                this.ticksToDowngrade < C.CONTROLLER_DOWNGRADE[this.level]/2 - C.CONTROLLER_DOWNGRADE_SAFEMODE_THRESHOLD) {
                return C.ERR_TIRED;
            }
            if(_.any(register.structures, i => i.structureType == 'controller' && i.my && i.safeMode)) {
                return C.ERR_BUSY;
            }
    
            if(lastActivateSafeMode) {
                intents.remove(lastActivateSafeMode, 'activateSafeMode');
            }
            lastActivateSafeMode = this.id;
    
            intents.set(this.id, 'activateSafeMode', {});
            return C.OK;
        }
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]undefined
    [22:03:55][shard3]controller
    [22:03:55][shard3]function() {
            return `[structure (${this.structureType}) #${this.id}]`;
        }
    [22:03:55][shard3]function() {
            if(!this.room) {
                return C.ERR_INVALID_TARGET;
            }
            if(!this.room.controller || !this.room.controller.my) {
                return C.ERR_NOT_OWNER;
            }
    
            if(this.room.find(C.FIND_HOSTILE_CREEPS).length > 0 ||
                this.room.find(C.FIND_HOSTILE_POWER_CREEPS).length > 0) {
                return C.ERR_BUSY;
            }
    
            intents.pushByName('room', 'destroyStructure', {roomName: this.room.name, id: this.id});
            return C.OK;
        }
    [22:03:55][shard3]function(enabled) {
            if(!this.room) {
                return C.ERR_INVALID_TARGET;
            }
            if(this.my === false || (this.room.controller && this.room.controller.owner && !this.room.controller.my)) {
                return C.ERR_NOT_OWNER;
            }
            if(!_.isBoolean(enabled)) {
                return C.ERR_INVALID_ARGS;
            }
    
            if(enabled != data(this.id).notifyWhenAttacked) {
    
                intents.set(this.id, 'notifyWhenAttacked', {enabled});
            }
    
            return C.OK;
        }
    [22:03:55][shard3]function() {
            if(!this.owner) {
                return true;
            }
            if(!C.CONTROLLER_STRUCTURES[data(this.id).type]) {
                return true;
            }
            if(!this.room || !this.room.controller) {
                return false;
            }
            return utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id));
        }
    

    But for the controller_2 that loop returns that:

    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]function Array() { [native code] }
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]function toString() { [native code] }
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    [22:03:23][shard3]undefined
    

    Look like that is the reason why method creep.reserveController(target) doesn't work for the second case. Is that an error, or expected behavior?



  • @keidence "Reversing the order of the .reserveController calls?"

    It doesn't affect 😞



  • Oh I see.

    Take a look at .lookForAt. https://docs.screeps.com/api/#Room.lookAtArea

    It says it returns an array of objects found, not a single object. I think it's unfortunate that screeps structures .toString uses square brackets.

    Try doing .reserveController(controller_2[0]). Does that give the results you expected?



  • I'm stupid 😞 Yesssss! Thank you a lot! That works nice!

    👍


  • Can you accept my answer to mark this topic resolved?



  • @keidence Done! Thank you again!