Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
add comment

marked as duplicate by Josh Petrie, Byte56, Trevor Powell, bummzack, Sean Middleditch Feb 17 '13 at 20:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
1  
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. –  MSalters Feb 15 '13 at 13:10
add comment

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.

share|improve this answer
add comment

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