Skip to content

Commit 3760167

Browse files
committed
Update for 1.181
1 parent 66eddee commit 3760167

6 files changed

Lines changed: 120 additions & 28 deletions

File tree

Concealment/Commands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Conceal(double distance = 0)
1414
{
1515
if (distance == 0)
1616
{
17-
distance = Plugin.Settings.ConcealDistance;
17+
distance = Plugin.Settings.Data.ConcealDistance;
1818
}
1919
var num = Plugin.ConcealDistantGrids(distance);
2020
Context.Respond($"{num} grids concealed.");
@@ -35,7 +35,7 @@ public void Reveal(double distance = 1000)
3535
Context.Respond($"{num} grids revealed.");
3636
}
3737

38-
[Command("all", "Reveal all grids", null, "reveal"), Permission(MyPromoteLevel.SpaceMaster)]
38+
[Command("reveal all", "Reveal all grids"), Permission(MyPromoteLevel.SpaceMaster)]
3939
public void RevealAll()
4040
{
4141
int num = Plugin.RevealAll();

Concealment/ConcealGroup.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ConcealGroup
2222
public List<MyCubeGrid> Grids { get; }
2323
public List<MyMedicalRoom> MedicalRooms { get; } = new List<MyMedicalRoom>();
2424
public List<MyCryoChamber> CryoChambers { get; } = new List<MyCryoChamber>();
25+
public event Action<ConcealGroup> Closing;
2526
internal volatile int ProxyId = -1;
2627

2728
public ConcealGroup(MyGroups<MyCubeGrid, MyGridPhysicalGroupData>.Group group)
@@ -39,6 +40,20 @@ public void UpdatePostConceal()
3940
{
4041
UpdateAABB();
4142
CacheSpawns();
43+
HookOnClosing();
44+
}
45+
46+
private void HookOnClosing()
47+
{
48+
foreach (var grid in Grids)
49+
grid.OnClosing += Grid_OnClosing;
50+
}
51+
52+
private void Grid_OnClosing(VRage.Game.Entity.MyEntity obj)
53+
{
54+
Closing?.Invoke(this);
55+
foreach (var grid in Grids)
56+
grid.OnClosing -= Grid_OnClosing;
4257
}
4358

4459
private void UpdateAABB()

Concealment/ConcealmentControl.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
d:DesignHeight="300" d:DesignWidth="300">
99
<DockPanel>
1010
<StackPanel DockPanel.Dock="Top">
11-
<StackPanel DataContext="{Binding Settings}">
11+
<StackPanel DataContext="{Binding Settings.Data}">
1212
<CheckBox Content="Enable Concealment" Margin="3" IsChecked="{Binding Enabled}" />
13+
<CheckBox Content="Manage Physics" Margin="3" IsChecked="{Binding ManagePhysics}" />
14+
<CheckBox Content="Manage Gamelogic" Margin="3" IsChecked="{Binding ManageGamelogic}" />
1315
<StackPanel Orientation="Horizontal">
1416
<TextBox Margin="3" Width="150" Text="{Binding ConcealDistance}" />
1517
<Label Content="Conceal Distance (meters)" Margin="3" />
@@ -31,8 +33,9 @@
3133
<Button Content="Manual Conceal" Margin="3" Click="Conceal_OnClick" />
3234
<Button Content="Manual Reveal" Margin="3" Click="Reveal_OnClick" />
3335
</StackPanel>
36+
<Button Content="Edit Excluded Subtypes" Margin="3" Click="RevealSelected_OnClick"/>
3437
</StackPanel>
35-
<Button Content="Reveal Selected" Margin="3" DockPanel.Dock="Bottom" Click="ButtonBase_OnClick" />
38+
<Button Content="Reveal Selected" Margin="3" DockPanel.Dock="Bottom" Click="EditExclusion_OnClick" />
3639
<ListView Name="Concealed" Margin="3" DockPanel.Dock="Bottom" ItemsSource="{Binding ConcealGroups}">
3740
<ItemsControl.ItemTemplate>
3841
<DataTemplate>

Concealment/ConcealmentControl.xaml.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Windows;
55
using System.Windows.Controls;
6+
using Torch.Views;
67

78
#endregion
89

@@ -20,7 +21,7 @@ public ConcealmentControl()
2021

2122
public ConcealmentPlugin Plugin => (ConcealmentPlugin)DataContext;
2223

23-
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
24+
private void RevealSelected_OnClick(object sender, RoutedEventArgs e)
2425
{
2526
var groups = Concealed.SelectedItems.Cast<ConcealGroup>().ToList();
2627
Concealed.SelectedItems.Clear();
@@ -38,13 +39,19 @@ private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
3839
private void Reveal_OnClick(object sender, RoutedEventArgs e)
3940
{
4041
var p = Plugin;
41-
Plugin.Torch.Invoke(delegate { p.RevealNearbyGrids(p.Settings.RevealDistance); });
42+
Plugin.Torch.Invoke(delegate { p.RevealNearbyGrids(p.Settings.Data.RevealDistance); });
4243
}
4344

4445
private void Conceal_OnClick(object sender, RoutedEventArgs e)
4546
{
4647
var p = Plugin;
47-
Plugin.Torch.Invoke(delegate { p.ConcealDistantGrids(p.Settings.ConcealDistance); });
48+
Plugin.Torch.Invoke(delegate { p.ConcealDistantGrids(p.Settings.Data.ConcealDistance); });
49+
}
50+
51+
private void EditExclusion_OnClick(object sender, RoutedEventArgs e)
52+
{
53+
var editor = new CollectionEditor() {Owner = Window.GetWindow(this)};
54+
editor.Edit(Plugin.Settings.Data.ExcludedSubtypes, "Excluded Subtypes");
4855
}
4956
}
5057
}

Concealment/ConcealmentPlugin.cs

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45
using 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");

Concealment/Settings.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.IO;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Xml.Serialization;
35
using Torch;
46

@@ -11,6 +13,11 @@ public class Settings : ViewModel
1113
private ulong _concealInterval = 3600;
1214
private double _revealDistance = 50000;
1315
private ulong _revealInterval = 60;
16+
private bool _managePhysics = true;
17+
private bool _manageGamelogic = true;
18+
19+
public MTObservableCollection<string> ExcludedSubtypes { get; } = new MTObservableCollection<string>();
20+
public event Action RevealNeeded;
1421

1522
public bool Enabled
1623
{
@@ -42,6 +49,18 @@ public double RevealDistance
4249
set { _revealDistance = value; OnPropertyChanged(); }
4350
}
4451

52+
public bool ManagePhysics
53+
{
54+
get => _managePhysics;
55+
set { RevealNeeded?.Invoke(); _managePhysics = value; OnPropertyChanged(); }
56+
}
57+
58+
public bool ManageGamelogic
59+
{
60+
get => _manageGamelogic;
61+
set { RevealNeeded?.Invoke(); _manageGamelogic = value; OnPropertyChanged(); }
62+
}
63+
4564
public void Save(string path)
4665
{
4766
var xmlSerializer = new XmlSerializer(typeof(Settings));

0 commit comments

Comments
 (0)