/// <summary> /// Takes in the cell it is supposed to start from. this way you can start a path from anywhere /// Possible future TODO - add checks to make sure the starting cell is on grass/path /// </summary> /// <param name="startingCell"></param> private void StartAPathAt(HexCell startingCell) { var startingNeighbors = startingCell.GetAllNeighborsList(); var possibleDirs = new List <int>(); rerouteCount = 0; numPathSplits = 0; var goodStart = false; int counter = 0; while (!goodStart) { counter++; if (counter == 10 * startingNeighbors.Count) { break; } //############# for (int i = 0; i < startingNeighbors.Count; i++) { try { if (startingNeighbors[i].landType == LandType.Grass && startingNeighbors[i].landType != LandType.Path && !isEdge(startingNeighbors[i])) { possibleDirs.Add(i); } } catch (NullReferenceException) { } } if (possibleDirs.Count != 0) { goodStart = true; } else { startingCell = GetStartingEdgeCell(); if (startingCell == null) { continue; } //######## startingNeighbors = startingCell.GetAllNeighborsList(); } } var directionIndex = possibleDirs[UnityEngine.Random.Range(0, possibleDirs.Count)]; StartCoroutine(CreatePathinDir(startingCell, directionIndex)); StopCoroutine("CreatePathinDir"); }
private int GetSimilarNeighbors(List <HexCell> acceptedCells, HexCell currentCell, HexCell neighborCell) { var neighborsNeighbors = neighborCell.GetAllNeighborsList(); var intersection = acceptedCells.Intersect(neighborsNeighbors).ToList(); if (intersection.Count > 1) { return(2); } return(1); }
/// <summary> /// Creates a path straight to an edge or nongrass type in the direction indicated from the current cell /// Used to finish up the paths when max splits have been reached /// </summary> /// <param name="currentCell"></param> /// <param name="directionIndex"></param> private void SimplePathinDir(HexCell currentCell, int directionIndex) { var currentNeighbors = currentCell.GetAllNeighborsList(); var next = currentCell; var pathCells = new List <HexCell> { currentCell }; var keepPathGoing = true; while (keepPathGoing) { //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "keeppathgoing \n "); next = currentNeighbors[directionIndex]; if (next == null) { break; } try { if (isEdge(next) && next.landType != LandType.Grass) { break; } else if (isEdge(next) && next.landType == LandType.Grass) { pathCells.Add(next); break; } else if (next.landType != LandType.Grass) { break; } } catch (NullReferenceException) { System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "caattch \n "); } pathCells.Add(next); currentNeighbors = next.GetAllNeighborsList(); } SetDepression(pathCells, 0f); }
/// <summary> /// Returns the two directions that the path should split in when a split is called. /// If only one direction is possible, only returns one. /// </summary> /// <param name="currentCell"></param> /// <param name="direction">current hexdirection that the path is going </param> /// <returns>List on (int)Hexdirections to go in</returns> private List <int> GetPathSplit(HexCell currentCell, HexDirection direction) { var neighbors = currentCell.GetAllNeighborsList(); var goodHexDirs = new List <HexDirection> { direction, direction.Next(), direction.Previous() }; var splitOptionIndices = new List <int>(); var splitableCellsNeighbors = new HexCell[] { neighbors[(int)direction], neighbors[(int)goodHexDirs[1]], neighbors[(int)goodHexDirs[2]] }; foreach (var neighbor in splitableCellsNeighbors) { try { if (neighbor.landType != LandType.Grass || isEdge(neighbor)) { goodHexDirs.Remove((HexDirection)neighbors.IndexOf(neighbor)); } } catch (NullReferenceException) { } } var splitDir = goodHexDirs[UnityEngine.Random.Range(0, goodHexDirs.Count)]; splitOptionIndices.Add((int)splitDir); goodHexDirs.Remove(splitDir); if (goodHexDirs.Count > 0) { //do the second path splitDir = goodHexDirs[UnityEngine.Random.Range(0, goodHexDirs.Count)]; splitOptionIndices.Add((int)splitDir); } return(splitOptionIndices); }
/// <summary> /// Coroutine used to recursively make path. /// Starts with passed in direction and when split chances occur calls a new CreatePathinDir in the directions to split /// Sets each portion of the path at each call /// </summary> /// <param name="startCell"></param> /// <param name="directionIndex"></param> /// <returns></returns> private IEnumerator CreatePathinDir(HexCell startCell, int directionIndex) { //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "push - "); //whereAmI++; //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", whereAmI.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "\n"); pathsInProgress.Push(true); //yield return new WaitForSeconds(.5f); //just for debugging purposes var goodDir = true; var currentNeighbors = startCell.GetAllNeighborsList(); var current = currentNeighbors[directionIndex]; var pathCells = new List <HexCell>() { startCell, currentNeighbors[directionIndex] }; if (rerouteCount >= 7) { HexCell s = GetStartingEdgeCell(); if (s != null) //###### { StartAPathAt(s); } //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "stop1 \n"); pathsInProgress.Pop(); //whereAmI-- ; yield break; ///######### //StopCoroutine("CreatePathinDir"); ########### } if (numPathSplits >= maxPathSplits) { //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "numPathSplits >= maxPathSplits \n"); if (!isEdge(startCell)) { SimplePathinDir(current, directionIndex); } //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "pop1 - "); //whereAmI--; //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", whereAmI.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "\n"); pathsInProgress.Pop(); yield break; } if (isEdge(current) && numPathSplits < 2) { HexCell s = GetStartingEdgeCell(); if (s != null) { StartAPathAt(s); } //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "pop3 - "); //whereAmI--; //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", whereAmI.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "\n"); pathsInProgress.Pop(); yield break; //StopCoroutine("CreatePathinDir"); } numPathSplits += 1; while (goodDir) { //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "gooddir "); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", mooCounter.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", " , \n "); mooCounter++; if (mooCounter == 2000) { InvokePathsFinished(); break; } currentNeighbors = current.GetAllNeighborsList(); var nextCell = currentNeighbors[directionIndex]; if (nextCell == null) { break; } else if (nextCell.landType != LandType.Grass && nextCell.landType != LandType.Path) { if (CanChangeDir(currentNeighbors, (HexDirection)directionIndex)) { directionIndex = GetNewDir(currentNeighbors, (HexDirection)directionIndex); rerouteCount += 1; yield return(CreatePathinDir(current, directionIndex)); break; } else { //!!System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "else1 \n"); } break; } // check if nextCell is grass, then check if its an edge , if not grass, have to change the direction else if (isEdge(nextCell)) { pathCells.Add(nextCell); if (CanChangeDir(nextCell.GetAllNeighborsList(), (HexDirection)directionIndex) && numPathSplits < maxPathSplits) { rerouteCount += 1; directionIndex = GetNewDir(nextCell.GetAllNeighborsList(), (HexDirection)directionIndex); yield return(CreatePathinDir(nextCell, directionIndex)); } else { //!!System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "is edge but can't charge direction \n"); } break; } else if (ifSplit(pathCells.Count)) { var splitIndices = GetPathSplit(current, (HexDirection)directionIndex); foreach (var index in splitIndices) { yield return(CreatePathinDir(current, index)); } //goodDir = false; break; } else { pathCells.Add(nextCell); current = nextCell; } } // kind of messy final clean for (int p = 0; p < pathCells.Count; p++) { if (pathCells[p].landType == LandType.Hill || pathCells[p].landType == LandType.Water) { pathCells.Remove(pathCells[p]); } } SetDepression(pathCells, 0f); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "pop2 - "); //whereAmI--; //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", whereAmI.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "\n"); pathsInProgress.Pop(); if (pathsInProgress.Count == 0) { InvokePathsFinished(); } else { //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "did not finish paths \n "); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", pathsInProgress.Count.ToString()); //System.IO.File.AppendAllText(@"/Users/cerealbar/Downloads/cereal-bar-master 12 2/debug.txt", "\n "); } }