3 Commits

Author SHA1 Message Date
5af8610c0c _some progerss? 2025-09-12 13:33:49 +03:00
95de571eb5 netcode progress, improved handling of missing frames 2025-09-07 18:45:24 +03:00
0d324c0ff0 _net prog 2025-06-21 20:59:56 +03:00
20 changed files with 574 additions and 155 deletions

View File

@@ -1,7 +1,7 @@
{
"ID": "82e58c9d462fba5a0df1a599417ff684",
"TypeName": "FlaxEngine.Prefab",
"EngineBuild": 6340,
"EngineBuild": 6704,
"Data": [
{
"ID": "a50f3639419a8306036ecfab7115e772",
@@ -17,7 +17,11 @@
"ID": "bc518e2f40ec06a4523d78b52809c668",
"TypeName": "Game.PlayerMovement",
"ParentID": "a50f3639419a8306036ecfab7115e772",
"V": {},
"V": {
"maskTest": {
"Mask": 1
}
},
"Enabled": false
},
{
@@ -162,8 +166,8 @@
}
},
"StaticFlags": 0,
"ContactOffset": 0.0,
"Material": "d99b8b6f40198beea4703dadd642150e",
"ContactOffset": 0.0,
"Radius": 16.0,
"Height": 52.0
},
@@ -173,8 +177,8 @@
"ParentID": "a50f3639419a8306036ecfab7115e772",
"Name": "BoxCollider",
"StaticFlags": 0,
"ContactOffset": 0.0,
"Material": "ffe0e84c49607480f67a3994a7fe11a8",
"ContactOffset": 0.0,
"Size": {
"X": 30.0,
"Y": 56.0,
@@ -211,9 +215,9 @@
"Name": "PlayerModel",
"Transform": {
"Scale": {
"X": 0.30000001192092898,
"X": 0.30000001192092896,
"Y": 0.5600000023841858,
"Z": 0.30000001192092898
"Z": 0.30000001192092896
}
},
"StaticFlags": 0,

View File

@@ -1,7 +1,7 @@
{
"ID": "af2e52554f7faed7b4937181dd22d166",
"TypeName": "FlaxEditor.Content.Settings.BuildSettings",
"EngineBuild": 6605,
"EngineBuild": 6705,
"Data": {
"OutputName": "${PROJECT_NAME}",
"MaxAssetsPerPackage": 4096,
@@ -27,18 +27,9 @@
"Name": "Preset 3",
"Targets": [
{
"Name": "Win64 Release",
"Output": "Output\\WindowsRelease",
"Platform": 2,
"Mode": 2,
"CustomDefines": null,
"PreBuildAction": null,
"PostBuildAction": null
},
{
"Name": "Win64 Development",
"Output": "Output\\WindowsDevelopment",
"Platform": 2,
"Name": "Windows 64bit",
"Output": "Output\\Win64",
"Platform": 13,
"Mode": 1,
"CustomDefines": null,
"PreBuildAction": null,

View File

@@ -1,13 +1,13 @@
{
"ID": "f94d5aae457aeba67033a8a4ca753214",
"TypeName": "FlaxEditor.Content.Settings.GraphicsSettings",
"EngineBuild": 6602,
"EngineBuild": 6705,
"Data": {
"UseVSync": false,
"AAQuality": 3,
"SSRQuality": 3,
"SSAOQuality": 0,
"VolumetricFogQuality": 3,
"SSAOQuality": 3,
"VolumetricFogQuality": 0,
"ShadowsQuality": 3,
"ShadowMapsQuality": 3,
"AllowCSMBlending": true,
@@ -19,6 +19,7 @@
"GenerateSDFOnModelImport": false,
"GIQuality": 3,
"GIProbesSpacing": 100.0,
"GICascadesBlending": false,
"GlobalSurfaceAtlasResolution": 4096,
"PostProcessSettings": {
"AO": {
@@ -49,8 +50,10 @@
"Enabled": false,
"Intensity": 1.0,
"Threshold": 3.0,
"BlurSigma": 4.0,
"Limit": 10.0
"ThresholdKnee": 0.5,
"Clamp": 3.0,
"BaseMix": 0.6,
"HighMix": 1.0
},
"ToneMapping": {
"OverrideFlags": 4,
@@ -198,7 +201,7 @@
"HistogramHighPercent": 98.0
},
"CameraArtifacts": {
"OverrideFlags": 9,
"OverrideFlags": 1,
"VignetteIntensity": 0.0,
"VignetteColor": {
"X": 0.0,
@@ -206,8 +209,8 @@
"Z": 0.001
},
"VignetteShapeFactor": 0.125,
"GrainAmount": 0.0,
"GrainParticleSize": 1.6,
"GrainAmount": 0.22999999,
"GrainParticleSize": 1.0,
"GrainSpeed": 1.0,
"ChromaticDistortion": 0.0,
"ScreenFadeColor": {
@@ -283,7 +286,11 @@
"TAA_JitterSpread": 0.75,
"TAA_Sharpness": 0.0,
"TAA_StationaryBlending": 0.95,
"TAA_MotionBlending": 0.4
"TAA_MotionBlending": 0.4,
"CAS_SharpeningAmount": 0.0,
"CAS_EdgeSharpening": 0.5,
"CAS_MinEdgeThreshold": 0.03,
"CAS_OverBlurLimit": 1.0
},
"PostFxMaterials": {
"Materials": null

View File

@@ -1,7 +1,7 @@
{
"ID": "8ec53dba4c238bfbea1d62922e612a4d",
"TypeName": "FlaxEditor.Content.Settings.InputSettings",
"EngineBuild": 6335,
"EngineBuild": 6705,
"Data": {
"ActionMappings": [
{
@@ -84,6 +84,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.0,
"Sensitivity": 0.11,
"Gravity": 1.0,
@@ -96,6 +98,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.0,
"Sensitivity": 0.11,
"Gravity": 1.0,
@@ -108,9 +112,11 @@
"Gamepad": 0,
"PositiveButton": 68,
"NegativeButton": 65,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.01,
"Sensitivity": 5.0,
"Gravity": 5.0,
"Sensitivity": 0.0,
"Gravity": 0.0,
"Scale": 1.0,
"Snap": true
},
@@ -120,9 +126,11 @@
"Gamepad": 0,
"PositiveButton": 87,
"NegativeButton": 83,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.01,
"Sensitivity": 5.0,
"Gravity": 5.0,
"Sensitivity": 0.0,
"Gravity": 0.0,
"Scale": 1.0,
"Snap": true
},
@@ -132,6 +140,8 @@
"Gamepad": 0,
"PositiveButton": 39,
"NegativeButton": 37,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.0,
"Sensitivity": 1.0,
"Gravity": 800.0,
@@ -144,6 +154,8 @@
"Gamepad": 0,
"PositiveButton": 38,
"NegativeButton": 40,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.0,
"Sensitivity": 1.0,
"Gravity": 800.0,
@@ -156,6 +168,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.19,
"Sensitivity": 1.0,
"Gravity": 1.0,
@@ -168,6 +182,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.19,
"Sensitivity": 1.0,
"Gravity": 1.0,
@@ -180,6 +196,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.19,
"Sensitivity": 1.0,
"Gravity": 1.0,
@@ -192,6 +210,8 @@
"Gamepad": 0,
"PositiveButton": 0,
"NegativeButton": 0,
"GamepadPositiveButton": 0,
"GamepadNegativeButton": 0,
"DeadZone": 0.19,
"Sensitivity": 1.0,
"Gravity": 1.0,

View File

@@ -1,11 +1,11 @@
{
"ID": "0c69a0c7471f8d0805965caf343d2f27",
"TypeName": "FlaxEditor.Content.Settings.NetworkSettings",
"EngineBuild": 6340,
"EngineBuild": 6705,
"Data": {
"MaxClients": 100,
"ProtocolVersion": 1,
"NetworkFPS": 60.0,
"NetworkFPS": 1.0,
"Address": "127.0.0.1",
"Port": 59183,
"NetworkDriver": "FlaxEngine.Networking.ENetDriver"

View File

@@ -1,7 +1,7 @@
{
"ID": "4bd8a4cc460399b5f1975fbe0a668e3f",
"TypeName": "FlaxEditor.Content.Settings.PhysicsSettings",
"EngineBuild": 6510,
"EngineBuild": 6705,
"Data": {
"DefaultGravity": {
"X": 0.0,
@@ -13,6 +13,7 @@
"RestitutionCombineMode": 0,
"DisableCCD": false,
"BroadPhaseType": 3,
"EnableEnhancedDeterminism": false,
"SolverType": 0,
"MaxDeltaTime": 0.1,
"EnableSubstepping": false,

View File

@@ -1,9 +1,8 @@
{
"ID": "a55dc3c04da4ea3744b7f1994565beac",
"TypeName": "FlaxEditor.Content.Settings.TimeSettings",
"EngineBuild": 6332,
"EngineBuild": 6705,
"Data": {
"UpdateMode": 1,
"UpdateFPS": 120.0,
"PhysicsFPS": 120.0,
"DrawFPS": 120.0,

View File

@@ -1,7 +1,8 @@
// comment
r_shadows 1
r_lighting 1
cl_maxfps 240
cl_maxfps 120
r_upscaling 0
r_gi 0
r_staticbatch 1
r_staticbatch 1
sensitivity 0.5

View File

@@ -5,8 +5,8 @@
"gulp.autoDetect": "off",
"jake.autoDetect": "off",
"grunt.autoDetect": "off",
"omnisharp.defaultLaunchSolution": "GoakeFlax.sln",
"omnisharp.useModernNet": true
"omnisharp.useModernNet": true,
"dotnet.defaultSolution": "GoakeFlax.sln"
},
"folders": [
{

View File

@@ -137,7 +137,7 @@ public class CameraRender : PostProcessEffect
private bool lastRescale = false;
public override void OnUpdate()
{
#if FLAX_EDITOR
/*#if FLAX_EDITOR
if (Input.GetKeyDown(KeyboardKeys.F7))
{
PhysicsSettings physicsSettings = GameSettings.Load<PhysicsSettings>();
@@ -145,7 +145,7 @@ public class CameraRender : PostProcessEffect
GameSettings.Save(physicsSettings);
//GameSettings.Apply();
}
#endif
#endif*/
if (lastEnabled != camera.IsActive)
{

View File

@@ -2,6 +2,8 @@
using FlaxEngine;
using Console = Game.Console;
using System.Linq;
using System.Threading;
#if FLAX_EDITOR
using FlaxEditor;
#endif

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using FlaxEngine;
using FlaxEngine.Assertions;
@@ -71,16 +72,16 @@ internal class ConsoleVariable
if (type == typeof(string))
{
if (field != null)
return (string)field.GetValue(null);
return Unsafe.As<string>(field.GetValue(null));
if (getter != null)
return ((float)getter.Invoke(null, null)).ToString();
return Unsafe.As<string>(getter.Invoke(null, null));
}
else if (type == typeof(float))
{
if (field != null)
return field.GetValue(null).ToString();
if (getter != null)
return ((float)getter.Invoke(null, null)).ToString();
return getter.Invoke(null, null).ToString();
}
else
throw new Exception($"ConsoleVariable: Unsupported variable type '{type.FullName}'");

View File

@@ -22,4 +22,16 @@ public static class Utilities
Profiler.EndEvent();
}
}
}
public static class VectorExtensions
{
/*extension(Float3 value)
{
Float2 XY => new Float2(value.X, value.Y);
}*/
public static Float2 XY(this Float3 vec) => new Float2(vec.X, vec.Y);
public static Vector2 XY(this Vector3 vec) => new Vector2(vec.X, vec.Y);
}

View File

@@ -161,10 +161,10 @@ public static partial class NetworkManager
public static void Cleanup()
{
Scripting.Exit -= Cleanup;
Scripting.FixedUpdate -= OnDemoUpdate;
Scripting.FixedUpdate -= OnServerNetworkUpdate;
Scripting.Update -= OnDemoUpdate;
Scripting.Update -= OnServerNetworkUpdate;
Level.ActorSpawned -= OnServerActorSpawned;
Scripting.FixedUpdate -= OnClientNetworkUpdate;
Scripting.Update -= OnClientNetworkUpdate;
Level.ActorSpawned -= OnClientActorSpawned;
if (server != null)

View File

@@ -24,14 +24,17 @@ public static partial class NetworkManager
//var driver = Object.New(typeof(ENetDriver));
//ClientNetworkDriver = null;
NetworkLagDriver driver = Object.New<NetworkLagDriver>();
driver.Lag = 0f;
//INetworkDriver driver = Object.New<ENetDriver>();
INetworkDriver driver = Object.New<NetworkLagDriver>();
if (driver is NetworkLagDriver networkLagDriver)
networkLagDriver.Lag = 50.0f;
ClientNetworkDriver = driver;
client = NetworkPeer.CreatePeer(new NetworkConfig
{
NetworkDriver = driver,
NetworkDriver = (Object)driver,
ConnectionsLimit = MaximumClients,
MessagePoolSize = 2048,
MessageSize = MTU,
@@ -61,7 +64,7 @@ public static partial class NetworkManager
}*/
World.InitClient();
Scripting.FixedUpdate += OnClientNetworkUpdate;
Scripting.Update += OnClientNetworkUpdate;
if (!listenServer)
{
Scripting.Exit += Cleanup;

View File

@@ -59,7 +59,7 @@ public static unsafe partial class NetworkManager
return false;
}*/
Scripting.FixedUpdate += OnDemoUpdate;
Scripting.Update += OnDemoUpdate;
//if (!listenServer)
{
Scripting.Exit += Cleanup;

View File

@@ -22,19 +22,21 @@ public static partial class NetworkManager
public static bool StartServer(bool listenServer = true)
{
Time.Synchronize();
Cleanup();
ConnectedClients = new List<NetworkConnection>(MaximumClients);
//var driver = Object.New(typeof(ENetDriver));
//ServerNetworkDriver = null;
NetworkLagDriver driver = Object.New<NetworkLagDriver>();
driver.Lag = 200f;
INetworkDriver driver = Object.New<ENetDriver>();
//NetworkLagDriver driver = Object.New<NetworkLagDriver>();
if (driver is NetworkLagDriver networkLagDriver)
networkLagDriver.Lag = 50.0f;//200f;
ServerNetworkDriver = driver;
server = NetworkPeer.CreatePeer(new NetworkConfig
{
NetworkDriver = driver,
NetworkDriver = (Object)driver,
ConnectionsLimit = MaximumClients,
MessagePoolSize = 2048,
MessageSize = MTU,
@@ -47,7 +49,7 @@ public static partial class NetworkManager
return false;
}
Scripting.FixedUpdate += OnServerNetworkUpdate;
Scripting.Update += OnServerNetworkUpdate;
Scripting.Exit += Cleanup;
Level.ActorSpawned += OnServerActorSpawned;

View File

@@ -22,31 +22,25 @@ public enum GameModeMessageType2 : byte
LastMessageType = 128,
}
public class PlayerFrame
{
public ulong frame => inputState.Frame;
public Float3 position;
public PlayerInputState2 inputState;
public PlayerMovementState movementState;
}
public interface IClientInfo
{
public NetworkConnection Connection { get; set; }
public PlayerActor PlayerActor { get; set; }
public PlayerFrame[] FrameHistory { get; set; }
}
public class World
{
protected class ClientInfo
{
public NetworkConnection Connection;
public PlayerActor PlayerActor;
public PlayerFrame[] FrameHistory = new PlayerFrame[120];
public ClientInfo()
{
for (int i = 0; i < FrameHistory.Length; i++)
FrameHistory[i] = new PlayerFrame();
}
}
public class PlayerFrame
{
public ulong frame;
public Float3 position;
public PlayerInputState2 inputState;
public PlayerMovementState movementState;
}
public bool IsServer => this is ServerWorld;
public bool IsClient => !IsServer;
public bool IsClient => this is ClientWorld;
public ulong Frame { get; protected set; }
public ulong ServerFrame { get; protected set; } // Last received frame from server
public float GameTime { get; protected set; } // The join time
@@ -54,7 +48,6 @@ public class World
protected Scene _scene;
protected Actor _worldSpawn;
protected Dictionary<uint, ClientInfo> _clients = new();
public static void InitClient()
{
@@ -65,6 +58,9 @@ public class World
{
WorldStore.ServerWorld = new ServerWorld();
WorldStore.ServerWorld.Init();
Scripting.Update += WorldStore.ServerWorld.OnUpdate;
Scripting.LateUpdate += WorldStore.ServerWorld.OnUpdateLate;
Scripting.FixedUpdate += WorldStore.ServerWorld.OnFixedUpdate;
}
public static void CleanupClient()
{
@@ -79,12 +75,17 @@ public class World
virtual protected void Init()
{
Scripting.LateFixedUpdate += OnLateUpdate;
}
virtual protected void Cleanup()
{
//Level.SceneLoaded -= OnLevelLoaded;
//Scripting.LateUpdate -= OnLateUpdatePre;
Scripting.Update -= OnUpdate;
Scripting.LateUpdate -= OnUpdateLate;
Scripting.FixedUpdate -= OnFixedUpdate;
Scripting.LateFixedUpdate -= OnLateUpdate;
if (_scene)
@@ -99,14 +100,35 @@ public class World
}
}
void OnUpdate()
{
//Console.Print("server Update");
}
void OnUpdateLate()
{
//Console.Print("server LateUpdate");
}
void OnFixedUpdate()
{
//Console.Print("server FixedUpdate");
}
protected void CreateScene(string sceneNamePrefix, string sceneGuid)
{
string physicsSceneName = $"{sceneNamePrefix}PhysicsScene";
PhysicsScene localPhysicsScene = Physics.FindOrCreateScene(physicsSceneName);
if (false && sceneNamePrefix == "Client")
{
_scene = Level.Scenes.FirstOrDefault(x => !x.Name.StartsWith("Server"));
}
else
{
string physicsSceneName = $"{sceneNamePrefix}PhysicsScene";
PhysicsScene localPhysicsScene = Physics.FindOrCreateScene(physicsSceneName);
Guid guid = JsonSerializer.ParseID(sceneGuid);
string guidStr = JsonSerializer.GetStringID(guid);
string sceneData = $@"
Guid guid = JsonSerializer.ParseID(sceneGuid);
string guidStr = JsonSerializer.GetStringID(guid);
string sceneData = $@"
{{
""ID"": ""{guidStr}"",
""TypeName"": ""FlaxEngine.SceneAsset"",
@@ -129,25 +151,26 @@ public class World
]
}}";
{
var onSceneLoaded = (Scene loadedScene, Guid id) =>
{
if (guid == id)
var onSceneLoaded = (Scene loadedScene, Guid id) =>
{
loadedScene.PhysicsScene = localPhysicsScene;
//scene = loadedScene;
}
};
if (guid == id)
{
loadedScene.PhysicsScene = localPhysicsScene;
//scene = loadedScene;
}
};
try
{
Level.SceneLoaded += onSceneLoaded;
//Level.LoadScene(new SceneReference(guid));
_scene = Level.LoadSceneFromBytes(Encoding.ASCII.GetBytes(sceneData));
}
finally
{
Level.SceneLoaded -= onSceneLoaded;
try
{
Level.SceneLoaded += onSceneLoaded;
//Level.LoadScene(new SceneReference(guid));
_scene = Level.LoadSceneFromBytes(Encoding.ASCII.GetBytes(sceneData));
}
finally
{
Level.SceneLoaded -= onSceneLoaded;
}
}
}
Assert.IsTrue(_scene);
@@ -155,7 +178,7 @@ public class World
_scene.Name = $"{sceneNamePrefix}Scene";
//Level.SceneLoaded += OnLevelLoaded;
Scripting.LateFixedUpdate += OnLateUpdate;
var importer = FlaxEngine.Object.New<Q3MapImporter>();
importer.mapPath = @"C:\dev\GoakeFlax\Assets\Maps\aerowalk.map";
@@ -167,16 +190,12 @@ public class World
Assert.IsTrue(_worldSpawn);
}
public void OnLateUpdate()
virtual protected void OnLateUpdate()
{
Frame++;
}
protected void OnClientConnected(NetworkConnection connection)
{
uint playerId = connection.ConnectionId;
_clients.Add(playerId, new ClientInfo() { Connection = connection });
}
virtual protected IClientInfo GetClient(uint playerId) => null;
virtual protected PlayerActor SpawnPlayer(uint playerId, Float3 position, Vector3 eulerAngles)
{
@@ -189,7 +208,8 @@ public class World
playerActor.Initialize(playerId, position, eulerAngles);
//playerActor.Teleport(position, eulerAngles);
_clients[playerId].PlayerActor = playerActor;
IClientInfo player = GetClient(playerId);
player.PlayerActor = playerActor;
IPlayerInput playerInput = playerActor.GetScript<PlayerMovement>().Input;
playerInput.SetFrame(Frame);
/*if (IsServer)
@@ -211,14 +231,17 @@ public class World
virtual public bool IsLocalPlayer(uint playerId) => false;
virtual public ulong GetLastProcessedFrame(uint playerId) => 0;
virtual public ulong GetLastReceivedFrame(uint playerId) => 0;
public PlayerFrame GetPlayerFrame(uint playerId, ulong frame)
{
ClientInfo player = _clients[playerId];
IClientInfo player = GetClient(playerId);
PlayerFrame playerFrame = player.FrameHistory[frame % 120];
if (playerFrame.frame != frame)
return null;
return playerFrame;
if (playerFrame.inputState.Frame == frame || playerFrame.movementState.frame == frame)
return playerFrame;
return null;
}
public bool HasPlayerFrame(uint playerId, ulong frame) => GetPlayerFrame(playerId, frame) != null;
@@ -229,10 +252,70 @@ public class World
inputState = playerFrame?.inputState ?? default;
return playerFrame != null;
}
virtual public void UpdatePlayerInputState(uint playerId, ulong frame, PlayerInputState2 inputState, PlayerMovementState movementState)
{
IClientInfo player = GetClient(playerId);
PlayerFrame playerFrame = player.FrameHistory[frame % 120];
/*if (playerFrame.frame == frame)
{
var old = playerFrame.inputState with { Frame = inputState.Frame };
playerFrame.inputState = inputState with
{
ViewDelta = playerFrame.inputState.ViewDelta + inputState.ViewDelta,
MoveForward = MathF.MaxMagnitude(playerFrame.inputState.MoveForward, inputState.MoveForward),
MoveRight = MathF.MaxMagnitude(playerFrame.inputState.MoveRight, inputState.MoveRight),
Attack = playerFrame.inputState.Attack || inputState.Attack,
Jump = playerFrame.inputState.Jump || inputState.Jump,
};
if (old != playerFrame.inputState)
old = old;
}
else*/
playerFrame.inputState = inputState;
playerFrame.inputState.Frame = frame;
//playerFrame.movementState = movementState;
//playerFrame.frame = frame;
}
virtual public void UpdatePlayerState(uint playerId, ulong frame, PlayerMovementState movementState)
{
IClientInfo player = GetClient(playerId);
PlayerFrame playerFrame = player.FrameHistory[frame % 120];
playerFrame.movementState = movementState;
playerFrame.movementState.frame = frame;
}
}
file class ServerWorld : World
{
protected class ClientInfo : IClientInfo
{
public NetworkConnection Connection { get; set; }
public PlayerActor PlayerActor { get; set; }
public PlayerFrame[] FrameHistory { get; set; } = new PlayerFrame[120];
public ulong LastReceivedFrame = 0;
public ulong LastSentDeltaFrame = 0; // TODO: Accumulate deltas since this frame
public ulong LastProcessedFrame = 0; // In case player is lagged behind, these frames should be caught up during next simulation
public int SendRate = 0; // How many updates the client wants to receive per second
public ClientInfo()
{
for (int i = 0; i < FrameHistory.Length; i++)
{
FrameHistory[i] = new PlayerFrame();
FrameHistory[i].inputState.Frame = ulong.MaxValue;
FrameHistory[i].movementState.frame = ulong.MaxValue;
}
}
}
protected Dictionary<uint, ClientInfo> _clients = new();
protected override IClientInfo GetClient(uint playerId) => _clients[playerId];
protected override void Init()
{
NetworkManager.RegisterServerCallbacks(OnMessage, OnClientConnecting, OnClientConnected);
@@ -249,6 +332,18 @@ file class ServerWorld : World
base.Cleanup();
}
protected override void OnLateUpdate()
{
//Console.Print("server LateFixedUpdate");
base.OnLateUpdate();
foreach (var client in _clients.Values)
{
// We assume all the scripts have processed the frames during this cycle
client.LastProcessedFrame = client.LastReceivedFrame;
}
}
public bool OnMessage(ref NetworkEvent networkEvent)
{
byte messageTypeByte = networkEvent.Message.ReadByte();
@@ -266,23 +361,9 @@ file class ServerWorld : World
{
case GameModeMessageType2.AcceptConnection: // Client
{
AcceptConnectionMessage message = AcceptConnectionMessage.Read(ref networkEvent.Message);
Frame += message.Frame;
GameTime = message.GameTime;
ServerFrame = message.Frame;
/*foreach (var player in message.Players)
{
SpawnPlayer(player.PlayerId, player.PlayerPosition, new Float3(0));
var playerFrames = new PlayerFrame[120];
for (int j = 0; j < playerFrames.Length; j++)
playerFrames[j] = new PlayerFrame();
clientWorldState.playerFrameHistory.Add(player.PlayerId, playerFrames);
}*/
break;
}
break;
case GameModeMessageType2.PlayerPosition:
case GameModeMessageType2.PlayerPosition: // FIXME: Should be the delta frame
{
//uint playerId = networkEvent.Sender.ConnectionId;
PlayerInputState2 inputState; //?
@@ -308,16 +389,70 @@ file class ServerWorld : World
actorState.jumped = networkEvent.Message.ReadBoolean();
//inputState.frame = receivedFrame;
inputState.ViewDelta.X = networkEvent.Message.ReadSingle();
inputState.ViewDelta.Y = networkEvent.Message.ReadSingle();
//inputState.ViewDelta.X = networkEvent.Message.ReadSingle();
//inputState.ViewDelta.Y = networkEvent.Message.ReadSingle();
inputState.MoveForward = networkEvent.Message.ReadSingle();
inputState.MoveRight = networkEvent.Message.ReadSingle();
inputState.Attack = networkEvent.Message.ReadBoolean();
inputState.Jump = networkEvent.Message.ReadBoolean();
ServerFrame = receivedFrame;
break;
}
case GameModeMessageType2.PlayerInput:
{
PlayerInputState2 inputState; //?
ulong receivedFrame = inputState.Frame = networkEvent.Message.ReadUInt64();
uint playerId = networkEvent.Message.ReadUInt32();
float time = networkEvent.Message.ReadSingle();
float delay = Time.TimeSinceStartup - time;
inputState.ViewDelta = Float2.Zero;
inputState.ViewAngles.X = networkEvent.Message.ReadSingle();
inputState.ViewAngles.Y = networkEvent.Message.ReadSingle();
inputState.MoveForward = networkEvent.Message.ReadSingle();
inputState.MoveRight = networkEvent.Message.ReadSingle();
inputState.Attack = networkEvent.Message.ReadBoolean();
inputState.Jump = networkEvent.Message.ReadBoolean();
//Console.Print("recv: " + receivedFrame.ToString());
ClientInfo player = GetClient(playerId) as ClientInfo;
if (receivedFrame <= player.LastReceivedFrame)
{
if (receivedFrame < player.LastProcessedFrame)
{
Console.PrintWarning($"Dropping late frame from client: {receivedFrame}, last processed: {player.LastProcessedFrame}");
break;
}
else
{
//Console.PrintWarning($"Out-of-order frame received from client: {receivedFrame}, latest: {player.LastReceivedFrame}");
}
}
player.LastReceivedFrame = Math.Max(receivedFrame, player.LastReceivedFrame);
//if (player.LastProcessedFrame == 0)
// player.LastProcessedFrame = receivedFrame - 1;
//Console.Print($"server receive client frame {receivedFrame}, current server frame {Frame}");
var asdf = receivedFrame;
//receivedFrame = Frame; // TODO: receive all frames from players, simulate all unprocessed frames in next simulation cycle?
if (inputState.MoveForward != 0)
receivedFrame = receivedFrame;
//Console.Print($"delay: {delay * 1000}ms");
// Sanity check
if (GetPlayerInputState(playerId, receivedFrame, out var prevInputState))
{
Console.PrintWarning($"Duplicate frame received from client: {asdf}, delay: {delay*1000}ms");
//break;
}
UpdatePlayerInputState(playerId, receivedFrame, inputState, default);
break;
}
break;
default:
break;
}
@@ -346,11 +481,10 @@ file class ServerWorld : World
return true;
}
public new void OnClientConnected(NetworkConnection connection)
public void OnClientConnected(NetworkConnection connection)
{
base.OnClientConnected(connection);
uint playerId = connection.ConnectionId;
_clients.Add(playerId, new ClientInfo() { Connection = connection });
var spawns = _worldSpawn.GetChildren<Actor>().Where(x => x.Name.StartsWith("PlayerSpawn_")).ToArray();
Console.Print($"found {spawns.Length} spawns");
@@ -375,12 +509,56 @@ file class ServerWorld : World
NetworkManager.ServerEndSendMessage(ref message, connection);
}
}
public override ulong GetLastProcessedFrame(uint playerId)
{
ClientInfo player = GetClient(playerId) as ClientInfo;
return player.LastProcessedFrame;
}
public override ulong GetLastReceivedFrame(uint playerId)
{
ClientInfo player = GetClient(playerId) as ClientInfo;
return player.LastReceivedFrame;
}
public override void UpdatePlayerInputState(uint playerId, ulong frame, PlayerInputState2 inputState, PlayerMovementState movementState)
{
base.UpdatePlayerInputState(playerId, frame, inputState, movementState);
//ClientInfo player = GetClient(playerId) as ClientInfo;
//player.LastReceivedFrame = frame; // Dropped frames should be ignored?
}
protected override PlayerActor SpawnPlayer(uint playerId, Float3 position, Vector3 eulerAngles)
{
var playerActor = base.SpawnPlayer(playerId, position, eulerAngles);
playerActor.Input = new PlayerInputNetwork2(playerId, this);
return playerActor;
}
}
file class ClientWorld : World
{
protected class ClientInfo : IClientInfo
{
public NetworkConnection Connection { get; set; }
public PlayerActor PlayerActor { get; set; }
public PlayerFrame[] FrameHistory { get; set; } = new PlayerFrame[120];
public ClientInfo()
{
for (int i = 0; i < FrameHistory.Length; i++)
FrameHistory[i] = new PlayerFrame();
}
}
private uint _localPlayerId;
protected Dictionary<uint, ClientInfo> _clients = new();
protected override IClientInfo GetClient(uint playerId) => _clients[playerId];
protected override void Init()
{
NetworkManager.RegisterClientCallbacks(OnMessage, null, OnClientConnected);
@@ -396,6 +574,49 @@ file class ClientWorld : World
base.Cleanup();
}
override protected void OnLateUpdate()
{
foreach (var player in _scene.GetChildren<PlayerActor>())
{
if (player.PlayerId != _localPlayerId)
continue;
ClientInfo playerClient = GetClient(player.PlayerId) as ClientInfo;
PlayerInputState2 inputState = player.Input.GetState();
PlayerMovementState movementState = player.GetScript<PlayerMovement>().movementState;
if (inputState.Frame == 0)
inputState = inputState;
/*public Float2 ViewDelta;
public float MoveForward;
public float MoveRight;
public bool Attack;
public bool Jump;*/
//Console.Print($"client send {Frame}");
UpdatePlayerInputState(player.PlayerId, Frame, inputState, movementState);
//Console.Print($"cframe: {Frame}");
NetworkMessage message = NetworkManager.ClientBeginSendMessage();
message.WriteByte((byte)GameModeMessageType2.PlayerInput);
message.WriteUInt64(Frame);
message.WriteUInt32(player.PlayerId);
message.WriteSingle(Time.TimeSinceStartup);
message.WriteSingle(movementState.viewAngles.X);
message.WriteSingle(movementState.viewAngles.Y);
//message.WriteSingle(movementState.viewAngles.Z);
message.WriteSingle(inputState.MoveForward);
message.WriteSingle(inputState.MoveRight);
message.WriteBoolean(inputState.Attack);
message.WriteBoolean(inputState.Jump);
NetworkManager.ClientEndSendMessage(ref message);
//Console.Print("send: " + Frame.ToString());
}
base.OnLateUpdate();
}
public bool OnMessage(ref NetworkEvent networkEvent)
{
byte messageTypeByte = networkEvent.Message.ReadByte();
@@ -426,8 +647,8 @@ file class ClientWorld : World
}
Console.Print("received welcome: frame " + ServerFrame);
break;
}
break;
case GameModeMessageType2.SpawnPlayer:
{
uint playerId = networkEvent.Message.ReadUInt32();
@@ -447,8 +668,8 @@ file class ClientWorld : World
//if (NetworkManager.IsClient)
//players[playerId].GetScript<PlayerMovement>().Input.SetFrame(ClientFrame);
break;
}
break;
#if false
case GameModeMessageType2.PlayerPosition:
{
@@ -484,8 +705,8 @@ file class ClientWorld : World
inputState.Jump = networkEvent.Message.ReadBoolean();
ServerFrame = receivedFrame;
break;
}
break;
#endif
default:
break;
@@ -496,10 +717,24 @@ file class ClientWorld : World
public override bool IsLocalPlayer(uint playerId) => playerId == _localPlayerId;
public new void OnClientConnected(NetworkConnection connection)
public override ulong GetLastProcessedFrame(uint playerId)
{
base.OnClientConnected(connection);
_localPlayerId = connection.ConnectionId;
//ClientInfo player = GetClient(playerId) as ClientInfo;
return 0;
}
public override ulong GetLastReceivedFrame(uint playerId)
{
//ClientInfo player = GetClient(playerId) as ClientInfo;
return 0;
}
public void OnClientConnected(NetworkConnection connection)
{
uint playerId = connection.ConnectionId;
_clients.Add(playerId, new ClientInfo() { Connection = connection });
_localPlayerId = playerId;
Console.Print($"ClientWorld: Connected, playerId: {_localPlayerId}");
}

View File

@@ -1,22 +1,49 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FlaxEngine;
namespace Game;
[StructLayout(LayoutKind.Sequential)]
public struct PlayerInputState2
public struct PlayerInputState2 : IEquatable<PlayerInputState2>
{
//public ulong frame;
public Float2 ViewDelta;
public ulong Frame;
public Float2 ViewAngles;
public float MoveForward;
public float MoveRight;
public bool Attack;
public bool Jump;
public Float2 ViewDelta; // Only used when gathering input for local player
/*public override bool Equals([NotNullWhen(true)] object obj)
{
if (obj is not PlayerInputState2 other)
return false;
return Frame == other.Frame && ViewDelta == other.ViewDelta && MoveForward == other.MoveForward && MoveRight == other.MoveRight && Attack == other.Attack && Jump == other.Jump;
}*/
public bool Equals(PlayerInputState2 other)
{
return Frame == other.Frame && ViewAngles == other.ViewAngles && MoveForward == other.MoveForward && MoveRight == other.MoveRight && Attack == other.Attack && Jump == other.Jump && ViewDelta == other.ViewDelta;
}
public static bool operator ==(PlayerInputState2 left, PlayerInputState2 right) => left.Equals(right);
public static bool operator !=(PlayerInputState2 left, PlayerInputState2 right) => !left.Equals(right);
// Apply delta to calculate the final view angles for this state
public void UpdateViewAngles(Float2 lastInputViewAngles)
{
ViewAngles = lastInputViewAngles + ViewDelta;
ViewDelta = Float2.Zero;
}
}
public interface IPlayerInput
@@ -53,8 +80,13 @@ public class PlayerInput2 : IPlayerInput
[ConsoleVariable("sensitivity")]
private static float _sensitivity { get; set; } = 1.0f;
private ulong _frame;
public void SetFrame(ulong frame)
{
if (_frame != frame)
ResetState();
_frame = frame;
}
public void UpdateState()
@@ -68,12 +100,20 @@ public class PlayerInput2 : IPlayerInput
float turnSpeed = 100.0f;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * _sensitivity;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("LookRight"), Input.GetAxisRaw("LookUp")) * Time.DeltaTime * turnSpeed;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("LookRight"), Input.GetAxisRaw("LookUp")) * Time.DeltaTime * turnSpeed;
_recordState.MoveForward = MathF.MaxMagnitude(_recordState.MoveForward, Input.GetAxis("Vertical"));
_recordState.MoveRight = MathF.MaxMagnitude(_recordState.MoveRight, Input.GetAxis("Horizontal"));
_recordState.Attack |= Input.GetAction("Attack");
_recordState.Jump |= Input.GetAction("Jump");
_recordState.Frame = _frame;
if (Input.GetKeyDown(KeyboardKeys.F))
Thread.Sleep(20);
var asdf = Input.GetAxis("Vertical");
if (asdf != 0.0f)
asdf = asdf;
}
public PlayerInputState2 GetState() => _recordState;

View File

@@ -74,6 +74,7 @@ public struct PlayerMovementState
public int numJumps;
public bool jumped;
public bool onGround;
public ulong frame;
public PlayerMovementState()
@@ -136,7 +137,11 @@ public class PlayerMovement : Script
private PlayerActor playerActor;
public Actor rootActor;
public Float3 viewAngles;
public Float3 viewAngles
{
get => movementState.viewAngles;
set => movementState.viewAngles = value;
}
private Float3 viewAnglesLastFrame;
public uint PlayerId => playerActor ? playerActor.PlayerId : 0;
@@ -250,14 +255,27 @@ public class PlayerMovement : Script
viewAnglesLastFrame = viewAngles;
}
private static int framedropped = 0;
public override void OnUpdate()
{
if (World.IsServer && Mathf.Abs(Time.DeltaTime - (1.0f / Time.UpdateFPS)) > (1.0f / Time.UpdateFPS) * 0.999f)
{
Console.Print($"drop: {Time.DeltaTime*1000.0f}ms");
framedropped = 3;
}
else if (World.IsServer && framedropped > 0)
{
Console.Print("dropping...");
framedropped--;
}
//Console.Print($"{(World.IsClient ? "[cl] " : "[sv] ") + World.Frame.ToString()} playerMovement OnUpdate");
//input.OnUpdate();
//if (Input is PlayerInputDemo /*&& currentInputFrame2 >= currentInputFrame*/)
// return;
Input.SetFrame(World.Frame);
Input.UpdateState();
@@ -311,13 +329,14 @@ public class PlayerMovement : Script
public override void OnFixedUpdate()
{
//Console.Print($"{(World.IsClient ? "[cl] " : "[sv] ") + World.Frame.ToString()} playerMovement OnFixedUpdate");
float timeDeltaDiff = Time.DeltaTime - 1.0f / Time.PhysicsFPS;
if (Time.PhysicsFPS > 0 && Math.Abs(timeDeltaDiff) > 0.0001f)
Console.Print("Time.DeltaTime is not stable: " + timeDeltaDiff);
//Input.OnFixedUpdate();
PlayerInputState2 inputState = Input.GetState();
bool predict = World.IsClient;
bool predict = World.IsClient && false;
if (predict)
{
// Get the latest frame we have predicted
@@ -408,13 +427,95 @@ public class PlayerMovement : Script
}
}
if (!predict)
if (World.IsServer)
{
ulong currentFrame = World.GetLastProcessedFrame(PlayerId);
{
// Rewind to last confirmed state
var frameInfo = World.GetPlayerFrame(PlayerId, currentFrame);
if (frameInfo != null && frameInfo.movementState.frame == currentFrame)
{
// Reset all state to latest confirmed state from server
movementState.position = frameInfo.movementState.position;
movementState.currentVelocity = frameInfo.movementState.currentVelocity;
movementState.lastJumped = frameInfo.movementState.lastJumped;
movementState.numJumps = frameInfo.movementState.numJumps;
movementState.jumped = frameInfo.movementState.jumped;
Actor.Position = movementState.position;
SetCameraEulerAngles(frameInfo.movementState.viewAngles, true);
//ApplyInputToCamera(frameInfo.inputState, true);
}
else if (frameInfo == null)
{
// First frame, update the known state
movementState.viewAngles = viewAngles;
World.UpdatePlayerState(PlayerId, currentFrame, movementState);
}
currentFrame++;
}
//Console.Print("before:" + viewAngles.ToString());
ulong lastFrame = World.GetLastReceivedFrame(PlayerId);
PlayerFrame lastFrameInfo = null;
for (; currentFrame < lastFrame+1; currentFrame++)
{
var frameInfo = World.GetPlayerFrame(PlayerId, currentFrame);
if (frameInfo == null || frameInfo.inputState.Frame != currentFrame)
//if (!Input.GetState(currentFrame, out var pastInputState, out var pastActorState))
{
Console.Print($"frame {currentFrame} missing");
frameInfo = lastFrameInfo; // Use last known input
if (frameInfo == null)
{
// FIXME
frameInfo = new PlayerFrame()
{
inputState =
{
Frame = currentFrame,
ViewAngles = movementState.viewAngles.XY(),
},
movementState = movementState,
};
frameInfo.movementState.frame = currentFrame;
}
//continue;
//Console.Print($"not predicting");
//predict = false;
//break;
}
else
{
Input.SetFrame(currentFrame);
Input.UpdateState();
SetCameraEulerAngles(new Float3(frameInfo.inputState.ViewAngles.X, frameInfo.inputState.ViewAngles.Y, viewAngles.Z), true);
//ApplyInputToCamera(frameInfo.inputState, true);
lastFrameInfo = frameInfo;
}
SimulatePlayerMovement(frameInfo.inputState, currentFrame);
movementState.viewAngles = viewAngles;
//Console.Print("tick: " + viewAngles.ToString());
//if (frameInfo.movementState.frame != currentFrame)
World.UpdatePlayerState(PlayerId, currentFrame, movementState);
//else
// currentFrame = currentFrame;
}
}
else if (!predict)
{
SetCameraEulerAngles(viewAnglesLastFrame, true);
ApplyInputToCamera(inputState, true);
SimulatePlayerMovement(inputState, World.Frame); // MAYBE?
}
//if (!movementState.currentVelocity.IsZero)
// Console.Print($"{(World.IsClient ? "client" : "server")} {World.Frame} vel: {movementState.currentVelocity}, pos: {movementState.position}");
viewAnglesLastFrame = viewAngles;
#if false
@@ -962,7 +1063,7 @@ public class PlayerMovement : Script
if (capsuleCollider && capsuleCollider.IsActive)
{
Quaternion rotation = capsuleCollider.LocalOrientation * Quaternion.Euler(0f, 90f, 0f);
DebugDraw.DrawWireTube(capsuleCollider.Position, rotation, capsuleCollider.Radius,
DebugDraw.DrawWireCapsule(capsuleCollider.Position, rotation, capsuleCollider.Radius,
capsuleCollider.Height, Color.GreenYellow * 0.8f);
}
else if (meshCollider && meshCollider.IsActive)
@@ -970,7 +1071,7 @@ public class PlayerMovement : Script
//Quaternion rotation = meshCollider.LocalOrientation * Quaternion.Euler(0f, 90f, 0f);
DebugDraw.DrawWireCylinder(meshCollider.Position, meshCollider.Orientation, capsuleCollider.Radius,
capsuleCollider.Height + capsuleCollider.Radius * 2, Color.GreenYellow * 0.8f);
//DebugDraw.DrawWireTube(meshCollider.Position, rotation, meshCollider.Radius, meshCollider.Height, Color.GreenYellow * 0.8f);
//DebugDraw.DrawWireCapsule(meshCollider.Position, rotation, meshCollider.Radius, meshCollider.Height, Color.GreenYellow * 0.8f);
}
else if (boxCollider && boxCollider.IsActive)
{