2929using VRage . Game . ModAPI ;
3030using VRage . Game . ObjectBuilders . ComponentSystem ;
3131using VRage . ModAPI ;
32+ using VRage . Network ;
3233using VRageMath ;
3334
3435namespace Concealment
@@ -50,6 +51,10 @@ public sealed class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
5051 private bool _settingsChanged ;
5152 private bool _ready ;
5253
54+ private MyReplicationServer Replication => ( MyReplicationServer ) MyMultiplayer . Static . ReplicationLayer ;
55+
56+ public static ConcealmentPlugin Instance { get ; private set ; }
57+
5358 public ConcealmentPlugin ( )
5459 {
5560 _intersectGroups = new List < ConcealGroup > ( ) ;
@@ -76,6 +81,8 @@ public override void Init(ITorchBase torch)
7681 Settings . Data . PropertyChanged += Data_PropertyChanged ;
7782 _concealedAabbTree = new MyDynamicAABBTreeD ( MyConstants . GAME_PRUNING_STRUCTURE_AABB_EXTENSION ) ;
7883 torch . Managers . GetManager < ITorchSessionManager > ( ) ? . AddFactory ( CreateManager ) ;
84+
85+ Instance = this ;
7986 }
8087
8188 private IManager CreateManager ( ITorchSession session )
@@ -97,9 +104,7 @@ public override void Update()
97104 if ( _ready )
98105 {
99106 if ( _counter % ( ulong ) Settings . Data . ConcealInterval == 0 )
100- ConcealGrids ( Settings . Data . ConcealDistance ) ;
101- if ( _counter % ( ulong ) Settings . Data . RevealInterval == 0 )
102- RevealGrids ( Settings . Data . RevealDistance ) ;
107+ ConcealGrids ( ) ;
103108 _counter += 1 ;
104109 }
105110
@@ -239,8 +244,8 @@ public int RevealGroup(ConcealGroup group)
239244 {
240245 if ( ! group . IsConcealed )
241246 {
242- Log . Warn ( $ "Attempted to reveal a group that wasn't concealed: { group . GridNames } ") ;
243- Log . Warn ( new StackTrace ( ) ) ;
247+ // Log.Warn($"Attempted to reveal a group that wasn't concealed: {group.GridNames}");
248+ // Log.Warn(new StackTrace());
244249 return 0 ;
245250 }
246251
@@ -268,57 +273,57 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
268273 return revealed ;
269274 }
270275
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 )
276+ // public int RevealGrids()
277+ // {
278+ // var revealed = 0;
279+
280+ // foreach (var sphere in playerSpheres)
281+ // {
282+ // Log.Trace($"{sphere.Center}: {sphere.Radius}");
283+ // revealed += RevealGridsInSphere(sphere);
284+ // }
285+
286+ // if (_settingsChanged)
287+ // {
288+ // for (var i = ConcealedGroups.Count - 1; i >= 0; i--)
289+ // {
290+ // if (IsExcluded(ConcealedGroups[i]))
291+ // revealed += RevealGroup(ConcealedGroups[i]);
292+ // }
293+
294+ // _settingsChanged = false;
295+ // }
296+
297+ // if (revealed != 0)
298+ // Log.Info($"Revealed {revealed} grids near players.");
299+ // return revealed;
300+ // }
301+
302+ public ( int , int ) ConcealGrids ( )
298303 {
299304 Log . Debug ( "Concealing grids" ) ;
300305 int concealed = 0 ;
301- var playerSpheres = GetPlayerViewSpheres ( distanceFromPlayers ) ;
306+ int revealed = 0 ;
302307
303308 ConcurrentBag < ConcealGroup > groups = new ConcurrentBag < ConcealGroup > ( ) ;
309+ ConcurrentBag < ConcealGroup > reveal = new ConcurrentBag < ConcealGroup > ( ) ;
304310 var sw = Stopwatch . StartNew ( ) ;
305311 Parallel . ForEach ( MyCubeGridGroups . Static . Physical . Groups , group =>
306312 {
307313 var concealGroup = new ConcealGroup ( group ) ;
308314
309- if ( distanceFromPlayers != 0 )
315+ if ( IsExcluded ( concealGroup ) )
310316 {
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- }
317+ Log . Trace ( "group excluded" ) ;
318+ reveal . Add ( concealGroup ) ;
319+ return ;
317320 }
318321
319- if ( IsExcluded ( concealGroup ) )
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 ) ) ) )
320324 {
321- Log . Trace ( "group excluded" ) ;
325+ Log . Trace ( "group in replication" ) ;
326+ reveal . Add ( concealGroup ) ;
322327 return ;
323328 }
324329
@@ -332,6 +337,14 @@ public int ConcealGrids(double distanceFromPlayers = 0)
332337 concealed += ConcealGroup ( group ) ;
333338 }
334339 Log . Debug ( $ "Concealed grids in { sw . ElapsedMilliseconds } ms.") ;
340+ sw . Restart ( ) ;
341+
342+ foreach ( var group in reveal )
343+ {
344+ revealed += RevealGroup ( group ) ;
345+ }
346+
347+ Log . Debug ( $ "Revealed grids in { sw . ElapsedMilliseconds } ms") ;
335348 sw . Stop ( ) ;
336349
337350 var concealedCount = ConcealedGroups . SelectMany ( x => x . Grids ) . Count ( ) ;
@@ -340,7 +353,10 @@ public int ConcealGrids(double distanceFromPlayers = 0)
340353 if ( concealed > 0 )
341354 Log . Info ( $ "{ concealedCount + concealed } /{ totalCount } grids are concealed ({ concealedCount + concealed / ( float ) totalCount : P} ), { concealed } new.") ;
342355
343- return concealed ;
356+ if ( revealed > 0 )
357+ Log . Info ( $ "Revealed { revealed } grids replicated to players.") ;
358+
359+ return ( concealed , revealed ) ;
344360 }
345361
346362 public bool IsExcluded ( ConcealGroup group )
0 commit comments