using System.Collections.Generic;
using UnityEngine;
namespace Hallucinate.GameSetup.Maze
{
///
/// Implements a simplified version of Prim's algorithm for maze generation.
/// It picks walls at random and converts them into corridors if they only have one corridor neighbor.
///
public class Prims : Maze
{
///
/// Generates the maze using Prim's algorithm logic.
///
public override void Generate()
{
int x = 2;
int z = 2;
map[x, z] = Corridor;
List walls = new List
{
new MapLocation(x + 1, z),
new MapLocation(x - 1, z),
new MapLocation(x, z + 1),
new MapLocation(x, z - 1)
};
int countloops = 0;
while (walls.Count > 0 && countloops < 5000)
{
int rwall = Random.Range(0, walls.Count);
x = walls[rwall].x;
z = walls[rwall].z;
walls.RemoveAt(rwall);
if (CountSquareNeighbours(x, z) == 1)
{
map[x, z] = Corridor;
walls.Add(new MapLocation(x + 1, z));
walls.Add(new MapLocation(x - 1, z));
walls.Add(new MapLocation(x, z + 1));
walls.Add(new MapLocation(x, z - 1));
}
countloops++;
}
}
}
}