Esempio n. 1
0
        public void ClearEntities()
        {
            foreach (var entity in Entities.ToArray())
            {
                Remove(entity.Key);
            }

            foreach (var blockEntity in BlockEntities.ToArray())
            {
                BlockEntities.TryRemove(blockEntity.Key, out _);
            }
        }
Esempio n. 2
0
        private void WorldTick(object sender)
        {
            if (_tickTimer.ElapsedMilliseconds < 40)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Warn($"World tick came too fast: {_tickTimer.ElapsedMilliseconds} ms");
                }
                return;
            }

            if (Log.IsDebugEnabled && _tickTimer.ElapsedMilliseconds >= 100)
            {
                Log.Error($"Time between World tick too too long: {_tickTimer.ElapsedMilliseconds} ms");
            }

            _tickTimer.Restart();
            try
            {
                TickTime++;

                if (IsWorldTimeStarted)
                {
                    CurrentWorldTime += 1.25;
                }
                if (CurrentWorldTime > _worldDayCycleTime)
                {
                    CurrentWorldTime = 0;
                }
                if (TickTime % 100 == 0)
                {
                    //McpeSetTime message = McpeSetTime.CreateObject();
                    //message.time = (int)CurrentWorldTime;
                    //message.started = (byte)(IsWorldTimeStarted ? 0x80 : 0x00);

                    //RelayBroadcast(players, message);
                }

                // Block updates
                foreach (KeyValuePair <BlockCoordinates, long> blockEvent in BlockWithTicks)
                {
                    Log.Debug($"Have block tick for {blockEvent.Key}");
                    if (blockEvent.Value <= TickTime)
                    {
                        GetBlock(blockEvent.Key).OnTick(this);
                        long value;
                        BlockWithTicks.TryRemove(blockEvent.Key, out value);
                    }
                }

                // Block entity updates
                foreach (BlockEntity blockEntity in BlockEntities.ToArray())
                {
                    blockEntity.OnTick(this);
                }

                // Entity updates
                Entity[] entities = Entities.Values.ToArray();
                foreach (Entity entity in entities)
                {
                    entity.OnTick();
                }

                Player[] players = GetSpawnedPlayers();
                PlayerCount = players.Length;

                // Player tick
                foreach (var player in players)
                {
                    if (player.IsSpawned)
                    {
                        player.OnTick();
                    }
                }

                // Send player movements
                //if (TickTime % 2 == 0)
                BroadCastMovement(players, entities);

                //if (TickTime%100 == 0) // Every 5 seconds
                //{
                //	var staledPlayers = GetStaledPlayers(players);
                //	foreach (var p in staledPlayers)
                //	{
                //		ThreadPool.QueueUserWorkItem(delegate(object state)
                //		{
                //			Player player = (Player) state;
                //			player.Disconnect("Staled.");
                //		}, p);
                //	}
                //}

                if (Log.IsDebugEnabled && _tickTimer.ElapsedMilliseconds >= 50)
                {
                    Log.Error($"World tick too too long: {_tickTimer.ElapsedMilliseconds} ms");
                }
            }
            catch (Exception e)
            {
                Log.Error("World ticking", e);
            }
            finally
            {
                LastTickProcessingTime    = _tickTimer.ElapsedMilliseconds;
                AvarageTickProcessingTime = ((AvarageTickProcessingTime * 9) + _tickTimer.ElapsedMilliseconds) / 10L;
            }
        }
Esempio n. 3
0
        private void WorldTick(object sender)
        {
            if (!Monitor.TryEnter(_tickSync))
            {
                return;
            }

            _tickTimer.Restart();
            try
            {
                TickTime++;

                if (IsWorldTimeStarted)
                {
                    CurrentWorldTime += 1.25;
                }
                if (CurrentWorldTime > _worldDayCycleTime)
                {
                    CurrentWorldTime = 0;
                }
                if (TickTime % 100 == 0)
                {
                    //McpeSetTime message = McpeSetTime.CreateObject();
                    //message.time = (int)CurrentWorldTime;
                    //message.started = (byte)(IsWorldTimeStarted ? 0x80 : 0x00);

                    //RelayBroadcast(players, message);
                }

                // Block updates
                foreach (KeyValuePair <BlockCoordinates, long> blockEvent in BlockWithTicks)
                {
                    if (blockEvent.Value <= TickTime)
                    {
                        GetBlock(blockEvent.Key).OnTick(this);
                        long value;
                        BlockWithTicks.TryRemove(blockEvent.Key, out value);
                    }
                }

                // Block entity updates
                foreach (BlockEntity blockEntity in BlockEntities.ToArray())
                {
                    blockEntity.OnTick(this);
                }

                // Entity updates
                Entity[] entities = Entities.ToArray();
                foreach (Entity entity in entities)
                {
                    entity.OnTick();
                }

                Player[] players = GetSpawnedPlayers();
                PlayerCount = players.Length;

                // Player tick
                foreach (var player in players)
                {
                    if (player.IsSpawned)
                    {
                        player.OnTick();
                    }
                }

                // Send player movements
                BroadCastMovement(players, entities);

                if (TickTime % 100 == 0)               // Every 5 seconds
                {
                    var staledPlayers = GetStaledPlayers(players);
                    foreach (var p in staledPlayers)
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object state)
                        {
                            Player player = (Player)state;
                            player.Disconnect("Staled.");
                        }, p);
                    }
                }
            }
            finally
            {
                LastTickProcessingTime    = _tickTimer.ElapsedMilliseconds;
                AvarageTickProcessingTime = ((AvarageTickProcessingTime * 9) + _tickTimer.ElapsedMilliseconds) / 10L;

                Monitor.Exit(_tickSync);
            }
        }
Esempio n. 4
0
File: Level.cs Progetto: wgaox/MiNET
        private void WorldTick(object sender)
        {
            if (!Monitor.TryEnter(_tickSync))
            {
                return;
            }

            _tickTimer.Restart();
            try
            {
                TickTime++;

                Player[] players = GetSpawnedPlayers();

                if (IsWorldTimeStarted)
                {
                    CurrentWorldTime += 1.25;
                }
                if (CurrentWorldTime > _worldDayCycleTime)
                {
                    CurrentWorldTime = 0;
                }
                if (TickTime % 100 == 0)
                {
                    //McpeSetTime message = McpeSetTime.CreateObject();
                    //message.time = (int) CurrentWorldTime;
                    //message.started = (byte) (IsWorldTimeStarted ? 0x80 : 0x00);

                    //RelayBroadcast(players, message);
                }

                // Block updates
                foreach (KeyValuePair <BlockCoordinates, long> blockEvent in BlockWithTicks.ToArray())
                {
                    if (blockEvent.Value <= TickTime)
                    {
                        GetBlock(blockEvent.Key).OnTick(this);
                        long value;
                        BlockWithTicks.TryRemove(blockEvent.Key, out value);
                    }
                }

                // Block entity updates
                foreach (BlockEntity blockEntity in BlockEntities.ToArray())
                {
                    blockEntity.OnTick(this);
                }

                // Entity updates
                foreach (Entity entity in Entities.ToArray())
                {
                    entity.OnTick();
                }

                // Player tick
                foreach (Player player in players)
                {
                    player.OnTick();
                }

                // Send player movements
                Player[] updatedPlayers = GetUpdatedPlayers(players);
                BroadCastMovement(players, updatedPlayers);
            }
            finally
            {
                LastTickProcessingTime = _tickTimer.ElapsedMilliseconds;
                Monitor.Exit(_tickSync);
            }
        }