예제 #1
0
    public void OnLoadWayPointPaths()
    {
        CTimeCheck.Start();

        FileStream   fileStream   = null;
        BinaryReader binaryReader = null;

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            CTextAssetMgr TextAssetMgr = GameObject.FindGameObjectWithTag("TextAssetMgr").GetComponent <CTextAssetMgr>();
            binaryReader = TextAssetMgr.LoadTextAsset(CTextAssetMgr.EFileType.MapWPP, FileName);
            if (binaryReader == null)
            {
                return;
            }
        }
        else
        {
            string FileFullPath = FileName + "_WPP";
            string Path         = GetPath(ref FileFullPath);
            if (false == File.Exists(Path))
            {
                return;
            }
            fileStream   = new FileStream(Path, FileMode.Open);
            binaryReader = new BinaryReader(fileStream);
        }
        WayPointPaths.Clear();
        int Count = binaryReader.ReadInt32();

        for (int i = 0; i < Count; ++i)
        {
            int       Key        = binaryReader.ReadInt32();
            int       ValueCount = binaryReader.ReadInt32();
            ArrayList paths      = new ArrayList();
            for (int j = 0; j < ValueCount; ++j)
            {
                paths.Add(binaryReader.ReadInt32());
            }
            WayPointPaths.Add(Key, paths);
        }
        binaryReader.Close();
        if (fileStream != null)
        {
            fileStream.Close();
        }
        CTimeCheck.End(ELogType.MapGenerator, "OnLoadWayPointPaths");
    }
예제 #2
0
    public override ArrayList FindPaths(int StartPos, int EndPos, int InID)
    {
        ArrayList Paths = new ArrayList();

        if (!IsValidPosIndex(StartPos) || !IsValidPosIndex(EndPos))
        {
            return(Paths);
        }

        CTimeCheck.Start(InID);

        Tile StartTile = (Tile)Tiles[StartPos];
        Tile EndTile   = (Tile)Tiles[EndPos];

        if (StartTile.bBlock || EndTile.bBlock)
        {
            return(Paths);
        }

        bool bFindedBlock = StartAndEndPathBlockCheck(StartPos, EndPos);

        if (false == bFindedBlock)
        {
            Paths.Add(EndPos);
            CTimeCheck.End(ELogType.AStar, "Success FindPaths");
            return(Paths);
        }

        Vector2 StartVec = new Vector2(StartTile.PosX, StartTile.PosY);
        Vector2 EndVec   = new Vector2(EndTile.PosX, EndTile.PosY);

        int CloseWayPointIdxStart = -1;
        int CloseWayPointIdxEnd   = -1;

        float StartLenth          = 99999.0f;
        float StartHeuristicLenth = 99999.0f;
        float EndLenth            = 99999.0f;
        float EndHeuristicLenth   = 99999.0f;

        for (int i = 0; i < WayPoints.Count; ++i)
        {
            Tile    WayPointTile     = (Tile)Tiles[(int)WayPoints[i]];
            Vector2 WayPointPos      = new Vector2(WayPointTile.PosX, WayPointTile.PosY);
            float   CurStartLenth    = Vector2.Distance(StartVec, WayPointPos);
            float   CurEndLenth      = Vector2.Distance(EndVec, WayPointPos);
            float   CurHeristicLenth = CurStartLenth + CurEndLenth;
            if (CurStartLenth < StartLenth || (CurStartLenth == StartLenth && CurHeristicLenth < StartHeuristicLenth))
            {
                CloseWayPointIdxStart = WayPointTile.Index;
                StartLenth            = CurStartLenth;
                StartHeuristicLenth   = CurHeristicLenth;
            }
            if (CurEndLenth < EndLenth || (CurEndLenth == EndLenth && CurHeristicLenth < EndHeuristicLenth))
            {
                CloseWayPointIdxEnd = WayPointTile.Index;
                EndLenth            = CurEndLenth;
                EndHeuristicLenth   = CurHeristicLenth;
            }
        }
        if (CloseWayPointIdxStart == -1)
        {
            Paths.Add(EndPos);
        }
        else if (CloseWayPointIdxStart == CloseWayPointIdxEnd)
        {
            Paths.Add(CloseWayPointIdxStart);
            Paths.Add(EndPos);
        }
        else
        {
            int       Key = CloseWayPointIdxStart < CloseWayPointIdxEnd ? CloseWayPointIdxStart * 10000 + CloseWayPointIdxEnd : CloseWayPointIdxEnd * 10000 + CloseWayPointIdxStart;
            ArrayList FindPaths;
            if (WayPointPaths.TryGetValue(Key, out FindPaths))
            {
                Paths = (ArrayList)FindPaths.Clone();
                if ((int)Paths[0] == CloseWayPointIdxStart)
                {
                    Paths.Insert(Paths.Count, EndPos);
                }
                else
                {
                    Paths.Reverse();
                    Paths.Insert(Paths.Count, EndPos);
                }
            }
            else
            {
                return(Paths);
            }
        }

        CTimeCheck.End(ELogType.AStar, "Success FindPaths");

        return(Paths);
    }
예제 #3
0
    public void OnBuildWayPointPaths()
    {
        CTimeCheck.Start(0);

        OnMakeWayPoints();

        CAStar AStar = new CAStar();

        AStar.InitTiles(ref Tiles);

        for (int i = WayPoints.Count / 2; i < WayPoints.Count; ++i)
        {
            for (int j = WayPoints.Count / 2 - 1; 0 <= j; --j)
            {
                int StartPosIndex = (int)WayPoints[i];
                int EndPosIndex   = (int)WayPoints[j];

                if (StartPosIndex == EndPosIndex)
                {
                    continue;
                }

                int Key = StartPosIndex < EndPosIndex ? StartPosIndex * 10000 + EndPosIndex : EndPosIndex * 10000 + StartPosIndex;
                if (WayPointPaths.ContainsKey(Key))
                {
                    continue;
                }


                ArrayList FindedPaths = AStar.FindPaths(StartPosIndex, EndPosIndex, 0);

                ArrayList RemoveIndexs = new ArrayList();
                for (int k = 0; k < FindedPaths.Count; ++k)
                {
                    if (!WayPoints.Contains(FindedPaths[k]))
                    {
                        RemoveIndexs.Add(FindedPaths[k]);
                    }
                }
                for (int a = 0; a < RemoveIndexs.Count; ++a)
                {
                    FindedPaths.Remove(RemoveIndexs[a]);
                }

                if (3 < FindedPaths.Count)
                {
                    int PathCount = FindedPaths.Count / 2;
                    for (int count = 1; count < PathCount; ++count)
                    {
                        int NewStartIndex = (int)FindedPaths[count];
                        int NewEndIndex   = (int)FindedPaths[FindedPaths.Count - 1 - count];
                        int NewKey        = NewStartIndex < NewEndIndex ? NewStartIndex * 10000 + NewEndIndex : NewEndIndex * 10000 + NewStartIndex;

                        if (WayPointPaths.ContainsKey(NewKey))
                        {
                            continue;
                        }

                        ArrayList clone = (ArrayList)FindedPaths.Clone();
                        clone.RemoveAt(FindedPaths.Count - count);
                        clone.RemoveAt(count - 1);
                        //clone = clone.GetRange(count, FindedPaths.Count - 1 - count);
                        WayPointPaths.Add(NewKey, clone);
                    }
                }
                WayPointPaths.Add(Key, FindedPaths);
            }
        }

        for (int i = 0; i < WayPoints.Count; ++i)
        {
            for (int j = i + 1; j < WayPoints.Count; ++j)
            {
                int StartPosIndex = (int)WayPoints[i];
                int EndPosIndex   = (int)WayPoints[j];

                if (StartPosIndex == EndPosIndex)
                {
                    continue;
                }

                int Key = StartPosIndex < EndPosIndex ? StartPosIndex * 10000 + EndPosIndex : EndPosIndex * 10000 + StartPosIndex;
                if (WayPointPaths.ContainsKey(Key))
                {
                    continue;
                }

                ArrayList FindedPaths = AStar.FindPaths(StartPosIndex, EndPosIndex, 0);

                ArrayList RemoveIndexs = new ArrayList();
                for (int k = 0; k < FindedPaths.Count; ++k)
                {
                    if (!WayPoints.Contains(FindedPaths[k]))
                    {
                        RemoveIndexs.Add(FindedPaths[k]);
                    }
                }
                for (int a = 0; a < RemoveIndexs.Count; ++a)
                {
                    FindedPaths.Remove(RemoveIndexs[a]);
                }

                if (3 < FindedPaths.Count)
                {
                    int PathCount = FindedPaths.Count / 2;
                    for (int count = 1; count < PathCount; ++count)
                    {
                        int NewStartIndex = (int)FindedPaths[count];
                        int NewEndIndex   = (int)FindedPaths[FindedPaths.Count - 1 - count];
                        int NewKey        = NewStartIndex < NewEndIndex ? NewStartIndex * 10000 + NewEndIndex : NewEndIndex * 10000 + NewStartIndex;

                        if (WayPointPaths.ContainsKey(NewKey))
                        {
                            continue;
                        }

                        ArrayList clone = (ArrayList)FindedPaths.Clone();
                        clone.RemoveAt(FindedPaths.Count - count);
                        clone.RemoveAt(count - 1);
                        //clone = clone.GetRange(count, FindedPaths.Count - 1 - count);
                        WayPointPaths.Add(NewKey, clone);
                    }
                }
                WayPointPaths.Add(Key, FindedPaths);
            }
        }

        CTimeCheck.End(ELogType.MapGenerator, "BuildFastPaths");
    }