Page 1 of 1

how do collision masks work in request_path

Posted: Wed May 31, 2023 1:38 am
by rawls
So I have the following code that I've been trying to make work for a few days now. Essentially, when the move(x,y) function is called it automatically gets a path with request_path then has the player walk along that path. The code to handle the walking is not shown here but it works fine, the main problem is that I can't for the life of me figure out how to make request_path avoid water. I feel that I've tried a million combinations of collision maps and bounding boxes, but without fail request_path will go over water if it gets the chance.

Does anyone know how to solve this? There was also a recent forum post about a similar topic but the posted solution was not actually a solution.

Code: Select all

function move(x,y)
    local surface = game.get_surface("nauvis")
    local character = game.get_player(1).character
    local position = {x = x, y = y}
    local collision_mask = {
      "player-layer",
      "train-layer",
      "consider-tile-transitions",
      "water-tile",
      "object-layer"
  }

    --
    t = character.bounding_box
    
    --probable reason for pathing over water is collision masks.
    --follow this link for collision masks https://wiki.factorio.com/Types/CollisionMask

    pos = character.position
    --local bbox ={{pos.x - 0.5, pos.y - 0.5},{pos.x + 0.5, pos.y + 0.5}}
    --local bbox2 = {{-0.1,-0.1},{0.1,0.1}}
    surface.request_path{
        bounding_box = t,
        collision_mask = collision_mask,
        start = character.position,
        goal = position,
        force = "player",
        path_resolution_modifier = 0
    }
  end

Re: how do collision masks work in request_path

Posted: Wed May 31, 2023 3:01 am
by rawls
Update, so now I'm just confused on the collision mask hierarchy. Making a bounding box centered at 0,0 and only utilizing the "water-tile" collision mask means it's suddenly not pathing on water, but also it won't path over things like belts which it should

Re: how do collision masks work in request_path

Posted: Wed May 31, 2023 4:59 am
by boskid
By having bounding box centered at {0,0} and passing a water-tile collision mask it should not path over water anymore since that layer collides with water tile. Most likely what you say is not what your code does so please share your currently not working code piece to get accurate answers.

As for the collision layers, collision masks system has some limitations built in, one of them being that any 2 objects that share a collision layer will collide, even if they are 2 entities both colliding water-tile layer. If you want a path that is suitable for a character, you should use character's collision mask which i think consists of "player" and "train" layers. Those layers guarantee the path will not go over water (since water tile collides with player layer) but it will be able to go over transport belts as they do not have a shared layer here.