Efficient way to check if player is within range of any events line of sight

Let us suppose we are concerned with the default event triggering system, where only players can trigger events.


Suppose you have a small, 17x13 map with 20 events, each with custom event ranges. Some of them have linear line of sight, while others have circular, triangular, or various shapes.


Whenever the player is in sight, the event that sees it will activate.


Now, both the player and the events can move around, so everytime someone makes a move, checks will need to be made.


When an event moves, only that event needs to check whether the player is in sight.


However, when the player moves, all events may potentially need to perform checks.


1. Assuming we have an efficient way to determine whether the player is within an event's range, what are some ways to avoid having to update every single event?


2. Furthermore, can the solution above be generalized to a system where any event can potentially trigger any other events?
First of all, you have to figure out how you're going to determine "line of sight" to begin with. Can they see through impassable tiles? How about over a bucket, or banister? Do you measure from the middle of the tiles or the corners. Are you parallaxing?
Base case assumes you can see through impassable tiles and does not consider these properties. This allows you to take advantage of simple geometric properties to determine whether a point is on a line or a circle, etc.


It uses whatever the engine considered to be a character's (x,y) position, so it's kind of independent of how you map it (tiles, parallax, etc).


This means that the distance between two characters in a 32x32 tile system is likely much different than the difference between two charaters in a 16x16 tile system or a 4x4.
The most obvious thing for me would be that there is no need to update events that arent 'on the screen'. I am assuming you would be aware of this already 😛
That's not a correct assumption. Someone with a range of 100 units could easily see you even if it's outside your own field of view.


We shouldn't be operating under that assumption either; it's a terrible attempt to address performance issues.
Oh thats a point.

I never thought of having an event with a larger 'field of view' than the visible screen tiles....

Maybe I should take that into account for my ~ABS...
Perhaps the algorithm for checking the line of sight should be modeled from the existing collision detection algorithms, as it is kinda similar.
Ok, here's my 'seat of the pants' idea of how I would start the algorithm:

1) You need to iterate through all events E(m).

2) Each event needs to iterate through all actors and other events a(n) and e(n).

So we've got two nested loops constantly running. (you could set each event up to run as a parallel process - but that raises serious threading issues).

- For each event pair E(m), e(n) - first calculate the distance 'normally': (x1-x2)^2+(y1-y2)^2 [sqrt is an unnecessary step]

- If e(n) is outside E(m)'s max range (counted in tiles, then squared - stored as an event variable), stop and move on to e(n+1)

- If e(n) is in range, iterate through the LOS pattern for E(m) [ie: the tiles in the circle, cone, straight line, square, each will need it's own method of traversal] - we now have 3 nested loops, very inefficient.

- Go back to the first loop iterating E(m). Directly start traversing its LOS pattern and store all elements within it as ordered pairs (E(m), e(n)). This is going to involve a lot of traversing areas with no events to be seen - also very inefficient.

- Combine the two ideas: Keep the first iteration through the events, and the second checking distance to events. When one event is found in range, stop the second iteration, traverse the LOS tiles for E(m), and store all "found" events as ordered pairs (E(m), e(n)). Repeat for E(m+1).  Two 'full' iterations: one through E(m), the second through E(m) traverse_los for those events with at least one other event/actor 'in range', and one that 'breaks' when a match E(m).less_than_max_distance?(E(m),e(n)) is found. I'm sure there's a math function already in RGSS to do the distance calculation.

-Finally, iterate through all ordered pairs executing whatever script or event you want to happen for each - this is the part where problems may crop up if you try to run each event as its own parallel process. You'd have to make these methods thread-safe somehow.

- Clear the list of ordered pairs (re-initialize a new list, garbage collection will get to the old one eventually) and go back to the top and start iterating over E(n) again.

Could that be made even more efficient? I'm sure it could, but that should at least give you someplace to start from if you haven't already gotten the whole thing figured out and coded by now.

Note: that algorithm is more efficient if there is likely to be more than one event 'in range' at a time. If it is more likely that only one (or none) will be in range, then traversing the LOS pattern immediately checking only for e(n) and stopping when either the match is found or the end of the pattern is reached will be the more efficient choice.
I was reading some articles on LOS, in particular this one


http://ncase.me/sight-and-light/


And I like the idea of drawing line segments between different objects and solving the equations to determine whether there is a solution or not, and whether there are multiple solutions (in case one object is blocking another)


Kind of wondering if solving equations would be better than checking tiles.


I'll have to play around with the concept to get a better idea how it works.
I would recommend first doing the trivial max distance check to see if a collision is even possible. If that passes, solve shape intersection equations as an early rejection method (including testing for occlusion), then finally check tiles directly for anything that passes that, which should be the minority of cases.

Shape intersections won't fit the tiles precisely, but if you make the shapes slightly larger than the actual tile area, they're an easy way of doing early rejection for events/players that are clearly outside the sight range, and they're very fast. The orange book is a pretty good source on fast shape intersections for most shapes you're likely to ever need.

Chances are you can approximate whatever shape you want with some combination of boxes, circles and triangles, and you can treat the player as a point (or 4 points if you want to be more precise). If you absolutely need more complicated shapes, you can run a 2D version of GJK: http://mollyrocket.com/849.

You basically want to exclude as many tiles (with events/players on them) as possible with trivial checks, then do more time consuming checks on a very small number of tiles.

I did something similar when I was working on tile-based targeting a while ago: http://kaelandev.blogspot.com/2013/03/a-hud-is-fine-too.html (it's like halfway down the post)

Also, make sure you're consistent about how you interpret tiles when converting back and forth between tile and screen coordinates. You'll get slightly different results depending on whether you just check the center of a tile, a corner of a tile or the entire tile for intersection tests (link above shows how I'm dealing with this, will probably be different for you).

I'm sure there's a math function already in RGSS to do the distance calculation.
I don't think it does. Even if it did you'll want to write your own anyway so you can compare squared distances to run through the algorithm faster.

Original thread: https://forums.rpgmakerweb.com/threads/efficient-way-to-check-if-player-is-within-range-of-any-events-line-of-sight.25361/