2021-06-09 22:19:39 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Content.Server.Stack;
|
2021-10-01 15:09:01 +02:00
|
|
|
using Content.Shared.ActionBlocker;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
2021-10-01 15:09:01 +02:00
|
|
|
using Content.Shared.Maps;
|
2019-09-19 11:47:45 -07:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Map;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2019-09-19 11:47:45 -07:00
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
namespace Content.Server.Power.Components
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-07-04 18:11:52 +02:00
|
|
|
internal class CablePlacerComponent : Component, IAfterInteract
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2019-09-19 11:47:45 -07:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-07-04 18:11:52 +02:00
|
|
|
public override string Name => "CablePlacer";
|
2019-09-19 11:47:45 -07:00
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
[ViewVariables]
|
2021-07-04 18:11:52 +02:00
|
|
|
[DataField("cablePrototypeID")]
|
|
|
|
|
private string? _cablePrototypeID = "CableHV";
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("blockingWireType")]
|
2021-07-04 18:11:52 +02:00
|
|
|
private CableType _blockingCableType = CableType.HighVoltage;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2019-09-19 11:47:45 -07:00
|
|
|
/// <inheritdoc />
|
2021-02-04 17:50:28 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2021-11-09 14:54:00 +01:00
|
|
|
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(eventArgs.User.Uid))
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
if (_cablePrototypeID == null)
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
2021-02-03 14:05:31 +01:00
|
|
|
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true))
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
2020-11-18 15:45:53 +01:00
|
|
|
if(!_mapManager.TryGetGrid(eventArgs.ClickLocation.GetGridId(Owner.EntityManager), out var grid))
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
var snapPos = grid.TileIndicesFor(eventArgs.ClickLocation);
|
2021-10-01 15:09:01 +02:00
|
|
|
var tileDef = grid.GetTileRef(snapPos).Tile.GetContentTileDefinition();
|
|
|
|
|
|
2021-10-23 19:32:33 -05:00
|
|
|
if(!tileDef.IsSubFloor || !tileDef.Sturdy)
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
foreach (var anchored in grid.GetAnchoredEntities(snapPos))
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (Owner.EntityManager.TryGetComponent<CableComponent>(anchored, out var wire) && wire.CableType == _blockingCableType)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
2019-09-19 11:47:45 -07:00
|
|
|
}
|
2021-05-26 10:20:57 +02:00
|
|
|
|
2021-06-12 11:24:34 +02:00
|
|
|
if (Owner.TryGetComponent<StackComponent>(out var stack)
|
2021-09-20 13:39:05 +02:00
|
|
|
&& !EntitySystem.Get<StackSystem>().Use(Owner.Uid, 1, stack))
|
2021-10-01 15:09:01 +02:00
|
|
|
return false;
|
2021-05-26 10:20:57 +02:00
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
Owner.EntityManager.SpawnEntity(_cablePrototypeID, grid.GridTileToLocal(snapPos));
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2019-09-19 11:47:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|