Example #1
0
    int RaiseTerrain(int chunkSize, int budget, MapRegion region)
    {
        searchFrontierPhase += 1;
        HexCell firstCell = GetRandomCell(region);

        firstCell.SearchPhase     = searchFrontierPhase;
        firstCell.MovementCost    = 0;
        firstCell.SearchHeuristic = 0;
        searchFrontier.Enqueue(firstCell);
        HexCoordinates center = firstCell.coordinates;

        int rise = Random.value < mountainProbabilty ? 2 : 1;
        int size = 0;

        while (size < chunkSize && searchFrontier.Count > 0)
        {
            HexCell current           = searchFrontier.Dequeue();
            int     originalElevation = current.Elevation;
            int     newElevation      = originalElevation + rise;
            if (newElevation > elevationMaximum)
            {
                continue;
            }
            else if (newElevation == elevationMaximum && Random.value > mountainProbabilty)
            {
                continue;
            }
            current.Elevation = newElevation;
            if (originalElevation < waterLevel && newElevation >= waterLevel && --budget == 0)
            {
                break;
            }
            size += 1;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (neighbor && neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.MovementCost    = neighbor.coordinates.DistanceTo(center);
                    neighbor.SearchHeuristic = Random.value < jitterProbability ? 1: 0;
                    searchFrontier.Enqueue(neighbor);
                }
            }
        }
        searchFrontier.Clear();
        return(budget);
    }
Example #2
0
    //the main search method. returns whether or not a path has been found.
    bool Search(HexCell fromCell, HexCell toCell, Unit unit)
    {
        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new HexPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        fromCell.SearchPhase  = searchFrontierPhase;
        fromCell.MovementCost = 0; //in turns, but unadjusted to speed
        searchFrontier.Enqueue(fromCell);
        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            int currentCellMovementCost = current.MovementCost - 1;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (neighbor == null || neighbor.SearchPhase > searchFrontierPhase)
                {
                    continue;
                }
                if (!unit.IsValidDestination(neighbor))
                {
                    continue;
                }


                float movementCost = unit.GetMoveCost(current, neighbor, d);


                if (movementCost < 0)
                {
                    continue;
                }

                //DEBUG
                movementCost = 1;
                //DEBUG

                int newNeighborMC = (int)(current.MovementCost + movementCost);

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.MovementCost    = newNeighborMC;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic = neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (newNeighborMC < neighbor.MovementCost)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.MovementCost = newNeighborMC;
                    neighbor.PathFrom     = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(false);
    }