public static BotBase FabricateBot(IMyCubeGrid grid, IMyRemoteControl rc) { try { BotTypeBase botType = BotBase.ReadBotType(rc); BotBase bot = null; switch (botType) { case BotTypeBase.Fighter: bot = new BotTypeFighter(grid); break; case BotTypeBase.Freighter: bot = new BotTypeFreighter(grid); break; case BotTypeBase.Station: bot = new BotTypeStation(grid); break; case BotTypeBase.None: break; case BotTypeBase.Invalid: break; case BotTypeBase.Carrier: break; default: if (Constants.AllowThrowingErrors) { throw new Exception("Invalid bot type"); } break; } return(bot); } catch (Exception scrap) { grid.LogError("BotFabric.FabricateBot", scrap); return(null); } }
public static BotTypeBase ReadBotType(IMyRemoteControl rc) { try { string customData = rc.CustomData.Trim().Replace("\r\n", "\n"); List <string> myCustomData = new List <string>(customData.Split('\n')); if (customData.IsNullEmptyOrWhiteSpace()) { return(BotTypeBase.None); } if (myCustomData.Count < 2) { if (Constants.AllowThrowingErrors) { throw new Exception("CustomData is invalid", new Exception("CustomData consists of less than two lines")); } return(BotTypeBase.Invalid); } if (myCustomData[0].Trim() != "[EEM_AI]") { if (Constants.AllowThrowingErrors) { throw new Exception("CustomData is invalid", new Exception($"AI tag invalid: '{myCustomData[0]}'")); } return(BotTypeBase.Invalid); } string[] bottype = myCustomData[1].Split(':'); if (bottype[0].Trim() != "Type") { if (Constants.AllowThrowingErrors) { throw new Exception("CustomData is invalid", new Exception($"Type tag invalid: '{bottype[0]}'")); } return(BotTypeBase.Invalid); } BotTypeBase botType = BotTypeBase.Invalid; // ReSharper disable once SwitchStatementMissingSomeCases switch (bottype[1].Trim()) { case "Fighter": botType = BotTypeBase.Fighter; break; case "Freighter": botType = BotTypeBase.Freighter; break; case "Carrier": botType = BotTypeBase.Carrier; break; case "Station": botType = BotTypeBase.Station; break; } return(botType); } catch (Exception scrap) { rc.CubeGrid.LogError("[STATIC]BotBase.ReadBotType", scrap); return(BotTypeBase.Invalid); } }