Example #1
0
        bool  Search(HexCell fromCell, HexCell toCell, int step)
        {
            searchFrontierPhase += 2;
            if (searchFrontier == null)
            {
                searchFrontier = new HexCellPriorityQueue();
            }
            else
            {
                searchFrontier.Clear();
            }

            /*
             * for (int i = 0; i < cells.Length; i++)
             * {
             *  //cells[i].Distance = int.MaxValue;
             *  cells[i].DisableHighlight();
             *  cells[i].SetLabel(null);
             * }
             * fromCell.EnableHighlight(Color.blue);
             * //toCell.EnableHighlight(Color.red);
             */

            //WaitForSeconds delay = new WaitForSeconds(1 / 300f);
            //List<HexCell> frontier = new List<HexCell>();
            //frontier.Add(fromCell);

            fromCell.SearchPhase = searchFrontierPhase;
            fromCell.Distance    = 0;
            searchFrontier.Enqueue(fromCell);
            while (searchFrontier.Count > 0)
            {
                // yield return delay;
                HexCell current = searchFrontier.Dequeue();
                current.SearchPhase += 1;
                //frontier.RemoveAt(0);

                if (current == toCell)
                {
                    return(true);
                    //current = current.PathFrom;

                    /*
                     * while (current != fromCell)
                     * {
                     *  int turn = current.Distance / step;
                     *  current.SetLabel(turn.ToString());
                     *  current.EnableHighlight(Color.white);
                     *  current = current.PathFrom;
                     * }
                     * toCell.EnableHighlight(Color.red);
                     * break;
                     */
                }
                //int currentTurn = ( current.Distance - 1) / step;
                for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
                {
                    HexCell neighbor = current.GetNeighbor(d);
                    if (neighbor == null || neighbor.SearchPhase > searchFrontierPhase)
                    {
                        continue;
                    }
                    if (neighbor.FleetUnit)
                    {
                        continue;
                    }

                    int distance = current.Distance;
                    if (neighbor.TerrainTypeIndex == 2)
                    {
                        continue;
                    }
                    else
                    {
                        distance += 5;
                    }
                    //int turn = (distance - 1) / step;


                    // if (neighbor.Distance == int.MaxValue)
                    if (neighbor.SearchPhase < searchFrontierPhase)
                    {
                        neighbor.SearchPhase = searchFrontierPhase;
                        neighbor.Distance    = distance;
                        //neighbor.SetLabel(turn.ToString());
                        neighbor.PathFrom        = current;
                        neighbor.SearchHeuristic =
                            neighbor.coordinates.DistanceTo(toCell.coordinates);
                        //frontier.Add(neighbor);
                        searchFrontier.Enqueue(neighbor);
                    }
                    else if (distance < neighbor.Distance)
                    {
                        int oldPriority = neighbor.SearchPriority;
                        neighbor.Distance = distance;
                        //neighbor.SetLabel(turn.ToString());
                        neighbor.PathFrom = current;
                        searchFrontier.Change(neighbor, oldPriority);
                    }
                    // frontier.Sort(
                    //     (x, y) => x.SearchPriority.CompareTo(y.SearchPriority)
                    //);
                }
            }
            return(false);
        }