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

            Dictionary <string, object> worldState = agent.WorldState();
            Dictionary <string, object> goal       = agent.CreateGoalState();

            Queue <GoapAction> plan = GoapPlanner.Plan(gameObject, availableActions, worldState, goal);

            if (plan != null)
            {
                currentPlan = plan;
                agent.PlanFound(goal, plan);

                fsm.PopState();
                fsm.PushState(performActionState);
            }
            else
            {
                Debug.Log("<color=orange>Failed Plan:</color>" + goal);
                agent.PlanFailed(goal);
                fsm.PopState();
                fsm.PushState(idleState);
            }
        };
    }
Exemple #2
0
    private void IdleState()
    {
        idle = (fsm, gameObj) => {
            List <KeyValuePair <string, object> > worldState = goapAgent.GetWorldState();

            KeyValuePair <string, object> goal = goapAgent.GetSubGoals();
            planner.Reset();
            if (!goal.Equals(new KeyValuePair <string, object>()))
            {
                Queue <GoapAction> plan = planner.Plan(availableActions, worldState, goal);

                if (plan != null)
                {
                    // we have a plan, hooray!
                    currentActions = plan;
                    goapAgent.PlanFound(goal, plan);

                    fsm.popState();
                    fsm.pushState(act);
                }
                else
                {
                    goapAgent.PlanFailed(goal);
                    fsm.popState();
                    fsm.pushState(idle);
                }
            }
            else
            {
                goapAgent.GameFinished();
            }
        };
    }
    void CreateIdleState()
    {
        _idleState = (fsm, gameObj) =>
        {
            // GOAP planning

            // get the world state and the goal we want to plan for
            var worldState = _dataProvider.GetWorldState();
            var goal       = _dataProvider.CreateGoalState();

            // Plan
            Queue <GoapAction> 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 #4
0
    void CreateIdleState()
    {
        idleState = (fsm, gameObj) =>
        {
            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
            {
                Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal));
                dataProvider.PlanFailed(goal);
                fsm.PopState();
                fsm.PushState(idleState);
            }
        };
    }
Exemple #5
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();

            // Plan
            Queue <GoapAction> 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
    private void CreateIdleState()
    {
        idleState = (fsm, gameObj) =>
        {
            // GOAP planning

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

            // search enable Plan
            Queue <GoapAction> plan = planner.Plan(gameObject, availableActions, worldState, goal, dataProvider);

            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("[" + this.name + "] " + "<color=orange>Plan failed:</color> " + PrettyPrint(goal) + " = NO PLAN");
                dataProvider.PlanFailed(goal);
                fsm.PopState();  // move back to IdleAction state
                fsm.PushState(idleState);
            }
        };
    }
Exemple #7
0
    private void CreateIdleState()
    {
        m_IdleState = (fsm) => {
            // GOAP planning

            // get the world state and the goal we want to plan for
            Dictionary <string, object> worldState = m_DataProvider.GetWorldState();
            Dictionary <string, object> goal       = m_DataProvider.CreateGoalState();

            // Plan
            Queue <GoapAction> plan = planner.Plan(m_AvailableActions, worldState, goal);

            if (plan != null)
            {
                // we have a plan, hooray!
                m_CurrentActions = plan;
                m_DataProvider.PlanFound(goal, plan);


                fsm.PopState();                  // move to PerformAction state
                fsm.PushState(m_PerformActionState);
            }
            else
            {
                // ugh, we couldn't get a plan
                Debug.Log("<color=orange>Failed Plan:</color>" + PrettyPrint(goal));
                m_DataProvider.PlanFailed(goal);
                fsm.PopState();                  // move back to IdleAction state
                fsm.PushState(m_IdleState);
            }
        };
    }
Exemple #8
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 = enemyTypeDataProvider.GetWorldState();
            HashSet <KeyValuePair <string, object> > goal       = enemyTypeDataProvider.CreateGoalState();

            // Plan
            Stack <GoapAction> plan = planner.Plan(worldState, goal);
            if (plan != null)
            {
                // we have a plan, hooray!
                currentActions = plan;
                enemyTypeDataProvider.PlanFound(goal, plan);

                fsm.PopState(); // switch to PerformAction state
                fsm.PushState(performActionState);
            }
            else
            {
                // ugh, we couldn't get a plan
                Debug.Log("Failed Plan: " + goal);
                enemyTypeDataProvider.PlanFailed(goal);
                fsm.PopState(); // switch back to IdleAction state
                fsm.PushState(idleState);
            }
        };
    }
Exemple #9
0
    //bool isPlanning = false;
    //HashSet<KeyValuePair<string, object>> worldState;
    //HashSet<KeyValuePair<string, object>> goal;

    void CreateIdleState()
    {
        idleState = (fsm, gameObj) =>
        {
            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
            {
                Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal));
                dataProvider.PlanFailed(goal);
                fsm.PopState();
                fsm.PushState(idleState);
            }

            //if (!isPlanning)
            //{
            //    worldState = dataProvider.GetWorldState();
            //    goal = dataProvider.CreateGoalState();

            //    planner.donePlanning = false;
            //    planner.finalPlan = null;
            //    StartCoroutine(planner.Plan(gameObject, availableActions, worldState, goal, this));
            //    isPlanning = true;
            //}
            //else
            //{
            //    if (planner.donePlanning)
            //    {
            //        if (planner.finalPlan != null)
            //        {
            //            currentActions = planner.finalPlan;
            //            dataProvider.PlanFound(goal, planner.finalPlan);

            //            fsm.PopState();
            //            fsm.PushState(performActionState);
            //        }
            //        else
            //        {
            //            Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal));
            //            dataProvider.PlanFailed(goal);
            //            fsm.PopState();
            //            fsm.PushState(idleState);
            //        }
            //        isPlanning = false;
            //    }
            //}
        };
    }
Exemple #10
0
    /// <summary>
    /// 创建空闲状态
    /// </summary>
    private void CreateIdleState()
    {
        idleState = (fsm, gameObj) =>
        {
            // GOAP planning
            // GOAP计划

            // get the world state and the goal we want to plan for
            //获得世界状态和我们想要计划的目标
            var worldState = dataProvider.GetWorldState();
            //创建目标状态
            var goals = dataProvider.CreateGoalState();

            // search enable Plan
            //搜索启用计划
            Queue <GoapAction>          plan     = null;
            KeyValuePair <string, bool> lastGoal = new KeyValuePair <string, bool>();
            foreach (var goal in goals)
            {
                lastGoal = goal;
                plan     = planner.Plan(gameObject, availableActions, worldState, goal, dataProvider);
                if (plan != null)
                {
                    break;
                }
            }
            if (plan != null)
            {
                // we have a plan, hooray!
                //我们有个计划,万岁!
                currentActions = plan;
                dataProvider.PlanFound(lastGoal, plan);
                //移动到“执行操作”状态
                fsm.popState(); // move to PerformAction state
                fsm.pushState(performActionState);
            }
            else
            {
                // ugh, we couldn't get a plan
                //呃,我们无法得到一个计划
                Debug.Log("<color=orange>失败的计划:</color>" + PrettyPrint(goals));
                dataProvider.PlanFailed(goals);
                //返回空闲操作状态
                fsm.popState(); // move back to IdleAction state
                fsm.pushState(idleState);
            }
        };
    }
Exemple #11
0
        /// <summary>
        /// Implementation of <see cref="IAction"/> interface. Calls the Plan method of the <see cref="GoapPlanner"/>.
        /// </summary>
        public void Execute()
        {
            // get the world state and the goal we want to plan for
            var worldState = _dataProvider.GetWorldState();
            var goal       = _dataProvider.CreateGoalState();

            // Plan
            var plan = _planner.Plan(_agent.gameObject, _agent.GetAvailableActions(), worldState, goal);

            if (plan != null)// we have a plan, hooray!
            {
                _agent.SetCurrentActions(plan);
                _dataProvider.PlanFound(goal, plan);
            }
            else// ugh, we couldn't get a plan
            {
                _dataProvider.PlanFailed(goal);
            }
        }
Exemple #12
0
    public void Plan()
    {
        Goal selectedGoal      = GoalPool.GoalsFor(NpcType.MERCENARY).OrderBy(goal => goal.priority).ToList().FirstOrDefault();
        List <GoapAction> plan = planner.Plan(ActionPool.ActionsFor, this.blackboard.worldState, selectedGoal.goalState).Where(action => action != null).ToList();

        StringBuilder sb = new StringBuilder("Planning finished. Path length: " + plan.Count + ", Path is: \n");

        foreach (GoapAction action in plan)
        {
            sb.Append(action.ToString() + "\n");
        }
        sb.Append("\n");

        Debug.Log(sb.ToString());

        plan.Reverse();

        this.planExecutor.AddNewPlan(new Stack <GoapAction>(plan));
    }
Exemple #13
0
 public void Update()
 {
     foreach (var sensor in sensors)
     {
         sensor.Run(this);
     }
     if (!busy && !planning)
     {
         planning    = true;
         plannerTask = new Task(() => planner.Plan(this));
         plannerTask.Start();
     }
     else if (!planning)
     {
         ExecutePlan();
     }
     else
     {
         Debug.Log("planning");
         currentAction = "Planning...";
     }
 }
Exemple #14
0
    // GOAP Planning State
    public void IdleState()
    {
        State = AgentState.Planning;

        // Get the world state and the goal we want to plan for
        Dictionary <string, bool> worldState = agentImplementation.GetWorldState();
        Dictionary <string, bool> goal       = agentImplementation.GetGoalState(goalIndex);


        // Clear previous plan ( actions )
        currentPlan.Clear();

        // Find new plan
        currentPlan = planner.Plan(gameObject, availableActions, worldState, goal, goalIndex == 0 ? "Eliminate" : "Find Enemy");


        if (HasActionPlan())
        {
            CurrentPlanTextual = Utilities.GetCollectionString(currentPlan.ToList());

            goalIndex = 0;

            // Plan Available - Change State
            ChangeState(FSMKeys.PERFORM_STATE);
        }
        else
        {
            Debug.LogError("IdleState: Failed Plan | Goal Index:: " + goalIndex + "\n");

            // Loop between goal index 0 and maximum number of goals
            goalIndex = (int)Mathf.Repeat(++goalIndex, agentImplementation.GetGoalsCount());

            // Plan Not Available - Loop State
            ChangeState(FSMKeys.IDLE_STATE);
        }
    }