I developed an algorithm some time ago for 2D labyrinths on a square grid, there is no reason why this should not work for a 3D labyrinth on a cubic grid either.
Start with a three-dimensional grid, originally completely filled with wall cells.
...
Launch the agent at the edge of the grid, the agent moves in a straight line on the cleaning wall X, Y, Z, -X, -Y or -Z as it moves.
Action 'N' has a small chance at every step.
The “M” action occurs when the cell immediately in front of the agent is a wall and the cell in front of it is empty.
'N' - random selection:
- removal of this agent
- 90 degrees left or right
- and creating an agent on the same square, rotated 90 degrees to the left, right, or both (two agents).
'M' - random selection:
- removal of this agent
- removing a wall in front of this agent and removing this agent
- and do nothing while continuing
- Rotate left or right 90 degrees.
- and creating an agent on the same square, rotated 90 degrees to the left, right, or both (two agents).
The labyrinths differ from each other, and their character is very flexible, adjusting the trigger for “M” (for actual compounds) and also changing the chances from 1 to 8. You can delete an action or two or enter your own actions, for example one, to make a small cleaning or bypassing one step.
A trigger for “N” can also be another kind of randomness, for example, the example below can be used to create fairly branched mazes that still have several long straight parts.
float n = 1; while (random_0_to_1 > 0.15) { n *= 1.2; } return (int)n;
Some minor adjustments will be required from my simple description, for example, a trigger for action “M” will have to check cells adjacent to cells, which it also checks, depending on which connections are desired.
In order for the labyrinth to contain cycles, 5 or 6 are required, and for the labyrinth to contain dead spots, at least one alternative action of "M" on 5 and 6 is required.
Some chances / actions and “M” triggers will tend to make labyrinths that do not work, such as unsolvable or full of empty or wall cells, but many of them will get consistently nice results.