Add a DebugDrawingManager for drawing server side events like Ra… (#892)

* add basis for debug drawing raycasts

* rays

* this was too much work

* i assumed you could do string object stuff. you cant.

* fixed a crash

* Greatly improved DebugDrawingManager code with a struct and list as opposed to dictionary

* change default ray debug color from green to magenta

* reviewed changes

* reviewed changes and adds network message to physicsmanager so it works without much user config
This commit is contained in:
Ephememory
2019-11-23 07:09:48 -05:00
committed by Pieter-Jan Briers
parent e82f969589
commit 519c28cad2
12 changed files with 268 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
using System;
using Robust.Client.Interfaces;
using Robust.Client.Interfaces.Debugging;
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.GameStates;
using Robust.Client.Interfaces.State;
@@ -50,6 +51,9 @@ namespace Robust.Client
[Dependency]
private readonly IClientGameStateManager _gameStates;
[Dependency]
private readonly IDebugDrawingManager _debugDrawMan;
#pragma warning restore 649
/// <inheritdoc />
@@ -68,12 +72,12 @@ namespace Robust.Client
public void Initialize()
{
_net.RegisterNetMessage<MsgServerInfo>(MsgServerInfo.NAME, HandleServerInfo);
_net.Connected += OnConnected;
_net.ConnectFailed += OnConnectFailed;
_net.Disconnect += OnNetDisconnect;
_playMan.Initialize();
_debugDrawMan.Initialize();
Reset();
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using Robust.Client.Audio.Midi;
using Robust.Client.Console;
using Robust.Client.Debugging;
@@ -75,6 +75,7 @@ namespace Robust.Client
IoCManager.Register<IUserInterfaceManager, UserInterfaceManager>();
IoCManager.Register<IUserInterfaceManagerInternal, UserInterfaceManager>();
IoCManager.Register<IDebugDrawing, DebugDrawing>();
IoCManager.Register<IDebugDrawingManager, DebugDrawingManager>();
IoCManager.Register<ILightManager, LightManager>();
IoCManager.Register<IDiscordRichPresence, DiscordRichPresence>();
IoCManager.Register<IClientConsole, ClientConsole>();

View File

@@ -194,6 +194,28 @@ namespace Robust.Client.Console.Commands
}
}
internal class ShowRayCommand : IConsoleCommand
{
public string Command => "showrays";
public string Help => "showrays <raylifetime>";
public string Description => "Toggles debug drawing of physics rays.";
public bool Execute(IDebugConsole console, params string[] args)
{
if (args.Length != 1)
{
console.AddLine("Must specify ray lifetime.", Color.Red);
return false;
}
var mgr = IoCManager.Resolve<IDebugDrawingManager>();
mgr.DebugDrawRays = !mgr.DebugDrawRays;
console.AddLine("Toggled showing rays to:" + mgr.DebugDrawRays.ToString(), Color.Green);
var indexSplit = args[0].Split(',');
mgr.DebugRayLifetime = TimeSpan.FromSeconds((double)int.Parse(indexSplit[0], CultureInfo.InvariantCulture));
return false;
}
}
internal class DisconnectCommand : IConsoleCommand
{
public string Command => "disconnect";

View File

@@ -0,0 +1,132 @@
using Robust.Client.Interfaces.Debugging;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Robust.Client.Graphics.Clyde;
using Robust.Client.Graphics.Overlays;
using Robust.Client.Graphics.Drawing;
using Robust.Shared.Maths;
using Robust.Client.Interfaces.Graphics.Overlays;
using Robust.Shared.Log;
using Robust.Shared.Timing;
using Robust.Shared.Interfaces.Timing;
namespace Robust.Client.Debugging
{
internal class DebugDrawingManager : IDebugDrawingManager
{
#pragma warning disable 649
[Dependency] private readonly IClientNetManager _net;
[Dependency] private readonly IOverlayManager _overlayManager;
[Dependency] private readonly IGameTiming _gameTimer;
#pragma warning restore 649
private List<RayWithLifetime> raysWithLifeTime;
private TimeSpan _rayLifeTime;
private bool _debugDrawRays;
private struct RayWithLifetime
{
public Ray TheRay;
public TimeSpan LifeTime;
}
public bool DebugDrawRays
{
get => _debugDrawRays;
set
{
if (value == DebugDrawRays)
{
return;
}
_debugDrawRays = value;
if (value)
{
_overlayManager.AddOverlay(new DebugDrawRayOverlay(raysWithLifeTime));
}
else
{
_overlayManager.RemoveOverlay(nameof(DebugDrawRayOverlay));
}
}
}
public TimeSpan DebugRayLifetime
{
get => _rayLifeTime;
set
{
_rayLifeTime = value;
}
}
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME, HandleDrawRay);
raysWithLifeTime = new List<RayWithLifetime>();
_rayLifeTime = TimeSpan.FromSeconds(5);
}
private void HandleDrawRay(MsgRay msg)
{
var newRay = msg.RayToSend;
var newRayWithLifetime = new RayWithLifetime
{
TheRay = newRay,
LifeTime = _gameTimer.RealTime + _rayLifeTime
};
if(!raysWithLifeTime.Contains(newRayWithLifetime))
{
raysWithLifeTime.Add(newRayWithLifetime);
}
}
public void FrameUpdate(FrameEventArgs frameEventArgs)
{
if (!_debugDrawRays)
{
return;
}
foreach (var rayWL in raysWithLifeTime)
{
raysWithLifeTime.RemoveAll(r => r.LifeTime < _rayLifeTime);
}
}
private sealed class DebugDrawRayOverlay : Overlay
{
public override OverlaySpace Space => OverlaySpace.WorldSpace;
private List<RayWithLifetime> raysWithLifeTime;
public DebugDrawRayOverlay(List<RayWithLifetime> _rays) : base(nameof(DebugDrawRayOverlay))
{
raysWithLifeTime = _rays;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
raysWithLifeTime = new List<RayWithLifetime>();
}
protected override void Draw(DrawingHandleBase handle)
{
var worldhandle = (DrawingHandleBase)handle;
foreach(var rayWL in raysWithLifeTime)
{
worldhandle.DrawLine(rayWL.TheRay.Position, rayWL.TheRay.Direction, Color.Magenta);
}
}
}
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Robust.Shared.Timing;
namespace Robust.Client.Interfaces.Debugging
{
public interface IDebugDrawingManager
{
bool DebugDrawRays { get; set; }
TimeSpan DebugRayLifetime { get; set; }
void Initialize();
void FrameUpdate(FrameEventArgs frameEventArgs);
}
}

View File

@@ -30,6 +30,7 @@ using Robust.Shared.Interfaces.Log;
using Robust.Shared.Interfaces.Resources;
using Robust.Shared.Exceptions;
using Robust.Shared.Localization;
using Robust.Server.Interfaces.Debugging;
namespace Robust.Server
{
@@ -229,6 +230,7 @@ namespace Robust.Server
_mapManager.Initialize();
IoCManager.Resolve<IPlacementManager>().Initialize();
IoCManager.Resolve<IViewVariablesHost>().Initialize();
IoCManager.Resolve<IDebugDrawingManager>().Initialize();
// Call Init in game assemblies.
_modLoader.BroadcastRunLevel(ModRunLevel.Init);

View File

@@ -0,0 +1,22 @@
using Robust.Server.Interfaces.Debugging;
using Robust.Shared.Interfaces.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
namespace Robust.Server.Debugging
{
internal class DebugDrawingManager : IDebugDrawingManager
{
#pragma warning disable 649
[Dependency] private readonly INetManager _net;
#pragma warning restore 649
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME);
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Robust.Server.Interfaces.Debugging
{
public interface IDebugDrawingManager
{
void Initialize();
}
}

View File

@@ -12,8 +12,8 @@
</PropertyGroup>
<Import Project="..\MSBuild\Robust.DefineConstants.targets" />
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="ConcurrentDataStructures" Version="0.2.0" />
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="ConcurrentDataStructures" Version="0.2.0" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="YamlDotNet" Version="6.1.2" />
</ItemGroup>

View File

@@ -1,8 +1,10 @@
using Robust.Server.Console;
using Robust.Server.Console;
using Robust.Server.Debugging;
using Robust.Server.GameObjects;
using Robust.Server.GameStates;
using Robust.Server.Interfaces;
using Robust.Server.Interfaces.Console;
using Robust.Server.Interfaces.Debugging;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.GameState;
using Robust.Server.Interfaces.Maps;
@@ -66,6 +68,7 @@ namespace Robust.Server
IoCManager.Register<ISystemConsoleManager, SystemConsoleManager>();
IoCManager.Register<ITileDefinitionManager, TileDefinitionManager>();
IoCManager.Register<IViewVariablesHost, ViewVariablesHost>();
IoCManager.Register<IDebugDrawingManager, DebugDrawingManager>();
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using Lidgren.Network;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Maths;
namespace Robust.Shared.Network.Messages
{
public class MsgRay : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgRay);
public uint RequestId { get; set; }
public uint SessionId { get; set; }
public Ray RayToSend { get; set; }
public MsgRay(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override void ReadFromBuffer(NetIncomingMessage buffer)
{
var origin = buffer.ReadVector2();
var dir = buffer.ReadVector2();
var mask = buffer.ReadInt32();
RayToSend = new Ray(origin, dir, mask);
}
public override void WriteToBuffer(NetOutgoingMessage buffer)
{
buffer.Write(RayToSend.Position);
buffer.Write(RayToSend.Direction);
buffer.Write(RayToSend.CollisionMask);
}
}
}

View File

@@ -3,10 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network.Messages;
namespace Robust.Shared.Physics
{
@@ -174,7 +177,10 @@ namespace Robust.Shared.Physics
IEntity entity = null;
var hitPosition = Vector2.Zero;
var minDist = maxLength;
var _network = IoCManager.Resolve<INetManager>();
var msg = _network.CreateNetMessage<MsgRay>();
msg.RayToSend = ray;
_network.ServerSendToAll(msg);
foreach (var body in _bodies)
{
if ((ray.CollisionMask & body.CollisionLayer) == 0x0)