2929using VRage . Game . ModAPI ;
3030using VRage . Game . ObjectBuilders . ComponentSystem ;
3131using VRage . ModAPI ;
32- using VRage . Network ;
3332using VRageMath ;
3433
3534namespace Concealment
@@ -51,10 +50,6 @@ public sealed class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
5150 private bool _settingsChanged ;
5251 private bool _ready ;
5352
54- private MyReplicationServer Replication => ( MyReplicationServer ) MyMultiplayer . Static . ReplicationLayer ;
55-
56- public static ConcealmentPlugin Instance { get ; private set ; }
57-
5853 public ConcealmentPlugin ( )
5954 {
6055 _intersectGroups = new List < ConcealGroup > ( ) ;
@@ -81,8 +76,6 @@ public override void Init(ITorchBase torch)
8176 Settings . Data . PropertyChanged += Data_PropertyChanged ;
8277 _concealedAabbTree = new MyDynamicAABBTreeD ( MyConstants . GAME_PRUNING_STRUCTURE_AABB_EXTENSION ) ;
8378 torch . Managers . GetManager < ITorchSessionManager > ( ) ? . AddFactory ( CreateManager ) ;
84-
85- Instance = this ;
8679 }
8780
8881 private IManager CreateManager ( ITorchSession session )
@@ -104,7 +97,9 @@ public override void Update()
10497 if ( _ready )
10598 {
10699 if ( _counter % ( ulong ) Settings . Data . ConcealInterval == 0 )
107- ConcealGrids ( ) ;
100+ ConcealGrids ( Settings . Data . ConcealDistance ) ;
101+ if ( _counter % ( ulong ) Settings . Data . RevealInterval == 0 )
102+ RevealGrids ( Settings . Data . RevealDistance ) ;
108103 _counter += 1 ;
109104 }
110105
@@ -244,9 +239,8 @@ public int RevealGroup(ConcealGroup group)
244239 {
245240 if ( ! group . IsConcealed )
246241 {
247- Log . Trace ( $ "Group for reveal is not concealed: { group . GridNames } ") ;
248- //Log.Warn($"Attempted to reveal a group that wasn't concealed: {group.GridNames}");
249- //Log.Warn(new StackTrace());
242+ Log . Warn ( $ "Attempted to reveal a group that wasn't concealed: { group . GridNames } ") ;
243+ Log . Warn ( new StackTrace ( ) ) ;
250244 return 0 ;
251245 }
252246
@@ -274,93 +268,70 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
274268 return revealed ;
275269 }
276270
277- // public int RevealGrids()
278- // {
279- // var revealed = 0;
280-
281- // foreach (var sphere in playerSpheres)
282- // {
283- // Log.Trace($"{sphere.Center}: {sphere.Radius}");
284- // revealed += RevealGridsInSphere(sphere);
285- // }
286-
287- // if (_settingsChanged)
288- // {
289- // for (var i = ConcealedGroups.Count - 1; i >= 0; i--)
290- // {
291- // if (IsExcluded(ConcealedGroups[i]))
292- // revealed += RevealGroup(ConcealedGroups[i]);
293- // }
294-
295- // _settingsChanged = false;
296- // }
297-
298- // if (revealed != 0)
299- // Log.Info($"Revealed {revealed} grids near players.");
300- // return revealed;
301- // }
302-
303- public ( int , int ) ConcealGrids ( )
271+ public int RevealGrids ( double distanceFromPlayers )
272+ {
273+ var revealed = 0 ;
274+ var playerSpheres = GetPlayerViewSpheres ( distanceFromPlayers ) ;
275+ foreach ( var sphere in playerSpheres )
276+ {
277+ Log . Trace ( $ "{ sphere . Center } : { sphere . Radius } ") ;
278+ revealed += RevealGridsInSphere ( sphere ) ;
279+ }
280+
281+ if ( _settingsChanged )
282+ {
283+ for ( var i = ConcealedGroups . Count - 1 ; i >= 0 ; i -- )
284+ {
285+ if ( IsExcluded ( ConcealedGroups [ i ] ) )
286+ revealed += RevealGroup ( ConcealedGroups [ i ] ) ;
287+ }
288+
289+ _settingsChanged = false ;
290+ }
291+
292+ if ( revealed != 0 )
293+ Log . Info ( $ "Revealed { revealed } grids near players.") ;
294+ return revealed ;
295+ }
296+
297+ public int ConcealGrids ( double distanceFromPlayers = 0 )
304298 {
305299 Log . Debug ( "Concealing grids" ) ;
306300 int concealed = 0 ;
307- int revealed = 0 ;
301+ var playerSpheres = GetPlayerViewSpheres ( distanceFromPlayers ) ;
308302
309303 ConcurrentBag < ConcealGroup > groups = new ConcurrentBag < ConcealGroup > ( ) ;
310- ConcurrentBag < ConcealGroup > reveal = new ConcurrentBag < ConcealGroup > ( ) ;
311304 var sw = Stopwatch . StartNew ( ) ;
312305 Parallel . ForEach ( MyCubeGridGroups . Static . Physical . Groups , group =>
313306 {
314307 var concealGroup = new ConcealGroup ( group ) ;
315308
316- if ( IsExcluded ( concealGroup ) )
309+ if ( distanceFromPlayers != 0 )
317310 {
318- Log . Trace ( "group excluded" ) ;
319- return ;
311+ var volume = group . GetWorldAABB ( ) ;
312+ if ( playerSpheres . Any ( s => s . Contains ( volume ) != ContainmentType . Disjoint ) )
313+ {
314+ Log . Trace ( "group near player" ) ;
315+ return ;
316+ }
320317 }
321318
322- //Checks if any connected clients have requested any grid in this group for replication
323- if ( concealGroup . Grids . Any ( g => Replication . IsReplicated ( Utilities . GetReplicable ( g ) ) ) )
319+ if ( IsExcluded ( concealGroup ) )
324320 {
325- Log . Trace ( "group in replication " ) ;
321+ Log . Trace ( "group excluded " ) ;
326322 return ;
327323 }
328324
329325 groups . Add ( concealGroup ) ;
330326 } ) ;
331- Log . Debug ( $ "Scanned conceal grids in { sw . ElapsedMilliseconds } ms.") ;
332- sw . Restart ( ) ;
333-
334- Parallel . ForEach ( ConcealedGroups , group =>
335- {
336- if ( IsExcluded ( group ) )
337- {
338- reveal . Add ( group ) ;
339- return ;
340- }
341-
342- if ( group . Grids . Any ( grid => Replication . IsReplicated ( Utilities . GetReplicable ( grid ) ) ) )
343- {
344- reveal . Add ( group ) ;
345- return ;
346- }
347- } ) ;
348- Log . Debug ( $ "Scanned reveal grids in { sw . ElapsedMilliseconds } ms") ;
327+ Log . Debug ( $ "Scanned grids in { sw . ElapsedMilliseconds } ms.") ;
349328 sw . Restart ( ) ;
350329
351330 foreach ( var group in groups )
352331 {
353332 concealed += ConcealGroup ( group ) ;
354333 }
355334 Log . Debug ( $ "Concealed grids in { sw . ElapsedMilliseconds } ms.") ;
356- sw . Restart ( ) ;
357-
358- foreach ( var group in reveal )
359- {
360- revealed += RevealGroup ( group ) ;
361- }
362-
363- Log . Debug ( $ "Revealed grids in { sw . ElapsedMilliseconds } ms") ;
364335 sw . Stop ( ) ;
365336
366337 var concealedCount = ConcealedGroups . SelectMany ( x => x . Grids ) . Count ( ) ;
@@ -369,10 +340,7 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
369340 if ( concealed > 0 )
370341 Log . Info ( $ "{ concealedCount + concealed } /{ totalCount } grids are concealed ({ concealedCount + concealed / ( float ) totalCount : P} ), { concealed } new.") ;
371342
372- if ( revealed > 0 )
373- Log . Info ( $ "Revealed { revealed } grids replicated to players.") ;
374-
375- return ( concealed , revealed ) ;
343+ return concealed ;
376344 }
377345
378346 public bool IsExcluded ( ConcealGroup group )
0 commit comments