Skip to content

Commit 930cedc

Browse files
committed
Fix PhysicsManager not properly resetting
1 parent 3c5a2fa commit 930cedc

File tree

9 files changed

+106
-400
lines changed

9 files changed

+106
-400
lines changed

Core/Game/Agents/AgentController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ private static void DestroyAgentBuffer (DeactivationData data)
271271

272272
public static void CacheAgent (LSAgent agent)
273273
{
274+
if (LockstepManager.PoolingEnabled)
274275
CachedAgents [agent.MyAgentCode].Add (agent);
276+
else
277+
GameObject.Destroy(agent.gameObject);
275278
}
276279

277280
private static void UpdateDiplomacy (AgentController newCont)
@@ -305,10 +308,6 @@ public static int GetStateHash ()
305308
if (operationToggle >= 2) {
306309
operationToggle = 0;
307310
}
308-
if (agent.Body.IsNotNull ()) {
309-
hash ^= agent.Body._position.GetStateHash ();
310-
hash ^= agent.Body._position.GetStateHash ();
311-
}
312311
}
313312
}
314313

Core/Game/Determinism/DeterminismTester.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Lockstep;
22
using System;
33
using UnityEngine;
4-
4+
using System.Linq;
55
namespace Lockstep
66
{
77
public class DeterminismTester : BehaviourHelper
@@ -28,14 +28,18 @@ protected override void OnSimulate()
2828
long lastHash = Hashes [LockstepManager.FrameCount];
2929
if (lastHash != hash)
3030
{
31-
Debug.Log("Desynced");
31+
Debug.Log("Desynced");// frame " + LockstepManager.FrameCount);
3232
}
3333
}
3434
} else
3535
{
3636
Hashes.InsertAt(hash, LockstepManager.FrameCount);
3737
}
38+
3839
}
39-
40+
protected override void OnDeactivate ()
41+
{
42+
iteration++;
43+
}
4044
}
4145
}

Core/Game/Managers/LockstepManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static class LockstepManager
5757

5858
public static bool Loaded { get; private set; }
5959

60+
//for testing purposes
61+
public const bool PoolingEnabled = true;
6062
private static GameManager _mainGameManager;
6163

6264
public static event Action onSetup;

Core/Simulation/Grid/Core/GridManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public static void Setup () {
135135
public static void Initialize ()
136136
{
137137
GridVersion = 1;
138-
138+
if (!LockstepManager.PoolingEnabled)
139+
_settingsChanged = true;
139140
if (_settingsChanged) {
140141
if (_settings == null)
141142
_settings = DefaultSettings;

Core/Simulation/Physics/Core/CollisionPair.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Initialize(LSBody b1, LSBody b2)
5959
}
6060

6161
_ranIndex = -1;
62-
;
62+
6363
_isColliding = false;
6464

6565
DistX = 0;
@@ -146,7 +146,7 @@ private void DistributeCollision()
146146

147147

148148

149-
if (Body1.IsTrigger || Body2.IsTrigger)
149+
if (!DoPhysics)
150150
return;
151151

152152
switch (LeCollisionType)

Core/Simulation/Physics/Core/PhysicsManager.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,38 @@ public static void Setup()
103103

104104
public static void Initialize()
105105
{
106-
Raycaster._Version = 0;
107-
PeakCount = 0;
108106

109-
CachedIDs.FastClear();
110107

111-
CollisionPair.CurrentCollisionPair = null;
112108

113-
PeakCount = 0;
114-
AssimilatedCount = 0;
115109

116-
Partition.Initialize();
110+
117111

118112
if (SettingsChanged)
119113
{
120114
SettingsChanged = false;
121115
}
122116

117+
118+
ResetVars();
119+
}
120+
121+
static void ResetVars () {
122+
for (int i = 0; i < PeakCount; i++) {
123+
SimObjects[i] = null;
124+
}
125+
DynamicSimObjects.FastClear();
126+
Raycaster._Version = 0;
127+
PeakCount = 0;
128+
CachedIDs.FastClear();
129+
130+
CollisionPair.CurrentCollisionPair = null;
131+
132+
PeakCount = 0;
133+
AssimilatedCount = 0;
134+
135+
Partition.Initialize();
136+
RanCollisionPairs.FastClear();
137+
InactiveCollisionPairs.FastClear();
123138
AccumulatedTime = 0;
124139
LastTime = 0;
125140
}
@@ -232,6 +247,7 @@ public static void LateSimulate()
232247
public static void Deactivate()
233248
{
234249
Partition.Deactivate();
250+
235251
}
236252

237253
public static float LerpTime { get; private set; }
@@ -383,6 +399,7 @@ public static void DeactivateCollisionPair(CollisionPair pair)
383399
public static void PoolPair(CollisionPair pair)
384400
{
385401
pair.Deactivate();
402+
if (LockstepManager.PoolingEnabled)
386403
CachedCollisionPairs.Add(pair);
387404
}
388405

Core/Simulation/Physics/Core/UnityLSBody.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public void Initialize(Vector3d StartPosition, Vector2d StartRotation, bool isDy
1919
{
2020
if (_internalBody.IsNull())
2121
_internalBody = new LSBody();
22-
InternalBody.transform = transform;
23-
InternalBody.Reset();
2422
InternalBody.Initialize (StartPosition,StartRotation,isDynamic);
2523
}
2624

Example/AutoSpawner.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,64 @@ public class AutoSpawner : BehaviourHelper
88
{
99
[SerializeField]
1010
private SpawnInfo[] Spawns;
11-
protected override void OnInitialize()
11+
public bool AutoCommand = true;
12+
13+
protected override void OnInitialize ()
1214
{
1315
}
14-
protected override void OnGameStart()
16+
17+
protected override void OnGameStart ()
1518
{
1619

1720

18-
for (int i = 0; i < Spawns.Length; i++)
19-
{
20-
SpawnInfo info = Spawns[i];
21-
while (AgentController.InstanceManagers.Count <= info.ControllerIndex)
22-
{
21+
for (int i = 0; i < Spawns.Length; i++) {
22+
SpawnInfo info = Spawns [i];
23+
while (AgentController.InstanceManagers.Count <= info.ControllerIndex) {
2324

24-
AgentController cont = AgentController.Create();
25-
PlayerManager.AddController(cont);
26-
for (int j = 0; j < AgentController.InstanceManagers.Count; j++)
27-
{
28-
AgentController ac = AgentController.InstanceManagers[j];
29-
if (ac != cont)
30-
{
31-
cont.SetAllegiance(ac, AllegianceType.Enemy);
32-
ac.SetAllegiance(cont, AllegianceType.Enemy);
25+
AgentController cont = AgentController.Create ();
26+
PlayerManager.AddController (cont);
27+
for (int j = 0; j < AgentController.InstanceManagers.Count; j++) {
28+
AgentController ac = AgentController.InstanceManagers [j];
29+
if (ac != cont) {
30+
cont.SetAllegiance (ac, AllegianceType.Enemy);
31+
ac.SetAllegiance (cont, AllegianceType.Enemy);
3332
}
3433
}
3534

3635
}
3736

38-
AgentController controller = AgentController.InstanceManagers[info.ControllerIndex];
37+
AgentController controller = AgentController.InstanceManagers [info.ControllerIndex];
3938

40-
for (int j = 0; j < info.Count; j++)
41-
{
42-
LSAgent agent = controller.CreateAgent(info.AgentCode, info.Position);
43-
Selector.Add(agent);
39+
for (int j = 0; j < info.Count; j++) {
40+
LSAgent agent = controller.CreateAgent (info.AgentCode, info.Position);
41+
if (AutoCommand)
42+
Selector.Add (agent);
4443
}
4544
}
4645

47-
//Find average of spawn positions
48-
Vector2d battlePos = Vector2d.zero;
49-
for (int i = 0; i < Spawns.Length; i++)
50-
{
51-
battlePos += Spawns[i].Position;
52-
}
53-
battlePos /= Spawns.Length;
54-
Command com = new Command(Lockstep.Data.AbilityDataItem.FindInterfacer<Scan>().ListenInputID);
55-
com.Add<Vector2d>(battlePos);
46+
if (AutoCommand) {
5647

57-
//PlayerManager.SendCommand(com);
48+
//Find average of spawn positions
49+
Vector2d battlePos = Vector2d.zero;
50+
for (int i = 0; i < Spawns.Length; i++) {
51+
battlePos += Spawns [i].Position;
52+
}
53+
battlePos /= Spawns.Length;
54+
Command com = new Command (Lockstep.Data.AbilityDataItem.FindInterfacer<Scan> ().ListenInputID);
55+
com.Add<Vector2d> (battlePos);
5856

59-
Selector.Clear();
57+
PlayerManager.SendCommand (com);
58+
Selector.Clear ();
59+
60+
}
6061
}
6162
}
63+
6264
[System.Serializable]
6365
public struct SpawnInfo
6466
{
6567

66-
[DataCode("Agents")]
68+
[DataCode ("Agents")]
6769
public string AgentCode;
6870
public int Count;
6971
public int ControllerIndex;

Example/ExampleScene.unity

Lines changed: 31 additions & 348 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)