1
0

SharedWieldableSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. // Do they have enough hands free?
  73. if (!TryComp<HandsComponent>(args.User, out var hands))
  74. {
  75. args.Cancel();
  76. }
  77. if (_hands.TryGetEmptyHand(args.User, out var hand, hands) == false)
  78. {
  79. if (!TryComp<WieldableComponent>(uid, out var _))
  80. {
  81. args.Cancel();
  82. var time = _timing.CurTime;
  83. if (time > component.LastPopup + component.PopupCooldown &&
  84. !HasComp<MeleeWeaponComponent>(uid) &&
  85. !HasComp<MeleeRequiresWieldComponent>(uid))
  86. {
  87. component.LastPopup = time;
  88. var message = Loc.GetString("wieldable-component-no-free-hands");
  89. _popup.PopupClient(message, args.Used, args.User);
  90. }
  91. }
  92. }
  93. if (TryComp<WieldableComponent>(uid, out var wieldable) &&
  94. !wieldable.Wielded)
  95. {
  96. args.Cancel();
  97. var time = _timing.CurTime;
  98. if (time > component.LastPopup + component.PopupCooldown &&
  99. !HasComp<MeleeWeaponComponent>(uid) &&
  100. !HasComp<MeleeRequiresWieldComponent>(uid))
  101. {
  102. component.LastPopup = time;
  103. var message = Loc.GetString("wieldable-component-requires", ("item", uid));
  104. _popup.PopupClient(message, args.Used, args.User);
  105. }
  106. }
  107. }
  108. private void OnGunUnwielded(EntityUid uid, GunWieldBonusComponent component, ItemUnwieldedEvent args)
  109. {
  110. _gun.RefreshModifiers(uid);
  111. }
  112. private void OnGunWielded(EntityUid uid, GunWieldBonusComponent component, ref ItemWieldedEvent args)
  113. {
  114. _gun.RefreshModifiers(uid);
  115. }
  116. private void OnDeselectWieldable(EntityUid uid, WieldableComponent component, HandDeselectedEvent args)
  117. {
  118. if (_hands.EnumerateHands(args.User).Count() > 2)
  119. return;
  120. TryUnwield(uid, component, args.User);
  121. }
  122. private void OnGunRefreshModifiers(Entity<GunWieldBonusComponent> bonus, ref GunRefreshModifiersEvent args)
  123. {
  124. if (TryComp(bonus, out WieldableComponent? wield) &&
  125. wield.Wielded)
  126. {
  127. args.MinAngle += bonus.Comp.MinAngle;
  128. args.MaxAngle += bonus.Comp.MaxAngle;
  129. args.AngleDecay += bonus.Comp.AngleDecay;
  130. args.AngleIncrease += bonus.Comp.AngleIncrease;
  131. }
  132. }
  133. private void OnSpeedModifierWielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ItemWieldedEvent args)
  134. {
  135. _movementSpeedModifier.RefreshMovementSpeedModifiers(args.User);
  136. }
  137. private void OnSpeedModifierUnwielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ItemUnwieldedEvent args)
  138. {
  139. _movementSpeedModifier.RefreshMovementSpeedModifiers(args.User);
  140. }
  141. private void OnRefreshSpeedWielded(EntityUid uid, SpeedModifiedOnWieldComponent component, ref HeldRelayedEvent<RefreshMovementSpeedModifiersEvent> args)
  142. {
  143. if (TryComp<WieldableComponent>(uid, out var wield) && wield.Wielded)
  144. {
  145. args.Args.ModifySpeed(component.WalkModifier, component.SprintModifier);
  146. }
  147. }
  148. private void OnExamineRequires(Entity<GunRequiresWieldComponent> entity, ref ExaminedEvent args)
  149. {
  150. if (entity.Comp.WieldRequiresExamineMessage != null)
  151. args.PushText(Loc.GetString(entity.Comp.WieldRequiresExamineMessage));
  152. }
  153. private void OnExamine(EntityUid uid, GunWieldBonusComponent component, ref ExaminedEvent args)
  154. {
  155. if (HasComp<GunRequiresWieldComponent>(uid))
  156. return;
  157. if (component.WieldBonusExamineMessage != null)
  158. args.PushText(Loc.GetString(component.WieldBonusExamineMessage));
  159. }
  160. private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<InteractionVerb> args)
  161. {
  162. if (args.Hands == null || !args.CanAccess || !args.CanInteract)
  163. return;
  164. if (!_hands.IsHolding(args.User, uid, out _, args.Hands))
  165. return;
  166. // TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
  167. // verb. Or just don't add it to the list if the action is not executable.
  168. // TODO VERBS ICON
  169. InteractionVerb verb = new()
  170. {
  171. Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"),
  172. Act = component.Wielded
  173. ? () => TryUnwield(uid, component, args.User)
  174. : () => TryWield(uid, component, args.User)
  175. };
  176. args.Verbs.Add(verb);
  177. }
  178. private void OnUseInHand(EntityUid uid, WieldableComponent component, UseInHandEvent args)
  179. {
  180. if (args.Handled)
  181. return;
  182. if (!component.Wielded)
  183. args.Handled = TryWield(uid, component, args.User);
  184. else if (component.UnwieldOnUse)
  185. args.Handled = TryUnwield(uid, component, args.User);
  186. if (HasComp<UseDelayComponent>(uid) && !component.UseDelayOnWield)
  187. args.ApplyDelay = false;
  188. }
  189. public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet = false)
  190. {
  191. // Do they have enough hands free?
  192. if (!TryComp<HandsComponent>(user, out var hands))
  193. {
  194. if (!quiet)
  195. _popup.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
  196. return false;
  197. }
  198. // Is it.. actually in one of their hands?
  199. if (!_hands.IsHolding(user, uid, out _, hands))
  200. {
  201. if (!quiet)
  202. _popup.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
  203. return false;
  204. }
  205. if (_hands.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
  206. {
  207. if (!quiet)
  208. {
  209. var message = Loc.GetString("wieldable-component-not-enough-free-hands",
  210. ("number", component.FreeHandsRequired), ("item", uid));
  211. _popup.PopupClient(message, user, user);
  212. }
  213. return false;
  214. }
  215. // Seems legit.
  216. return true;
  217. }
  218. /// <summary>
  219. /// Attempts to wield an item, starting a UseDelay after.
  220. /// </summary>
  221. /// <returns>True if the attempt wasn't blocked.</returns>
  222. public bool TryWield(EntityUid used, WieldableComponent component, EntityUid user)
  223. {
  224. if (!CanWield(used, component, user))
  225. return false;
  226. if (TryComp(used, out UseDelayComponent? useDelay) && component.UseDelayOnWield)
  227. {
  228. if (!_delay.TryResetDelay((used, useDelay), true))
  229. return false;
  230. }
  231. var attemptEv = new WieldAttemptEvent(user);
  232. RaiseLocalEvent(used, ref attemptEv);
  233. if (attemptEv.Cancelled)
  234. return false;
  235. if (TryComp<ItemComponent>(used, out var item))
  236. {
  237. component.OldInhandPrefix = item.HeldPrefix;
  238. _item.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
  239. }
  240. SetWielded((used, component), true);
  241. if (component.WieldSound != null)
  242. _audio.PlayPredicted(component.WieldSound, used, user);
  243. //This section handles spawning the virtual item(s) to occupy the required additional hand(s).
  244. //Since the client can't currently predict entity spawning, only do this if this is running serverside.
  245. //Remove this check if TrySpawnVirtualItem in SharedVirtualItemSystem is allowed to complete clientside.
  246. if (_netManager.IsServer)
  247. {
  248. var virtuals = new List<EntityUid>();
  249. for (var i = 0; i < component.FreeHandsRequired; i++)
  250. {
  251. if (_virtualItem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
  252. {
  253. virtuals.Add(virtualItem.Value);
  254. continue;
  255. }
  256. foreach (var existingVirtual in virtuals)
  257. {
  258. QueueDel(existingVirtual);
  259. }
  260. return false;
  261. }
  262. }
  263. var selfMessage = Loc.GetString("wieldable-component-successful-wield", ("item", used));
  264. var othersMessage = Loc.GetString("wieldable-component-successful-wield-other", ("user", Identity.Entity(user, EntityManager)), ("item", used));
  265. _popup.PopupPredicted(selfMessage, othersMessage, user, user);
  266. var ev = new ItemWieldedEvent(user);
  267. RaiseLocalEvent(used, ref ev);
  268. return true;
  269. }
  270. /// <summary>
  271. /// Attempts to unwield an item, with no use delay.
  272. /// </summary>
  273. /// <returns>True if the attempt wasn't blocked.</returns>
  274. public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user, bool force = false)
  275. {
  276. if (!component.Wielded)
  277. return false; // already unwielded
  278. if (!force)
  279. {
  280. var attemptEv = new UnwieldAttemptEvent(user);
  281. RaiseLocalEvent(used, ref attemptEv);
  282. if (attemptEv.Cancelled)
  283. return false;
  284. }
  285. SetWielded((used, component), false);
  286. var ev = new ItemUnwieldedEvent(user, force);
  287. RaiseLocalEvent(used, ref ev);
  288. return true;
  289. }
  290. /// <summary>
  291. /// Sets wielded without doing any checks.
  292. /// </summary>
  293. private void SetWielded(Entity<WieldableComponent> ent, bool wielded)
  294. {
  295. ent.Comp.Wielded = wielded;
  296. Dirty(ent);
  297. _appearance.SetData(ent, WieldableVisuals.Wielded, wielded);
  298. }
  299. private void OnItemUnwielded(EntityUid uid, WieldableComponent component, ItemUnwieldedEvent args)
  300. {
  301. _item.SetHeldPrefix(uid, component.OldInhandPrefix);
  302. var user = args.User;
  303. _virtualItem.DeleteInHandsMatching(user, uid);
  304. if (!args.Force) // don't play sound/popup if this was a forced unwield
  305. {
  306. if (component.UnwieldSound != null)
  307. _audio.PlayPredicted(component.UnwieldSound, uid, user);
  308. var selfMessage = Loc.GetString("wieldable-component-failed-wield", ("item", uid));
  309. var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User, EntityManager)), ("item", uid));
  310. _popup.PopupPredicted(selfMessage, othersMessage, user, user);
  311. }
  312. }
  313. private void OnItemLeaveHand(EntityUid uid, WieldableComponent component, GotUnequippedHandEvent args)
  314. {
  315. if (uid == args.Unequipped)
  316. TryUnwield(uid, component, args.User, force: true);
  317. }
  318. private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
  319. {
  320. if (args.BlockingEntity == uid)
  321. TryUnwield(uid, component, args.User, force: true);
  322. }
  323. private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)
  324. {
  325. if (!TryComp<WieldableComponent>(uid, out var wield))
  326. return;
  327. if (!wield.Wielded)
  328. return;
  329. args.Damage += component.BonusDamage;
  330. }
  331. }