SharedWieldableSystem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System.Linq;
  2. using Content.Shared.Camera;
  3. using Content.Shared.Examine;
  4. using Content.Shared.Hands;
  5. using Content.Shared.Hands.Components;
  6. using Content.Shared.Hands.EntitySystems;
  7. using Content.Shared.IdentityManagement;
  8. using Content.Shared.Interaction.Events;
  9. using Content.Shared.Inventory.VirtualItem;
  10. using Content.Shared.Item;
  11. using Content.Shared.Movement.Components;
  12. using Content.Shared.Movement.Systems;
  13. using Content.Shared.Popups;
  14. using Content.Shared.Timing;
  15. using Content.Shared.Verbs;
  16. using Content.Shared.Weapons.Melee;
  17. using Content.Shared.Weapons.Melee.Components;
  18. using Content.Shared.Weapons.Melee.Events;
  19. using Content.Shared.Weapons.Ranged.Components;
  20. using Content.Shared.Weapons.Ranged.Events;
  21. using Content.Shared.Weapons.Ranged.Systems;
  22. using Content.Shared.Wieldable.Components;
  23. using Robust.Shared.Audio.Systems;
  24. using Robust.Shared.Network;
  25. using Robust.Shared.Timing;
  26. namespace Content.Shared.Wieldable;
  27. public abstract class SharedWieldableSystem : EntitySystem
  28. {
  29. [Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!;
  30. [Dependency] private readonly IGameTiming _timing = default!;
  31. [Dependency] private readonly INetManager _netManager = default!;
  32. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  33. [Dependency] private readonly SharedAudioSystem _audio = default!;
  34. [Dependency] private readonly SharedGunSystem _gun = default!;
  35. [Dependency] private readonly SharedHandsSystem _hands = default!;
  36. [Dependency] private readonly SharedItemSystem _item = default!;
  37. [Dependency] private readonly SharedPopupSystem _popup = default!;
  38. [Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
  39. [Dependency] private readonly UseDelaySystem _delay = default!;
  40. public override void Initialize()
  41. {
  42. base.Initialize();
  43. SubscribeLocalEvent<WieldableComponent, UseInHandEvent>(OnUseInHand, before: [typeof(SharedGunSystem)]);
  44. SubscribeLocalEvent<WieldableComponent, ItemUnwieldedEvent>(OnItemUnwielded);
  45. SubscribeLocalEvent<WieldableComponent, GotUnequippedHandEvent>(OnItemLeaveHand);
  46. SubscribeLocalEvent<WieldableComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
  47. SubscribeLocalEvent<WieldableComponent, GetVerbsEvent<InteractionVerb>>(AddToggleWieldVerb);
  48. SubscribeLocalEvent<WieldableComponent, HandDeselectedEvent>(OnDeselectWieldable);
  49. SubscribeLocalEvent<MeleeRequiresWieldComponent, AttemptMeleeEvent>(OnMeleeAttempt);
  50. SubscribeLocalEvent<GunRequiresWieldComponent, ExaminedEvent>(OnExamineRequires);
  51. SubscribeLocalEvent<GunRequiresWieldComponent, ShotAttemptedEvent>(OnShootAttempt);
  52. SubscribeLocalEvent<GunWieldBonusComponent, ItemWieldedEvent>(OnGunWielded);
  53. SubscribeLocalEvent<GunWieldBonusComponent, ItemUnwieldedEvent>(OnGunUnwielded);
  54. SubscribeLocalEvent<GunWieldBonusComponent, GunRefreshModifiersEvent>(OnGunRefreshModifiers);
  55. SubscribeLocalEvent<GunWieldBonusComponent, ExaminedEvent>(OnExamine);
  56. SubscribeLocalEvent<SpeedModifiedOnWieldComponent, ItemWieldedEvent>(OnSpeedModifierWielded);
  57. SubscribeLocalEvent<SpeedModifiedOnWieldComponent, ItemUnwieldedEvent>(OnSpeedModifierUnwielded);
  58. SubscribeLocalEvent<SpeedModifiedOnWieldComponent, HeldRelayedEvent<RefreshMovementSpeedModifiersEvent>>(OnRefreshSpeedWielded);
  59. SubscribeLocalEvent<IncreaseDamageOnWieldComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
  60. }
  61. private void OnMeleeAttempt(EntityUid uid, MeleeRequiresWieldComponent component, ref AttemptMeleeEvent args)
  62. {
  63. if (TryComp<WieldableComponent>(uid, out var wieldable) &&
  64. !wieldable.Wielded)
  65. {
  66. args.Cancelled = true;
  67. args.Message = Loc.GetString("wieldable-component-requires", ("item", uid));
  68. }
  69. }
  70. private void OnShootAttempt(EntityUid uid, GunRequiresWieldComponent component, ref ShotAttemptedEvent args)
  71. {
  72. if (TryComp<WieldableComponent>(uid, out var wieldable) &&
  73. !wieldable.Wielded)
  74. {
  75. args.Cancel();
  76. var time = _timing.CurTime;
  77. if (time > component.LastPopup + component.PopupCooldown &&
  78. !HasComp<MeleeWeaponComponent>(uid) &&
  79. !HasComp<MeleeRequiresWieldComponent>(uid))
  80. {
  81. component.LastPopup = time;
  82. var message = Loc.GetString("wieldable-component-requires", ("item", uid));
  83. _popup.PopupClient(message, args.Used, args.User);
  84. }
  85. }
  86. }
  87. private void OnGunUnwielded(EntityUid uid, GunWieldBonusComponent component, ItemUnwieldedEvent args)
  88. {
  89. _gun.RefreshModifiers(uid);
  90. }
  91. private void OnGunWielded(EntityUid uid, GunWieldBonusComponent component, ref ItemWieldedEvent args)
  92. {
  93. _gun.RefreshModifiers(uid);
  94. }
  95. private void OnDeselectWieldable(EntityUid uid, WieldableComponent component, HandDeselectedEvent args)
  96. {
  97. if (_hands.EnumerateHands(args.User).Count() > 2)
  98. return;
  99. TryUnwield(uid, component, args.User);
  100. }
  101. private void OnGunRefreshModifiers(Entity<GunWieldBonusComponent> bonus, ref GunRefreshModifiersEvent args)
  102. {
  103. if (TryComp(bonus, out WieldableComponent? wield) &&
  104. wield.Wielded)
  105. {
  106. args.MinAngle += bonus.Comp.MinAngle;
  107. args.MaxAngle += bonus.Comp.MaxAngle;
  108. args.AngleDecay += bonus.Comp.AngleDecay;
  109. args.AngleIncrease += bonus.Comp.AngleIncrease;
  110. }
  111. }
  112. private void OnSpeedModifierWielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ItemWieldedEvent args)
  113. {
  114. _movementSpeedModifier.RefreshMovementSpeedModifiers(args.User);
  115. }
  116. private void OnSpeedModifierUnwielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ItemUnwieldedEvent args)
  117. {
  118. _movementSpeedModifier.RefreshMovementSpeedModifiers(args.User);
  119. }
  120. private void OnRefreshSpeedWielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ref HeldRelayedEvent<RefreshMovementSpeedModifiersEvent> args)
  121. {
  122. if (TryComp<WieldableComponent>(uid, out var wield) && wield.Wielded)
  123. {
  124. args.Args.ModifySpeed(component.WalkModifier, component.SprintModifier);
  125. }
  126. }
  127. private void OnExamineRequires(Entity<GunRequiresWieldComponent> entity, ref ExaminedEvent args)
  128. {
  129. if (entity.Comp.WieldRequiresExamineMessage != null)
  130. args.PushText(Loc.GetString(entity.Comp.WieldRequiresExamineMessage));
  131. }
  132. private void OnExamine(EntityUid uid, GunWieldBonusComponent component, ref ExaminedEvent args)
  133. {
  134. if (HasComp<GunRequiresWieldComponent>(uid))
  135. return;
  136. if (component.WieldBonusExamineMessage != null)
  137. args.PushText(Loc.GetString(component.WieldBonusExamineMessage));
  138. }
  139. private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<InteractionVerb> args)
  140. {
  141. if (args.Hands == null || !args.CanAccess || !args.CanInteract)
  142. return;
  143. if (!_hands.IsHolding(args.User, uid, out _, args.Hands))
  144. return;
  145. // TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
  146. // verb. Or just don't add it to the list if the action is not executable.
  147. // TODO VERBS ICON
  148. InteractionVerb verb = new()
  149. {
  150. Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"),
  151. Act = component.Wielded
  152. ? () => TryUnwield(uid, component, args.User)
  153. : () => TryWield(uid, component, args.User)
  154. };
  155. args.Verbs.Add(verb);
  156. }
  157. private void OnUseInHand(EntityUid uid, WieldableComponent component, UseInHandEvent args)
  158. {
  159. if (args.Handled)
  160. return;
  161. if (!component.Wielded)
  162. args.Handled = TryWield(uid, component, args.User);
  163. else if (component.UnwieldOnUse)
  164. args.Handled = TryUnwield(uid, component, args.User);
  165. if (HasComp<UseDelayComponent>(uid) && !component.UseDelayOnWield)
  166. args.ApplyDelay = false;
  167. }
  168. public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet = false)
  169. {
  170. // Do they have enough hands free?
  171. if (!TryComp<HandsComponent>(user, out var hands))
  172. {
  173. if (!quiet)
  174. _popup.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
  175. return false;
  176. }
  177. // Is it.. actually in one of their hands?
  178. if (!_hands.IsHolding(user, uid, out _, hands))
  179. {
  180. if (!quiet)
  181. _popup.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
  182. return false;
  183. }
  184. if (_hands.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
  185. {
  186. if (!quiet)
  187. {
  188. var message = Loc.GetString("wieldable-component-not-enough-free-hands",
  189. ("number", component.FreeHandsRequired), ("item", uid));
  190. _popup.PopupClient(message, user, user);
  191. }
  192. return false;
  193. }
  194. // Seems legit.
  195. return true;
  196. }
  197. /// <summary>
  198. /// Attempts to wield an item, starting a UseDelay after.
  199. /// </summary>
  200. /// <returns>True if the attempt wasn't blocked.</returns>
  201. public bool TryWield(EntityUid used, WieldableComponent component, EntityUid user)
  202. {
  203. if (!CanWield(used, component, user))
  204. return false;
  205. if (TryComp(used, out UseDelayComponent? useDelay) && component.UseDelayOnWield)
  206. {
  207. if (!_delay.TryResetDelay((used, useDelay), true))
  208. return false;
  209. }
  210. var attemptEv = new WieldAttemptEvent(user);
  211. RaiseLocalEvent(used, ref attemptEv);
  212. if (attemptEv.Cancelled)
  213. return false;
  214. if (TryComp<ItemComponent>(used, out var item))
  215. {
  216. component.OldInhandPrefix = item.HeldPrefix;
  217. _item.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
  218. }
  219. SetWielded((used, component), true);
  220. if (component.WieldSound != null)
  221. _audio.PlayPredicted(component.WieldSound, used, user);
  222. //This section handles spawning the virtual item(s) to occupy the required additional hand(s).
  223. //Since the client can't currently predict entity spawning, only do this if this is running serverside.
  224. //Remove this check if TrySpawnVirtualItem in SharedVirtualItemSystem is allowed to complete clientside.
  225. if (_netManager.IsServer)
  226. {
  227. var virtuals = new List<EntityUid>();
  228. for (var i = 0; i < component.FreeHandsRequired; i++)
  229. {
  230. if (_virtualItem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
  231. {
  232. virtuals.Add(virtualItem.Value);
  233. continue;
  234. }
  235. foreach (var existingVirtual in virtuals)
  236. {
  237. QueueDel(existingVirtual);
  238. }
  239. return false;
  240. }
  241. }
  242. var selfMessage = Loc.GetString("wieldable-component-successful-wield", ("item", used));
  243. var othersMessage = Loc.GetString("wieldable-component-successful-wield-other", ("user", Identity.Entity(user, EntityManager)), ("item", used));
  244. _popup.PopupPredicted(selfMessage, othersMessage, user, user);
  245. var ev = new ItemWieldedEvent(user);
  246. RaiseLocalEvent(used, ref ev);
  247. return true;
  248. }
  249. /// <summary>
  250. /// Attempts to unwield an item, with no use delay.
  251. /// </summary>
  252. /// <returns>True if the attempt wasn't blocked.</returns>
  253. public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user, bool force = false)
  254. {
  255. if (!component.Wielded)
  256. return false; // already unwielded
  257. if (!force)
  258. {
  259. var attemptEv = new UnwieldAttemptEvent(user);
  260. RaiseLocalEvent(used, ref attemptEv);
  261. if (attemptEv.Cancelled)
  262. return false;
  263. }
  264. SetWielded((used, component), false);
  265. var ev = new ItemUnwieldedEvent(user, force);
  266. RaiseLocalEvent(used, ref ev);
  267. return true;
  268. }
  269. /// <summary>
  270. /// Sets wielded without doing any checks.
  271. /// </summary>
  272. private void SetWielded(Entity<WieldableComponent> ent, bool wielded)
  273. {
  274. ent.Comp.Wielded = wielded;
  275. Dirty(ent);
  276. _appearance.SetData(ent, WieldableVisuals.Wielded, wielded);
  277. }
  278. private void OnItemUnwielded(EntityUid uid, WieldableComponent component, ItemUnwieldedEvent args)
  279. {
  280. _item.SetHeldPrefix(uid, component.OldInhandPrefix);
  281. var user = args.User;
  282. _virtualItem.DeleteInHandsMatching(user, uid);
  283. if (!args.Force) // don't play sound/popup if this was a forced unwield
  284. {
  285. if (component.UnwieldSound != null)
  286. _audio.PlayPredicted(component.UnwieldSound, uid, user);
  287. var selfMessage = Loc.GetString("wieldable-component-failed-wield", ("item", uid));
  288. var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User, EntityManager)), ("item", uid));
  289. _popup.PopupPredicted(selfMessage, othersMessage, user, user);
  290. }
  291. }
  292. private void OnItemLeaveHand(EntityUid uid, WieldableComponent component, GotUnequippedHandEvent args)
  293. {
  294. if (uid == args.Unequipped)
  295. TryUnwield(uid, component, args.User, force: true);
  296. }
  297. private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
  298. {
  299. if (args.BlockingEntity == uid)
  300. TryUnwield(uid, component, args.User, force: true);
  301. }
  302. private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)
  303. {
  304. if (!TryComp<WieldableComponent>(uid, out var wield))
  305. return;
  306. if (!wield.Wielded)
  307. return;
  308. args.Damage += component.BonusDamage;
  309. }
  310. }