Example #1
0
    public bool IsOverCurAction()
    {
        bool r = false;

        if (aiActions != null)
        {
            IAIAction curAction = GetCurAIAction();
            if (curAction.aiType == EAIAction.MoveBy)
            {
                Vector3 curPos = transform.position;
                if (Mathf.Abs(curPos.x - walkTargetPos.x) <= 0.5f)
                {
                    r = true;
                }
            }
            else if (curAction.aiType == EAIAction.Stop)
            {
                if (Mathf.Abs(Time.time - stopOverTime) <= 0.1f)
                {
                    r = true;
                }
            }

            else if (curAction.aiType == EAIAction.Turn)
            {
                r = true;
            }
        }

        return(r);
    }
 private bool ExecuteAction(IAIAction action, Token token)
 {
     if (action is IAIAction.IConditional conditional)
     {
         return(ExecuteAction(action, conditional, token));
     }
     else if (action.Execute(token))
     {
         if (cancelableAction != null && cancelableAction != action)
         {
             cancelableAction.Cancel(new IAIAction.ICancelable.Token(Source));
             cancelableAction = null;
         }
         if (action is IAIAction.ICancelable cancelAction)
         {
             cancelableAction = cancelAction;
         }
         if (currentAIAction != action && action is IAIAction.IStartable startableAction)
         {
             startableAction.Start(new IAIAction.IStartable.Token(Source));
         }
         currentAIAction = action;
         return(true);
     }
     else if (cancelableAction == action)
     {
         cancelableAction.Cancel(new IAIAction.ICancelable.Token(Source));
         cancelableAction = null;
     }
     return(false);
 }
Example #3
0
 public void StopCurrentAction()
 {
     if (m_currentAction != null)
     {
         m_currentAction.Stop();
     }
     m_currentAction = null;
 }
Example #4
0
 public void StopCurrentAction()
 {
     if (this.m_currentAction != null)
     {
         this.m_currentAction.Stop();
     }
     this.m_currentAction = (IAIAction)null;
 }
Example #5
0
    public void AddAction(IAIAction newAction, int priority)
    {
        ActionChoice pair = new ActionChoice();

        pair.action   = newAction;
        pair.priority = Mathf.Max(priority, 1);

        actions.Add(pair);
    }
 public void CancelAction()
 {
     if (cancelableAction == null)
     {
         return;
     }
     cancelableAction.Cancel(new IAIAction.ICancelable.Token(Source));
     cancelableAction = null;
     currentAIAction  = null;
 }
        public void AddAction(IAIAction <L, V> action)
        {
            var matchedActions = actions.Where(g => g is L).ToArray();

            foreach (var matchedAction in matchedActions)
            {
                RemoveAction(matchedAction);
            }
            action.Init(agent);
            actions.Add(action);
        }
 protected override void OnReset()
 {
     if (_actions != null)
     {
         foreach (var a in _actions)
         {
             a.Reset();
         }
     }
     _currentAction = null;
 }
        protected override ActionResult OnTick(IAIController ai)
        {
            if (_currentAction == null)
            {
                if (_actions == null)
                {
                    return(ActionResult.Success);
                }
                this.Reset();

                if (_weightSupplier != null)
                {
                    _currentAction = _actions.PickRandom((a) =>
                    {
                        return(_weightSupplier.GetWeight(a));
                    });
                }
                else
                {
                    _currentAction = _actions.PickRandom();
                }
                if (_currentAction == null)
                {
                    return(ActionResult.Success);
                }
            }

            if (!_currentAction.Enabled)
            {
                _currentAction = null;
                return(ActionResult.Failed);
            }

            var result = _currentAction.Tick(ai);

            if (result != ActionResult.Waiting)
            {
                if (_weightSupplier != null)
                {
                    switch (result)
                    {
                    case ActionResult.Failed:
                        _weightSupplier.OnActionFailure(_currentAction);
                        break;

                    case ActionResult.Success:
                        _weightSupplier.OnActionSuccess(_currentAction);
                        break;
                    }
                }
                _currentAction = null;
            }
            return(result);
        }
Example #10
0
 public void OnActionFailure(IAIAction action)
 {
     if (_valid)
     {
         int i = System.Array.IndexOf(_actions, action);
         if (i >= 0)
         {
             _weights[i].Signal();
         }
     }
 }
 public void AddAction(IAIAction action)
 {
     if (action is IAIAction.IInitialize initializeAction)
     {
         initializeAction.Initialize(new IAIAction.IInitialize.Token(Source, PoolerRepository));
     }
     if (action is IAIAction.IReinitialize reinitializeAction)
     {
         reinitializeActions.Add(reinitializeAction);
     }
     if (action is IAIAction.IStartable startableAction)
     {
         startableActions.Add(startableAction);
     }
     actions.Add(action);
 }
Example #12
0
    public void DoRandomAction(Action callback)
    {
        if (controlling.isStunned)
        {
            callback.Invoke();
            return;
        }

        List <ActionChoice> candidates = GetPossibleActions();

        candidates.Sort((a, b) => { return(Mathf.Clamp(a.priority - b.priority, -1, 1)); });

        int prioritySum = 0;

        foreach (ActionChoice pair in candidates)
        {
            prioritySum += pair.priority;
        }

        int budget = UnityEngine.Random.Range(0, prioritySum);

        Debug.Log("candidatsize: " + candidates.Count + ", budget: " + budget);

        if (candidates.Count != 0)
        {
            //select from weighted set
            IAIAction action = null;
            int       index  = 0;
            while (budget >= 0 && index < candidates.Count)
            {
                ActionChoice pair = candidates[index];
                action  = pair.action;
                budget -= pair.priority;
                index++;
            }

            Debug.Log("chose " + action?.ActionName);

            action.OnActionFinish += callback;
            action.Do(controlling);
        }
        else
        {
            Debug.LogWarning(controlling.entityName + " had no valid actions this turn");
            callback?.Invoke();
        }
    }
Example #13
0
        private VisualElement GetActionDetails(IAIAction <L, V> action)
        {
            var actionContainer = new VisualElement();

            actionContainer.AddToClassList("action");

            var title = new Button {
                text = action.ToString()
            };

            title.AddToClassList("action-title");
            actionContainer.Add(title);

            title.clicked += () => {
                if (expandedActions.Contains(action))
                {
                    expandedActions.Remove(action);
                }
                else
                {
                    expandedActions.Add(action);
                }
                UpdateContents();
            };

            if (expandedActions.Contains(action))
            {
                var componentsSuccess = action.CheckComponents();
                actionContainer.Add(new Label(componentsSuccess ? "Has Components" : "Components check failed"));

                var actionDetails = new VisualElement();
                actionDetails.AddToClassList("action-details");

                var preconditions = action.GetPreconditions();
                var effects       = action.GetEffects();

                var preconditionsContainer = GetConditionContainer("Preconditions", preconditions);
                actionDetails.Add(preconditionsContainer);

                var effectsContainer = GetConditionContainer("Effects", effects);
                actionDetails.Add(effectsContainer);

                actionContainer.Add(actionDetails);
            }

            return(actionContainer);
        }
Example #14
0
 public float GetWeight(IAIAction action)
 {
     if (_valid)
     {
         int i = System.Array.IndexOf(_actions, action);
         if (i >= 0)
         {
             return(_weights[i].GetAdjustedWeight());
         }
         else
         {
             return(_defaultWeight);
         }
     }
     else
     {
         return(_defaultWeight);
     }
 }
        public bool Execute()
        {
            Token token = new Token(Source);

            foreach (IAIAction action in Actions)
            {
                if (ExecuteAction(action, token))
                {
                    return(true);
                }
            }
            if (cancelableAction != null)
            {
                cancelableAction.Cancel(new IAIAction.ICancelable.Token(Source));
                cancelableAction = null;
            }
            currentAIAction = null;
            return(false);
        }
Example #16
0
        public AINode(IAIAgent <L, V> agent, IAIGoal <L, V> goal, IAIAction <L, V> action = null, AINode <L, V> parent = null)
        {
            this.agent  = agent;
            this.action = action;
            this.parent = parent;

            if (action == null)
            {
                // This is a goal node
                state = new AIState <L, V>(goal, agent.Memory.CloneState());
                state.AddUnmetPreconditions(goal.GetConditions());
                g = 0;
            }
            else
            {
                // This is an action node
                state = new AIState <L, V>();
                g     = float.MaxValue;
            }
        }
Example #17
0
    public void DoCurAction()
    {
        IAIAction curAction = GetCurAIAction();

        if (curAction.aiType == EAIAction.MoveBy)
        {
            AIMoveBy amb = curAction as AIMoveBy;
            walkTargetPos = transform.position + new Vector3(amb.x, 0f, 0f);
            updataState(new IActorAction(EActorAction.NPC_WALK));
        }
        else if (curAction.aiType == EAIAction.Stop)
        {
            AIStop stopAction = curAction as AIStop;
            stopOverTime = Time.time + stopAction.durTime;
            updataState(new IActorAction(EActorAction.NPC_IDLE));
        }
        else if (curAction.aiType == EAIAction.Turn)
        {
            TurnDir();
        }
    }
 public void RemoveAction(IAIAction action)
 {
     if (cancelableAction == action)
     {
         cancelableAction.Cancel(new IAIAction.ICancelable.Token(Source));
         cancelableAction = null;
     }
     if (action is IAIAction.IReinitialize reinitializeAction)
     {
         reinitializeActions.Remove(reinitializeAction);
     }
     if (action is IAIAction.IStartable startableAction)
     {
         startableActions.Remove(startableAction);
     }
     if (action is IAIAction.IRemove removeAction)
     {
         removeAction.Remove();
     }
     actions.Remove(action);
 }
Example #19
0
 /// <summary>Performs a full Brain cycle</summary>
 public void Perform()
 {
     if (this.m_owner.IsUsingSpell || this.m_owner.CastingTill > DateTime.Now)
     {
         return;
     }
     if (this.m_currentAction == null)
     {
         this.m_currentAction = (IAIAction)this.m_actions[this.m_state];
         if (this.m_currentAction == null)
         {
             this.State = this.m_defaultState;
         }
         else
         {
             this.m_currentAction.Start();
         }
     }
     else
     {
         this.m_currentAction.Update();
     }
 }
Example #20
0
        /// <summary>
        /// Performs a full Brain cycle
        /// </summary>
        public void Perform()
        {
            // update current Action if any
            if (m_owner.IsUsingSpell || m_owner.CastingTill > DateTime.Now)
            {
                return;
            }

            if (m_currentAction == null)
            {
                m_currentAction = m_actions[m_state];
                if (m_currentAction == null)
                {
                    // no Action found for current state
                    State = m_defaultState;
                    return;
                }
                m_currentAction.Start();
            }
            else
            {
                m_currentAction.Update();
            }
        }
Example #21
0
        private void NormalizeActionAndWeightArrays()
        {
            var go = GameObjectUtil.GetGameObjectFromSource(this.serializedObject.targetObject);

            if (go == null)
            {
                return;
            }

            var propActions    = this.serializedObject.FindProperty(PROP_ACTIONS);
            var propWeights    = this.serializedObject.FindProperty(PROP_WEIGHTS);
            var currentActions = go.GetComponentsAlt <IAIAction>();

            IAIAction[] serializedActions = new IAIAction[propActions.arraySize];
            for (int i = 0; i < serializedActions.Length; i++)
            {
                serializedActions[i] = propActions.GetArrayElementAtIndex(i).objectReferenceValue as IAIAction;
            }

            if (currentActions.Length == serializedActions.Length && currentActions.Compare(serializedActions))
            {
                if (propWeights.arraySize != serializedActions.Length)
                {
                    propWeights.arraySize = serializedActions.Length;
                }
                return;
            }

            var serializedWeights = (EditorHelper.GetTargetObjectOfProperty(propWeights) as DiminishingWeightOverDuration[]);

            if (serializedWeights == null)
            {
                serializedWeights = new DiminishingWeightOverDuration[] { }
            }
            ;
            else
            {
                serializedWeights = serializedWeights.ToArray();
            }

            propActions.arraySize = currentActions.Length;
            var   weights       = new List <DiminishingWeightOverDuration>();
            float defaultWeight = this.serializedObject.FindProperty(PROP_DEFAULTWEIGHT).floatValue;

            for (int i = 0; i < currentActions.Length; i++)
            {
                propActions.GetArrayElementAtIndex(i).objectReferenceValue = currentActions[i] as Component;

                int j = serializedActions.IndexOf(currentActions[i]);
                if (j >= 0 && j < serializedWeights.Length && serializedWeights[j] != null)
                {
                    weights.Add(serializedWeights[j]);
                }
                else
                {
                    Debug.Log(currentActions[i].GetType().Name + " had no weight");
                    weights.Add(new DiminishingWeightOverDuration(defaultWeight));
                }
            }
            this.serializedObject.ApplyModifiedProperties();
            EditorHelper.SetTargetObjectOfProperty(propWeights, weights.ToArray());
            this.serializedObject.Update();
        }
Example #22
0
        /// <summary>
        /// Performs a full Brain cycle
        /// </summary>
        public void Perform()
        {
            // update current Action if any
            if (m_owner.IsUsingSpell)
            {
                return;
            }

            if (m_currentAction == null)
            {
                m_currentAction = m_actions[m_state];
                if (m_currentAction == null)
                {
                    // no Action found for current state
                    State = m_defaultState;
                    return;
                }
                m_currentAction.Start();
            }
            else
            {
                m_currentAction.Update();
            }
        }
Example #23
0
 public void StopCurrentAction()
 {
     if (m_currentAction != null)
     {
         m_currentAction.Stop();
     }
     m_currentAction = null;
 }
 public void RemoveAction(IAIAction <L, V> action)
 {
     action.OnRemove();
     actions.Remove(action);
 }
 public void AddViableAction(IAIAction <L, V> action)
 {
     goalInfo.ViableActions.Add(action);
 }