How to determine if a 3D voxel-based room is sealed, efficiently
- by NigelMan1010
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.