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