public void GenerateMap(int x, int z) { Random.State originalRandomState = Random.state; if (!useFixedSeed) { seed = Random.Range(0, int.MaxValue); seed ^= (int)System.DateTime.Now.Ticks; seed ^= (int)Time.unscaledTime; seed &= int.MaxValue; } Random.InitState(seed); Debug.Log("Initiate with seed " + seed.ToString()); cellCount = x * z; grid.CreateMap(x, z); if (searchFrontier == null) { searchFrontier = new CellPriorityQueue(); } for (int i = 0; i < cellCount; i++) { grid.GetCell(i).WaterLevel = waterLevel; } CreateLand(); // SetTerrainType(); for (int i = 0; i < cellCount; i++) { grid.GetCell(i).SearchPhase = 0; } Random.state = originalRandomState; }
private bool SearchForPath(PathRequest request) { try { var fromCell = request.From; var toCell = request.To; var mobility = request.Mobility; _searchFrontierPhase += 2; if (_searchFrontier == null) { _searchFrontier = new CellPriorityQueue(); } else { _searchFrontier.Clear(); } fromCell.SearchPhase = _searchFrontierPhase; fromCell.SearchDistance = 0; _searchFrontier.Enqueue(fromCell); while (_searchFrontier.Count > 0) { var current = _searchFrontier.Dequeue(); current.SearchPhase++; if (current == toCell) { return(true); } for (var d = Direction.N; d <= Direction.NW; d++) { var neighbor = current.GetNeighbor(d); if (neighbor?.PathableWith(mobility) != true) { continue; } var neighborTravelCost = 1f; if (mobility != Mobility.Fly) { neighborTravelCost = neighbor.TravelCost; } if (neighbor == null || neighbor.SearchPhase > _searchFrontierPhase) { continue; } if (neighborTravelCost < 0) { continue; } var distance = current.SearchDistance + neighborTravelCost; if (neighbor.SearchPhase < _searchFrontierPhase) { neighbor.SearchPhase = _searchFrontierPhase; neighbor.SearchDistance = distance; neighbor.PathFrom = current; neighbor.SearchHeuristic = neighbor.DistanceTo(toCell); _searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.SearchDistance) { var oldPriority = neighbor.SearchPriority; neighbor.SearchDistance = distance; neighbor.PathFrom = current; _searchFrontier.Change(neighbor, oldPriority); } } } return(false); } catch (Exception ex) { Debug.LogWarning("Pathing error: " + ex.ToString()); throw; } }