EmitterSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System.Numerics;
  2. using System.Threading;
  3. using Content.Server.Administration.Logs;
  4. using Content.Server.DeviceLinking.Events;
  5. using Content.Server.Power.Components;
  6. using Content.Server.Power.EntitySystems;
  7. using Content.Server.Projectiles;
  8. using Content.Server.Weapons.Ranged.Systems;
  9. using Content.Shared.Database;
  10. using Content.Shared.Examine;
  11. using Content.Shared.Interaction;
  12. using Content.Shared.Lock;
  13. using Content.Shared.Popups;
  14. using Content.Shared.Power;
  15. using Content.Shared.Projectiles;
  16. using Content.Shared.Singularity.Components;
  17. using Content.Shared.Singularity.EntitySystems;
  18. using Content.Shared.Verbs;
  19. using Content.Shared.Weapons.Ranged.Components;
  20. using JetBrains.Annotations;
  21. using Robust.Shared.Map;
  22. using Robust.Shared.Physics;
  23. using Robust.Shared.Physics.Components;
  24. using Robust.Shared.Prototypes;
  25. using Robust.Shared.Random;
  26. using Robust.Shared.Utility;
  27. using Timer = Robust.Shared.Timing.Timer;
  28. namespace Content.Server.Singularity.EntitySystems
  29. {
  30. [UsedImplicitly]
  31. public sealed class EmitterSystem : SharedEmitterSystem
  32. {
  33. [Dependency] private readonly IRobustRandom _random = default!;
  34. [Dependency] private readonly IPrototypeManager _prototype = default!;
  35. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  36. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  37. [Dependency] private readonly SharedPopupSystem _popup = default!;
  38. [Dependency] private readonly ProjectileSystem _projectile = default!;
  39. [Dependency] private readonly GunSystem _gun = default!;
  40. public override void Initialize()
  41. {
  42. base.Initialize();
  43. SubscribeLocalEvent<EmitterComponent, PowerConsumerReceivedChanged>(ReceivedChanged);
  44. SubscribeLocalEvent<EmitterComponent, PowerChangedEvent>(OnApcChanged);
  45. SubscribeLocalEvent<EmitterComponent, ActivateInWorldEvent>(OnActivate);
  46. SubscribeLocalEvent<EmitterComponent, GetVerbsEvent<Verb>>(OnGetVerb);
  47. SubscribeLocalEvent<EmitterComponent, ExaminedEvent>(OnExamined);
  48. SubscribeLocalEvent<EmitterComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
  49. SubscribeLocalEvent<EmitterComponent, SignalReceivedEvent>(OnSignalReceived);
  50. }
  51. private void OnAnchorStateChanged(EntityUid uid, EmitterComponent component, ref AnchorStateChangedEvent args)
  52. {
  53. if (args.Anchored)
  54. return;
  55. SwitchOff(uid, component);
  56. }
  57. private void OnActivate(EntityUid uid, EmitterComponent component, ActivateInWorldEvent args)
  58. {
  59. if (args.Handled)
  60. return;
  61. if (TryComp(uid, out LockComponent? lockComp) && lockComp.Locked)
  62. {
  63. _popup.PopupEntity(Loc.GetString("comp-emitter-access-locked",
  64. ("target", uid)), uid, args.User);
  65. return;
  66. }
  67. if (TryComp(uid, out PhysicsComponent? phys) && phys.BodyType == BodyType.Static)
  68. {
  69. if (!component.IsOn)
  70. {
  71. SwitchOn(uid, component);
  72. _popup.PopupEntity(Loc.GetString("comp-emitter-turned-on",
  73. ("target", uid)), uid, args.User);
  74. }
  75. else
  76. {
  77. SwitchOff(uid, component);
  78. _popup.PopupEntity(Loc.GetString("comp-emitter-turned-off",
  79. ("target", uid)), uid, args.User);
  80. }
  81. _adminLogger.Add(LogType.FieldGeneration,
  82. component.IsOn ? LogImpact.Medium : LogImpact.High,
  83. $"{ToPrettyString(args.User):player} toggled {ToPrettyString(uid):emitter}");
  84. args.Handled = true;
  85. }
  86. else
  87. {
  88. _popup.PopupEntity(Loc.GetString("comp-emitter-not-anchored",
  89. ("target", uid)), uid, args.User);
  90. }
  91. }
  92. private void OnGetVerb(EntityUid uid, EmitterComponent component, GetVerbsEvent<Verb> args)
  93. {
  94. if (!args.CanAccess || !args.CanInteract || args.Hands == null)
  95. return;
  96. if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked)
  97. return;
  98. if (component.SelectableTypes.Count < 2)
  99. return;
  100. foreach (var type in component.SelectableTypes)
  101. {
  102. var proto = _prototype.Index<EntityPrototype>(type);
  103. var v = new Verb
  104. {
  105. Priority = 1,
  106. Category = VerbCategory.SelectType,
  107. Text = proto.Name,
  108. Disabled = type == component.BoltType,
  109. Impact = LogImpact.Medium,
  110. DoContactInteraction = true,
  111. Act = () =>
  112. {
  113. component.BoltType = type;
  114. _popup.PopupEntity(Loc.GetString("emitter-component-type-set", ("type", proto.Name)), uid);
  115. }
  116. };
  117. args.Verbs.Add(v);
  118. }
  119. }
  120. private void OnExamined(EntityUid uid, EmitterComponent component, ExaminedEvent args)
  121. {
  122. if (component.SelectableTypes.Count < 2)
  123. return;
  124. var proto = _prototype.Index<EntityPrototype>(component.BoltType);
  125. args.PushMarkup(Loc.GetString("emitter-component-current-type", ("type", proto.Name)));
  126. }
  127. private void ReceivedChanged(
  128. EntityUid uid,
  129. EmitterComponent component,
  130. ref PowerConsumerReceivedChanged args)
  131. {
  132. if (!component.IsOn)
  133. {
  134. return;
  135. }
  136. if (args.ReceivedPower < args.DrawRate)
  137. {
  138. PowerOff(uid, component);
  139. }
  140. else
  141. {
  142. PowerOn(uid, component);
  143. }
  144. }
  145. private void OnApcChanged(EntityUid uid, EmitterComponent component, ref PowerChangedEvent args)
  146. {
  147. if (!component.IsOn)
  148. {
  149. return;
  150. }
  151. if (!args.Powered)
  152. {
  153. PowerOff(uid, component);
  154. }
  155. else
  156. {
  157. PowerOn(uid, component);
  158. }
  159. }
  160. public void SwitchOff(EntityUid uid, EmitterComponent component)
  161. {
  162. component.IsOn = false;
  163. if (TryComp<PowerConsumerComponent>(uid, out var powerConsumer))
  164. powerConsumer.DrawRate = 1; // this needs to be not 0 so that the visuals still work.
  165. if (TryComp<ApcPowerReceiverComponent>(uid, out var apcReceiver))
  166. apcReceiver.Load = 1;
  167. PowerOff(uid, component);
  168. UpdateAppearance(uid, component);
  169. }
  170. public void SwitchOn(EntityUid uid, EmitterComponent component)
  171. {
  172. component.IsOn = true;
  173. if (TryComp<PowerConsumerComponent>(uid, out var powerConsumer))
  174. powerConsumer.DrawRate = component.PowerUseActive;
  175. if (TryComp<ApcPowerReceiverComponent>(uid, out var apcReceiver))
  176. {
  177. apcReceiver.Load = component.PowerUseActive;
  178. if (apcReceiver.Powered)
  179. PowerOn(uid, component);
  180. }
  181. // Do not directly PowerOn().
  182. // OnReceivedPowerChanged will get fired due to DrawRate change which will turn it on.
  183. UpdateAppearance(uid, component);
  184. }
  185. public void PowerOff(EntityUid uid, EmitterComponent component)
  186. {
  187. if (!component.IsPowered)
  188. {
  189. return;
  190. }
  191. component.IsPowered = false;
  192. // Must be set while emitter powered.
  193. DebugTools.AssertNotNull(component.TimerCancel);
  194. component.TimerCancel?.Cancel();
  195. UpdateAppearance(uid, component);
  196. }
  197. public void PowerOn(EntityUid uid, EmitterComponent component)
  198. {
  199. if (component.IsPowered)
  200. {
  201. return;
  202. }
  203. component.IsPowered = true;
  204. component.FireShotCounter = 0;
  205. component.TimerCancel = new CancellationTokenSource();
  206. Timer.Spawn(component.FireBurstDelayMax, () => ShotTimerCallback(uid, component), component.TimerCancel.Token);
  207. UpdateAppearance(uid, component);
  208. }
  209. private void ShotTimerCallback(EntityUid uid, EmitterComponent component)
  210. {
  211. if (component.Deleted)
  212. return;
  213. // Any power-off condition should result in the timer for this method being cancelled
  214. // and thus not firing
  215. DebugTools.Assert(component.IsPowered);
  216. DebugTools.Assert(component.IsOn);
  217. Fire(uid, component);
  218. TimeSpan delay;
  219. if (component.FireShotCounter < component.FireBurstSize)
  220. {
  221. component.FireShotCounter += 1;
  222. delay = component.FireInterval;
  223. }
  224. else
  225. {
  226. component.FireShotCounter = 0;
  227. var diff = component.FireBurstDelayMax - component.FireBurstDelayMin;
  228. // TIL you can do TimeSpan * double.
  229. delay = component.FireBurstDelayMin + _random.NextFloat() * diff;
  230. }
  231. // Must be set while emitter powered.
  232. DebugTools.AssertNotNull(component.TimerCancel);
  233. Timer.Spawn(delay, () => ShotTimerCallback(uid, component), component.TimerCancel!.Token);
  234. }
  235. private void Fire(EntityUid uid, EmitterComponent component)
  236. {
  237. if (!TryComp<GunComponent>(uid, out var gunComponent))
  238. return;
  239. var xform = Transform(uid);
  240. var ent = Spawn(component.BoltType, xform.Coordinates);
  241. var proj = EnsureComp<ProjectileComponent>(ent);
  242. _projectile.SetShooter(ent, proj, uid);
  243. var targetPos = new EntityCoordinates(uid, new Vector2(0, -1));
  244. _gun.Shoot(uid, gunComponent, ent, xform.Coordinates, targetPos, out _);
  245. }
  246. private void UpdateAppearance(EntityUid uid, EmitterComponent component)
  247. {
  248. EmitterVisualState state;
  249. if (component.IsPowered)
  250. {
  251. state = EmitterVisualState.On;
  252. }
  253. else if (component.IsOn)
  254. {
  255. state = EmitterVisualState.Underpowered;
  256. }
  257. else
  258. {
  259. state = EmitterVisualState.Off;
  260. }
  261. _appearance.SetData(uid, EmitterVisuals.VisualState, state);
  262. }
  263. private void OnSignalReceived(EntityUid uid, EmitterComponent component, ref SignalReceivedEvent args)
  264. {
  265. // must anchor the emitter for signals to work
  266. if (TryComp<PhysicsComponent>(uid, out var phys) && phys.BodyType != BodyType.Static)
  267. return;
  268. if (args.Port == component.OffPort)
  269. {
  270. SwitchOff(uid, component);
  271. }
  272. else if (args.Port == component.OnPort)
  273. {
  274. SwitchOn(uid, component);
  275. }
  276. else if (args.Port == component.TogglePort)
  277. {
  278. if (component.IsOn)
  279. {
  280. SwitchOff(uid, component);
  281. }
  282. else
  283. {
  284. SwitchOn(uid, component);
  285. }
  286. }
  287. else if (component.SetTypePorts.TryGetValue(args.Port, out var boltType))
  288. {
  289. component.BoltType = boltType;
  290. }
  291. }
  292. }
  293. }