# How to determine if a 3D voxel-based room is sealed, efficiently

This topic is 1901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there,

I've been having some issues with efficiently determining if large rooms are sealed in a voxel-based 3D rooms. I'm at a point where I have tried my hardest to solve the problem without asking for help, but not tried enough to give up, so I'm asking for help.

To clarify, sealed being that there are no holes in the room. There are oxygen sealers, which check if the room is sealed, and seal depending on the oxygen input level.

Right now, this is how I'm doing it:

- Starting at the block above the sealer tile (the vent is on the sealer's top face), recursively loop through in all 6 adjacent directions

- If the adjacent tile is a full, non-vacuum tile, continue through the loop

- If the adjacent tile is not full, or is a vacuum tile, check if it's adjacent blocks are, recursively.

- Each time a tile is checked, decrement a counter

- If the count hits zero, if the last block is adjacent to a vacuum tile, return that the area is unsealed

- If the count hits zero and the last block is not a vacuum tile, or the recursive loop ends (no vacuum tiles left) before the counter is zero, the area is sealed

If the area is not sealed, run the loop again with some changes:

- Checking adjacent blocks for "breathable air" tile instead of a vacuum tile

- Instead of using a decrementing counter, continue until no adjacent "breathable air" tiles are found.

- Once loop is finished, set each checked block to a vacuum tile.

Here's the code I'm using:

http://pastebin.com/NimyKncC

The problem:

I'm running this check every 3 seconds, sometimes a sealer will have to loop through hundreds of blocks, and a large world with many oxygen sealers, these multiple recursive loops every few seconds can be very hard on the CPU.

I was wondering if anyone with more experience with optimization can give me a hand, or at least point me in the right direction.

Thanks a bunch.

##### Share on other sites

Why do you need to do it continuously? It would only change if vents are opened or voxels are filled/unfilled.

Anyway, close all the vents (treat them as doors) and flood fill each empty tile, connected tiles get the same ID. Repeat (incrementing the ID each time you run out of tiles to fill) until all empty space is filled. When a vent is opened that connects the rooms. If you destroy a wall section and it tunnels into another area then you combine the area IDs, otherwise give it the same ID as the room you tunneled from. You can build a connectivity graph if you want by examining rooms IDs on each side of a vent.

Edited by Paradigm Shifter

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015291
×