public void GetPlanner() { // GOAP planning // get the world state and the goal we want to plan for Dictionary <string, bool> worldState = goapAgent.dataProvider.getWorldState(); Dictionary <string, bool> goal = goapAgent.dataProvider.createGoalState(); // Plan Queue <GoapAction> executeActionQueue = goapPlanner.plan(goapAgent.gameObject, availableActions, worldState, goal); if (executeActionQueue != null && executeActionQueue.Count > 0) { // we have a plan, hooray! currentActions = executeActionQueue; goapAgent.stateMachine.clearState(); goapAgent.stateMachine.pushState(FSMStateEnum.Perform); } else { // ugh, we couldn't get a plan Debug.Log("<color=orange>Failed Plan:</color>" + GoapAgent.prettyPrint(goal)); goapAgent.stateMachine.clearState(); goapAgent.stateMachine.pushState(FSMStateEnum.Idle); } }
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); } }; }
// Creating states for the GOAP FSM. private void createIdleState() { idleState = (fsm, gameObj) => { // Get world state and goal from data provider. Dictionary <string, object> worldState = dataProvider.getWorldState(); Dictionary <string, object> goal = dataProvider.createGoalState(); Queue <GoapAction> plan = planner.plan(gameObj, availableActions, worldState, goal); if (plan != null) { // Found a plan. Providing it to the data provider. currentActions = plan; dataProvider.planFound(goal, plan); // Move FSM to performAction state. fsm.popState(); fsm.pushState(performActionState); } else { // No plan found for goal. Debug.Log("<color=orange>Failed plan:</color> " + prettyPrint(goal)); dataProvider.planFailed(goal); // Going back to idleState. fsm.popState(); fsm.pushState(idleState); } }; }
private void createIdleState() { idleState = (fsm, gameObj) => { // Get the world state and the goal Dictionary <string, object> worldState = dataProvider.getWorldState(); Dictionary <string, object> goal = dataProvider.createGoalState(); // Generate the plan using GOAP Planner Queue <GoapAction> plan = planner.plan(gameObject, availableActions, worldState, goal); if (isSelected) { setMetrics(); } if (plan != null) { // Plan found currentActions = plan; if (isSelected) { generatePlanPanel(); } dataProvider.planFound(goal, plan); fsm.popState(); // move to PerformAction state fsm.pushState(performActionState); } else { // No plan Debug.Log("<color=orange>Failed Plan:</color>" + prettyPrint(goal)); dataProvider.planFailed(goal); fsm.popState(); // move back to IdleAction state fsm.pushState(idleState); } }; }
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 // MAYBE ONLY DO THIS ONCE EVERY n SECONDS if (!hasPlan) { planner.plan(gameObject, availableActions, worldState, goal); } if (plan != null && hasPlan) { // need to find the targets for our actions here before trying to do anything foreach (var item in plan) { foreach (var pre in item.Preconditions) { Debug.Log("Precondition: " + pre.Key + " " + pre.Value); } item.checkProceduralPrecondition(gameObject); } // we have a plan, hooray! currentActions = plan; dataProvider.planFound(goal, plan); //doOnce = true; 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); } }; }
private void createIdleState() { idleState = (fsm, aiStateController) => { // GOAP planning // get the world state and the goal we want to plan for UpdateAgentState(); if (debugPlanner) { foreach (var item in _goal) { Debug.Log(item.Key + " " + item.Value); } foreach (var item in _worldState) { Debug.Log(item.Key + " " + item.Value); } } // Plan Queue <GoapAction> plan = planner.plan(aiStateController, availableActions, _worldState, _goal); if (plan != null) { // we have a plan, hooray! currentActions = plan; dataProvider.planFound(_goal, plan); //Debug.Log(plan.Count); 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); } }; }
private void createIdleState() { idleState = (fsm, gameObj) => { // GOAP planning // 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>Failed Plan:</color>" + prettyPrint(goals)); dataProvider.planFailed(goals); fsm.popState(); // move back to IdleAction state fsm.pushState(idleState); } }; }
/// <summary> /// Identifies a plan of action /// </summary> public void IdleState() { if (fsm.fsmStack.Peek() == "Idle") { HashSet <KeyValuePair <string, object> > worldState = gameObject.GetComponent <Player>().currentStateInWorld; HashSet <KeyValuePair <string, object> > goal = gameObject.GetComponent <Player>().goal; //Plan Queue <Action> plan = planner.plan(gameObject, availableActions, worldState, goal); if (plan != null) { currentActions = plan; fsm.popFSM(); fsm.pushFSM("PerformAction"); } else { Debug.Log("plan failed"); fsm.popFSM(); fsm.pushFSM("idle"); } } }
void LateUpdate() { if (currentAction != null && currentAction.running) { float distanceToTarget = Vector3.Distance(currentAction.target.transform.position, this.transform.position); if (currentAction.agent.hasPath && distanceToTarget < 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; foreach (KeyValuePair <SubGoal, int> sg in sortedGoals) { actionQueue = planner.plan(actions, sg.Key.subGoals, beliefs); 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.targetTag != "") { currentAction.target = GameObject.FindWithTag(currentAction.targetTag); } if (currentAction.target != null) { currentAction.running = true; currentAction.agent.SetDestination(currentAction.target.transform.position); } } else { actionQueue = null; } } }