/// <summary> /// Crears all values. /// </summary> private void Clear() { this._parent = null; this._runningCost = 0; this._weight = 0; this._state = null; this._action = null; }
/// <summary> /// Reinitializes node with new values. /// </summary> public void Reinitialize(GOAPNode parent, float runningCost, float weight, Dictionary <string, bool> state, GOAPAction action) { Clear(); this._parent = parent; this._runningCost = runningCost; this._weight = weight; this._state = state; this._action = action; }
/// <summary> /// Creates a subset of the actions excluding the actionToRemove. Creates a new set. /// </summary> /// <param name="actions">Usuable actions</param> /// <param name="actionToRemove">Action to exclude from this branch</param> private HashSet <GOAPAction> ActionSubset(HashSet <GOAPAction> actions, GOAPAction actionToRemove) { HashSet <GOAPAction> subset = GOAPPlannerHelper.GetFreeActionSet(); foreach (GOAPAction action in actions) { if (!action.Equals(actionToRemove)) { subset.Add(action); } } return(subset); }
/// <summary> /// Creates performActionState for the FSM. This state is used to make the AI Agent perform an action. /// </summary> private void CreatePerformActionState() { _performActionState = (fsm, obj) => { if (!HasActionPlan()) { _AIAgent.ActionsFinished(); fsm.ClearState(); fsm.PushState(_idleState); return; } GOAPAction action = _currentActions.Peek(); if (action.IsDone()) { // If action is done, remove it to perform the next one. _currentActions.Dequeue(); } if (HasActionPlan()) { action = _currentActions.Peek(); bool inRange = action.RequiresInRange() ? action.IsInRange() : true; if (inRange) { bool success = action.Perform(obj, _AIAgent.DataHolder); if (!success) { _AIAgent.PlanAborted(action); fsm.ClearState(); fsm.PushState(_idleState); } } else { fsm.PushState(_moveToState); } } else { _AIAgent.ActionsFinished(); fsm.ClearState(); fsm.PushState(_idleState); } }; }
/// <summary> /// Reinitializes free node, turns it into used one and returns it. If there is not enough free nodes, creates a new one. /// </summary> public static GOAPNode GetFreeNode(GOAPNode parent, float runningCost, float weight, Dictionary <string, bool> state, GOAPAction action) { GOAPNode free = null; if (_freeNodes.Count <= 0) { free = new GOAPNode(parent, runningCost, weight, state, action); } else { free = _freeNodes.Pop(); free.Reinitialize(parent, runningCost, weight, state, action); } _usedNodes.Push(free); return(free); }
public bool MoveAgent(GOAPAction nextAction) { NavMeshAgent.SetDestination(nextAction._target.transform.position); // Check if we've reached the destination if (!NavMeshAgent.pathPending) { if (NavMeshAgent.remainingDistance <= NavMeshAgent.stoppingDistance) { if (!NavMeshAgent.hasPath || NavMeshAgent.velocity.sqrMagnitude == 0f) { nextAction.SetInRange(true); return(true); } } } return(false); }
/// <summary> /// Creates moveToState for the FSM. This state is used to move the Ai Agent to its destination. /// </summary> private void CreateMoveToState() { _moveToState = (fsm, gameObject) => { GOAPAction action = _currentActions.Peek(); if (action.RequiresInRange() && action._target == null) { fsm.ClearState(); fsm.PushState(_idleState); return; } if (_AIAgent.MoveAgent(action)) { fsm.PopState(); } }; }
public void PlanAborted(GOAPAction aborter) { // For debugging. }
/// <summary> /// Constructor. /// </summary> public GOAPNode(GOAPNode parent, float runningCost, float weight, Dictionary <string, bool> state, GOAPAction action) { _ID = MaxID++; Reinitialize(parent, runningCost, weight, state, action); }
public void AddAction(GOAPAction action) { _availableActions.Add(action); }
public void RemoveAction(GOAPAction action) { _availableActions.Remove(action); }