Skip to content

Commit 92c40cc

Browse files
committed
Fix desync issue
1 parent 59cc922 commit 92c40cc

File tree

3 files changed

+85
-70
lines changed

3 files changed

+85
-70
lines changed

Core/Game/Abilities/Essential/Move.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,11 @@ public bool GetFullCanCollisionStop ()
4949

5050
public long StopMultiplier { get; set; }
5151

52-
private bool forcePathfind { get; set; }
5352

5453
private bool collisionTicked;
55-
private int stuckTick;
5654

5755
public bool CollisionTicked { get { return collisionTicked; } }
5856

59-
public int StuckTick { get { return stuckTick; } }
6057

6158
//Has this unit arrived at destination? Default set to false.
6259
public bool Arrived { get; private set; }
@@ -177,9 +174,9 @@ protected override void OnInitialize ()
177174
hasPath = false;
178175
IsMoving = false;
179176
collisionTicked = false;
180-
stuckTick = 0;
177+
StopTimer = 0;
178+
RepathTries = 0;
181179

182-
forcePathfind = false;
183180

184181
Arrived = true;
185182
LastPosition = Agent.Body.Position;
@@ -203,7 +200,7 @@ protected override void OnSimulate ()
203200
if (viableDestination) {
204201
if (Pathfinder.GetPathNode (cachedBody._position.x, cachedBody._position.y, out currentNode)) {
205202
if (straightPath) {
206-
if (forcePathfind || Pathfinder.NeedsPath (currentNode, destinationNode, this.GridSize)) {
203+
if (Pathfinder.NeedsPath (currentNode, destinationNode, this.GridSize)) {
207204
if (Pathfinder.FindPath (Destination, currentNode, destinationNode, myPath,
208205
GridSize)) {
209206
hasPath = true;
@@ -218,7 +215,7 @@ protected override void OnSimulate ()
218215
} else {
219216
}
220217
} else {
221-
if (forcePathfind || Pathfinder.NeedsPath (currentNode, destinationNode, this.GridSize)) {
218+
if (Pathfinder.NeedsPath (currentNode, destinationNode, this.GridSize)) {
222219
if (Pathfinder.FindPath (Destination, currentNode, destinationNode, myPath,
223220
GridSize)) {
224221
hasPath = true;
@@ -382,7 +379,6 @@ protected virtual void OnArrive ()
382379
public void StopMove ()
383380
{
384381
if (IsMoving) {
385-
forcePathfind = false;
386382

387383
if (MyMovementGroup.IsNotNull ()) {
388384
MyMovementGroup.Remove (this);
@@ -392,7 +388,6 @@ public void StopMove ()
392388
StoppedTime = 0;
393389

394390
IsCasting = false;
395-
stuckTick = 0;
396391
if (OnStopMove.IsNotNull ()) {
397392
OnStopMove ();
398393
}
@@ -430,9 +425,9 @@ public void StartMove (Vector2d destination)
430425

431426
viableDestination = Pathfinder.GetPathNode (this.Destination.x, this.Destination.y, out destinationNode);
432427

428+
StopTimer = 0;
429+
RepathTries = 0;
433430
IsCasting = true;
434-
stuckTick = 0;
435-
forcePathfind = false;
436431
if (onStartMove != null)
437432
onStartMove ();
438433
}

Core/Simulation/Grid/Core/GridManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static bool UseDiagonalConnections {
3131
get {
3232
return _useDiagonalConnections;
3333
}
34-
set {
34+
private set {
3535
_useDiagonalConnections = value;
3636
}
3737
}
@@ -86,6 +86,7 @@ private static void Generate ()
8686
int min = Grid.Length;
8787
CachedGridNodes.EnsureCapacity (min);
8888
for (int i = min - 1; i >= 0; i--) {
89+
if (LockstepManager.PoolingEnabled)
8990
CachedGridNodes.Add (Grid[i]);
9091
}
9192
}
@@ -95,6 +96,7 @@ private static void Generate ()
9596
int min = ScanGrid.Length;
9697
CachedScanNodes.EnsureCapacity(min);
9798
for (int i = min - 1; i >= 0; i--) {
99+
if (LockstepManager.PoolingEnabled);
98100
CachedScanNodes.Add(ScanGrid[i]);
99101
}
100102
}
@@ -135,6 +137,7 @@ public static void Setup () {
135137
public static void Initialize ()
136138
{
137139
GridVersion = 1;
140+
GridChanged = false;
138141
if (!LockstepManager.PoolingEnabled)
139142
_settingsChanged = true;
140143
if (_settingsChanged) {

Integration/Test/TestHelper.cs

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,81 @@
22
using System.Collections;
33
using System;
44
using System.Collections.Generic;
5-
namespace Lockstep {
6-
public class TestHelper : BehaviourHelper{
7-
protected override void OnLateInitialize () {
8-
ClientManager.NetworkHelper.OnTestData += HandleOnTestData;
9-
}
10-
Dictionary<int,List<int>> frameHashes = new Dictionary<int, List<int>>();
11-
void HandleOnTestData (byte[] obj) {
12-
int frame = BitConverter.ToInt32 (obj, 0);
13-
int pos = 4;
14-
List<int> hashes = new List<int>();
15-
while (pos < obj.Length - (sizeof(int) - 1)) {
16-
int hash = BitConverter.ToInt32 (obj, pos);
17-
hashes.Add (hash);
18-
pos += sizeof (int);
19-
}
20-
if (frameHashes.ContainsKey (frame)) {
21-
frameHashes[frame].AddRange (hashes);
22-
}
23-
else {
24-
frameHashes.Add (frame,hashes);
25-
}
265

6+
namespace Lockstep
7+
{
8+
public class TestHelper : BehaviourHelper
9+
{
10+
[SerializeField]
11+
private int _checkFrameInterval = 4;
12+
int ticker;
2713

28-
}
29-
private int lastFrameSent;
30-
protected override void OnSimulate () {
31-
List<int> hashes;
32-
if (frameHashes.TryGetValue (LockstepManager.FrameCount - 10, out hashes)) {
33-
int mainHash = hashes[0];
34-
bool desynced = false;
35-
for (int i = 1; i < hashes.Count; i++) {
36-
if (mainHash != hashes[i]) {
37-
desynced = true;
38-
}
39-
}
40-
if (desynced) {
41-
Debug.LogError ("DESYCNED");
42-
}
43-
}
44-
if (LockstepManager.FrameCount > lastFrameSent) {
45-
List<byte> newMessage = new List<byte>();
46-
newMessage.AddRange (BitConverter.GetBytes (LockstepManager.FrameCount));
47-
newMessage.AddRange (BitConverter.GetBytes (LockstepManager.GetStateHash()));
48-
if (ClientManager.NetworkHelper.IsServer) {
49-
ClientManager.NetworkHelper.SendMessageToAll (MessageType.Test, newMessage.ToArray ());
50-
}
51-
{
52-
List<int> newHashes = new List<int>();
53-
newHashes.Add (LockstepManager.GetStateHash ());
54-
if (frameHashes.ContainsKey(LockstepManager.FrameCount)) {
55-
frameHashes[LockstepManager.FrameCount].AddRange (newHashes);
56-
}
57-
else {
58-
frameHashes.Add (LockstepManager.FrameCount,newHashes);
59-
}
60-
}
61-
lastFrameSent = LockstepManager.FrameCount;
62-
}
63-
}
64-
}
14+
protected override void OnLateInitialize ()
15+
{
16+
ClientManager.NetworkHelper.OnTestData += HandleOnTestData;
17+
ticker = 0;
18+
}
19+
20+
Dictionary<int,List<int>> frameHashes = new Dictionary<int, List<int>> ();
21+
22+
void HandleOnTestData (byte[] obj)
23+
{
24+
int frame = BitConverter.ToInt32 (obj, 0);
25+
int pos = 4;
26+
List<int> hashes = new List<int> ();
27+
while (pos < obj.Length - (sizeof(int) - 1)) {
28+
int hash = BitConverter.ToInt32 (obj, pos);
29+
hashes.Add (hash);
30+
pos += sizeof(int);
31+
}
32+
if (frameHashes.ContainsKey (frame)) {
33+
frameHashes [frame].AddRange (hashes);
34+
} else {
35+
frameHashes.Add (frame, hashes);
36+
}
37+
38+
39+
}
40+
41+
private int lastFrameSent;
42+
43+
protected override void OnSimulate ()
44+
{
45+
ticker++;
46+
if (ticker >= _checkFrameInterval) {
47+
ticker = 0;
48+
List<int> hashes;
49+
if (frameHashes.TryGetValue (LockstepManager.FrameCount - 10, out hashes)) {
50+
int mainHash = hashes [0];
51+
bool desynced = false;
52+
for (int i = 1; i < hashes.Count; i++) {
53+
if (mainHash != hashes [i]) {
54+
desynced = true;
55+
}
56+
}
57+
if (desynced) {
58+
Debug.LogError ("DESYCNED");
59+
}
60+
}
61+
if (LockstepManager.FrameCount > lastFrameSent) {
62+
List<byte> newMessage = new List<byte> ();
63+
newMessage.AddRange (BitConverter.GetBytes (LockstepManager.FrameCount));
64+
newMessage.AddRange (BitConverter.GetBytes (LockstepManager.GetStateHash ()));
65+
if (ClientManager.NetworkHelper.IsServer) {
66+
ClientManager.NetworkHelper.SendMessageToAll (MessageType.Test, newMessage.ToArray ());
67+
}
68+
{
69+
List<int> newHashes = new List<int> ();
70+
newHashes.Add (LockstepManager.GetStateHash ());
71+
if (frameHashes.ContainsKey (LockstepManager.FrameCount)) {
72+
frameHashes [LockstepManager.FrameCount].AddRange (newHashes);
73+
} else {
74+
frameHashes.Add (LockstepManager.FrameCount, newHashes);
75+
}
76+
}
77+
lastFrameSent = LockstepManager.FrameCount;
78+
}
79+
}
80+
}
81+
}
6582
}

0 commit comments

Comments
 (0)