diff --git a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs index 40730bfe0f..97454023fb 100644 --- a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs @@ -18,6 +18,7 @@ using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Map; using Robust.Shared.Players; +using System; namespace Content.Server.GameObjects.EntitySystems { @@ -121,22 +122,13 @@ namespace Content.Server.GameObjects.EntitySystems if (handsComp.GetActiveHand == null) return false; + var entCoords = ent.Transform.GridPosition.Position; + var entToDesiredDropCoords = coords.Position - entCoords; + var targetLength = Math.Min(entToDesiredDropCoords.Length, InteractionSystem.InteractionRange - 0.001f); // InteractionRange is reduced due to InRange not dealing with floating point error + var newCoords = new GridCoordinates((entToDesiredDropCoords.Normalized * targetLength) + entCoords, coords.GridID); + var rayLength = EntitySystem.Get().UnobstructedRayLength(ent.Transform.MapPosition, newCoords.ToMap(_mapManager), ignoredEnt: ent); - if(EntitySystem.Get().InRangeUnobstructed(coords.ToMap(_mapManager), ent.Transform.MapPosition, ignoredEnt: ent)) - if (coords.InRange(_mapManager, ent.Transform.GridPosition, InteractionSystem.InteractionRange)) - { - handsComp.Drop(handsComp.ActiveIndex, coords); - } - else - { - var entCoords = ent.Transform.GridPosition.Position; - var entToDesiredDropCoords = coords.Position - entCoords; - var clampedDropCoords = ((entToDesiredDropCoords.Normalized * InteractionSystem.InteractionRange) + entCoords); - - handsComp.Drop(handsComp.ActiveIndex, new GridCoordinates(clampedDropCoords, coords.GridID)); - } - else - handsComp.Drop(handsComp.ActiveIndex, ent.Transform.GridPosition); + handsComp.Drop(handsComp.ActiveIndex, new GridCoordinates(entCoords + (entToDesiredDropCoords.Normalized * rayLength), coords.GridID)); return true; } diff --git a/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs b/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs index f4a5b7d735..6b0a937ff8 100644 --- a/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs +++ b/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs @@ -26,6 +26,43 @@ namespace Content.Server.GameObjects.EntitySystems public const float InteractionRange = 2; public const float InteractionRangeSquared = InteractionRange * InteractionRange; + /// + /// Traces a ray from coords to otherCoords and returns the length + /// of the vector between coords and the ray's first hit. + /// + /// Set of coordinates to use. + /// Other set of coordinates to use. + /// the mask to check for collisions + /// . + /// Length of resulting ray. + public float UnobstructedRayLength(MapCoordinates coords, MapCoordinates otherCoords, + int collisionMask = (int) CollisionGroup.Impassable, Func predicate = null) + { + var dir = otherCoords.Position - coords.Position; + + if (dir.LengthSquared.Equals(0f)) return 0f; + + var ray = new CollisionRay(coords.Position, dir.Normalized, collisionMask); + var rayResults = _physicsManager.IntersectRayWithPredicate(coords.MapId, ray, dir.Length, predicate, returnOnFirstHit: false).ToList(); + + if (rayResults.Count == 0) return dir.Length; + return (rayResults[0].HitPos - coords.Position).Length; + } + + /// + /// Traces a ray from coords to otherCoords and returns the length + /// of the vector between coords and the ray's first hit. + /// + /// Set of coordinates to use. + /// Other set of coordinates to use. + /// the mask to check for collisions + /// the entity to be ignored when checking for collisions. + /// Length of resulting ray. + public float UnobstructedRayLength(MapCoordinates coords, MapCoordinates otherCoords, + int collisionMask = (int) CollisionGroup.Impassable, IEntity ignoredEnt = null) => + UnobstructedRayLength(coords, otherCoords, collisionMask, + ignoredEnt == null ? null : (Func) (entity => ignoredEnt == entity)); + /// /// Checks that these coordinates are within a certain distance without any /// entity that matches the collision mask obstructing them. @@ -51,7 +88,7 @@ namespace Content.Server.GameObjects.EntitySystems if (range > 0f && !(dir.LengthSquared <= range * range)) return false; var ray = new CollisionRay(coords.Position, dir.Normalized, collisionMask); - var rayResults = _physicsManager.IntersectRayWithPredicate(coords.MapId, ray, dir.Length, predicate).ToList(); + var rayResults = _physicsManager.IntersectRayWithPredicate(coords.MapId, ray, dir.Length, predicate, returnOnFirstHit: false).ToList(); return rayResults.Count == 0 || (insideBlockerValid && rayResults.Count > 0 && (rayResults[0].HitPos - otherCoords.Position).Length < 1f); }