public void GenerateMap(int x, int z, bool wrapping) { var originalRandomState = Random.state; if (!UseFixedSeed) { Seed = Random.Range(0, int.MaxValue); } Random.InitState(Seed); cellCount = x * z; Grid.CreateMap(x, z, wrapping); if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } for (var i = 0; i < cellCount; i++) { Grid.GetCell(i).WaterLevel = WaterLevel; } CreateRegions(); CreateLand(); ErodeLand(); CreateClimate(); CreateRivers(); SetTerrainType(); for (var i = 0; i < cellCount; i++) { Grid.GetCell(i).SearchPhase = 0; } }
private List <HexCell> GetVisibleCells(HexCell fromCell, int range) { var visibleCells = ListPool <HexCell> .Get(); searchFrontierPhase += 2; fromCell.Distance = 0; fromCell.SearchPhase = searchFrontierPhase; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } searchFrontier.Enqueue(fromCell); var fromCoordinates = fromCell.Coordinates; range += fromCell.ViewElevation; while (searchFrontier.Count > 0) { var current = searchFrontier.Dequeue(); current.SearchPhase++; visibleCells.Add(current); for (var d = HexDirection.NE; d <= HexDirection.NW; d++) { var neighbour = current.Neighbours[(int)d]; if (neighbour == null || neighbour.SearchPhase > searchFrontierPhase) { continue; } var distance = current.Distance + 1; if (distance + neighbour.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbour.Coordinates) || !neighbour.Explorable) { continue; } if (neighbour.SearchPhase < searchFrontierPhase) { neighbour.SearchPhase = searchFrontierPhase; neighbour.Distance = distance; neighbour.SearchHeuristic = 0; searchFrontier.Enqueue(neighbour); } else if (distance < neighbour.Distance) { var oldPriority = neighbour.SearchPriority; neighbour.Distance = distance; searchFrontier.Change(neighbour, oldPriority); } } } return(visibleCells); }
private bool Search(HexCell fromCell, HexCell toCell, HexUnit unit) { searchFrontierPhase += 2; fromCell.Distance = 0; fromCell.SearchPhase = searchFrontierPhase; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } searchFrontier.Enqueue(fromCell); while (searchFrontier.Count > 0) { var current = searchFrontier.Dequeue(); if (current == toCell) { return(true); } current.SearchPhase++; var currentTurn = (current.Distance - 1) / unit.Speed; for (var d = HexDirection.NE; d <= HexDirection.NW; d++) { var neighbour = current.Neighbours[(int)d]; if (neighbour == null || neighbour.SearchPhase > searchFrontierPhase) { continue; } if (!unit.IsValidDestination(neighbour)) { continue; } var moveCost = unit.GetMoveCost(current, neighbour, d); if (moveCost < 0) { continue; } var distance = current.Distance + moveCost; var turn = (distance - 1) / unit.Speed; if (turn > currentTurn) { distance = turn * unit.Speed + moveCost; } if (neighbour.SearchPhase < searchFrontierPhase) { neighbour.SearchPhase = searchFrontierPhase; neighbour.Distance = distance; neighbour.PathFrom = current; neighbour.SearchHeuristic = neighbour.Coordinates.DistanceTo(toCell.Coordinates); searchFrontier.Enqueue(neighbour); } else if (distance < neighbour.Distance) { var oldPriority = neighbour.SearchPriority; neighbour.Distance = distance; neighbour.PathFrom = current; searchFrontier.Change(neighbour, oldPriority); } } } return(false); }