public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            var nodeMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex) as MyBehaviorTreeControlNodeMemory;
            for (int i = nodeMemory.InitialIndex; i < m_children.Count; i++)
            {
                bot.BotMemory.RememberNode(m_children[i].MemoryIndex);
                var state = m_children[i].Tick(bot, botTreeMemory);
                if (state == SearchedValue || state == FinalValue)
                    m_children[i].PostTick(bot, botTreeMemory);
                if (state == MyBehaviorTreeState.RUNNING || state == SearchedValue)
                {
                    nodeMemory.NodeState = state;
                    if (state == MyBehaviorTreeState.RUNNING)
                    {
                        if (m_isMemorable)
                            nodeMemory.InitialIndex = i;
                    }
                    else
                    {
                        bot.BotMemory.ForgetNode();
                    }
                    return state;
                }
                bot.BotMemory.ForgetNode();
            }

            nodeMemory.NodeState = FinalValue;
            return FinalValue;
        }
예제 #2
0
        public void Init(MyObjectBuilder_BotMemory builder)
        {
            if (builder.BehaviorTreeMemory != null)
            {
                var treeBotMemory = new MyPerTreeBotMemory();
                foreach (var nodeMemoryBuilder in builder.BehaviorTreeMemory.Memory)
                {
                    var nodeMemoryObj = MyBehaviorTreeNodeMemoryFactory.CreateNodeMemory(nodeMemoryBuilder);
                    nodeMemoryObj.Init(nodeMemoryBuilder);
                    treeBotMemory.AddNodeMemory(nodeMemoryObj);
                }

                if (builder.BehaviorTreeMemory.BlackboardMemory != null)
                {
                    foreach (var bbMemInstance in builder.BehaviorTreeMemory.BlackboardMemory)
                    {
                        treeBotMemory.AddBlackboardMemoryInstance(bbMemInstance.MemberName, bbMemInstance.Value);
                    }
                }
            }

            if (builder.OldPath != null)
                for (int i = 0; i < builder.OldPath.Count; i++)
                    m_oldNodePath.Add(i);
            if (builder.NewPath != null)
                for (int i = 0; i < builder.NewPath.Count; i++)
                    m_newNodePath.Push(builder.NewPath[i]);

            LastRunningNodeIndex = builder.LastRunningNodeIndex;
        }
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            var decoratorMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex) as MyBehaviorTreeDecoratorNodeMemory;

            if (m_child == null)
                return m_defaultReturnValue;

            if (decoratorMemory.ChildState != MyBehaviorTreeState.RUNNING)
            {
                m_decoratorLogic.Update(decoratorMemory.DecoratorLogicMemory);
                if (m_decoratorLogic.CanRun(decoratorMemory.DecoratorLogicMemory))
                {
                    return TickChild(bot, botTreeMemory, decoratorMemory);
                }
                else
                {
                    if (IsRunningStateSource)
                        bot.BotMemory.ProcessLastRunningNode(this);

                    botTreeMemory.GetNodeMemoryByIndex(MemoryIndex).NodeState = m_defaultReturnValue;
                    return m_defaultReturnValue;
                }
            }
            else
            {
                return TickChild(bot, botTreeMemory, decoratorMemory);
            }
        }
예제 #4
0
 public void CallPostTickOnPath(IMyBot bot, MyPerTreeBotMemory botTreeMemory, IEnumerable<int> postTickNodes)
 {
     foreach (var nodeIdx in postTickNodes)
     {
         m_treeDesc.Nodes[nodeIdx].PostTick(bot, botTreeMemory);
     }
 }
예제 #5
0
 public MyBotMemory(IMyBot bot)
 {
     LastRunningNodeIndex = -1;
     m_memoryUser = bot;
     m_treeBotMemory = new MyPerTreeBotMemory();
     m_newNodePath = new Stack<int>(20);
     m_oldNodePath = new HashSet<int>();
 }
 public override void PostTick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
 {
     botTreeMemory.GetNodeMemoryByIndex(MemoryIndex).PostTickMemory();
     foreach (var child in m_children)
     {
         child.PostTick(bot, botTreeMemory);
     }
 }
        public override void PostTick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            var nodeMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex);
            if (nodeMemory.InitCalled)
            {
                if (bot.ActionCollection.ContainsPostAction(m_actionName))
                    bot.ActionCollection.PerformPostAction(bot, m_actionName);

                nodeMemory.InitCalled = false;
            }
        }
 private MyBehaviorTreeState TickChild(IMyBot bot, MyPerTreeBotMemory botTreeMemory, MyBehaviorTreeDecoratorNodeMemory thisMemory)
 {
     bot.BotMemory.RememberNode(m_child.MemoryIndex);
     var state = m_child.Tick(bot, botTreeMemory);
     thisMemory.NodeState = state;
     thisMemory.ChildState = state;
     if (state != MyBehaviorTreeState.RUNNING)
     {
         bot.BotMemory.ForgetNode();
     }
     return state;
 }
예제 #9
0
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            bot.BotMemory.RememberNode(m_child.MemoryIndex);

            var state = m_child.Tick(bot, botTreeMemory);
            botTreeMemory.GetNodeMemoryByIndex(MemoryIndex).NodeState = state;

            if (state != MyBehaviorTreeState.RUNNING)
                bot.BotMemory.ForgetNode();

            return state;
        }
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            if (bot.ActionCollection.ReturnsRunning(m_actionName))
                bot.BotMemory.ProcessLastRunningNode(this);

            var nodeMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex);
            if (!nodeMemory.InitCalled)
            {
                nodeMemory.InitCalled = true;
                if (bot.ActionCollection.ContainsInitAction(m_actionName))
                    bot.ActionCollection.PerformInitAction(bot, m_actionName);
            }

            var state = bot.ActionCollection.PerformAction(bot, m_actionName, m_parameters);
            nodeMemory.NodeState = state;
            return state;
        }
예제 #11
0
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            bot.BotMemory.RememberNode(m_child.MemoryIndex);

            if ( Sandbox.Engine.Utils.MyDebugDrawSettings.DEBUG_DRAW_BOTS )
            {
                // store this old memory
                bot.LastBotMemory = bot.BotMemory.Clone();
            }

            var state = m_child.Tick(bot, botTreeMemory);
            botTreeMemory.GetNodeMemoryByIndex(MemoryIndex).NodeState = state;
            RecordRunningNodeName(bot, state);

            if (state != MyBehaviorTreeState.RUNNING)
                bot.BotMemory.ForgetNode();

            return state;
        }
예제 #12
0
        public void Init(MyObjectBuilder_BotMemory builder)
        {
            if (builder.BehaviorTreeMemory != null)
            {
                var treeBotMemory = new MyPerTreeBotMemory();
                foreach (var nodeMemoryBuilder in builder.BehaviorTreeMemory.Memory)
                {
                    var nodeMemoryObj = MyBehaviorTreeNodeMemoryFactory.CreateNodeMemory(nodeMemoryBuilder);
                    nodeMemoryObj.Init(nodeMemoryBuilder);
                    treeBotMemory.AddNodeMemory(nodeMemoryObj);
                }

                if (builder.BehaviorTreeMemory.BlackboardMemory != null)
                {
                    foreach (var bbMemInstance in builder.BehaviorTreeMemory.BlackboardMemory)
                    {
                        treeBotMemory.AddBlackboardMemoryInstance(bbMemInstance.MemberName, bbMemInstance.Value);
                    }
                }
                CurrentTreeBotMemory = treeBotMemory;
            }

            if (builder.OldPath != null)
            {
                for (int i = 0; i < builder.OldPath.Count; i++)
                {
                    m_oldNodePath.Add(i);
                }
            }
            if (builder.NewPath != null)
            {
                for (int i = 0; i < builder.NewPath.Count; i++)
                {
                    m_newNodePath.Push(builder.NewPath[i]);
                }
            }

            LastRunningNodeIndex = builder.LastRunningNodeIndex;
            TickCounter          = 0;
        }
예제 #13
0
        public MyBehaviorTreeState PerformAction(IMyBot bot, MyStringId actionId, object[] args)
        {
            BotActionDesc action = this.m_actions[actionId];

            if (action == null)
            {
                return(MyBehaviorTreeState.ERROR);
            }
            MyPerTreeBotMemory currentTreeBotMemory = bot.BotMemory.CurrentTreeBotMemory;

            if (action.ParametersDesc.Count == 0)
            {
                return(action._Action(bot, args));
            }
            if (args == null)
            {
                return(MyBehaviorTreeState.FAILURE);
            }
            this.LoadActionParams(action, args, currentTreeBotMemory);
            this.SaveActionParams(action, args, currentTreeBotMemory);
            return(action._Action(bot, action.ActionParams));
        }
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            var nodeMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex) as MyBehaviorTreeControlNodeMemory;
            for (int i = nodeMemory.InitialIndex; i < m_children.Count; i++)
            {
                bot.BotMemory.RememberNode(m_children[i].MemoryIndex);
                if (Sandbox.Engine.Utils.MyDebugDrawSettings.DEBUG_DRAW_BOTS)
                {
                    string childName = (m_children[i] is MyBehaviorTreeControlBaseNode) ? ((m_children[i] as MyBehaviorTreeControlBaseNode)).m_name : 
                        (m_children[i] is MyBehaviorTreeActionNode)? (m_children[i] as MyBehaviorTreeActionNode).GetActionName(): 
                        (m_children[i] is MyBehaviorTreeDecoratorNode)? (m_children[i] as MyBehaviorTreeDecoratorNode).GetName():
                        "";                     // just variable for conditional debugging
                    m_runningActionName = "";   // this line is good candidate for breakpoint is you want to debug special part of behavior tree
                }
                var state = m_children[i].Tick(bot, botTreeMemory);
                if (state == SearchedValue || state == FinalValue)
                    m_children[i].PostTick(bot, botTreeMemory);
                if (state == MyBehaviorTreeState.RUNNING || state == SearchedValue)
                {
                    nodeMemory.NodeState = state;
                    if (state == MyBehaviorTreeState.RUNNING)
                    {
                        if (m_isMemorable)
                            nodeMemory.InitialIndex = i;
                    }
                    else
                    {
                        bot.BotMemory.ForgetNode();
                    }
                    RecordRunningNodeName(state, m_children[i]);
                    return state;
                }
                bot.BotMemory.ForgetNode();
            }

            nodeMemory.NodeState = FinalValue;
            nodeMemory.InitialIndex = 0;
            return FinalValue;
        }
        public override MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
        {
            var decoratorMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex) as MyBehaviorTreeDecoratorNodeMemory;

            if (m_child == null)
                return m_defaultReturnValue;

            if (decoratorMemory.ChildState != MyBehaviorTreeState.RUNNING)
            {
                m_decoratorLogic.Update(decoratorMemory.DecoratorLogicMemory);
                if (m_decoratorLogic.CanRun(decoratorMemory.DecoratorLogicMemory))
                {
                    MyBehaviorTreeState state = TickChild(bot, botTreeMemory, decoratorMemory);
                    RecordRunningNodeName(state);
                    return state;
                }
                else
                {
                    if (IsRunningStateSource)
                        bot.BotMemory.ProcessLastRunningNode(this);

                    botTreeMemory.GetNodeMemoryByIndex(MemoryIndex).NodeState = m_defaultReturnValue;
                    if (Sandbox.Engine.Utils.MyDebugDrawSettings.DEBUG_DRAW_BOTS && m_defaultReturnValue == MyBehaviorTreeState.RUNNING)
                    {
                        m_runningActionName = ParentName + m_decoratorLogicName;
                    }
                        
                    return m_defaultReturnValue;
                }
            }
            else
            {
                MyBehaviorTreeState state = TickChild(bot, botTreeMemory, decoratorMemory);
                RecordRunningNodeName(state);
                return state;
                //return TickChild(bot, botTreeMemory, decoratorMemory);
            }
        }
예제 #16
0
        private void LoadActionParams(BotActionDesc action, object[] args, MyPerTreeBotMemory botMemory)
        {
            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                if (arg is Boxed <MyStringId> && action.ParametersDesc.ContainsKey(i))
                {
                    var parameterDesc           = action.ParametersDesc[i];
                    Boxed <MyStringId> stringId = arg as Boxed <MyStringId>;
                    MyBBMemoryValue    value    = null;

                    if (botMemory.TryGetFromBlackboard(stringId, out value))
                    {
                        if (value == null || (value.GetType() == parameterDesc.Item1 && parameterDesc.Item2 != MyMemoryParameterType.OUT))
                        {
                            action.ActionParams[i] = value;
                        }
                        else
                        {
                            if (value.GetType() != parameterDesc.Item1)
                            {
                                Debug.Assert(false, "Mismatch of types in the blackboard. Did you use a wrong identifier?");
                            }

                            action.ActionParams[i] = null;
                        }
                    }
                    else
                    {
                        action.ActionParams[i] = null;
                    }
                }
                else
                {
                    action.ActionParams[i] = arg;
                }
            }
        }
예제 #17
0
 public void Init(MyObjectBuilder_BotMemory builder)
 {
     if (builder.BehaviorTreeMemory != null)
     {
         MyPerTreeBotMemory memory = new MyPerTreeBotMemory();
         foreach (MyObjectBuilder_BehaviorTreeNodeMemory memory2 in builder.BehaviorTreeMemory.Memory)
         {
             MyBehaviorTreeNodeMemory nodeMemory = MyBehaviorTreeNodeMemoryFactory.CreateNodeMemory(memory2);
             nodeMemory.Init(memory2);
             memory.AddNodeMemory(nodeMemory);
         }
         if (builder.BehaviorTreeMemory.BlackboardMemory != null)
         {
             foreach (MyObjectBuilder_BotMemory.BehaviorTreeBlackboardMemory memory4 in builder.BehaviorTreeMemory.BlackboardMemory)
             {
                 memory.AddBlackboardMemoryInstance(memory4.MemberName, memory4.Value);
             }
         }
         this.CurrentTreeBotMemory = memory;
     }
     if (builder.OldPath != null)
     {
         for (int i = 0; i < builder.OldPath.Count; i++)
         {
             this.m_oldNodePath.Add(i);
         }
     }
     if (builder.NewPath != null)
     {
         for (int i = 0; i < builder.NewPath.Count; i++)
         {
             this.m_newNodePath.Push(builder.NewPath[i]);
         }
     }
     this.LastRunningNodeIndex = builder.LastRunningNodeIndex;
     this.TickCounter          = 0;
 }
예제 #18
0
 private void ResetMemoryInternal(MyBehaviorTree behaviorTree, MyPerTreeBotMemory treeMemory)
 {
     for (int i = 0; i < behaviorTree.TotalNodeCount; i++)
     {
         treeMemory.AddNodeMemory(behaviorTree.GetNodeByIndex(i).GetNewMemoryObject());
     }
 }
예제 #19
0
        private void SaveActionParams(BotActionDesc action, object[] args, MyPerTreeBotMemory botMemory)
        {
            foreach (var key in action.ParametersDesc.Keys)
            {
                MyStringId stringId = args[key] as Boxed<MyStringId>;
				var parameterDesc = action.ParametersDesc[key];
				if(parameterDesc.Item2 != MyMemoryParameterType.IN)
				  botMemory.SaveToBlackboard(stringId, action.ActionParams[key] as MyBBMemoryValue);
            }
        }
예제 #20
0
 public virtual void PostTick(IMyBot bot, MyPerTreeBotMemory nodesMemory) { }
예제 #21
0
 public abstract MyBehaviorTreeState Tick(IMyBot bot, MyPerTreeBotMemory nodesMemory);
 public override void PostTick(IMyBot bot, MyPerTreeBotMemory botTreeMemory)
 {
     base.PostTick(bot, botTreeMemory);
     var decoratorMemory = botTreeMemory.GetNodeMemoryByIndex(MemoryIndex) as MyBehaviorTreeDecoratorNodeMemory;
     if (decoratorMemory.ChildState != MyBehaviorTreeState.NOT_TICKED)
     {
         decoratorMemory.PostTickMemory();
         if (m_child != null)
             m_child.PostTick(bot, botTreeMemory);
     }
     else
     {
         if (IsRunningStateSource)
             decoratorMemory.PostTickMemory();
     }
 }
예제 #23
0
        private void LoadActionParams(BotActionDesc action, object[] args, MyPerTreeBotMemory botMemory)
        {
            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                if (arg is Boxed<MyStringId> && action.ParametersDesc.ContainsKey(i))
                {
                    var parameterDesc = action.ParametersDesc[i];
                    Boxed<MyStringId> stringId = arg as Boxed<MyStringId>;
                    MyBBMemoryValue value = null;

					if (botMemory.TryGetFromBlackboard(stringId, out value))
					{
						if (value == null || (value.GetType() == parameterDesc.Item1 && parameterDesc.Item2 != MyMemoryParameterType.OUT))
						{
							action.ActionParams[i] = value;
						}
						else
						{
							if (value.GetType() != parameterDesc.Item1)
								Debug.Assert(false, "Mismatch of types in the blackboard. Did you use a wrong identifier?");

							action.ActionParams[i] = null;
						}
					}
					else
						action.ActionParams[i] = null;
                }
                else
                {
                    action.ActionParams[i] = arg;
                }
            }
        }
예제 #24
0
        public void ResetMemory(MyBehaviorTree behaviorTree, bool clearMemory = false)
        {
            if (clearMemory)
                ClearPathMemory(true);

            if (CurrentBehaviorTree.BehaviorTreeId == behaviorTree.BehaviorTreeId)
                CurrentTreeBotMemory.Clear();
            else
                CurrentTreeBotMemory = new MyPerTreeBotMemory();

            ResetMemoryInternal(behaviorTree, CurrentTreeBotMemory);
        }
예제 #25
0
 private MyPerTreeBotMemory CreateBehaviorTreeMemory(MyBehaviorTree behaviorTree)
 {
     var treeMemory = new MyPerTreeBotMemory(); 
     ResetMemoryInternal(behaviorTree, treeMemory);
     return treeMemory;
 }