SharedToolSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using Content.Shared.Administration.Logs;
  2. using Content.Shared.Chemistry.EntitySystems;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.Examine;
  5. using Content.Shared.Interaction;
  6. using Content.Shared.Item.ItemToggle;
  7. using Content.Shared.Maps;
  8. using Content.Shared.Popups;
  9. using Content.Shared.Tools.Components;
  10. using JetBrains.Annotations;
  11. using Robust.Shared.Audio.Systems;
  12. using Robust.Shared.Map;
  13. using Robust.Shared.Prototypes;
  14. using Robust.Shared.Serialization;
  15. using Robust.Shared.Utility;
  16. namespace Content.Shared.Tools.Systems;
  17. public abstract partial class SharedToolSystem : EntitySystem
  18. {
  19. [Dependency] private readonly IMapManager _mapManager = default!;
  20. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  21. [Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
  22. [Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
  23. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  24. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  25. [Dependency] protected readonly SharedInteractionSystem InteractionSystem = default!;
  26. [Dependency] protected readonly ItemToggleSystem ItemToggle = default!;
  27. [Dependency] private readonly SharedMapSystem _maps = default!;
  28. [Dependency] private readonly SharedPopupSystem _popup = default!;
  29. [Dependency] protected readonly SharedSolutionContainerSystem SolutionContainerSystem = default!;
  30. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  31. [Dependency] private readonly TileSystem _tiles = default!;
  32. [Dependency] private readonly TurfSystem _turfs = default!;
  33. public const string CutQuality = "Cutting";
  34. public const string PulseQuality = "Pulsing";
  35. public override void Initialize()
  36. {
  37. InitializeMultipleTool();
  38. InitializeTile();
  39. InitializeWelder();
  40. SubscribeLocalEvent<ToolComponent, ToolDoAfterEvent>(OnDoAfter);
  41. SubscribeLocalEvent<ToolComponent, ExaminedEvent>(OnExamine);
  42. }
  43. private void OnDoAfter(EntityUid uid, ToolComponent tool, ToolDoAfterEvent args)
  44. {
  45. if (!args.Cancelled)
  46. PlayToolSound(uid, tool, args.User);
  47. var ev = args.WrappedEvent;
  48. ev.DoAfter = args.DoAfter;
  49. if (args.OriginalTarget != null)
  50. RaiseLocalEvent(GetEntity(args.OriginalTarget.Value), (object) ev);
  51. else
  52. RaiseLocalEvent((object) ev);
  53. }
  54. private void OnExamine(Entity<ToolComponent> ent, ref ExaminedEvent args)
  55. {
  56. // If the tool has no qualities, exit early
  57. if (ent.Comp.Qualities.Count == 0)
  58. return;
  59. var message = new FormattedMessage();
  60. // Create a list to store tool quality names
  61. var toolQualities = new List<string>();
  62. // Loop through tool qualities and add localized names to the list
  63. foreach (var toolQuality in ent.Comp.Qualities)
  64. {
  65. if (_protoMan.TryIndex<ToolQualityPrototype>(toolQuality ?? string.Empty, out var protoToolQuality))
  66. {
  67. toolQualities.Add(Loc.GetString(protoToolQuality.Name));
  68. }
  69. }
  70. // Combine the qualities into a single string and localize the final message
  71. var qualitiesString = string.Join(", ", toolQualities);
  72. // Add the localized message to the FormattedMessage object
  73. message.AddMarkupPermissive(Loc.GetString("tool-component-qualities", ("qualities", qualitiesString)));
  74. args.PushMessage(message);
  75. }
  76. public void PlayToolSound(EntityUid uid, ToolComponent tool, EntityUid? user)
  77. {
  78. if (tool.UseSound == null)
  79. return;
  80. _audioSystem.PlayPredicted(tool.UseSound, uid, user);
  81. }
  82. /// <summary>
  83. /// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
  84. /// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
  85. /// </summary>
  86. /// <param name="tool">The tool to use</param>
  87. /// <param name="user">The entity using the tool</param>
  88. /// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
  89. /// event. If null, the event will be broadcast</param>
  90. /// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
  91. /// <param name="toolQualitiesNeeded">The qualities needed for this tool to work.</param>
  92. /// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
  93. /// will be directed at the tool target.</param>
  94. /// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
  95. /// <param name="toolComponent">The tool component.</param>
  96. /// <returns>Returns true if any interaction takes place.</returns>
  97. public bool UseTool(
  98. EntityUid tool,
  99. EntityUid user,
  100. EntityUid? target,
  101. float doAfterDelay,
  102. IEnumerable<string> toolQualitiesNeeded,
  103. DoAfterEvent doAfterEv,
  104. float fuel = 0,
  105. ToolComponent? toolComponent = null)
  106. {
  107. return UseTool(tool,
  108. user,
  109. target,
  110. TimeSpan.FromSeconds(doAfterDelay),
  111. toolQualitiesNeeded,
  112. doAfterEv,
  113. out _,
  114. fuel,
  115. toolComponent);
  116. }
  117. /// <summary>
  118. /// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
  119. /// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
  120. /// </summary>
  121. /// <param name="tool">The tool to use</param>
  122. /// <param name="user">The entity using the tool</param>
  123. /// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
  124. /// event. If null, the event will be broadcast</param>
  125. /// <param name="delay">The base tool use delay. This will be modified by the tool's quality</param>
  126. /// <param name="toolQualitiesNeeded">The qualities needed for this tool to work.</param>
  127. /// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
  128. /// will be directed at the tool target.</param>
  129. /// <param name="id">The id of the DoAfter that was created. This may be null even if the function returns true in
  130. /// the event that this tool-use cancelled an existing DoAfter</param>
  131. /// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
  132. /// <param name="toolComponent">The tool component.</param>
  133. /// <returns>Returns true if any interaction takes place.</returns>
  134. public bool UseTool(
  135. EntityUid tool,
  136. EntityUid user,
  137. EntityUid? target,
  138. TimeSpan delay,
  139. IEnumerable<string> toolQualitiesNeeded,
  140. DoAfterEvent doAfterEv,
  141. out DoAfterId? id,
  142. float fuel = 0,
  143. ToolComponent? toolComponent = null)
  144. {
  145. id = null;
  146. if (!Resolve(tool, ref toolComponent, false))
  147. return false;
  148. if (!CanStartToolUse(tool, user, target, fuel, toolQualitiesNeeded, toolComponent))
  149. return false;
  150. var toolEvent = new ToolDoAfterEvent(fuel, doAfterEv, GetNetEntity(target));
  151. var doAfterArgs = new DoAfterArgs(EntityManager, user, delay / toolComponent.SpeedModifier, toolEvent, tool, target: target, used: tool)
  152. {
  153. BreakOnDamage = true,
  154. BreakOnMove = true,
  155. BreakOnWeightlessMove = false,
  156. NeedHand = tool != user,
  157. AttemptFrequency = fuel > 0 ? AttemptFrequency.EveryTick : AttemptFrequency.Never
  158. };
  159. _doAfterSystem.TryStartDoAfter(doAfterArgs, out id);
  160. return true;
  161. }
  162. /// <summary>
  163. /// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
  164. /// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
  165. /// </summary>
  166. /// <param name="tool">The tool to use</param>
  167. /// <param name="user">The entity using the tool</param>
  168. /// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
  169. /// event. If null, the event will be broadcast</param>
  170. /// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
  171. /// <param name="toolQualityNeeded">The quality needed for this tool to work.</param>
  172. /// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
  173. /// will be directed at the tool target.</param>
  174. /// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
  175. /// <param name="toolComponent">The tool component.</param>
  176. /// <returns>Returns true if any interaction takes place.</returns>
  177. public bool UseTool(
  178. EntityUid tool,
  179. EntityUid user,
  180. EntityUid? target,
  181. float doAfterDelay,
  182. string toolQualityNeeded,
  183. DoAfterEvent doAfterEv,
  184. float fuel = 0,
  185. ToolComponent? toolComponent = null)
  186. {
  187. return UseTool(tool,
  188. user,
  189. target,
  190. TimeSpan.FromSeconds(doAfterDelay),
  191. new[] { toolQualityNeeded },
  192. doAfterEv,
  193. out _,
  194. fuel,
  195. toolComponent);
  196. }
  197. /// <summary>
  198. /// Whether a tool entity has the specified quality or not.
  199. /// </summary>
  200. public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
  201. {
  202. return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
  203. }
  204. /// <summary>
  205. /// Whether a tool entity has all specified qualities or not.
  206. /// </summary>
  207. [PublicAPI]
  208. public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
  209. {
  210. return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
  211. }
  212. private bool CanStartToolUse(EntityUid tool, EntityUid user, EntityUid? target, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
  213. {
  214. if (!Resolve(tool, ref toolComponent))
  215. return false;
  216. // check if the tool can do what's required
  217. if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
  218. return false;
  219. // check if the user allows using the tool
  220. var ev = new ToolUserAttemptUseEvent(target);
  221. RaiseLocalEvent(user, ref ev);
  222. if (ev.Cancelled)
  223. return false;
  224. // check if the tool allows being used
  225. var beforeAttempt = new ToolUseAttemptEvent(user, fuel);
  226. RaiseLocalEvent(tool, beforeAttempt);
  227. if (beforeAttempt.Cancelled)
  228. return false;
  229. // check if the target allows using the tool
  230. if (target != null && target != tool)
  231. {
  232. RaiseLocalEvent(target.Value, beforeAttempt);
  233. }
  234. return !beforeAttempt.Cancelled;
  235. }
  236. #region DoAfterEvents
  237. [Serializable, NetSerializable]
  238. protected sealed partial class ToolDoAfterEvent : DoAfterEvent
  239. {
  240. [DataField]
  241. public float Fuel;
  242. /// <summary>
  243. /// Entity that the wrapped do after event will get directed at. If null, event will be broadcast.
  244. /// </summary>
  245. [DataField("target")]
  246. public NetEntity? OriginalTarget;
  247. [DataField("wrappedEvent")]
  248. public DoAfterEvent WrappedEvent = default!;
  249. private ToolDoAfterEvent()
  250. {
  251. }
  252. public ToolDoAfterEvent(float fuel, DoAfterEvent wrappedEvent, NetEntity? originalTarget)
  253. {
  254. DebugTools.Assert(wrappedEvent.GetType().HasCustomAttribute<NetSerializableAttribute>(), "Tool event is not serializable");
  255. Fuel = fuel;
  256. WrappedEvent = wrappedEvent;
  257. OriginalTarget = originalTarget;
  258. }
  259. public override DoAfterEvent Clone()
  260. {
  261. var evClone = WrappedEvent.Clone();
  262. // Most DoAfter events are immutable
  263. if (evClone == WrappedEvent)
  264. return this;
  265. return new ToolDoAfterEvent(Fuel, evClone, OriginalTarget);
  266. }
  267. public override bool IsDuplicate(DoAfterEvent other)
  268. {
  269. return other is ToolDoAfterEvent toolDoAfter && WrappedEvent.IsDuplicate(toolDoAfter.WrappedEvent);
  270. }
  271. }
  272. [Serializable, NetSerializable]
  273. protected sealed partial class LatticeCuttingCompleteEvent : DoAfterEvent
  274. {
  275. [DataField(required:true)]
  276. public NetCoordinates Coordinates;
  277. private LatticeCuttingCompleteEvent()
  278. {
  279. }
  280. public LatticeCuttingCompleteEvent(NetCoordinates coordinates)
  281. {
  282. Coordinates = coordinates;
  283. }
  284. public override DoAfterEvent Clone() => this;
  285. }
  286. }
  287. [Serializable, NetSerializable]
  288. public sealed partial class CableCuttingFinishedEvent : SimpleDoAfterEvent;
  289. #endregion