void AddOpenNode(int x, int y)
    {
        Vector2Int searchPos = new Vector2Int(x, y);
        int        posType   = MapDataController.map[pos.x, pos.y].GetType();

        if (posType == 0)
        {
            CoordinateRouteInfo cri = new CoordinateRouteInfo(pos, searchPos, goal, null);
            openNodes.Add(cri);
        }
    }
    public CoordinateRouteInfo(Vector2Int start, Vector2Int pos, Vector2Int goal, CoordinateRouteInfo prevNode)
    {
        this.pos = pos;
        SetHCost(goal);

        if (prevNode != null)
        {
            this.prevNode = prevNode;
            SetGCost(start);
        }

        if (prevNode == null)
        {
            gCost = 0;
        }

        SetFCost();
        //Debug.Log("POS " + pos);
        //Debug.Log("Start " + start);
        //Debug.Log("Goal " + goal);
    }
    void BuildRouteBackwards()
    {
        int i = 0;

        if (current.prevNode != null)
        {
            goal = current.prevNode.pos;
            routeBackwards.Add(current.prevNode);
        }

        if (current.prevNode == null)
        {
            routeBackwards.Add(current);
        }


        while (routeBackwards[i].prevNode != null)
        {
            CoordinateRouteInfo prev = routeBackwards[i].prevNode;
            routeBackwards.Add(prev);
            i++;
        }
    }
 void AddClosedNode(CoordinateRouteInfo current)
 {
     closedNodes.Add(current);
 }
    void FindPath()
    {
        MapCoordinate[,] map = MapDataController.map;
        AddOpenNode(pos.x, pos.y);
        current = openNodes[0];
        int searches = 0;

        while (openNodes.Count > 0)
        {
            if (MapDataController.plrSurrounded == true)
            {
                break;
            }

            int IndexWithLowestFCost = SearchForLowestOpenFCost();
            current = openNodes[IndexWithLowestFCost];

            RemoveOpenNode(IndexWithLowestFCost);
            AddClosedNode(current);

            if (closedNodes.Count >= (MapDataController.area))
            {
                waiting = true;
                return;
            }

            if (current.pos == goal)
            {
                SetRoute();
                return;
            }

            if (searches > (MapDataController.area / 4))
            {
                SetRoute();
                break;
            }

            for (int i = 1; i <= 9; i++)
            {
                if (i == 5)
                {
                    i++;
                }

                Vector2Int neightbourPos = GetNeightbour(current.pos, i);
                //print(current.pos + " Current");
                //print(neightbourPos + " Neightbour " + i);
                CoordinateRouteInfo neightbour = new CoordinateRouteInfo(pos, neightbourPos, goal, current);

                bool SkipToNextNeightbour = false;
                if (SkipToNextNeightbour == false)
                {
                    //if neightbour is not traversable
                    int        neightbourPosType = map[neightbourPos.x, neightbourPos.y].GetType();
                    PlayerInfo neightbourPosNpc  = map[neightbourPos.x, neightbourPos.y].GetNpc();
                    int        neightbourTeam    = 2;

                    if (neightbourPosNpc != null)
                    {
                        neightbourTeam = neightbourPosNpc.GetTeam();
                    }

                    if (neightbourPosType == 1 || (neightbourPosNpc != null && neightbourTeam == 2))
                    {
                        SkipToNextNeightbour = true;
                    }

                    if (SkipToNextNeightbour == false)
                    {
                        //or neightbour is in closed
                        bool neightbourIsInClosed = CheckIfNeightboorIsInClosed(neightbourPos);
                        if (neightbourIsInClosed)
                        {
                            SkipToNextNeightbour = true;
                        }
                    }
                }



                bool stepTwoControl = false;
                if (stepTwoControl == false && SkipToNextNeightbour == false)
                {
                    //if new path to neightbour is shorter
                    if (neightbour.fCost < current.fCost)
                    {
                        openNodes.Add(neightbour);
                        stepTwoControl = true;
                    }
                    if (stepTwoControl == false)
                    {
                        //or neightbour is not in open
                        bool neightbourIsInOpen = CheckIfNeightboorIsInOpen(neightbourPos);
                        if (!neightbourIsInOpen)
                        {
                            openNodes.Add(neightbour);
                            stepTwoControl = true;
                        }
                    }
                }

                if (showSearch)
                {
                    MarkopenNodessWithColor();
                    MarkClosedNodessWithColor();
                    MarkCurrentNodeWithColor();
                }

                totalSearch++;
                searches++;
            }
        }
    }