List <HexCell> GetVisibleCells(HexCell fromCell, int range) { List <HexCell> visibleCells = ListPool <HexCell> .Get(); searchFrontierPhase += 2; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } range += fromCell.ViewElevation; fromCell.SearchPhase = searchFrontierPhase; fromCell.Distance = 0; searchFrontier.Enqueue(fromCell); HexCoordinates fromCoordinates = fromCell.coordinates; while (searchFrontier.Count > 0) { HexCell current = searchFrontier.Dequeue(); current.SearchPhase += 1; visibleCells.Add(current); for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if (neighbor == null || neighbor.SearchPhase > searchFrontierPhase || !neighbor.Explorable) { continue; } int distance = current.Distance + 1; if (distance + neighbor.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbor.coordinates)) { continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } } return(visibleCells); }
List <HexCell> GetVisibleCells(HexCell fromCell, int range) { List <HexCell> visibleCells = ListPool <HexCell> .Get(); range += fromCell.ViewElevation; m_searchFrontierPhase = 2; ClearPath(); ResetSearchPhase(); HexCoordinates fromCoordinates = fromCell.Coordinates; fromCell.Distance = 0; fromCell.SearchPhase = m_searchFrontierPhase; m_searchQueue.Enqueue(fromCell); while (m_searchQueue.Count > 0) { //yield return delay;s HexCell current = m_searchQueue.Dequeue(); current.SearchPhase += 1; visibleCells.Add(current); for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbour = current.GetNeighbour(d); if (neighbour == null || neighbour.SearchPhase > m_searchFrontierPhase || !neighbour.IsExplorable) { continue; } int distance = current.Distance + 1; //if(distance + neighbour.ViewElevation > range) if (distance + neighbour.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbour.Coordinates)) { continue; } if (neighbour.SearchPhase < m_searchFrontierPhase) { neighbour.SearchPhase = m_searchFrontierPhase; neighbour.Distance = distance; SetPathCurrentNext(current, neighbour); neighbour.SearchHeuristic = 0; m_searchQueue.Enqueue(neighbour); } else if (distance < neighbour.Distance) { neighbour.Distance = distance; SetPathCurrentNext(current, neighbour); m_searchQueue.Change(neighbour); } } } return(visibleCells); }
private HexCoordinates LookLimited(int visionRange, HexCoordinates fromCoordinates, HexCell current, HexDirection start, HexDirection end) { HexDirection d = start; for (; d != end; d = d.Next()) { HexCell neighbor = current.GetNeighbor(d); if ( neighbor == null || neighbor.SearchPhase > searchFrontierPhase || !neighbor.Explorable || (!current.Walled && neighbor.Walled && !current.HasRoadThroughEdge(d)) ) { continue; } int distance = current.Distance + 1; if (distance + neighbor.ViewElevation > visionRange || distance > fromCoordinates.DistanceTo(neighbor.coordinates) ) { continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } return(fromCoordinates); }
private HexCoordinates LookUnlimitedTruWalls(int visionRange, HexCoordinates fromCoordinates, HexCell current) { for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if ( neighbor == null || neighbor.SearchPhase > searchFrontierPhase || !neighbor.Explorable || neighbor.Walled ) { continue; } int distance = current.Distance + 1; if (distance + neighbor.ViewElevation > visionRange || distance > fromCoordinates.DistanceTo(neighbor.coordinates) ) { continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } return(fromCoordinates); }
// Compute H value from current hex to targetHex public int ComputeHValue(HexCell targetHex) { return(coordinates.DistanceTo(targetHex.coordinates)); }
/// <summary> /// Получить видимые ячейки /// </summary> /// <param name="fromCell"></param> /// <param name="range"></param> /// <returns></returns> List <HexCell> GetVisibleCells(HexCell fromCell, int range)//по большей части это сильно измененный поиск пути { List <HexCell> visibleCells = ListPool <HexCell> .Get(); searchFrontierPhase += 2; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } range += fromCell.ViewElevation; fromCell.SearchPhase = searchFrontierPhase; fromCell.Distance = 0; searchFrontier.Enqueue(fromCell); HexCoordinates fromCoordinates = fromCell.coordinates; while (searchFrontier.Count > 0) { HexCell current = searchFrontier.Dequeue(); current.SearchPhase += 1; visibleCells.Add(current); // Поиск в ширину for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); //если соседа нет или есть еще не достигнутые клетки, то в первую очередь обходим их и не обходим клетки которые не могут быть исследованы if (neighbor == null || neighbor.SearchPhase > searchFrontierPhase || !neighbor.Explorable) { continue; } //расстояние до клетки назначения int distance = current.Distance + 1; //ограничение на видимость по расстоянию до клетки и высоте if (distance + neighbor.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbor.coordinates) ) { continue; } if (distance > range) { continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } } return(visibleCells); }
List <HexCell> GetVisibleCells(HexCell fromCell, int range) { List <HexCell> visibleCells = ListPool <HexCell> .GLGet(); searchFrontierPhase += 2; if (searchFrontier == null) { searchFrontier = new HexCellPriorityQueue(); } else { searchFrontier.Clear(); } // Units view range (Line of Sight) increase by altitude of cell they are in range += fromCell.ViewElevation; fromCell.SearchPhase = searchFrontierPhase; fromCell.Distance = 0; searchFrontier.Enqueue(fromCell); HexCoordinates fromCoordinates = fromCell.Coordinates; while (searchFrontier.Count > 0) { HexCell current = searchFrontier.Dequeue(); current.SearchPhase += 1; visibleCells.Add(current); for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++) { HexCell neighbor = current.GetNeighbor(d); if ( neighbor == null || neighbor.SearchPhase > searchFrontierPhase ) { continue; } int distance = current.Distance + 1; if (distance + neighbor.ViewElevation > range || distance > fromCoordinates.DistanceTo(neighbor.Coordinates) || !neighbor.Explorable) { // Take neighbor view elevation into acount when comparing with range // Tall cells should block vision this way // Also ignore cells farther away than the shortest path to them // If we didnt do this then when high cells blocked vision we would sometimes // Be able to see around them, as if our vision could bend corners, because although // Higher cells blocked vision the searching algorithm still found a path to them // Also cells that cannot be explored should block vision continue; } if (neighbor.SearchPhase < searchFrontierPhase) { neighbor.SearchPhase = searchFrontierPhase; neighbor.Distance = distance; neighbor.SearchHeuristic = 0; searchFrontier.Enqueue(neighbor); } else if (distance < neighbor.Distance) { int oldPriority = neighbor.SearchPriority; neighbor.Distance = distance; searchFrontier.Change(neighbor, oldPriority); } } } return(visibleCells); }