Generalize tile prying to any tool quality (#24432)
* Generalize tile prying to any tool quality * ballin
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Tools.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Allows prying tiles up on a grid.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||
public sealed partial class TilePryingComponent : Component
|
||||
{
|
||||
[DataField("toolComponentNeeded"), AutoNetworkedField]
|
||||
public bool ToolComponentNeeded = true;
|
||||
|
||||
[DataField("qualityNeeded", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>)), AutoNetworkedField]
|
||||
public string QualityNeeded = "Prying";
|
||||
|
||||
/// <summary>
|
||||
/// Whether this tool can pry tiles with CanAxe.
|
||||
/// </summary>
|
||||
[DataField("advanced"), AutoNetworkedField]
|
||||
public bool Advanced = false;
|
||||
|
||||
[DataField("delay"), AutoNetworkedField]
|
||||
public float Delay = 1f;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Tools.Systems;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Tools.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for entities with <see cref="ToolComponent"/> that are additionally
|
||||
/// able to modify tiles.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
[Access(typeof(SharedToolSystem))]
|
||||
public sealed partial class ToolTileCompatibleComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The time it takes to modify the tile.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public TimeSpan Delay = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the tile being modified must be unobstructed
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool RequiresUnobstructed = true;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed partial class TileToolDoAfterEvent : DoAfterEvent
|
||||
{
|
||||
public NetCoordinates Coordinates;
|
||||
|
||||
public TileToolDoAfterEvent(NetCoordinates coordinates)
|
||||
{
|
||||
Coordinates = coordinates;
|
||||
}
|
||||
|
||||
public override DoAfterEvent Clone()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Content.Shared.Tools.Components;
|
||||
|
||||
namespace Content.Shared.Tools.Systems;
|
||||
|
||||
public abstract partial class SharedToolSystem : EntitySystem
|
||||
public abstract partial class SharedToolSystem
|
||||
{
|
||||
public void InitializeMultipleTool()
|
||||
{
|
||||
@@ -57,7 +57,7 @@ public abstract partial class SharedToolSystem : EntitySystem
|
||||
if (!Resolve(uid, ref multiple, ref tool))
|
||||
return;
|
||||
|
||||
Dirty(multiple);
|
||||
Dirty(uid, multiple);
|
||||
|
||||
if (multiple.Entries.Length <= multiple.CurrentEntry)
|
||||
{
|
||||
|
||||
103
Content.Shared/Tools/Systems/SharedToolSystem.Tile.cs
Normal file
103
Content.Shared/Tools/Systems/SharedToolSystem.Tile.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Fluids.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Tools.Systems;
|
||||
|
||||
public abstract partial class SharedToolSystem
|
||||
{
|
||||
[Dependency] private readonly INetManager _net = default!;
|
||||
|
||||
public void InitializeTile()
|
||||
{
|
||||
SubscribeLocalEvent<ToolTileCompatibleComponent, AfterInteractEvent>(OnToolTileAfterInteract);
|
||||
SubscribeLocalEvent<ToolTileCompatibleComponent, TileToolDoAfterEvent>(OnToolTileComplete);
|
||||
}
|
||||
|
||||
private void OnToolTileAfterInteract(Entity<ToolTileCompatibleComponent> ent, ref AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled || args.Target != null && !HasComp<PuddleComponent>(args.Target))
|
||||
return;
|
||||
|
||||
args.Handled = UseToolOnTile((ent, ent, null), args.User, args.ClickLocation);
|
||||
}
|
||||
|
||||
private void OnToolTileComplete(Entity<ToolTileCompatibleComponent> ent, ref TileToolDoAfterEvent args)
|
||||
{
|
||||
var comp = ent.Comp;
|
||||
if (args.Handled || args.Cancelled)
|
||||
return;
|
||||
|
||||
if (!TryComp<ToolComponent>(ent, out var tool))
|
||||
return;
|
||||
var coordinates = GetCoordinates(args.Coordinates);
|
||||
|
||||
var gridUid = coordinates.GetGridUid(EntityManager);
|
||||
if (!TryComp<MapGridComponent>(gridUid, out var grid))
|
||||
{
|
||||
Log.Error("Attempted use tool on a non-existent grid?");
|
||||
return;
|
||||
}
|
||||
|
||||
var tileRef = _maps.GetTileRef(gridUid.Value, grid, coordinates);
|
||||
if (comp.RequiresUnobstructed && _turfs.IsTileBlocked(gridUid.Value, tileRef.GridIndices, CollisionGroup.MobMask))
|
||||
return;
|
||||
|
||||
if (!TryDeconstructWithToolQualities(tileRef, tool.Qualities))
|
||||
return;
|
||||
|
||||
AdminLogger.Add(LogType.LatticeCut, LogImpact.Medium,
|
||||
$"{ToPrettyString(args.User):player} used {ToPrettyString(ent)} to edit the tile at {args.Coordinates}");
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private bool UseToolOnTile(Entity<ToolTileCompatibleComponent?, ToolComponent?> ent, EntityUid user, EntityCoordinates clickLocation)
|
||||
{
|
||||
if (!Resolve(ent, ref ent.Comp1, ref ent.Comp2, false))
|
||||
return false;
|
||||
|
||||
var comp = ent.Comp1!;
|
||||
var tool = ent.Comp2!;
|
||||
|
||||
if (!_mapManager.TryFindGridAt(clickLocation.ToMap(EntityManager, _transformSystem), out var gridUid, out var mapGrid))
|
||||
return false;
|
||||
|
||||
var tileRef = _maps.GetTileRef(gridUid, mapGrid, clickLocation);
|
||||
var tileDef = (ContentTileDefinition) _tileDefManager[tileRef.Tile.TypeId];
|
||||
|
||||
if (!tool.Qualities.ContainsAny(tileDef.DeconstructTools))
|
||||
return false;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(tileDef.BaseTurf))
|
||||
return false;
|
||||
|
||||
if (comp.RequiresUnobstructed && _turfs.IsTileBlocked(gridUid, tileRef.GridIndices, CollisionGroup.MobMask))
|
||||
return false;
|
||||
|
||||
var coordinates = _maps.GridTileToLocal(gridUid, mapGrid, tileRef.GridIndices);
|
||||
if (!InteractionSystem.InRangeUnobstructed(user, coordinates, popup: false))
|
||||
return false;
|
||||
|
||||
var args = new TileToolDoAfterEvent(GetNetCoordinates(coordinates));
|
||||
UseTool(ent, user, ent, comp.Delay, tool.Qualities, args, out _, toolComponent: tool);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryDeconstructWithToolQualities(TileRef tileRef, PrototypeFlags<ToolQualityPrototype> withToolQualities)
|
||||
{
|
||||
var tileDef = (ContentTileDefinition) _tileDefManager[tileRef.Tile.TypeId];
|
||||
if (withToolQualities.ContainsAny(tileDef.DeconstructTools))
|
||||
{
|
||||
// don't do this on the client or else the tile entity spawn mispredicts and looks horrible
|
||||
return _net.IsClient || _tiles.DeconstructTile(tileRef);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Fluids.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
|
||||
namespace Content.Shared.Tools.Systems;
|
||||
|
||||
public abstract partial class SharedToolSystem
|
||||
{
|
||||
private void InitializeTilePrying()
|
||||
{
|
||||
SubscribeLocalEvent<TilePryingComponent, AfterInteractEvent>(OnTilePryingAfterInteract);
|
||||
SubscribeLocalEvent<TilePryingComponent, TilePryingDoAfterEvent>(OnTilePryComplete);
|
||||
}
|
||||
|
||||
private void OnTilePryingAfterInteract(EntityUid uid, TilePryingComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled || !args.CanReach || args.Target != null && !HasComp<PuddleComponent>(args.Target))
|
||||
return;
|
||||
|
||||
if (TryPryTile(uid, args.User, component, args.ClickLocation))
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnTilePryComplete(EntityUid uid, TilePryingComponent component, TilePryingDoAfterEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
return;
|
||||
|
||||
var coords = GetCoordinates(args.Coordinates);
|
||||
var gridUid = coords.GetGridUid(EntityManager);
|
||||
if (!TryComp(gridUid, out MapGridComponent? grid))
|
||||
{
|
||||
Log.Error("Attempted to pry from a non-existent grid?");
|
||||
return;
|
||||
}
|
||||
|
||||
var tile = _maps.GetTileRef(gridUid.Value, grid, coords);
|
||||
var center = _turfs.GetTileCenter(tile);
|
||||
|
||||
if (args.Used != null)
|
||||
{
|
||||
_adminLogger.Add(LogType.Tile, LogImpact.Low,
|
||||
$"{ToPrettyString(args.User):actor} used {ToPrettyString(args.Used.Value):tool} to pry {_tileDefManager[tile.Tile.TypeId].Name} at {center}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_adminLogger.Add(LogType.Tile, LogImpact.Low,
|
||||
$"{ToPrettyString(args.User):actor} pried {_tileDefManager[tile.Tile.TypeId].Name} at {center}");
|
||||
}
|
||||
|
||||
if (_netManager.IsServer)
|
||||
_tiles.PryTile(tile, component.Advanced);
|
||||
}
|
||||
|
||||
private bool TryPryTile(EntityUid toolEntity, EntityUid user, TilePryingComponent component, EntityCoordinates clickLocation)
|
||||
{
|
||||
if (!TryComp<ToolComponent>(toolEntity, out var tool) && component.ToolComponentNeeded)
|
||||
return false;
|
||||
|
||||
if (!_mapManager.TryFindGridAt(clickLocation.ToMap(EntityManager, _transformSystem), out var gridUid, out var mapGrid))
|
||||
return false;
|
||||
|
||||
var tile = _maps.GetTileRef(gridUid, mapGrid, clickLocation);
|
||||
var coordinates = _maps.GridTileToLocal(gridUid, mapGrid, tile.GridIndices);
|
||||
|
||||
if (!InteractionSystem.InRangeUnobstructed(user, coordinates, popup: false))
|
||||
return false;
|
||||
|
||||
var tileDef = (ContentTileDefinition) _tileDefManager[tile.Tile.TypeId];
|
||||
|
||||
if (!tileDef.CanCrowbar && !(tileDef.CanAxe && component.Advanced))
|
||||
return false;
|
||||
|
||||
var ev = new TilePryingDoAfterEvent(GetNetCoordinates(coordinates));
|
||||
|
||||
return UseTool(toolEntity, user, toolEntity, component.Delay, component.QualityNeeded, ev, toolComponent: tool);
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,8 @@ using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
@@ -16,9 +14,8 @@ namespace Content.Shared.Tools.Systems;
|
||||
public abstract partial class SharedToolSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly INetManager _netManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
|
||||
@@ -31,7 +28,7 @@ public abstract partial class SharedToolSystem : EntitySystem
|
||||
public override void Initialize()
|
||||
{
|
||||
InitializeMultipleTool();
|
||||
InitializeTilePrying();
|
||||
InitializeTile();
|
||||
SubscribeLocalEvent<ToolComponent, ToolDoAfterEvent>(OnDoAfter);
|
||||
}
|
||||
|
||||
@@ -151,8 +148,6 @@ public abstract partial class SharedToolSystem : EntitySystem
|
||||
/// <param name="toolQualityNeeded">The quality needed for this tool to work.</param>
|
||||
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
|
||||
/// will be directed at the tool target.</param>
|
||||
/// <param name="id">The id of the DoAfter that was created. This may be null even if the function returns true in
|
||||
/// the event that this tool-use cancelled an existing DoAfter</param>
|
||||
/// <param name="toolComponent">The tool component.</param>
|
||||
/// <returns>Returns true if any interaction takes place.</returns>
|
||||
public bool UseTool(
|
||||
|
||||
Reference in New Issue
Block a user