An implementation of Permissive Field Of View algorithm in JavaScript.
A Precise Permissive FOV algorithm, based on the Python implementation.
```
npm install permissive-fov
In order to instantiate a PermissiveFov object, one needs to pass it the map dimensions and an isTransparent function that will determine whether a pair of coordinates points to a transparent (non-blocking) cell on the map.
`
const PermissiveFov = require("permissive-fov").PermissiveFov;
const width = 80;
const height = 50;
const isTransparent = (x, y) => map[x][y].transparent;
const fov = new PermissiveFov(width, height, isTransparent);
`
Since Permissive FOV contains TypeScript typings, it can also be used within TypeScript applications, the only difference being, obviously, the import statement:
``
import { PermissiveFov } from "permissive-fov";
A FOV computation requires four parametres: the source coordinates x and y, maximum sight radius (use Infinity for unlimited sight radius) and a function that will mark a given pair of coordinates as seen.
`
const startX = 40;
const startY = 25;
const radius = 10;
const setVisible = (x, y) => map[x][y].visible = true;
fov.compute(40, 25, 10, setVisible);
`
`
const newWidth = 100;
const newHeight = 60;
fov.setMapDimensions(newWidth, newHeight);
`
`
const newIsTransparent = (x, y) => {
return map[x][y].transparent ||
map[x][y].xRayTransparent;
};
fov.setIsTransparent(newIsTransparent);
`
Precise Permissive Field Of View is a field of view (FOV) computation algorithm. Given a 2D grid with a source point S and n points marked as obstacles, it will determine all points visible from S.
Here's an example of a field of view calculated using the Permissive FOV algorithm:
As with most field of view algorithms, Precise Permissive Field Of View can be used in any game with a two dimensional grid layout, e.g. a roguelike.
Due to the permissive nature of this algorithm, it is well suited for situations where symmetry and permissiveness are the desired features, e.g. for reasons of gameplay balance.
As the name itself implies, this algorithm is permissive (as opposed to restrictive). It means that it will typically consider many more points to be in field of view than other algorithms would.
This also means that in some cases, the field of view will take an unnatural looking shape, including points that are considered to be visible despite being right behind an obstacle. Here are two examples that demonstrate this:
A FOV algorithm is considered symmetric given that the following is true:
>Given any set of points A and B, when B is in FOV of A, then A is in FOV of B.
In other words, a field of view is symmetric when all of the points seen from source can also see the source, while all the points not seen by the source, cannot see the source either.
Permissive Precise FOV is perfectly symmetric.
Compared to other available FOV algorithms, Precise Permissive FOV is on the slower side of the spectrum, with an average time required to compute a single field of view several times longer than that of the best performing alternatives.
This means that this algorithm may not be well suited for uses where the field of view computation is performed very frequently. That being said, the symmetric nature of Precise Permissive FOV allows for smart optimisations. For instance, if the FOV has already been calculated for origin point A, determining whether A is visible from B doesn't require another FOV computation: if A sees B, then B sees A.
FOV images have been generated by FOV Torture Chamber.
Python implementation's authorship and copyright notice:
`
Author: Aaron MacDonald
Date: June 14, 2007
Description: An implementation of the precise permissive field
of view algorithm for use in tile-based games.
Based on the algorithm presented at
http://roguebasin.roguelikedevelopment.org/
index.php?title=
Precise_Permissive_Field_of_View.
You are free to use or modify this code as long as this notice is
included.
This code is released without warranty.
``