66using Havok ;
77using NAPI ;
88using Sandbox ;
9- using Sandbox . Engine . Physics ;
109using Sandbox . Engine . Voxels ;
1110using Sandbox . Game . Components ;
1211using Sandbox . Game . Entities ;
@@ -79,19 +78,30 @@ private void UnfreezeGrids(HashSet<MyCubeGrid> grids, bool isWakeUpTime)
7978 }
8079
8180 var groupWithFixedGrid = GroupContainsFixedGrid ( grids ) ;
82- foreach ( var grid in grids )
81+
82+ var realyNeedToUnFreezeGrids = grids . Where ( grid =>
8383 {
8484 if ( ! FrozenGrids . Contains ( grid . EntityId ) )
8585 {
86- continue ;
86+ return false ;
87+ }
88+
89+ if ( grid . IsPreview )
90+ {
91+ return false ;
8792 }
8893
94+ return true ;
95+ } ) . ToList ( ) ;
96+ foreach ( var grid in realyNeedToUnFreezeGrids )
97+ {
98+
8999 if ( ! isWakeUpTime )
90100 {
91101 WakeUpDatas . Remove ( grid . EntityId ) ;
92102 }
93103
94- if ( grid . Parent == null && ! grid . IsPreview )
104+ if ( grid . Parent == null )
95105 {
96106 Log ( "Unfreeze grid " + grid . DisplayName ) ;
97107 FrozenGrids . Remove ( grid . EntityId ) ;
@@ -101,25 +111,30 @@ private void UnfreezeGrids(HashSet<MyCubeGrid> grids, bool isWakeUpTime)
101111 }
102112
103113 CompensateFrozenFrames ( grid ) ;
104- MyAPIGateway . Utilities . InvokeOnGameThread ( ( ) =>
114+
115+ }
116+
117+ }
118+ MyAPIGateway . Utilities . InvokeOnGameThread ( ( ) =>
119+ {
120+ foreach ( var grid in realyNeedToUnFreezeGrids )
121+ {
122+ if ( ! grid . IsStatic )
105123 {
106- if ( ! grid . IsStatic )
107- {
108- var gridPhysics = grid . Physics ;
124+ var gridPhysics = grid . Physics ;
109125
110- if ( gridPhysics != null && SentisOptimisationsPlugin . Config . FreezePhysics &&
111- ! groupWithFixedGrid )
112- {
113- DoUnfreezePhysics ( grid ) ;
114- FrozenPhysicsGrids . Remove ( grid . EntityId ) ;
115- }
126+ if ( gridPhysics != null && SentisOptimisationsPlugin . Config . FreezePhysics &&
127+ ! groupWithFixedGrid )
128+ {
129+ DoUnfreezePhysics ( grid ) ;
130+ FrozenPhysicsGrids . Remove ( grid . EntityId ) ;
116131 }
132+ }
117133
118- RegisterRecursive ( grid ) ;
119- grid . PlayerPresenceTier = MyUpdateTiersPlayerPresence . Normal ;
120- } ) ;
134+ RegisterRecursive ( grid ) ;
135+ grid . PlayerPresenceTier = MyUpdateTiersPlayerPresence . Normal ;
121136 }
122- }
137+ } ) ;
123138 }
124139
125140 private static void CompensateFrozenFrames ( MyCubeGrid grid )
@@ -245,12 +260,12 @@ private void FreezeGrids(HashSet<MyCubeGrid> grids)
245260 {
246261 try
247262 {
248- foreach ( var grid in new List < MyCubeGrid > ( needToFreezeGrids ) )
263+ var realyNeedToFreezeGrids = grids . Where ( grid =>
249264 {
250- if ( grid == null || grid . Closed || grid . MarkedForClose || grid . IsPreview ) continue ;
265+ if ( grid == null || grid . Closed || grid . MarkedForClose || grid . IsPreview ) return false ;
251266 if ( FrozenGrids . Contains ( grid . EntityId ) )
252267 {
253- continue ;
268+ return false ;
254269 }
255270
256271 var isInQueue = false ;
@@ -259,30 +274,40 @@ private void FreezeGrids(HashSet<MyCubeGrid> grids)
259274 isInQueue = InFreezeQueue . Contains ( minEntityId ) ;
260275 }
261276
262- if ( grid . Parent == null && isInQueue )
277+ return grid . Parent == null && isInQueue ;
278+ } ) . ToList ( ) ;
279+
280+ MyAPIGateway . Utilities . InvokeOnGameThread ( ( ) =>
281+ {
282+ foreach ( var grid in realyNeedToFreezeGrids )
263283 {
284+ if ( grid == null || grid . Closed || grid . MarkedForClose || grid . IsPreview ) continue ;
285+
264286 Log ( "Freeze grid " + grid . DisplayName ) ;
265- MyAPIGateway . Utilities . InvokeOnGameThread ( ( ) =>
287+
288+ if ( ! grid . IsStatic )
266289 {
267- if ( ! grid . IsStatic )
290+ var gridPhysics = grid . Physics ;
291+ if ( gridPhysics != null )
268292 {
269- var gridPhysics = grid . Physics ;
270- if ( gridPhysics != null )
293+ gridPhysics . SetSpeeds ( Vector3 . Zero , Vector3 . Zero ) ;
294+
295+ if ( SentisOptimisationsPlugin . Config . FreezePhysics && ! groupWithFixedGrid )
271296 {
272- gridPhysics . SetSpeeds ( Vector3 . Zero , Vector3 . Zero ) ;
273- if ( SentisOptimisationsPlugin . Config . FreezePhysics && ! groupWithFixedGrid )
274- {
275- DoFreezePhysics ( grid ) ;
276- }
297+ DoFreezePhysics ( grid ) ;
277298 }
299+ grid . RaisePhysicsChanged ( ) ;
278300 }
301+ }
279302
280- FrozenGrids . Add ( grid . EntityId ) ;
281-
282- UnregisterRecursive ( grid ) ;
283- } ) ;
303+ FrozenGrids . Add ( grid . EntityId ) ;
304+ UnregisterRecursive ( grid ) ;
284305 }
306+ } ) ;
285307
308+ foreach ( var grid in realyNeedToFreezeGrids )
309+ {
310+ if ( grid == null || grid . Closed || grid . MarkedForClose || grid . IsPreview ) continue ;
286311 foreach ( var myCubeBlock in grid . GetFatBlocks ( ) )
287312 {
288313 if ( ! ( myCubeBlock is MyFunctionalBlock ) )
@@ -349,37 +374,8 @@ private static void DoFreezePhysics(MyCubeGrid grid)
349374 try
350375 {
351376 var gridPhysics = grid . Physics ;
352-
353- if ( new HashSet < HkConstraint > ( gridPhysics . Constraints ) . Any ( constraint =>
354- {
355- if ( ! ( constraint . ConstraintData is HkFixedConstraintData ) )
356- {
357- return false ;
358- }
359-
360- if ( constraint . RigidBodyB == null || constraint . RigidBodyB . UserObject == null )
361- {
362- return false ;
363- }
364-
365- if ( constraint . RigidBodyB . UserObject is MyPhysicsBody phb )
366- {
367- if ( phb . Entity is null or MyShipConnector or MyEntitySubpart )
368- {
369- return false ;
370- }
371- }
372-
373- return constraint . ConstraintData is HkFixedConstraintData && constraint . Enabled ;
374- }
375- ) )
376- {
377- return ;
378- }
379-
380377 grid . Physics . Gravity = Vector3 . Zero ;
381378 gridPhysics . ConvertToStatic ( ) ;
382- grid . RaisePhysicsChanged ( ) ;
383379 FrozenPhysicsGrids . Add ( grid . EntityId ) ;
384380 }
385381 catch ( Exception e )
@@ -398,11 +394,8 @@ private static void DoUnfreezePhysics(MyCubeGrid grid)
398394
399395 gridPhysics . ConvertToDynamic ( grid . GridSizeEnum == MyCubeSize . Large , grid . IsClientPredicted ) ;
400396 gridPhysics . SetSpeeds ( Vector3 . Zero , Vector3 . Zero ) ;
401- grid . RaisePhysicsChanged ( ) ;
402397 grid . RecalculateGravity ( ) ;
403-
404- // gridPhysics.RigidBody.UpdateMotionType(HkMotionType.Box_Inertia);
405- // gridPhysics.RigidBody.Quality = HkCollidableQualityType.Moving;
398+ grid . RaisePhysicsChanged ( ) ;
406399 }
407400
408401 public static bool NeedToCompensate ( MyFunctionalBlock myCubeBlock )
0 commit comments