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 void AddEntity(IMyEntity RadarEntity, Vector3D?Hit) { Ingame.MyDetectedEntityInfo RadarInfo = MyDetectedEntityInfoHelper.Create(RadarEntity as MyEntity, RadarBlock.OwnerId, Hit); bool AddMarker = DetectedEntities.Add(RadarInfo) & MarkerModule.ShouldMarkerExist(RadarInfo); if (!AddMarker || MarkerModule.RadarMarkers.ContainsKey(RadarInfo.EntityId)) { return; } IMyVoxelMap voxel; if (RadarEntity is IMyCubeGrid) { string GridName = (RadarEntity as IMyCubeGrid).CustomName; MarkerModule.AddGPSMarker(RadarInfo.Rename(GridName), GridName); } else if (RadarEntity.IsOfType(out voxel)) { string VoxelName = (!voxel.StorageName.StartsWith("Asteroid_") ? voxel.StorageName : "Asteroid"); MarkerModule.AddGPSMarker(RadarInfo, VoxelName); } else { MarkerModule.AddGPSMarker(RadarInfo, RadarEntity.DisplayName); } }
public void Update60() { if (!remoteControl.IsControlledByFaction("GCORP")) { // return; // No point bothering to remove from the list, it will disappear next time the game reloads } /* * TODO: * refill weapon inventory (if designated?) * sequence shooting when multiple on same sub-grid * appropriate sequencing for missile launchers */ playerTarget = DuckUtils.GetNearestPlayerToPosition(bodyGrid.GetPosition(), Range); if (playerTarget != null) { //MyAPIGateway.Utilities.ShowNotification("Found Player: " + info.Type, 2000, MyFontEnum.DarkBlue); IHitInfo hitInfo; if (MyAPIGateway.Physics.CastLongRay(weapons[0].GetPosition(), playerTarget.GetPosition(), out hitInfo, false)) { if (!spoken) { bodyGrid.PlaySoundBlocks(); spoken = true; } if (timeSincePlayerSeen < 3) { timeSincePlayerSeen++; return; } var info = MyDetectedEntityInfoHelper.Create((MyEntity)hitInfo.HitEntity, remoteControl.OwnerId, hitInfo.Position); if (info.Relationship == MyRelationsBetweenPlayerAndBlock.Enemies || info.Relationship == MyRelationsBetweenPlayerAndBlock.NoOwnership || info.Relationship == MyRelationsBetweenPlayerAndBlock.Neutral) { SetWeaponsShooting(true); } else { SetWeaponsShooting(false); } // MyAPIGateway.Utilities.ShowNotification("Hit: " + info.Type, 2000, MyFontEnum.DarkBlue); // MyAPIGateway.Utilities.ShowNotification("Relation: " + info.Relationship, 2000, MyFontEnum.DarkBlue); } else { SetWeaponsShooting(true); } } else { SetWeaponsShooting(false); StopAllRotors(); } }
public void Update60() { // TODO: need a reference to the whole base grid to properly determine ownership if (!remoteControl.IsControlledByFaction("GCORP")) { // return; // No point bothering to remove from the list, it will disappear next time the game reloads } playerTarget = DuckUtils.GetNearestPlayerToPosition(bodyGrid.GetPosition(), Range); if (playerTarget != null) { IHitInfo hitInfo; if (MyAPIGateway.Physics.CastLongRay(weapons[0].GetPosition(), playerTarget.GetPosition(), out hitInfo, false)) { if (!spoken) { bodyGrid.PlaySoundBlocks(); spoken = true; } if (timeSincePlayerSeen < 3) { timeSincePlayerSeen++; return; } var info = MyDetectedEntityInfoHelper.Create((MyEntity)hitInfo.HitEntity, remoteControl.OwnerId, hitInfo.Position); if (info.Relationship == MyRelationsBetweenPlayerAndBlock.Enemies || info.Relationship == MyRelationsBetweenPlayerAndBlock.NoOwnership || info.Relationship == MyRelationsBetweenPlayerAndBlock.Neutral) { SetWeaponsShooting(true); } else { SetWeaponsShooting(false); } // MyAPIGateway.Utilities.ShowNotification("Hit: " + info.Type, 2000, MyFontEnum.DarkBlue); // MyAPIGateway.Utilities.ShowNotification("Relation: " + info.Relationship, 2000, MyFontEnum.DarkBlue); } else { SetWeaponsShooting(true); } } else { SetWeaponsShooting(false); StopAllRotors(); } }
public void PerformScan() { try { if (RadarBlock == null || !IsWorking() || !RadarBlock.IsVisible()) { return; } //DetectedEntities is cleared in UpdateBeforeSim10 if (OwnerPlayer.Controller.ControlledEntity is IMyCharacter && OwnerPlayer.GetPosition().DistanceTo(Position) <= RadarCore.GuaranteedDetectionRange) { DetectedEntities.Add(MyDetectedEntityInfoHelper.Create(OwnerPlayer.Controller.ControlledEntity as MyEntity, RadarBlock.OwnerId, null)); } BoundingSphereD RadarSphere = new BoundingSphereD(Position, RadarCore.MaxVisibilityRange); List <IMyEntity> EntitiesAround = new List <IMyEntity>(); EntitiesAround = MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref RadarSphere); HashSet <IMyCubeGrid> Grids = new HashSet <IMyCubeGrid>(); HashSet <IMyEntity> OtherEntities = new HashSet <IMyEntity>(); EntitiesAround.SortByType(Grids, OtherEntities); foreach (IMyCubeGrid Grid in Grids) { try { ScanGrid(Grid.AsRadarable()); } catch { } } foreach (IMyEntity Entity in OtherEntities) { if (Entity.IsOfType <IMyCubeGrid>()) { continue; } try { ScanEntity(Entity); } catch { } } if (RadarCore.Debug) { TestNote.Text = $"{RadarBlock.CustomName}: detected {DetectedEntities.Count} objects; RadarPower: {Math.Round(RadarPower / 1000)} MW"; } } catch { } }
void TryAddRelay(MyDataReceiver Receiver) { if (Receiver == null) { return; } if (Receiver == Antenna) { return; } if (RelayedReceivers.Contains(Receiver)) { return; } IMyTerminalBlock ReceiverBlock = (Receiver.Entity as IMyTerminalBlock); if (ReceiverBlock != null) { if (!ReceiverBlock.HasPlayerAccess(AntennaBlock.OwnerId)) { return; } if (!ReceiverBlock.IsWorking) { return; } IMyCubeGrid Grid = ReceiverBlock.CubeGrid.GetTopMostParent() as IMyCubeGrid; if (Grid == null || Grid == AntennaGrid) { return; } RelayedReceivers.Add(Receiver); if (!RelayedGrids.Any(x => x.EntityId == Grid.EntityId)) { RelayedGrids.Add(Grid); RelayedGridsIngame.Add(MyDetectedEntityInfoHelper.Create(Grid as MyEntity, AntennaBlock.OwnerId)); } } else { IMyCharacter Char = Receiver.Entity as IMyCharacter; if (Char != null) { if (Char.IsPlayer && !Char.IsDead && AntennaBlock.HasPlayerAccess(Char.ControllerInfo.ControllingIdentityId)) { RelayedChars.Add(Char); } } } }
//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); }
public override void UpdateAfterSimulation10() { base.UpdateAfterSimulation10(); if (!Sync.IsServer || !IsWorking) { return; } if (!ResourceSink.IsPoweredByType(MyResourceDistributorComponent.ElectricityId)) { if (ResourceSink.IsPowerAvailable(MyResourceDistributorComponent.ElectricityId, BlockDefinition.RequiredPowerInput)) { float origInput = ResourceSink.RequiredInputByType(MyResourceDistributorComponent.ElectricityId); ResourceSink.SetRequiredInputByType(MyResourceDistributorComponent.ElectricityId, 0); ResourceSink.SetRequiredInputByType(MyResourceDistributorComponent.ElectricityId, origInput); } else { return; } } var rotation1 = Quaternion.CreateFromForwardUp(WorldMatrix.Forward, WorldMatrix.Up); var position1 = PositionComp.GetPosition() + Vector3D.Transform(PositionComp.LocalVolume.Center + (m_fieldMax.Value + m_fieldMin.Value) * 0.5f, rotation1); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Recreate Field"); if (m_recreateField) { m_recreateField = false; m_fieldShape.RemoveReference(); m_fieldShape = GetHkShape(); ResourceSink.Update(); } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); var boundingBox = new BoundingBoxD(m_fieldMin.Value, m_fieldMax.Value).Translate(PositionComp.LocalVolume.Center).TransformFast(WorldMatrix.GetOrientation()).Translate(PositionComp.GetPosition()); m_potentialPenetrations.Clear(); MyGamePruningStructure.GetTopMostEntitiesInBox(ref boundingBox, m_potentialPenetrations); m_potentialVoxelPenetrations.Clear(); MyGamePruningStructure.GetAllVoxelMapsInBox(ref boundingBox, m_potentialVoxelPenetrations);//disabled until heightmap queries are finished VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Sensor Physics"); LastDetectedEntity = null; bool empty = true; m_detectedEntities.Clear(); //foreach (var entity in m_potentialPenetrations) Parallel.ForEach(m_potentialPenetrations, entity => { if (entity is MyVoxelBase) { //voxels are handled in different loop (becaose of planets) return; } if (ShouldDetect(entity)) { Quaternion rotation2; Vector3 posDiff; HkShape?shape2; if (GetPropertiesFromEntity(entity, ref position1, out rotation2, out posDiff, out shape2)) { if (entity.GetPhysicsBody().HavokWorld.IsPenetratingShapeShape(m_fieldShape, ref Vector3.Zero, ref rotation1, shape2.Value, ref posDiff, ref rotation2)) { if (LastDetectedEntity == null) { LastDetectedEntity = entity; } empty = false; //entities.Add(entity); var inf = MyDetectedEntityInfoHelper.Create(entity, this.OwnerId); m_detectedEntities.Add(inf); } } } }); if (DetectAsteroids) { //foreach (var entity in m_potentialVoxelPenetrations) Parallel.ForEach(m_potentialVoxelPenetrations, entity => { var voxel = entity as MyVoxelPhysics; if (voxel != null) { Vector3D localPositionMin, localPositionMax; MyVoxelCoordSystems.WorldPositionToLocalPosition(boundingBox.Min, voxel.PositionComp.WorldMatrix, voxel.PositionComp.WorldMatrixInvScaled, voxel.SizeInMetresHalf, out localPositionMin); MyVoxelCoordSystems.WorldPositionToLocalPosition(boundingBox.Max, voxel.PositionComp.WorldMatrix, voxel.PositionComp.WorldMatrixInvScaled, voxel.SizeInMetresHalf, out localPositionMax); var aabb = new BoundingBox(localPositionMin, localPositionMax); aabb.Translate(voxel.StorageMin); if (voxel.Storage.Intersect(ref aabb) != ContainmentType.Disjoint) { if (LastDetectedEntity == null) { LastDetectedEntity = entity; } empty = false; //entities.Add(entity); var inf = MyDetectedEntityInfoHelper.Create(entity, this.OwnerId); m_detectedEntities.Add(inf); } } else { Quaternion rotation2; Vector3 posDiff; HkShape?shape2; if (GetPropertiesFromEntity(entity, ref position1, out rotation2, out posDiff, out shape2)) { if (entity.GetPhysicsBody().HavokWorld.IsPenetratingShapeShape(m_fieldShape, ref Vector3.Zero, ref rotation1, shape2.Value, ref posDiff, ref rotation2)) { if (LastDetectedEntity == null) { LastDetectedEntity = entity; } empty = false; //entities.Add(entity); var inf = MyDetectedEntityInfoHelper.Create(entity, this.OwnerId); m_detectedEntities.Add(inf); } } } }); } IsActive = !empty; m_potentialPenetrations.Clear(); m_potentialVoxelPenetrations.Clear(); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); }