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); } }
static bool IsDamagedByPlayerInNeutralGrid(IMyCubeGrid Grid, out IMyPlayer Damager) { Damager = null; try { Damager = Grid.FindControllingPlayer(); if (Damager != null) { return(!Damager.IsBot); } try { List <MyCubeBlock> CubeBlocks = Grid.GetBlocks <MyCubeBlock>(x => x.BuiltBy != 0); if (CubeBlocks.Count != 0) { var ThatCunningGrieferID = CubeBlocks[0].BuiltBy; Damager = MyAPIGateway.Players.GetPlayerByID(ThatCunningGrieferID); return(Damager != null); } else { List <IMySlimBlock> SlimBlocks = Grid.GetBlocks(Selector: x => x.GetBuiltBy() != 0, BlockLimit: 50); if (SlimBlocks.Count == 0) { return(false); // We give up on this one } else { try { Damager = MyAPIGateway.Players.GetPlayerByID(SlimBlocks.First().GetBuiltBy()); if (Damager != null) { Grid.DebugWrite("Damage.IsDoneByPlayer.FindBuilderBySlimBlocks", $"Found damager player from slim block. Damager is {Damager.DisplayName}"); } return(Damager != null); } catch (Exception Scrap) { AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via SlimBlocks BuiltBy crashed.", Scrap)); return(false); } } } } catch (Exception Scrap) { AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via BuiltBy crashed.", Scrap)); return(false); } } catch (Exception Scrap) { AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check neutral grid crashed", Scrap)); return(false); } }
private static bool IsDamagedByPlayerInNeutralGrid(IMyCubeGrid grid, out IMyPlayer damager) { damager = null; try { damager = grid.FindControllingPlayer(); if (damager != null) { return(!damager.IsBot); } try { List <MyCubeBlock> cubeBlocks = grid.GetBlocks <MyCubeBlock>(x => x.BuiltBy != 0); if (cubeBlocks.Count != 0) { long thatCunningGrieferId = cubeBlocks[0].BuiltBy; damager = MyAPIGateway.Players.GetPlayerById(thatCunningGrieferId); return(damager != null); } else { List <IMySlimBlock> slimBlocks = grid.GetBlocks(Selector: x => x.GetBuiltBy() != 0, BlockLimit: 50); if (slimBlocks.Count == 0) { return(false); // We give up on this one } else { try { damager = MyAPIGateway.Players.GetPlayerById(slimBlocks.First().GetBuiltBy()); if (damager != null) { grid.DebugWrite("Damage.IsDoneByPlayer.FindBuilderBySlimBlocks", $"Found damager player from slim block. Damager is {damager.DisplayName}"); } return(damager != null); } catch (Exception scrap) { AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via SlimBlocks BuiltBy crashed.", scrap)); return(false); } } } } catch (Exception scrap) { AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via BuiltBy crashed.", scrap)); return(false); } } catch (Exception scrap) { AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check neutral grid crashed", scrap)); return(false); } }
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); } }
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); } }