Line Of Sight

by DarkCampainger
Determine if two atoms have a clear line of sight between them
ID:983300
 
Back when this library was originally released, Moussiffer suggested a feature to limit the line of sight to the area in front of the mob. The more I thought about it, the more I liked the idea, and finally (wow, it's already been a month!) a "field of view" argument has been added!



To limit the FOV like in the above image, you simply have to pass your desired FOV to InLineOfSight():
InLineOfSight(source, target, fov=60, range=96)

I also added a range limit argument, so if you have a specific target in mind (as opposed to looping through a nearby range()) or just want a nice circular range, it can handle that for you.

One important thing to note is that the FOV argument is the distance in degrees that the target can be from the source's sight vector, meaning that it's actually half the value of what the final visible arc will be.

As a small optimization, you can also pass a negative value holding the cosine of the FOV angle (which you can cache on your mob):
// Identical to the previous example, but doesn't need a cos() call
InLineOfSight(source, target, fov=-0.5, range=96)

You can use the fovX and fovY arguments to define the source's sight vector (direction). If left blank, it will default to the source atom's direction. If only fovX is set, it will be treated as a direction flag (eg NORTH, SOUTH, ect). If both are set, they are treated as a vector (preferably normalized, but it will do that for you).

Finally, I updated the demo with a little "cat and mouse" mechanic. Basically, you're a thief (yellow block), and if the guards (black blocks) spot you, they'll chase you until you leave their line of sight. I did this because I didn't want to give people the wrong idea about the library: it isn't for drawing a visible cone in front of the mobs, that's just there to show how it works. If you want a visual cone in front of your mobs, I would suggest using a pregenerated graphic.

There were also a few accuracy and rounding-error fixes, so there shouldn't be any more "dead zones" and the arcs should be more symmetrical.
It most definitely does not like having 10+ mobs searching like this.
In response to Albro1
Hahaha, somehow those little black squares look so unhappy in their little boxes.

If you disable UpdateMarkers() it should run fine, though. It calls InLineOfSight() tens of thousands of times each tick.