Creep keeps switching between 2 rooms



  • I've wrote a code so my harvester moves to a flag in the next room, harvests the source till hes full and then returns to my main room and store the energy in the storage.

     

    The problem is sometimes they stuck in an infinite lop of switching the rooms so triangle exit > switch > triangle exit > switch...

     

    http://pastebin.com/U5Q4nkQJ

     



  • Yes I have spent ages trying to get moving between rooms sorted out, I really with this was explained properly.

     

    I think maybe the flags and position objects are the way to go. Would be nice to have a clear example and explanation of moving between rooms. I expect its a nightmare for lots of others as well..



  • it happens because it cant find a path on where to move when it switches rooms. if you're sure it should find a path, check that its not trying to walk 'on' the edge as that makes it not move at all. i find that using roads and not enough move parts makes them walk off the edges exactly where you want btw



  • Seems like it is working now.




  • If anyone is interested this is what I finally came up with. The nextStepIntoRoom is to create an object that I know that the creep can find a path to. The first row in the room is always clear.

    I know its very clanky, feel to free to improve it.

    move: function(creep)
    {
    ...stuff
      var route = Game.map.findRoute(creep.room, creep.memory.targetRoom);
      if (route.length > 0) {
        var exit = creep.pos.findClosestByRange(route[0].exit);
        if (exit != null) {
          if (creep.pos.x == exit.x && creep.pos.y == exit.y) {
          var nextStep = this.nextStepIntoRoom(creep.pos, creep.memory.targetRoom);
          creep.moveTo(nextStep);
        } else {
          creep.moveTo(exit);
        }
       }
      }
      ..stuff
    }

    nextStepIntoRoom: function(pos, nextRoom) {
      var x = pos.x;
      var y= pos.y;
      if (pos.x == 0) {
        x =48;
      }
      if (pos.x == 49) {
        x = 1;
      }
      if (pos.y == 0) {
        y =48;
      }
      if (pos.y == 49) { 
        y = 1;
      }
      return new RoomPosition(x,y,nextRoom);
    },

     

    BTW This forum interface is horrible for typing in code.



  • I had a problem with creep jumping swapping between rooms. It seem like creep.room changes to a new room on second turn being in new room. However we need to move on the first tick not second in the new room. I haven`t figured out exact rule for that but this seems to fixed it for me.

    [code]

    // START OF HARVESTING
    if(creep_action == 'harvesting') {
    var targetRoomName = creep.memory.target;// load target room from creep memory
    if(creep.room.name != targetRoomName || (creep.room.name == targetRoomName && borderPosition(creep))){
    var targetPos = new RoomPosition(25,25, targetRoomName); // create position in the middle of target room.
    creep.moveTo(targetPos);
    } else {
    // when you are in the specified room find resource and go to it.
    var target_srouce = Game.rooms[targetRoomName].find(FIND_SOURCES)[creep.memory.sourceIndex];
    if(creep.harvest(target_srouce) == ERR_NOT_IN_RANGE) {
    creep.moveTo(target_srouce.pos);
    }
    }
    }
    // END OF HARVESTING

    [/code]

    1, there is a condition which check if we are not in the new room OR we are in the new room AND creep is in any of the border positions borderPosition(). if X or Y is either 0 or 49). then he still need to move on to the predefined position in the target room.
    2, finally when creep gets out of border space he look for the resource to mine.

    I don`t believe this is good solution , but it works for me until I find better one.



  • Having the same issue. I tried this code to keep things short, but still definitely a kludge:

    if(creep.pos.x*creep.pos.y === 0 || creep.pos.x === 49 || creep.pos.y === 49)
    creep.moveTo(new RoomPosition(25,25,creep.memory.workInRoom));

    Basically the first step in a room is a blind movement toward the centerpoint of the room. Then the next step is calculated normally. I know people like this actually. 🙂



  • Still broken a year later.



  • There are some “common” cases which may lead to strange movement behavior.

    • If you are using a different target pos for moving into the room and are calculating a new path as soon as you enter the room, the new path may be a route through the room where you actually came from (entering through an other entrance). As soon as the creep is leaving again, your other code part may calculate a simple path into the room again (e.g. to pos 25/25), and the procedure repeats.

      Setting maxRooms = 1 for path calculation after entering the target room, will force to take a path through that room only. It’s a quick solution for the problem. While it may still be an issue in “certain” scenarios, where you have to take another path into the room at all…
    • There may be some scenarios, where path calculation becomes expensive (cpu operations). There is a maxOps parameter, preventing to exceed a threshold (default:2000 (~2cpu)). You will not get a valid path if that threshold is reached.

      Increasing that threshold can solve the problem, but may lead to cpu issues if you are not caching paths. But I guess this one is more relevant for inter-room paths…