protected List <Ingame.MyDetectedEntityInfo> LookAround(float Radius, Func <Ingame.MyDetectedEntityInfo, bool> Filter = null) { List <Ingame.MyDetectedEntityInfo> RadarData = new List <Ingame.MyDetectedEntityInfo>(); BoundingSphereD LookaroundSphere = new BoundingSphereD(GridPosition, Radius); List <IMyEntity> EntitiesAround = MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref LookaroundSphere); EntitiesAround.RemoveAll(x => x == Grid || GridPosition.DistanceTo(x.GetPosition()) < GridRadius * 1.5); long OwnerID; if (OwnerFaction != null) { OwnerID = OwnerFaction.FounderId; Grid.DebugWrite("LookAround", "Found owner via faction owner"); } else { OwnerID = RC.OwnerId; Grid.DebugWrite("LookAround", "OWNER FACTION NOT FOUND, found owner via RC owner"); } foreach (IMyEntity DetectedEntity in EntitiesAround) { Ingame.MyDetectedEntityInfo RadarDetectedEntity = MyDetectedEntityInfoHelper.Create(DetectedEntity as MyEntity, OwnerID); if (Filter == null ? true : (Filter(RadarDetectedEntity))) { RadarData.Add(RadarDetectedEntity); } } //DebugWrite("LookAround", $"Radar entities detected: {String.Join(" | ", RadarData.Select(x => $"{x.Name}"))}"); return(RadarData); }
//protected virtual void RegisterHostileAction(IMyPlayer player, TimeSpan truceDelay) //{ // try // { // #region Sanity checks // if (player == null) // { // Grid.DebugWrite("RegisterHostileAction", "Error: Damager is null."); // return; // } // if (_ownerFaction == null) // { // _ownerFaction = Grid.GetOwnerFaction(); // } // if (_ownerFaction == null || !_ownerFaction.IsNpc()) // { // Grid.DebugWrite("RegisterHostileAction", $"Error: {(_ownerFaction == null ? "can't find own faction" : "own faction isn't recognized as NPC.")}"); // return; // } // #endregion // IMyFaction hostileFaction = player.GetFaction(); // if (hostileFaction == null) // { // Grid.DebugWrite("RegisterHostileAction", "Error: can't find damager's faction"); // return; // } // if (hostileFaction == _ownerFaction) // { // _ownerFaction.Kick(player); // return; // } // //AiSessionCore.WarDeclared = // AiSessionCore.DeclareWar(_ownerFaction, hostileFaction, truceDelay); // //if (!_ownerFaction.IsLawful()) return; // //AiSessionCore.DeclareWar(Diplomacy.Police, hostileFaction, truceDelay); // //AiSessionCore.DeclareWar(Diplomacy.Army, hostileFaction, truceDelay); // } // catch (Exception scrap) // { // LogError("RegisterHostileAction", scrap); // } //} //protected virtual void RegisterHostileAction(IMyFaction hostileFaction, TimeSpan truceDelay) //{ // try // { // if (hostileFaction != null) // { // //AiSessionCore.WarDeclared = // AiSessionCore.DeclareWar(_ownerFaction, hostileFaction, truceDelay); // //if (!_ownerFaction.IsLawful()) return; // //AiSessionCore.DeclareWar(Diplomacy.Police, hostileFaction, truceDelay); // //AiSessionCore.DeclareWar(Diplomacy.Army, hostileFaction, truceDelay); // } // else // { // Grid.DebugWrite("RegisterHostileAction", "Error: can't find damager's faction"); // } // } // catch (Exception scrap) // { // LogError("RegisterHostileAction", scrap); // } //} //TODO Figure out why there is a NULL REFERENCE EXCEPTION from this call on velocity from MyDetectedEntityInfo // velocity = myCubeGrid.Physics.LinearVelocity; + $exception {System.NullReferenceException: Object reference not set to an instance of an object. // at Sandbox.Game.Entities.MyDetectedEntityInfoHelper.Create(MyEntity entity, Int64 sensorOwner, Nullable`1 hitPosition)} // System.NullReferenceException protected List <MyDetectedEntityInfo> LookAround(float radius, Func <MyDetectedEntityInfo, bool> filter = null) { List <MyDetectedEntityInfo> radarData = new List <MyDetectedEntityInfo>(); BoundingSphereD lookaroundSphere = new BoundingSphereD(GridPosition, radius); List <IMyEntity> entitiesAround = MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref lookaroundSphere); entitiesAround.RemoveAll(x => x == Grid || GridPosition.DistanceTo(x.GetPosition()) < GridRadius * 1.5); long ownerId; if (_ownerFaction != null) { ownerId = _ownerFaction.FounderId; Grid.DebugWrite("LookAround", "Found owner via faction owner"); } else { ownerId = Rc.OwnerId; Grid.DebugWrite("LookAround", "OWNER FACTION NOT FOUND, found owner via RC owner"); } foreach (IMyEntity detectedEntity in entitiesAround) { if (detectedEntity is IMyFloatingObject || detectedEntity.Physics == null) { continue; } MyDetectedEntityInfo radarDetectedEntity = MyDetectedEntityInfoHelper.Create(detectedEntity as MyEntity, ownerId); if (radarDetectedEntity.Type == MyDetectedEntityType.None || radarDetectedEntity.Type == MyDetectedEntityType.Unknown) { continue; } if (filter == null || filter(radarDetectedEntity)) { radarData.Add(radarDetectedEntity); } } //DebugWrite("LookAround", $"Radar entities detected: {String.Join(" | ", RadarData.Select(x => $"{x.Name}"))}"); return(radarData); }
private void Flee(List <Ingame.MyDetectedEntityInfo> radarData = null) { try { if (!IsFleeing) { return; } try { if (!FleeTimersTriggered) { TriggerFleeTimers(); } try { if (radarData == null) { radarData = LookForEnemies(_freighterSetup.FleeTriggerDistance); } if (radarData.Count == 0) { return; } try { Ingame.MyDetectedEntityInfo closestEnemy = radarData.OrderBy(x => GridPosition.DistanceTo(x.Position)).FirstOrDefault(); if (closestEnemy.IsEmpty()) { Grid.DebugWrite("Flee", "Cannot find closest hostile"); return; } try { IMyEntity enemyEntity = MyAPIGateway.Entities.GetEntityById(closestEnemy.EntityId); if (enemyEntity == null) { Grid.DebugWrite("Flee", "Cannot find enemy entity from closest hostile ID"); return; } try { //Grid.DebugWrite("Flee", $"Fleeing from '{EnemyEntity.DisplayName}'. Distance: {Math.Round(GridPosition.DistanceTo(ClosestEnemy.Position))}m; FleeTriggerDistance: {FreighterSetup.FleeTriggerDistance}"); //ShowIngameMessage.ShowMessage($"Fleeing from '{enemyEntity.DisplayName}'. Distance: {Math.Round(GridPosition.DistanceTo(closestEnemy.Position))}m; FleeTriggerDistance: {_freighterSetup.FleeTriggerDistance}"); Vector3D fleePoint = GridPosition.InverseVectorTo(closestEnemy.Position, 100 * 1000); //ShowIngameMessage.ShowMessage($"Flee point: {fleePoint} which is {GridPosition.DistanceTo(fleePoint)}m from me and enemy {enemyEntity.DisplayName}"); //ShowIngameMessage.ShowMessage($"Fleeing at: {DetermineFleeSpeed()}m/s..."); Rc.AddWaypoint(fleePoint, "Flee Point"); (Rc as MyRemoteControl)?.ChangeFlightMode(Ingame.FlightMode.OneWay); (Rc as MyRemoteControl)?.SetAutoPilotSpeedLimit(DetermineFleeSpeed()); Rc.SetAutoPilotEnabled(true); } catch (Exception scrap) { Grid.LogError("Flee.AddWaypoint", scrap); } } catch (Exception scrap) { Grid.LogError("Flee.LookForEnemies.GetEntity", scrap); } } catch (Exception scrap) { Grid.LogError("Flee.LookForEnemies.Closest", scrap); } } catch (Exception scrap) { Grid.LogError("Flee.LookForEnemies", scrap); } } catch (Exception scrap) { Grid.LogError("Flee.TriggerTimers", scrap); } } catch (Exception scrap) { Grid.LogError("Flee", scrap); } }
private void Flee(List <Ingame.MyDetectedEntityInfo> RadarData = null) { try { if (!IsFleeing) { return; } try { if (!FleeTimersTriggered) { TriggerFleeTimers(); } try { if (RadarData == null) { RadarData = LookForEnemies(FreighterSetup.FleeTriggerDistance); } if (RadarData.Count == 0) { return; } try { Ingame.MyDetectedEntityInfo ClosestEnemy = RadarData.OrderBy(x => GridPosition.DistanceTo(x.Position)).FirstOrDefault(); if (ClosestEnemy.IsEmpty()) { Grid.DebugWrite("Flee", "Cannot find closest hostile"); return; } try { IMyEntity EnemyEntity = MyAPIGateway.Entities.GetEntityById(ClosestEnemy.EntityId); if (EnemyEntity == null) { Grid.DebugWrite("Flee", "Cannot find enemy entity from closest hostile ID"); return; } try { //Grid.DebugWrite("Flee", $"Fleeing from '{EnemyEntity.DisplayName}'. Distance: {Math.Round(GridPosition.DistanceTo(ClosestEnemy.Position))}m; FleeTriggerDistance: {FreighterSetup.FleeTriggerDistance}"); Vector3D FleePoint = GridPosition.InverseVectorTo(ClosestEnemy.Position, 100 * 1000); RC.AddWaypoint(FleePoint, "Flee Point"); (RC as MyRemoteControl).ChangeFlightMode(MyRemoteControl.FlightMode.OneWay); (RC as MyRemoteControl).SetAutoPilotSpeedLimit(DetermineFleeSpeed()); RC.SetAutoPilotEnabled(true); } catch (Exception Scrap) { Grid.LogError("Flee.AddWaypoint", Scrap); } } catch (Exception Scrap) { Grid.LogError("Flee.LookForEnemies.GetEntity", Scrap); } } catch (Exception Scrap) { Grid.LogError("Flee.LookForEnemies.Closest", Scrap); } } catch (Exception Scrap) { Grid.LogError("Flee.LookForEnemies", Scrap); } } catch (Exception Scrap) { Grid.LogError("Flee.TriggerTimers", Scrap); } } catch (Exception Scrap) { Grid.LogError("Flee", Scrap); } }