11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Linq ;
45using System . Threading . Tasks ;
@@ -24,7 +25,7 @@ namespace Concealment
2425 [ Plugin ( "Concealment" , "1.0" , "17f44521-b77a-4e85-810f-ee73311cf75d" ) ]
2526 public class ConcealmentPlugin : TorchPluginBase , IWpfPlugin
2627 {
27- public Settings Settings { get ; }
28+ public Persistent < Settings > Settings { get ; }
2829 public MTObservableCollection < ConcealGroup > ConcealGroups { get ; } = new MTObservableCollection < ConcealGroup > ( ) ;
2930
3031 private static readonly Logger Log = LogManager . GetLogger ( "Concealment" ) ;
@@ -35,11 +36,11 @@ public class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
3536 private readonly List < ConcealGroup > _intersectGroups ;
3637 private MyDynamicAABBTreeD _concealedAabbTree ;
3738
38-
3939 public ConcealmentPlugin ( )
4040 {
4141 _intersectGroups = new List < ConcealGroup > ( ) ;
42- Settings = Settings . LoadOrCreate ( "Concealment.cfg" ) ;
42+ Settings = Persistent < Settings > . Load ( "Concealment.cfg" ) ;
43+ Settings . Data . RevealNeeded += ( ) => RevealNearbyGrids ( Settings . Data . RevealDistance ) ;
4344 }
4445
4546 public UserControl GetControl ( )
@@ -51,17 +52,23 @@ public override void Init(ITorchBase torch)
5152 {
5253 base . Init ( torch ) ;
5354 _concealedAabbTree = new MyDynamicAABBTreeD ( MyConstants . GAME_PRUNING_STRUCTURE_AABB_EXTENSION ) ;
55+ torch . SessionUnloading += Torch_SessionUnloading ;
56+ }
57+
58+ private void Torch_SessionUnloading ( )
59+ {
60+ RevealAll ( ) ;
5461 }
5562
5663 public override void Update ( )
5764 {
5865 if ( MyAPIGateway . Session == null )
5966 return ;
6067
61- if ( _counter % Settings . ConcealInterval == 0 )
62- ConcealDistantGrids ( Settings . ConcealDistance ) ;
63- if ( _counter % Settings . RevealInterval == 0 )
64- RevealNearbyGrids ( Settings . RevealDistance ) ;
68+ if ( _counter % Settings . Data . ConcealInterval == 0 )
69+ ConcealDistantGrids ( Settings . Data . ConcealDistance ) ;
70+ if ( _counter % Settings . Data . RevealInterval == 0 )
71+ RevealNearbyGrids ( Settings . Data . RevealDistance ) ;
6572 _counter += 1 ;
6673
6774 if ( _init )
@@ -75,6 +82,7 @@ public override void Update()
7582
7683 public override void Dispose ( )
7784 {
85+ RevealAll ( ) ;
7886 Settings . Save ( "Concealment.cfg" ) ;
7987 }
8088
@@ -123,11 +131,16 @@ private void RevealSpawns(PlayerRequestArgs args)
123131 private void ConcealEntity ( IMyEntity entity )
124132 {
125133 if ( entity != entity . GetTopMostParent ( ) )
126- throw new InvalidOperationException ( "Can only conceal top-level entities." ) ;
134+ return ;
135+
136+ if ( Settings . Data . ManagePhysics )
137+ {
138+ MyGamePruningStructure . Remove ( ( MyEntity ) entity ) ;
139+ entity . Physics ? . Deactivate ( ) ;
140+ }
127141
128- MyGamePruningStructure . Remove ( ( MyEntity ) entity ) ;
129- entity . Physics ? . Deactivate ( ) ;
130- UnregisterRecursive ( entity ) ;
142+ if ( Settings . Data . ManageGamelogic )
143+ UnregisterRecursive ( entity ) ;
131144
132145 void UnregisterRecursive ( IMyEntity e )
133146 {
@@ -143,11 +156,16 @@ void UnregisterRecursive(IMyEntity e)
143156 private void RevealEntity ( IMyEntity entity )
144157 {
145158 if ( entity != entity . GetTopMostParent ( ) )
146- throw new InvalidOperationException ( "Can only conceal top-level entities." ) ;
159+ return ;
147160
148- MyGamePruningStructure . Add ( ( MyEntity ) entity ) ;
149- entity . Physics ? . Activate ( ) ;
150- RegisterRecursive ( entity ) ;
161+ if ( Settings . Data . ManagePhysics )
162+ {
163+ MyGamePruningStructure . Add ( ( MyEntity ) entity ) ;
164+ entity . Physics ? . Activate ( ) ;
165+ }
166+
167+ if ( Settings . Data . ManageGamelogic )
168+ RegisterRecursive ( entity ) ;
151169
152170 void RegisterRecursive ( IMyEntity e )
153171 {
@@ -176,9 +194,15 @@ private int ConcealGroup(ConcealGroup group)
176194 Log . Debug ( $ "Group { group . Id } cached") ;
177195 Torch . Invoke ( ( ) => _concealGroups . Add ( group ) ) ;
178196 } ) ;
197+ group . Closing += Group_Closing ;
179198 return group . Grids . Count ;
180199 }
181200
201+ private void Group_Closing ( ConcealGroup group )
202+ {
203+ RevealGroup ( group ) ;
204+ }
205+
182206 public int RevealGroup ( ConcealGroup group )
183207 {
184208 Log . Info ( $ "Revealing grids: { string . Join ( ", " , group . Grids . Select ( g => g . DisplayName ) ) } ") ;
@@ -201,7 +225,8 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
201225
202226 public int RevealNearbyGrids ( double distanceFromPlayers )
203227 {
204- Log . Debug ( "Revealing nearby grids" ) ;
228+ //annoying log spam
229+ //Log.Debug("Revealing nearby grids");
205230 var revealed = 0 ;
206231 var playerSpheres = GetPlayerBoundingSpheres ( distanceFromPlayers ) ;
207232 foreach ( var sphere in playerSpheres )
@@ -213,21 +238,44 @@ public int RevealNearbyGrids(double distanceFromPlayers)
213238 public int ConcealDistantGrids ( double distanceFromPlayers )
214239 {
215240 Log . Debug ( "Concealing distant grids" ) ;
216- var concealed = 0 ;
241+ int concealed = 0 ;
217242 var playerSpheres = GetPlayerBoundingSpheres ( distanceFromPlayers ) ;
218243
219- foreach ( var group in MyCubeGridGroups . Static . Physical . Groups )
244+ ConcurrentBag < ConcealGroup > groups = new ConcurrentBag < ConcealGroup > ( ) ;
245+ Parallel . ForEach ( MyCubeGridGroups . Static . Physical . Groups , group =>
220246 {
247+ var concealGroup = new ConcealGroup ( group ) ;
248+
221249 var volume = group . GetWorldAABB ( ) ;
222250 if ( playerSpheres . Any ( s => s . Contains ( volume ) != ContainmentType . Disjoint ) )
223- continue ;
251+ return ;
224252
225- concealed += ConcealGroup ( new ConcealGroup ( group ) ) ;
253+ //if (IsExcluded(concealGroup))
254+ // return;
255+
256+ groups . Add ( concealGroup ) ;
257+ } ) ;
258+ foreach ( var group in groups )
259+ {
260+ concealed += ConcealGroup ( group ) ;
226261 }
227262
228263 return concealed ;
229264 }
230265
266+ public bool IsExcluded ( ConcealGroup group )
267+ {
268+ foreach ( var block in group . Grids . SelectMany ( g => g . CubeBlocks ) . Select ( x => x . FatBlock ) )
269+ {
270+ if ( block == null )
271+ continue ;
272+ if ( Settings . Data . ExcludedSubtypes . Contains ( block . BlockDefinition . Id . SubtypeName ) )
273+ return false ;
274+ }
275+
276+ return true ;
277+ }
278+
231279 public int RevealAll ( )
232280 {
233281 Log . Debug ( "Revealing all grids" ) ;
0 commit comments