Files
crystall-punk-14/Content.Shared/Tools/Systems/SharedToolSystem.cs

304 lines
12 KiB
C#
Raw Normal View History

2023-10-24 00:20:33 +11:00
using Content.Shared.Administration.Logs;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.DoAfter;
2023-10-24 00:20:33 +11:00
using Content.Shared.Interaction;
using Content.Shared.Item.ItemToggle;
2023-10-24 00:20:33 +11:00
using Content.Shared.Maps;
using Content.Shared.Popups;
using Content.Shared.Tools.Components;
using JetBrains.Annotations;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
2023-10-24 00:20:33 +11:00
namespace Content.Shared.Tools.Systems;
public abstract partial class SharedToolSystem : EntitySystem
{
2023-10-24 00:20:33 +11:00
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
2023-10-24 00:20:33 +11:00
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
[Dependency] protected readonly SharedInteractionSystem InteractionSystem = default!;
item toggling giga rework + full ninja refactor (#28039) * item toggle refactoring and some new systems * add ToggleClothing component/system * unhardcode magboots gravity logic * make magboots and speedboots use ItemToggle and stuff * remove now useless clothing components * update client/server magboots systems * add note to use ItemToggledEvent in ToggleActionEvent doc * refactor PowerCellDraw to use ItemToggle for ui open/close control * add TryUseCharges, refactor charges system * update magboot trigger code * make borg use ItemToggle, network SelectedModule instead of now removed Activated * add AccessToggle for borg * the giga ninja refactor * update ninja yml * update ItemToggle usage for some stuff * fix activatableui requires power * random fixing * yaml fixing * nuke ItemToggleDisarmMalus * make defib use ItemToggle * make things that use power not turn on if missing use charge * pro * fix sound prediction * bruh * proximity detector use ItemToggle * oop * big idiot syndrome * fix ninja spawn rule and make it generic * fix ninja spawn rule yml * move loading profiles into AntagLoadProfileRule * more ninja refactor * ninja yml fixes * the dreaded copy paste ops * remove useless NinjaRuleComponent and ue AntagSelection for greeting * fix invisibility * move IsCompleted to SharedObjectivesSystem * ability fixes * oop fix powercell instantly draining itself * sentient speedboots gaming * make reflect use ItemToggle * fix other test * loadprofilerule moved into its own pr * remove conflict with dragon refactor * remove all GenericAntag code from ninja * ) * probably * remove old enabled * great language bravo vince * GREAT LANGUAGE * who made this language * because it stinks * reparent blood-red magboots to magboots probbbly works * most of the review stuff * hasGrav doesnt mean what i thought it did * make health analyzer use itemtoggle, not fail test * fix mag/speed boots being wacky * UNTROLL * add ItemToggle to the random health analyzers * a * remove unused obsolete borg func * untrolling * :trollface: * fix test * fix * g * untroll --------- Co-authored-by: deltanedas <@deltanedas:kde.org>
2024-07-11 05:55:56 +00:00
[Dependency] protected readonly ItemToggleSystem ItemToggle = default!;
2023-10-24 00:20:33 +11:00
[Dependency] private readonly SharedMapSystem _maps = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] protected readonly SharedSolutionContainerSystem SolutionContainerSystem = default!;
2023-10-24 00:20:33 +11:00
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly TileSystem _tiles = default!;
[Dependency] private readonly TurfSystem _turfs = default!;
THE RETURN OF ITEM STATUS (#22986) * THE RETURN OF ITEM STATUS Item status is now inline with the hands again. You can now see item status for both hands at once. If you have more than 2 hands, the last active hand of that side is displayed in the respective item status. The item status for the active hand is also highlighted. Item status has been given a new look so it looks unique and matches every UI theme. * Shrink item status to 125px This is going to require fixing the existing controls. Do that later. * New bullet item status rendering sex * Make gun item status look just a little bit nicer. Avoid only one or two bullets ending up on a single row of an item status. * Delete Eris theme files * More improvements Fixed the fact that left/right were flipped around when assigning status panel locations. Involved renaming all the UI textures. Redid how content margins are set from the theme. Less complex and cleaner now. Made the item name always left-aligned, just looks better since other UI elements don't adapt anyways. * Compact down item status text Now it fits 3 lines of text on one line. Yay. This is achieved by compacting RichTextLabels by reducing their line height and giving them a negative bottom margin. * Add item status sprites for Ashen theme. * Add status control to show beaker/bucket/jug solution/transfer volumes Also PollingItemStatusControl I'll be using that more. * Fix welder item status, clean up welder code The item status control implementation was ancient and bad. That's why it was buggy. Removed all the complex dated networking stuff for welders, we just sync the solution contents now anyways so none of that is needed anymore. This moves a buncha stuff to shared and just removes code. Cleanup. The code was doing some really dumb stuff. * Spray bottles show contents in item status. * cowtools * Fix plasmafire and clockwork themes. Actual git gaslighting wtf. --------- Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2024-04-21 15:16:23 +02:00
[Dependency] protected readonly SharedSolutionContainerSystem SolutionContainer = default!;
public override void Initialize()
{
InitializeMultipleTool();
InitializeTile();
InitializeWelder();
SubscribeLocalEvent<ToolComponent, ToolDoAfterEvent>(OnDoAfter);
}
private void OnDoAfter(EntityUid uid, ToolComponent tool, ToolDoAfterEvent args)
{
if (!args.Cancelled)
PlayToolSound(uid, tool, args.User);
var ev = args.WrappedEvent;
ev.DoAfter = args.DoAfter;
if (args.OriginalTarget != null)
RaiseLocalEvent(GetEntity(args.OriginalTarget.Value), (object) ev);
else
RaiseLocalEvent((object) ev);
}
public void PlayToolSound(EntityUid uid, ToolComponent tool, EntityUid? user)
{
if (tool.UseSound == null)
return;
_audioSystem.PlayPredicted(tool.UseSound, uid, user);
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
/// <param name="toolQualitiesNeeded">The qualities 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="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
float doAfterDelay,
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
float fuel = 0,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
user,
target,
TimeSpan.FromSeconds(doAfterDelay),
toolQualitiesNeeded,
doAfterEv,
out _,
fuel,
toolComponent);
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="delay">The base tool use delay. This will be modified by the tool's quality</param>
/// <param name="toolQualitiesNeeded">The qualities 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="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
TimeSpan delay,
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
out DoAfterId? id,
float fuel = 0,
ToolComponent? toolComponent = null)
{
id = null;
if (!Resolve(tool, ref toolComponent, false))
return false;
if (!CanStartToolUse(tool, user, target, fuel, toolQualitiesNeeded, toolComponent))
return false;
var toolEvent = new ToolDoAfterEvent(fuel, doAfterEv, GetNetEntity(target));
var doAfterArgs = new DoAfterArgs(EntityManager, user, delay / toolComponent.SpeedModifier, toolEvent, tool, target: target, used: tool)
{
BreakOnDamage = true,
BreakOnMove = true,
BreakOnWeightlessMove = false,
2023-04-21 15:05:29 +10:00
NeedHand = tool != user,
AttemptFrequency = fuel > 0 ? AttemptFrequency.EveryTick : AttemptFrequency.Never
};
_doAfterSystem.TryStartDoAfter(doAfterArgs, out id);
return true;
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
/// <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="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
float doAfterDelay,
string toolQualityNeeded,
DoAfterEvent doAfterEv,
float fuel = 0,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
user,
target,
TimeSpan.FromSeconds(doAfterDelay),
new[] { toolQualityNeeded },
doAfterEv,
out _,
fuel,
toolComponent);
}
/// <summary>
/// Whether a tool entity has the specified quality or not.
/// </summary>
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
}
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
[PublicAPI]
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
private bool CanStartToolUse(EntityUid tool, EntityUid user, EntityUid? target, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
// check if the tool can do what's required
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
return false;
// check if the user allows using the tool
var ev = new ToolUserAttemptUseEvent(target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
// check if the tool allows being used
2024-06-02 22:28:53 -05:00
var beforeAttempt = new ToolUseAttemptEvent(user, fuel);
RaiseLocalEvent(tool, beforeAttempt);
if (beforeAttempt.Cancelled)
return false;
// check if the target allows using the tool
if (target != null && target != tool)
{
RaiseLocalEvent(target.Value, beforeAttempt);
}
return !beforeAttempt.Cancelled;
}
#region DoAfterEvents
[Serializable, NetSerializable]
protected sealed partial class ToolDoAfterEvent : DoAfterEvent
{
[DataField]
public float Fuel;
/// <summary>
/// Entity that the wrapped do after event will get directed at. If null, event will be broadcast.
/// </summary>
[DataField("target")]
public NetEntity? OriginalTarget;
[DataField("wrappedEvent")]
public DoAfterEvent WrappedEvent = default!;
private ToolDoAfterEvent()
{
}
public ToolDoAfterEvent(float fuel, DoAfterEvent wrappedEvent, NetEntity? originalTarget)
{
DebugTools.Assert(wrappedEvent.GetType().HasCustomAttribute<NetSerializableAttribute>(), "Tool event is not serializable");
Fuel = fuel;
WrappedEvent = wrappedEvent;
OriginalTarget = originalTarget;
}
public override DoAfterEvent Clone()
{
var evClone = WrappedEvent.Clone();
// Most DoAfter events are immutable
if (evClone == WrappedEvent)
return this;
return new ToolDoAfterEvent(Fuel, evClone, OriginalTarget);
}
public override bool IsDuplicate(DoAfterEvent other)
{
return other is ToolDoAfterEvent toolDoAfter && WrappedEvent.IsDuplicate(toolDoAfter.WrappedEvent);
}
}
[Serializable, NetSerializable]
protected sealed partial class LatticeCuttingCompleteEvent : DoAfterEvent
{
[DataField(required:true)]
public NetCoordinates Coordinates;
private LatticeCuttingCompleteEvent()
{
}
public LatticeCuttingCompleteEvent(NetCoordinates coordinates)
{
Coordinates = coordinates;
}
public override DoAfterEvent Clone() => this;
}
}
[Serializable, NetSerializable]
public sealed partial class CableCuttingFinishedEvent : SimpleDoAfterEvent;
#endregion