Skip to content

Commit

Permalink
Reveal and conceal while respecting weld groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Equinox- committed Aug 31, 2017
1 parent b3bc068 commit 2bd198a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Concealment.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Concealment", "Concealment/Concealment.csproj", "{E5C0184B-7DC4-43D8-872E-2F71162748AA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Concealment", "Concealment\Concealment.csproj", "{E5C0184B-7DC4-43D8-872E-2F71162748AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
86 changes: 86 additions & 0 deletions Concealment/ConcealGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
using System.Linq;
using System.Reflection;
using NLog;
using Sandbox.Engine.Physics;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Blocks;
using Sandbox.Game.World;
using SpaceEngineers.Game.Entities.Blocks;
using VRage.Game.Entity;
using VRage.Groups;
using VRage.ModAPI;
using VRageMath;

namespace Concealment
Expand Down Expand Up @@ -120,6 +123,89 @@ public bool IsCryoOccupied(ulong steamId)

return false;
}

/// <summary>
/// Conceals this group from game and physics logic.
/// </summary>
public void Conceal()
{
foreach (var body in Grids)
if (body.Parent == null)
UnregisterRecursive(body);

foreach (var body in Grids)
body.Physics?.UnweldAll(false);
foreach (var body in Grids)
body.Physics?.Deactivate();

foreach (var entity in Grids)
if (entity.Parent == null)
MyGamePruningStructure.Remove(entity);

void UnregisterRecursive(IMyEntity e)
{
MyEntities.UnregisterForUpdate((MyEntity)e);
if (e.Hierarchy == null)
return;

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

/// <summary>
/// Reveals this group to game and physics logic.
/// </summary>
public void Reveal()
{
foreach (var entity in Grids)
if (entity.Parent == null)
MyGamePruningStructure.Add(entity);


var weldGroups = new HashSet<MyGroups<MyEntity, MyWeldGroupData>.Group>();
foreach (var body in Grids)
{
if (body.Physics == null)
continue;
var group = MyWeldingGroups.Static.GetGroup(body);
if (group == null)
body.Physics.Activate();
else
weldGroups.Add(group);
}
foreach (var group in weldGroups)
{
var body = group.GroupData.Parent;
if (!(body.Physics is MyPhysicsBody bodyPhysics))
continue;
bodyPhysics.Activate();

foreach (var child in group.Nodes)
if (child.NodeData != body &&
!child.NodeData.MarkedForClose &&
child.NodeData.Physics is MyPhysicsBody physBody)
bodyPhysics.Weld(physBody);

body.RaisePhysicsChanged();
}


foreach (var entity in Grids)
if (entity.Parent == null)
RegisterRecursive(entity);

void RegisterRecursive(IMyEntity e)
{
MyEntities.RegisterForUpdate((MyEntity)e);
if (e.Hierarchy == null)
return;

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

}

}
55 changes: 9 additions & 46 deletions Concealment/ConcealmentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using Havok;
using NLog;
using Sandbox.Definitions;
using Sandbox.Engine.Multiplayer;
Expand All @@ -23,7 +22,6 @@
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.Definitions;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.Game.ObjectBuilders.ComponentSystem;
using VRage.ModAPI;
Expand Down Expand Up @@ -54,7 +52,7 @@ public ConcealmentPlugin()

UserControl IWpfPlugin.GetControl()
{
return _control ?? (_control = new ConcealmentControl {DataContext = this});
return _control ?? (_control = new ConcealmentControl { DataContext = this });
}

public override void Init(ITorchBase torch)
Expand Down Expand Up @@ -198,55 +196,17 @@ private void RevealSpawns(PlayerRequestArgs args)
});
}

private void ConcealEntity(IMyEntity entity)
{
if (entity != entity.GetTopMostParent())
return;

entity.GetStorage().SetValue(Id, "True");
MyGamePruningStructure.Remove((MyEntity)entity);
entity.Physics?.Deactivate();
UnregisterRecursive(entity);

void UnregisterRecursive(IMyEntity e)
{
MyEntities.UnregisterForUpdate((MyEntity)e);
if (e.Hierarchy == null)
return;

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

private void RevealEntity(IMyEntity entity)
{
if (entity != entity.GetTopMostParent())
return;

entity.GetStorage().SetValue(Id, "False");
MyGamePruningStructure.Add((MyEntity)entity);
entity.Physics?.Activate();
RegisterRecursive(entity);

void RegisterRecursive(IMyEntity e)
{
MyEntities.RegisterForUpdate((MyEntity)e);
if (e.Hierarchy == null)
return;

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

private int ConcealGroup(ConcealGroup group)
{
if (ConcealedGroups.Any(g => g.Id == group.Id))
return 0;

Log.Debug($"Concealing grids: {group.GridNames}");
group.Grids.ForEach(ConcealEntity);
group.Conceal();
foreach (var entity in group.Grids)
entity.GetStorage().SetValue(Id, "True");

group.UpdateAABB();
var aabb = group.WorldAABB;
group.ProxyId = _concealedAabbTree.AddProxy(ref aabb, group, 0);
Expand Down Expand Up @@ -277,7 +237,10 @@ public int RevealGroup(ConcealGroup group)

var count = group.Grids.Count;
Log.Debug($"Revealing grids: {group.GridNames}");
group.Grids.ForEach(RevealEntity);
group.Reveal();
foreach (var entity in group.Grids)
entity.GetStorage().SetValue(Id, "False");

ConcealedGroups.Remove(group);
_concealedAabbTree.RemoveProxy(group.ProxyId);
group.UpdatePostReveal();
Expand Down

0 comments on commit 2bd198a

Please sign in to comment.