Ejemplo n.º 1
0
        public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition)
        {
            MyObjectBuilderType obType = MyObjectBuilderType.Invalid;

            if (botBuilder == null)
            {
                obType     = botDefinition.Id.TypeId;
                botBuilder = m_objectFactory.CreateObjectBuilder <MyObjectBuilder_Bot>(m_objectFactory.GetProducedType(obType));
            }
            else
            {
                obType = botBuilder.TypeId;
                Debug.Assert(botDefinition.Id == botBuilder.BotDefId, "Bot builder type does not match bot definition type!");
            }

            Debug.Assert(m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType), "Undefined behavior type. Bot is not going to be created");
            if (!m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType))
            {
                return(null);
            }
            var    botData = m_botDataByBehaviorType[botDefinition.BehaviorType];
            IMyBot output  = CreateBot(m_objectFactory.GetProducedType(obType), player, botDefinition);

            CreateActions(output, botData.BotActionsType);
            CreateLogic(output, botData.LogicType, botDefinition.BehaviorSubtype);
            output.Init(botBuilder);
            return(output);
        }
Ejemplo n.º 2
0
        public virtual void Init(MyObjectBuilder_Bot botBuilder)
        {
            var ob = botBuilder as MyObjectBuilder_AgentBot;

            if (ob == null)
            {
                return;
            }

            m_deathCountdownMs = ob.RespawnCounter;

            if (AgentDefinition.FactionTag != null)
            {
                var faction = MySession.Static.Factions.TryGetOrCreateFactionByTag(AgentDefinition.FactionTag);
                if (faction != null)
                {
                    MyFactionCollection.SendJoinRequest(faction.FactionId, Player.Identity.IdentityId);
                    m_joinRequestSent = true;
                }
            }

            if (ob.AiTarget != null)
            {
                AgentActions.AiTargetBase.Init(ob.AiTarget);
            }
            if (botBuilder.BotMemory != null)
            {
                m_botMemory.Init(botBuilder.BotMemory);
            }
            MyAIComponent.Static.BehaviorTrees.SetBehaviorName(this, ob.LastBehaviorTree);
        }
Ejemplo n.º 3
0
        public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition)
        {
            MyObjectBuilderType invalid = MyObjectBuilderType.Invalid;

            if (botBuilder != null)
            {
                invalid = botBuilder.TypeId;
            }
            else
            {
                invalid    = botDefinition.Id.TypeId;
                botBuilder = m_objectFactory.CreateObjectBuilder <MyObjectBuilder_Bot>(m_objectFactory.GetProducedType(invalid));
            }
            if (!this.m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType))
            {
                return(null);
            }
            BehaviorData data = this.m_botDataByBehaviorType[botDefinition.BehaviorType];
            IMyBot       bot  = this.CreateBot(m_objectFactory.GetProducedType(invalid), player, botDefinition);

            this.CreateActions(bot, data.BotActionsType);
            this.CreateLogic(bot, data.LogicType, botDefinition.BehaviorSubtype);
            bot.Init(botBuilder);
            return(bot);
        }
Ejemplo n.º 4
0
        public override void BeforeStart()
        {
            base.BeforeStart();

            if (MyPerGameSettings.EnableAi)
            {
                // We have to create bots here, because when the game is loading, they will have the correct character only after the whole scene is loaded
                foreach (int playerNumber in m_loadedLocalPlayers)
                {
                    MyObjectBuilder_Bot botBuilder = null;
                    m_loadedBotObjectBuildersByHandle.TryGetValue(playerNumber, out botBuilder);

                    Debug.Assert(botBuilder == null || botBuilder.TypeId == botBuilder.BotDefId.TypeId, "Bot types don't match! Are you loading an old save?");
                    if ((botBuilder == null || botBuilder.TypeId == botBuilder.BotDefId.TypeId))
                    {
                        CreateBot(playerNumber, botBuilder);
                    }
                }

                m_loadedLocalPlayers.Clear();
                m_loadedBotObjectBuildersByHandle.Clear();

                Sync.Players.LocalPlayerRemoved += LocalPlayerRemoved;

                if (MyPerGameSettings.Game == GameEnum.ME_GAME && Sync.IsServer)
                {
                    CleanUnusedIdentities();
                }
            }
        }
Ejemplo n.º 5
0
        public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition)
        {
            var output = new MySandboxBot(player, botDefinition);

            if (botBuilder != null)
            {
                output.Init(botBuilder);
            }
            return(output);
        }
Ejemplo n.º 6
0
        public virtual void Init(MyObjectBuilder_Bot botBuilder)
        {
            var ob = botBuilder as MyObjectBuilder_AgentBot;

            if (ob == null)
            {
                return;
            }

            if (ob.AiTarget != null)
            {
                AgentActions.AiTarget.Init(ob.AiTarget);
            }
            if (botBuilder.BotMemory != null)
            {
                m_botMemory.Init(botBuilder.BotMemory);
            }
            BehaviorSubtypeName = ob.LastBehaviorTree;
        }
Ejemplo n.º 7
0
        public override void BeforeStart()
        {
            base.BeforeStart();

            if (MyPerGameSettings.EnableAi)
            {
                // We have to create bots here, because when the game is loading, they will have the correct character only after the whole scene is loaded
                foreach (int playerNumber in m_loadedLocalPlayers)
                {
                    MyObjectBuilder_Bot botBuilder = null;
                    m_loadedBotObjectBuildersByHandle.TryGetValue(playerNumber, out botBuilder);

                    CreateBot(playerNumber, botBuilder);
                }

                m_loadedLocalPlayers.Clear();
                m_loadedBotObjectBuildersByHandle.Clear();

                Sync.Players.LocalPlayerRemoved += LocalPlayerRemoved;
            }
        }
Ejemplo n.º 8
0
        public virtual void Init(MyObjectBuilder_Bot botBuilder)
        {
            var ob = botBuilder as MyObjectBuilder_AgentBot;

            if (ob == null)
            {
                return;
            }

            m_removeAfterDeath = ob.RemoveAfterDeath;
            m_deathCountdownMs = ob.RespawnCounter;

            if (ob.AiTarget != null)
            {
                AgentActions.AiTargetBase.Init(ob.AiTarget);
            }
            if (botBuilder.BotMemory != null)
            {
                m_botMemory.Init(botBuilder.BotMemory);
            }
            MyAIComponent.Static.BehaviorTrees.SetBehaviorName(this, ob.LastBehaviorTree);
        }
Ejemplo n.º 9
0
 public override void BeforeStart()
 {
     base.BeforeStart();
     if (MyPerGameSettings.EnableAi)
     {
         foreach (int num in this.m_loadedLocalPlayers)
         {
             MyObjectBuilder_Bot bot = null;
             this.m_loadedBotObjectBuildersByHandle.TryGetValue(num, out bot);
             if ((bot == null) || (bot.TypeId == bot.BotDefId.TypeId))
             {
                 this.CreateBot(num, bot);
             }
         }
         this.m_loadedLocalPlayers.Clear();
         this.m_loadedBotObjectBuildersByHandle.Clear();
         Sync.Players.LocalPlayerRemoved += new Action <int>(this.LocalPlayerRemoved);
         if ((MyPerGameSettings.Game == GameEnum.ME_GAME) && Sync.IsServer)
         {
             this.CleanUnusedIdentities();
         }
     }
 }
Ejemplo n.º 10
0
        public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition)
        {
            Debug.Assert(m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType), "Undefined behavior type. Bot is not going to be created");
            if (!m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType))
            {
                return(null);
            }
            Debug.Assert(m_botTypeByDefinitionType.ContainsKey(botDefinition.TypeDefinitionId.TypeId), "Type not found. Bot is not going to be created!");
            if (!m_botTypeByDefinitionType.ContainsKey(botDefinition.TypeDefinitionId.TypeId))
            {
                return(null);
            }
            var    botData          = m_botDataByBehaviorType[botDefinition.BehaviorType];
            var    behaviorTypeData = m_botTypeByDefinitionType[botDefinition.TypeDefinitionId.TypeId];
            IMyBot output           = CreateBot(behaviorTypeData.BotType, player, botDefinition);

            CreateActions(output, botData.BotActionsType);
            CreateLogic(output, botData.LogicType, botDefinition.BehaviorSubtype);
            if (botBuilder != null)
            {
                output.Init(botBuilder);
            }
            return(output);
        }
        private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder)
        {
            Debug.Assert(BotFactory != null, "Bot factory is not set! Cannot create a new bot!");
            if (BotFactory == null) return;

            var newPlayer = Sync.Clients.LocalClient.GetPlayer(playerNumber);
            if (newPlayer == null) return;

            var isBotSpawned = m_agentsToSpawn.ContainsKey(playerNumber);
            var isLoading = botBuilder != null;
            var createdByPlayer = false;
            MyBotDefinition botDefinition = null;
            AgentSpawnData spawnData = default(AgentSpawnData);

            // We have to get the bot object builder and bot definition somehow
            // Either, the bot is being spawned on this computer and the definition was saved in the spawn data
            // or the bot is just being created from the object builder (MP bot creation, etc.), so the definition is there
            if (isBotSpawned)
            {
                spawnData = m_agentsToSpawn[playerNumber];
                createdByPlayer = spawnData.CreatedByPlayer;
                botDefinition = spawnData.AgentDefinition;
                m_agentsToSpawn.Remove(playerNumber);
            }
            else
            {
                if (botBuilder == null || botBuilder.BotDefId.TypeId.IsNull)
                {
                    MyPlayer missingBotPlayer = null;
                    if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber), out missingBotPlayer))
                    {
                        Sync.Players.RemovePlayer(missingBotPlayer);
                    }
                    return;
                }

                MyDefinitionManager.Static.TryGetBotDefinition(botBuilder.BotDefId, out botDefinition);
                Debug.Assert(botDefinition != null, "Bot definition could not be found.");
                if (botDefinition == null)
                    return;
            }

            if ((newPlayer.Character == null || !newPlayer.Character.IsDead)
                && BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, isLoading) 
                || createdByPlayer)
            {
                IMyBot bot = null;
                if (isBotSpawned)
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, spawnData.AgentDefinition);                
                else
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, botDefinition);

                if (bot == null)
                {
                    MyLog.Default.WriteLine("Could not create a bot for player " + newPlayer + "!");
                }
                else
                {
                    m_botCollection.AddBot(playerNumber, bot);
                    if (isBotSpawned && bot is IMyEntityBot)
                        (bot as IMyEntityBot).Spawn(spawnData.SpawnPosition, createdByPlayer);

                    if (BotCreatedEvent != null)
                    {
                        BotCreatedEvent(playerNumber, bot.BotDefinition);
                    }
                }
            }
            else
            {
                // hack for removing uncontrolled bot players or saved dead characters
                var player = Sync.Players.GetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber));
                Sync.Players.RemovePlayer(player);
            }
        }
Ejemplo n.º 12
0
        public virtual void Init(MyObjectBuilder_Bot botBuilder)
        {
            var ob = botBuilder as MyObjectBuilder_AgentBot;
            if (ob == null)
                return;

            m_deathCountdownMs = ob.RespawnCounter;

            if (AgentDefinition.FactionTag != null)
            {
                var faction = MySession.Static.Factions.TryGetOrCreateFactionByTag(AgentDefinition.FactionTag);
                if (faction != null)
                {
                    MyFactionCollection.SendJoinRequest(faction.FactionId, Player.Identity.IdentityId);
                    m_joinRequestSent = true;
                }
            }

            if (ob.AiTarget != null)
                AgentActions.AiTargetBase.Init(ob.AiTarget);
            if (botBuilder.BotMemory != null)
                m_botMemory.Init(botBuilder.BotMemory);
            MyAIComponent.Static.BehaviorTrees.SetBehaviorName(this, ob.LastBehaviorTree);
        }
Ejemplo n.º 13
0
        private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder)
        {
            Debug.Assert(BotFactory != null, "Bot factory is not set! Cannot create a new bot!");
            if (BotFactory == null)
            {
                return;
            }

            var newPlayer = Sync.Clients.LocalClient.GetPlayer(playerNumber);

            if (newPlayer == null)
            {
                return;
            }

            var             isBotSpawned    = m_agentsToSpawn.ContainsKey(playerNumber);
            var             isLoading       = botBuilder != null;
            var             createdByPlayer = false;
            MyBotDefinition botDefinition   = null;
            AgentSpawnData  spawnData       = default(AgentSpawnData);

            if (isBotSpawned)
            {
                spawnData       = m_agentsToSpawn[playerNumber];
                createdByPlayer = spawnData.CreatedByPlayer;
                botDefinition   = spawnData.AgentDefinition;
                m_agentsToSpawn.Remove(playerNumber);
            }
            else
            {
                Debug.Assert(botBuilder != null && !botBuilder.BotDefId.TypeId.IsNull, "Null or invalid bot builder. Bot is not going to be created");
                if (botBuilder == null || botBuilder.BotDefId.TypeId.IsNull)
                {
                    return;
                }

                botDefinition = MyDefinitionManager.Static.GetBotDefinition(botBuilder.BotDefId);
                Debug.Assert(botDefinition != null, "Bot definition could not be found.");
                if (botDefinition == null)
                {
                    return;
                }
            }

            if ((newPlayer.Character == null || !newPlayer.Character.IsDead) &&
                BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, isLoading) ||
                createdByPlayer)
            {
                IMyBot bot = null;
                if (isBotSpawned)
                {
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, spawnData.AgentDefinition);
                }
                else
                {
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, botDefinition);
                }

                if (bot == null)
                {
                    MyLog.Default.WriteLine("Could not create a bot for player " + newPlayer + "!");
                }
                else
                {
                    m_botCollection.AddBot(playerNumber, bot);
                    if (isBotSpawned && bot is IMyEntityBot)
                    {
                        (bot as IMyEntityBot).Spawn(spawnData.SpawnPosition, createdByPlayer);
                    }
                }
            }
            else
            {
                // hack for removing uncontrolled bot players or saved dead characters
                var player = Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(MySteam.UserId, playerNumber));
                Sync.Players.RemovePlayer(player);
            }
        }
Ejemplo n.º 14
0
        public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition)
        {
            MyObjectBuilderType obType = MyObjectBuilderType.Invalid;
            if (botBuilder == null)
            {
                obType = botDefinition.Id.TypeId;
                botBuilder = m_objectFactory.CreateObjectBuilder<MyObjectBuilder_Bot>(m_objectFactory.GetProducedType(obType));
            }
            else
            {
                obType = botBuilder.TypeId;
                Debug.Assert(botDefinition.Id == botBuilder.BotDefId, "Bot builder type does not match bot definition type!");
            }

            Debug.Assert(m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType), "Undefined behavior type. Bot is not going to be created");
            if (!m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType))
                return null;
            var botData = m_botDataByBehaviorType[botDefinition.BehaviorType];
            IMyBot output = CreateBot(m_objectFactory.GetProducedType(obType), player, botDefinition);
            CreateActions(output, botData.BotActionsType);
            CreateLogic(output, botData.LogicType, botDefinition.BehaviorSubtype);
            output.Init(botBuilder);
            return output;
        }
Ejemplo n.º 15
0
        private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder)
        {
            if (BotFactory != null)
            {
                MyPlayer player = Sync.Clients.LocalClient.GetPlayer(playerNumber);
                if (player != null)
                {
                    int             num1;
                    bool            flag            = this.m_agentsToSpawn.ContainsKey(playerNumber);
                    bool            load            = botBuilder != null;
                    bool            spawnedByPlayer = false;
                    MyBotDefinition botDefinition   = null;
                    AgentSpawnData  data            = new AgentSpawnData();
                    if (flag)
                    {
                        data            = this.m_agentsToSpawn[playerNumber];
                        spawnedByPlayer = data.CreatedByPlayer;
                        botDefinition   = data.AgentDefinition;
                        this.m_agentsToSpawn.Remove(playerNumber);
                    }
                    else
                    {
                        if ((botBuilder == null) || botBuilder.BotDefId.TypeId.IsNull)
                        {
                            MyPlayer player2 = null;
                            if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber), out player2))
                            {
                                Sync.Players.RemovePlayer(player2, true);
                            }
                            return;
                        }
                        MyDefinitionManager.Static.TryGetBotDefinition(botBuilder.BotDefId, out botDefinition);
                        if (botDefinition == null)
                        {
                            return;
                        }
                    }
                    if ((player.Character == null) || !player.Character.IsDead)
                    {
                        num1 = (int)BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, load);
                    }
                    else
                    {
                        num1 = 0;
                    }
                    if ((num1 | spawnedByPlayer) == 0)
                    {
                        MyPlayer playerById = Sync.Players.GetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber));
                        Sync.Players.RemovePlayer(playerById, true);
                    }
                    else
                    {
                        IMyBot newBot = null;
                        newBot = !flag?BotFactory.CreateBot(player, botBuilder, botDefinition) : BotFactory.CreateBot(player, botBuilder, data.AgentDefinition);

                        if (newBot == null)
                        {
                            MyLog.Default.WriteLine("Could not create a bot for player " + player + "!");
                        }
                        else
                        {
                            this.m_botCollection.AddBot(playerNumber, newBot);
                            if (flag && (newBot is IMyEntityBot))
                            {
                                (newBot as IMyEntityBot).Spawn(data.SpawnPosition, spawnedByPlayer);
                            }
                            if (this.BotCreatedEvent != null)
                            {
                                this.BotCreatedEvent(playerNumber, newBot.BotDefinition);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 16
0
 public void Init(MyObjectBuilder_Bot botBuilder)
 {
     var ob = (MyObjectBuilder_BarbarianBot)botBuilder; // MW:TODO replace with sandbox builder
 }
Ejemplo n.º 17
0
        private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder)
        {
            Debug.Assert(BotFactory != null, "Bot factory is not set! Cannot create a new bot!");
            if (BotFactory == null) return;

            var newPlayer = Sync.Clients.LocalClient.GetPlayer(playerNumber);
            if (newPlayer == null) return;

            var isBotSpawned = m_agentsToSpawn.ContainsKey(playerNumber);
            var isLoading = botBuilder != null;
            var createdByPlayer = false;
            MyBotDefinition botDefinition = null;
            AgentSpawnData spawnData = default(AgentSpawnData);

            // We have to get the bot object builder and bot definition somehow
            // Either, the bot is being spawned on this computer and the definition was saved in the spawn data
            // or the bot is just being created from the object builder (MP bot creation, etc.), so the definition is there
            if (isBotSpawned)
            {
                spawnData = m_agentsToSpawn[playerNumber];
                createdByPlayer = spawnData.CreatedByPlayer;
                botDefinition = spawnData.AgentDefinition;
                m_agentsToSpawn.Remove(playerNumber);
            }
            else
            {
                if (botBuilder == null || botBuilder.BotDefId.TypeId.IsNull)
                {
                    MyPlayer missingBotPlayer = null;
                    if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber), out missingBotPlayer))
                    {
                        Sync.Players.RemovePlayer(missingBotPlayer);
                    }
                    return;
                }

                MyDefinitionManager.Static.TryGetBotDefinition(botBuilder.BotDefId, out botDefinition);
                Debug.Assert(botDefinition != null, "Bot definition could not be found.");
                if (botDefinition == null)
                    return;
            }

            if ((newPlayer.Character == null || !newPlayer.Character.IsDead)
                && BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, isLoading) 
                || createdByPlayer)
            {
                IMyBot bot = null;
                if (isBotSpawned)
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, spawnData.AgentDefinition);                
                else
                    bot = BotFactory.CreateBot(newPlayer, botBuilder, botDefinition);

                if (bot == null)
                {
                    MyLog.Default.WriteLine("Could not create a bot for player " + newPlayer + "!");
                }
                else
                {
                    m_botCollection.AddBot(playerNumber, bot);
                    if (isBotSpawned && bot is IMyEntityBot)
                        (bot as IMyEntityBot).Spawn(spawnData.SpawnPosition, createdByPlayer);

                    if (BotCreatedEvent != null)
                    {
                        BotCreatedEvent(playerNumber, bot.BotDefinition);
                    }
                }
            }
            else
            {
                // hack for removing uncontrolled bot players or saved dead characters
                var player = Sync.Players.GetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber));
                Sync.Players.RemovePlayer(player);
            }
        }