Exemplo n.º 1
0
    public static ElevationDigraph FromHexGrid(
        HexGrid <Hex> hexGrid
        )
    {
        List <ElevationEdge> edges =
            new List <ElevationEdge>();

        for (
            int i = 0;
            i < hexGrid.Rows * hexGrid.Columns;
            i++
            )
        {
            Hex current = hexGrid[i];

            foreach (
                Hex neighbor in hexGrid.GetNeighbors(i)
                )
            {
                ElevationEdge outEdge = new ElevationEdge(
                    current,
                    neighbor,
                    CubeVector.HexDirectionWrapping(
                        current.CubeCoordinates,
                        neighbor.CubeCoordinates,
                        hexGrid.WrapSize
                        )
                    );

                edges.Add(outEdge);

                ElevationEdge inEdge = new ElevationEdge(
                    neighbor,
                    current,
                    CubeVector.HexDirectionWrapping(
                        neighbor.CubeCoordinates,
                        current.CubeCoordinates,
                        hexGrid.WrapSize
                        )
                    );

                edges.Add(inEdge);
            }
        }

        ElevationDigraph result =
            new ElevationDigraph();

        result.AddVerticesAndEdgeRange(edges);
        return(result);
    }
Exemplo n.º 2
0
// TODO: This is a presentation concern and should be moved out of
//       this class.
    public void DecreaseVisibility(
        Hex fromHex,
        int range,
        ElevationDigraph elevationGraph
        )
    {
        List <Hex> hexes =
            GetVisibleHexes(
                fromHex
//              range
                );

        for (int i = 0; i < hexes.Count; i++)
        {
            hexes[i].DecreaseVisibility();
        }

        ListPool <Hex> .Add(hexes);
    }
Exemplo n.º 3
0
    public void Triangulate(
        HexMap hexMap,
        float hexOuterRadius,
        HexAdjacencyGraph adjacencyGraph,
        RiverDigraph riverGraph,
        RoadUndirectedGraph roadGraph,
        ElevationDigraph elevationGraph
        )
    {
        _terrainLayer.Clear();
        _riversLayer.Clear();
        _roadsLayer.Clear();
        _openWaterLayer.Clear();
        _waterShoreLayer.Clear();
        _estuariesLayer.Clear();
        _features.Clear();

        for (int i = 0; i < Hexes.Length; i++)
        {
            Hex current = Hexes[i];

            if (current)
            {
                Dictionary <HexDirections, Hex> neighbors =
                    adjacencyGraph.GetNeighborByDirection(current);

                Dictionary <HexDirections, ElevationEdgeTypes> edgeTypes =
                    elevationGraph.GetNeighborEdgeTypes(current);

                Dictionary <HexDirections, bool> roadEdges =
                    roadGraph.GetNeighborRoads(current);

                List <HexDirections> borderDirections =
                    adjacencyGraph.GetBorderDirectionsList(current);

                HexRiverData riverData = riverGraph.GetRiverData(current);

                TriangulateHex(
                    current,
                    neighbors,
                    borderDirections,
                    hexOuterRadius,
                    riverData,
                    roadEdges,
                    edgeTypes,
                    hexMap.WrapSize,
                    _terrainLayer,
                    _riversLayer,
                    _roadsLayer,
                    _openWaterLayer,
                    _waterShoreLayer,
                    _estuariesLayer,
                    _features
                    );
            }
        }

        _terrainLayer.Draw();
        _riversLayer.Draw();
        _roadsLayer.Draw();
        _openWaterLayer.Draw();
        _waterShoreLayer.Draw();
        _estuariesLayer.Draw();
        _features.Apply();
    }
Exemplo n.º 4
0
// This should be a simple graph traversal which stops when it hits an
// edge which has a higher elevation.
    private List <Hex> GetVisibleHexes(
        Hex fromHex
//        int sightRange
        )
    {
        ElevationDigraph elevationGraph = ElevationDigraph.FromHexGrid(
            HexGrid
            );

        List <Hex> visibleHexes = new List <Hex>();

        Queue <Hex> open   = new Queue <Hex>();
        List <Hex>  closed = new List <Hex>();

        Hex current = fromHex;

        open.Enqueue(current);
        visibleHexes.Add(current);

        List <HexEdge> visibleEdges = elevationGraph.GetVisibleEdges(
            current
            );

        while (visibleEdges.Count > 0)
        {
            foreach (HexEdge edge in visibleEdges)
            {
                if (!closed.Contains(edge.Target))
                {
                    open.Enqueue(edge.Target);
                    visibleHexes.Add(edge.Target);
                    closed.Add(current);
                }
            }

            current = open.Dequeue();
        }

        return(visibleHexes);

// USE OUT EDGES OF ADJACENCY GRAPH INSTEAD
// This method represents returning a breadth first list of the graph
// which terminates when an edge is encountered where the out edge
// target is higher in elevation than the out edge source.

/*        Queue<ElevationEdge> edgeQueue =
 *          (Queue<ElevationEdge>)graph.OutEdges(fromHex);
 *
 *      List<Hex> result = new List<Hex>();
 *      result.Add(fromHex);
 *
 *      while (edgeQueue.Count > 0) {
 *          ElevationEdge current = edgeQueue.Dequeue();
 *
 *          if (
 *              current.Delta <= 0
 *          ) {
 *              result.Add(current.Target);
 *
 *              foreach (
 *                  ElevationEdge edge in
 *                  (List<HexEdge>)graph.OutEdges(current.Target)
 *              ) {
 *                 edgeQueue.Enqueue(edge);
 *              }
 *          }
 *      }
 *
 *      return result;
 */
/*
 *      List<Hex> visibleHexes = ListPool<Hex>.Get();
 *
 *      _searchFrontierPhase += 2;
 *
 *      if (_searchFrontier == null)
 *      {
 *          _searchFrontier = new HexPriorityQueue();
 *      }
 *      else
 *      {
 *          _searchFrontier.Clear();
 *      }
 *
 *      sightRange += fromHex.ViewElevation;
 *
 * // Temporarily using a list instead of a priority queue.
 * // Should optimize this later.
 * //
 *      fromHex.SearchPhase = _searchFrontierPhase;
 *      fromHex.Distance = 0;
 *      _searchFrontier.Enqueue(fromHex);
 *
 *      HexCoordinates fromCoordinates = fromHex.Coordinates;
 *
 *      while (_searchFrontier.Count > 0)
 *      {
 *          Hex current = _searchFrontier.Dequeue();
 *          current.SearchPhase += 1;
 *
 *          visibleHexes.Add(current);
 *
 *          for (HexDirection direction = HexDirection.Northeast; direction <= HexDirection.Northwest; direction++)
 *          {
 *              Hex neighbor = current.GetNeighbor(direction);
 *
 *              if
 *              (
 *                  neighbor == null ||
 *                  neighbor.SearchPhase > _searchFrontierPhase ||
 *                  !neighbor.IsExplorable
 *              )
 *              {
 *                  continue;
 *              }
 *
 *              int distance = current.Distance + 1;
 *
 *              if
 *              (
 *                  distance + neighbor.ViewElevation > sightRange ||
 *                  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 visibleHexes;
 */
    }