public override void UpdateOnceBeforeFrame() { try { if (Tool.CubeGrid.Physics == null) { NeedsUpdate |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME; return; } Term = Grid.GetTerminalSystem(); ToolCargo = Tool.GetInventory(); CheckInitControls(); SyncBeamLength = new AutoSet <float>(Tool, "BeamLength", Checker: val => val >= MinBeamLengthBlocks && val <= MaxBeamLengthBlocks); SyncDistanceMode = new AutoSet <bool>(Tool, "DistanceBasedMode"); SyncBeamLength.Ask(); SyncDistanceMode.Ask(); BeamLength = 1; Tool.AppendingCustomInfo += Tool_AppendingCustomInfo; Load(); NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME | MyEntityUpdateEnum.EACH_100TH_FRAME; DebugNote = MyAPIGateway.Utilities.CreateNotification($"{Tool.CustomName}", int.MaxValue, (IsWelder ? "Blue" : "Red")); if (SessionCore.Debug) { DebugNote.Show(); } } catch { } }
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); } }
/// <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); } }
/// <summary> /// Acquires a list of players currently manning any ship controllers (including RCs). /// </summary> /// <param name="IncludeSeats">Whether to include passenger seats.</param> /// <returns></returns> public static List <IMyPlayer> GetPilots(this IMyCubeGrid Grid, bool IncludeSeats = true) { List <IMyPlayer> Pilots = new List <IMyPlayer>(); if (Grid == null) { return(Pilots); } IMyGridTerminalSystem Term = Grid.GetTerminalSystem(); List <IMyShipController> ShipControllers = Term.GetBlocksOfType <IMyShipController>(); foreach (IMyShipController ShipController in ShipControllers) { if (!ShipController.IsUnderControl) { continue; } if (!ShipController.CanControlShip && !IncludeSeats) { continue; } IMyPlayer Controller = MyAPIGateway.Players.GetPlayerControllingEntity(ShipController); if (Controller != null && !Pilots.Contains(Controller)) { Pilots.Add(Controller); } } return(Pilots); }
public static bool IsFunctionalGrid(this IMyCubeGrid Grid) { List <IMyTerminalBlock> Blocks = new List <IMyTerminalBlock>(); Grid.GetTerminalSystem().GetBlocksOfType(Blocks, x => x.IsFunctional); bool HasPower = Blocks.Any(x => (x as IMyReactor)?.IsWorking == true || (x as IMyBatteryBlock)?.CurrentStoredPower > 0f); return(Blocks.Any(x => x is IMyShipController) && HasPower && (Grid.IsStatic || (Blocks.Any(x => x is IMyGyro) && Blocks.Any(x => x is IMyThrust)))); }
public BotBase(IMyCubeGrid Grid) { if (Grid == null) { return; } this.Grid = Grid; Term = Grid.GetTerminalSystem(); Antennae = new List <IMyRadioAntenna>(); }
//protected event Action Alert; protected BotBase(IMyCubeGrid grid) { if (grid == null) { return; } Grid = grid; Term = grid.GetTerminalSystem(); Antennae = new List <IMyRadioAntenna>(); }
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); } }
/** * Locate the primary communication antenna and beacon. These may be used by the specific * bot implementation to indicate the status of the ship. */ private void SetupComms() { IMyGridTerminalSystem terminal = m_ownedGrid.GetTerminalSystem(); List<IMyRadioAntenna> antennas = terminal.GetBlocksOfType<IMyRadioAntenna>(); if (antennas.Count > 0) { m_mainAntenna = antennas[0]; } foreach(IMyRadioAntenna antenna in antennas) { antenna.EnableBroadcasting = false; } List<IMyBeacon> beacons = terminal.GetBlocksOfType<IMyBeacon>(); if (beacons.Count > 0) { m_mainBeacon = beacons[0]; } foreach(IMyBeacon beacon in beacons) { beacon.Enabled = false; } }
public List <Dictionary <string, string> > ScanTarget(Ingame.MyDetectedEntityInfo Target) { if (CanScan(Target) == false) { return(null); } List <Dictionary <string, string> > Scan = new List <Dictionary <string, string> >(); List <IMyTerminalBlock> TargetBlocks = new List <IMyTerminalBlock>(); IMyCubeGrid Grid = MyAPIGateway.Entities.GetEntityById(Target.EntityId) as IMyCubeGrid; Grid.GetTerminalSystem().GetBlocks(TargetBlocks); foreach (IMyTerminalBlock Block in TargetBlocks) { Scan.Add(ReadBlock(Block)); } LastScan = DateTime.Now; return(Scan); }
public static float GetMaxBatteryPowerOutput(this IMyCubeGrid Grid) { List <IMyBatteryBlock> Batteries = new List <IMyBatteryBlock>(); Grid.GetTerminalSystem().GetBlocksOfType(Batteries, x => x.IsWorking && x.HasCapacityRemaining); if (Batteries.Count == 0) { return(0); } float SummarizedOutput = 0; foreach (var Battery in Batteries) { SummarizedOutput += Battery.MaxOutput(); } return(SummarizedOutput); }
public static float GetMaxReactorPowerOutput(this IMyCubeGrid Grid) { List <IMyReactor> Reactors = new List <IMyReactor>(); Grid.GetTerminalSystem().GetBlocksOfType(Reactors, x => x.IsWorking); if (Reactors.Count == 0) { return(0); } float SummarizedOutput = 0; foreach (var Reactor in Reactors) { SummarizedOutput += Reactor.MaxOutput; } return(SummarizedOutput); }
protected bool TrackTargetPickSubtarget(Ingame.MyDetectedEntityInfo Target) { if (TurretPosition.DistanceTo(Target.Position) > Turret.Range) { return(false); } if (!Target.IsGrid()) { IMyEntity TargetEntity = MyAPIGateway.Entities.GetEntityById(Target.EntityId); if (TurretPosition.DistanceTo(TargetEntity.GetPosition()) > Turret.Range) { return(false); } Turret.TrackTarget(TargetEntity); return(true); } IMyCubeGrid Grid = MyAPIGateway.Entities.GetEntityById(Target.EntityId) as IMyCubeGrid; if (Grid == null) { return(false); } List <IMyTerminalBlock> TermBlocks = new List <IMyTerminalBlock>(); IMyGridTerminalSystem Term = Grid.GetTerminalSystem(); if (Term == null) { return(false); } Term.GetBlocks(TermBlocks); TermBlocks.RemoveAll(x => !x.IsFunctional); if (!TermBlocks.Any()) { Turret.TrackTarget(Grid); return(true); } var PrioritizedBlocks = TermBlocks.OrderByDescending(x => PriorityIndex(x)).ThenBy(x => DistanceSq(x.GetPosition())); Turret.TrackTarget(PrioritizedBlocks.First()); return(true); }
/** * Get the player currently flying a ship. */ public static IMyPlayer GetControllingPlayer(this IMyCubeGrid grid) { IMyGridTerminalSystem term = grid.GetTerminalSystem(); List<IMyShipController> shipControllers = term.GetBlocksOfType<IMyShipController>(collect: x => x.IsUnderControl); if (shipControllers.Count > 0) { return MyAPIGateway.Players.GetPlayerByID( shipControllers[0].ControllerInfo.ControllingIdentityId); } shipControllers = term.GetBlocksOfType<IMyShipController>(x => x.GetBuiltBy() != 0); if (shipControllers.Count > 0) { IMyShipController mainController = shipControllers.Find(x => x.IsMainCockpit); if (mainController == null) { mainController = shipControllers[0]; } return MyAPIGateway.Players.GetPlayerByID(mainController.GetBuiltBy()); } 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); } }
public void SpawnerCallback(EncounterType encounterType, MySpawnGroupDefinition.SpawnGroupPrefab prefab, Vector3D spawnCoord, Vector3D despawnCoords, IMyFaction ownerFaction) { Util.Log("Searching for spawned Prefab..."); IMyCubeGrid spawnedShip = null; double minDist = double.MaxValue; BoundingSphereD searchSphere = new BoundingSphereD(spawnCoord, SPAWNER_SEARCH_RADIUS); List <IMyEntity> entityList = MyAPIGateway.Entities.GetEntitiesInSphere(ref searchSphere); foreach (IMyEntity entity in entityList) { IMyCubeGrid targetGrid = entity as IMyCubeGrid; if (targetGrid == null) { continue; } //if (targetGrid.CustomName != prefab.SubtypeId) { //continue; //} if ((targetGrid.GetPosition() - spawnCoord).LengthSquared() < minDist) { spawnedShip = targetGrid; } } if (spawnedShip == null) { Util.Error("Error: Could not find ship " + prefab.SubtypeId + " after spawning."); return; } spawnedShip.ChangeGridOwnership(m_empireData.m_faction.FounderId, MyOwnershipShareModeEnum.None); IMyGridTerminalSystem gts = spawnedShip.GetTerminalSystem(); List <IMyRemoteControl> blocks = new List <IMyRemoteControl>(); gts.GetBlocksOfType(blocks); IMyRemoteControl firstRemote = blocks.Find(b => b.IsFunctional); LaunchDrone(encounterType, firstRemote, spawnedShip, despawnCoords); }
protected bool TrackTarget(Ingame.MyDetectedEntityInfo Target, long SubtargetID) { if (TurretPosition.DistanceTo(Target.Position) > Turret.Range) { return(false); } IMyCubeGrid Grid = MyAPIGateway.Entities.GetEntityById(Target.EntityId) as IMyCubeGrid; if (Grid == null) { return(false); } IMyTerminalBlock Block = null; List <IMyTerminalBlock> Blocks = new List <IMyTerminalBlock>(); Grid.GetTerminalSystem().GetBlocks(Blocks); Block = Blocks.FirstOrDefault(x => x.EntityId == SubtargetID); if (Block == null || TurretPosition.DistanceTo(Block.GetPosition()) > Turret.Range) { return(false); } Turret.TrackTarget(Block); return(true); }
/// <summary> /// Returns the distance from which the grid can be spotted by Radar via antenna/beacon. /// 0 if no broadcasting beacons/antennae are found. /// </summary> public static float GetMarkerRange(this IMyCubeGrid Grid) { try { var Term = Grid.GetTerminalSystem(); List <IMyBeacon> Beacons = new List <IMyBeacon>(); List <IMyRadioAntenna> Antennae = new List <IMyRadioAntenna>(); Term.GetBlocksOfType(Beacons, x => x.IsWorking); Term.GetBlocksOfType(Antennae, x => x.IsWorking && x.IsBroadcasting); if (Beacons.Count == 0 && Antennae.Count == 0) { return(0); } float BeaconRange = 0; float AntennaRange = 0; if (Beacons.Count > 0) { BeaconRange = Beacons.Select(x => x.Radius).Max(); } if (Antennae.Count > 0) { AntennaRange = Antennae.Select(x => x.Radius).Max(); } return(Math.Max(BeaconRange, AntennaRange)); } catch (Exception Scrap) { GridDebugHelpers.LogError(Grid, "GetMarkerRange", Scrap); return(0); } }
public static bool HasController(this IMyCubeGrid Grid) { return(Grid.GetTerminalSystem().GetBlocksOfType <IMyShipController>().Any(x => x.IsFunctional)); }
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); } }