Пример #1
0
        private int pathWidth = 2; //additional left cell along moving direction must be available

        /// <summary>
        /// Create all possible paths from this position
        /// </summary>
        /// <param name="A"></param>
        private void CreateGlobWayMap(Map WorkMap, PFCell A)
        {
            // UnityEngine.Debug.Log("create path to top ");
            WorkMap.ResetPath();
            List <PFCell> waveArray = new List <PFCell>();

            waveArray.Add(A);
            A.mather = A;

            bool work = true;

            while (work)
            {
                work = false;
                List <PFCell> waveArrayTemp = new List <PFCell>();
                foreach (PFCell mather in waveArray)
                {
                    if (mather.available || (A == mather && !mather.available))
                    {
                        List <PFCell> childrens = mather.neighBorns;
                        foreach (PFCell child in childrens)
                        {
                            if (!child.HaveMather() && child.available && child.IsPassabilityFrom(mather))  /// try
                            {
                                child.mather = mather;
                                waveArrayTemp.Add(child);
                                work = true;
                            }
                        }
                    }
                }
                waveArray = waveArrayTemp;
            }
        }
Пример #2
0
        /// <summary>
        /// Create the shortest path if exist, else fullPath set to null
        /// </summary>
        /// <param name="WorkMap"></param>
        /// <param name="A"></param>
        /// <param name="B"></param>
        public void CreatePath(Map WorkMap, PFCell A, List <PFCell> B)
        {
            fullPath = null;
            if (WorkMap == null || A == null || B == null || B.Count == 0 || !A.available)
            {
                return;
            }

            List <PFCell> tempPath;

            CreateGlobWayMap(WorkMap, A);
            foreach (var item in B)
            {
                if (item.available)
                {
                    if (IsWayExistTo(item))
                    {
                        tempPath = new List <PFCell>();
                        tempPath.Add(item);

                        PFCell mather = item.mather;
                        while (mather != A.mather)
                        {
                            tempPath.Add(mather);
                            mather = mather.mather;
                        }
                        tempPath.Reverse();
                        if (fullPath == null || fullPath.Count > tempPath.Count)
                        {
                            fullPath = tempPath;
                        }
                    }
                }
            }
        }
Пример #3
0
 public PFCell(GridCell gCell)
 {
     this.gCell = gCell;
     mather     = null;
     row        = gCell.Row;
     col        = gCell.Column;
 }
Пример #4
0
 public void CreatePath(Map WorkMap, PFCell A, PFCell B)
 {
     //   UnityEngine.Debug.Log(A + " : " + B);
     fullPath = null;
     if (WorkMap == null || A == null || B == null || !A.available || !B.available)
     {
         return;
     }
     //    if (!IsWayCreated(A, B))
     {
         CreateGlobWayMap(WorkMap, A, B);
         if (IsWayExistTo(B))
         {
             fullPath = new List <PFCell>();
             fullPath.Add(B);
             PFCell mather = B.mather;
             while (mather != A.mather)
             {
                 fullPath.Add(mather);
                 mather = mather.mather;
             }
             fullPath.Reverse();
         }
         //else
         //{
         //    fullPath.Add(A);
         //}
     }
 }
Пример #5
0
 public void CreatePath(Map WorkMap, PFCell A, PFCell B)
 {
     fullPath = null;
     if (WorkMap == null || A == null || B == null || !A.available || !B.available)
     {
         return;
     }
     if (!IsWayCreated(A, B))
     {
         CreateGlobWayMap(WorkMap, A, B);
         if (IsWayExistTo(B))
         {
             fullPath = new List <PFCell>();
             fullPath.Add(B);
             PFCell mather = B.mather;
             while (mather != A.mather)
             {
                 fullPath.Add(mather);
                 mather = mather.mather;
             }
             fullPath.Reverse();
         }
         else
         {
             fullPath.Add(A);
         }
     }
 }
Пример #6
0
        /// <summary>
        /// Create the shortest path if exist, else fullPath set to null
        /// </summary>
        /// <param name="WorkMap"></param>
        /// <param name="A"></param>
        /// <param name="B"></param>
        public void CreatePathToTop(Map WorkMap, PFCell A)
        {
            fullPath = null;
            if (WorkMap == null || A == null)
            {
                return;
            }

            List <PFCell> tempPath;

            CreateGlobWayMap(WorkMap, A);
            PFCell        mather;
            List <PFCell> topAvailable = new List <PFCell>();
            int           minRow       = int.MaxValue;

            // get top available cells
            foreach (var item in WorkMap.PFCells)
            {
                if (IsWayExistTo(item))
                {
                    if (minRow >= item.row)
                    {
                        minRow = item.row;
                        topAvailable.Add(item);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            // UnityEngine.Debug.Log("min row :" + minRow);

            // create shortest path to top available cells
            foreach (var item in topAvailable)
            {
                if (item.row == minRow)
                {
                    tempPath = new List <PFCell>(topAvailable.Count);
                    tempPath.Add(item);

                    mather = item.mather;
                    while (mather != A.mather)
                    {
                        tempPath.Add(mather);
                        mather = mather.mather;
                    }
                    tempPath.Reverse();
                    if (fullPath == null || fullPath.Count > tempPath.Count)
                    {
                        fullPath = tempPath;
                    }
                }
            }
            //  UnityEngine.Debug.Log("Path to top created " + DebugPath());
        }
Пример #7
0
        public PFCell GetRandomPFPositionInMapToGo(PFCell A)
        {
            PFCell B = pfCells[UnityEngine.Random.Range(0, pfCells.Count)];

            while (B == A || B.available)
            {
                B = pfCells[UnityEngine.Random.Range(0, pfCells.Count)];
            }
            return(B);
        }
Пример #8
0
 public bool IsNeighBorn(PFCell a)
 {
     foreach (var item in neighBorns)
     {
         if (item == a)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #9
0
 public bool IsNeighBorn(PFCell a)
 {
     foreach (var item in Neighbors.Cells)
     {
         if (item.pfCell == a)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #10
0
        public bool IsPassabilityFrom(PFCell a)
        {
            //// min 2 neighborns isavailabe
            //List<PFCell> availableNeighBorns = GetAvailableNeighBorns();
            //if (availableNeighBorns.Count == 6) return true;

            //foreach (var item in availableNeighBorns)
            //{
            //    if (item.IsNeighBorn(a)) return true;
            //}
            //return false;
            return(true);
        }
Пример #11
0
        public List <PFCell> GetAvailablePFPositionAround(Map WorkMap, PFCell A, int distance)
        {
            List <PFCell> lPos = new List <PFCell>();

            CreateGlobWayMap(WorkMap, A);
            foreach (var item in WorkMap.PFCells)
            {
                if (IsWayExistTo(item) && item.GetDistanceTo(A) == distance)
                {
                    lPos.Add(item);
                }
            }
            return(lPos);
        }
Пример #12
0
        /// <summary>
        /// Create all possible paths to destination point
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        private void CreateGlobWayMap(Map WorkMap, PFCell A, PFCell B)
        {
            WorkMap.ResetPath();
            List <PFCell> waveArray = new List <PFCell>();

            waveArray.Add(A);
            A.mather = A;
            bool work = true;

            while (work)
            {
                work = false;
                List <PFCell> waveArrayTemp = new List <PFCell>();
                foreach (PFCell mather in waveArray)
                {
                    if (mather.available)
                    {
                        List <PFCell> childrens = new List <PFCell>();
                        if (pathType == PathType.Vertical)
                        {
                            if (mather.Neighbors.Top)
                            {
                                childrens.Add(mather.Neighbors.Top.pfCell);
                            }
                        }
                        else
                        {
                            childrens = mather.Neighbors.GetNeighBorsPF();
                        }

                        foreach (PFCell child in childrens)
                        {
                            if (!child.HaveMather())
                            {
                                child.mather = mather;
                                waveArrayTemp.Add(child);
                                work = true;
                                if (child == B)
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
                waveArray = waveArrayTemp;
            }
        }
Пример #13
0
        private void CreateMap(BubbleGrid grid)
        {
            pfCells = new List <PFCell>(grid.Cells.Count);

            // create all pfcells
            foreach (var item in grid.Cells)
            {
                PFCell pfc = new PFCell(item);
                pfc.available = (item.Mainobject == null);
                item.pfCell   = pfc;
                pfCells.Add(pfc);
            }

            // set pfcell neighborns
            foreach (var item in pfCells)
            {
                item.CreateNeighBorns();
            }
        }
Пример #14
0
        public bool IsPassabilityFrom(PFCell a) // depends on width,  we use width = 1.5 for anchor, (half wave)
        {
            // min 2 neighborns isavailabe
            List <PFCell> availableNeighBorns = GetAvailableNeighBorns();

            if (availableNeighBorns.Count == 6)
            {
                return(true);
            }

            foreach (var item in availableNeighBorns)
            {
                if (item.IsNeighBorn(a))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #15
0
        private void CreateMap(MatchGrid grid)
        {
            pfCells = new List <PFCell>(grid.Cells.Count);
            //  UnityEngine.Debug.Log("Create new map");

            // create all pfcells
            foreach (var item in grid.Cells)
            {
                PFCell pfc = new PFCell(item);
                pfc.available = (!item.IsDisabled && !item.Blocked && !item.StaticBlocker);
                item.pfCell   = pfc;
                pfCells.Add(pfc);
            }

            // set pfcell neighborns
            foreach (var item in pfCells)
            {
                item.CreateNeighBorns();
            }
        }
Пример #16
0
        public void CreateNeighBorns()
        {
            if (gCell == null)
            {
                return;
            }
            NeighBorns nBs = new NeighBorns(gCell); // gridcell neighborns

            neighBorns = new List <PFCell>(nBs.Cells.Count);

            foreach (var n in nBs.Cells)
            {
                neighBorns.Add(n.pfCell);
            }

            Left        = (nBs.Left) ? nBs.Left.pfCell : null;
            Right       = (nBs.Right) ? nBs.Right.pfCell : null;
            TopLeft     = (nBs.TopLeft) ? nBs.TopLeft.pfCell : null;
            TopRight    = (nBs.TopRight) ? nBs.TopRight.pfCell : null;
            BottomLeft  = (nBs.BottomLeft) ? nBs.BottomLeft.pfCell : null;
            BottomRight = (nBs.BottomRight) ? nBs.BottomRight.pfCell : null;
        }
Пример #17
0
        /// <summary>
        /// Create all possible paths to destination point
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        private void CreateGlobWayMap(Map WorkMap, PFCell A, PFCell B)
        {
            WorkMap.ResetPath();
            List <PFCell> waveArray = new List <PFCell>();

            waveArray.Add(A);
            A.mather = A;
            bool work = true;

            while (work)
            {
                work = false;
                List <PFCell> waveArrayTemp = new List <PFCell>();
                foreach (PFCell mather in waveArray)
                {
                    if (mather.available)
                    {
                        List <PFCell> childrens = mather.neighBorns;
                        foreach (PFCell child in childrens)
                        {
                            if (!child.HaveMather())
                            {
                                child.mather = mather;
                                waveArrayTemp.Add(child);
                                work = true;
                                if (child == B)
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
                waveArray = waveArrayTemp;
            }
        }
Пример #18
0
 private bool IsWayExistTo(PFCell B)
 {
     return(B.HaveMather() && B.available);
 }
Пример #19
0
 private void CreatePathThread(Map WorkMap, PFCell A, PFCell B)
 {
     ThreadPool.QueueUserWorkItem(m => CreatePath(WorkMap, A, B));
 }
Пример #20
0
 public int GetDistanceTo(PFCell other)
 {
     return(Mathf.Abs(other.row - row) + Mathf.Abs(other.col - col));
 }
Пример #21
0
 /// <summary>
 /// Return true if FullPathA contain start point and end point
 /// </summary>
 /// <param name="A"></param>
 /// <param name="B"></param>
 /// <returns></returns>
 private bool IsWayCreated(PFCell A, PFCell B)
 {
     return(PathLenght > 0 && fullPath[0] == A && fullPath[PathLenght - 1] == B);
 }