Exemplo n.º 1
0
        public void InitAI()
        {
            if (!AISessionCore.IsServer)
            {
                return;
            }
            SetupRecompileTimer();

            try
            {
                if (Grid.Physics == null || (Grid as MyEntity).IsPreview)
                {
                    return;
                }
            }
            catch (Exception Scrap)
            {
                LogError("InitAI[grid check]", Scrap);
            }

            try
            {
                if (string.IsNullOrWhiteSpace(RC.CustomData) || !RC.CustomData.Contains("[EEM_AI]"))
                {
                    Shutdown(Notify: false);
                    return;
                }

                //DebugWrite("GridComponent.InitAI", "Booting up RC component.");

                if (!RC.IsOwnedByNPC())
                {
                    DebugWrite("GridComponent.InitAI", "RC is not owned by NPC!");
                    Shutdown(Notify: false);
                    return;
                }

                if (RC.CustomData.Contains("Faction:"))
                {
                    try
                    {
                        TryAssignToFaction();
                    }
                    catch (Exception Scrap)
                    {
                        LogError("TryAssignToFaction", Scrap);
                    }
                }

                if (RC.CustomData.Contains("Type:None"))
                {
                    IsOperable = true;
                    Inited     = true;
                    DebugWrite("GridComponent.InitAI", "Type:None, shutting down.");
                    Shutdown(Notify: false);
                    return;
                }

                BotTypes BotType = BotBase.ReadBotType(RC);
                if (BotType == BotTypes.None)
                {
                    DebugWrite("GridComponent.InitAI", "Skipping grid — no setup found");
                }
                else if (BotType == BotTypes.Invalid)
                {
                    LogError("GridComponent.InitAI", new Exception("Bot type is not valid!", new Exception()));
                }

                //DebugWrite("GridComponent.InitAI", $"Bot found. Bot type: {BotType.ToString()}");
            }
            catch (Exception Scrap)
            {
                LogError("GridComponent.InitAI", Scrap);
                return;
            }

            try
            {
                AI = BotFabric.FabricateBot(Grid, RC);

                if (AI == null)
                {
                    DebugWrite("GridComponent.InitAI", "Bot Fabricator yielded null");
                    Shutdown();
                    return;
                }

                bool init = AI.Init(RC);

                if (init)
                {
                    //DebugWrite("GridComponent.InitAI", "AI.Init() successfully initialized AI component");
                }
                else
                {
                    DebugWrite("GridComponent.InitAI", "AI.Init() returned false — bot initialization failed somewhy");
                    Shutdown();
                    return;
                }

                //DebugWrite("GridComponent.InitAI", $"AI Operable: {AI.Operable}");

                if (AI.Update != default(MyEntityUpdateEnum))
                {
                    RC.NeedsUpdate |= AI.Update;
                }
                RC.OnMarkForClose += (trash) => { Shutdown(); };
                IsOperable         = true;
                Inited             = true;
                //Grid.DebugWrite("InitAI", "Grid Component successfully inited.");
            }
            catch (Exception Scrap)
            {
                LogError("GridComponent.InitAI", Scrap);
                Shutdown();
            }
        }