private void RegisterDrone(long antennaEntityId, MyEntity droneMainEntity, bool immediate = true) { var newInfo = DroneInfo.Allocate(antennaEntityId, MySandboxGame.TotalGamePlayTimeInMilliseconds + DRONE_DESPAWN_TIMER); m_droneInfos.Add(droneMainEntity.EntityId, newInfo, immediate: immediate); droneMainEntity.OnClosing += DroneMainEntityOnClosing; PirateAntennaInfo antennaInfo = null; if (!m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo)) { MyEntity entity; if (MyEntities.TryGetEntityById(antennaEntityId, out entity)) { var antenna = entity as MyRadioAntenna; if (antenna != null) { antenna.UpdatePirateAntenna(); m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo); } } } if (antennaInfo != null) { antennaInfo.SpawnedDrones++; } Debug.Assert(antennaEntityId == 0 || antennaInfo != null, "Antenna info not present when registering a drone!"); var remote = droneMainEntity as MyRemoteControl; if (remote != null) { remote.OwnershipChanged += DroneRemoteOwnershipChanged; } }
private void UnregisterDrone(MyEntity entity, bool immediate = true) { long antennaEntityId = 0; DroneInfo info = null; Debug.Assert(m_droneInfos.ContainsKey(entity.EntityId), "Unregistering drone with inconsistend entity id"); m_droneInfos.TryGetValue(entity.EntityId, out info); if (info != null) { antennaEntityId = info.AntennaEntityId; DroneInfo.Deallocate(info); } m_droneInfos.Remove(entity.EntityId, immediate: immediate); PirateAntennaInfo antennaInfo = null; m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo); if (antennaInfo != null) { antennaInfo.SpawnedDrones--; Debug.Assert(antennaInfo.SpawnedDrones >= 0, "Inconsistence in registered drone counts!"); } entity.OnClosing -= DroneMainEntityOnClosing; var remote = entity as MyRemoteControl; if (remote != null) { remote.OwnershipChanged -= DroneRemoteOwnershipChanged; } }
public static void UpdatePirateAntenna(long antennaEntityId, bool remove, bool activeState, StringBuilder antennaName) { Debug.Assert(Sync.IsServer, "Pirate antennas can only be registered on the server"); // This can happen while unloading the game, because this component unloads before entities. if (m_pirateAntennas == null) { return; } if (remove == true) { m_pirateAntennas.Remove(antennaEntityId, immediate: !m_iteratingAntennas); return; } string antennaNameStr = antennaName.ToString(); PirateAntennaInfo antennaInfo = null; if (!m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo)) { MyPirateAntennaDefinition antennaDef = null; if (m_definitionsByAntennaName.TryGetValue(antennaNameStr, out antennaDef)) { antennaInfo = PirateAntennaInfo.Allocate(antennaDef); antennaInfo.IsActive = activeState; m_pirateAntennas.Add(antennaEntityId, antennaInfo, immediate: !m_iteratingAntennas); } } else { if (antennaInfo.AntennaDefinition.Name != antennaNameStr) { MyPirateAntennaDefinition antennaDef = null; if (!m_definitionsByAntennaName.TryGetValue(antennaNameStr, out antennaDef)) { PirateAntennaInfo.Deallocate(antennaInfo); m_pirateAntennas.Remove(antennaEntityId, immediate: !m_iteratingAntennas); } else { antennaInfo.Reset(antennaDef); antennaInfo.IsActive = activeState; } } else { antennaInfo.IsActive = activeState; } } }
public static PirateAntennaInfo Allocate(MyPirateAntennaDefinition antennaDef) { PirateAntennaInfo info = null; if (m_pool.Count == 0) { info = new PirateAntennaInfo(); } else { info = m_pool[m_pool.Count - 1]; m_pool.RemoveAt(m_pool.Count - 1); } info.Reset(antennaDef); return info; }
public static PirateAntennaInfo Allocate(MyPirateAntennaDefinition antennaDef) { PirateAntennaInfo info = null; if (m_pool.Count == 0) { info = new PirateAntennaInfo(); } else { info = m_pool[m_pool.Count - 1]; m_pool.RemoveAt(m_pool.Count - 1); } info.Reset(antennaDef); return(info); }
public static void Deallocate(PirateAntennaInfo toDeallocate) { toDeallocate.AntennaDefinition = null; toDeallocate.SpawnPositionsIndexes = null; m_pool.Add(toDeallocate); }
private void UpdateDroneSpawning() { int currentTime = MySandboxGame.TotalGamePlayTimeInMilliseconds; m_iteratingAntennas = true; foreach (var antennaEntry in m_pirateAntennas) { PirateAntennaInfo antennaInfo = antennaEntry.Value; if (!antennaInfo.IsActive) { continue; } if (currentTime - antennaInfo.LastGenerationGameTime <= antennaInfo.AntennaDefinition.SpawnTimeMs) { continue; } MyRadioAntenna antenna = null; MyEntities.TryGetEntityById(antennaEntry.Key, out antenna); Debug.Assert(antenna != null, "Could not find antenna for spawning enemy drones!"); if (antennaInfo.AntennaDefinition.SpawnGroupSampler == null) { return; } var spawnGroup = antennaInfo.AntennaDefinition.SpawnGroupSampler.Sample(); Debug.Assert(spawnGroup != null, "Could not find spawnGroup for spawning enemy drones!"); if ( !MySession.Static.Settings.EnableDrones || antennaInfo.SpawnedDrones >= antennaInfo.AntennaDefinition.MaxDrones || antenna == null || spawnGroup == null || (m_droneInfos.Reader.Count() >= MySession.Static.Settings.MaxDrones) ) { antennaInfo.LastGenerationGameTime = currentTime; continue; } spawnGroup.ReloadPrefabs(); BoundingSphereD antennaSphere = new BoundingSphereD(antenna.WorldMatrix.Translation, antenna.GetRadius()); var players = MySession.Static.Players.GetOnlinePlayers(); bool successfulSpawn = false; foreach (var player in players) { if (antennaSphere.Contains(player.GetPosition()) == ContainmentType.Contains) { Vector3D?spawnPosition = null; for (int i = 0; i < 10; ++i) { Vector3D position = antenna.WorldMatrix.Translation + MyUtils.GetRandomVector3Normalized() * antennaInfo.AntennaDefinition.SpawnDistance; spawnPosition = MyEntities.FindFreePlace(position, spawnGroup.SpawnRadius); if (spawnPosition.HasValue) { break; } } successfulSpawn = SpawnDrone(antenna, antenna.OwnerId, spawnPosition.Value, spawnGroup); break; } } // Don't reschedule if there was no player inside if (successfulSpawn) { antennaInfo.LastGenerationGameTime = currentTime; } } m_pirateAntennas.ApplyChanges(); m_iteratingAntennas = false; }
public static void Deallocate(PirateAntennaInfo toDeallocate) { toDeallocate.AntennaDefinition = null; m_pool.Add(toDeallocate); }
public static void Deallocate(PirateAntennaInfo toDeallocate) { toDeallocate.AntennaDefinition = null; m_pool.Add(toDeallocate); }