#rogue-like #fov #shadowcasting

symmetric-shadowcasting

Simple implementation of the shadow casting FOV algorithm

3 unstable releases

0.2.0 Nov 1, 2020
0.1.1 Aug 16, 2020
0.1.0 Aug 16, 2020

#1592 in Algorithms


Used in areaportal2d

CC0 license

13KB
213 lines

Symmetric Shadow Casting

This repository is just a Rust translation of the shadow casting algorithm from the Python implementation shown here. It is a very nice article, with visuals and explaination.

This implementation is tested against the examples in the article, and produces identical results.

Usage

The use of this crate is simple- there is only a single function exposed:

pub type Pos = (isize, isize);

pub fn compute_fov<F, G>(origin: Pos, is_blocking: &mut F, mark_visible: &mut G)
    where F: FnMut(Pos) -> bool,
          G: FnMut(Pos), {

This function takes a position, which is a pair of isizes (you may have to cast into and out of this type), and two closures.

The closure is_blocking tells the algorithm when a tile is blocked, such as by a wall. This may capture some kind of user structure in its environment, such as a grid or map.

The closure mark_visible is called for each visible tile, allowing the user to handle the tile. This may capture some mutable state in its environment, modifying it by indicating visible tiles.

See the tests for examples on how this might be used.

Limitations

I would have liked to have this interface provide a way to lookup blocking tiles and modify the same structure, but I couldn't get the lifetimes to work out. As it is, you will have to capture your map structure immutably in is_blocking, and some other structure mutably in mark_visible which tracks the results. I would expect that this mutated structure will by used to modify the map or grid, perhaps something cloned first if we are marking visibility within the structure itself.

Similar Crates

There is already the shadowcast crate. This crate seems to implement additional algorithms, and is likely a fine choice. It seems to provide some additional features such as casting different shapes, which is cool. It also seems to make some additional assumptions, requiring more dependencies, and providing a more complex API. Check it out if you are looking for different types of casting.

Alternate Name

My brother suggested another name for this crate- I Don't Eat Anything That Casts a Shadow. This is much funnier, but I went with the boring option of using the title of the tutorial that the algorithm came from.

License

As with the original algorith, this is licensed as CC0.

Dependencies

~320KB