I am currently writing a little roguelike and wondered how Nethack/other games do the FoV of the player. I had a look at Nethacks source code, but since it's C it's pretty hard to read.

For people who don't know how it looks in nethack:

First glimpse into room At the entrance Full Room

I couldn't find good resources on how to achieve this result.
Of note is that my maps are a little more complex with regards to shape and objects that block sight.

my map

So how would I actually implement that? Map is just an array of ints.

I figure I should have flags on each cell saying if it's been discovered or not, but how to I simulate the FoV?


This is a very similar question to one posted just today: Cast ray to select block in voxel game

Basically, you could implement the Bressenham's Line Algorithm and cast rays from the player's location to all the possible tiles. If the ray intersects a wall, then given tile is not visible.

  • 1
    \$\begingroup\$ If the outer wall is closed, you can also cast rays to each point on the wall. Depending on layout, this may be more efficient. \$\endgroup\$ – MSalters Feb 15 '13 at 13:10

I wrote an article recently on how to do field-of-view calculations. That article shows an implementation for a slightly different type of map then the one you would have in a Nethack map but the basic math should still apply.

Basically I ray-cast from the observer to via the edges of obstacles to the extents of the visible area and then calculate and area that would be obscured from the observer.

The article can be found here; http://www.codeproject.com/Articles/545808/Visual-concealment-for-games-using-Javascript

And there's an JsFiddle over here if you want to play around with it.


Not the answer you're looking for? Browse other questions tagged or ask your own question.