I'm creating a top-down 2d strategy game, with a square grid map. So far, I've used Bresenham's line drawing algorithm in a circle to determine what's in LOS of each unit, and then targedt one of the targets in the circle. Now I find that this limits my units to shoot only at targets that they see. I want to extend my targeting algorithm to target any other unit in range of my weapon, even if they're out of sight range of this given unit, if they're "spotted" by another friendly unit. In other words, I want to enable usage of weapons with ranges longer than sight range. Is there a better way than iterating over all sighted units and computing range and LOSto each of them?
If you store the units for each side in an quadtree, you can cheaply avoid iterating over enemy units that are beyond range (assuming your units can't shoot over more than half the board!).
If you store your unit list in each quadtree sorted by whether its been seen or not (e.g. just have two lists), you only have to compute the line-of-sight (LoS) between a unit and an enemy if its potentially unspotted.
And if you track who saw each who first, you only have to recompute the LoS when one of the pair moves.
Tangentially related, here's how to efficiently compute the visible board for a unit (my blog). If you only want to compute LoS between units, and don't need tile visibility (e.g. fog-of-war), then checking LoS using Bresenhams is likely a better bet.
Small complication: Bresenhams doesn't actually visit all tiles on a line! It will miss some. If you need a precise computation, look at my own question (3D, but the problem is the same in 2D).