10
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Only checking when things change would be a start. Checking every three seconds seems like overkill, since you know when voxels change that could break the seal. If a voxel that makes up a sealed room is changed, you can mark that room to be rechecked, otherwise, don't bother. \$\endgroup\$
    – House
    Commented Nov 2, 2013 at 21:35
  • \$\begingroup\$ Since there could be hundreds of voxels changed in that 3-second time frame, I thought it would be more efficient to just do it periodically rather than check if something has changed nearby. I will experiment with that though. \$\endgroup\$ Commented Nov 4, 2013 at 4:13
  • \$\begingroup\$ Oh, well if hundreds of voxels can change in a 3 second time frame, you are likely going to have a number of issues with performance. Start profiling your code to find inefficiencies. Good luck! \$\endgroup\$
    – House
    Commented Nov 4, 2013 at 4:14

2 Answers 2

3
\$\begingroup\$

The best solution will rely on multiple factors, like expected size of room.

  1. Only do this checks, when something is actually changing.

Approach 1:

You could use an A* to find a path from the vent to the tile above the vent / the vent itself or to a tile which is known as vacuum. If path is found, room is unsealed. This is not so different from your current approach but should be faster. Once found, make a "flood fill" to set the tiles as vacuum.

Approach 2:

Maybe your outer structure is less complex - considering, that there is a surface where under are the rooms, you do not need to move in all 6 directions, so you should be travel along the surface, mark every tile as vacuum which you travel.

\$\endgroup\$
0
\$\begingroup\$

When you do your recursive search, do you make sure you're not checking the same voxel multiple times? I couldn't tell from the way you described your algorithm, but you should have some kind of flag to indicate whether you've recursively expanded a voxel already, so you don't do it more than once.

And as Byte56 said, you should also only check for leaks when things change. That could greatly minimize the amount of work you do, depending on how frequently changes occur. You may even be able to cache information between successive calls to the algorithm, which can trivialize the amount of computation you do after the first call.

Edit:

I looked at some of your code. It seems like you're using a LinkedList to indicate whether a voxel has been checked already, as in my first paragraph. You may have better results if you use something other than a LinkedList for this. Perhaps try a HashSet or something? This should reduce the complexity of your check method from O(n) to O(1).

\$\endgroup\$
1
  • \$\begingroup\$ Yes, I'm adding it to a LinkedList called "checked", and then checking if that list contains the position before checking it. I thought that checking if something changed would also be extremely CPU-intensive, since hundreds of voxels could have changed within that 3-second time period. I'll see how a hashset compares to the linkedlist in this situation though, thanks. \$\endgroup\$ Commented Nov 4, 2013 at 4:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .