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 #27 from proepkes/develop
Browse files Browse the repository at this point in the history
Composite key pes GameEntity: OwnerId + ID
  • Loading branch information
proepkes authored Jan 31, 2019
2 parents 26c4bf3 + 9ee3353 commit 8c4a48e
Show file tree
Hide file tree
Showing 38 changed files with 584 additions and 336 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,5 @@ Unity/Assembly-CSharp-Editor\.csproj
/Unity/Assets/Editor Default Resources
/Unity/Assets/Editor Default Resources.meta
/Unity/Assets/Debug.meta
/Unity/Assets/GitIgnored
/Unity/Assets/GitIgnored.meta
26 changes: 14 additions & 12 deletions Engine/Client/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Simulation
private readonly IWorld _world;
private readonly ICommandBuffer _remoteCommandBuffer;

private readonly Dictionary<byte, List<ICommand>> _commandCache = new Dictionary<byte, List<ICommand>>();
private readonly List<ICommand> _commandCache = new List<ICommand>();

public Simulation(IWorld world, ICommandBuffer remoteCommandBuffer)
{
Expand All @@ -35,9 +35,7 @@ public void Initialize(Init init)
{
_tickDt = 1000f / init.TargetFPS;
LocalPlayerId = init.PlayerID;

_commandCache.Add(LocalPlayerId, new List<ICommand>());


_world.Initialize(LocalPlayerId);

Running = true;
Expand All @@ -52,7 +50,7 @@ public void Execute(ICommand command)

lock (_commandCache)
{
_commandCache[LocalPlayerId].Add(command);
_commandCache.Add(command);
}
}

Expand All @@ -79,12 +77,12 @@ private void Tick()
{
lock (_commandCache)
{
if (_commandCache[LocalPlayerId].Count > 0)
if (_commandCache.Count > 0)
{
_world.AddInput(_world.CurrentTick + LagCompensation, _commandCache);
_remoteCommandBuffer.Insert(_world.CurrentTick + LagCompensation, LocalPlayerId, _commandCache[LocalPlayerId].ToArray());
_world.AddInput(_world.CurrentTick + LagCompensation, LocalPlayerId, _commandCache);
_remoteCommandBuffer.Insert(_world.CurrentTick + LagCompensation, LocalPlayerId, _commandCache.ToArray());

_commandCache[LocalPlayerId].Clear();
_commandCache.Clear();
}
}

Expand Down Expand Up @@ -113,10 +111,14 @@ private void SyncCommandBuffer()
{
//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 we generated local ids, the command's entity-ids have to be adjusted
_world.AddInput(remoteFrame, allPlayerCommands);
//TODO: if command contains entity-ids (which can be predicted) and due to rollback we generated local ids, the command's entity-ids have to be adjusted
//https://github.com/proepkes/UnityLockstep/wiki/Rollback-WIP-Log
foreach (var playerCommands in allPlayerCommands)
{
_world.AddInput(remoteFrame, playerCommands.Key, playerCommands.Value);
}
}

//Only rollback if the mispredicted frame was in the past (the frame can be in the future due to high lag compensation)
Expand Down
6 changes: 2 additions & 4 deletions Engine/Core/Components/Game/IdComponent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Entitas;
using Entitas.CodeGeneration.Attributes;
using Entitas;

namespace Lockstep.Core.Components.Game
{
[Game]
public sealed class IdComponent : IComponent
{
[PrimaryEntityIndex]
{
public uint value;
}
}
12 changes: 12 additions & 0 deletions Engine/Core/Components/Game/InternalIdComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Entitas;
using Entitas.CodeGeneration.Attributes;

namespace Lockstep.Core.Components.Game
{
[Game]
public sealed class LocalIdComponent : IComponent
{
[PrimaryEntityIndex]
public uint value;
}
}
5 changes: 1 addition & 4 deletions Engine/Core/Components/Game/ShadowComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ namespace Lockstep.Core.Components.Game
[Game]
//A shadow refers to an entity in the past
public class ShadowComponent : IComponent
{
public uint entityId;

public uint tick;
{
}
}
10 changes: 10 additions & 0 deletions Engine/Core/Components/Game/TickComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Entitas;

namespace Lockstep.Core.Components.Game
{
[Game]
public class TickComponent : IComponent
{
public uint value;
}
}
10 changes: 10 additions & 0 deletions Engine/Core/Components/Input/CommanderId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Entitas;

namespace Lockstep.Core.Components.Input
{
[Input]
public class PlayerId : IComponent
{
public byte value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Lockstep.Core.Components.Input
{
[Input]
public class PlayerIdComponent : IComponent
public class TargetPlayerIdComponent : IComponent
{
public byte value;
}
Expand Down
30 changes: 21 additions & 9 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,15 +56,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DefaultServices\DefaultPlayerEntityIdProvider.cs" />
<Compile Include="Generated\GameState\Components\GameStateTickComponent.cs" />
<Compile Include="Generated\GameState\Components\GameStateHashCodeComponent.cs" />
<Compile Include="Generated\GameState\Components\GameStatePlayerIdComponent.cs" />
<Compile Include="Generated\GameState\Components\GameStatePausedComponent.cs" />
<Compile Include="Generated\Input\Components\InputTickComponent.cs" />
<Compile Include="Generated\Input\Components\InputCoordinateComponent.cs" />
<Compile Include="Generated\Input\Components\InputEntityConfigIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputPlayerIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputSelectionComponent.cs" />
<Compile Include="Generated\Game\Components\GameTickComponent.cs" />
<Compile Include="Generated\Game\Components\GameLocalIdComponent.cs" />
<Compile Include="Generated\Game\Components\GameDestinationComponent.cs" />
<Compile Include="Generated\Game\Components\GameNewComponent.cs" />
<Compile Include="Generated\Game\Components\GameShadowComponent.cs" />
Expand All @@ -79,10 +77,16 @@
<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\InputTickComponent.cs" />
<Compile Include="Generated\Input\Components\InputCoordinateComponent.cs" />
<Compile Include="Generated\Input\Components\InputEntityConfigIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputPlayerIdComponent.cs" />
<Compile Include="Generated\Input\Components\InputSelectionComponent.cs" />
<Compile Include="Generated\Game\Components\GameDestinationListenerComponent.cs" />
<Compile Include="Generated\Game\Components\GamePositionListenerComponent.cs" />
<Compile Include="Generated\Input\InputComponentsLookup.cs" />
<Compile Include="Generated\Game\GameComponentsLookup.cs" />
<Compile Include="Generated\Input\InputComponentsLookup.cs" />
<Compile Include="Generated\GameState\GameStateComponentsLookup.cs" />
<Compile Include="Generated\Config\ConfigComponentsLookup.cs" />
<Compile Include="Generated\Game\GameAttribute.cs" />
Expand Down Expand Up @@ -111,6 +115,13 @@
<Compile Include="Generated\Events\GameEventSystems.cs" />
<Compile Include="Generated\Feature.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Game\TickComponent.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Game\InternalIdComponent.cs" />
<Compile Include="Components\Input\TargetPlayerIdComponent.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Input\TickComponent.cs" />
</ItemGroup>
Expand All @@ -122,7 +133,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Game\ShadowComponent.cs" />
<Compile Include="GameSystems.cs" />
<Compile Include="Interfaces\IIdProviderService.cs" />
<Compile Include="World.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\GameState\TickComponent.cs" />
Expand All @@ -148,7 +160,7 @@
<ItemGroup>
<Compile Include="Components\Input\CoordinateComponent.cs" />
<Compile Include="Components\Input\EntityConfigIdComponent.cs" />
<Compile Include="Components\Input\PlayerIdComponent.cs" />
<Compile Include="Components\Input\CommanderId.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Game\HashableComponent.cs" />
Expand Down
36 changes: 36 additions & 0 deletions Engine/Core/DefaultServices/DefaultPlayerEntityIdProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using Lockstep.Core.Interfaces;

namespace Lockstep.Core.DefaultServices
{
/// <summary>
/// Every player has its own id-counter
/// </summary>
public class DefaultPlayerEntityIdProvider : IPlayerEntityIdProvider
{
private readonly Dictionary<byte, uint> _idMap = new Dictionary<byte, uint>();

public uint Get(byte key)
{
return _idMap[key];
}

public uint GetNext(byte key)
{
if (!_idMap.ContainsKey(key))
{
_idMap.Add(key, 0);
}

var nextId = _idMap[key];
_idMap[key] += 1;
return nextId;
}

public void SetNext(byte key, uint value)
{
_idMap[key] = value;
}
}
}
5 changes: 3 additions & 2 deletions Engine/Core/Features/InputFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ public sealed class InputFeature : Feature
{
public InputFeature(Contexts contexts, ServiceContainer serviceContainer)
{
//TODO: Add InputValidationSystem using input's playerIdComponent
//TODO: Add InputValidationSystem

Add(new OnSpawnInputCreateEntity(contexts, serviceContainer));


//TODO: Add CleanupInput that removes input of validated frames (no rollback required => can be removed)
//Add(new CleanupInput(contexts));
}

}
}
Loading

0 comments on commit 8c4a48e

Please sign in to comment.