From 8a81f54a45c29c8cebafaec69fdd25662ab2641c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Aguilera=20Puerto?= Date: Tue, 26 May 2020 14:23:25 +0200 Subject: [PATCH] Makes InRangeUnobstructed use MapCoordinates. Fixes #1003 (Also updates Submoduke) --- Content.Client/State/GameScreenBase.cs | 2 +- .../Construction/ConstructorComponent.cs | 2 +- .../Interactable/TilePryingComponent.cs | 2 +- .../Components/Items/Storage/ItemComponent.cs | 2 +- .../GameObjects/Components/WiresComponent.cs | 2 +- .../GameObjects/EntitySystems/HandsSystem.cs | 2 +- Content.Server/Utility/InteractionChecks.cs | 12 ++++----- .../EntitySystems/ExamineSystemShared.cs | 2 +- .../EntitySystems/SharedInteractionSystem.cs | 25 ++++++------------- RobustToolbox | 2 +- 10 files changed, 21 insertions(+), 32 deletions(-) diff --git a/Content.Client/State/GameScreenBase.cs b/Content.Client/State/GameScreenBase.cs index a673870c62..f1c0843b2c 100644 --- a/Content.Client/State/GameScreenBase.cs +++ b/Content.Client/State/GameScreenBase.cs @@ -58,7 +58,7 @@ namespace Content.Client.State if (_playerManager.LocalPlayer.ControlledEntity != null && entityToClick != null) { var playerPos = _playerManager.LocalPlayer.ControlledEntity.Transform.MapPosition; - var entityPos = entityToClick.Transform.WorldPosition; + var entityPos = entityToClick.Transform.MapPosition; inRange = _entitySystemManager.GetEntitySystem() .InRangeUnobstructed(playerPos, entityPos, predicate:entity => entity != _playerManager.LocalPlayer.ControlledEntity || entity != entityToClick); } diff --git a/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs b/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs index 53e1b83886..5ac9f6a4c8 100644 --- a/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs +++ b/Content.Server/GameObjects/Components/Construction/ConstructorComponent.cs @@ -50,7 +50,7 @@ namespace Content.Server.GameObjects.Components.Construction { var prototype = _prototypeManager.Index(prototypeName); - if (!InteractionChecks.InRangeUnobstructed(Owner, loc.ToMapPos(_mapManager), + if (!InteractionChecks.InRangeUnobstructed(Owner, loc.ToMap(_mapManager), ignoredEnt: Owner, insideBlockerValid: prototype.CanBuildInImpassable)) { return; diff --git a/Content.Server/GameObjects/Components/Interactable/TilePryingComponent.cs b/Content.Server/GameObjects/Components/Interactable/TilePryingComponent.cs index 32b87fa525..4a08588a5f 100644 --- a/Content.Server/GameObjects/Components/Interactable/TilePryingComponent.cs +++ b/Content.Server/GameObjects/Components/Interactable/TilePryingComponent.cs @@ -47,7 +47,7 @@ namespace Content.Server.GameObjects.Components.Interactable var coordinates = mapGrid.GridTileToLocal(tile.GridIndices); - if (!_entitySystemManager.GetEntitySystem().InRangeUnobstructed(user.Transform.MapPosition, coordinates.ToMapPos(_mapManager), ignoredEnt:user)) + if (!_entitySystemManager.GetEntitySystem().InRangeUnobstructed(user.Transform.MapPosition, coordinates.ToMap(_mapManager), ignoredEnt:user)) return; var tileDef = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId]; diff --git a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs index f80d7e977a..9e71faa71e 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs @@ -97,7 +97,7 @@ namespace Content.Server.GameObjects return false; var userPos = user.Transform.MapPosition; - var itemPos = Owner.Transform.WorldPosition; + var itemPos = Owner.Transform.MapPosition; return InteractionChecks.InRangeUnobstructed(user, itemPos, ignoredEnt: Owner, insideBlockerValid:true); } diff --git a/Content.Server/GameObjects/Components/WiresComponent.cs b/Content.Server/GameObjects/Components/WiresComponent.cs index 6cc34385f4..1d42032427 100644 --- a/Content.Server/GameObjects/Components/WiresComponent.cs +++ b/Content.Server/GameObjects/Components/WiresComponent.cs @@ -239,7 +239,7 @@ namespace Content.Server.GameObjects.Components return; } - if (!EntitySystem.Get().InRangeUnobstructed(player.Transform.MapPosition, Owner.Transform.WorldPosition, ignoredEnt: Owner)) + if (!EntitySystem.Get().InRangeUnobstructed(player.Transform.MapPosition, Owner.Transform.MapPosition, ignoredEnt: Owner)) { _notifyManager.PopupMessage(Owner.Transform.GridPosition, player, _localizationManager.GetString("You can't reach there!")); return; diff --git a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs index 6751a04a5c..40730bfe0f 100644 --- a/Content.Server/GameObjects/EntitySystems/HandsSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/HandsSystem.cs @@ -122,7 +122,7 @@ namespace Content.Server.GameObjects.EntitySystems return false; - if(EntitySystem.Get().InRangeUnobstructed(coords.ToMap(_mapManager), ent.Transform.WorldPosition, 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); diff --git a/Content.Server/Utility/InteractionChecks.cs b/Content.Server/Utility/InteractionChecks.cs index 107e3120ec..3588c61944 100644 --- a/Content.Server/Utility/InteractionChecks.cs +++ b/Content.Server/Utility/InteractionChecks.cs @@ -28,7 +28,7 @@ namespace Content.Server.Utility public static bool InRangeUnobstructed(ITargetedInteractEventArgs eventArgs, bool insideBlockerValid = true) { if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition, - eventArgs.Target.Transform.WorldPosition, ignoredEnt: eventArgs.Target, insideBlockerValid: insideBlockerValid)) + eventArgs.Target.Transform.MapPosition, ignoredEnt: eventArgs.Target, insideBlockerValid: insideBlockerValid)) { var localizationManager = IoCManager.Resolve(); eventArgs.Target.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!")); @@ -50,7 +50,7 @@ namespace Content.Server.Utility if (eventArgs.Target != null) { if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition, - eventArgs.Target.Transform.WorldPosition, ignoredEnt: eventArgs.Target, insideBlockerValid: insideBlockerValid)) + eventArgs.Target.Transform.MapPosition, ignoredEnt: eventArgs.Target, insideBlockerValid: insideBlockerValid)) { var localizationManager = IoCManager.Resolve(); eventArgs.Target.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!")); @@ -59,9 +59,8 @@ namespace Content.Server.Utility } else { - var mapManager = IoCManager.Resolve(); if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition, - eventArgs.ClickLocation.ToMapPos(mapManager), ignoredEnt: eventArgs.User, insideBlockerValid: insideBlockerValid)) + eventArgs.ClickLocation.ToMap(IoCManager.Resolve()), ignoredEnt: eventArgs.User, insideBlockerValid: insideBlockerValid)) { var localizationManager = IoCManager.Resolve(); eventArgs.User.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!")); @@ -77,13 +76,14 @@ namespace Content.Server.Utility /// Convenient static alternative to , which also /// shows a popup message if not in range. /// - public static bool InRangeUnobstructed(IEntity user, Vector2 targetWorldCoords, + public static bool InRangeUnobstructed(IEntity user, MapCoordinates otherCoords, float range = SharedInteractionSystem.InteractionRange, int collisionMask = (int) CollisionGroup.Impassable, IEntity ignoredEnt = null, bool insideBlockerValid = false) { + var mapManager = IoCManager.Resolve(); var interactionSystem = EntitySystem.Get(); - if (!interactionSystem.InRangeUnobstructed(user.Transform.MapPosition, targetWorldCoords, range, collisionMask, + if (!interactionSystem.InRangeUnobstructed(user.Transform.MapPosition, otherCoords, range, collisionMask, ignoredEnt, insideBlockerValid)) { var localizationManager = IoCManager.Resolve(); diff --git a/Content.Shared/GameObjects/EntitySystems/ExamineSystemShared.cs b/Content.Shared/GameObjects/EntitySystems/ExamineSystemShared.cs index f75efd385b..fb24f9c6c7 100644 --- a/Content.Shared/GameObjects/EntitySystems/ExamineSystemShared.cs +++ b/Content.Shared/GameObjects/EntitySystems/ExamineSystemShared.cs @@ -31,7 +31,7 @@ namespace Content.Shared.GameObjects.EntitySystems } return EntitySystem.Get() - .InRangeUnobstructed(examiner.Transform.MapPosition, examined.Transform.MapPosition.Position, + .InRangeUnobstructed(examiner.Transform.MapPosition, examined.Transform.MapPosition, ExamineRange, predicate: entity => entity == examiner || entity == examined, insideBlockerValid:true); } } diff --git a/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs b/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs index 77bc6965c4..f4a5b7d735 100644 --- a/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs +++ b/Content.Shared/GameObjects/EntitySystems/SharedInteractionSystem.cs @@ -39,31 +39,20 @@ namespace Content.Server.GameObjects.EntitySystems /// . /// if coordinates inside obstructions count as obstructed or not /// True if the two points are within a given range without being obstructed. - public bool InRangeUnobstructed(MapCoordinates coords, Vector2 otherCoords, float range = InteractionRange, + public bool InRangeUnobstructed(MapCoordinates coords, MapCoordinates otherCoords, float range = InteractionRange, int collisionMask = (int)CollisionGroup.Impassable, Func predicate = null, bool insideBlockerValid = false) { - var dir = otherCoords - coords.Position; + if (!coords.InRange(otherCoords, range)) + return false; + + var dir = otherCoords.Position - coords.Position; if (dir.LengthSquared.Equals(0f)) return true; 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(); - if(rayResults.Count == 0 || (insideBlockerValid && rayResults.Count > 0 && (rayResults[0].HitPos - otherCoords).Length < 1f)) - { - - if (_mapManager.TryFindGridAt(coords, out var mapGrid) && mapGrid != null) - { - var srcPos = mapGrid.MapToGrid(coords); - var destPos = new GridCoordinates(otherCoords, mapGrid); - if (srcPos.InRange(_mapManager, destPos, range)) - { - return true; - } - } - - } - return false; + return rayResults.Count == 0 || (insideBlockerValid && rayResults.Count > 0 && (rayResults[0].HitPos - otherCoords.Position).Length < 1f); } /// @@ -79,7 +68,7 @@ namespace Content.Server.GameObjects.EntitySystems /// the entity to be ignored when checking for collisions. /// if coordinates inside obstructions count as obstructed or not /// True if the two points are within a given range without being obstructed. - public bool InRangeUnobstructed(MapCoordinates coords, Vector2 otherCoords, float range = InteractionRange, + public bool InRangeUnobstructed(MapCoordinates coords, MapCoordinates otherCoords, float range = InteractionRange, int collisionMask = (int)CollisionGroup.Impassable, IEntity ignoredEnt = null, bool insideBlockerValid = false) => InRangeUnobstructed(coords, otherCoords, range, collisionMask, ignoredEnt == null ? null : (Func)(entity => ignoredEnt == entity), insideBlockerValid); diff --git a/RobustToolbox b/RobustToolbox index deebe5b274..8d2dcb23f7 160000 --- a/RobustToolbox +++ b/RobustToolbox @@ -1 +1 @@ -Subproject commit deebe5b2743eef23970b9685b1adf03a043e9497 +Subproject commit 8d2dcb23f7695fb412bdbaed6465a1699eb95a48