diff --git a/Robust.Client/Console/Commands/Debug.cs b/Robust.Client/Console/Commands/Debug.cs index 592c89704..bfae55e4b 100644 --- a/Robust.Client/Console/Commands/Debug.cs +++ b/Robust.Client/Console/Commands/Debug.cs @@ -198,6 +198,7 @@ namespace Robust.Client.Console.Commands } } +#if DEBUG internal sealed class ShowRayCommand : LocalizedCommands { [Dependency] private readonly IEntitySystemManager _entitySystems = default!; @@ -224,6 +225,7 @@ namespace Robust.Client.Console.Commands mgr.DebugRayLifetime = TimeSpan.FromSeconds(duration); } } +#endif internal sealed class DisconnectCommand : LocalizedCommands { diff --git a/Robust.Client/Debugging/DebugRayDrawingSystem.cs b/Robust.Client/Debugging/DebugRayDrawingSystem.cs index d987b7c3e..8bc537729 100644 --- a/Robust.Client/Debugging/DebugRayDrawingSystem.cs +++ b/Robust.Client/Debugging/DebugRayDrawingSystem.cs @@ -4,18 +4,17 @@ using System.Numerics; using Robust.Client.Graphics; using Robust.Shared.Enums; using Robust.Shared.Debugging; -using Robust.Shared.GameObjects; using Robust.Shared.IoC; +using Robust.Shared.Map; using Robust.Shared.Maths; -using Robust.Shared.Network; using Robust.Shared.Network.Messages; -using Robust.Shared.Physics; using Robust.Shared.Timing; namespace Robust.Client.Debugging { internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem { +#if DEBUG [Dependency] private readonly IOverlayManager _overlayManager = default!; [Dependency] private readonly IGameTiming _gameTimer = default!; @@ -28,6 +27,8 @@ namespace Robust.Client.Debugging public Vector2 RayHit; public TimeSpan LifeTime; public bool DidActuallyHit; + public bool Server; + public MapId Map; } public bool DebugDrawRays @@ -73,7 +74,8 @@ namespace Robust.Client.Debugging DidActuallyHit = ev.Results != null, RayOrigin = ev.Ray.Position, RayHit = ev.Results?.HitPos ?? ev.Ray.Direction * ev.MaxLength + ev.Ray.Position, - LifeTime = _gameTimer.RealTime + DebugRayLifetime + LifeTime = _gameTimer.RealTime + DebugRayLifetime, + Map = ev.Map }; _raysWithLifeTime.Add(newRayWithLifetime); @@ -93,7 +95,9 @@ namespace Robust.Client.Debugging DidActuallyHit = msg.DidHit, RayOrigin = msg.RayOrigin, RayHit = msg.RayHit, - LifeTime = _gameTimer.RealTime + DebugRayLifetime + LifeTime = _gameTimer.RealTime + DebugRayLifetime, + Server = true, + Map = msg.Map }; _raysWithLifeTime.Add(newRayWithLifetime); @@ -114,10 +118,20 @@ namespace Robust.Client.Debugging var handle = args.WorldHandle; foreach (var ray in _owner._raysWithLifeTime) { + if (args.MapId != ray.Map) + continue; + + Color color; + if (ray.Server) + color = ray.DidActuallyHit ? Color.Cyan : Color.Orange; + else + color = ray.DidActuallyHit ? Color.Blue : Color.Red; + handle.DrawLine( ray.RayOrigin, ray.RayHit, - ray.DidActuallyHit ? Color.Yellow : Color.Magenta); + color + ); } } @@ -128,5 +142,6 @@ namespace Robust.Client.Debugging _owner._raysWithLifeTime.RemoveAll(r => r.LifeTime < _owner._gameTimer.RealTime); } } +#endif } } diff --git a/Robust.Server/Debugging/DebugRayDrawingSystem.cs b/Robust.Server/Debugging/DebugRayDrawingSystem.cs index 7bf297b1d..b8117ddff 100644 --- a/Robust.Server/Debugging/DebugRayDrawingSystem.cs +++ b/Robust.Server/Debugging/DebugRayDrawingSystem.cs @@ -1,8 +1,5 @@ using JetBrains.Annotations; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Network.Messages; -using Robust.Shared.Physics; using Robust.Shared.Debugging; namespace Robust.Server.Debugging; @@ -10,11 +7,11 @@ namespace Robust.Server.Debugging; [UsedImplicitly] internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem { +#if DEBUG protected override void ReceiveLocalRayAtMainThread(DebugRayData data) { // This code won't be called on release - eliminate it anyway for good measure. -#if DEBUG - var msg = new MsgRay {RayOrigin = data.Ray.Position}; + var msg = new MsgRay {RayOrigin = data.Ray.Position, Map = data.Map}; if (data.Results != null) { msg.DidHit = true; @@ -25,8 +22,7 @@ internal sealed class DebugRayDrawingSystem : SharedDebugRayDrawingSystem msg.RayHit = data.Ray.Position + data.Ray.Direction * data.MaxLength; } - EntityManager.EventBus.RaiseEvent(EventSource.Network, msg); -#endif + RaiseNetworkEvent(msg); } +#endif } - diff --git a/Robust.Shared/Debugging/SharedDebugRayDrawingSystem.cs b/Robust.Shared/Debugging/SharedDebugRayDrawingSystem.cs index 3bf7ff03e..5e278ebd3 100644 --- a/Robust.Shared/Debugging/SharedDebugRayDrawingSystem.cs +++ b/Robust.Shared/Debugging/SharedDebugRayDrawingSystem.cs @@ -1,36 +1,38 @@ -using System; -using System.Collections.Generic; using System.Collections.Concurrent; using System.Runtime.CompilerServices; -using Robust.Shared.Enums; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Maths; -using Robust.Shared.Network; -using Robust.Shared.Network.Messages; +using Robust.Shared.Map; using Robust.Shared.Physics; -using Robust.Shared.Timing; namespace Robust.Shared.Debugging; -[Virtual] public abstract class SharedDebugRayDrawingSystem : EntitySystem { #if DEBUG private ConcurrentBag _rayDataThreadShuntBag = new(); -#endif public override void FrameUpdate(float frameTime) { -#if DEBUG - // Pull rays into main thread for distribution. + Process(); + } + + public override void Update(float frameTime) + { + Process(); + } + + private void Process() + { + if (_rayDataThreadShuntBag.Count == 0) + return; + var arr = _rayDataThreadShuntBag.ToArray(); _rayDataThreadShuntBag.Clear(); + foreach (var drd in arr) { ReceiveLocalRayAtMainThread(drd); } -#endif } /// @@ -42,9 +44,7 @@ public abstract class SharedDebugRayDrawingSystem : EntitySystem { // If not on DEBUG, we're not going to use this anyway. // Let the inlining DCE this away. -#if DEBUG _rayDataThreadShuntBag.Add(drd); -#endif } /// @@ -53,5 +53,7 @@ public abstract class SharedDebugRayDrawingSystem : EntitySystem /// Note that on release builds (!DEBUG), this function is never called. /// protected abstract void ReceiveLocalRayAtMainThread(DebugRayData drd); -} + public readonly record struct DebugRayData(Ray Ray, float MaxLength, RayCastResults? Results, bool ServerSide, MapId Map); +#endif +} diff --git a/Robust.Shared/Network/Messages/MsgRay.cs b/Robust.Shared/Network/Messages/MsgRay.cs index ccc1de3ef..06e9998ef 100644 --- a/Robust.Shared/Network/Messages/MsgRay.cs +++ b/Robust.Shared/Network/Messages/MsgRay.cs @@ -1,16 +1,19 @@ -using System.Numerics; -using Lidgren.Network; +using System; +using System.Numerics; using Robust.Shared.GameObjects; -using Robust.Shared.Maths; +using Robust.Shared.Map; +using Robust.Shared.Serialization; -#nullable disable +namespace Robust.Shared.Network.Messages; -namespace Robust.Shared.Network.Messages +/// +/// Debug message contain physics collision ray data. +/// +[Serializable, NetSerializable] +public sealed class MsgRay : EntityEventArgs { - public sealed class MsgRay : EntityEventArgs - { - public Vector2 RayOrigin { get; set; } - public Vector2 RayHit { get; set; } - public bool DidHit { get; set; } - } + public Vector2 RayOrigin; + public Vector2 RayHit; + public bool DidHit; + public MapId Map; } diff --git a/Robust.Shared/Physics/DebugRayData.cs b/Robust.Shared/Physics/DebugRayData.cs deleted file mode 100644 index a92c1fa6a..000000000 --- a/Robust.Shared/Physics/DebugRayData.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Runtime.CompilerServices; -using JetBrains.Annotations; -using Robust.Shared.Maths; - -namespace Robust.Shared.Physics -{ - public struct DebugRayData - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public DebugRayData(Ray ray, float maxLength, [CanBeNull] RayCastResults? results) - { - Ray = ray; - MaxLength = maxLength; - Results = results; - } - - public Ray Ray - { - get; - } - - public RayCastResults? Results { get; } - public float MaxLength { get; } - } -} diff --git a/Robust.Shared/Physics/RayCastResults.cs b/Robust.Shared/Physics/RayCastResults.cs index e28b17a65..44cd7f980 100644 --- a/Robust.Shared/Physics/RayCastResults.cs +++ b/Robust.Shared/Physics/RayCastResults.cs @@ -1,6 +1,5 @@ using System.Numerics; using Robust.Shared.GameObjects; -using Robust.Shared.Maths; namespace Robust.Shared.Physics { diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs index 5fe04db79..a6d9855ad 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Queries.cs @@ -6,6 +6,7 @@ using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; +using Robust.Shared.Network; using Robust.Shared.Physics.Collision; using Robust.Shared.Physics.Collision.Shapes; using Robust.Shared.Physics.Components; @@ -20,6 +21,7 @@ namespace Robust.Shared.Physics.Systems public partial class SharedPhysicsSystem { [Dependency] private readonly SharedDebugRayDrawingSystem _sharedDebugRaySystem = default!; + [Dependency] private readonly INetManager _netMan = default!; /// /// Checks to see if the specified collision rectangle collides with any of the physBodies under management. @@ -300,7 +302,9 @@ namespace Robust.Shared.Physics.Systems // Need to convert it back to world-space. var result = new RayCastResults(distFromOrigin, matrix.Transform(point), proxy.Entity); results.Add(result); - _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, result)); +#if DEBUG + _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, result, _netMan.IsServer, mapId)); +#endif return true; }, gridRay); @@ -326,15 +330,19 @@ namespace Robust.Shared.Physics.Systems // Need to convert it back to world-space. var result = new RayCastResults(distFromOrigin, matrix.Transform(point), proxy.Entity); results.Add(result); - _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, result)); +#if DEBUG + _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, result, _netMan.IsServer, mapId)); +#endif return true; }, gridRay); } +#if DEBUG if (results.Count == 0) { - _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, null)); + _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, null, _netMan.IsServer, mapId)); } +#endif results.Sort((a, b) => a.Distance.CompareTo(b.Distance)); return results; @@ -423,7 +431,9 @@ namespace Robust.Shared.Physics.Systems } // This hid rays that didn't penetrate something. Don't hide those because that causes rays to disappear that shouldn't. - _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new DebugRayData(ray, maxLength, null)); +#if DEBUG + _sharedDebugRaySystem.ReceiveLocalRayFromAnyThread(new(ray, maxLength, null, _netMan.IsServer, mapId)); +#endif return penetration; }