Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #30 from proepkes/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
proepkes authored Feb 4, 2019
2 parents e71c1d2 + a13debb commit 0eccbe6
Show file tree
Hide file tree
Showing 69 changed files with 1,286 additions and 430 deletions.
12 changes: 12 additions & 0 deletions Engine/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
<ProjectReference Include="..\Network\Network.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="BEPUutilities">
<HintPath>..\Dependencies\BEPUutilities.dll</HintPath>
</Reference>
<Reference Include="Entitas">
<HintPath>..\Dependencies\Entitas.dll</HintPath>
</Reference>
<Reference Include="FixMath.NET">
<HintPath>..\Dependencies\FixMath.NET.dll</HintPath>
</Reference>
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /d /y &quot;$(TargetDir)Lockstep.*.dll&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.pdb&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.json&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;" />
</Target>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BEPUutilities;
using Lockstep.Client.Interfaces;
using System;
using BEPUutilities;
using Lockstep.Client.Interfaces;
using Lockstep.Network.Utils;

namespace Lockstep.Commands
namespace Lockstep.Client.Commands
{
[Serializable]
public class NavigateCommand : ISerializableCommand
{
public ushort Tag => 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BEPUutilities;
using System;
using BEPUutilities;
using Lockstep.Client.Interfaces;
using Lockstep.Network.Utils;

namespace Lockstep.Commands
namespace Lockstep.Client.Commands
{
[Serializable]
public class SpawnCommand : ISerializableCommand
{
public ushort Tag => 2;
Expand Down
11 changes: 6 additions & 5 deletions Engine/Client/Implementations/NetworkCommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace Lockstep.Client.Implementations
{
public class NetworkCommandBuffer : CommandBuffer
public sealed class NetworkCommandBuffer : CommandBuffer
{
//TODO: refactor: don't receive meta information through commandbuffer
public event Action<Init> InitReceived;
//TODO: refactor: don't do meta stuff through commandbuffer just because it has INetwork
public event Action<Init> InitReceived;

private readonly INetwork _network;
private readonly IDictionary<ushort, Func<ISerializableCommand>> _commandFactories = new Dictionary<ushort, Func<ISerializableCommand>>();
Expand All @@ -28,7 +28,7 @@ public void RegisterCommand(Func<ISerializableCommand> commandFactory)
var tag = commandFactory.Invoke().Tag;
if (_commandFactories.ContainsKey(tag))
{
throw new InvalidDataException("The command tag " + tag + " is already registered. Every command tag must be unique.");
throw new InvalidDataException($"The command tag {tag} is already registered. Every command tag must be unique.");
}
_commandFactories.Add(tag, commandFactory);
}
Expand All @@ -51,7 +51,8 @@ public override void Insert(uint frameNumber, byte commanderId, ICommand[] comma
}

_network.Send(Compressor.Compress(writer));
}
}


private void OnDataReceived(byte[] data)
{
Expand Down
14 changes: 0 additions & 14 deletions Engine/Client/Interfaces/ICommandBuffer.cs

This file was deleted.

58 changes: 28 additions & 30 deletions Engine/Client/Simulation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Lockstep.Client.Interfaces;
using Lockstep.Client.Interfaces;
using Lockstep.Core.Interfaces;
using Lockstep.Network.Messages;
using Lockstep.Network.Utils;

namespace Lockstep.Client
{
Expand All @@ -20,8 +22,7 @@ public class Simulation
public byte LocalPlayerId { get; private set; }

private float _tickDt;
private float _accumulatedTime;
private uint _lastValidatedFrame;
private float _accumulatedTime;

private readonly IWorld _world;
private readonly ICommandBuffer _remoteCommandBuffer;
Expand Down Expand Up @@ -51,6 +52,13 @@ public void Execute(ICommand command)
return;
}

if (command is ISerializableCommand s)
{
var er = new Serializer();
s.Serialize(er);
s.Deserialize(new Deserializer(er.Data));
}

lock (_commandCache)
{
_commandCache.Add(command);
Expand Down Expand Up @@ -97,35 +105,24 @@ private void Tick()

private void SyncCommandBuffer()
{
var currentRemoteFrame = _remoteCommandBuffer.LastInsertedFrame;
var commands = _remoteCommandBuffer.GetChanges();

if (_lastValidatedFrame < currentRemoteFrame)
if (commands.Count > 0)
{
//We guess everything was predicted correctly (except the last received frame)
var firstMispredictedFrame = currentRemoteFrame;

for (var remoteFrame = _lastValidatedFrame + 1; remoteFrame <= currentRemoteFrame; remoteFrame++)
{
//All frames that have no commands were predicted correctly => increase remote frame
var allPlayerCommands = _remoteCommandBuffer.Get(remoteFrame);
if (allPlayerCommands.Count == 0)
{
continue;
}
var firstMispredictedFrame = commands.Keys.Min();
var lastInputFrame = commands.Keys.Max();

if (firstMispredictedFrame > remoteFrame)
{
//Set the first mispredicted frame to the first frame which contains commands
firstMispredictedFrame = remoteFrame;
}

//TODO: if command contains entity-ids (which can be predicted) and due to rollback->fast-forward we generated local ids, the command's entity-ids have to be adjusted
//https://github.com/proepkes/UnityLockstep/wiki/Rollback-devlog
foreach (var playerCommands in allPlayerCommands)
_world.Services.Get<ILogService>().Trace(">>>Input from " + firstMispredictedFrame + " to " + lastInputFrame);

foreach (var tick in commands.Keys)
{
foreach (var actorId in commands[tick].Keys)
{
_world.AddInput(remoteFrame, playerCommands.Key, playerCommands.Value);
_world.AddInput(tick, actorId, commands[tick][actorId]);
}
}
}

//Only rollback if the mispredicted frame was in the past (the frame can be in the future due to high lag compensation)
if (firstMispredictedFrame <= _world.CurrentTick)
Expand All @@ -134,19 +131,20 @@ private void SyncCommandBuffer()

_world.RevertToTick(firstMispredictedFrame);

while (_world.CurrentTick < firstMispredictedFrame)
//Restore last local state
while (_world.CurrentTick <= lastInputFrame)
{
_world.Simulate();
}

//Restore last local state
_world.Services.Get<ILogService>().Trace(">>>Predicting up to " + targetTick);
while (_world.CurrentTick < targetTick)
{
_world.Predict();
}
}

_lastValidatedFrame = currentRemoteFrame;
_world.Services.Get<ILogService>().Trace(">>>Done: now at " + _world.CurrentTick);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.Collections.Generic;
using Lockstep.Client.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using Lockstep.Core.Interfaces;

namespace Lockstep.Client.Implementations
{
{
[Serializable]
public class CommandBuffer : ICommandBuffer
{
/// <summary>
/// Mapping: FrameNumber -> Commands per player(Id)
/// </summary>
protected Dictionary<uint, Dictionary<byte, List<ICommand>>> Buffer { get; } = new Dictionary<uint, Dictionary<byte, List<ICommand>>>(5000);

public uint LastInsertedFrame { get; private set; }
public Dictionary<uint, Dictionary<byte, List<ICommand>>> Buffer { get; } = new Dictionary<uint, Dictionary<byte, List<ICommand>>>(5000);

public virtual void Insert(uint frameNumber, byte commanderId, ICommand[] commands)
{
Expand All @@ -27,24 +27,18 @@ public virtual void Insert(uint frameNumber, byte commanderId, ICommand[] comman
Buffer[frameNumber].Add(commanderId, new List<ICommand>(5)); //Initial size of 5 commands per frame per player
}

Buffer[frameNumber][commanderId].AddRange(commands);

LastInsertedFrame = frameNumber;
Buffer[frameNumber][commanderId].AddRange(commands);
}
}

public Dictionary<byte, List<ICommand>> Get(uint frame)
public Dictionary<uint, Dictionary<byte, List<ICommand>>> GetChanges()
{
lock (Buffer)
{
//If no commands were inserted then return an empty list
if (!Buffer.ContainsKey(frame))
{
Buffer.Add(frame, new Dictionary<byte, List<ICommand>>());
}

return Buffer[frame];
{
var result = Buffer.ToDictionary(pair => pair.Key, pair => pair.Value);
Buffer.Clear();
return result;
}
}
}
}
}
2 changes: 1 addition & 1 deletion Engine/Core/Components/Input/TargetPlayerIdComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Lockstep.Core.Components.Input
{
[Input]
public class TargetPlayerIdComponent : IComponent
public class TargetActorIdComponent : IComponent
{
public byte value;
}
Expand Down
15 changes: 10 additions & 5 deletions Engine/Core/Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -56,6 +56,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommandBuffer.cs" />
<Compile Include="DefaultServices\DefaultIDebugService.cs" />
<Compile Include="GameLog.cs" />
<Compile Include="Generated\GameState\Components\GameStatePredictingComponent.cs" />
<Compile Include="Generated\GameState\Components\GameStateTickComponent.cs" />
<Compile Include="Generated\GameState\Components\GameStateHashCodeComponent.cs" />
Expand All @@ -78,7 +81,7 @@
<Compile Include="Generated\Game\Components\GamePositionComponent.cs" />
<Compile Include="Generated\Game\Components\GameTeamComponent.cs" />
<Compile Include="Generated\Game\Components\GameHashableComponent.cs" />
<Compile Include="Generated\Input\Components\InputTargetPlayerIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputTargetActorIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputTickComponent.cs" />
<Compile Include="Generated\Input\Components\InputCoordinateComponent.cs" />
<Compile Include="Generated\Input\Components\InputEntityConfigIdComponent.cs" />
Expand Down Expand Up @@ -152,7 +155,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Game\BackupComponent.cs" />
<Compile Include="Interfaces\ICommandBuffer.cs" />
<Compile Include="Interfaces\IDebugService.cs" />
<Compile Include="Interfaces\ISnapshotIndexService.cs" />
<Compile Include="Systems\Debugging\VerifySelectionIdExists.cs" />
<Compile Include="Systems\OnNewPredictionCreateBackup.cs" />
<Compile Include="Systems\Debugging\VerifyNoDuplicateBackups.cs" />
<Compile Include="World.cs" />
Expand Down Expand Up @@ -191,7 +197,6 @@
<Compile Include="DefaultServices\Navigation\Agent.cs" />
<Compile Include="DefaultServices\Navigation\DefaultNavigationService.cs" />
<Compile Include="Interfaces\IService.cs" />
<Compile Include="Features\HashCodeFeature.cs" />
<Compile Include="Features\InputFeature.cs" />
<Compile Include="Features\NavigationFeature.cs" />
<Compile Include="Interfaces\IViewService.cs" />
Expand All @@ -207,10 +212,10 @@
<Compile Include="Systems\RemoveNewFlag.cs" />
<Compile Include="Systems\GameState\IncrementTick.cs" />
<Compile Include="Systems\Input\CleanupInput.cs" />
<Compile Include="Systems\Input\OnSpawnInputCreateEntity.cs" />
<Compile Include="Systems\Input\ExecuteSpawnInput.cs" />
<Compile Include="Systems\Navigation\NavigationTick.cs" />
<Compile Include="Systems\Navigation\OnNavigableDoRegisterAgent.cs" />
<Compile Include="Systems\Navigation\OnNavigationInputDoSetDestination.cs" />
<Compile Include="Systems\Navigation\ExecuteNavigationInput.cs" />
<Compile Include="Systems\Navigation\SyncAgentVelocity.cs" />
<Compile Include="Systems\Navigation\UpdateAgentPosition.cs" />
<Compile Include="Util.cs" />
Expand Down
23 changes: 18 additions & 5 deletions Engine/Core/DefaultServices/DefaultHashService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using Lockstep.Core.Interfaces;

namespace Lockstep.Core.DefaultServices
{
class DefaultHashService : IHashService
public class DefaultHashService : IHashService
{
public long CalculateHashCode(IEnumerable<GameEntity> entities)
public long CalculateHashCode(IEnumerable<GameEntity> hashableEntities, GameStateContext context, ILogService logger)
{
long hashCode = 0;
foreach (var entity in entities)
foreach (var entity in hashableEntities.OrderBy(entity => entity.localId.value))
{
hashCode ^= entity.position.value.X.RawValue;
hashCode ^= entity.position.value.Y.RawValue;
hashCode ^= CalculateHashCode(entity);
if (context.tick.value > 170 && context.tick.value < 175 && entity.actorId.value == 2 && entity.id.value == 89)
//if (context.tick.value == 38)
{
logger.Warn(entity.actorId.value + "/" + entity.id.value + ": (" + entity.position.value + ")");
}
}
return hashCode;
}

public long CalculateHashCode(GameEntity entity)
{
long hashCode = 0;
hashCode ^= entity.position.value.X.RawValue;
hashCode ^= entity.position.value.Y.RawValue;
return hashCode;
}
}
}
Loading

0 comments on commit 0eccbe6

Please sign in to comment.