UNPKG

permissive-fov

Version:

An implementation of Permissive Field Of View algorithm in JavaScript.

129 lines (80 loc) 5.1 kB
# Precise Permissive Field Of View A [Precise Permissive FOV](http://www.roguebasin.com/index.php?title=Precise_Permissive_Field_of_View) algorithm, based on the [Python implementation](http://www.roguebasin.com/index.php?title=Permissive_Field_of_View_in_Python). ## Installation ``` npm install permissive-fov ``` ## Usage ### Instantiation 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"; ``` ### FOV computation 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); ``` ### Changing map dimensions ``` const newWidth = 100; const newHeight = 60; fov.setMapDimensions(newWidth, newHeight); ``` ### Changing the transparency check function ``` const newIsTransparent = (x, y) => { return map[x][y].transparent || map[x][y].xRayTransparent; }; fov.setIsTransparent(newIsTransparent); ``` ## About Permissive FOV 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: ![indoor](https://mingos.bitbucket.io/img/umbraprojekt/permissive-fov/fov-indoor.png) ### Real life uses 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](http://www.roguebasin.com/index.php?title=What_a_roguelike_is). 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. ### Restrictiveness vs. permissiveness 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: ![pillar](https://mingos.bitbucket.io/img/umbraprojekt/permissive-fov/fov-pillar.png) ![outdoor](https://mingos.bitbucket.io/img/umbraprojekt/permissive-fov/fov-outdoor.png) ### Symmetry 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. ### Performance 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*. ## Images FOV images have been generated by [FOV Torture Chamber](https://bitbucket.org/umbraprojekt/fov-torture-chamber). ## Credits [Python implementation's](http://www.roguebasin.com/index.php?title=Permissive_Field_of_View_in_Python) 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. ```