Exemplo n.º 1
0
 public void RegisterComponent(MySessionComponentBase component, MyUpdateOrder updateOrder, int priority)
 {
     // TODO: Better handling of component overrides
     //if(m_sessionComponents.ContainsKey(component.ComponentType))
     m_sessionComponents[component.ComponentType] = component;
     component.Session = this;
     AddComponentForUpdate(updateOrder, component);
     m_sessionComponents.ApplyChanges();
 }
Exemplo n.º 2
0
        public void LoadDataComponents()
        {
            RaiseOnLoading();

            if (SyncLayer.AutoRegisterGameEvents)
            {
                SyncLayer.RegisterGameEvents();
            }

            Sync.Clients.SetLocalSteamId(Sync.MyId, createLocalClient: !(MyMultiplayer.Static is MyMultiplayerClient));
            Sync.Players.RegisterEvents();

            SetAsNotReady();

            HashSet <MySessionComponentBase> processedComponents = new HashSet <MySessionComponentBase>();

            do
            {
                m_sessionComponents.ApplyChanges();
                foreach (var comp in m_sessionComponents.Values)
                {
                    if (processedComponents.Contains(comp))
                    {
                        continue;
                    }
                    LoadComponent(comp);
                    processedComponents.Add(comp);
                }
            } while (m_sessionComponents.HasChanges());
        }
Exemplo n.º 3
0
        internal void SyncClientEwarBlocks()
        {
            foreach (var ewarPair in CurrentClientEwaredCubes)
            {
                BlockState state;
                MyEntity   ent;
                var        entId = ewarPair.Key;
                if (MyEntities.TryGetEntityById(entId, out ent))
                {
                    var cube = (MyCubeBlock)ent;
                    var func = (IMyFunctionalBlock)cube;
                    func.RefreshCustomInfo();

                    if (!_activeEwarCubes.ContainsKey(entId))
                    {
                        state = new BlockState {
                            FunctBlock = func, FirstState = func.Enabled, Endtick = Tick + ewarPair.Value.EndTick, Session = this
                        };
                        _activeEwarCubes[entId] = state;
                        ActivateClientEwarState(ref state);
                    }
                }
                else if (_activeEwarCubes.TryGetValue(entId, out state))
                {
                    DeactivateClientEwarState(ref state);
                    _activeEwarCubes.Remove(entId);
                }

                ClientEwarStale = false;
            }

            _activeEwarCubes.ApplyChanges();
            foreach (var activeEwar in _activeEwarCubes)
            {
                if (!CurrentClientEwaredCubes.ContainsKey(activeEwar.Key))
                {
                    var state = activeEwar.Value;
                    DeactivateClientEwarState(ref state);
                    _activeEwarCubes.Remove(activeEwar.Key);
                }
            }
            _activeEwarCubes.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();
        }
        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;
        }
Exemplo n.º 6
0
        public void Update()
        {
            ProfilerShort.Begin("MyToolbar.Update");
            if (MySession.Static == null)
            {
                ProfilerShort.End();
                return;
            }

            long playerID = GetControllerPlayerID();

            for (int i = 0; i < m_items.Length; i++)
            {
                if (m_items[i] != null)
                {
                    var updated = m_items[i].Update(Owner, playerID);
                    if (updated == MyToolbarItem.ChangeInfo.None)
                    {
                        continue;
                    }

                    ToolbarItemUpdated(i, updated);
                }
            }

            int?previousSelectedSlot = m_selectedSlot;

            if (!StagedSelectedSlot.HasValue)
            {
                m_selectedSlot = null;
                for (int i = 0; i < SlotCount; i++)
                {
                    if (m_items[SlotToIndex(i)] != null)
                    {
                        if (m_items[SlotToIndex(i)].WantsToBeSelected)
                        {
                            m_selectedSlot = i;
                        }
                    }
                }
            }
            else
            {
                if (!m_selectedSlot.HasValue || m_selectedSlot.Value != StagedSelectedSlot.Value)
                {
                    m_selectedSlot = StagedSelectedSlot;
                    var item = m_items[SlotToIndex(m_selectedSlot.Value)];
                    if (item != null && !item.ActivateOnClick)
                    {
                        ActivateItemAtSlot(m_selectedSlot.Value);
                        m_activateSelectedItem = false;
                    }
                    else
                    {
                        m_activateSelectedItem = true;
                        Unselect();
                    }
                }
            }

            if (previousSelectedSlot != m_selectedSlot && SelectedSlotChanged != null)
            {
                SelectedSlotChanged(this, new SlotArgs()
                {
                    SlotNumber = m_selectedSlot
                });
            }

            EnabledOverride = null;

            if (m_extensions != null)
            {
                foreach (var extension in m_extensions.Values)
                {
                    extension.Update();
                }
                m_extensions.ApplyChanges();
            }

            ProfilerShort.End();
        }
Exemplo n.º 7
0
        public override void UpdateBeforeSimulation()
        {
            {
                m_trackedEntities.ApplyChanges();
                foreach (var module in m_modulesToAdd)
                {
                    m_modules.Add(module);
                }

                foreach (var module in m_modules)
                {
                    var needsFullUpdate = m_modulesToAdd.Remove(module);

                    if (!module.RunOnClients && !Utilities.IsDecisionMaker)
                    {
                        return;
                    }
                    m_stopwatch.Restart();
                    foreach (var entity in m_trackedEntities.Values)
                    {
                        if (!entity.ShouldGenerate())
                        {
                            continue;
                        }
                        foreach (var result in module.Generate(entity.CurrentView,
                                                               needsFullUpdate ? null : (BoundingSphereD?)entity.PreviousView))
                        {
                            AddToTree(result);
                        }
                    }

                    var elapsed = m_stopwatch.Elapsed;
                    if (elapsed > TolerableLag)
                    {
                        Log(MyLogSeverity.Warning, "Module {0} took {1} to generate", module.GetType().Name, elapsed);
                    }
                }

                foreach (var entity in m_trackedEntities.Values)
                {
                    if (entity.ShouldGenerate())
                    {
                        m_dirtyVolumes.Enqueue(entity.PreviousView);
                        entity.UpdatePrevious();
                    }
                }
            }

            if (m_dirtyVolumes.Count == 0)
            {
                return;
            }
            var dirtyObjectList = m_objectListPool.Get();

            try
            {
                // Query tree for objects in volume.
                dirtyObjectList.Clear();
                while (m_dirtyVolumes.Count > 0)
                {
                    var volume = m_dirtyVolumes.Dequeue();
                    m_tree.OverlapAllBoundingSphere(ref volume, dirtyObjectList, false);
                }

                // Remove those not included by another entity
                foreach (var t in dirtyObjectList)
                {
                    bool any = false;
                    foreach (TrackedEntity entity in m_trackedEntities.Values)
                    {
                        if (t.m_boundingBox.Intersects(entity.CurrentView))
                        {
                            any = true;
                            break;
                        }
                    }

                    if (!any)
                    {
                        t.RaiseRemoved();
                    }
                }
            }
            finally
            {
                m_objectListPool.Return(dirtyObjectList);
            }
        }