private void GetBotActions(IMyBot bot, ActionCollection actions)
        {
            var methodInfos = bot.BotActions.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (var methodInfo in methodInfos)
            {
                var attrs = methodInfo.GetCustomAttributes(true);
                for (int i = 0; i < attrs.Length; i++)
                {
                    if (attrs[i] is MyBehaviorTreeActionAttribute)
                    {
                        MyBehaviorTreeActionAttribute btActionAttribute = (MyBehaviorTreeActionAttribute)attrs[i];

                        switch (btActionAttribute.ActionType)
                        {
                        case MyBehaviorTreeActionType.INIT:
                            actions.AddInitAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null));
                            break;

                        case MyBehaviorTreeActionType.BODY:
                            actions.AddAction(btActionAttribute.ActionName, methodInfo, btActionAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y));
                            break;

                        case MyBehaviorTreeActionType.POST:
                            actions.AddPostAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null));
                            break;
                        }

                        break;
                    }
                }
            }
        }
Beispiel #2
0
        public static ActionCollection CreateActionCollection(IMyBot bot)
        {
            ActionCollection actions = new ActionCollection();

            foreach (MethodInfo info in bot.BotActions.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                ExtractAction(actions, info);
            }
            return(actions);
        }
        public static ActionCollection CreateActionCollection(IMyBot bot)
        {
            var actions     = new ActionCollection();
            var methodInfos = bot.BotActions.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (var methodInfo in methodInfos)
            {
                ExtractAction(actions, methodInfo);
            }
            return(actions);
        }
        public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition)
        {
            m_player           = player;
            m_navigation       = new MyBotNavigation();
            m_actionCollection = null;
            m_botMemory        = new MyBotMemory(this);
            m_botDefinition    = botDefinition as MyAgentDefinition;

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;
            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);
        }
Beispiel #5
0
        public MySandboxBot(MyPlayer botPlayer, MyBotDefinition botDefinition)
        {
            m_definition = botDefinition as MyAgentDefinition;

            m_player             = botPlayer;
            m_navigation         = new MyBotNavigation();
            m_respawnRequestSent = false;
            m_actionCollection   = null;
            m_botMemory          = new MyBotMemory(this);

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;

            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);
        }
        public void AddBot(int botHandler, IMyBot newBot)
        {
            Debug.Assert(!m_allBots.ContainsKey(botHandler), "Bot with the given handler already exists!");
            if (m_allBots.ContainsKey(botHandler))
            {
                return;
            }

            ActionCollection botActions = null;

            if (!m_botActions.ContainsKey(newBot.BotActions.GetType()))
            {
                botActions = new ActionCollection();
                GetBotActions(newBot, botActions);
                m_botActions[newBot.GetType()] = botActions;
            }
            else
            {
                botActions = m_botActions[newBot.GetType()];
            }

            newBot.InitActions(botActions);
            if (string.IsNullOrEmpty(newBot.BehaviorSubtypeName))
            {
                m_behaviorTreeCollection.AssignBotToBehaviorTree(newBot.BotDefinition.BotBehaviorTree.SubtypeName, newBot);
            }
            else
            {
                m_behaviorTreeCollection.AssignBotToBehaviorTree(newBot.BehaviorSubtypeName, newBot);
            }
            m_allBots.Add(botHandler, newBot);
            m_botsQueue.Add(botHandler);

            if (m_botsCountPerBehavior.ContainsKey(newBot.BotDefinition.BehaviorType))
            {
                m_botsCountPerBehavior[newBot.BotDefinition.BehaviorType]++;
            }
            else
            {
                m_botsCountPerBehavior[newBot.BotDefinition.BehaviorType] = 1;
            }

#if DEBUG
            if (Sandbox.Game.Gui.MyMichalDebugInputComponent.Static != null && Sandbox.Game.Gui.MyMichalDebugInputComponent.Static.OnSelectDebugBot)
            {
                Sandbox.Engine.Utils.MyDebugDrawSettings.DEBUG_DRAW_BOTS = true;
                SelectBotForDebugging(newBot);
            }
#endif
        }
Beispiel #7
0
        public MySandboxBot(MyPlayer botPlayer, MyBotDefinition botDefinition)
        {
            m_definition = botDefinition as MyAgentDefinition;

            m_player = botPlayer;
            m_navigation = new MyBotNavigation();
            m_respawnRequestSent = false;
            m_actionCollection = null;
            m_botMemory = new MyBotMemory(this);

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;

            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);
        }
        public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition)
        {
            m_player           = player;
            m_navigation       = new MyBotNavigation();
            m_actionCollection = null;
            m_botMemory        = new MyBotMemory(this);
            m_botDefinition    = botDefinition as MyAgentDefinition;

            m_removeAfterDeath   = m_botDefinition.RemoveAfterDeath;
            m_respawnRequestSent = false;
            m_botRemoved         = false;

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;
            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);

            Sandbox.Game.Gui.MyCestmirDebugInputComponent.PlacedAction += DebugGoto;
        }
Beispiel #9
0
        private static void ExtractAction(ActionCollection actions, MethodInfo methodInfo)
        {
            MyBehaviorTreeActionAttribute customAttribute = methodInfo.GetCustomAttribute <MyBehaviorTreeActionAttribute>();

            if (customAttribute != null)
            {
                switch (customAttribute.ActionType)
                {
                case MyBehaviorTreeActionType.INIT:
                    actions.AddInitAction(customAttribute.ActionName, x => methodInfo.Invoke(x.BotActions, null));
                    return;

                case MyBehaviorTreeActionType.BODY:
                    actions.AddAction(customAttribute.ActionName, methodInfo, customAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y));
                    return;

                case MyBehaviorTreeActionType.POST:
                    actions.AddPostAction(customAttribute.ActionName, x => methodInfo.Invoke(x.BotActions, null));
                    return;
                }
            }
        }
Beispiel #10
0
 public void AddBot(int botHandler, IMyBot newBot)
 {
     if (!this.m_allBots.ContainsKey(botHandler))
     {
         ActionCollection actionCollection = null;
         if (this.m_botActions.ContainsKey(newBot.BotActions.GetType()))
         {
             actionCollection = this.m_botActions[newBot.GetType()];
         }
         else
         {
             actionCollection = ActionCollection.CreateActionCollection(newBot);
             this.m_botActions[newBot.GetType()] = actionCollection;
         }
         newBot.InitActions(actionCollection);
         if (string.IsNullOrEmpty(newBot.BehaviorSubtypeName))
         {
             this.m_behaviorTreeCollection.AssignBotToBehaviorTree(newBot.BotDefinition.BotBehaviorTree.SubtypeName, newBot);
         }
         else
         {
             this.m_behaviorTreeCollection.AssignBotToBehaviorTree(newBot.BehaviorSubtypeName, newBot);
         }
         this.m_allBots.Add(botHandler, newBot);
         this.m_botsQueue.Add(botHandler);
         if (!this.m_botsCountPerBehavior.ContainsKey(newBot.BotDefinition.BehaviorType))
         {
             this.m_botsCountPerBehavior[newBot.BotDefinition.BehaviorType] = 1;
         }
         else
         {
             string behaviorType = newBot.BotDefinition.BehaviorType;
             this.m_botsCountPerBehavior[behaviorType] += 1;
         }
     }
 }
Beispiel #11
0
 public void InitActions(ActionCollection actionCollection)
 {
     m_actionCollection = actionCollection;
 }
 public virtual void InitActions(ActionCollection actionCollection)
 {
     m_actionCollection = actionCollection;
 }
        public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition)
        {
            m_player = player;
            m_navigation = new MyBotNavigation(); 
            m_actionCollection = null;
            m_botMemory = new MyBotMemory(this);
            m_botDefinition = botDefinition as MyAgentDefinition;

            m_removeAfterDeath = m_botDefinition.RemoveAfterDeath;
            m_respawnRequestSent = false;
            m_botRemoved = false;

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;
            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);

            Sandbox.Game.Gui.MyCestmirDebugInputComponent.PlacedAction += DebugGoto;
        }
Beispiel #14
0
        public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition)
        {
            m_player = player;
            m_navigation = new MyBotNavigation(); 
            m_actionCollection = null;
            m_botMemory = new MyBotMemory(this);
            m_botDefinition = botDefinition as MyAgentDefinition;

            m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged;
            m_navigation.ChangeEntity(m_player.Controller.ControlledEntity);
        }
        //private static ActionCollection CreateStaticActionCollection()
        //{
        //    var actions = new ActionCollection();
        //    var types = MyAIActionsParser.GetAllTypesFromAssemblies();

        //    foreach (var type in types)
        //    {
        //        var attr = type.GetCustomAttribute<MyBehaviorDescriptorAttribute>();
        //        if (!string.IsNullOrEmpty(attr.DescriptorCategory))
        //            continue;
        //        var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
        //        foreach (var method in methods)
        //        {
        //            ExtractAction(actions, method);
        //        }
        //    }

        //    return actions;
        //}

        private static void ExtractAction(ActionCollection actions, MethodInfo methodInfo)
        {
            var btActionAttribute = methodInfo.GetCustomAttribute<MyBehaviorTreeActionAttribute>();
            if (btActionAttribute == null)
                return;
            switch (btActionAttribute.ActionType)
            {
                case MyBehaviorTreeActionType.INIT:
                    actions.AddInitAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null));
                    break;
                case MyBehaviorTreeActionType.BODY:
                    actions.AddAction(btActionAttribute.ActionName, methodInfo, btActionAttribute.ReturnsRunning, (x, y) => (MyBehaviorTreeState)methodInfo.Invoke(x.BotActions, y));
                    break;
                case MyBehaviorTreeActionType.POST:
                    actions.AddPostAction(btActionAttribute.ActionName, (x) => methodInfo.Invoke(x.BotActions, null));
                    break;
            }
        }
 public static ActionCollection CreateActionCollection(IMyBot bot)
 {
     var actions = new ActionCollection();
     var methodInfos = bot.BotActions.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
     foreach (var methodInfo in methodInfos)
     {
         ExtractAction(actions, methodInfo);
     }
     return actions;
 }