}//unlinkAllNeighbors //------------------------------- public void linkAllNeighbors(ref UPoint cell) { //Cell B is above Cell A nodes[cell.ix, cell.iy].links |= UP; nodes[cell.ix, cell.iy].links |= DOWN; nodes[cell.ix, cell.iy].links |= RIGHT; nodes[cell.ix, cell.iy].links |= LEFT; if (cell.y - 1 >= 0) { nodes[cell.ix, cell.iy - 1].links |= UP; } if (cell.y + 1 < nodes.GetLength(1)) { nodes[cell.ix, cell.iy + 1].links |= DOWN; } if (cell.x - 1 >= 0) { nodes[cell.ix - 1, cell.iy].links |= RIGHT; } if (cell.x + 1 < nodes.GetLength(0)) { nodes[cell.ix + 1, cell.iy].links |= LEFT; } }//unlinkAllNeighbors
} //wal //------------------------------- //Given a list, pops a random point and returns it public UPoint randomPointFromList(ref List <UPoint> list) { if (list.Count <= 0) { return(null); }//if int index = r.getIntInRange(0, list.Count - 1); UPoint popped = list[index]; list.RemoveAt(index); return(popped); }//randomPointFromList
}//addUnvisitedToList // Add all surrounding visited link cells to the supplied list public int addAllVisitedToList(UPoint center, ref List <UPoint> list) { int nonFrontiersAdded = 0; for (int i = 1; i <= 8; i *= 2) { UPoint pt = findNeighbor(center, i); if (inBounds(pt, true) && nodes[pt.ix, pt.iy].links >= VISITED) // UP { list.Add(pt); nonFrontiersAdded++; } //if } //for return(nonFrontiersAdded); }//addFrontiers
private void DijkstraMaze() { List <UPoint> frontiers = new List <UPoint>(); //Choose a starting point for maze gen UPoint startingNode = new UPoint(r.getIntInRange(0, nodes.GetLength(0) - 1), r.getIntInRange(0, nodes.GetLength(1) - 1)); nodes[startingNode.ix, startingNode.iy].links = VISITED; //Visited startPos = startingNode; addUnvisitedToList(startingNode, ref frontiers); foreach (UPoint frontier in frontiers) { nodes[frontier.ix, frontier.iy].links = IN_FRONTIERS; }//foreach while (frontiers.Count > 0) { //Choose a frontier at random UPoint frontier = randomPointFromList(ref frontiers); nodes[frontier.ix, frontier.iy].links = VISITED; //Visited //Choose a neighbor (that is not a frontier) at random List <UPoint> nonFrontiersTemp = new List <UPoint>(); //Find the non-frontier neighbors of this frontier addAllVisitedToList(frontier, ref nonFrontiersTemp); //Get one at random UPoint nonFrontier = nonFrontiersTemp[r.getIntInRange(0, nonFrontiersTemp.Count - 1)]; //Connect it to the frontier linkNeighbors(frontier, nonFrontier); //Add all unvisited neighbors of this frontier to the frontiers list List <UPoint> unvisitedNeighbors = new List <UPoint>(); addUnvisitedToList(frontier, ref unvisitedNeighbors); for (int i = 0; i < unvisitedNeighbors.Count; i++) { frontiers.Add(unvisitedNeighbors[i]); nodes[unvisitedNeighbors[i].ix, unvisitedNeighbors[i].iy].links = IN_FRONTIERS; } //for } //while } //generateMaze
} //FindAllDeadEnds public void Loopify(float amount) { //GO OVER THE MAP AND RANDOMLY LINK A RANDOM NUMBER OF TILES TO NEIGHBORS for (int i = 0; i < Mathf.RoundToInt(nodes.GetLength(0) * amount * 2); i++) { UPoint cell = new UPoint(r.getIntInRange(0, nodes.GetLength(0) - 1), r.getIntInRange(0, nodes.GetLength(1) - 1)); //int newLinks = tiles[cell.ix, cell.iy].links; for (int dir = 1; dir <= LEFT; dir *= 2) { if (r.getRandom() <= amount && inBounds(findNeighbor(cell, dir), true)) { linkNeighbors(cell, findNeighbor(cell, dir)); } //if } //while } //for } //Loopify
} //Update public void DrawDebugLines(UPoint offset) { UPoint _temp1 = new UPoint(0, 0); UPoint _temp2 = new UPoint(0, 0); for (int y = maze.nodes.GetLength(1) - 1; y >= 0; y--) { for (int x = 0; x < maze.nodes.GetLength(0); x++) { if (maze.isFlagged(maze.nodes[x, y].links, Maze.UP)) { _temp1 = offset + (new UPoint(x, y)); _temp2 = offset + (new UPoint(x, y + 1)); Debug.DrawLine(_temp1, _temp2); }//if if (maze.isFlagged(maze.nodes[x, y].links, Maze.RIGHT)) { _temp1 = offset + (new UPoint(x, y)); _temp2 = offset + (new UPoint(x + 1, y)); Debug.DrawLine(_temp1, _temp2); }//if if (maze.isFlagged(maze.nodes[x, y].links, Maze.DOWN)) { _temp1 = offset + (new UPoint(x, y)); _temp2 = offset + (new UPoint(x, y - 1)); Debug.DrawLine(_temp1, _temp2); }//if if (maze.isFlagged(maze.nodes[x, y].links, Maze.LEFT)) { _temp1 = offset + (new UPoint(x, y)); _temp2 = offset + (new UPoint(x - 1, y)); Debug.DrawLine(_temp1, _temp2); } //if } //for } //for } //DrawDebugLines
}//isLinkedInDir //------------------------------- //Links both "doors" between adjacent "neighbor" cells public void linkNeighbors(UPoint cellA, UPoint cellB) { //Figure out what direction cellB is from cellA //Does not check if cells are actually adjacent if (Mathf.Abs(cellA.ix - cellB.ix) != 1 && Mathf.Abs(cellA.iy - cellB.iy) != 1) { Debug.LogError("MapMaker.cs: CELLS ARE NOT ADJACENT! : (" + cellA.ix + "," + cellA.iy + " -> " + cellB.ix + "," + cellB.iy + ")"); return; }//if if (cellB.iy > cellA.iy) { //Cell B is above Cell A nodes[cellA.ix, cellA.iy].links |= UP; nodes[cellB.ix, cellB.iy].links |= DOWN; }//if else if (cellB.iy < cellA.iy) { //Cell B is below Cell A nodes[cellA.ix, cellA.iy].links |= DOWN; nodes[cellB.ix, cellB.iy].links |= UP; }//else if else if (cellB.ix > cellA.ix) { //Cell B is to the right of Cell A nodes[cellA.ix, cellA.iy].links |= RIGHT; nodes[cellB.ix, cellB.iy].links |= LEFT; }//else if else if (cellB.ix < cellA.ix) { //Cell B is to the left of Cell A nodes[cellA.ix, cellA.iy].links |= LEFT; nodes[cellB.ix, cellB.iy].links |= RIGHT; } //else if } //linkNeighbors
}//findNeighbor //------------------------------- ///Check bitmap to see if a room has a link in that direction public bool isLinkedInDir(UPoint loc, int dir) { return(isLinkedInDir(loc.ix, loc.iy, dir)); }//isLinkedInDir
}//addFrontiers //------------------------------- //Find a "neighbor" cell in a direction and return coordinates public UPoint findNeighbor(UPoint loc, int dir) { return(findNeighbor(loc.ix, loc.iy, dir)); }//findNeighbor
} //generateMaze //------------------------------- //Check if a coordinate point is within bounds public bool inBounds(UPoint loc, bool dataBounds = false) { return(inBounds(loc.ix, loc.iy, dataBounds)); }//inBounds
public int links = Maze.UNVISITED; //-1 means hasn't been checked for links yet public NodeData(UPoint _index) { index = _index; }
public void CreateBricks() { GameObject _brickPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube); //_brickPrefab.transform.localScale = transform.localScale; Destroy(_brickPrefab.GetComponent <BoxCollider>()); if (t == null) { t = new Texturizer(Color.white); } int scaleX = 2; int scaleY = 2; int xMin = 1 - scaleX; int xMax = scaleX - 1; int yMin = 1 - scaleY; int yMax = scaleY - 1; int xOffset = scaleX + (scaleX - 1); int yOffset = scaleY + (scaleY - 1); //PlaceAllDoors(scaleX, scaleY); for (int y = 0; y < maze.nodes.GetLength(1); y++) { for (int x = 0; x < maze.nodes.GetLength(0); x++) { UPoint startingOffset = new Vector3(x * xOffset, y * yOffset); for (int yOff = yMin; yOff <= yMax; yOff++) { for (int xOff = xMin; xOff <= xMax; xOff++) { int links = maze.nodes[x, y].links; int newLink = 15; if ((yOff == yMin && !maze.isFlagged(links, Maze.DOWN)) || (yOff == yMax && !maze.isFlagged(links, Maze.UP)) || (xOff == xMax && !maze.isFlagged(links, Maze.RIGHT)) || (xOff == xMin && !maze.isFlagged(links, Maze.LEFT))) { newLink = 0; }//if else if (maze.numLinks(links) < 4 && ( (yOff == yMin && xOff == xMin) || (yOff == yMax && xOff == xMin) || (yOff == yMin && xOff == xMax) || (yOff == yMax && xOff == xMax))) { newLink = 0; } else if (yOff == 0 && xOff == 0 && links == 0) { newLink = 0; }//else if UPoint placeAt = startingOffset + new Vector3(xOff, yOff); //UPoint tilesIndex = startingOffset + new UPoint(scaleX - 1, scaleY - 1) + new UPoint(xOff, yOff); t.setupTile(newLink); GameObject tile = t.makeObject(); //tile.Initialize(tilesIndex.ix, tilesIndex.iy); //tile.links = newLink; tile.transform.position = placeAt; tile.transform.position += new Vector3(0, 0, 10); tile.transform.parent = transform; tile.name = "Tile [" + x + "," + y + "]"; //if (x == 0 && y == 0 && maze.numLinks(links) == 1) //{ //tile.tag = "ChestTile"; //}//if //tiles[tilesIndex.ix, tilesIndex.iy] = tile; } //for } //for } //for } //for _brickPrefab.SetActive(false); }//CreateBricks