Example #1
0
        public void GenerateMap(int x, int z, bool wrapping)
        {
            Random.State originalRandomState = Random.state;
            if (!useFixedSeed)
            {
                seed  = Random.Range(0, int.MaxValue);
                seed ^= (int)System.DateTime.Now.Ticks;
                seed ^= (int)Time.time;
                seed &= int.MaxValue;
            }
            Random.InitState(seed);
            {
                cellCount = x * z;
                grid.CreateMap(x, z, wrapping);
                if (searchFrontier == null)
                {
                    searchFrontier = new HexCellPriorityQueue();
                }

                for (int i = 0; i < cellCount; i++)
                {
                    grid.GetCell(i).WaterLevel = waterLevel;
                }

                CreateRegions();
                CreateLand();
                ErodeLand();
                CreateClimate();
                CreateRivers();
                SetTerrainType();

                for (int i = 0; i < cellCount; i++)
                {
                    grid.GetCell(i).SearchPhase = 0;
                }
            }
            Random.state = originalRandomState;
        }
Example #2
0
        private 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);
        }
Example #3
0
        private bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
        {
            int speed = unit.Speed;

            searchFrontierPhase += 2;
            if (searchFrontier == null)
            {
                searchFrontier = new HexCellPriorityQueue();
            }
            else
            {
                searchFrontier.Clear();
            }

            fromCell.SearchPhase = searchFrontierPhase;
            fromCell.Distance    = 0;
            searchFrontier.Enqueue(fromCell);

            while (searchFrontier.Count > 0)
            {
                HexCell current = searchFrontier.Dequeue();
                current.SearchPhase += 1;

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

                int currentTurn = (current.Distance - 1) / speed;
                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;
                    }
                    int moveCost = unit.GetMoveCost(current, neighbor, d);
                    if (moveCost < 0)
                    {
                        continue;
                    }

                    int distance = current.Distance + moveCost;
                    int turn     = (distance - 1) / speed;
                    if (turn > currentTurn)
                    {
                        distance = turn * speed + moveCost;
                    }

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