Example #1
0
        void PhysicsLoop()
        {
            int wait = Config.PhysicsSpeed;

            while (true)
            {
                try {
                    if (PhysicsPaused)
                    {
                        if (physics == 0)
                        {
                            break;
                        }
                        Thread.Sleep(500); continue;
                    }

                    if (wait > 0)
                    {
                        Thread.Sleep(wait);
                    }
                    if (physics == 0)
                    {
                        break;
                    }

                    // No block calculations in this tick
                    if (ListCheck.Count == 0)
                    {
                        lastCheck = 0;
                        wait      = Config.PhysicsSpeed;
                        continue;
                    }

                    DateTime tickStart = default(DateTime);
                    try {
                        lock (physTickLock) {
                            tickStart = DateTime.UtcNow;
                            PhysicsTick();
                        }
                    } catch (Exception ex) {
                        Logger.LogError("Error in physics tick", ex);
                    }

                    // Measure how long this physics tick took to execute
                    TimeSpan elapsed = DateTime.UtcNow - tickStart;
                    wait = Config.PhysicsSpeed - (int)elapsed.TotalMilliseconds;

                    // Check if tick took too long to execute (server is falling behind)
                    if (wait < (int)(-Config.PhysicsOverload * 0.75f))
                    {
                        if (wait < -Config.PhysicsOverload)
                        {
                            if (!Server.Config.PhysicsRestart)
                            {
                                SetPhysics(0);
                            }
                            ClearPhysics();
                            Chat.MessageGlobal("Physics shutdown on {0}", ColoredName);

                            Logger.Log(LogType.Warning, "Physics shutdown on " + name);
                            OnPhysicsStateChangedEvent.Call(this, PhysicsState.Stopped);
                            wait = Config.PhysicsSpeed;
                        }
                        else
                        {
                            Message("Physics warning!");
                            Logger.Log(LogType.Warning, "Physics warning on " + name);
                            OnPhysicsStateChangedEvent.Call(this, PhysicsState.Warning);
                        }
                    }
                } catch {
                    wait = Config.PhysicsSpeed;
                }
            }

            lastCheck         = 0;
            physThreadStarted = false;
        }
Example #2
0
        void PhysicsLoop()
        {
            int wait = Config.PhysicsSpeed;

            while (true)
            {
                if (!PhysicsEnabled)
                {
                    Thread.Sleep(500); continue;
                }

                try {
                    if (wait > 0)
                    {
                        Thread.Sleep(wait);
                    }
                    if (physics == 0 || ListCheck.Count == 0)
                    {
                        lastCheck = 0;
                        wait      = Config.PhysicsSpeed;
                        if (physics == 0)
                        {
                            break;
                        }
                        continue;
                    }

                    DateTime start = DateTime.UtcNow;
                    if (physics > 0)
                    {
                        try {
                            lock (physStepLock)
                                CalcPhysics();
                        } catch (Exception ex) {
                            Logger.Log(LogType.Warning, "Level physics error");
                            Logger.LogError(ex);
                        }
                    }

                    TimeSpan delta = DateTime.UtcNow - start;
                    wait = Config.PhysicsSpeed - (int)delta.TotalMilliseconds;

                    if (wait < (int)(-Config.PhysicsOverload * 0.75f))
                    {
                        if (wait < -Config.PhysicsOverload)
                        {
                            if (!ServerConfig.PhysicsRestart)
                            {
                                setPhysics(0);
                            }
                            ClearPhysics();

                            Chat.MessageGlobal("Physics shutdown on {0}", ColoredName);
                            Logger.Log(LogType.Warning, "Physics shutdown on " + name);
                            OnPhysicsStateChangedEvent.Call(this, PhysicsState.Stopped);
                            wait = Config.PhysicsSpeed;
                        }
                        else
                        {
                            Player[] online = PlayerInfo.Online.Items;
                            foreach (Player p in online)
                            {
                                if (p.level != this)
                                {
                                    continue;
                                }
                                Player.Message(p, "Physics warning!");
                            }
                            Logger.Log(LogType.Warning, "Physics warning on " + name);
                            OnPhysicsStateChangedEvent.Call(this, PhysicsState.Warning);
                        }
                    }
                } catch {
                    wait = Config.PhysicsSpeed;
                }
            }
            physThreadStarted = false;
        }