Adds InRangeUnobstructed method to InteractionSystem (#698)

* You cannot pickup items across walls, or pickup items when dead/in crit.

* Adds InRangeUnobstructed method to InteractionSystem.
Changes HandsSystem and ItemComponent to use it.
This commit is contained in:
Víctor Aguilera Puerto
2020-02-16 23:04:06 +01:00
committed by GitHub
parent c7a171cf14
commit 70c41f63b0
6 changed files with 88 additions and 9 deletions

View File

@@ -2,11 +2,14 @@
using Content.Server.Interfaces.GameObjects; using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects; using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Items; using Content.Shared.GameObjects.Components.Items;
using Content.Shared.Physics;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Interfaces.GameObjects; using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Maths; using Robust.Shared.Maths;
@@ -24,6 +27,9 @@ namespace Content.Server.GameObjects
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IRobustRandom _robustRandom; [Dependency] private readonly IRobustRandom _robustRandom;
[Dependency] private readonly IPhysicsManager _physicsManager;
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649 #pragma warning restore 649
private string _equippedPrefix; private string _equippedPrefix;
@@ -64,8 +70,19 @@ namespace Content.Server.GameObjects
serializer.DataField(ref _equippedPrefix, "HeldPrefix", null); serializer.DataField(ref _equippedPrefix, "HeldPrefix", null);
} }
public bool CanPickup(IEntity user)
{
var coords = Owner.Transform.GridPosition;
if (!ActionBlockerSystem.CanPickup(user)) return false;
return _entitySystemManager.GetEntitySystem<InteractionSystem>()
.InRangeUnobstructed(coords, user.Transform.GridPosition, ignoredEnt:Owner);
}
public bool AttackHand(AttackHandEventArgs eventArgs) public bool AttackHand(AttackHandEventArgs eventArgs)
{ {
if (!CanPickup(eventArgs.User)) return false;
var hands = eventArgs.User.GetComponent<IHandsComponent>(); var hands = eventArgs.User.GetComponent<IHandsComponent>();
hands.PutInHand(this, hands.ActiveIndex, fallback: false); hands.PutInHand(this, hands.ActiveIndex, fallback: false);
return true; return true;
@@ -85,7 +102,7 @@ namespace Content.Server.GameObjects
protected override VerbVisibility GetVisibility(IEntity user, ItemComponent component) protected override VerbVisibility GetVisibility(IEntity user, ItemComponent component)
{ {
if (ContainerHelpers.IsInContainer(component.Owner)) if (ContainerHelpers.IsInContainer(component.Owner) || !component.CanPickup(user))
{ {
return VerbVisibility.Invisible; return VerbVisibility.Invisible;
} }

View File

@@ -62,7 +62,12 @@ namespace Content.Server.GameObjects
{ {
return true; return true;
} }
bool IActionBlocker.CanPickup()
{
return true;
}
bool IActionBlocker.CanEmote() bool IActionBlocker.CanEmote()
{ {
return true; return true;
@@ -113,7 +118,12 @@ namespace Content.Server.GameObjects
{ {
return false; return false;
} }
bool IActionBlocker.CanPickup()
{
return false;
}
bool IActionBlocker.CanEmote() bool IActionBlocker.CanEmote()
{ {
return false; return false;
@@ -184,7 +194,12 @@ namespace Content.Server.GameObjects
{ {
return false; return false;
} }
bool IActionBlocker.CanPickup()
{
return false;
}
bool IActionBlocker.CanEmote() bool IActionBlocker.CanEmote()
{ {
return false; return false;

View File

@@ -107,6 +107,11 @@ namespace Content.Server.GameObjects
return CurrentDamageState.CanDrop(); return CurrentDamageState.CanDrop();
} }
bool IActionBlocker.CanPickup()
{
return CurrentDamageState.CanPickup();
}
bool IActionBlocker.CanEmote() bool IActionBlocker.CanEmote()
{ {
return CurrentDamageState.CanEmote(); return CurrentDamageState.CanEmote();

View File

@@ -17,6 +17,8 @@ namespace Content.Server.GameObjects.EntitySystems
bool CanDrop(); bool CanDrop();
bool CanPickup();
bool CanEmote(); bool CanEmote();
} }
@@ -81,7 +83,17 @@ namespace Content.Server.GameObjects.EntitySystems
} }
return candrop; return candrop;
} }
public static bool CanPickup(IEntity entity)
{
bool canpickup = true;
foreach (var actionblockercomponents in entity.GetAllComponents<IActionBlocker>())
{
canpickup &= actionblockercomponents.CanPickup();
}
return canpickup;
}
public static bool CanEmote(IEntity entity) public static bool CanEmote(IEntity entity)
{ {
bool canemote = true; bool canemote = true;

View File

@@ -4,6 +4,7 @@ using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Timing; using Content.Server.GameObjects.Components.Timing;
using Content.Server.Interfaces.GameObjects; using Content.Server.Interfaces.GameObjects;
using Content.Shared.Input; using Content.Shared.Input;
using Content.Shared.Physics;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Player; using Robust.Server.Interfaces.Player;
@@ -14,6 +15,7 @@ using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Map; using Robust.Shared.Map;
@@ -248,6 +250,7 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager; [Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IPhysicsManager _physicsManager;
#pragma warning restore 649 #pragma warning restore 649
public const float InteractionRange = 2; public const float InteractionRange = 2;
@@ -264,6 +267,34 @@ namespace Content.Server.GameObjects.EntitySystems
new PointerInputCmdHandler(HandleActivateItemInWorld)); new PointerInputCmdHandler(HandleActivateItemInWorld));
} }
/// <summary>
/// Checks that these coordinates are within a certain distance without any
/// entity that matches the collision mask obstructing them.
/// If the <paramref name="range"/> is zero or negative,
/// this method will only check if nothing obstructs the two sets of coordinates..
/// </summary>
/// <param name="mapManager">Map manager containing the two GridIds.</param>
/// <param name="coords">Set of coordinates to use.</param>
/// <param name="otherCoords">Other set of coordinates to use.</param>
/// <param name="range">maximum distance between the two sets of coordinates.</param>
/// <param name="collisionMask">the mask to check for collisions</param>
/// <param name="ignoredEnt">the entity to be ignored when checking for collisions.</param>
/// <returns>True if the two points are within a given range without being obstructed.</returns>
public bool InRangeUnobstructed(GridCoordinates coords, GridCoordinates otherCoords, float range = InteractionRange, int collisionMask = (int)CollisionGroup.Impassable, IEntity ignoredEnt = null)
{
if (range > 0f && !coords.InRange(_mapManager, otherCoords, range))
{
return false;
}
var dir = (otherCoords.Position - coords.Position);
if (!(dir.Length > 0f)) return true;
var ray = new CollisionRay(coords.Position, dir.Normalized, collisionMask);
var rayResults = _physicsManager.IntersectRay(_mapManager.GetGrid(coords.GridID).ParentMapId, ray, dir.Length, ignoredEnt);
return !rayResults.DidHitObject;
}
private bool HandleActivateItemInWorld(ICommonSession session, GridCoordinates coords, EntityUid uid) private bool HandleActivateItemInWorld(ICommonSession session, GridCoordinates coords, EntityUid uid)
{ {
if (!EntityManager.TryGetEntity(uid, out var used)) if (!EntityManager.TryGetEntity(uid, out var used))

View File

@@ -31,6 +31,7 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager; [Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649 #pragma warning restore 649
private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons
@@ -126,11 +127,9 @@ namespace Content.Server.GameObjects.EntitySystems
if (handsComp.GetActiveHand == null) if (handsComp.GetActiveHand == null)
return false; return false;
var dir = (coords.Position - ent.Transform.GridPosition.Position); var interactionSystem = _entitySystemManager.GetEntitySystem<InteractionSystem>();
var ray = new CollisionRay(ent.Transform.GridPosition.Position, dir.Normalized, (int) CollisionGroup.Impassable);
var rayResults = IoCManager.Resolve<IPhysicsManager>().IntersectRay(ent.Transform.MapID, ray, dir.Length, ent);
if(!rayResults.DidHitObject) if(interactionSystem.InRangeUnobstructed(coords, ent.Transform.GridPosition, 0f, ignoredEnt:ent))
if (coords.InRange(_mapManager, ent.Transform.GridPosition, InteractionSystem.InteractionRange)) if (coords.InRange(_mapManager, ent.Transform.GridPosition, InteractionSystem.InteractionRange))
{ {
handsComp.Drop(handsComp.ActiveIndex, coords); handsComp.Drop(handsComp.ActiveIndex, coords);