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();

            // 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();                 // switch to PerformAction state
                fsm.pushState(performActionState);
            }
            else
            {
                // ugh, we couldn't get a plan
                Debug.Log("Failed Plan: " + goal);
                dataProvider.PlanFailed(goal);
                fsm.popState();                  // switch back to IdleAction state
                fsm.pushState(idleState);
            }
        };
    }
Exemple #2
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();
                Dictionary <Int64, Int64>   resourceState = _dataProvider.GetResourceState();
                Dictionary <string, object> goal          = _dataProvider.CreateGoalState();
                Dictionary <Int64, Int64>   resourceGoal  = _dataProvider.CreateResourceGoal();
                loadActions();

                // Plan
                Queue <GoapAction> plan = _planner.Plan(this, _availableActions, worldState, resourceState, goal, resourceGoal);
                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
                    // Console.WriteLine("<color=orange>Failed Plan:</color>" + PrettyPrint(goal));
                    _dataProvider.PlanFailed(goal);
                    fsm.popState(); // move back to IdleAction state
                    fsm.pushState(_idleState);
                }
            };
        }
    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
    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();
            }
        };
    }
Exemple #5
0
    private void createIdleState()
    {
        //this is a lambda expression nameless/anonymous delegate thingy of type FSM.FSMState
        idleState = (fsm, gameObj) =>
        {
            //HashSet<KeyValuePair<string, object>> worldState = dataProvider.getWorldState();
            //HashSet<KeyValuePair<string, object>> goal = dataProvider.createGoalState();
            Profiler.BeginSample("Gathering and ordering goals by priority  ");
            List <Condition> originalState = dataProvider.GetWorldState();

            List <Goal> goals = dataProvider.GetGoalState();
            goals = OrderByPriority(goals);

            //Debug.Log("WORLD STATE " + prettyPrint(originalState));
            //Debug.Log("GOAL STATE " + prettyPrint(goals));

            Profiler.EndSample();

            Profiler.BeginSample("Begin Planning -- for loop through goals and calling planner");
            Queue <GoapAction> plan = null;
            foreach (Goal ourGoal in goals)
            { //this cycles through the goals by priority until it finds one that works with a plan
                plan = planner_.Plan(ourGoal, availableActions_, originalState, this.gameObject);
                if (plan != null)
                {
                    //if you find a plan that works, break and continue on
                    //Debug.Log("<color=cyan>Plan found!:</color>" + prettyPrint(plan) + " for " + gameObj.name);
                    break;
                }
                else
                {
                }
            }
            Profiler.EndSample();


            //  planner_.Plan(goals, availableActions_, originalState, this.gameObject);
            Profiler.BeginSample("Checking to see if plan is null/failed and returning to idle if so");
            if (plan != null)
            {
                currentActions = plan;
                dataProvider.PlanFound(goals, 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(goals) +  " for " + gameObj.name);
                dataProvider.PlanFailed(goals);
                fsm.popState(); // move back to IdleAction state
                fsm.pushState(idleState);
            }



            Profiler.EndSample();
        };
    }
Exemple #6
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 #7
0
    private void CreatePlanningState()
    {
        planningState = (fsm, gameObj) =>
        {
            Dictionary <string, object> worldState = worldDataProvider.GetWorldState();
            Dictionary <string, object> goalState  = worldDataProvider.CreateGoalState();

            Queue <GAction> plan = planner.Plan(this, availableActions, worldState, goalState);
            if (plan != null)
            {
                currentActions = plan;
                worldDataProvider.PlanFound(goalState, plan);

                fsm.PopState();
                fsm.PushState(performActionState);
            }
            else
            {
                Debug.Log("Planning failed! Goal: " + goalState);
                worldDataProvider.PlanFailed(goalState);
                fsm.PopState();
                fsm.PushState(planningState);
            }
        };
    }
Exemple #8
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 #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
 public IdleState(IGoap _goapData, StateMachine _controller, List <GoapAction> _availableActions, GoapPlanner _planner, GoapAgent _agent)
 {
     goapData         = _goapData;
     worldState       = goapData.GetWorldState();
     goal             = goapData.GetGoalState();
     controller       = _controller;
     planner          = _planner;
     agent            = _agent;
     availableActions = _availableActions;
 }
        public override void Update()
        {
            IGoap c = (IGoap)fsm.owner.controller;
            Queue <GOAP_action> plan = fsm.goaplanner.MakePlan(c.GetGoalsState(), c.GetWorldState(), c.GetActions());

            if (plan.Count > 0)
            {
                fsm.currentPlan = plan;
                fsm.PushState(new PerformActionState(fsm));
            }
        }
Exemple #12
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 #13
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 #14
0
        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);
                }
            };
        }
Exemple #15
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);
        }
    }
Exemple #16
0
        private void CreateIdleState()
        {
            _idleState = (fsm, gameObject) =>
            {
                Debug.Log("Making plan --");


                //  GOAP Planning -- What're you gonna do?!

                //  get world state and 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)
                {
                    Debug.Log("-- plan made");

                    //  plan created, success
                    _currentActions = plan;
                    _dataProvider.PlanFound(goal, plan);

                    _fsm.PopState();
                    _fsm.PushState(_performState);
                }
                else
                {
                    Debug.Log("-- plan unable to be made");
                    //  plan could not be created
                    _dataProvider.PlanFailed(goal);
                    _fsm.PopState();
                    _fsm.PushState(_idleState);
                }
            };
        }