Efficient code for entering a code

  • My creeps blink between rooms at random when trying to enter a room.

    Do any of you have a great code snippet that makes a creep enter a room efficiently?


  • Creeps blink when they dont know what to do once they enter a new room. I would suggest review your code and find the broken bits.

    As for your request:

    if((creep.pos.x == 0) || (creep.pos.y == 0) || (creep.pos.x == 49) || (creep.pos.y == 49))

    creep.moveTo(25, 25);

    This is super basic and will not work is pos 25/25 is blocked or unreachable. You can replace it with advanced logic like moving towards controller or something but this should stop your creeps blinking long enough to find out whats the actual problem 🙂

  • I had some problems dealing with that too.
    Here's what i did to keep it simple.

    I save the room my creep is supposed to work in memory on spawn.
    example: spawn.createCreep(body, "", { role: 'harvester', room: 'ABC12'});

    I create a flag in the middle of every room i want to move in called exactly the same name as the room.
    Then, before calling my creeps roles, i make them move to that flag if they're not in the correct room.
    So the moment it steps into the right room, it starts doing it's job, doesn't matter where the "roomFlag" is.

    if (creep.memory.room != creep.room.name)
    var roomFlag = Game.flags[creep.memory.room];
    if (roomFlag)
    switch (creep.memory.role)
    case "miner": this.miner(creep); break;
    case "carrier": this.carrier(creep); break;
    case "upgrader": this.upgrader(creep); break;
    case "transfer": this.transfer(creep); break;
    case "retire": this.retire(creep); break;
    case "repair": this.repair(creep); break;
    case "builder": this.builder(creep); break;
    case "reserver": actions.reserve(creep); break;
    case "claimer": actions.claim(creep); break;
    case "support": this.support(creep); break;
    case "soldier": actions.fight(creep); break;
    default : console.log(creep.name + " is a " + creep.memory.role + " in " + creep.room.name + " and it doesnt know what to do")

  • Thanks for the code.

    I also tried this:

    if (this.pos.x == 49) {this.move(LEFT);}
    if (this.pos.x == 0 ) {this.move(RIGHT);}
    if (this.pos.y == 49) {this.move(TOP);}
    if (this.pos.y == 0 ) {this.move(BOTTOM);}

    And it also seems to work.

    My problem was made worse when I skipped all creeps with fatigue > 0. But creeps with 0 < fatigue < 1 seem to be able to move so now I only skip creeps with fatigue >=1.