using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Physics;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
namespace Content.Server.Utility
{
///
/// Convenient methods for checking for various conditions commonly needed
/// for interactions.
///
public static class InteractionChecks
{
///
/// Default interaction check for targeted attack interaction types.
/// Same as , but defaults to ignore inside blockers
/// (making the check less restrictive).
/// Validates that attacker is in range of the attacked entity. Additionally shows a popup if
/// validation fails.
///
public static bool InRangeUnobstructed(ITargetedInteractEventArgs eventArgs, bool ignoreInsideBlocker = true)
{
if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition,
eventArgs.Target.Transform.MapPosition, ignoredEnt: eventArgs.Target, ignoreInsideBlocker: ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
eventArgs.Target.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!"));
return false;
}
return true;
}
///
/// Default interaction check for Drag drop interaction types.
/// Same as , but defaults to ignore inside blockers
/// (making the check less restrictive) and checks reachability of both the target and the dragged / dropped object.
/// Additionally shows a popup if validation fails.
///
public static bool InRangeUnobstructed(DragDropEventArgs eventArgs, bool ignoreInsideBlocker = true)
{
if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition,
eventArgs.Target.Transform.MapPosition, ignoredEnt: eventArgs.Target, ignoreInsideBlocker: ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
eventArgs.Target.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!"));
return false;
}
if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition,
eventArgs.Dropped.Transform.MapPosition, ignoredEnt: eventArgs.Dropped, ignoreInsideBlocker: ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
eventArgs.Dropped.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!"));
return false;
}
return true;
}
///
/// Default interaction check for after attack interaction types.
/// Same as , but defaults to ignore inside blockers
/// (making the check less restrictive).
/// Validates that attacker is in range of the attacked entity, if there is such an entity.
/// If there is no attacked entity, validates that they are in range of the clicked position.
/// Additionally shows a popup if validation fails.
///
public static bool InRangeUnobstructed(AfterInteractEventArgs eventArgs, bool ignoreInsideBlocker = true)
{
if (eventArgs.Target != null)
{
if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition,
eventArgs.Target.Transform.MapPosition, ignoredEnt: eventArgs.Target, ignoreInsideBlocker: ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
eventArgs.Target.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!"));
return false;
}
}
else
{
if (!EntitySystem.Get().InRangeUnobstructed(eventArgs.User.Transform.MapPosition,
eventArgs.ClickLocation.ToMap(IoCManager.Resolve()), ignoredEnt: eventArgs.User, ignoreInsideBlocker: ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
eventArgs.User.PopupMessage(eventArgs.User, localizationManager.GetString("You can't reach there!"));
return false;
}
}
return true;
}
///
/// Convenient static alternative to , which also
/// shows a popup message if not in range.
///
public static bool InRangeUnobstructed(IEntity user, MapCoordinates otherCoords,
float range = SharedInteractionSystem.InteractionRange,
int collisionMask = (int) CollisionGroup.Impassable, IEntity ignoredEnt = null, bool ignoreInsideBlocker = false)
{
var mapManager = IoCManager.Resolve();
var interactionSystem = EntitySystem.Get();
if (!interactionSystem.InRangeUnobstructed(user.Transform.MapPosition, otherCoords, range, collisionMask,
ignoredEnt, ignoreInsideBlocker))
{
var localizationManager = IoCManager.Resolve();
user.PopupMessage(user, localizationManager.GetString("You can't reach there!"));
return false;
}
return true;
}
}
}