Claiming a new room, please help



  • I have recently reached GCL 2 and am trying to claim a new room. i have been trying to use
    creep.moveTo(new RoomPosition(47, 25, 'E15S5'));
    however it is giving me error messages that make no sense. please give me a code that will tell my creep to move to a specific room, go to the controler in that room, and claim it



  • Hello @UncreativeUsername

    I'm pretty new too, so my approach was to stablish flag colors for different purposes. This way, you can do something like...

    if(creep.room != flag.room){
        creep.moveTo(flag);
    }else{
        creep.claim(creep.room.controller);
    }
    

    Hope it helps, and if you still want me to give you the code i use, just ask for it.



  • so creeps can see flags in other rooms? or do you place the flag on the border of the first room so the creep goes into the second room?

    I would love your code, but I also will want to know how it works, so if you are willing to give it to me expect potential questions on the how and why of the code.



  • Yes, creeps can directly moveTo(flag) even if that flag is in another room.

    Here is the code for my "flagReserver". It's obvious purpose is to reserve a room controller.

    //Assign new flag will execute if i remove the flag creep belongs to    
    const AssignNewFlag = require('routines.assignNewFlag');
    
    var roleFlagReserver = {
    
        run: function(creep) {
    
            let flag = Game.flags[creep.memory.targetFlag];
    
            let ctrl = creep.room.controller;
            
            //If creep has a flag    
            if(flag && flag.color == COLOR_PURPLE && flag.secondaryColor == COLOR_PURPLE ){
    
                //If they are in the same room
                if(creep.room == flag.room){
    
                    let reserveResult = creep.reserveController(ctrl);
    
                    if(reserveResult == ERR_NOT_IN_RANGE){
    
                        creep.moveTo(ctrl, {visualizePathStyle: {stroke: '#cc00cc'}});
    
                    }
    
                }else{
    
                    creep.moveTo(flag, {visualizePathStyle: {stroke: '#cc00cc'}});
    
                }
    
            }else{
    
                AssignNewFlag.run(creep, COLOR_PURPLE, COLOR_PURPLE);
    
            }
    
        }
    
    };
    module.exports = roleFlagReserver;
    

    Now i realize that rather than having the creep trying to reserve each tick, i should check creep.pos.isNearTo(ctrl) and then start reserving, but as an example i think it's ok.

    Feel free to ask whatever you need.

    This is the code for my "flagManager":

    const CreateCreep = require('routines.spawnCreep');
    const Show = require('show.info');
    
    var FlagManager = {
    
        run: function(flag){
        
            let desiredMinions = 0;
            let currentMinions = 0;
            let ticksToDie = 100;
    
    
    
            if(Number.isInteger(parseInt(flag.name))){
                desiredMinions = parseInt(flag.name);
                flag.memory.desiredMinions = desiredMinions;
            }else{
                console.log('Flag "'+flag.name+'" at room '+flag.room.name+' has a wrong name!');
            }
        
    
            for(let i in Game.creeps){
    
                let creep = Game.creeps[i];
                if(creep.memory.targetFlag == flag.name && creep.ticksToLive > ticksToDie ){
            
                    currentMinions++;
                }
            }
            flag.memory.currentMinions = currentMinions;
            if(SHOW_FLAG_INFO){
                Show.assignedMinions(flag, desiredMinions, currentMinions);
            }
    
    
            if(currentMinions < desiredMinions){
            
                let minDist = 10;
                let distance = 0;
                let targetSpawn = null;
    
                if(_.isString(flag.memory.targetRoom)){
    
                    targetSpawn = Game.getObjectById(Game.rooms[flag.memory.targetRoom].memory.spawns[0].id);
    
                }else{
                    for (let i in Game.rooms){
    
                        if(Game.rooms[i].controller.my){
        
                            distance = Game.map.getRoomLinearDistance(flag.pos.roomName, Game.rooms[i].name, false);
                            if(distance < minDist && null!=Game.rooms[i].memory.spawns){
                                minDist = distance;
                                flag.memory.targetRoom = Game.rooms[i].name;
        
                                targetSpawn = Game.getObjectById(Game.rooms[flag.memory.targetRoom].memory.spawns[0].id);
                                break;
                            }
                        }
                    }
            }    
    
    
                if(targetSpawn){
    
                    let extendedAttributes = {};
                    let creepRole = '';
                
                    switch(flag.color){
    //RESERVE
                        case COLOR_PURPLE:
                            if(flag.secondaryColor == COLOR_PURPLE){
                                creepRole = 'flagReserver';
                                extendedAttributes = {myRoom: flag.memory.targetRoom,   mySpawn: targetSpawn.name,      targetFlag: flag.name.toString()    };
                            }else if(flag.secondaryColor == COLOR_WHITE){
                                creepRole = 'flagClaimer';
                                extendedAttributes = {myRoom: flag.memory.targetRoom,   mySpawn: targetSpawn.name,      targetFlag: flag.name.toString()    };
                            }
                        break;
    //HARVEST
                        case COLOR_BLUE:
                            if(flag.secondaryColor == COLOR_BLUE){    
                                creepRole = 'flagHarvester';
                                extendedAttributes = {myRoom: flag.memory.targetRoom,   mySpawn: targetSpawn.name,      targetFlag: flag.name.toString(),       harvesting: true    };
                            }
                        break;
    //BUILD/ASSIST NEW ROOM
                        case COLOR_GREEN:
                            if(flag.secondaryColor == COLOR_GREEN){    
                                creepRole = 'flagBuilder';
                                extendedAttributes = {myRoom: flag.memory.targetRoom,   mySpawn: targetSpawn.name,      targetFlag: flag.name.toString(),       harvesting : true   };
                            }else if(flag.secondaryColor == COLOR_WHITE){    
                                creepRole = 'flagRepairer';
                                extendedAttributes = {myRoom: flag.memory.targetRoom,   mySpawn: targetSpawn.name,      targetFlag: flag.name.toString(),       harvesting : true   };
                            }
                        break;
                    
                        default:    break;
                    }
                
                    let targetRoom = Game.rooms[flag.memory.targetRoom];
                    let createResult = CreateCreep.run(targetRoom, creepRole, extendedAttributes);
                }
            }
        }
    };
    
    module.exports = FlagManager;


  • const AssignNewFlag = require('routines.assignNewFlag');

    does this assign the creep to a flag? how do you control wich flag if you have multiple flags?

    edit: nvm i found at the bottom that you assign by color.



  • @uncreativeusername

    Creeps are created from "flagManager" and they already know which is their flag when they born. However, assignNewFlag is used in case a creep can't find his flag because i removed it.

    Here is "assignNewFlag"

    module.exports = {
    
        run: function(creep, flagColor,flagSecColor){
            
            //Assign the creep to a flag which "needs" it
            
                for(let i in Game.flags){
                    let thatFlag = Game.flags[i];
                    if(thatFlag.color == flagColor && thatFlag.secondaryColor == flagSecColor && Game.flags[i].memory.desiredMinions > Game.flags[i].memory.currentMinions){
                        creep.memory.targetFlag = thatFlag.name;
                    }
                }
            
            //If there aren't, just assign to a flag of the right color
            
                if(!(Game.flags[creep.memory.targetFlag] && Game.flags[creep.memory.targetFlag].color == flagColor && Game.flags[creep.memory.targetFlag].secondaryColor == flagSecColor)){
                    for(let i in Game.flags){
                        let thatFlag = Game.flags[i];
                        if(thatFlag.color == flagColor && thatFlag.secondaryColor == flagSecColor){
                            creep.memory.targetFlag = thatFlag.name;
                        }
                    }
                }
                
            //If i can't find the right flag after all, send creep back to the spawn it was born
    
                if(!Game.flags[creep.memory.targetFlag]){
                    creep.memory.targetFlag = null;
                    if(!creep.fatige){
                        creep.moveTo(Game.spawns[creep.memory.mySpawn], {reusePath: REUSE_PATH, visualizePathStyle: {stroke: '#ffffff'}});
                    }
                }
        }
    
    };


  • so i am guessing flagmanager is its own module?



  • @uncreativeusername Yes, each piece of code i posted has it's own module



  • so, i I just want one of my roles to involve flags, because my other roles are already working, how could I tell the one role to be assigned to a flag? like, without using assign new flag, and encorperating it into the same module?



  • @uncreativeusername The way i create the creeps involving flags is by using the name of the flag. It must be an integer (could be 1, 01, 001....2....), and it means the number of creeps you want for that flag.

    You can take my "flagManager" code and reduce the switch statement so it works only with 1 color, and if you need in the future it'll be pretty easy to add more.

    Then, from the main module...

    for(let i in Game.flags){
        let thisFlag = Game.flags[i];
        FlagManager.run(thisFlag);
    }
    

    You don't really need to use "assignNewFlag" as long as you make sure the flag exists as long as creep does.



  • or, could I do somthing like this in the module:

    var roleclaimer = {

    run: function(creep) {

    let flag = Game.flags[creep.memory.targetFlag];
    
    let ctrl = creep.room.controller;
    
    if(flag){
        
        if(creep.room == flag.room){
    
            let claimResult = creep.claimController(ctrl);
    
            if(claimResult == ERR_NOT_IN_RANGE){
    
                creep.moveTo(ctrl, {visualizePathStyle: {stroke: '#cc00cc'}});
    
            }
    
        }
        else{
    
            creep.moveTo(flag, {visualizePathStyle: {stroke: '#cc00cc'}});
    
        }
    
    }
    

    } }; module.exports = roleclaimer;

    and then, since i will only use each creep once, spawn the creep like this : Game.spawns['S1'].spawnCreep( [CLAIM, MOVE], 'Claimer1', { memory: { role: 'claimer' } {targetFlag: 'flag1'} );



  • @uncreativeusername Yes, despite possible errors on the code i think that should do it. I also started with a simpler code, but it became more and more complex as i detected exceptions, errors and undesired behaviours.

    I recommend you to do it with a new role whose body is just [MOVE] and see what happens, so you don't bankrupt while you solve it xDD



  • @Calfa thank you this is a huge help.



  • @uncreativeusername A big pleasure to help. Even more considering the amount of knowledge and help i got from this forum.



  • confirmed, role.claimer is functional!

    👏