From c120edc0978084cf802e79eb25f262f5345203ff Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Sun, 5 Nov 2023 16:00:23 +0100 Subject: [PATCH] Moved to shared, fixed some problems with code --- .../Tools/ToolSystem.EarthDigging.cs | 155 ------------------ Content.Server/Tools/ToolSystem.cs | 1 - .../Nyanotrasen/Digging/DiggingEvents.cs | 23 +++ .../Digging}/EarthDiggingComponent.cs | 10 +- .../Digging/SharedDiggingSystem.cs | 115 +++++++++++++ 5 files changed, 141 insertions(+), 163 deletions(-) delete mode 100644 Content.Server/Nyanotrasen/Tools/ToolSystem.EarthDigging.cs create mode 100644 Content.Shared/Nyanotrasen/Digging/DiggingEvents.cs rename {Content.Server/Nyanotrasen/Tools/Components => Content.Shared/Nyanotrasen/Digging}/EarthDiggingComponent.cs (74%) create mode 100644 Content.Shared/Nyanotrasen/Digging/SharedDiggingSystem.cs diff --git a/Content.Server/Nyanotrasen/Tools/ToolSystem.EarthDigging.cs b/Content.Server/Nyanotrasen/Tools/ToolSystem.EarthDigging.cs deleted file mode 100644 index bca3121f2d39..000000000000 --- a/Content.Server/Nyanotrasen/Tools/ToolSystem.EarthDigging.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System.Threading; -using Content.Server.Interaction; -using Robust.Shared.Map; -using Content.Server.Tools.Components; -using Content.Shared.DoAfter; -using Content.Shared.Interaction; -using Content.Shared.Maps; -using Content.Shared.Physics; -using Content.Shared.Tools.Components; -using Robust.Server.GameObjects; -using Robust.Shared.Map.Components; -using Robust.Shared.Serialization; - -namespace Content.Server.Tools; - -public sealed partial class ToolSystem -{ - [Dependency] private readonly TurfSystem _turf = default!; - [Dependency] private readonly InteractionSystem _interactionSystem = default!; - [Dependency] private readonly MapSystem _mapSystem = default!; - - private void InitializeEarthDigging() - { - SubscribeLocalEvent(OnEarthDiggingAfterInteract); - SubscribeLocalEvent(OnEarthDigComplete); - SubscribeLocalEvent(OnEarthDigCancelled); - } - - private void OnEarthDigCancelled(EarthDiggingCancelledEvent args) - { - if (!TryComp(args.Shovel, out var component)) - return; - component.CancelToken = null; - } - - private void OnEarthDigComplete(EarthDiggingCompleteEvent args) - { - if (!TryComp(args.Shovel, out var component)) - return; - component.CancelToken = null; - - var gridUid = args.Coordinates.GetGridUid(EntityManager); - if (gridUid == null) - return; - - var grid = Comp(gridUid.Value); - var tile = _mapSystem.GetTileRef(gridUid.Value, grid, args.Coordinates); - - if (_tileDefinitionManager[tile.Tile.TypeId] is not ContentTileDefinition tileDef - || !tileDef.CanShovel - // || tileDef.BaseTurfs.Count == 0 - || _turf.IsTileBlocked(tile, CollisionGroup.MobMask)) - { - return; - } - - _tile.DigTile(tile); - } - - private void OnEarthDiggingAfterInteract(EntityUid uid, EarthDiggingComponent component, - AfterInteractEvent args) - { - if (args.Handled || !args.CanReach || args.Target != null) - return; - - if (TryDig(args.User, uid, component, args.ClickLocation)) - args.Handled = true; - } - - private bool TryDig(EntityUid user, EntityUid shovel, EarthDiggingComponent component, EntityCoordinates clickLocation) - { - if (component.CancelToken != null) - return true; - - ToolComponent? tool = null; - if (component.ToolComponentNeeded && !TryComp(shovel, out tool)) - return false; - - if (!_mapManager.TryGetGrid(clickLocation.GetGridUid(EntityManager), out var mapGrid)) - return false; - - var tile = mapGrid.GetTileRef(clickLocation); - - var coordinates = mapGrid.GridTileToLocal(tile.GridIndices); - - if (!_interactionSystem.InRangeUnobstructed(user, coordinates, popup: false)) - return false; - - if (_tileDefinitionManager[tile.Tile.TypeId] is not ContentTileDefinition tileDef - || !tileDef.CanShovel - || tileDef.BaseTurf.Length == 0 - // || _tileDefinitionManager[tileDef.BaseTurfs[^1]] is not ContentTileDefinition newDef - || tile.IsBlockedTurf(true)) - { - return false; - } - - var token = new CancellationTokenSource(); - component.CancelToken = token; - - var success = UseTool( - shovel, - user, - // FIXME - target: EntityUid.FirstUid, - doAfterDelay: component.Delay, - toolQualitiesNeeded: new []{component.QualityNeeded}, - doAfterEv: new EarthDiggingCompleteEvent - { - Coordinates = clickLocation, - Shovel = shovel, - }, - toolComponent: tool - ); - - // bool success = UseTool( - // component.Owner, - // user, - // null, - // 0f, - // component.Delay, - // new [] {component.QualityNeeded}, - // new EarthDiggingCompleteEvent - // { - // Coordinates = clickLocation, - // Shovel = shovel, - // }, - // new EarthDiggingCancelledEvent() - // { - // Shovel = shovel, - // }, - // toolComponent: tool); - - if (!success) - component.CancelToken = null; - - return true; - } - - [Serializable, NetSerializable] - private sealed partial class EarthDiggingCompleteEvent : DoAfterEvent - { - public EntityCoordinates Coordinates { get; set; } - public EntityUid Shovel; - public override DoAfterEvent Clone() - { - return this; - } - } - - private sealed class EarthDiggingCancelledEvent : EntityEventArgs - { - public EntityUid Shovel; - } -} diff --git a/Content.Server/Tools/ToolSystem.cs b/Content.Server/Tools/ToolSystem.cs index ee0f662d16fe..88a96dc1e8da 100644 --- a/Content.Server/Tools/ToolSystem.cs +++ b/Content.Server/Tools/ToolSystem.cs @@ -28,7 +28,6 @@ public override void Initialize() base.Initialize(); InitializeLatticeCutting(); - InitializeEarthDigging(); // Delta V InitializeWelders(); } diff --git a/Content.Shared/Nyanotrasen/Digging/DiggingEvents.cs b/Content.Shared/Nyanotrasen/Digging/DiggingEvents.cs new file mode 100644 index 000000000000..f8b3829d8890 --- /dev/null +++ b/Content.Shared/Nyanotrasen/Digging/DiggingEvents.cs @@ -0,0 +1,23 @@ +using Content.Shared.DoAfter; +using Robust.Shared.Map; +using Robust.Shared.Serialization; + +namespace Content.Shared.Nyanotrasen.Digging; + + +[Serializable, NetSerializable] +public sealed partial class EarthDiggingCompleteEvent : DoAfterEvent +{ + public NetCoordinates Coordinates { get; set; } + public NetEntity Shovel; + public override DoAfterEvent Clone() + { + return this; + } +} + +[Serializable, NetSerializable] +public sealed class EarthDiggingCancelledEvent : EntityEventArgs +{ + public NetEntity Shovel; +} diff --git a/Content.Server/Nyanotrasen/Tools/Components/EarthDiggingComponent.cs b/Content.Shared/Nyanotrasen/Digging/EarthDiggingComponent.cs similarity index 74% rename from Content.Server/Nyanotrasen/Tools/Components/EarthDiggingComponent.cs rename to Content.Shared/Nyanotrasen/Digging/EarthDiggingComponent.cs index 021baaf8f7ea..65b6a1b6bf3e 100644 --- a/Content.Server/Nyanotrasen/Tools/Components/EarthDiggingComponent.cs +++ b/Content.Shared/Nyanotrasen/Digging/EarthDiggingComponent.cs @@ -1,8 +1,8 @@ -using System.Threading; -using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; +using System.Threading; using Content.Shared.Tools; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; -namespace Content.Server.Tools.Components; +namespace Content.Shared.Nyanotrasen.Digging; [RegisterComponent] public sealed partial class EarthDiggingComponent : Component @@ -19,8 +19,4 @@ public sealed partial class EarthDiggingComponent : Component [DataField("delay")] public float Delay = 2f; - /// - /// Used for do_afters. - /// - public CancellationTokenSource? CancelToken = null; } diff --git a/Content.Shared/Nyanotrasen/Digging/SharedDiggingSystem.cs b/Content.Shared/Nyanotrasen/Digging/SharedDiggingSystem.cs new file mode 100644 index 000000000000..111ffd50e59f --- /dev/null +++ b/Content.Shared/Nyanotrasen/Digging/SharedDiggingSystem.cs @@ -0,0 +1,115 @@ +using System.Threading; +using Content.Shared.Interaction; +using Content.Shared.Maps; +using Content.Shared.Nyanotrasen.Digging; +using Content.Shared.Physics; +using Content.Shared.Tools.Components; +using Content.Shared.Tools.Systems; +using Robust.Shared.Map; +using Robust.Shared.Map.Components; + +namespace Content.Shared.Digging; + +public sealed class SharedDiggingSystem : EntitySystem +{ + [Dependency] private readonly TileSystem _tiles = default!; + [Dependency] private readonly SharedMapSystem _maps = default!; + [Dependency] private readonly SharedToolSystem _tools = default!; + [Dependency] private readonly TurfSystem _turfs = default!; + [Dependency] private readonly IMapManager _mapManager = default!; + [Dependency] private readonly ITileDefinitionManager _tileDefManager = default!; + [Dependency] private readonly SharedInteractionSystem _interactionSystem = default!; + // [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!; + + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnDiggingAfterInteract); + SubscribeLocalEvent(OnEarthDigComplete); + } + + + + private void OnEarthDigComplete(EntityUid shovel, EarthDiggingComponent comp, EarthDiggingCompleteEvent args) + { + var coordinates = GetCoordinates(args.Coordinates); + if (!TryComp(shovel, out var component)) + return; + + + var gridUid = coordinates.GetGridUid(EntityManager); + if (gridUid == null) + return; + + var grid = Comp(gridUid.Value); + var tile = _maps.GetTileRef(gridUid.Value, grid, coordinates); + + if (_tileDefManager[tile.Tile.TypeId] is not ContentTileDefinition tileDef + || !tileDef.CanShovel + // || tileDef.BaseTurfs.Count == 0 + || _turfs.IsTileBlocked(tile, CollisionGroup.MobMask)) + { + return; + } + + _tiles.DigTile(tile); + } + + private void OnDiggingAfterInteract(EntityUid uid, EarthDiggingComponent component, + AfterInteractEvent args) + { + if (args.Handled || !args.CanReach || args.Target != null) + return; + + if (TryDig(args.User, uid, component, args.ClickLocation)) + args.Handled = true; + } + + private bool TryDig(EntityUid user, EntityUid shovel, EarthDiggingComponent component, + EntityCoordinates clickLocation) + { + ToolComponent? tool = null; + if (component.ToolComponentNeeded && !TryComp(shovel, out tool)) + return false; + + if (!_mapManager.TryGetGrid(clickLocation.GetGridUid(EntityManager), out var mapGrid)) + return false; + + var tile = mapGrid.GetTileRef(clickLocation); + + var coordinates = mapGrid.GridTileToLocal(tile.GridIndices); + + if (!_interactionSystem.InRangeUnobstructed(user, coordinates, popup: false)) + return false; + + if (_tileDefManager[tile.Tile.TypeId] is not ContentTileDefinition tileDef + || !tileDef.CanShovel + || tileDef.BaseTurf.Length == 0 + // || _tileDefinitionManager[tileDef.BaseTurfs[^1]] is not ContentTileDefinition newDef + || tile.IsBlockedTurf(true)) + { + return false; + } + + return _tools.UseTool( + shovel, + user, + // FIXME + target: shovel, + doAfterDelay: component.Delay, + toolQualitiesNeeded: new[] { component.QualityNeeded }, + doAfterEv: new EarthDiggingCompleteEvent + { + Coordinates = GetNetCoordinates(clickLocation), + Shovel = GetNetEntity(shovel) + }, + toolComponent: tool + ); + } + +} + + +