- Home /
Spawning collectibles in a randomly generated maze
So I made a proceduraly generated maze that uses a digging algorithm to create the maze and then sometimes add spike traps on the tiles it digs. I now want to set collectibles to spawn only in dead ends. I tried to detect if a maze position is sou rounded by 3 wall block it would change the the maze position to a different state than the walls and spikes. When I tried, it only generated 1 collectible instead of putting one in each dead end. I would also eventually want to limit the amount of collectibles. Thanks for the help.
Here's the code for the maze generation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MazeGenerator : MonoBehaviour
{
public int mazeHeight = 11;
public int mazeWidth = 11;
private int[,] maze;
public MazeCell cellPrefab;
public SpikeCell cellAlt;
private MazeCell[,] cells;
public GameObject wallGo;
public GameObject spikeGo;
public GameObject collectGo;
public GameObject plane;
public double percent = 0.25;
static System.Random _random = new System.Random();
void Start()
{
maze = GenerateMaze(mazeHeight, mazeWidth);
for (int i = 0; i < mazeHeight; i++)
for (int j = 0; j < mazeWidth; j++)
{
if (maze[i, j] == 1)
{
Vector3 pos = new Vector3(i, 1, j);
GameObject wall = Instantiate(wallGo, transform) as GameObject;
if (wall != null)
wall.transform.position = pos;
}
if ((maze[i, j] == 2) && (i != 1) && (j != 1))
{
Vector3 spikePos = new Vector3(i, 0, j);
GameObject spike = Instantiate(spikeGo, transform) as GameObject;
if (spike != null)
spike.transform.position = spikePos;
}
if (maze[i, j] == 3)
{
Vector3 collectPos = new Vector3(i, 1, j);
GameObject collect = Instantiate(collectGo, transform) as GameObject;
if (collect != null)
collect.transform.position = collectPos;
}
}
}
private int[,] GenerateMaze(int Height, int Width)
{
int[,] maze = new int[Height, Width];
for (int i = 0; i < Height; i++)
for (int j = 0; j < Width; j++)
maze[i, j] = 1;
System.Random rand = new System.Random();
int r = rand.Next(Height);
while (r % 2 == 0)
r = rand.Next(Height);
int c = rand.Next(Width);
while (c % 2 == 0)
c = rand.Next(Width);
maze[r, c] = 0;
MazeDigger(maze, r, c);
CollectibleGenerator(maze, r, c);
return maze;
}
private void CollectibleGenerator(int[,] maze, int r, int c)
{
for (int i = 0; i < mazeHeight; i++)
for (int j = 0; j < mazeWidth; j++)
{
if (((maze[r+1, c]) == 1 && (maze[r-1, c] == 1) && (maze[r,c-1]==1)) || ((maze[r + 1, c]) == 1 && (maze[r, c + 1] == 1) && (maze[r, c - 1] == 1)) || ((maze[r + 1, c]) == 1 && (maze[r - 1, c] == 1) && (maze[r, c + 1] == 1)) || ((maze[r - 1, c]) == 1 && (maze[r, c + 1] == 1) && (maze[r, c - 1] == 1)))
{
maze[r, c] = 3;
}
}
}
private void MazeDigger(int[,] maze, int r, int c)
{
// 1 - North
// 2 - East
// 3 - South
// 4 - West
int[] directions = new int[] { 1, 2, 3, 4 };
Shuffle(directions);
for (int i = 0; i < directions.Length; i++)
{
switch (directions[i])
{
case 1:
if (r - 2 <= 0)
continue;
if ((maze[r - 2, c] != 0) && (maze[r - 2, c] != 2))
{
maze[r - 2, c] = 0;
maze[r - 1, c] = 0;
if(Random.value <= percent)
{
maze[r - 2, c] = 2;
}
MazeDigger(maze, r - 2, c);
}
break;
case 2:
if (c + 2 >= mazeWidth - 1)
continue;
if ((maze[r, c + 2] != 0) && (maze[r, c + 2] != 2))
{
maze[r, c + 2] = 0;
maze[r, c + 1] = 0;
if (Random.value <= percent)
{
maze[r, c + 2] = 2;
}
MazeDigger(maze, r, c + 2);
}
break;
case 3:
if (r + 2 >= mazeHeight - 1)
continue;
if ((maze[r + 2, c] != 0) && (maze[r + 2, c] != 2))
{
maze[r + 2, c] = 0;
maze[r + 1, c] = 0;
if (Random.value <= percent)
{
maze[r + 2, c] = 2;
}
MazeDigger(maze, r + 2, c);
}
break;
case 4:
if (c - 2 <= 0)
continue;
if ((maze[r, c - 2] != 0) && (maze[r, c - 2] != 2))
{
maze[r, c - 2] = 0;
maze[r, c - 1] = 0;
if (Random.value <= percent)
{
maze[r, c - 2] = 2;
}
MazeDigger(maze, r, c - 2);
}
break;
}
}
}
public static void Shuffle<T>(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
int j = random.Next(i);
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}
// public void Generate()
// {
// GameObject floor = Instantiate(plane, transform) as GameObject;
// floor.transform.localScale = new Vector3(mazeHeight, mazeWidth, 1f);
// }
//}
public void Generate()
{ cells = new MazeCell[mazeHeight, mazeWidth];
for (int x = 0; x < mazeHeight; x++)
{
for (int z = 0; z < mazeWidth; z++)
{
CreateCell(x, z, maze);
}
}
}
private void CreateCell(int x, int z, int[,] maze)
{
MazeCell newCell = Instantiate(cellPrefab) as MazeCell;
cells[x, z] = newCell;
newCell.name = "Maze Cell " + x + ", " + z;
newCell.transform.parent = transform;
newCell.transform.localPosition = new Vector3(x - mazeHeight * 0f, 0f, z - mazeWidth * 0f);
}
}
Answer by oStaiko · May 15, 2018 at 09:47 PM
I don't really want to dig through your code... so I'm just gonna give you the steps to do it yourself.
If you want a LIMITED number of RANDOM spawning collectibles, you definitely don't want to set them as you find them in your algorithm. Instead, use an algorithm to find their positions, and save those to a list. Now, you have a list of every possible position to spawn a collectible, and a map that hasn't been changed yet. Randomly pick as many positions as you want from the list and set those to collectibles. This is now truly random, and can be limited.
I did notice your algorithm to check for walls was a little... poor. Here's a sudo version for you to use that is a lot easier to understand.
for (i=1) // DO NOT START AT 0. 0-1 is -1 and you'll get an error. Run this from 1 to max-1.
for (k=1)
{
if (pos==wall) // Goes to next iteration if this is a wall
continue;
int walls = 0;
if (pos+i==wall)
walls++;
if (pos-i==wall)
walls++;
if (pos+k==wall)
walls++;
if (pos-k==wall)
walls++;
if (walls==3)
Debug.Log("Success! Save this position for a collectible:);
}
}