Skip to content

Commit fa697d5

Browse files
committed
First pass at using replciation status for concealment
1 parent bf1e119 commit fa697d5

10 files changed

Lines changed: 163 additions & 130 deletions

‎Concealment/Commands.cs‎

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,13 @@ public class Commands : CommandModule
99
{
1010
public ConcealmentPlugin Plugin => (ConcealmentPlugin)Context.Plugin;
1111

12-
[Command("conceal", "Conceal grids x distance from players."), Permission(MyPromoteLevel.SpaceMaster)]
13-
public void Conceal(double distance = 0)
12+
[Command("update", "Forces a concealment update immediately."), Permission(MyPromoteLevel.SpaceMaster)]
13+
public void Conceal()
1414
{
15-
if (distance == 0)
16-
{
17-
distance = Plugin.Settings.Data.ConcealDistance;
18-
}
19-
var num = Plugin.ConcealGrids(distance);
20-
Context.Respond($"{num} grids concealed.");
15+
var num = Plugin.ConcealGrids();
16+
Context.Respond($"{num.Item1} grids concealed, {num.Item2} grids revealed");
2117
}
22-
23-
[Command("reveal", "Reveal all grids within the given distance"), Permission(MyPromoteLevel.SpaceMaster)]
24-
public void Reveal(double distance = 1000)
25-
{
26-
var pos = Context.Player?.Controller.ControlledEntity?.Entity.GetPosition();
27-
if (!pos.HasValue)
28-
{
29-
Context.Respond("You must be controlling an entity.");
30-
return;
31-
}
32-
33-
var sphere = new BoundingSphereD(pos.Value, distance);
34-
var num = Plugin.RevealGridsInSphere(sphere);
35-
Context.Respond($"{num} grids revealed.");
36-
}
37-
18+
3819
[Command("reveal all", "Reveal all grids"), Permission(MyPromoteLevel.SpaceMaster)]
3920
public void RevealAll()
4021
{
@@ -46,7 +27,7 @@ public void RevealAll()
4627
public void Enable()
4728
{
4829
Plugin.Settings.Data.Enabled = true;
49-
Plugin.ConcealGrids(Plugin.Settings.Data.ConcealDistance);
30+
Plugin.ConcealGrids();
5031
}
5132

5233
[Command("conceal off", "Disable concealment.")]

‎Concealment/Concealment.csproj‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
<HintPath>$(SolutionDir)\GameBinaries\HavokWrapper.dll</HintPath>
3434
<Private>False</Private>
3535
</Reference>
36-
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
37-
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
38-
<Private>False</Private>
36+
<Reference Include="NLog">
37+
<HintPath>..\TorchBinaries\NLog.dll</HintPath>
3938
</Reference>
4039
<Reference Include="PresentationCore" />
4140
<Reference Include="PresentationFramework" />
@@ -154,8 +153,10 @@
154153
<DependentUpon>DynamicConcealmentEditor.xaml</DependentUpon>
155154
</Compile>
156155
<Compile Include="DynamicConcealmentManager.cs" />
156+
<Compile Include="Patches\PatchReplicableRequest.cs" />
157157
<Compile Include="Properties\AssemblyInfo.cs" />
158158
<Compile Include="Settings.cs" />
159+
<Compile Include="Utilities.cs" />
159160
</ItemGroup>
160161
<ItemGroup>
161162
<Page Include="ConcealmentControl.xaml">
@@ -170,8 +171,6 @@
170171
<ItemGroup>
171172
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
172173
</ItemGroup>
173-
<ItemGroup>
174-
<None Include="packages.config" />
175-
</ItemGroup>
174+
<ItemGroup />
176175
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
177176
</Project>

‎Concealment/ConcealmentControl.xaml‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,14 @@
1919
<CheckBox Content="Enable Concealment" Margin="3" IsChecked="{Binding Enabled}" />
2020
<CheckBox Content="Conceal Production" ToolTip="Conceal grids with active production blocks." Margin="3" IsChecked="{Binding ConcealProduction}" />
2121
<CheckBox Content="Conceal Pirates" ToolTip="Conceal grids owned by Space Pirates." Margin="3" IsChecked="{Binding ConcealPirates}" />
22-
<StackPanel Orientation="Horizontal">
23-
<TextBox Margin="3" Width="150" Text="{Binding ConcealDistance}" />
24-
<Label Content="Conceal Distance (meters)" Margin="3" />
25-
</StackPanel>
26-
<StackPanel Orientation="Horizontal">
27-
<TextBox Margin="3" Width="150" Text="{Binding RevealDistance}" />
28-
<Label Content="Reveal Distance (meters)" Margin="3" />
29-
</StackPanel>
22+
3023
<StackPanel Orientation="Horizontal">
3124
<TextBox Margin="3" Width="150" Text="{Binding ConcealInterval}" />
3225
<Label Content="Conceal Interval (ticks, 1/60 s)" Margin="3" />
3326
</StackPanel>
34-
<StackPanel Orientation="Horizontal">
35-
<TextBox Margin="3" Width="150" Text="{Binding RevealInterval}" />
36-
<Label Content="Reveal Interval (ticks, 1/60 s)" Margin="3" />
37-
</StackPanel>
3827
</StackPanel>
3928
<StackPanel Orientation="Horizontal">
40-
<Button Content="Manual Conceal" Margin="3" Click="Conceal_OnClick" />
41-
<Button Content="Manual Reveal" Margin="3" Click="Reveal_OnClick" />
29+
<Button Content="Force Update" Margin="3" Click="Conceal_OnClick" />
4230
</StackPanel>
4331
<Button Content="Edit Excluded Subtypes" Margin="3" Click="EditExclusion_OnClick"/>
4432
<Button Content="Edit Dynamic Concealment" Margin="3" Click="EditDynamicConcealment_OnClick"/>

‎Concealment/ConcealmentControl.xaml.cs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ private void RevealSelected_OnClick(object sender, RoutedEventArgs e)
3636
});
3737
}
3838

39-
private void Reveal_OnClick(object sender, RoutedEventArgs e)
40-
{
41-
var p = Plugin;
42-
Plugin.Torch.Invoke(delegate { p.RevealGrids(p.Settings.Data.RevealDistance); });
43-
}
44-
4539
private void Conceal_OnClick(object sender, RoutedEventArgs e)
4640
{
4741
var p = Plugin;
48-
Plugin.Torch.Invoke(delegate { p.ConcealGrids(p.Settings.Data.ConcealDistance); });
42+
Plugin.Torch.Invoke(delegate { p.ConcealGrids(); });
4943
}
5044

5145
private void EditExclusion_OnClick(object sender, RoutedEventArgs e)

‎Concealment/ConcealmentPlugin.cs‎

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using VRage.Game.ModAPI;
3030
using VRage.Game.ObjectBuilders.ComponentSystem;
3131
using VRage.ModAPI;
32+
using VRage.Network;
3233
using VRageMath;
3334

3435
namespace Concealment
@@ -50,6 +51,10 @@ public sealed class ConcealmentPlugin : TorchPluginBase, IWpfPlugin
5051
private bool _settingsChanged;
5152
private bool _ready;
5253

54+
private MyReplicationServer Replication => (MyReplicationServer)MyMultiplayer.Static.ReplicationLayer;
55+
56+
public static ConcealmentPlugin Instance { get; private set; }
57+
5358
public ConcealmentPlugin()
5459
{
5560
_intersectGroups = new List<ConcealGroup>();
@@ -76,6 +81,8 @@ public override void Init(ITorchBase torch)
7681
Settings.Data.PropertyChanged += Data_PropertyChanged;
7782
_concealedAabbTree = new MyDynamicAABBTreeD(MyConstants.GAME_PRUNING_STRUCTURE_AABB_EXTENSION);
7883
torch.Managers.GetManager<ITorchSessionManager>()?.AddFactory(CreateManager);
84+
85+
Instance = this;
7986
}
8087

8188
private IManager CreateManager(ITorchSession session)
@@ -97,9 +104,7 @@ public override void Update()
97104
if (_ready)
98105
{
99106
if (_counter % (ulong)Settings.Data.ConcealInterval == 0)
100-
ConcealGrids(Settings.Data.ConcealDistance);
101-
if (_counter % (ulong)Settings.Data.RevealInterval == 0)
102-
RevealGrids(Settings.Data.RevealDistance);
107+
ConcealGrids();
103108
_counter += 1;
104109
}
105110

@@ -239,8 +244,8 @@ public int RevealGroup(ConcealGroup group)
239244
{
240245
if (!group.IsConcealed)
241246
{
242-
Log.Warn($"Attempted to reveal a group that wasn't concealed: {group.GridNames}");
243-
Log.Warn(new StackTrace());
247+
//Log.Warn($"Attempted to reveal a group that wasn't concealed: {group.GridNames}");
248+
//Log.Warn(new StackTrace());
244249
return 0;
245250
}
246251

@@ -268,57 +273,57 @@ public int RevealGridsInSphere(BoundingSphereD sphere)
268273
return revealed;
269274
}
270275

271-
public int RevealGrids(double distanceFromPlayers)
272-
{
273-
var revealed = 0;
274-
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);
275-
foreach (var sphere in playerSpheres)
276-
{
277-
Log.Trace($"{sphere.Center}: {sphere.Radius}");
278-
revealed += RevealGridsInSphere(sphere);
279-
}
280-
281-
if (_settingsChanged)
282-
{
283-
for (var i = ConcealedGroups.Count - 1; i >= 0; i--)
284-
{
285-
if (IsExcluded(ConcealedGroups[i]))
286-
revealed += RevealGroup(ConcealedGroups[i]);
287-
}
288-
289-
_settingsChanged = false;
290-
}
291-
292-
if (revealed != 0)
293-
Log.Info($"Revealed {revealed} grids near players.");
294-
return revealed;
295-
}
296-
297-
public int ConcealGrids(double distanceFromPlayers = 0)
276+
//public int RevealGrids()
277+
//{
278+
// var revealed = 0;
279+
280+
// foreach (var sphere in playerSpheres)
281+
// {
282+
// Log.Trace($"{sphere.Center}: {sphere.Radius}");
283+
// revealed += RevealGridsInSphere(sphere);
284+
// }
285+
286+
// if (_settingsChanged)
287+
// {
288+
// for (var i = ConcealedGroups.Count - 1; i >= 0; i--)
289+
// {
290+
// if (IsExcluded(ConcealedGroups[i]))
291+
// revealed += RevealGroup(ConcealedGroups[i]);
292+
// }
293+
294+
// _settingsChanged = false;
295+
// }
296+
297+
// if (revealed != 0)
298+
// Log.Info($"Revealed {revealed} grids near players.");
299+
// return revealed;
300+
//}
301+
302+
public (int ,int) ConcealGrids()
298303
{
299304
Log.Debug("Concealing grids");
300305
int concealed = 0;
301-
var playerSpheres = GetPlayerViewSpheres(distanceFromPlayers);
306+
int revealed = 0;
302307

303308
ConcurrentBag<ConcealGroup> groups = new ConcurrentBag<ConcealGroup>();
309+
ConcurrentBag<ConcealGroup> reveal = new ConcurrentBag<ConcealGroup>();
304310
var sw = Stopwatch.StartNew();
305311
Parallel.ForEach(MyCubeGridGroups.Static.Physical.Groups, group =>
306312
{
307313
var concealGroup = new ConcealGroup(group);
308314

309-
if (distanceFromPlayers != 0)
315+
if (IsExcluded(concealGroup))
310316
{
311-
var volume = group.GetWorldAABB();
312-
if (playerSpheres.Any(s => s.Contains(volume) != ContainmentType.Disjoint))
313-
{
314-
Log.Trace("group near player");
315-
return;
316-
}
317+
Log.Trace("group excluded");
318+
reveal.Add(concealGroup);
319+
return;
317320
}
318321

319-
if (IsExcluded(concealGroup))
322+
//Checks if any connected clients have requested any grid in this group for replication
323+
if (concealGroup.Grids.Any(g => Replication.IsReplicated(Utilities.GetReplicable(g))))
320324
{
321-
Log.Trace("group excluded");
325+
Log.Trace("group in replication");
326+
reveal.Add(concealGroup);
322327
return;
323328
}
324329

@@ -332,6 +337,14 @@ public int ConcealGrids(double distanceFromPlayers = 0)
332337
concealed += ConcealGroup(group);
333338
}
334339
Log.Debug($"Concealed grids in {sw.ElapsedMilliseconds}ms.");
340+
sw.Restart();
341+
342+
foreach (var group in reveal)
343+
{
344+
revealed += RevealGroup(group);
345+
}
346+
347+
Log.Debug($"Revealed grids in {sw.ElapsedMilliseconds}ms");
335348
sw.Stop();
336349

337350
var concealedCount = ConcealedGroups.SelectMany(x => x.Grids).Count();
@@ -340,7 +353,10 @@ public int ConcealGrids(double distanceFromPlayers = 0)
340353
if (concealed > 0)
341354
Log.Info($"{concealedCount+concealed}/{totalCount} grids are concealed ({concealedCount+concealed/(float)totalCount:P}), {concealed} new.");
342355

343-
return concealed;
356+
if(revealed > 0)
357+
Log.Info($"Revealed {revealed} grids replicated to players.");
358+
359+
return (concealed, revealed);
344360
}
345361

346362
public bool IsExcluded(ConcealGroup group)

‎Concealment/DynamicConcealmentManager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ private static MyRelationsBetweenPlayerAndBlock GetRelationTolerant(long id1, lo
613613
if (playerFaction1 == playerFaction2)
614614
return MyRelationsBetweenPlayerAndBlock.FactionShare;
615615
return MySession.Static.Factions.GetRelationBetweenFactions(playerFaction1.FactionId,
616-
playerFaction2.FactionId) == MyRelationsBetweenFactions.Neutral
616+
playerFaction2.FactionId).Item1 == MyRelationsBetweenFactions.Neutral
617617
? MyRelationsBetweenPlayerAndBlock.Neutral
618618
: MyRelationsBetweenPlayerAndBlock.Enemies;
619619
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using NLog;
8+
using Torch.Managers.PatchManager;
9+
using Torch.Utils;
10+
using VRage;
11+
using VRage.Network;
12+
13+
namespace Concealment.Patches
14+
{
15+
/// <summary>
16+
/// Clients call ReplicableRequest when they need a replicable immediately (remote terminal, remote control, etc).
17+
/// We listen to this even and immediately reveal the grid if concealed.
18+
///
19+
/// Event is also called when client no longer needs the replicable, but we can just let the grid re-conceal
20+
/// when the timer elapses.
21+
/// </summary>
22+
[PatchShim]
23+
static class PatchReplicableRequest
24+
{
25+
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
26+
27+
[ReflectedMethodInfo(typeof(MyReplicationServer), nameof(MyReplicationServer.ReplicableRequest))]
28+
private static MethodInfo _requestMethod;
29+
30+
public static void Patch(PatchContext ctx)
31+
{
32+
ctx.GetPattern(_requestMethod).Prefixes.Add(typeof(PatchReplicableRequest).GetMethod(nameof(PrefixRequest)));
33+
}
34+
35+
public static void PrefixRequest(MyPacket packet)
36+
{
37+
try
38+
{
39+
var stream = packet.BitStream;
40+
var id = stream.ReadInt64();
41+
bool add = stream.ReadBool();
42+
43+
var g = ConcealmentPlugin.Instance.ConcealedGroups.First(gr => gr.Grids.Any(q => q.EntityId == id));
44+
if (g != null && add)
45+
ConcealmentPlugin.Instance.RevealGroup(g);
46+
47+
stream.ResetRead();
48+
}
49+
catch (Exception ex)
50+
{
51+
Log.Error("Exception encountered in Request patch!");
52+
Log.Error(ex);
53+
}
54+
finally
55+
{
56+
//this allows us to fail gracefully
57+
packet.BitStream.ResetRead();
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)