Made further accuracy improvements to the timing of the dedicated server

TODO: make the dedicated server load all of the settings from
serversettings.xml
This commit is contained in:
juanjp600
2018-01-08 18:11:07 -03:00
parent 1aa654a5a3
commit 539845665a
2 changed files with 37 additions and 25 deletions
+17 -8
View File
@@ -3,6 +3,7 @@ using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Xml.Linq; using System.Xml.Linq;
@@ -133,24 +134,32 @@ namespace Barotrauma
Timing.Accumulator = 0.0; Timing.Accumulator = 0.0;
DateTime prevTime = DateTime.Now; double frequency = (double)Stopwatch.Frequency;
if (frequency <= 1500)
{
DebugConsole.NewMessage("WARNING: Stopwatch frequency under 1500 ticks per second. Expect significant syncing accuracy issues.", Color.Yellow);
}
Stopwatch stopwatch = Stopwatch.StartNew();
long prevTicks = stopwatch.ElapsedTicks;
while (ShouldRun) while (ShouldRun)
{ {
Timing.Accumulator += ((float)(DateTime.Now.Subtract(prevTime).Milliseconds) / 1000.0)/Timing.Step; long currTicks = stopwatch.ElapsedTicks;
prevTime = DateTime.Now; Timing.Accumulator += (double)(currTicks - prevTicks) / frequency;
while (Timing.Accumulator>0.0) prevTicks = currTicks;
while (Timing.Accumulator>=Timing.Step)
{ {
DebugConsole.Update(); DebugConsole.Update();
if (Screen.Selected != null) Screen.Selected.Update((float)Timing.Step); if (Screen.Selected != null) Screen.Selected.Update((float)Timing.Step);
Server.Update((float)Timing.Step); Server.Update((float)Timing.Step);
CoroutineManager.Update((float)Timing.Step, (float)Timing.Step); CoroutineManager.Update((float)Timing.Step, (float)Timing.Step);
Timing.Accumulator -= 1.0; Timing.Accumulator -= Timing.Step;
} }
int frameTime = DateTime.Now.Subtract(prevTime).Milliseconds; int frameTime = (int)(((double)(stopwatch.ElapsedTicks - prevTicks) / frequency)*1000.0);
Thread.Sleep(Math.Max((int)(Timing.Step * 1000.0) - frameTime/2,0)); Thread.Sleep(Math.Max(((int)(Timing.Step * 1000.0) - frameTime)/2,0));
} }
stopwatch.Stop();
CloseServer(); CloseServer();
@@ -168,7 +177,7 @@ namespace Barotrauma
} }
} }
public CoroutineHandle ShowLoading(IEnumerable<object> loader, bool waitKeyHit = true) public CoroutineHandle ShowLoadings(IEnumerable<object> loader, bool waitKeyHit = true)
{ {
return CoroutineManager.StartCoroutine(loader); return CoroutineManager.StartCoroutine(loader);
} }
@@ -234,7 +234,7 @@ namespace Barotrauma.Networking
private set; private set;
} }
[Serialize(false,true)] [Serialize(false, true)]
public bool KarmaEnabled public bool KarmaEnabled
{ {
get; get;
@@ -325,6 +325,9 @@ namespace Barotrauma.Networking
#endif #endif
) )
{ {
#if SERVER
GameMain.NetLobbyScreen.ServerName = doc.Root.GetAttributeString("name", "");
#endif
GameMain.NetLobbyScreen.ServerMessageText = doc.Root.GetAttributeString("ServerMessage", ""); GameMain.NetLobbyScreen.ServerMessageText = doc.Root.GetAttributeString("ServerMessage", "");
} }