Best way to determine LOS for turn-based strategy game like X-Com 2?

Hello,

I’m working on a TBS game like X-Com 2 that uses a square tile grid. I want the player to be able to hover over a tile and draw a line to all visible enemies from that tile–indicating that the hovered tile has LOS on the enemy. Turns out, it’s difficult!

I’ve tried a few ways of doing this, and none seem very good:

  1. The obvious way is to perform a line trace from the hovered tile to the enemy unit, then check for collision. This works when enemies aren’t behind tall objects…but in TBS games, it’s common to be able to target enemies that are fully obscured, so long as they are immediately behind the obscuring object.
    1a. I could tell the line trace to ignore cover objects for the sake of LOS–but tall cover should block LOS when the enemy is not directly behind it.
    1b. If a direct line trace fails, maybe it could trace new lines from valid tiles adjacent to the hovered square and see if they collide with the enemy unit? This kind of works, but it requires many traces to ensure every possible viewable angle is covered. It seems sub-optimal, especially for performance.

  2. Instead of line traces, run a pathfinding algorithm from the hovered tile to the target tile, get the minimum Distance between them (in tiles) and check if the generated path is equal to that Distance (indicating that a “straight” line exists from the start tile to the target). This is extremely resource-intensive and seems infeasible to run at gametime–I’m not sure it would even provide accurate LOS data.

  3. On startup, check for tile visibility from every tile, to every tile, via a line trace or pathfinding algorithm, then store that data for use when the mouse hovers over a tile. This option is most appealing, but I don’t think modern TBS games do it this way. Many of them feature dynamically changing maps that seem to generate LOS dynamically, and for the sake of practice I’d like to understand how the big boys do it.

Any ideas/critiques/suggestions on how I can approach LOS differently? Thanks!

Possible variation on 1, do the test as ‘normal’, if hit tall object, test if target is within one tile of object and deny cover if so.

If this is all on a grid though, and objects occupy the whole tile, good old Bresenham might be better than casting rays about.

Yes, all on a grid! I’ll try a Bresenham line and see where that gets me. Thank you!