Пример #1
0
        private bool Search(HexCell fromCell, HexCell toCell)
        {
            searchFrontierPhase += 2;
            if (searchFrontier == null)
            {
                searchFrontier = new HexCellPriorityQueue();
            }
            else
            {
                searchFrontier.Clear();
            }

            fromCell.SearchPhase = searchFrontierPhase;
            fromCell.Distance    = 0;
            searchFrontier.Enqueue(fromCell);
            while (searchFrontier.Count > 0)
            {
                var current = searchFrontier.Dequeue();
                current.SearchPhase += 1;

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

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

                    var distance = current.Distance + 1;

                    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)
                    {
                        var oldPriority = neighbor.SearchPriority;
                        neighbor.Distance = distance;
                        neighbor.PathFrom = current;
                        searchFrontier.Change(neighbor, oldPriority);
                    }
                }
            }

            return(false);
        }
Пример #2
0
        public void GenerateMap(int x, int z)
        {
            var originalRandomState = Random.state;

            if (!useFixedSeed)
            {
                seed  = Random.Range(0, int.MaxValue);
                seed ^= (int)DateTime.Now.Ticks;
                seed ^= (int)Time.unscaledTime;
                seed &= int.MaxValue;
            }

            Random.InitState(seed);

            cellCount = x * z;
            grid.CreateMap(x, z);
            if (searchFrontier == null)
            {
                searchFrontier = new HexCellPriorityQueue();
            }
            for (var i = 0; i < cellCount; i++)
            {
                grid.GetCell(i).WaterLevel = waterLevel;
            }
            CreateRegions();
            CreateLand();
            ErodeLand();
            CreateClimate();
            CreateRivers();
            SetTerrainType();
            for (var i = 0; i < cellCount; i++)
            {
                grid.GetCell(i).SearchPhase = 0;
            }

            Random.state = originalRandomState;
        }
Пример #3
0
        private List <HexCell> GetVisibleCells(HexCell fromCell, int range)
        {
            var 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);
            var fromCoordinates = fromCell.coordinates;

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

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

                    var 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)
                    {
                        var oldPriority = neighbor.SearchPriority;
                        neighbor.Distance = distance;
                        searchFrontier.Change(neighbor, oldPriority);
                    }
                }
            }

            return(visibleCells);
        }
Пример #4
0
        private bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
        {
            var 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)
            {
                var current = searchFrontier.Dequeue();
                current.SearchPhase += 1;

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

                var currentTurn = (current.Distance - 1) / speed;

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

                    var distance = current.Distance + moveCost;
                    var 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)
                    {
                        var oldPriority = neighbor.SearchPriority;
                        neighbor.Distance = distance;
                        neighbor.PathFrom = current;
                        searchFrontier.Change(neighbor, oldPriority);
                    }
                }
            }

            return(false);
        }
Пример #5
0
        public List <HexCell> SearchInRange(HexCell fromCell, int range, bool includeStart)
        {
            searchFrontierPhase += 2;

            List <HexCell> retCells = new List <HexCell>();

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

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

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

                retCells.Add(current);

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

                    var distance = current.Distance + 1;
                    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)
                    {
                        var oldPriority = neighbor.SearchPriority;
                        neighbor.Distance = distance;
                        neighbor.PathFrom = current;
                        searchFrontier.Change(neighbor, oldPriority);
                    }
                }
            }

            if (!includeStart)
            {
                retCells.Remove(fromCell);
            }
            return(retCells);
        }