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;
            }
        }
예제 #2
0
        public override void BeforeStart()
        {
            base.BeforeStart();

            // Make sure that the pirate identity exists
            if (m_piratesIdentityId != 0)
            {
                MyIdentity pirateIdentity = Sync.Players.TryGetIdentity(m_piratesIdentityId);
                Debug.Assert(pirateIdentity != null, "The pirate identity does not exist, although its ID was saved!");

                if (Sync.IsServer && pirateIdentity == null)
                {
                    Sync.Players.CreateNewIdentity(IDENTITY_NAME, m_piratesIdentityId, null);
                }
            }
            else
            {
                var identity = Sync.Players.CreateNewIdentity(IDENTITY_NAME);
                m_piratesIdentityId = identity.IdentityId;
            }

            if (!Sync.Players.IdentityIsNpc(m_piratesIdentityId))
            {
                Sync.Players.MarkIdentityAsNPC(m_piratesIdentityId);
            }

            // Make sure that all the drone entities exist
            foreach (var drone in m_droneInfos)
            {
                MyEntity entity;
                MyEntities.TryGetEntityById(drone.Key, out entity);
                if (entity == null)
                {
                    DroneInfo.Deallocate(drone.Value);
                    m_droneInfos.Remove(drone.Key);
                }
                else
                {
                    if (!MySession.Static.Settings.EnableDrones)
                    {
                        MyCubeGrid grid   = entity as MyCubeGrid;
                        var        remote = entity as MyRemoteControl;
                        if (grid == null)
                        {
                            grid = remote.CubeGrid;
                        }

                        UnregisterDrone(entity, immediate: false);
                        grid.SyncObject.SendCloseRequest();
                    }
                    else
                    {
                        RegisterDrone(drone.Value.AntennaEntityId, entity, immediate: false);
                    }
                }
            }
            m_droneInfos.ApplyRemovals();
        }
        private void UpdateDroneDespawning()
        {
            foreach (var entry in m_droneInfos)
            {
                if (entry.Value.DespawnTime < MySandboxGame.TotalGamePlayTimeInMilliseconds)
                {
                    MyEntity droneEntity = null;
                    MyEntities.TryGetEntityById(entry.Key, out droneEntity);
                    Debug.Assert(droneEntity != null, "Could not find the drone entity to despawn!");

                    if (droneEntity != null)
                    {
                        MyCubeGrid grid   = droneEntity as MyCubeGrid;
                        var        remote = droneEntity as MyRemoteControl;
                        if (grid == null)
                        {
                            grid = remote.CubeGrid;
                        }

                        if (CanDespawn(grid, remote))
                        {
                            UnregisterDrone(droneEntity, immediate: false);
                            grid.SyncObject.SendCloseRequest();
                        }
                        else
                        {
                            entry.Value.DespawnTime = MySandboxGame.TotalGamePlayTimeInMilliseconds + DRONE_DESPAWN_RETRY;
                        }
                    }
                    else
                    {
                        DroneInfo.Deallocate(entry.Value);
                        m_droneInfos.Remove(entry.Key);
                    }
                }
            }

            m_droneInfos.ApplyChanges();
        }
        public override void BeforeStart()
        {
            base.BeforeStart();

            MyFaction pirateFaction = MySession.Static.Factions.TryGetFactionByTag(PIRATE_FACTION_TAG);

            Debug.Assert(pirateFaction != null, "No pirate faction in the world. Pirate antenan needs it.");

            if (pirateFaction != null)
            {
                // Make sure that the pirate identity exists
                if (m_piratesIdentityId != 0)
                {
                    if (Sync.IsServer)
                    {
                        MyIdentity pirateIdentity = Sync.Players.TryGetIdentity(m_piratesIdentityId);
                        Debug.Assert(pirateIdentity != null, "The pirate identity does not exist, although its ID was saved!");

                        if (pirateIdentity == null)
                        {
                            Sync.Players.CreateNewIdentity(IDENTITY_NAME, m_piratesIdentityId, null);
                        }

                        pirateIdentity.LastLoginTime = DateTime.Now;

                        // Check if he is already in a faction.
                        MyFaction oldPirateFaction = MySession.Static.Factions.GetPlayerFaction(m_piratesIdentityId);
                        if (oldPirateFaction == null)
                        {
                            MyFactionCollection.SendJoinRequest(pirateFaction.FactionId, m_piratesIdentityId);
                        }
                    }
                }
                else
                {
                    m_piratesIdentityId = pirateFaction.FounderId;
                }

                if (!Sync.Players.IdentityIsNpc(m_piratesIdentityId))
                {
                    Sync.Players.MarkIdentityAsNPC(m_piratesIdentityId);
                }
            }

            // Make sure that all the drone entities exist
            foreach (var drone in m_droneInfos)
            {
                MyEntity entity;
                MyEntities.TryGetEntityById(drone.Key, out entity);
                if (entity == null)
                {
                    DroneInfo.Deallocate(drone.Value);
                    m_droneInfos.Remove(drone.Key);
                }
                else
                {
                    if (!MySession.Static.Settings.EnableDrones)
                    {
                        MyCubeGrid grid   = entity as MyCubeGrid;
                        var        remote = entity as MyRemoteControl;
                        if (grid == null)
                        {
                            grid = remote.CubeGrid;
                        }

                        UnregisterDrone(entity, immediate: false);
                        grid.Close();
                        //grid.SyncObject.SendCloseRequest();
                    }
                    else
                    {
                        RegisterDrone(drone.Value.AntennaEntityId, entity, immediate: false);
                    }
                }
            }
            m_droneInfos.ApplyRemovals();
        }