Esempio n. 1
0
        static public BotBase FabricateBot(IMyCubeGrid Grid, IMyRemoteControl RC)
        {
            try
            {
                BotTypes BotType = BotBase.ReadBotType(RC);

                BotBase Bot = null;
                switch (BotType)
                {
                case BotTypes.Fighter:
                    Bot = new FighterBot(Grid);
                    break;

                case BotTypes.Freighter:
                    Bot = new FreighterBot(Grid);
                    break;

                case BotTypes.Station:
                    Bot = new StationBot(Grid);
                    break;

                default:
                    if (AISessionCore.AllowThrowingErrors)
                    {
                        throw new Exception("Invalid bot type");
                    }
                    break;
                }

                return(Bot);
            }
            catch (Exception Scrap)
            {
                Grid.LogError("BotFabric.FabricateBot", Scrap);
                return(null);
            }
        }
Esempio n. 2
0
        protected override bool ParseSetup()
        {
            if (BotBase.ReadBotType(RC) != BotType)
            {
                return(false);
            }

            List <string> CustomData = RC.CustomData.Trim().Replace("\r\n", "\n").Split('\n').ToList();

            foreach (string DataLine in CustomData)
            {
                if (DataLine.Contains("EEM_AI"))
                {
                    continue;
                }
                if (DataLine.Contains("Type"))
                {
                    continue;
                }
                var Data = DataLine.Trim().Split(':');
                Data[1] = Data[1].Trim();
                switch (Data[0].Trim())
                {
                case "Faction":
                    break;

                case "FleeOnlyWhenDamaged":
                    if (!bool.TryParse(Data[1], out FreighterSetup.FleeOnlyWhenDamaged))
                    {
                        DebugWrite("ParseSetup", "AI setup error: FleeOnlyWhenDamaged cannot be parsed");
                        return(false);
                    }
                    break;

                case "FleeTriggerDistance":
                    if (!float.TryParse(Data[1], out FreighterSetup.FleeTriggerDistance))
                    {
                        DebugWrite("ParseSetup", "AI setup error: FleeTriggerDistance cannot be parsed");
                        return(false);
                    }
                    break;

                case "FleeSpeedRatio":
                    if (!float.TryParse(Data[1], out FreighterSetup.FleeSpeedRatio))
                    {
                        DebugWrite("ParseSetup", "AI setup error: FleeSpeedRatio cannot be parsed");
                        return(false);
                    }
                    break;

                case "FleeSpeedCap":
                    if (!float.TryParse(Data[1], out FreighterSetup.FleeSpeedCap))
                    {
                        DebugWrite("ParseSetup", "AI setup error: FleeSpeedCap cannot be parsed");
                        return(false);
                    }
                    break;

                default:
                    DebugWrite("ParseSetup", $"AI setup error: Cannot parse '{DataLine}'");
                    return(false);
                }
            }

            FreighterSetup.Default();
            return(true);
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
        protected override bool ParseSetup()
        {
            if (BotBase.ReadBotType(RC) != BotType)
            {
                return(false);
            }

            List <string> CustomData = RC.CustomData.Trim().Replace("\r\n", "\n").Split('\n').ToList();

            foreach (string DataLine in CustomData)
            {
                if (DataLine.Contains("EEM_AI"))
                {
                    continue;
                }
                if (DataLine.Contains("Type"))
                {
                    continue;
                }
                var Data = DataLine.Trim().Split(':');
                Data[0] = Data[0].Trim();
                Data[1] = Data[1].Trim();

                switch (Data[0])
                {
                case "Faction":
                    break;

                case "Preset":
                    FighterSetup.Preset = Data[1];
                    break;

                case "DelayedAI":
                    if (!bool.TryParse(Data[1], out FighterSetup.DelayedAIEnable))
                    {
                        DebugWrite("ParseSetup", "AI setup error: DelayedAI cannot be parsed");
                        return(false);
                    }
                    break;

                case "AssignToPirates":
                    if (!bool.TryParse(Data[1], out FighterSetup.AssignToPirates))
                    {
                        DebugWrite("ParseSetup", "AI setup error: AssignToPirates cannot be parsed");
                        return(false);
                    }
                    break;

                case "AttackNeutrals":
                    if (!bool.TryParse(Data[1], out FighterSetup.AttackNeutrals))
                    {
                        DebugWrite("ParseSetup", "AI setup error: AttackNeutrals cannot be parsed");
                        return(false);
                    }
                    break;

                case "AmbushMode":
                    if (!bool.TryParse(Data[1], out FighterSetup.AmbushMode))
                    {
                        DebugWrite("ParseSetup", "AI setup error: AmbushMode cannot be parsed");
                        return(false);
                    }
                    break;

                case "SeekDistance":
                    if (!float.TryParse(Data[1], out FighterSetup.SeekDistance))
                    {
                        DebugWrite("ParseSetup", "AI setup error: SeekDistance cannot be parsed");
                        return(false);
                    }
                    break;

                case "ActivationDistance":
                    if (!float.TryParse(Data[1], out FighterSetup.AIActivationDistance))
                    {
                        DebugWrite("ParseSetup", "AI setup error: ActivationDistance cannot be parsed");
                        return(false);
                    }
                    break;

                case "PlayerPriority":
                    DebugWrite("ParseSetup", "AI setup warning: PlayerPriority is deprecated and no longer used.");
                    break;

                case "CallHelpProbability":
                    int probability;
                    if (!int.TryParse(Data[1], out probability))
                    {
                        DebugWrite("ParseSetup", "AI setup error: CallHelpProbability cannot be parsed");
                        return(false);
                    }
                    else if (probability < 0 || probability > 100)
                    {
                        DebugWrite("ParseSetup", "AI setup error: CallHelpProbability out of bounds. Must be between 0 and 100");
                        return(false);
                    }
                    else
                    {
                        FighterSetup.CallHelpProbability = probability;
                    }
                    break;

                case "ThrustMultiplier":
                    float multiplier;
                    if (!float.TryParse(Data[1], out multiplier))
                    {
                        DebugWrite("ParseSetup", "AI setup error: ThrustMultiplier cannot be parsed");
                        return(false);
                    }
                    else
                    {
                        ApplyThrustMultiplier(multiplier);
                    }
                    break;

                default:
                    DebugWrite("ParseSetup", $"AI setup error: Cannot parse '{DataLine}'");
                    return(false);
                }
            }
            FighterSetup.Default();
            return(true);
        }