Hello,

I'm developing a tactical turn-based game and have encountered a challenge. I need to highlight all the tiles that are within a certain distance from a specific source tile using AStarGrid2D. This is to show players possible target tiles they can move to or interact with during their turn. While I'm familiar with using AStar to find the shortest path between two points, I'm unsure how to adapt it to find and highlight all tiles within a specified distance from a source.

I'm pondering whether to run multiple AStar searches or if there's a more efficient method to achieve this.

Has anyone tackled something similar or can guide me on this?

Thank you in advance!

  • xyz replied to this.

    gaborit Something like flood fill algorithm may do the trick. Although I think running A* searches for all tiles in the radius should be ok if total number of tiles to check is not enormous and you don't do it every frame.

    I have done something like this quite recently in my game(searching nearby tiles for a place where I can dump poopy debris using a pitch fork) but I want to ask first if you have like a character who can move x tiles and you will only want the tiles which the player can get to. If so, I'd do it in two steps, the first step being to get a list of all possible valid tiles in a radius based on max movement speed and then for each cell make sure the player can actually get there.

      Erich_L I might just do that, since it is true that there is an upper bound in the ball of radius equal to the move speed.

      This is range based here, right? That is, given a position, valid tiles are within a certain range. You could take a look at this repo here, albeit it in C# (I linked this because it's a turn-based project)

      A quick gdscript version:

      var valid_positions = []
      for x in max_move_distance:
          for y in max_move_distance:
              var test_distance = absi(x) + absi(y)
              if test_distance <= max_move_distance:
                  valid_positions.append(unit_position + Vector2i(x, y))

      I ain't tested that so caveat emptor. valid_positions is an array of vectors to highlight. max_move_distance comes from the thing you want to test, e.g. like the current unit. unit_position is hopefully obvious!

      edited for clarity