public override ActionResult Execute(AI ai)
    {
        if (!MoveTargetVariable.IsValid || !MoveTargetVariable.IsVariable)
        {
            return(ActionResult.FAILURE);
        }

        WaypointSet waypointSet = GetWaypointSetFromExpression(ai);

        if (waypointSet == null)
        {
            return(ActionResult.FAILURE);
        }

        if (waypointSet != lastWaypointSet)
        {
            lastWaypoint    = -1;
            lastWaypointSet = waypointSet;
        }

        if (lastWaypoint == -1)
        {
            lastWaypoint = waypointSet.GetClosestWaypointIndex(ai.Kinematic.Position);
            if (lastWaypoint < 0)
            {
                return(ActionResult.FAILURE);
            }

            moveTarget.VectorTarget        = waypointSet.Waypoints[lastWaypoint].position;
            moveTarget.CloseEnoughDistance = Mathf.Max(waypointSet.Waypoints[lastWaypoint].range, ai.Motor.CloseEnoughDistance);
            if (!ai.Motor.IsAt(moveTarget))
            {
                ai.WorkingMemory.SetItem <MoveLookTarget>(MoveTargetVariable.VariableName, moveTarget);
                return(ActionResult.SUCCESS);
            }
        }

        //**REMOVED EXTRA LINE HERE
        NavigationGraphNode tNode = waypointSet.Graph.GetNode(lastWaypoint);

        if (tNode.OutEdgeCount > 0)
        {
            int tRandomEdge = UnityEngine.Random.Range(0, tNode.OutEdgeCount); //**FIXED THIS LINE
            lastWaypoint = ((VectorPathNode)tNode.EdgeOut(tRandomEdge).ToNode).NodeIndex;
        }
        moveTarget.VectorTarget        = waypointSet.Waypoints[lastWaypoint].position;
        moveTarget.CloseEnoughDistance = Mathf.Max(waypointSet.Waypoints[lastWaypoint].range, ai.Motor.CloseEnoughDistance);
        ai.WorkingMemory.SetItem <MoveLookTarget>(MoveTargetVariable.VariableName, moveTarget);

        return(ActionResult.SUCCESS);
    }
Пример #2
0
    public override ActionResult Execute(AI ai)
    {
        NavigationTarget finish = NavigationManager.Instance.GetNavigationTarget("Finish");

        moveTarget.VectorTarget        = finish.Position;
        moveTarget.CloseEnoughDistance = Mathf.Max(finish.Range, ai.Motor.CloseEnoughDistance);
        if (ai.Motor.IsAt(moveTarget))
        {
            return(ActionResult.FAILURE);
        }

        if (!MoveTargetVariable.IsValid || !MoveTargetVariable.IsVariable)
        {
            return(ActionResult.FAILURE);
        }

        WaypointSet waypointSet = NavigationManager.Instance.GetWaypointSet("Network");

        if (waypointSet == null)
        {
            return(ActionResult.FAILURE);
        }


        lastWaypoint = waypointSet.GetClosestWaypointIndex(ai.Kinematic.Position);
        if (lastWaypoint < 0)
        {
            return(ActionResult.FAILURE);
        }
        Debug.Log(lastWaypoint);

        visited.Add(lastWaypoint);
        moveTarget.VectorTarget        = waypointSet.Waypoints[lastWaypoint].Position;
        moveTarget.CloseEnoughDistance = Mathf.Max(waypointSet.Waypoints[lastWaypoint].Range, ai.Motor.CloseEnoughDistance);
        if (!ai.Motor.IsAt(moveTarget))
        {
            ai.WorkingMemory.SetItem <MoveLookTarget>(MoveTargetVariable.VariableName, moveTarget);
            return(ActionResult.SUCCESS);
        }
        //If currently at a waypoint
        NavigationGraphNode tNode = waypointSet.Graph.GetNode(lastWaypoint);

        if (tNode.OutEdgeCount > 0)
        {
            var numbers = new List <int>();
            for (int i = 0; i < tNode.OutEdgeCount; i++)
            {
                numbers.Add(i);
            }
            for (int i = 0; i < tNode.OutEdgeCount; i++)
            {
                int j    = UnityEngine.Random.Range(0, tNode.OutEdgeCount - 1);
                int temp = numbers[i];
                numbers[i] = numbers[j];
                numbers[j] = temp;
            }
            for (int i = 0; i < tNode.OutEdgeCount; i++)
            {
                lastWaypoint = ((VectorPathNode)tNode.EdgeOut(numbers[i]).ToNode).NodeIndex;
                if (visited.Contains(lastWaypoint))
                {
                    continue;
                }
                visited.Add(lastWaypoint);
                stack.Push(lastWaypoint);
                moveTarget.VectorTarget        = waypointSet.Waypoints[lastWaypoint].Position;
                moveTarget.CloseEnoughDistance = Mathf.Max(waypointSet.Waypoints[lastWaypoint].Range, ai.Motor.CloseEnoughDistance);
                ai.WorkingMemory.SetItem <MoveLookTarget>(MoveTargetVariable.VariableName, moveTarget);
                return(ActionResult.SUCCESS);
            }
        }
        if (stack.Count == 0)
        {
            return(ActionResult.FAILURE);
        }
        stack.Pop();
        Debug.Log(stack.Count);
        if (stack.Count > 0)
        {
            moveTarget.VectorTarget        = waypointSet.Waypoints[stack.Peek()].Position;
            moveTarget.CloseEnoughDistance = Mathf.Max(waypointSet.Waypoints[lastWaypoint].Range, ai.Motor.CloseEnoughDistance);
            ai.WorkingMemory.SetItem <MoveLookTarget>(MoveTargetVariable.VariableName, moveTarget);
            return(ActionResult.SUCCESS);
        }
        else
        {
            return(ActionResult.FAILURE);
        }
    }