A working field-of-view ?(FOV) algorithm is one of the essential parts in any roguelike, it is used to calculate which mapcells, within a given radius, that can be seen by seen by the player. This article describes a C# implementation of such an algorithm, known as recursive shadow casting which is described in more detail in the article ?FOV using recursive shadowcasting - improved.

Shadow Casting

Shadowcasting divides the FOV calculations into eight octants and visits the ?mapcells of each ?row by row or column by column, starting with the nearest row or column and working it's way outward.

------> 6 row 6 last -----> 5 . ----> 4 . ---> 3 . --> 2 row 2 second -> 1 row 1 is scanned first @ @ this is the starting point

When a scan comes across a cell that blocks the players line of sight it calculates which other cells in rows/columns farther away that isn't visible because of the blocker. Those cells are "in shadow", hence the term shadowcasting.

-...--- - = visible cells -..--- # = blocking cell -#--- . = cells in blocker's shadow ---- --- -- @

The above text is taken from this article, from the website RogueBasin. I have shamelessly lifted this text, as I feel it is a very clear description of how shadowcasting works, and I can't improve upon it.

The Application

Below are several pictures of the application which demonstrate that algorithm.?The lighter squares represent the cells visible to the player, and the darker ones are those which the player cannot see.?The maze displayed in the map has been generated using another algorithm (which I'll post soon) and is loaded when the application starts up.

The keys q,w,e,a,d,z,x and c control the player move the player. W is up, S down, d right, A left right etc

Source Code

The source code can be downloaded?here.?And to run it, you'll need Microsoft Visual Studio which you can find here.

Github: the code for the class FOVRecurse.

What's Going On?

The class FOVRecurse.cs?contains the FOV algorithm, and the?methods are GetVisibleCells and ScanOctant are where the magic happens.



When the player moves, the method GetVisibleCells?is called which in turn calls the method ScanOctant for each of the?8 areas surrounding the player and?all the cells that the player can see are stored in a generic list. Upon completion of this scan the the event?playerMoved is fired, which causes the map to be redrawn in the form event?pictureBox1_Paint.

There really isn't that much to say about it as the code speaks for itself, but if you're stuck with anything please add a comment and I'll get back to you.

Acknowledgements

This post takes text from the RogueBasin articles:

I have used the take from the above as I feel these are the best explanations of how the shadow casting technique works, and I can't really improve upon them.