Пример #1
0
    void RemakePath(Node_Info meNodeInfo) //Backtracking From The EndNode. When The Backtracking Reaches The StartNode, Then The Path Is Set
    {
        ArrayLengthSaver = 0;             //Just A Reused Variable For Holding The Index Number For The Next Node To Enter In The Path

        _CurrentNode = _EndNode;
        meNodeInfo.MyNodePath[ArrayLengthSaver++] = _CurrentNode;


        if (_EndNode == _TheStartNode)
        {
            return;
        }
        else
        {
            _CurrentNode = _EndNode;
        }

        while (true)          //Going Backwards Parent To Parent To Parent.....
        {
            if (_CurrentNode._ParentNode != _TheStartNode)
            {
                meNodeInfo.MyNodePath[ArrayLengthSaver++] = _CurrentNode._ParentNode;
                _CurrentNode = _CurrentNode._ParentNode;
            }
            else
            {
                meNodeInfo.MyNodePath[ArrayLengthSaver++] = _CurrentNode._ParentNode;
                meNodeInfo.MyNodePath[ArrayLengthSaver--] = null;               //Setting This To Be 'null' To Symbolize That This Is The End

                for (int i = 0; i < ArrayLengthSaver / 2; i++)                  //Turning The List Around
                {
                    _CurrentNode             = meNodeInfo.MyNodePath[i];
                    meNodeInfo.MyNodePath[i] = meNodeInfo.MyNodePath[ArrayLengthSaver - i];
                    meNodeInfo.MyNodePath[ArrayLengthSaver - i] = _CurrentNode;
                }

                return;
            }
        }
    }
Пример #2
0
    //float one, two;

    public void StartRunning(Object_Node_Position me, Node_Info meNodeInfo, Object_Node_Position taget)
    {
        if (_TheStartNode == null)
        {
            Setup();
        }

        #region Startup Phase

        for (int i = 0; i < _OpenListAtIndex; i++)          //The Used Nodes Is In These Two Lists. So I Only Need To Reset The Nodes Used
        {
            _OpenList[i].NodeSearchedThrough = false;
        }

        for (int i = 0; i < _ClosedListAtIndex; i++)
        {
            _ClosedList[i].NodeSearchedThrough = false;
        }

        _OpenListAtIndex   = 0;
        _ClosedListAtIndex = 0;

        _NodeXPos = Mathf.FloorToInt(_TheStartNode.PosX + (taget.XNode - me.XNode));
        _NodeYPos = Mathf.FloorToInt(_TheStartNode.PosY + (taget.YNode - me.YNode));

        if (_NodeXPos >= TurretMan_WorldChanger.NodesSize)
        {
            _NodeXPos = TurretMan_WorldChanger.NodesSize - 1;
        }
        else if (_NodeXPos < 0)
        {
            _NodeXPos = 0;
        }
        if (_NodeYPos >= TurretMan_WorldChanger.NodesSize)
        {
            _NodeYPos = TurretMan_WorldChanger.NodesSize - 1;
        }
        else if (_NodeYPos < 0)
        {
            _NodeYPos = 0;
        }

        _EndNode = _NodeMap[_NodeXPos, _NodeYPos];
        _TheStartNode.SetStartNode(_TheStartNode, _EndNode);  //Giving The StartNode its Costs

        _OpenList[_OpenListAtIndex++] = _TheStartNode;        //Giving The Search Through List Its First Node

        #endregion

        #region A* Algorythm

        while (_ClosedListAtIndex < TurretMan_WorldChanger.NodesTotal)          //If The ClosedListAtIndex Is Equalt To Or Greater The Total Amount Of Nodes Then This Is False And The Search Is Stopped
        {
            _LowerstFScore = 10000000;

            for (int i = 0; i < _OpenListAtIndex; i++)              //Iterating Through The List With Unused Nodes To Find The Node With The Lowerst FCost

            {
                if (_OpenList[i].FCost < _LowerstFScore)
                {
                    _CurrentNode    = _OpenList[i];
                    _NodeIndexSaved = i;
                    _LowerstFScore  = _CurrentNode.FCost;
                }
            }

            _ClosedList[_ClosedListAtIndex++] = _CurrentNode;                  //The Node That Was Chosen From Openlist Is Put In Here
            _OpenList[_NodeIndexSaved]        = _OpenList[--_OpenListAtIndex]; //The Node That Was Added To ClosedList Is Being Removed Here And Replaced With The Last Node In The Openlist


            if (_CurrentNode == _EndNode)              //If True Then A Path Was Found And The Search Is Complete
            {
                RemakePath(meNodeInfo);
                return;
            }

            ArrayLengthSaver = _CurrentNode.NeighbourNodess.Length;            //This Is An Improvement Rather Then Getting The Length Each i++ (Not Much But Some)

            for (int i = 0; i < ArrayLengthSaver; i++)
            {
                _NodeHolder = _CurrentNode.NeighbourNodess[i];

                if (_NodeHolder.NodeSearchedThrough == false)                  //If false Then The Node Havent Been Searched Through And Info Need To Be Set
                {
                    _NodeHolder.SetParentAndHCost(_CurrentNode, _EndNode, meNodeInfo.PathfindingNodeID);
                    _OpenList[_OpenListAtIndex++] = _NodeHolder;
                }
                else if (_CurrentNode.GCost < _NodeHolder._ParentNode.GCost)                    //If Current.Gcost Is Less Then Nodeholder.parent.Gcost Then A New ParentNode Is Set  ...... If Errors Occur Use (_NodeHolder.GCost > _CurrentNode.GCost + (PathfindingNodeID[CollisionID[_NodeHolder.PosX, _NodeHolder.PosY]] * 1.4f))
                {
                    _NodeHolder.SetNewParent(_CurrentNode, meNodeInfo.PathfindingNodeID);
                }
            }
        }

        #endregion

        Debug.LogWarning("No Path Detected, Initiating Self Destruct Algorythms.... 3..... 2..... 1..... .");
    }