Beispiel #1
0
 public override void Start(GameScene scene, IPoint point)
 {
     base.Start(scene, point);
     Pixel.BackgroundColor = ConsoleColor.DarkYellow;
     _findPaths            = new FindPaths(this);
 }
Beispiel #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);
    }