public override bool Perform(GOAP_Agent agent, float deltaTime) { StartPerform(agent); alphaWorkTime = 0.5f; //Add a new plan for this quest. agent.activeQuest = agent.CheckForQuests(); if (agent.activeQuest != null) { //if there is a quest, set the agents goal and add a planInfo agent.activeGoal = agent.activeQuest.RequiredStates; agent.planMemory.Add(new PlanInfo(agent.PrintGoal(), agent.Character.characterData.characterName)); Queue <GOAP_Action> actionQueue = GOAP_Planner.Plan(agent, new List_GOAP_Worldstate(agent.activeQuest.RequiredStates), agent.currentWorldstates, agent.Character.characterData.availableActions); agent.ResetPlanningTimer(); if (actionQueue != null && actionQueue.Count > 0) { //Approve the planInfo agent.planMemory[agent.planMemory.Count - 1].ApprovePlan(agent.planMemory.Count - 1, agent.PrintActionQueue()); agent.activePlanInfo = agent.planMemory.Count - 1; //if a valid plan was found, add it to the actionQueue agent.Character.Log("<color=#0000cc>" + agent.Character.characterData.characterName + "</color> found a valid Plan for Quest " + agent.activeQuest.id); agent.UpdateActionQueue(actionQueue); agent.checkedCharacterGoals.Remove(new GOAP_Worldstate(WorldStateKey.bHasCheckedQuestboard, 1)); CompletePerform(agent); return(true); } else { agent.planMemory.RemoveAt(agent.planMemory.Count - 1); agent.Character.Log("<color=#0000cc>" + agent.Character.characterData.characterName + "</color> didn't find a valid Plan for Quest " + agent.activeQuest.id); agent.activeQuest = null; agent.activeGoal = null; return(false); } } else { agent.Character.Log("<color=#0000cc>" + agent.Character.characterData.characterName + "</color> didn't find any Quests."); CompletePerform(agent); agent.checkedCharacterGoals.Remove(new GOAP_Worldstate(WorldStateKey.bHasCheckedQuestboard, 1)); return(true); } }
private void PlanningUpdate(float deltaTime) { View.PrintMessage("Planning"); #region cleanup Useless Quests //First check if any of the posted quests are already done so they can be removed List <int> alreadySolvedQuests = new List <int>(); for (int i = 0; i < postedQuestIDs.Count; i++) { if (IsSatisfiedInCurrentWorldstate(GOAP_QuestBoard.instance.quests[postedQuestIDs[i]].RequiredStates)) { Character.Log("<color=#0000cc>" + character.characterData.characterName + "</color>s Quest " + postedQuestIDs[i] + " was already solved."); alreadySolvedQuests.Add(postedQuestIDs[i]); } } for (int i = 0; i < alreadySolvedQuests.Count; i++) { GOAP_QuestBoard.instance.CompleteQuest(alreadySolvedQuests[i]); } #endregion //Actual planning: if (activeGoal.Count > 0) { Queue <GOAP_Action> newPlan; //Fetch a new Plan from the planner planMemory.Add(new PlanInfo(PrintGoal(), Character.characterData.characterName)); newPlan = GOAP_Planner.Plan(this, activeGoal, currentWorldstates, character.characterData.availableActions); timeSincePlanned = 0.0f; if (newPlan != null) { //do what the plan says! currentActions = newPlan; actionCompleted = true; planMemory[planMemory.Count - 1].ApprovePlan(planMemory.Count - 1, PrintActionQueue()); activePlanInfo = planMemory.Count - 1; if (activePlanInfo == -1) { Debug.LogError(character.characterData.characterName + " ActivePlanIndex: -1"); } ChangeState(FSM_State.PERFORMACTION); return; } else { planMemory.RemoveAt(planMemory.Count - 1); //try again? or something... Debug.Log("No plan?"); if (activeQuest != null) { activeQuest = null; activeGoal.Clear(); ChangeState(FSM_State.IDLE); return; } } } else { //Before checking goals, check if any of your quests have been completed if (completedQuestIDs.Count > 0) { //choose the first one and go int id = completedQuestIDs[0]; if (questPlans.ContainsKey(id)) { currentActions = new Queue <GOAP_Action>(questPlans[id].ToArray()); Character.Log("<color=#0000cc>" + character.characterData.characterName + "</color> has completed Quest " + id + " to finish. It includes " + currentActions.Count + " actions and starts with " + currentActions.Peek()); questPlans.Remove(id); Character.Log("<color=#0000cc>" + character.characterData.characterName + "</color> removes questplan" + id); ChangeState(FSM_State.PERFORMACTION); return; } else { string msg = ""; foreach (int key in questPlans.Keys) { msg += key + ","; } Debug.LogError("<color=#0000cc>" + character.characterData.characterName + "</color> tried to continue Quest " + id + " but didnt find a corresponding plan.\nExisting plans:" + msg); completedQuestIDs.Remove(id); ChangeState(FSM_State.IDLE); return; } } else { if (postedQuestIDs.Count == 0) { checkedCharacterGoals.Clear(); } ChooseGoal(); if (activeGoal.Count == 0) { ChangeState(FSM_State.IDLE); return; } } } }