Skip to content

Commit df8e209

Browse files
committed
Fix bug revealing grids near players, add option to exempt grids with active production blocks
1 parent 3a5431f commit df8e209

6 files changed

Lines changed: 48 additions & 44 deletions

File tree

‎Concealment/Commands.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void Conceal(double distance = 0)
1616
{
1717
distance = Plugin.Settings.Data.ConcealDistance;
1818
}
19-
var num = Plugin.ConcealDistantGrids(distance);
19+
var num = Plugin.ConcealGrids(distance);
2020
Context.Respond($"{num} grids concealed.");
2121
}
2222

‎Concealment/ConcealGroup.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public ConcealGroup(MyGroups<MyCubeGrid, MyGridPhysicalGroupData>.Group group)
3434
public string GridNames
3535
{
3636
get { return string.Join(", ", Grids.Select(g => g.DisplayName)); }
37+
set { }
3738
}
3839

3940
public void UpdatePostConceal()

‎Concealment/ConcealmentControl.xaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<StackPanel DockPanel.Dock="Top">
1111
<StackPanel DataContext="{Binding Settings.Data}">
1212
<CheckBox Content="Enable Concealment" Margin="3" IsChecked="{Binding Enabled}" />
13+
<CheckBox Content="Exempt Production" ToolTip="Don't conceal grids with active production blocks." Margin="3" IsChecked="{Binding ExemptProduction}" />
1314
<StackPanel Orientation="Horizontal">
1415
<TextBox Margin="3" Width="150" Text="{Binding ConcealDistance}" />
1516
<Label Content="Conceal Distance (meters)" Margin="3" />

‎Concealment/ConcealmentControl.xaml.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void Reveal_OnClick(object sender, RoutedEventArgs e)
4545
private void Conceal_OnClick(object sender, RoutedEventArgs e)
4646
{
4747
var p = Plugin;
48-
Plugin.Torch.Invoke(delegate { p.ConcealDistantGrids(p.Settings.Data.ConcealDistance); });
48+
Plugin.Torch.Invoke(delegate { p.ConcealGrids(p.Settings.Data.ConcealDistance); });
4949
}
5050

5151
private void EditExclusion_OnClick(object sender, RoutedEventArgs e)

‎Concealment/ConcealmentPlugin.cs‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
using VRage.Game.ModAPI;
2828
using VRage.Game.ObjectBuilders.ComponentSystem;
2929
using VRage.ModAPI;
30-
using VRage.ObjectBuilders;
31-
using VRage.Utils;
3230
using VRageMath;
3331

3432
namespace Concealment
3533
{
36-
[Plugin("Concealment", "1.1", "17f44521-b77a-4e85-810f-ee73311cf75d")]
37-
public class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
34+
[Plugin("Concealment", "1.1.2", "17f44521-b77a-4e85-810f-ee73311cf75d")]
35+
public sealed class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
3836
{
3937
public Persistent<Settings> Settings { get; private set; }
4038
public MTObservableCollection<ConcealGroup> ConcealGroups { get; } = new MTObservableCollection<ConcealGroup>();
@@ -52,7 +50,7 @@ public ConcealmentPlugin()
5250
_intersectGroups = new List<ConcealGroup>();
5351
}
5452

55-
public UserControl GetControl()
53+
UserControl IWpfPlugin.GetControl()
5654
{
5755
return _control ?? (_control = new ConcealmentControl {DataContext = this});
5856
}
@@ -87,7 +85,7 @@ public override void Update()
8785
return;
8886

8987
if (_counter % Settings.Data.ConcealInterval == 0)
90-
ConcealDistantGrids(Settings.Data.ConcealDistance);
88+
ConcealGrids(Settings.Data.ConcealDistance);
9189
if (_counter % Settings.Data.RevealInterval == 0)
9290
RevealNearbyGrids(Settings.Data.RevealDistance);
9391
_counter += 1;
@@ -168,7 +166,7 @@ void UnregisterRecursive(IMyEntity e)
168166
return;
169167

170168
foreach (var child in e.Hierarchy.Children)
171-
UnregisterRecursive(child.Entity);
169+
UnregisterRecursive(child.Container.Entity);
172170
}
173171
}
174172

@@ -191,7 +189,7 @@ void RegisterRecursive(IMyEntity e)
191189
return;
192190

193191
foreach (var child in e.Hierarchy.Children)
194-
RegisterRecursive(child.Entity);
192+
RegisterRecursive(child.Container.Entity);
195193
}
196194
}
197195

@@ -203,6 +201,7 @@ private int ConcealGroup(ConcealGroup group)
203201
Log.Debug($"Concealing grids: {group.GridNames}");
204202
group.Grids.ForEach(ConcealEntity);
205203
#if !NOPHYS
204+
group.UpdateAABB();
206205
var aabb = group.WorldAABB;
207206
group.ProxyId = _concealedAabbTree.AddProxy(ref aabb, group, 0);
208207
#endif
@@ -230,18 +229,19 @@ public int RevealGroup(ConcealGroup group)
230229
Log.Warn(new StackTrace());
231230
return 0;
232231
}
232+
233+
var count = group.Grids.Count;
233234
Log.Debug($"Revealing grids: {group.GridNames}");
234235
group.Grids.ForEach(RevealEntity);
235236
#if !NOPHYS
236237
_concealGroups.Remove(group);
237238
_concealedAabbTree.RemoveProxy(group.ProxyId);
238239
#endif
239-
return group.Grids.Count;
240+
return count;
240241
}
241242

242243
public int RevealGridsInSphere(BoundingSphereD sphere)
243244
{
244-
Log.Debug($"reveal in sphere {sphere.Center} | {sphere.Radius}");
245245
var revealed = 0;
246246
#if !NOPHYS
247247
_concealedAabbTree.OverlapAllBoundingSphere(ref sphere, _intersectGroups);
@@ -253,6 +253,7 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
253253
_intersectGroups.Add(group);
254254
}
255255
#endif
256+
Log.Trace($"{_intersectGroups.Count} groups");
256257
foreach (var group in _intersectGroups)
257258
revealed += RevealGroup(group);
258259

@@ -262,23 +263,24 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
262263

263264
public int RevealNearbyGrids(double distanceFromPlayers)
264265
{
265-
Log.Debug("Revealing nearby grids");
266266
var revealed = 0;
267-
var playerSpheres = GetPlayerBoundingSpheres(distanceFromPlayers);
268-
Log.Debug(playerSpheres.Count);
267+
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);
269268
foreach (var sphere in playerSpheres)
269+
{
270+
Log.Trace($"{sphere.Center}: {sphere.Radius}");
270271
revealed += RevealGridsInSphere(sphere);
272+
}
271273

272274
if (revealed != 0)
273275
Log.Info($"Revealed {revealed} grids near players.");
274276
return revealed;
275277
}
276278

277-
public int ConcealDistantGrids(double distanceFromPlayers)
279+
public int ConcealGrids(double distanceFromPlayers)
278280
{
279-
Log.Debug("Concealing distant grids");
281+
Log.Debug("Concealing grids");
280282
int concealed = 0;
281-
var playerSpheres = GetPlayerBoundingSpheres(distanceFromPlayers);
283+
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);
282284

283285
ConcurrentBag<ConcealGroup> groups = new ConcurrentBag<ConcealGroup>();
284286
//Parallel.ForEach(MyCubeGridGroups.Static.Physical.Groups, group =>
@@ -288,10 +290,16 @@ public int ConcealDistantGrids(double distanceFromPlayers)
288290

289291
var volume = group.GetWorldAABB();
290292
if (playerSpheres.Any(s => s.Contains(volume) != ContainmentType.Disjoint))
293+
{
294+
Log.Trace("group near player");
291295
continue;
296+
}
292297

293-
//if (IsExcluded(concealGroup))
294-
// return;
298+
if (IsExcluded(concealGroup))
299+
{
300+
Log.Trace("group excluded");
301+
continue;
302+
}
295303

296304
groups.Add(concealGroup);
297305
}
@@ -312,11 +320,21 @@ public bool IsExcluded(ConcealGroup group)
312320
{
313321
if (block == null)
314322
continue;
323+
324+
if (block is IMyProductionBlock p && Settings.Data.ExemptProduction && p.IsProducing)
325+
{
326+
Log.Trace($"{group.GridNames} exempted production ({p.CustomName} active)");
327+
return true;
328+
}
329+
315330
if (Settings.Data.ExcludedSubtypes.Contains(block.BlockDefinition.Id.SubtypeName))
316-
return false;
331+
{
332+
Log.Trace($"{group.GridNames} exempted subtype {block.BlockDefinition.Id.SubtypeName}");
333+
return true;
334+
}
317335
}
318336

319-
return true;
337+
return false;
320338
}
321339

322340
public int RevealAll()
@@ -330,9 +348,9 @@ public int RevealAll()
330348
return revealed;
331349
}
332350

333-
private List<BoundingSphereD> GetPlayerBoundingSpheres(double distance)
351+
private List<BoundingSphereD> GetPlayerViewSpheres(double distance)
334352
{
335-
return ((MyPlayerCollection)MyAPIGateway.Multiplayer.Players).GetOnlinePlayers().Where(p => p.Controller?.ControlledEntity != null).Select(p => new BoundingSphereD(p.Controller.ControlledEntity.Entity.PositionComp.GetPosition(), distance)).ToList();
353+
return ((MyPlayerCollection)MyAPIGateway.Multiplayer.Players).GetOnlinePlayers().Select(p => new BoundingSphereD(p.GetPosition(), distance)).ToList();
336354
}
337355
}
338356

‎Concealment/Settings.cs‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Settings : ViewModel
1313
private ulong _concealInterval = 3600;
1414
private double _revealDistance = 50000;
1515
private ulong _revealInterval = 60;
16+
private bool _exemptProduction;
1617

1718
public MTObservableCollection<string> ExcludedSubtypes { get; } = new MTObservableCollection<string>();
1819

@@ -46,27 +47,10 @@ public double RevealDistance
4647
set { _revealDistance = value; OnPropertyChanged(); }
4748
}
4849

49-
public void Save(string path)
50+
public bool ExemptProduction
5051
{
51-
var xmlSerializer = new XmlSerializer(typeof(Settings));
52-
using (var fileStream = File.Open(path, FileMode.OpenOrCreate))
53-
{
54-
xmlSerializer.Serialize(fileStream, this);
55-
}
56-
}
57-
58-
public static Settings LoadOrCreate(string path)
59-
{
60-
if (!File.Exists(path))
61-
return new Settings();
62-
63-
var xmlSerializer = new XmlSerializer(typeof(Settings));
64-
Settings result;
65-
using (var fileStream = File.OpenRead(path))
66-
{
67-
result = (Settings)xmlSerializer.Deserialize(fileStream);
68-
}
69-
return result;
52+
get => _exemptProduction;
53+
set { _exemptProduction = value; OnPropertyChanged(); }
7054
}
7155
}
7256
}

0 commit comments

Comments
 (0)