How i can control second room?



  • When i level up to 2 gcl. I think about control second room. But how i can control creeps in second room? Duplicate code or how? 😐



  • As a total beginner myself I found the following things helpful (mostly because that was what I had to change when I invaded the first room):

    Avoid using anything like Game.controllers.MyController, instead loop over the rooms like:

    for(var room_it in Game.rooms) {
        var room = Game.rooms[room_it]
        var spawn = room.find(FIND_MY_SPAWNS)[0]; // TODO: decide on multi spawn rooms
    

    This is the start of the loop that decides if any creeps need to be spawned. The following code will make decisions mostly based on number of creeps in this room (which I count in a different loop) and room params like energy available. Also there is a global object create where I hard code some tasks for each room that I can't calculate automatically (yet anyway, maybe when my code gets better).

    room_strategy : {
      'W5S7': {
        maintenance_area: {ax: 11, ay: 5, bx: 46, by: 30},
        min_wall: 300000,
    },
    'W4S7': {
      maintenance_area: {ax: 0, ay: 0, bx: 49, by: 49},
      min_wall: 75000,
    }
    

    Maintenance area is the area in which the creeps should upgrade walls (a bit specific to my rooms) and min_wall is the level to which they should upgrade (My older room with better walls is at a higher level). That prevents that creeps upgrade one wall to a super high value before going on with the next wall. That way I just adjust the level once a day. There are more such values in this data structure.

    The creeps themselves are just looped like this:

    for(var name in Game.creeps) {
    Game.creeps[name].runTask();
    }

    runTask() is a method I added to the Creeps.prototype.

    They will then take their current room object and make all decisions based on that (eg instead of moving energy to Game.spawns.MyFirstSpawn they will use

    creep.room.find(FIND_MY_SPAWNS)[0]
    

    Since by now I have only one spawn in my rooms (and all my rooms have a single energy source) that works good enough. There is no code duplication at all. Just make sure when you calculate any task targets based on the creeps position and room. I use something like

    creep.pos.findClosest(FIND_STRUCTURES... 
    

    a lot. Finding targets to get or drop energy for haulers, finding construction sites for builder or whatever, just use those room/position based finders and your code will run the same in all rooms.

    The only type of creep that needs special attention are those that move from one room to the next. Especially in the beginning I found it very helpful to have a permanent stream of creeps moving from room 1 to room 2 and helping with harvesting energy, since without extensions you can only build very small creep that do not fully harvest energy fast enough.

    They start with the second room as target hardcoded and will then move to the exit. Once they reach the exit and are in the next room I just switch the 'role' and 'task' stored in their memory. Then they are simple harvesters and their task assignment code will use above finders to search for the rooms energy source like any other harvester.



  • I do not understand it 😢



  • Well, it's difficult to explain since I don't know your current code. If your current setup is running you should have code that makes creeps do stuff in your main room. Like look where the energy source is, move to the energy source and harvest it. After that move to the next spawn or storage and deliver the energy. Then back to the energy source and repeat. That would be a simple setup for a harvester. You don't have extra code for every single harvester, they all share the same code.

    So far there isn't much involved that would require any code to be duplicated or anything else very special if this creep would operate in another room.

    The only special task you need is to move a creep into another room and claim the controller. For this I had a creep type 'invader'. It would use findExit() to get into the next room, then just go to the room controller and claim it. From there just harvest energy and move back to the controller and upgrade it. Once you can build a spawn and extension the room operates normally like your first room.

    Another matter would be those rooms without a controller. You just need to move there, harvest energy and when the creep is full move back. (Source keepers make this much more difficult of course)



  • Hey guys,

    I'm sorry to bring this up again, but I have a question about this topic, too.

     

    so, assuming I do the stuff in the main loop so it works for any room. e.g. assigning creep roles managing tower and stuff. I would only need to make a seperate autospawn for a second spawner, but I don't have that yet anyway, as my controller just got lo lvl 4.

    SO, I would need to make it so my creeps I want there only ever work in one room, and if they are not there, then move to that room. 

     

    My question is if functions like find(MY_STRUCTURES) search only one room.

     

    or like this, so does this do it for the current room of the creep?

    if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
    creep.moveTo(creep.room.controller);
    }

     

    I'm kind of wondering about the basics of multirooms. also without using too much extra cpu, beacause I see some ppl with many many rooms and I'm spiking to 7 or 13 sometimes with just 1 room.



  • room.find, creep.pos.findclosest, and so one all work in only one room. creep.room returns the room the creep is in.
    Unless you move a creep out of the current room all your role scrips should work in the room the creep is in. If they are in similar style to the tutorial scripts.

    If you claim another room and have a spawn there you only need to setup autospawning for that spawn and you should be done.
    Your autospawning should use room specific data. Just iterating over Game.creeps to count builders and such doesnt do the job. Since Game.creeps has all your creeps. You have to filter on the room you want to spawn for.

    Dont use roomnames or IDs in static code. Like the old poster did. Your code will be useless if you need to respawn for example.



  • I know this is old, but here is a suggestion:

    In your main loop .. loop through all rooms and then all the creeps in the room. 

    From there I would loop through the creeps and make decisions based on their role and logic. 

    I think basing your main loop on spawns is a mistake, many of your important interactions will not be spawn based. Creeps harvesting in other rooms, attacking, chasing down the enemy to punish them for their hubris...

     

    for(var thisRoom in Game.rooms) {

    for (var thisCreep in thisRoom.find(FIND_MY_CREEPS)) {
    // Handle creeps in this room
    }
    }


  • Your question is very broad. 

    Loop through all creeps in the game and you don't need to worry about rooms.

     

    for(var name in Game.creeps) {
    var creep = Game.creeps[name];
    //run creep role code
    }

     

    If your question is regarding a second spawn , the above still applies.

    You may however want to let creeps store their home / source room in memory when spawning.

    var roomName = creep.room.name;
    creep.memory.sourceRoom = roomName;