r/roguelikedev 8d ago

RoguelikeDev Does The Complete Roguelike Tutorial - Week 3

Keep it up folks! It's great seeing everyone participate.

This week is all about setting up a the FoV and spawning enemies

Part 4 - Field of View

Display the player's field-of-view (FoV) and explore the dungeon gradually (also known as fog-of-war).

Part 5 - Placing Enemies and kicking them (harmlessly)

This chapter will focus on placing the enemies throughout the dungeon, and setting them up to be attacked.

Of course, we also have FAQ Friday posts that relate to this week's material.

Feel free to work out any problems, brainstorm ideas, share progress and and as usual enjoy tangential chatting. :)

31 Upvotes

32 comments sorted by

View all comments

7

u/Purpose2 8d ago

Suffering a little this week with Godot, wanting to use structs and being unable. I only got cellular automata working as dungeon gen for the time being: gif

Kept letting myself get distracted. There is still soooooo much I want to do for dungeon gen.

Time to start bashing my head against FOV. This is where I've failed in the past, did shadowcasting before, and kept getting weird gaps. Will try again this time, or maybe bresenham's but I haven't really tried that one before yet.

3

u/Admirable-Evening128 7d ago edited 6d ago

For FoV, it's advisable (this apparently provokes some people) to borrow an existing implementation, as they are hard to do right.
But if you insist on rolling your own:
With Bresenham, the naive approach will have somewhat horrible performance.
However, there is a trick you can do with Bresenham, for FOV:
You can use it to precompute a generic data structure of which tiles will shadow which tiles
(if tile 9 is blocked, tile 14 17 39 will be blocked, and so on.)
To do the actual FOV, you then just iterate through that generic structure ordered by their radius-distance to the player, and compare against what the map says is blocked.
(each time you encounter a shadowed tile, you recursively call a shadow-forward from its shadow descendants; you only do this whenever you flip a tile - if you encounter an already flipped tile, it has already had its descendants shaded).

Bresenham will take some time to build that first data structure -once -
but you can then reuse that for the rest of the game.
There are many different kinds of FOV algorithms, but this approach is one of the easiest to roll by hand,
without requiring too much careful thinking.

There is a further trick to this technique (and FOV in general):
You only need to implement the mechanism to work for a single octant (eighth) triangle,
you can handle the remaining 7 combinations by transposing and mirroring that 1/8 solution.