Skip to content

Commit

Permalink
Moved to shared, fixed some problems with code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygg01 committed Nov 5, 2023
1 parent ebd1ee7 commit c120edc
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 163 deletions.
155 changes: 0 additions & 155 deletions Content.Server/Nyanotrasen/Tools/ToolSystem.EarthDigging.cs

This file was deleted.

1 change: 0 additions & 1 deletion Content.Server/Tools/ToolSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public override void Initialize()
base.Initialize();

InitializeLatticeCutting();
InitializeEarthDigging(); // Delta V
InitializeWelders();
}

Expand Down
23 changes: 23 additions & 0 deletions Content.Shared/Nyanotrasen/Digging/DiggingEvents.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,8 +19,4 @@ public sealed partial class EarthDiggingComponent : Component
[DataField("delay")]
public float Delay = 2f;

/// <summary>
/// Used for do_afters.
/// </summary>
public CancellationTokenSource? CancelToken = null;
}
115 changes: 115 additions & 0 deletions Content.Shared/Nyanotrasen/Digging/SharedDiggingSystem.cs
Original file line number Diff line number Diff line change
@@ -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<EarthDiggingComponent, AfterInteractEvent>(OnDiggingAfterInteract);
SubscribeLocalEvent<EarthDiggingComponent, EarthDiggingCompleteEvent>(OnEarthDigComplete);
}



private void OnEarthDigComplete(EntityUid shovel, EarthDiggingComponent comp, EarthDiggingCompleteEvent args)
{
var coordinates = GetCoordinates(args.Coordinates);
if (!TryComp<EarthDiggingComponent>(shovel, out var component))
return;


var gridUid = coordinates.GetGridUid(EntityManager);
if (gridUid == null)
return;

var grid = Comp<MapGridComponent>(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
);
}

}



0 comments on commit c120edc

Please sign in to comment.