BiomassReclaimerSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System.Numerics;
  2. using Content.Server.Body.Components;
  3. using Content.Server.Botany.Components;
  4. using Content.Server.Fluids.EntitySystems;
  5. using Content.Server.Materials;
  6. using Content.Server.Power.Components;
  7. using Content.Shared.Administration.Logs;
  8. using Content.Shared.Audio;
  9. using Content.Shared.CCVar;
  10. using Content.Shared.Chemistry.Components;
  11. using Content.Shared.Climbing.Events;
  12. using Content.Shared.Construction.Components;
  13. using Content.Shared.Database;
  14. using Content.Shared.DoAfter;
  15. using Content.Shared.Humanoid;
  16. using Content.Shared.Interaction;
  17. using Content.Shared.Interaction.Events;
  18. using Content.Shared.Inventory;
  19. using Content.Shared.Jittering;
  20. using Content.Shared.Medical;
  21. using Content.Shared.Mind;
  22. using Content.Shared.Materials;
  23. using Content.Shared.Mobs.Components;
  24. using Content.Shared.Mobs.Systems;
  25. using Content.Shared.Nutrition.Components;
  26. using Content.Shared.Popups;
  27. using Content.Shared.Power;
  28. using Content.Shared.Throwing;
  29. using Robust.Server.Player;
  30. using Robust.Shared.Audio.Systems;
  31. using Robust.Shared.Configuration;
  32. using Robust.Shared.Physics.Components;
  33. using Robust.Shared.Prototypes;
  34. using Robust.Shared.Random;
  35. namespace Content.Server.Medical.BiomassReclaimer
  36. {
  37. public sealed class BiomassReclaimerSystem : EntitySystem
  38. {
  39. [Dependency] private readonly IConfigurationManager _configManager = default!;
  40. [Dependency] private readonly SharedTransformSystem _transform = default!;
  41. [Dependency] private readonly MobStateSystem _mobState = default!;
  42. [Dependency] private readonly SharedJitteringSystem _jitteringSystem = default!;
  43. [Dependency] private readonly SharedAudioSystem _sharedAudioSystem = default!;
  44. [Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
  45. [Dependency] private readonly SharedPopupSystem _popup = default!;
  46. [Dependency] private readonly PuddleSystem _puddleSystem = default!;
  47. [Dependency] private readonly ThrowingSystem _throwing = default!;
  48. [Dependency] private readonly IRobustRandom _robustRandom = default!;
  49. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  50. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  51. [Dependency] private readonly IPlayerManager _playerManager = default!;
  52. [Dependency] private readonly MaterialStorageSystem _material = default!;
  53. [Dependency] private readonly SharedMindSystem _minds = default!;
  54. [Dependency] private readonly InventorySystem _inventory = default!;
  55. [ValidatePrototypeId<MaterialPrototype>]
  56. public const string BiomassPrototype = "Biomass";
  57. public override void Update(float frameTime)
  58. {
  59. base.Update(frameTime);
  60. var query = EntityQueryEnumerator<ActiveBiomassReclaimerComponent, BiomassReclaimerComponent>();
  61. while (query.MoveNext(out var uid, out var _, out var reclaimer))
  62. {
  63. reclaimer.ProcessingTimer -= frameTime;
  64. reclaimer.RandomMessTimer -= frameTime;
  65. if (reclaimer.RandomMessTimer <= 0)
  66. {
  67. if (_robustRandom.Prob(0.2f) && reclaimer.BloodReagent is not null)
  68. {
  69. Solution blood = new();
  70. blood.AddReagent(reclaimer.BloodReagent, 50);
  71. _puddleSystem.TrySpillAt(uid, blood, out _);
  72. }
  73. if (_robustRandom.Prob(0.03f) && reclaimer.SpawnedEntities.Count > 0)
  74. {
  75. var thrown = Spawn(_robustRandom.Pick(reclaimer.SpawnedEntities).PrototypeId, Transform(uid).Coordinates);
  76. var direction = new Vector2(_robustRandom.Next(-30, 30), _robustRandom.Next(-30, 30));
  77. _throwing.TryThrow(thrown, direction, _robustRandom.Next(1, 10));
  78. }
  79. reclaimer.RandomMessTimer += (float) reclaimer.RandomMessInterval.TotalSeconds;
  80. }
  81. if (reclaimer.ProcessingTimer > 0)
  82. {
  83. continue;
  84. }
  85. var actualYield = (int) (reclaimer.CurrentExpectedYield); // can only have integer biomass
  86. reclaimer.CurrentExpectedYield = reclaimer.CurrentExpectedYield - actualYield; // store non-integer leftovers
  87. _material.SpawnMultipleFromMaterial(actualYield, BiomassPrototype, Transform(uid).Coordinates);
  88. reclaimer.BloodReagent = null;
  89. reclaimer.SpawnedEntities.Clear();
  90. RemCompDeferred<ActiveBiomassReclaimerComponent>(uid);
  91. }
  92. }
  93. public override void Initialize()
  94. {
  95. base.Initialize();
  96. SubscribeLocalEvent<ActiveBiomassReclaimerComponent, ComponentInit>(OnInit);
  97. SubscribeLocalEvent<ActiveBiomassReclaimerComponent, ComponentShutdown>(OnShutdown);
  98. SubscribeLocalEvent<ActiveBiomassReclaimerComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
  99. SubscribeLocalEvent<BiomassReclaimerComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
  100. SubscribeLocalEvent<BiomassReclaimerComponent, ClimbedOnEvent>(OnClimbedOn);
  101. SubscribeLocalEvent<BiomassReclaimerComponent, PowerChangedEvent>(OnPowerChanged);
  102. SubscribeLocalEvent<BiomassReclaimerComponent, SuicideByEnvironmentEvent>(OnSuicideByEnvironment);
  103. SubscribeLocalEvent<BiomassReclaimerComponent, ReclaimerDoAfterEvent>(OnDoAfter);
  104. }
  105. private void OnSuicideByEnvironment(Entity<BiomassReclaimerComponent> ent, ref SuicideByEnvironmentEvent args)
  106. {
  107. if (args.Handled)
  108. return;
  109. if (HasComp<ActiveBiomassReclaimerComponent>(ent))
  110. return;
  111. if (TryComp<ApcPowerReceiverComponent>(ent, out var power) && !power.Powered)
  112. return;
  113. _popup.PopupEntity(Loc.GetString("biomass-reclaimer-suicide-others", ("victim", args.Victim)), ent, PopupType.LargeCaution);
  114. StartProcessing(args.Victim, ent);
  115. args.Handled = true;
  116. }
  117. private void OnInit(EntityUid uid, ActiveBiomassReclaimerComponent component, ComponentInit args)
  118. {
  119. _jitteringSystem.AddJitter(uid, -10, 100);
  120. _sharedAudioSystem.PlayPvs("/Audio/Machines/reclaimer_startup.ogg", uid);
  121. _ambientSoundSystem.SetAmbience(uid, true);
  122. }
  123. private void OnShutdown(EntityUid uid, ActiveBiomassReclaimerComponent component, ComponentShutdown args)
  124. {
  125. RemComp<JitteringComponent>(uid);
  126. _ambientSoundSystem.SetAmbience(uid, false);
  127. }
  128. private void OnPowerChanged(EntityUid uid, BiomassReclaimerComponent component, ref PowerChangedEvent args)
  129. {
  130. if (args.Powered)
  131. {
  132. if (component.ProcessingTimer > 0)
  133. EnsureComp<ActiveBiomassReclaimerComponent>(uid);
  134. }
  135. else
  136. RemComp<ActiveBiomassReclaimerComponent>(uid);
  137. }
  138. private void OnUnanchorAttempt(EntityUid uid, ActiveBiomassReclaimerComponent component, UnanchorAttemptEvent args)
  139. {
  140. args.Cancel();
  141. }
  142. private void OnAfterInteractUsing(Entity<BiomassReclaimerComponent> reclaimer, ref AfterInteractUsingEvent args)
  143. {
  144. if (!args.CanReach || args.Target == null)
  145. return;
  146. if (!CanGib(reclaimer, args.Used))
  147. return;
  148. if (!TryComp<PhysicsComponent>(args.Used, out var physics))
  149. return;
  150. var delay = reclaimer.Comp.BaseInsertionDelay * physics.FixturesMass;
  151. _doAfterSystem.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, delay, new ReclaimerDoAfterEvent(), reclaimer, target: args.Target, used: args.Used)
  152. {
  153. NeedHand = true,
  154. BreakOnMove = true,
  155. });
  156. }
  157. private void OnClimbedOn(Entity<BiomassReclaimerComponent> reclaimer, ref ClimbedOnEvent args)
  158. {
  159. if (!CanGib(reclaimer, args.Climber))
  160. {
  161. var direction = new Vector2(_robustRandom.Next(-2, 2), _robustRandom.Next(-2, 2));
  162. _throwing.TryThrow(args.Climber, direction, 0.5f);
  163. return;
  164. }
  165. _adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.Instigator):player} used a biomass reclaimer to gib {ToPrettyString(args.Climber):target} in {ToPrettyString(reclaimer):reclaimer}");
  166. StartProcessing(args.Climber, reclaimer);
  167. }
  168. private void OnDoAfter(Entity<BiomassReclaimerComponent> reclaimer, ref ReclaimerDoAfterEvent args)
  169. {
  170. if (args.Handled || args.Cancelled)
  171. return;
  172. if (args.Args.Used == null || args.Args.Target == null || !HasComp<BiomassReclaimerComponent>(args.Args.Target.Value))
  173. return;
  174. _adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.Args.User):player} used a biomass reclaimer to gib {ToPrettyString(args.Args.Target.Value):target} in {ToPrettyString(reclaimer):reclaimer}");
  175. StartProcessing(args.Args.Used.Value, reclaimer);
  176. args.Handled = true;
  177. }
  178. private void StartProcessing(EntityUid toProcess, Entity<BiomassReclaimerComponent> ent, PhysicsComponent? physics = null)
  179. {
  180. if (!Resolve(toProcess, ref physics))
  181. return;
  182. var component = ent.Comp;
  183. AddComp<ActiveBiomassReclaimerComponent>(ent);
  184. if (TryComp<BloodstreamComponent>(toProcess, out var stream))
  185. {
  186. component.BloodReagent = stream.BloodReagent;
  187. }
  188. if (TryComp<ButcherableComponent>(toProcess, out var butcherableComponent))
  189. {
  190. component.SpawnedEntities = butcherableComponent.SpawnedEntities;
  191. }
  192. var expectedYield = physics.FixturesMass * component.YieldPerUnitMass;
  193. if (HasComp<ProduceComponent>(toProcess))
  194. expectedYield *= component.ProduceYieldMultiplier;
  195. component.CurrentExpectedYield += expectedYield;
  196. component.ProcessingTimer = physics.FixturesMass * component.ProcessingTimePerUnitMass;
  197. var inventory = _inventory.GetHandOrInventoryEntities(toProcess);
  198. foreach (var item in inventory)
  199. {
  200. _transform.DropNextTo(item, ent.Owner);
  201. }
  202. QueueDel(toProcess);
  203. }
  204. private bool CanGib(Entity<BiomassReclaimerComponent> reclaimer, EntityUid dragged)
  205. {
  206. if (HasComp<ActiveBiomassReclaimerComponent>(reclaimer))
  207. return false;
  208. bool isPlant = HasComp<ProduceComponent>(dragged);
  209. if (!isPlant && !HasComp<MobStateComponent>(dragged))
  210. return false;
  211. if (!Transform(reclaimer).Anchored)
  212. return false;
  213. if (TryComp<ApcPowerReceiverComponent>(reclaimer, out var power) && !power.Powered)
  214. return false;
  215. if (!isPlant && reclaimer.Comp.SafetyEnabled && !_mobState.IsDead(dragged))
  216. return false;
  217. // Reject souled bodies in easy mode.
  218. if (_configManager.GetCVar(CCVars.BiomassEasyMode) &&
  219. HasComp<HumanoidAppearanceComponent>(dragged) &&
  220. _minds.TryGetMind(dragged, out _, out var mind))
  221. {
  222. if (mind.UserId != null && _playerManager.TryGetSessionById(mind.UserId.Value, out _))
  223. return false;
  224. }
  225. return true;
  226. }
  227. }
  228. }