Skip to content

Commit a1ac0d2

Browse files
committed
Improve logging and projector behavior
Remove bad lines
1 parent 193f667 commit a1ac0d2

2 files changed

Lines changed: 49 additions & 44 deletions

File tree

‎Concealment/ConcealGroup.cs‎

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Sandbox.Game.Entities.Blocks;
1010
using Sandbox.Game.World;
1111
using SpaceEngineers.Game.Entities.Blocks;
12+
using VRage.Audio;
1213
using VRage.Game.Entity;
1314
using VRage.Game.Entity.EntityComponents.Interfaces;
1415
using VRage.Groups;
@@ -19,6 +20,7 @@ namespace Concealment
1920
{
2021
public class ConcealGroup
2122
{
23+
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
2224
/// <summary>
2325
/// Entity ID of the first grid in the group.
2426
/// </summary>
@@ -70,9 +72,10 @@ private void UnhookOnClosing()
7072
grid.OnMarkForClose -= Grid_OnMarkForClose;
7173
}
7274

73-
private void Grid_OnMarkForClose(VRage.Game.Entity.MyEntity obj)
75+
private void Grid_OnMarkForClose(MyEntity obj)
7476
{
75-
LogManager.GetLogger(nameof(ConcealGroup)).Info($"Grid group '{GridNames}' was marked for close.");
77+
_log.Debug($"Grid group '{GridNames}' was marked for close.");
78+
EnableProjectors();
7679
UnhookOnClosing();
7780
Closing?.Invoke(this);
7881
}
@@ -127,36 +130,54 @@ public bool IsCryoOccupied(ulong steamId)
127130
return false;
128131
}
129132

133+
private readonly HashSet<long> _projectors = new HashSet<long>();
134+
private void DisableProjectors(MyCubeGrid grid)
135+
{
136+
foreach (var projector in grid.GetFatBlocks<MyProjectorBase>())
137+
{
138+
if (projector.ProjectedGrid == null)
139+
continue;
140+
141+
projector.Enabled = false;
142+
_projectors.Add(projector.EntityId);
143+
}
144+
}
145+
146+
public void EnableProjectors()
147+
{
148+
foreach (var projector in _projectors.Select(x => (MyProjectorBase)MyEntities.GetEntityById(x)))
149+
{
150+
projector.Enabled = true;
151+
}
152+
_projectors.Clear();
153+
}
154+
130155
/// <summary>
131156
/// Conceals this group from game and physics logic.
132157
/// </summary>
133158
public void Conceal()
134159
{
135-
//TODO: find good way to disable grid movement/physics
136-
//_unstatic.Clear();
137160
foreach (var grid in Grids)
138161
{
139-
//_unstatic[grid.EntityId] = !grid.IsStatic;
140-
//grid.ConvertToStatic();
162+
DisableProjectors(grid);
141163

142164
if (grid.Parent == null)
143165
UnregisterRecursive(grid);
144166
}
145167

146-
//foreach (var entity in Grids)
147-
// if (entity.Parent == null)
148-
// MyGamePruningStructure.Remove(entity);
149-
150-
void UnregisterRecursive(IMyEntity e)
168+
void UnregisterRecursive(MyEntity e)
151169
{
152-
MyEntities.UnregisterForUpdate((MyEntity)e);
170+
if (e.IsPreview)
171+
return;
172+
173+
MyEntities.UnregisterForUpdate(e);
153174
(e.GameLogic as IMyGameLogicComponent)?.UnregisterForUpdate();
154175
e.Flags |= (EntityFlags)4;
155176
if (e.Hierarchy == null)
156177
return;
157178

158179
foreach (var child in e.Hierarchy.Children)
159-
UnregisterRecursive(child.Container.Entity);
180+
UnregisterRecursive((MyEntity)child.Container.Entity);
160181
}
161182
}
162183

@@ -165,32 +186,29 @@ void UnregisterRecursive(IMyEntity e)
165186
/// </summary>
166187
public void Reveal()
167188
{
168-
// foreach (var entity in Grids)
169-
// if (entity.Parent == null)
170-
// MyGamePruningStructure.Add(entity);
171-
172189
foreach (var grid in Grids)
173190
{
174-
//if (_unstatic[grid.EntityId])
175-
// grid.OnConvertToDynamic();
176-
177191
if (grid.Parent == null)
178192
RegisterRecursive(grid);
179193
}
194+
195+
EnableProjectors();
180196

181-
void RegisterRecursive(IMyEntity e)
197+
void RegisterRecursive(MyEntity e)
182198
{
183-
MyEntities.RegisterForUpdate((MyEntity)e);
199+
if (e.IsPreview)
200+
return;
201+
202+
MyEntities.RegisterForUpdate(e);
184203
(e.GameLogic as IMyGameLogicComponent)?.RegisterForUpdate();
185204
e.Flags &= ~(EntityFlags)4;
186205
if (e.Hierarchy == null)
187206
return;
188207

189208
foreach (var child in e.Hierarchy.Children)
190-
RegisterRecursive(child.Container.Entity);
209+
RegisterRecursive((MyEntity)child.Container.Entity);
191210
}
192211
}
193-
194212
}
195213

196214
}

‎Concealment/ConcealmentPlugin.cs‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public override void Init(ITorchBase torch)
7373
Settings = new Persistent<Settings>(Path.Combine(StoragePath, "Concealment.cfg"), new Settings());
7474
Settings.Data.PropertyChanged += Data_PropertyChanged;
7575
_concealedAabbTree = new MyDynamicAABBTreeD(MyConstants.GAME_PRUNING_STRUCTURE_AABB_EXTENSION);
76-
RegisterEntityStorage("Concealment", Id);
7776
torch.Managers.GetManager<ITorchSessionManager>()?.AddFactory(CreateManager);
7877
}
7978

@@ -87,18 +86,6 @@ private void Data_PropertyChanged(object sender, System.ComponentModel.PropertyC
8786
_settingsChanged = true;
8887
}
8988

90-
//TODO: make this stop spamming the log with warnings
91-
private void RegisterEntityStorage(string name, Guid id)
92-
{
93-
return;
94-
var comp = new MyModStorageComponentDefinition
95-
{
96-
Id = new MyDefinitionId(typeof(MyObjectBuilder_ModStorageComponent), name),
97-
RegisteredStorageGuids = new[] { id }
98-
};
99-
MyDefinitionManager.Static.Definitions.AddDefinition(comp);
100-
}
101-
10289
//TODO: divide conceal/reveal runs over several ticks to avoid stuttering.
10390
public override void Update()
10491
{
@@ -345,9 +332,9 @@ public int ConcealGrids(double distanceFromPlayers = 0)
345332
Log.Debug($"Concealed grids in {sw.ElapsedMilliseconds}ms.");
346333
sw.Stop();
347334

348-
349-
if (concealed != 0)
350-
Log.Info($"Concealed {concealed} grids distant from players.");
335+
var concealedCount = ConcealedGroups.SelectMany(x => x.Grids).Count();
336+
var totalCount = MyEntities.GetEntities().Count(x => x is MyCubeGrid);
337+
Log.Info($"Status: {concealedCount}/{totalCount} grids concealed ({concealedCount/(float)totalCount:P}).");
351338

352339
return concealed;
353340
}
@@ -359,13 +346,13 @@ public bool IsExcluded(ConcealGroup group)
359346
{
360347
if (_keepAliveTimers.ContainsKey(grid.EntityId))
361348
{
362-
Log.Trace($"{group.GridNames} is kept alive by PB action");
349+
Log.Debug($"{group.GridNames} is kept alive by PB action");
363350
return true;
364351
}
365352

366353
if (!Settings.Data.ConcealPirates && grid.BigOwners.Contains(pirateId))
367354
{
368-
Log.Trace($"{group.GridNames} is kept alive by pirate ownership");
355+
Log.Debug($"{group.GridNames} is kept alive by pirate ownership");
369356
return true;
370357
}
371358
}
@@ -380,14 +367,14 @@ public bool IsExcluded(ConcealGroup group)
380367

381368
if (block is IMyProductionBlock p && !Settings.Data.ConcealProduction && p.IsProducing)
382369
{
383-
Log.Trace($"{group.GridNames} exempted production ({p.CustomName} active)");
370+
Log.Debug($"{group.GridNames} exempted production ({p.CustomName} active)");
384371
exclude = true;
385372
break;
386373
}
387374

388375
if (Settings.Data.ExcludedSubtypes.Contains(block.BlockDefinition.Id.SubtypeName))
389376
{
390-
Log.Trace($"{group.GridNames} exempted subtype {block.BlockDefinition.Id.SubtypeName}");
377+
Log.Debug($"{group.GridNames} exempted subtype {block.BlockDefinition.Id.SubtypeName}");
391378
exclude = true;
392379
break;
393380
}

0 commit comments

Comments
 (0)