private void removeAchievedGoal() { if (actionQueue != null && actionQueue.Count == 0) { if (currentGoal.shouldRemove) { mainGoals.Remove(currentGoal); } planner = null; } }
// Use this for initialization void Awake() { GOAP_Planner.Init(); if (instance == null) { instance = this; } else { Destroy(this); } }
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 makePlansforGoals() { if (hasNoPlan()) { planner = new GOAP_Planner(); // Order all agent's desired goals and create a plan for each to see which are achievable. IOrderedEnumerable <KeyValuePair <GOAP_Goal, int> > orderedGoals = from goal in mainGoals orderby goal.Value descending select goal; foreach (KeyValuePair <GOAP_Goal, int> orderedGoal in orderedGoals) { actionQueue = planner.createPlan(possibleActions, orderedGoal.Key.goals, null); if (actionQueue != null) { // We have a plan! currentGoal = orderedGoal.Key; break; } } } }
/// <summary> /// This action chooses a goal from among the personal goals after checking if there are completed quests to finish first /// </summary> public void ChooseGoal() { activeGoal.Clear(); string msg = "<color=#0000cc><b>CHECKING GOALS</b>:" + character.characterData.characterName + "</color>\n"; //Check goals top to bottom, to see which need to be fulfilled for (int i = 0; i < character.characterData.goals.Count; i++) { GOAP_Worldstate goal = character.characterData.goals[i]; //Check if the goal is already satisfied if (!GOAP_Planner.IsGoalSatisfied(currentWorldstates, goal)) { //And if it has been checked before if (!checkedCharacterGoals.ContainsExactly(goal)) { checkedCharacterGoals.Add(character.characterData.goals[i]); activeGoal.Add(goal); msg += character.characterData.goals[i].key + ":" + character.characterData.goals[i].value + " not yet satisfied\n"; break; } else { msg += character.characterData.goals[i].key + ":" + character.characterData.goals[i].value + " not yet satisfied, but was already checked\n"; } } else { if (checkedCharacterGoals.ContainsExactly(character.characterData.goals[i])) { checkedCharacterGoals.Remove(character.characterData.goals[i]); } msg += character.characterData.goals[i].key + ":" + character.characterData.goals[i].value + " already satisfied\n"; } } Character.Log(msg); timeSincePlanned = 0.0f; }
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; } } } }