/// <summary> /// Remember, this is only for server-side. /// </summary> public static void ChangeOwnershipSmart(this IMyCubeGrid Grid, long NewOwnerID, MyOwnershipShareModeEnum ShareMode) { if (!MyAPIGateway.Session.IsServer) { return; } try { var Subgrids = Grid.GetAllSubgrids(); Grid.ChangeGridOwnership(NewOwnerID, ShareMode); foreach (IMyCubeGrid Subgrid in Subgrids) { try { Subgrid.ChangeGridOwnership(NewOwnerID, ShareMode); } catch (Exception Scrap) { Grid.LogError("ChangeOwnershipSmart.ChangeSubgridOwnership", Scrap); } } } catch (Exception Scrap) { Grid.LogError("ChangeOwnershipSmart", Scrap); } }
/// <summary> /// Remember, this is only for server-side. /// </summary> public static void ChangeOwnershipSmart(this IMyCubeGrid Grid, long newOwnerId, MyOwnershipShareModeEnum shareMode) { if (!MyAPIGateway.Session.IsServer) { return; } try { List <IMyCubeGrid> subgrids = Grid.GetAllSubgrids(); Grid.ChangeGridOwnership(newOwnerId, shareMode); foreach (IMyCubeGrid subgrid in subgrids) { try { subgrid.ChangeGridOwnership(newOwnerId, shareMode); try { foreach (IMyProgrammableBlock pb in subgrid.GetTerminalSystem().GetBlocksOfType <IMyProgrammableBlock>()) { try { //if (!string.IsNullOrEmpty(pb.ProgramData)) continue; //ShowIngameMessage.ShowOverrideMessage($"PB's recompiling... {subgrid.CustomName}"); pb.Recompile(); } catch (Exception) { //ShowIngameMessage.ShowOverrideMessage($"Recompiling this pb threw and error: {e.TargetSite} {e} "); // MyAPIGateway.Utilities.InvokeOnGameThread(() => { pb.Recompile(); }); } } } catch (Exception) { //ShowIngameMessage.ShowOverrideMessage($"PB's recompile threw: {e} "); } } catch (Exception scrap) { Grid.LogError("ChangeOwnershipSmart.ChangeSubgridOwnership", scrap); } } } catch (Exception scrap) { Grid.LogError("ChangeOwnershipSmart", scrap); } }
/// <summary> /// Returns the distance from which the grid can be spotted by Radar via active detection. /// </summary> public static float GetBaseDetectionDistance(this IMyCubeGrid Grid) { try { float Distance = RadarCore.VisibilityDistanceByDefaultRule(Grid); if (Distance == 0) { return(0); } List <IMyDecoy> Decoys = new List <IMyDecoy>(); Grid.GetTerminalSystem().GetBlocksOfType(Decoys, x => x.IsWorking); if (Decoys.Count == 0) { return(Distance); } return(Distance / (float)Math.Pow(RadarCore.DecoyStealthCoefficient, Decoys.Count)); } catch (Exception Scrap) { Grid.LogError("GetBaseDetectionDistance", Scrap); return(0); } }
public static List <T> GetWorkingBlocks <T>(this IMyCubeGrid Grid, bool OverrideEnabledCheck = false, Func <T, bool> collect = null) where T : class, IMyTerminalBlock { try { List <IMySlimBlock> slimBlocks = new List <IMySlimBlock>(); List <T> Blocks = new List <T>(); Grid.GetBlocks(slimBlocks, (x) => x != null && x is T && (!OverrideEnabledCheck ? (x as IMyTerminalBlock).IsWorking : (x as IMyTerminalBlock).IsFunctional)); if (slimBlocks.Count == 0) { return(new List <T>()); } foreach (var _block in slimBlocks) { if (collect == null || collect(_block as T)) { Blocks.Add(_block as T); } } return(Blocks); } catch (Exception Scrap) { Grid.LogError("GridExtensions.GetWorkingBlocks", Scrap); return(new List <T>()); } }
public static List <IMyCubeGrid> GetAllSubgrids(this IMyCubeGrid Grid) { List <IMyCubeGrid> Subgrids = new List <IMyCubeGrid>(); try { IMyGridTerminalSystem Term = Grid.GetTerminalSystem(); List <IMyMechanicalConnectionBlock> MCBlocks = Term.GetBlocksOfType <IMyMechanicalConnectionBlock>(); foreach (IMyMechanicalConnectionBlock MCBlock in MCBlocks) { try { if (!Subgrids.Contains(MCBlock.TopGrid)) { Subgrids.Add(MCBlock.TopGrid); } } catch (Exception Scrap) { Grid.DebugWrite("GetAllSubgrids", $"Error encountered while iterating over mechanical block: {Scrap.Message}"); return(Subgrids); } } return(Subgrids); } catch (Exception Scrap) { Grid.LogError("GetAllSubgrids", Scrap); return(Subgrids); } }
//public static void DeleteSmart(this IMyCubeGrid Grid) //{ // if (!MyAPIGateway.Session.IsServer) return; // List<IMyCubeGrid> Subgrids = Grid.GetAllSubgrids(); // foreach (IMyCubeGrid Subgrid in Subgrids) // Subgrid.Close(); // Grid.Close(); //} public static List <IMyCubeGrid> GetAllSubgrids(this IMyCubeGrid Grid) { try { return(MyAPIGateway.GridGroups.GetGroup(Grid, GridLinkTypeEnum.Logical)); } catch (Exception Scrap) { Grid.LogError("GetAllSubgrids", Scrap); return(new List <IMyCubeGrid>()); } }
public static float GetRadarPower(this IMyCubeGrid Grid) { try { var Radars = new List <MyRadar>(); var blocks = new List <IMyUpgradeModule>(); Grid.GetTerminalSystem().GetBlocksOfType(blocks); var Radar = blocks.FirstOrDefault(x => Controls.IsRadar(x)).Components.Get <MyRadar>(); return(Radar != null ? Radar.TotalRadarPower : 0); } catch (Exception Scrap) { Grid.LogError("GetRadarPower", Scrap); return(0); } }
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 IMyPlayer FindControllingPlayer(this IMyCubeGrid Grid, bool Write = true) { try { IMyPlayer Player = null; IMyGridTerminalSystem Term = Grid.GetTerminalSystem(); List <IMyShipController> ShipControllers = Term.GetBlocksOfType <IMyShipController>(collect: x => x.IsUnderControl); if (ShipControllers.Count == 0) { ShipControllers = Term.GetBlocksOfType <IMyShipController>(x => x.GetBuiltBy() != 0); if (ShipControllers.Count > 0) { IMyShipController MainController = ShipControllers.FirstOrDefault(x => x.IsMainCockpit()) ?? ShipControllers.First(); long ID = MainController.GetBuiltBy(); Player = MyAPIGateway.Players.GetPlayerByID(ID); if (Write && Player != null) { Grid.DebugWrite("Grid.FindControllingPlayer", $"Found cockpit built by player {Player.DisplayName}."); } return(Player); } if (Write) { Grid.DebugWrite("Grid.FindControllingPlayer", "No builder player was found."); } return(null); } Player = MyAPIGateway.Players.GetPlayerByID(ShipControllers.First().ControllerInfo.ControllingIdentityId); if (Write && Player != null) { Grid.DebugWrite("Grid.FindControllingPlayer", $"Found player in control: {Player.DisplayName}"); } return(Player); } catch (Exception Scrap) { Grid.LogError("Grid.FindControllingPlayer", Scrap); return(null); } }
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); } }
public static IMyFaction GetOwnerFaction(this IMyCubeGrid Grid, bool RecalculateOwners = false) { try { if (RecalculateOwners) { (Grid as MyCubeGrid).RecalculateOwners(); } IMyFaction FactionFromBigowners = null; IMyFaction Faction = null; if (Grid.BigOwners.Count > 0 && Grid.BigOwners[0] != 0) { long OwnerID = Grid.BigOwners[0]; FactionFromBigowners = GeneralExtensions.FindOwnerFactionById(OwnerID); } else { Grid.LogError("Grid.GetOwnerFaction", new Exception("Cannot get owner faction via BigOwners.", new Exception("BigOwners is empty."))); } IMyGridTerminalSystem Term = Grid.GetTerminalSystem(); List <IMyTerminalBlock> AllTermBlocks = new List <IMyTerminalBlock>(); Term.GetBlocks(AllTermBlocks); if (AllTermBlocks.Empty()) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Terminal system is empty!"); return(null); } var BiggestOwnerGroup = AllTermBlocks.GroupBy(x => x.GetOwnerFactionTag()).OrderByDescending(gp => gp.Count()).FirstOrDefault(); if (BiggestOwnerGroup != null) { string factionTag = BiggestOwnerGroup.Key; Faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(factionTag); if (Faction != null) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Found owner faction {factionTag} via terminal system"); } return(Faction ?? FactionFromBigowners); } else { Grid.DebugWrite("Grid.GetOwnerFaction", $"CANNOT GET FACTION TAGS FROM TERMINALSYSTEM!"); List <IMyShipController> Controllers = Grid.GetBlocks <IMyShipController>(); if (Controllers.Any()) { List <IMyShipController> MainControllers; if (Controllers.Any(x => x.IsMainCockpit(), out MainControllers)) { Faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(MainControllers[0].GetOwnerFactionTag()); if (Faction != null) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Found owner faction {Faction.Tag} via main cockpit"); return(Faction ?? FactionFromBigowners); } } // Controls falls down if faction was not found by main cockpit Faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(Controllers[0].GetOwnerFactionTag()); if (Faction != null) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Found owner faction {Faction.Tag} via cockpit"); return(Faction ?? FactionFromBigowners); } else { Grid.DebugWrite("Grid.GetOwnerFaction", $"Unable to owner faction via cockpit!"); Faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(AllTermBlocks.First().GetOwnerFactionTag()); if (Faction != null) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Found owner faction {Faction.Tag} via first terminal block"); return(Faction ?? FactionFromBigowners); } else { Grid.DebugWrite("Grid.GetOwnerFaction", $"Unable to owner faction via first terminal block!"); return(Faction ?? FactionFromBigowners); } } } else { Faction = MyAPIGateway.Session.Factions.TryGetFactionByTag(AllTermBlocks.First().GetOwnerFactionTag()); if (Faction != null) { Grid.DebugWrite("Grid.GetOwnerFaction", $"Found owner faction {Faction.Tag} via first terminal block"); return(Faction ?? FactionFromBigowners); } else { Grid.DebugWrite("Grid.GetOwnerFaction", $"Unable to owner faction via first terminal block!"); return(Faction ?? FactionFromBigowners); } } } } catch (Exception Scrap) { Grid.LogError("Faction.GetOwnerFaction", Scrap); return(null); } }