Exemple #1
0
    private void createIdleState()
    {
        idleState = (fsm, gameObj) => {
            // GOAP planning

            // Get the world state and the goal we want to plan for
            HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState();
            HashSet <KeyValuePair <string, object> > goal       = dataProvider.createGoalState();

            // Make a plan with the actions the AI wants to do in order
            Queue <GOAPAction> plan = planner.plan(gameObject, availableActions, worldState, goal);
            if (plan != null)
            {
                // We have a plan!
                currentActions = plan;
                dataProvider.planFound(goal, plan);

                fsm.popState();                    // Clear current state
                fsm.pushState(performActionState); // Change to performActionState
            }
            else
            {
                // We don't have a plan
                dataProvider.planFailed(goal);
                fsm.popState();
                fsm.pushState(idleState);                  // Back to square one
            }
        };
    }
Exemple #2
0
    private void LateUpdate()
    {
        if (currentAction != null && currentAction.isActionRunning)
        {
            //float distanceToTarget = Vector3.Distance(currentAction.target.transform.position, this.transform.position);
            if (currentAction.navAgent.hasPath && currentAction.navAgent.remainingDistance < 2.0f)
            {
                if (!invoked)
                {
                    Invoke("CompleteAction", currentAction.duration);
                    invoked = true;
                }
            }
            return;
        }
        if (planner == null || actionQueue == null)
        {
            planner = new GOAPPlanner();

            var sortedGoals = from entry in goals orderby entry.Value descending select entry;//sort goals in order of priority

            foreach (KeyValuePair <Goal, int> sgoal in sortedGoals)
            {
                actionQueue = planner.plan(actions, sgoal.Key.subGoals, beliefs);
                if (actionQueue != null)
                {
                    currentGoal = sgoal.Key;
                    break;
                }
            }
        }
        if (actionQueue != null && actionQueue.Count == 0)
        {
            if (currentGoal.remove)
            {
                goals.Remove(currentGoal);
            }
            planner = null;
        }
        if (actionQueue != null && actionQueue.Count > 0)
        {
            currentAction = actionQueue.Dequeue();
            if (currentAction.PrePerform())
            {
                if (currentAction.target == null && currentAction.targetTag != "")
                {
                    currentAction.target = GameObject.FindGameObjectWithTag(currentAction.targetTag);
                }
                if (currentAction.target != null)
                {
                    currentAction.isActionRunning = true;
                    currentAction.navAgent.SetDestination(currentAction.target.transform.position);
                }
            }
            else
            {
                actionQueue = null;
            }
        }
    }
Exemple #3
0
    private void CreateIdleState(GOAPComponent aic)
    {
        aic.idleState = (fsm, obj) =>
        {
            //aic.AnnounceIdleState();

            HashSet <KeyValuePair <string, object> > worldState = worldStateComponent.worldState;
            HashSet <KeyValuePair <string, object> > goal       = aic.CreateGoalState();

            Queue <GOAPAction> plan = planner.plan(aic.gameObject, aic.availableActions, worldState, goal);

            /*int index = 1;
             * foreach (GOAPAction a in aic.availableActions)
             * {
             *  Debug.Log("action no: " + index + " " + a);
             *  index++;
             * }*/

            if (plan != null)
            {
                aic.currentActions = plan;
                aic.PlanFound(goal, plan);

                fsm.popState();
                fsm.pushState(aic.performActionState);
            }
            else
            {
                aic.PlanFailed(goal);
                fsm.popState();
                fsm.pushState(aic.idleState);
            }
        };
    }
Exemple #4
0
    private void createIdleState()
    {
        idleState = (FiniteStateMachine, gameObj) =>
        {
            //Planning

            //Gets world state
            HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState();
            HashSet <KeyValuePair <string, object> > goal       = dataProvider.createGoalState();

            //Plan
            Queue <GOAPAction> plan = planner.plan(gameObject, actionsAvailable, worldState, goal);
            if (plan != null)
            {
                //Already Have Plan
                currentActions = plan;
                dataProvider.planFound(goal, plan);

                FiniteStateMachine.Pop(); //switch to PerformAction
                FiniteStateMachine.Push(performActionState);
            }
            else
            {
                //No Plan
                dataProvider.planFailed(goal);
                FiniteStateMachine.Pop(); //switch to idle
                FiniteStateMachine.Push(idleState);
            }
        };
    }
Exemple #5
0
    /// <summary>
    /// Creates the Idle state to be fed into the FSM.
    /// The IdleState will communicate with the concrete implementation of the IGoap interface and get from it the World state.
    /// It will then proceed to create a Goal and formulate a plan from those variables.
    /// If a plan is found, the event is communicated to the data provider through the planFound(goal, plan) method, then the PerformActionState is pushed on top of the FSM.
    /// If a plan is NOT found, the IdleState gets pushed again and a planFailed(goal) message is sent to the data provider.
    /// </summary>
    private void createIdleState()
    {
        idleState = (fsm, gameObj) => {
            // GOAP planning

            // get the world state and the goal we want to plan for
            HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState();
            HashSet <KeyValuePair <string, object> > goal       = dataProvider.createGoalState();

            // Plan
            Queue <Action> plan = planner.plan(gameObject, availableActions, worldState, goal);
            if (plan != null)
            {
                // we have a plan, hooray!
                currentActions = plan;
                dataProvider.planFound(goal, plan);

                fsm.PopState(); // move to PerformAction state
                fsm.PushState(performActionState);
            }
            else
            {
                // ugh, we couldn't get a plan
                Debug.Log("<color=orange>Failed Plan:</color>" + prettyPrint(goal));
                dataProvider.planFailed(goal);
                fsm.PopState(); // move back to IdleAction state
                fsm.PushState(idleState);
            }
        };
    }
Exemple #6
0
    void LateUpdate()
    {
        if (currentAction != null && currentAction.running)
        {
            if (currentAction.agent.hasPath && currentAction.agent.remainingDistance < 1f)
            {
                if (!invoked)
                {
                    Invoke("CompleteAction", currentAction.duration);
                    invoked = true;
                }
            }
            return;
        }

        if (planner == null || actionQueue == null)
        {
            planner = new GOAPPlanner();

            var sortedGoals = from entry in goals orderby entry.Value descending select entry;

            foreach (KeyValuePair <SubGoal, int> sg in sortedGoals)
            {
                actionQueue = planner.plan(actions, sg.Key.sgoals, null);
                if (actionQueue != null)
                {
                    currentGoal = sg.Key;
                    break;
                }
            }
        }

        if (actionQueue != null && actionQueue.Count == 0)
        {
            if (currentGoal.remove)
            {
                goals.Remove(currentGoal);
            }
            planner = null;
        }

        if (actionQueue != null && actionQueue.Count > 0)
        {
            currentAction = actionQueue.Dequeue();
            if (currentAction.PrePerform())
            {
                if (currentAction.target != null)
                {
                    currentAction.running = true;
                    currentAction.agent.SetDestination(currentAction.target.transform.position);
                }
            }
            else
            {
                actionQueue = null;
            }
        }
    }
Exemple #7
0
    // private FSM.FSMState
    private void createIdleState()
    {
        idleState = (fsm, obj) => {
            HashSet <KeyValuePair <string, object> > worldState = dataProvider.GetWorldState();
            HashSet <KeyValuePair <string, object> > goal       = dataProvider.CreateGoalState();

            Queue <GOAPAction> plan = planner.plan(gameObject, availableActions, worldState, goal);
            if (plan != null)
            {
                currentActions = plan;
                dataProvider.PlanFound(goal, plan);

                fsm.popState();
                fsm.pushState(performActionState);
            }
            else
            {
                dataProvider.PlanFailed(goal);
                fsm.popState();
                fsm.pushState(idleState);
            }
        };
    }