Ejemplo n.º 1
0
        public void InitAi()
        {
            //if (!MyAPIGateway.Multiplayer.IsServer) return;
            if (!Constants.IsServer)
            {
                return;
            }
            if (Constants.DisableAi)
            {
                IsOperable = true;
                _inited    = true;
                DebugWrite("GridComponent.InitAI", "Type:None, shutting down.");
                Shutdown(notify: false);
                return;
            }

            //SetupRecompileTimer();
            try
            {
                if (Grid.Physics == null || ((MyEntity)Grid).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;
                }

                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;
                }

                //BotTypeBase botType = BotBase.ReadBotType(Rc);
                //switch (botType)
                //{
                //	case BotTypeBase.None:
                //		DebugWrite("GridComponent.InitAI", "Skipping grid — no setup found");
                //		break;
                //	case BotTypeBase.Invalid:
                //		LogError("GridComponent.InitAI", new Exception("Bot type is not valid!", new Exception()));
                //		break;
                //	case BotTypeBase.Station:
                //		break;
                //	case BotTypeBase.Fighter:
                //		break;
                //	case BotTypeBase.Freighter:
                //		break;
                //	case BotTypeBase.Carrier:
                //		break;
                //}

                //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;
                }
                if (Ai.Update != default(MyEntityUpdateEnum))
                {
                    NeedsUpdate |= Ai.Update;
                }
                Rc.OnMarkForClose += (trash) => { Shutdown(); };
                IsOperable         = true;
                _inited            = true;
            }
            catch (Exception scrap)
            {
                LogError("GridComponent.InitAI", scrap);
                Shutdown();
            }
        }