1036
Programming / Re: How to make sure every tile connects.
« on: April 11, 2009, 02:52:29 PM »
I must say I much prefer the simplicity of the traditional floodfill algorithm. It is likely faster too (since you tend to use boolean checks rather than integer calculations). The process:
Step 1: Choose an initial passable tile to work with - say the down stairs, or a random floor tile.
Step 2: Mark square as "connected" (a specific boolean flag for this purpose).
Step 3. For every square surrounding the last square, IF it's a passable tile AND IF it's not already marked as connected THEN repeat step 2.
(conditions are important to make sure the recursion eventually stops)
That's all there is to it - simple, elegant and quick. If you want all of your dungeons to have no diagonal connections (so you can always use the arrow keys to move) then have step 3 only check the squares in the 4 cardinal directions instead of all 8 adjacent squares.
Step 1: Choose an initial passable tile to work with - say the down stairs, or a random floor tile.
Step 2: Mark square as "connected" (a specific boolean flag for this purpose).
Step 3. For every square surrounding the last square, IF it's a passable tile AND IF it's not already marked as connected THEN repeat step 2.
(conditions are important to make sure the recursion eventually stops)
That's all there is to it - simple, elegant and quick. If you want all of your dungeons to have no diagonal connections (so you can always use the arrow keys to move) then have step 3 only check the squares in the 4 cardinal directions instead of all 8 adjacent squares.