// Update is called once per frame void Update() { if (dead == false) { if (currentAction == null || currentAction.runAction(this)) { if (currentPlan.Count != 0) { currentAction = currentPlan.Dequeue(); } else { currentAction = null; } } if (lookAtPlayer) { transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z)); } if (lastHealedHealth - currentHealth >= 2500 && !worldState.states.Contains("lowHealth")) //Check whether its time to go to heal position { damageTaken = 0; lastHealedHealth = currentHealth; selectedGoal = 2; worldState.states.Clear(); worldState.states.Add("lowHealth"); Plan(); } if (currentHealth <= 0) { if (dead == false) { anim.SetTrigger("Death"); currentAction = null; currentPlan.Clear(); dead = true; } } } }
public void Plan() { currentAction = null; currentPlan.Clear(); Stack <StatesCollection> simWorldState = new Stack <StatesCollection>(); Stack <ActionGoap> simActions = new Stack <ActionGoap>(); Stack <int> simDepths = new Stack <int>(); ActionGoap[] simPlan = new ActionGoap[planDepth]; int minDepth = int.MaxValue; simWorldState.Push(new StatesCollection(worldState)); simDepths.Push(0); simActions.Push(null); while (simWorldState.Count != 0) { StatesCollection cSimState = simWorldState.Pop(); int cDepth = simDepths.Pop(); ActionGoap cSimActions = simActions.Pop(); simPlan[cDepth] = cSimActions; if (cDepth > minDepth) // Bigger than previous plan thus inefficient { continue; } if (cSimState.CompareStates(goals[selectedGoal].desiredStates) == 0 || cDepth >= planDepth) { if (cDepth < minDepth) { minDepth = cDepth; currentPlan.Clear(); for (int i = 0; i < simPlan.Length; i++) { if (simPlan[i] != null) { currentPlan.Enqueue(simPlan[i]); } } } } else { for (int i = 0; i < actions.Count; i++) { if (cSimState.CompareStates(actions[i].requiredStates) == 0 && cSimState.CompareStates(actions[i].outcomeStates) > 0) // has to be possible and has to cause something { StatesCollection newState = new StatesCollection(cSimState); newState.AddStates(actions[i].outcomeStates); simWorldState.Push(newState); simActions.Push(actions[i]); simDepths.Push(cDepth + 1); } } } } }