public static CellEntranceExit Create(Cell cell, CellEntranceExit otherSide, ErrorCollector errors) { CellEntranceExit entranceExit = new CellEntranceExit(); entranceExit.Other = otherSide; otherSide.Other = entranceExit; entranceExit.Width = otherSide.Width; int tries = 0; entranceExit.Side = RandomDirection(otherSide.Side); while (!EntranceFits(cell, entranceExit)) { if (tries > 10) { errors.Add(new System.Exception("Unable to fit the EntranceExit.")); return(null); } entranceExit.Side = RandomDirection(); tries++; } PositionRandomly(cell, entranceExit, errors); return(entranceExit); }
public static Path GeneratePath(Cell cell, CellEntranceExit entrance, CellEntranceExit exit, ErrorCollector errors) { // A* Path path = null; bool complete = false; HashSet <Node> openNodes = new HashSet <Node>(); List <Node> closedNodes = new List <Node>(); Vector2Int target = exit.Position + (exit.Side.Opposite().Vector()) * 2; Node startNode = new Node(entrance); SetCost(startNode, target); openNodes.Add(startNode); int depth = 0; while (openNodes.Count > 0 && complete == false && depth < 100000f) { Node node = GetLowestCostNode(openNodes); openNodes.Remove(node); closedNodes.Add(node); List <Node> newNodes = PerformStep(cell, node, target); foreach (Node newNode in newNodes) { if (IsComplete(newNode, target)) { if (newNode.directionCombo < MINIMUM_COMBO) { continue; } path = CreatePath(newNode, exit); complete = true; } else { openNodes.Add(newNode); } } //closedNodes.Add(); depth++; } if (!complete) { errors.Add(new System.Exception("Could not generate a path! " + startNode.position + " -> " + target)); path = new Path(); path.nodes = closedNodes; } return(path); }
public static CellEntranceExit Create(Cell cell, uint width, ErrorCollector errors) { CellEntranceExit entrance = new CellEntranceExit(); entrance.Width = width; int tries = 0; entrance.Side = RandomDirection(); while (!EntranceFits(cell, entrance)) { if (tries > 10) { errors.Add(new System.Exception("Unable to fit the entrance.")); return(null); } entrance.Side = RandomDirection(); tries++; } PositionRandomly(cell, entrance, errors); return(entrance); }
private static void PositionRandomly(Cell cell, CellEntranceExit entrance, ErrorCollector errors) { int x, y; bool searchX; switch (entrance.Side) { case Direction.NORTH: x = 0; y = (int)cell.Size - 1; searchX = true; break; case Direction.SOUTH: x = 0; y = 0; searchX = true; break; case Direction.WEST: x = 0; y = 0; searchX = false; break; case Direction.EAST: x = (int)cell.Size - 1; y = 0; searchX = false; break; default: return; } // Try to randomly place int tries = 0; while (tries < 10) { int count = 0; int index = Random.Range(EDGE_MARGIN, (int)(cell.Size - entrance.Width - EDGE_MARGIN)); for (int i = 0; i < entrance.Width; i++) { int x1 = x, y1 = y; if (searchX) { x1 += i + index; } else { y1 += i + index; } if (cell.Get(x1, y1) == 0) { count++; } else { break; } } if (count == entrance.Width) { SetPosition(cell, entrance, x, y, index, searchX); return; } tries++; } // Give up and just place it in the first possible spot for (int i = EDGE_MARGIN; i < cell.Size - entrance.Width - EDGE_MARGIN; i++) { int count = 0; for (int j = 0; j < entrance.Width; j++) { int x1 = x, y1 = y; if (searchX) { x1 += i + j; } else { y1 += i + j; } if (cell.Get(x1, y1) == 0) { count++; } else { break; } } if (count == entrance.Width) { SetPosition(cell, entrance, x, y, i, searchX); return; } } errors.Add(new System.Exception("Unable to place the EntranceExit.")); }