Ejemplo n.º 1
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the decider will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            // Create blackboard.
            Blackboard blackboard = new Blackboard();

            // Add parent blackboards.
            if (this.Blackboard != null)
            {
                blackboard.Parents.Add(this.Blackboard);
            }

            blackboard.Parents.Add(agentData.Blackboard);

            // Setup blackboard.
            Blackboard previousBlackboard = agentData.Blackboard;
            agentData.Blackboard = blackboard;

            // Deactivate child.
            IDecisionData childDecisionData = null;
            float decisionValue = this.DecideChild(agentData, ref childDecisionData);

            // Tear down.
            agentData.Blackboard = previousBlackboard;

            // Create decision data.
            if (decisionValue > 0.0f)
            {
                decisionData = new DecisionData { Blackboard = blackboard, ChildDecisionData = childDecisionData };
            }

            return decisionValue;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the task will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            if (this.Children.Count == 0)
            {
                return(0.0f);
            }

            // Check if all children want to run.
            float maxChildDecisionValue = 0.0f;

            for (int index = 0; index < this.Children.Count; index++)
            {
                ITask         child              = this.Children[index];
                IDecisionData childDecisionData  = null;
                float         childDecisionValue = child.Decide(agentData, ref childDecisionData);
                maxChildDecisionValue = Math.Max(maxChildDecisionValue, childDecisionValue);

                // If one doesn't want to run, we don't run the whole task.
                if (childDecisionValue <= 0.0f)
                {
                    return(0.0f);
                }

                // Store decision data of first child to pass it to it in Activate.
                if (index == 0)
                {
                    decisionData = childDecisionData;
                }
            }

            return(maxChildDecisionValue);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Per frame update.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <returns> Execution status after this update. </returns>
        public override ExecutionStatus Update(IAgentData agentData)
        {
            Data data = agentData.CurrentTaskData as Data;

            if (data.Status == ExecutionStatus.Running)
            {
                data.Status = this.UpdateChild(agentData);
            }
            else
            {
                // Restart if still possible.
                IDecisionData decisionData  = null;
                float         decisionValue = this.Task.Decide(agentData, ref decisionData);
                if (decisionValue > 0.0f)
                {
                    data.Status = this.ActivateChild(agentData, decisionData);
                    if (data.Status == ExecutionStatus.Running)
                    {
                        data.Status = this.UpdateChild(agentData);
                    }
                }
            }

            return(data.Status == ExecutionStatus.Failed ? ExecutionStatus.Failed : ExecutionStatus.Running);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Per frame update.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <returns> Execution status after this update. </returns>
        public override ExecutionStatus Update(IAgentData agentData)
        {
            Data data = agentData.GetTaskData <Data>();

            if (data.Status == ExecutionStatus.Running)
            {
                // Update.
                data.Status = this.UpdateChild(agentData);
            }
            else
            {
                // Restart if still possible.
                IDecisionData decisionData = null;
                ++agentData.CurrentDeciderLevel;
                float decisionValue = this.Task.Decide(agentData, ref decisionData);
                --agentData.CurrentDeciderLevel;
                if (decisionValue > 0.0f)
                {
                    data.Status = this.ActivateChild(agentData, decisionData);
                    if (data.Status == ExecutionStatus.Running)
                    {
                        data.Status = this.UpdateChild(agentData);
                    }
                }
                else
                {
                    data.Status = ExecutionStatus.Failed;
                }
            }

            return(ExecutionStatus.Running);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            DecisionData taskDecisionData = (DecisionData)decisionData;

            // Create child blackboard.
            Data data = new Data {
                Blackboard = taskDecisionData.Blackboard, PreviousBlackboard = agentData.Blackboard
            };

            agentData.CurrentTaskData = data;

            // Setup blackboard.
            Setup(agentData, data);

            // Activate child.
            ExecutionStatus result = this.ActivateChild(agentData, taskDecisionData.ChildDecisionData);

            if (result != ExecutionStatus.Running)
            {
                // Tear down.
                TearDown(agentData, data);
            }

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the task will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            List <IDecisionData> childrenDecisionData = new List <IDecisionData>();

            // Check if all children want to run.
            float maxChildDecisionValue = 0.0f;

            foreach (ITask child in this.Children)
            {
                IDecisionData childDecisionData  = null;
                float         childDecisionValue = child.Decide(agentData, ref childDecisionData);
                maxChildDecisionValue = Math.Max(maxChildDecisionValue, childDecisionValue);

                // If one doesn't want to run, we don't run the whole task.
                if (childDecisionValue <= 0.0f)
                {
                    return(0.0f);
                }

                childrenDecisionData.Add(childDecisionData);
            }

            decisionData = new DecisionData {
                ChildrenDecisionData = childrenDecisionData
            };

            return(maxChildDecisionValue);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the decider will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            // Create blackboard.
            Blackboard blackboard = new Blackboard();

            // Add parent blackboards.
            if (this.Blackboard != null)
            {
                blackboard.Parents.Add(this.Blackboard);
            }

            blackboard.Parents.Add(agentData.Blackboard);

            // Setup blackboard.
            Blackboard previousBlackboard = agentData.Blackboard;

            agentData.Blackboard = blackboard;

            // Deactivate child.
            IDecisionData childDecisionData = null;
            float         decisionValue     = this.DecideChild(agentData, ref childDecisionData);

            // Tear down.
            agentData.Blackboard = previousBlackboard;

            // Create decision data.
            if (decisionValue > 0.0f)
            {
                decisionData = new DecisionData {
                    Blackboard = blackboard, ChildDecisionData = childDecisionData
                };
            }

            return(decisionValue);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///   Let's the child decide.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the task will be activated. </returns>
        protected float DecideChild(IAgentData agentData, ref IDecisionData decisionData)
        {
            ++agentData.CurrentDeciderLevel;
            float decisionValue = this.Task.Decide(agentData, ref decisionData);

            --agentData.CurrentDeciderLevel;
            return(decisionValue);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///   Activates the child of the decorator.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data. </param>
        /// <returns> Execution status after activation. </returns>
        protected ExecutionStatus ActivateChild(IAgentData agentData, IDecisionData decisionData)
        {
            ++agentData.CurrentDeciderLevel;
            ExecutionStatus result = this.Task.Activate(agentData, decisionData);

            --agentData.CurrentDeciderLevel;
            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///   Activates the passed child. Does no index range checking.
        /// </summary>
        /// <param name="childIdx"> Child index. Has to be in correct range. </param>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data from the decide function. </param>
        /// <returns> Execution status after the activation. </returns>
        protected ExecutionStatus ActivateChild(int childIdx, IAgentData agentData, IDecisionData decisionData)
        {
            ++agentData.CurrentDeciderLevel;
            ExecutionStatus executionStatus = this.children[childIdx].Activate(agentData, decisionData);

            --agentData.CurrentDeciderLevel;
            return(executionStatus);
        }
Ejemplo n.º 11
0
 /// <summary>
 ///   Checks for an interrupting task. Only checks higher prioritized deciders as they are the only ones which can interrupt the running task.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="data"> task data. </param>
 /// <param name="childIdx"> Index of child which will be activated. </param>
 /// <param name="childDecideValue"> Decide value of child which will be activated. </param>
 /// <param name="childDecisionData"> Decision data of child to be used in activate method. </param>
 /// <returns> True if there's a child which wants to be activated, else false. </returns>
 private bool CheckForInterruptingDecider(
     IAgentData agentData,
     Data data,
     ref int childIdx,
     ref float childDecideValue,
     ref IDecisionData childDecisionData)
 {
     return(this.DecideForFirstPossible(
                agentData, 0, data.ActiveChildIdx, ref childIdx, ref childDecideValue, ref childDecisionData));
 }
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Data data = new Data();
            data.Status = this.ActivateChild(agentData, decisionData);
            if (data.Status == ExecutionStatus.Failed)
            {
                return ExecutionStatus.Failed;
            }

            agentData.CurrentTaskData = data;

            return ExecutionStatus.Running;
        }
Ejemplo n.º 13
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Data data = new Data();

            data.Status = this.ActivateChild(agentData, decisionData);
            if (data.Status == ExecutionStatus.Failed)
            {
                return(ExecutionStatus.Failed);
            }

            agentData.CurrentTaskData = data;

            return(ExecutionStatus.Running);
        }
Ejemplo n.º 14
0
 /// <summary>
 ///   Checks for a task which will take over the control when the current active child task failed.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="data"> task data. </param>
 /// <param name="childIdx"> Index of child which will be activated. </param>
 /// <param name="childDecideValue"> Decide value of child which will be activated. </param>
 /// <param name="childDecisionData"> Decision data of child to be used in activate method. </param>
 /// <returns> True if there's a child which wants to be activated, else false. </returns>
 private bool CheckForTakeOverDecider(
     IAgentData agentData,
     Data data,
     ref int childIdx,
     ref float childDecideValue,
     ref IDecisionData childDecisionData)
 {
     return(this.DecideForFirstPossible(
                agentData,
                data.ActiveChildIdx + 1,
                this.Children.Count,
                ref childIdx,
                ref childDecideValue,
                ref childDecisionData));
 }
Ejemplo n.º 15
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Data data = new Data { Status = ExecutionStatus.Failed };

            // Check if child wants to run.
            float childDecisionValue = this.DecideChild(agentData, ref decisionData);
            if (childDecisionValue > 0.0f)
            {
                data.Status = this.ActivateChild(agentData, decisionData);
            }

            agentData.CurrentTaskData = data;

            return ExecutionStatus.Running;
        }
Ejemplo n.º 16
0
        /// <summary>
        ///   Computes the relevancy for the decide method by taking the maximum relevancy of all children.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data. </param>
        /// <returns> Maximum relevancy of the children. </returns>
        protected float DelegateDecideToChildren(IAgentData agentData, ref IDecisionData decisionData)
        {
            float maxDecisionValue = 0.0f;

            foreach (ITask decider in this.children)
            {
                IDecisionData childDecisionData = null;
                float         decisionValue     = decider.Decide(agentData, ref childDecisionData);
                if (decisionValue > maxDecisionValue)
                {
                    maxDecisionValue = decisionValue;
                    decisionData     = childDecisionData;
                }
            }

            return(maxDecisionValue);
        }
Ejemplo n.º 17
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Data data = new Data {
                Status = ExecutionStatus.Failed
            };

            // Check if child wants to run.
            float childDecisionValue = this.DecideChild(agentData, ref decisionData);

            if (childDecisionValue > 0.0f)
            {
                data.Status = this.ActivateChild(agentData, decisionData);
            }

            agentData.CurrentTaskData = data;

            return(ExecutionStatus.Running);
        }
Ejemplo n.º 18
0
        /// <summary>
        ///   Per frame data.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        public void Update(IAgentData agentData)
        {
            if (this.Root == null)
            {
                return;
            }

            agentData.CurrentDeciderLevel = 0;

            agentData.PreUpdate();

            switch (agentData.ExecutionStatus)
            {
            case ExecutionStatus.None:
            case ExecutionStatus.Failed:
            case ExecutionStatus.Success:
            {
                IDecisionData decisionData = null;
                if (this.Root.Decide(agentData, ref decisionData) > 0.0f)
                {
                    // Activate decider.
                    agentData.ExecutionStatus = this.Root.Activate(agentData, decisionData);

                    if (agentData.ExecutionStatus == ExecutionStatus.Running)
                    {
                        // Update decider.
                        agentData.ExecutionStatus = this.Root.Update(agentData);
                    }
                }
            }

            break;

            case ExecutionStatus.Running:
            {
                // Update decider.
                agentData.ExecutionStatus = this.Root.Update(agentData);
            }

            break;
            }

            agentData.PostUpdate();
        }
Ejemplo n.º 19
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the task will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            float         decideValue       = 0.0f;
            int           selectedChildIdx  = -1;
            IDecisionData childDecisionData = null;

            if (this.DecideForFirstPossible(agentData, ref selectedChildIdx, ref decideValue, ref childDecisionData))
            {
                // Create decision data to pass to use in Activate() method.
                decisionData = new DecisionData
                {
                    SelectedChildIdx  = selectedChildIdx,
                    ChildDecisionData = childDecisionData
                };

                return(decideValue);
            }

            return(0.0f);
        }
Ejemplo n.º 20
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            DecisionData taskDecisionData = (DecisionData)decisionData;

            // Create child blackboard.
            Data data = new Data { Blackboard = taskDecisionData.Blackboard, PreviousBlackboard = agentData.Blackboard };
            agentData.CurrentTaskData = data;

            // Setup blackboard.
            Setup(agentData, data);

            // Activate child.
            ExecutionStatus result = this.ActivateChild(agentData, taskDecisionData.ChildDecisionData);
            if (result != ExecutionStatus.Running)
            {
                // Tear down.
                TearDown(agentData, data);
            }

            return result;
        }
Ejemplo n.º 21
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after the activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            // Get event data.
            AgentEventData eventData;

            if (!this.agentEventData.TryGetValue(agentData, out eventData))
            {
                return(ExecutionStatus.Failed);
            }

            if (!eventData.EventOccured)
            {
                return(ExecutionStatus.Failed);
            }

            // Execute event task code.
            this.Execute(agentData, eventData.EventData);

            // Reset flag.
            eventData.EventOccured = false;

            return(ExecutionStatus.Success);
        }
Ejemplo n.º 22
0
        /// <summary>
        ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Floating point value used to decide if the decider will be activated. </returns>
        public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
        {
            // Check if event data for agent available.
            AgentEventData eventData;

            if (this.agentEventData.TryGetValue(agentData, out eventData))
            {
                // Check if event occured.
                if (eventData.EventOccured)
                {
                    return(1.0f);
                }
            }
            else
            {
                // Register agent for event.
                this.RegisterAgent(agentData);
                this.agentEventData.Add(agentData, new AgentEventData());
            }

            // Not execute event task.
            return(0.0f);
        }
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Blackboard blackboard = agentData.Blackboard;
            if (blackboard.Parents == null)
            {
                return ExecutionStatus.Failed;
            }

            // Find attribute on parent blackboard.
            foreach (Blackboard parent in blackboard.Parents)
            {
                object attribute = null;
                if (parent.TryGetValue(this.AttributeKey, out attribute))
                {
                    // Set attribute on blackboard.
                    blackboard.SetValue(this.AttributeKey, attribute);

                    return ExecutionStatus.Success;
                }
            }

            return ExecutionStatus.Failed;
        }
Ejemplo n.º 24
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            base.Activate(agentData, decisionData);

            Data data = agentData.GetTaskData <Data>();

            // Activate first child.
            for (data.ActiveChildIdx = 0; data.ActiveChildIdx < this.Children.Count; ++data.ActiveChildIdx)
            {
                ExecutionStatus childResult = this.ActivateChild(data.ActiveChildIdx, agentData, decisionData);
                switch (childResult)
                {
                case ExecutionStatus.None:
                case ExecutionStatus.Failed:
                    return(ExecutionStatus.Failed);

                case ExecutionStatus.Running:
                    return(ExecutionStatus.Running);
                }
            }

            // All children succeeded.
            return(ExecutionStatus.Success);
        }
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            Blackboard blackboard = agentData.Blackboard;

            if (blackboard.Parents == null)
            {
                return(ExecutionStatus.Failed);
            }

            // Find attribute on parent blackboard.
            foreach (Blackboard parent in blackboard.Parents)
            {
                object attribute = null;
                if (parent.TryGetValue(this.AttributeKey, out attribute))
                {
                    // Set attribute on blackboard.
                    blackboard.SetValue(this.AttributeKey, attribute);

                    return(ExecutionStatus.Success);
                }
            }

            return(ExecutionStatus.Failed);
        }
Ejemplo n.º 26
0
        /// <summary>
        ///   Takes first task which wants to be active. Checks only a subset of the children from index 0 to passed lastChildIdx (exclusive).
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="firstChildIdx"> First child index to check (inclusive). </param>
        /// <param name="lastChildIdx"> Last child index to check (exclusive). </param>
        /// <param name="childIdx"> Index of child which will be activated. </param>
        /// <param name="childDecideValue"> Decide value of child which will be activated. </param>
        /// <param name="childDecisionData"> Decision data of child to be used in activate method. </param>
        /// <returns> True if there's a child which wants to be activated, else false. </returns>
        protected bool DecideForFirstPossible(
            IAgentData agentData,
            int firstChildIdx,
            int lastChildIdx,
            ref int childIdx,
            ref float childDecideValue,
            ref IDecisionData childDecisionData)
        {
            for (int idx = firstChildIdx; idx < lastChildIdx; idx++)
            {
                ITask task        = this.Children[idx];
                float decideValue = task.Decide(agentData, ref childDecisionData);
                if (decideValue <= 0.0f)
                {
                    continue;
                }

                childIdx         = idx;
                childDecideValue = decideValue;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 27
0
 /// <summary>
 ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> The SlashGames.AI.BehaviorTrees.Enums.ExecutionStatus. </returns>
 public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
 {
     return this.SubTree.Activate(agentData, decisionData);
 }
Ejemplo n.º 28
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the decider will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     ITask task = this.GetTask(agentData);
     return task == null ? 0.0f : task.Decide(agentData, ref decisionData);
 }
Ejemplo n.º 29
0
 /// <summary>
 ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Execution status after activation. </returns>
 public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
 {
     ITask task = this.GetTask(agentData);
     return task == null ? ExecutionStatus.Failed : task.Activate(agentData, decisionData);
 }
Ejemplo n.º 30
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> The SlashGames.AI.BehaviorTrees.Enums.ExecutionStatus. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            DecisionData selectorDecisionData = decisionData as DecisionData;

            if (selectorDecisionData == null)
            {
                throw new InvalidCastException(string.Format("Decision data was null or not of correct type."));
            }

            Data data = new Data {
                ActiveChildIdx = selectorDecisionData.SelectedChildIdx
            };

            agentData.CurrentTaskData = data;

            // Activate selected child.
            IDecisionData   childDecisionData = selectorDecisionData.ChildDecisionData;
            ExecutionStatus executionStatus   = ExecutionStatus.None;

            while (executionStatus == ExecutionStatus.None)
            {
                // Update child.
                ExecutionStatus childExecutionStatus = this.ActivateChild(
                    data.ActiveChildIdx, agentData, childDecisionData);

                switch (childExecutionStatus)
                {
                case ExecutionStatus.Success:
                {
                    // Invoke callback.
                    this.InvokeOnSuccess();

                    executionStatus = ExecutionStatus.Success;
                }

                break;

                case ExecutionStatus.Failed:
                case ExecutionStatus.None:
                {
                    // Try next child.
                    float decideValue      = 0.0f;
                    int   selectedChildIdx = -1;
                    if (this.CheckForTakeOverDecider(
                            agentData, data, ref selectedChildIdx, ref decideValue, ref childDecisionData))
                    {
                        data.ActiveChildIdx = selectedChildIdx;
                    }
                    else
                    {
                        executionStatus = ExecutionStatus.Failed;
                    }
                }

                break;

                default:
                {
                    executionStatus = childExecutionStatus;
                }

                break;
                }
            }

            return(executionStatus);
        }
Ejemplo n.º 31
0
 /// <summary>
 ///   Takes first task which wants to be active.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="childIdx"> Index of child which will be activated. </param>
 /// <param name="childDecideValue"> Decide value of child which will be activated. </param>
 /// <param name="childDecisionData"> Decision data of child to be used in activate method. </param>
 /// <returns> True if there's a child which wants to be activated, else false. </returns>
 private bool DecideForFirstPossible(
     IAgentData agentData, ref int childIdx, ref float childDecideValue, ref IDecisionData childDecisionData)
 {
     return(this.DecideForFirstPossible(
                agentData, 0, this.Children.Count, ref childIdx, ref childDecideValue, ref childDecisionData));
 }
Ejemplo n.º 32
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the task will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return this.Task != null ? this.DecideChild(agentData, ref decisionData) : 0.0f;
 }
Ejemplo n.º 33
0
 /// <summary>
 ///   Activates the child of the decorator.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data. </param>
 /// <returns> Execution status after activation. </returns>
 protected ExecutionStatus ActivateChild(IAgentData agentData, IDecisionData decisionData)
 {
     ++agentData.CurrentDeciderLevel;
     ExecutionStatus result = this.Task.Activate(agentData, decisionData);
     --agentData.CurrentDeciderLevel;
     return result;
 }
Ejemplo n.º 34
0
        /// <summary>
        ///   Per frame update.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <returns> Execution status after this update. </returns>
        public override ExecutionStatus Update(IAgentData agentData)
        {
            Data data = agentData.CurrentTaskData as Data;

            if (data == null)
            {
                throw new InvalidCastException(string.Format("task data was null or not of correct type."));
            }

            // Check for higher prioritized task which interrupts current executing task.
            float           decideValue          = 0.0f;
            int             selectedChildIdx     = -1;
            IDecisionData   childDecisionData    = null;
            ExecutionStatus childExecutionStatus = ExecutionStatus.Running;

            if (this.CheckForInterruptingDecider(
                    agentData, data, ref selectedChildIdx, ref decideValue, ref childDecisionData))
            {
                // Deactivate current task.
                this.DeactivateChild(data.ActiveChildIdx, agentData);

                data.ActiveChildIdx = selectedChildIdx;

                // Activate new task.
                childExecutionStatus = this.ActivateChild(data.ActiveChildIdx, agentData, childDecisionData);
            }

            // Loop while an execution status was determined.
            ExecutionStatus executionStatus = ExecutionStatus.None;

            while (executionStatus == ExecutionStatus.None)
            {
                // Activate child if necessary.
                if (childExecutionStatus == ExecutionStatus.None)
                {
                    // Activate new task.
                    childExecutionStatus = this.ActivateChild(data.ActiveChildIdx, agentData, childDecisionData);
                }

                if (childExecutionStatus == ExecutionStatus.Running)
                {
                    // Update child.
                    childExecutionStatus = this.UpdateChild(data.ActiveChildIdx, agentData);
                }

                switch (childExecutionStatus)
                {
                case ExecutionStatus.Success:
                {
                    // Invoke callback.
                    this.InvokeOnSuccess();

                    executionStatus = ExecutionStatus.Success;
                }

                break;

                case ExecutionStatus.Failed:
                case ExecutionStatus.None:
                {
                    // Try next child.
                    if (this.CheckForTakeOverDecider(
                            agentData, data, ref selectedChildIdx, ref decideValue, ref childDecisionData))
                    {
                        data.ActiveChildIdx  = selectedChildIdx;
                        childExecutionStatus = ExecutionStatus.None;
                    }
                    else
                    {
                        executionStatus = ExecutionStatus.Failed;
                    }
                }

                break;

                default:
                {
                    executionStatus = childExecutionStatus;
                }

                break;
                }
            }

            return(executionStatus);
        }
Ejemplo n.º 35
0
 /// <summary>
 ///   Let's the child decide.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the task will be activated. </returns>
 protected float DecideChild(IAgentData agentData, ref IDecisionData decisionData)
 {
     ++agentData.CurrentDeciderLevel;
     float decisionValue = this.Task.Decide(agentData, ref decisionData);
     --agentData.CurrentDeciderLevel;
     return decisionValue;
 }
Ejemplo n.º 36
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the task will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     // Inverts the result to run of the decorated task.
     return 1.0f - this.Task.Decide(agentData, ref decisionData);
 }
Ejemplo n.º 37
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the decider will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return this.Decide(agentData) ? 1.0f : 0.0f;
 }
Ejemplo n.º 38
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the task will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     // Loop always runs.
     return 1.0f;
 }
Ejemplo n.º 39
0
 /// <summary>
 ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Execution status after activation. </returns>
 public virtual ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
 {
     return(ExecutionStatus.Running);
 }
Ejemplo n.º 40
0
 /// <summary>
 ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Execution status after activation. </returns>
 public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
 {
     return(this.ActivateChild(agentData, decisionData));
 }
Ejemplo n.º 41
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the task will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the task will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return(this.Task != null?this.DecideChild(agentData, ref decisionData) : 0.0f);
 }
Ejemplo n.º 42
0
        /// <summary>
        ///   Activation. This method is called when the task was chosen to be executed. It's called right before the first update of the task. The task can setup its specific task data in here and do initial actions.
        /// </summary>
        /// <param name="agentData"> Agent data. </param>
        /// <param name="decisionData"> Decision data to use in activate method. </param>
        /// <returns> Execution status after activation. </returns>
        public override ExecutionStatus Activate(IAgentData agentData, IDecisionData decisionData)
        {
            agentData.CurrentTaskData = new TTaskData();

            return(ExecutionStatus.Running);
        }
Ejemplo n.º 43
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the decider will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return this.SubTree != null ? this.SubTree.Decide(agentData, ref decisionData) : 0.0f;
 }
Ejemplo n.º 44
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the decider will be activated. </returns>
 public virtual float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return(1.0f);
 }
Ejemplo n.º 45
0
 /// <summary>
 ///   Depending on the group policy of its parent, the floating point return value indicates whether the decider will be activated.
 /// </summary>
 /// <param name="agentData"> Agent data. </param>
 /// <param name="decisionData"> Decision data to use in activate method. </param>
 /// <returns> Floating point value used to decide if the decider will be activated. </returns>
 public override float Decide(IAgentData agentData, ref IDecisionData decisionData)
 {
     return(this.Decide(agentData) ? 1.0f : 0.0f);
 }