Skip to content

Commit

Permalink
Fix bug revealing grids near players, add option to exempt grids with…
Browse files Browse the repository at this point in the history
… active production blocks
  • Loading branch information
Jimmacle committed Jun 29, 2017
1 parent 3a5431f commit df8e209
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Concealment/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Conceal(double distance = 0)
{
distance = Plugin.Settings.Data.ConcealDistance;
}
var num = Plugin.ConcealDistantGrids(distance);
var num = Plugin.ConcealGrids(distance);
Context.Respond($"{num} grids concealed.");
}

Expand Down
1 change: 1 addition & 0 deletions Concealment/ConcealGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ConcealGroup(MyGroups<MyCubeGrid, MyGridPhysicalGroupData>.Group group)
public string GridNames
{
get { return string.Join(", ", Grids.Select(g => g.DisplayName)); }
set { }
}

public void UpdatePostConceal()
Expand Down
1 change: 1 addition & 0 deletions Concealment/ConcealmentControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<StackPanel DockPanel.Dock="Top">
<StackPanel DataContext="{Binding Settings.Data}">
<CheckBox Content="Enable Concealment" Margin="3" IsChecked="{Binding Enabled}" />
<CheckBox Content="Exempt Production" ToolTip="Don't conceal grids with active production blocks." Margin="3" IsChecked="{Binding ExemptProduction}" />
<StackPanel Orientation="Horizontal">
<TextBox Margin="3" Width="150" Text="{Binding ConcealDistance}" />
<Label Content="Conceal Distance (meters)" Margin="3" />
Expand Down
2 changes: 1 addition & 1 deletion Concealment/ConcealmentControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void Reveal_OnClick(object sender, RoutedEventArgs e)
private void Conceal_OnClick(object sender, RoutedEventArgs e)
{
var p = Plugin;
Plugin.Torch.Invoke(delegate { p.ConcealDistantGrids(p.Settings.Data.ConcealDistance); });
Plugin.Torch.Invoke(delegate { p.ConcealGrids(p.Settings.Data.ConcealDistance); });
}

private void EditExclusion_OnClick(object sender, RoutedEventArgs e)
Expand Down
62 changes: 40 additions & 22 deletions Concealment/ConcealmentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
using VRage.Game.ModAPI;
using VRage.Game.ObjectBuilders.ComponentSystem;
using VRage.ModAPI;
using VRage.ObjectBuilders;
using VRage.Utils;
using VRageMath;

namespace Concealment
{
[Plugin("Concealment", "1.1", "17f44521-b77a-4e85-810f-ee73311cf75d")]
public class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
[Plugin("Concealment", "1.1.2", "17f44521-b77a-4e85-810f-ee73311cf75d")]
public sealed class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
{
public Persistent<Settings> Settings { get; private set; }
public MTObservableCollection<ConcealGroup> ConcealGroups { get; } = new MTObservableCollection<ConcealGroup>();
Expand All @@ -52,7 +50,7 @@ public ConcealmentPlugin()
_intersectGroups = new List<ConcealGroup>();
}

public UserControl GetControl()
UserControl IWpfPlugin.GetControl()
{
return _control ?? (_control = new ConcealmentControl {DataContext = this});
}
Expand Down Expand Up @@ -87,7 +85,7 @@ public override void Update()
return;

if (_counter % Settings.Data.ConcealInterval == 0)
ConcealDistantGrids(Settings.Data.ConcealDistance);
ConcealGrids(Settings.Data.ConcealDistance);
if (_counter % Settings.Data.RevealInterval == 0)
RevealNearbyGrids(Settings.Data.RevealDistance);
_counter += 1;
Expand Down Expand Up @@ -168,7 +166,7 @@ void UnregisterRecursive(IMyEntity e)
return;

foreach (var child in e.Hierarchy.Children)
UnregisterRecursive(child.Entity);
UnregisterRecursive(child.Container.Entity);
}
}

Expand All @@ -191,7 +189,7 @@ void RegisterRecursive(IMyEntity e)
return;

foreach (var child in e.Hierarchy.Children)
RegisterRecursive(child.Entity);
RegisterRecursive(child.Container.Entity);
}
}

Expand All @@ -203,6 +201,7 @@ private int ConcealGroup(ConcealGroup group)
Log.Debug($"Concealing grids: {group.GridNames}");
group.Grids.ForEach(ConcealEntity);
#if !NOPHYS
group.UpdateAABB();
var aabb = group.WorldAABB;
group.ProxyId = _concealedAabbTree.AddProxy(ref aabb, group, 0);
#endif
Expand Down Expand Up @@ -230,18 +229,19 @@ public int RevealGroup(ConcealGroup group)
Log.Warn(new StackTrace());
return 0;
}

var count = group.Grids.Count;
Log.Debug($"Revealing grids: {group.GridNames}");
group.Grids.ForEach(RevealEntity);
#if !NOPHYS
_concealGroups.Remove(group);
_concealedAabbTree.RemoveProxy(group.ProxyId);
#endif
return group.Grids.Count;
return count;
}

public int RevealGridsInSphere(BoundingSphereD sphere)
{
Log.Debug($"reveal in sphere {sphere.Center} | {sphere.Radius}");
var revealed = 0;
#if !NOPHYS
_concealedAabbTree.OverlapAllBoundingSphere(ref sphere, _intersectGroups);
Expand All @@ -253,6 +253,7 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
_intersectGroups.Add(group);
}
#endif
Log.Trace($"{_intersectGroups.Count} groups");
foreach (var group in _intersectGroups)
revealed += RevealGroup(group);

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

public int RevealNearbyGrids(double distanceFromPlayers)
{
Log.Debug("Revealing nearby grids");
var revealed = 0;
var playerSpheres = GetPlayerBoundingSpheres(distanceFromPlayers);
Log.Debug(playerSpheres.Count);
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);
foreach (var sphere in playerSpheres)
{
Log.Trace($"{sphere.Center}: {sphere.Radius}");
revealed += RevealGridsInSphere(sphere);
}

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

public int ConcealDistantGrids(double distanceFromPlayers)
public int ConcealGrids(double distanceFromPlayers)
{
Log.Debug("Concealing distant grids");
Log.Debug("Concealing grids");
int concealed = 0;
var playerSpheres = GetPlayerBoundingSpheres(distanceFromPlayers);
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);

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

var volume = group.GetWorldAABB();
if (playerSpheres.Any(s => s.Contains(volume) != ContainmentType.Disjoint))
{
Log.Trace("group near player");
continue;
}

//if (IsExcluded(concealGroup))
// return;
if (IsExcluded(concealGroup))
{
Log.Trace("group excluded");
continue;
}

groups.Add(concealGroup);
}
Expand All @@ -312,11 +320,21 @@ public bool IsExcluded(ConcealGroup group)
{
if (block == null)
continue;

if (block is IMyProductionBlock p && Settings.Data.ExemptProduction && p.IsProducing)
{
Log.Trace($"{group.GridNames} exempted production ({p.CustomName} active)");
return true;
}

if (Settings.Data.ExcludedSubtypes.Contains(block.BlockDefinition.Id.SubtypeName))
return false;
{
Log.Trace($"{group.GridNames} exempted subtype {block.BlockDefinition.Id.SubtypeName}");
return true;
}
}

return true;
return false;
}

public int RevealAll()
Expand All @@ -330,9 +348,9 @@ public int RevealAll()
return revealed;
}

private List<BoundingSphereD> GetPlayerBoundingSpheres(double distance)
private List<BoundingSphereD> GetPlayerViewSpheres(double distance)
{
return ((MyPlayerCollection)MyAPIGateway.Multiplayer.Players).GetOnlinePlayers().Where(p => p.Controller?.ControlledEntity != null).Select(p => new BoundingSphereD(p.Controller.ControlledEntity.Entity.PositionComp.GetPosition(), distance)).ToList();
return ((MyPlayerCollection)MyAPIGateway.Multiplayer.Players).GetOnlinePlayers().Select(p => new BoundingSphereD(p.GetPosition(), distance)).ToList();
}
}

Expand Down
24 changes: 4 additions & 20 deletions Concealment/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Settings : ViewModel
private ulong _concealInterval = 3600;
private double _revealDistance = 50000;
private ulong _revealInterval = 60;
private bool _exemptProduction;

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

Expand Down Expand Up @@ -46,27 +47,10 @@ public double RevealDistance
set { _revealDistance = value; OnPropertyChanged(); }
}

public void Save(string path)
public bool ExemptProduction
{
var xmlSerializer = new XmlSerializer(typeof(Settings));
using (var fileStream = File.Open(path, FileMode.OpenOrCreate))
{
xmlSerializer.Serialize(fileStream, this);
}
}

public static Settings LoadOrCreate(string path)
{
if (!File.Exists(path))
return new Settings();

var xmlSerializer = new XmlSerializer(typeof(Settings));
Settings result;
using (var fileStream = File.OpenRead(path))
{
result = (Settings)xmlSerializer.Deserialize(fileStream);
}
return result;
get => _exemptProduction;
set { _exemptProduction = value; OnPropertyChanged(); }
}
}
}

0 comments on commit df8e209

Please sign in to comment.