record demos as packets

This commit is contained in:
2023-05-21 00:48:03 +03:00
parent 3a59bad850
commit 280be62caa
10 changed files with 579 additions and 255 deletions

View File

@@ -90,11 +90,13 @@ namespace Game
public class PlayerInput
{
public const byte DemoVer = 1;
public const int MaxPlayerStates = 120;
public PlayerState currentState;
public ulong frame;
//public ulong oldestFrame;
private PlayerState[] states = new PlayerState[120];
private PlayerState[] states = new PlayerState[MaxPlayerStates];
public virtual bool Predict => false;
@@ -110,7 +112,7 @@ namespace Game
{
//Console.Print("recorded frame " + frame);
states[frame % 120] = currentState;
states[frame % MaxPlayerStates] = currentState;
/*ulong oldest = ulong.MaxValue;
for (int i = 0; i < 120; i++)
@@ -130,7 +132,7 @@ namespace Game
public bool GetState(ulong frame, out PlayerInputState inputState, out PlayerActorState actorState)
{
int frameIndex = (int)frame % 120;
int frameIndex = (int)frame % MaxPlayerStates;
if (states[frameIndex].input.frame != frame)
{
inputState = default;
@@ -145,7 +147,7 @@ namespace Game
public void SetState(ulong frame, PlayerInputState inputState)
{
int frameIndex = (int)frame % 120;
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = new PlayerActorState();
@@ -153,7 +155,7 @@ namespace Game
public void SetState(ulong frame, PlayerInputState inputState, PlayerActorState actorState)
{
int frameIndex = (int)frame % 120;
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = actorState;

View File

@@ -27,9 +27,11 @@ namespace Game
public override bool Predict => true;
public PlayerInputLocal(PlayerActor playerActor, string demoPath)
public PlayerInputLocal(PlayerActor playerActor, string demoPath = null)
{
this.playerActor = playerActor;
if (demoPath == null)
return;
var demoFolder = Directory.GetParent(demoPath);
if (!demoFolder.Exists)
Directory.CreateDirectory(demoFolder.FullName);
@@ -72,6 +74,7 @@ namespace Game
public override void OnEndFrame()
{
currentState.input.frame = frame;
if (IsRecording)
{
currentState.input.verificationPosition = currentState.actor.position;
@@ -79,7 +82,6 @@ namespace Game
currentState.input.verificationViewAngles = currentState.actor.viewAngles;
currentState.input.verificationOrientation = currentState.actor.orientation;
currentState.input.frame = frame;
buffer.Add(currentState.input);
}
@@ -111,14 +113,6 @@ namespace Game
currentState.input.viewDeltaY = 0;
}
public override void RecordCurrentActorState(PlayerActorState actorState)
{
if (!IsRecording)
return;
base.RecordCurrentActorState(actorState);
}
public void FlushDemo()
{
if (!IsRecording)

View File

@@ -135,7 +135,6 @@ namespace Game
public Float3 viewAngles;
private Float3 viewAnglesLastFrame;
[NetworkReplicated]
public uint PlayerId = 0;
[ReadOnly]
@@ -186,8 +185,9 @@ namespace Game
if (PlayerId == NetworkManager.LocalPlayerClientId)//if (NetworkReplicator.GetObjectRole(this.Parent) == NetworkObjectRole.OwnedAuthoritative)// if (playerId == NetworkManager.LocalPlayerClientId)
{
Console.Print("local player?: " + playerId.ToString());
string demoPath = System.IO.Path.Combine(AssetManager.DemoPath, $"{DateTimeOffset.Now.UtcTicks}.gdem");
input = new PlayerInputLocal(playerActor, demoPath); // TODO: support recording
//string demoPath = System.IO.Path.Combine(AssetManager.DemoPath, $"{DateTimeOffset.Now.UtcTicks}.gdem");
//input = new PlayerInputLocal(playerActor, demoPath); // TODO: support recording
input = new PlayerInputLocal(playerActor);
}
else
@@ -327,67 +327,14 @@ namespace Game
public override void OnFixedUpdate()
{
PlayerInputDemo demoInput = input as PlayerInputDemo;
if (demoInput != null)
demoInput.OnUpdate();
float timeDeltaDiff = Time.DeltaTime - 1.0f / Time.PhysicsFPS;
if (Math.Abs(timeDeltaDiff) > 0.0001f)
if (Time.PhysicsFPS > 0 && Math.Abs(timeDeltaDiff) > 0.0001f)
Console.Print("Time.DeltaTime is not stable: " + timeDeltaDiff);
input.OnFixedUpdate();
PlayerInputState inputState = input.GetCurrentInputState();
if (demoInput != null && demoInput.IsPlaying)
{
ApplyInputToCamera(inputState, true);
// Verify view angles first
if (demoDeltasVerify)
{
Float3 verifAngles = new Float3(inputState.verificationViewAngles.Y, inputState.verificationViewAngles.X, inputState.verificationViewAngles.Z);
float viewAnglesDelta = (viewAngles - verifAngles).Length;
if (viewAnglesDelta > 0.00001)
{
Console.Print($"Demo verification failed, view angles delta: {viewAnglesDelta}, viewAngles:{viewAngles}, verif:{verifAngles}");
if (demoDeltasCorrect)
SetCameraEulerAngles(verifAngles, false);
}
}
SimulatePlayerMovement(inputState);
if (demoDeltasVerify)
{
// verify
float positionDelta = (movementState.position - inputState.verificationPosition).Length;
if (positionDelta > 0.00001)
Console.Print("Demo verification failed, position delta: " + positionDelta);
float velocityDelta = (movementState.currentVelocity - inputState.verificationVelocity).Length;
if (velocityDelta > 0.00001)
Console.Print("Demo verification failed, velocity delta: " + velocityDelta);
float orientationDelta = (rootActor.Orientation - inputState.verificationOrientation).Length;
if (orientationDelta > 0.00001)
{
Console.Print("Demo verification failed, orientation delta: " + orientationDelta);
if (demoDeltasCorrect)
{
}
}
//if (currentInputFrame == 0)
/*{
//Console.Print("repos: " + inputState.verificationPosition);
movementState.position = inputState.verificationPosition;
currentVelocity = inputState.verificationVelocity;
rootActor.Orientation = inputState.verificationOrientation;
}*/
}
}
else if (input is PlayerInputNetwork)
if (input is PlayerInputNetwork)
{
#if false
bool canpredict = true;
@@ -497,7 +444,7 @@ namespace Game
//viewAngles = viewAnglesLastFrame;
bool canpredict = true;
if (true && input.Predict && GameModeManager.ClientFrame > 0 && GameModeManager.ServerFrame > 0)
if (true && input.Predict /*&& !NetworkManager.IsDemoPlaying*/ && GameModeManager.ClientFrame > 0 && GameModeManager.ServerFrame > 0)
{
ulong currentFrame = GameModeManager.ServerFrame;
for (; currentFrame < GameModeManager.ClientFrame; currentFrame++)
@@ -580,9 +527,11 @@ namespace Game
predicting = false;
if ((movementState.position - oldPos).Length > 0.001)
var posDelta = (movementState.position - oldPos);
var velDelta = (movementState.currentVelocity - oldVel);
if (posDelta.Length > 0.001)
Console.Print($"mispredicted final position");
if ((movementState.currentVelocity - oldVel).Length > 0.001)
if (velDelta.Length > 0.001)
Console.Print($"mispredicted final velocity");
@@ -602,7 +551,8 @@ namespace Game
SimulatePlayerMovement(inputState);
}
if ((viewAngles - oldAngles).Length > 0.001)
var viewDelta = (viewAngles - oldAngles);
if (viewDelta.Length > 0.001)
Console.Print($"mispredicted final viewangles: {viewAngles} <- {oldAngles}");
//if (viewAngles != new Float3(90f, 0f, 0f))