Files
wwdpublic/Content.Shared/Materials/SharedMaterialReclaimerSystem.cs
SimpleStation14 dfeb3b986d Mirror: Fix Recycled Containers Deleting Items Inside Them (#267)
## Mirror of PR #26045: [Fix recycled containers deleting items inside
them](https://github.com/space-wizards/space-station-14/pull/26045) from
<img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `d7484ae9f57477a5ec5c575e3077aa94c0564db6`

PR opened by <img
src="https://avatars.githubusercontent.com/u/83650252?v=4"
width="16"/><a href="https://github.com/SlamBamActionman">
SlamBamActionman</a> at 2024-03-12 16:16:42 UTC

---

PR changed 2 files with 19 additions and 1 deletions.

The PR had the following labels:
- Status: Awaiting Changes


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> 
> Right now you can delete any item that fits in a survival box by
placing it inside of one and then recycling the box in a
Recycler/Material Reclaimer. This PR fixes this by dumping out any item
inside containers.
> 
> - For Reclaimers, any recyclable items inside the container get placed
on top of the Reclaimer.
> - For Recyclers, any recyclable items inside the container get
recursively recycled as well.
> 
> ## Why / Balance
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> 
> I doubt it's intended behavior to be able to effectively delete any
item that fits in a box. Also ensures a recycled box provides the
correct amount of material as its contents also get recycled.
> 
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> 
> When trying to recycle, once all checks are passed it iterates through
the object's containers if it has any and dumps out any items onto the
machine doing the recycling.
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
>
![image](https://github.com/space-wizards/space-station-14/assets/83650252/9e95b099-ab95-449d-b5a1-25b80afda8cd)
> The result of recycling a survival box in a Recycler/Reclaimer.
> 
> - [x] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> ## Breaking changes
> <!--
> List any breaking changes, including namespace, public
class/method/field changes, prototype renames; and provide instructions
for fixing them. This will be pasted in #codebase-changes.
> -->
> 
> **Changelog**
> <!--
> Make players aware of new features and changes that could affect how
they play the game by adding a Changelog entry. Please read the
Changelog guidelines located at:
https://docs.spacestation14.io/en/getting-started/pr-guideline#changelog
> -->
> 
> 🆑
> - fix: Recyclers no longer delete items stored inside of recycled
entities.
> 


</details>

---------

Co-authored-by: SimpleStation14 <Unknown>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
2024-07-11 17:43:32 -07:00

249 lines
9.2 KiB
C#

using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Audio;
using Content.Shared.Body.Components;
using Content.Shared.Coordinates;
using Content.Shared.Database;
using Content.Shared.Emag.Components;
using Content.Shared.Emag.Systems;
using Content.Shared.Examine;
using Content.Shared.Mobs.Components;
using Content.Shared.Stacks;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Physics.Events;
using Robust.Shared.Timing;
namespace Content.Shared.Materials;
/// <summary>
/// Handles interactions and logic related to <see cref="MaterialReclaimerComponent"/>,
/// <see cref="CollideMaterialReclaimerComponent"/>, and <see cref="ActiveMaterialReclaimerComponent"/>.
/// </summary>
public abstract class SharedMaterialReclaimerSystem : EntitySystem
{
[Dependency] private readonly ISharedAdminLogManager _adminLog = default!;
[Dependency] protected readonly IGameTiming Timing = default!;
[Dependency] protected readonly SharedAmbientSoundSystem AmbientSound = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] protected readonly SharedContainerSystem Container = default!;
public const string ActiveReclaimerContainerId = "active-material-reclaimer-container";
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<MaterialReclaimerComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<MaterialReclaimerComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<MaterialReclaimerComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<MaterialReclaimerComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CollideMaterialReclaimerComponent, StartCollideEvent>(OnCollide);
SubscribeLocalEvent<ActiveMaterialReclaimerComponent, ComponentStartup>(OnActiveStartup);
}
private void OnMapInit(EntityUid uid, MaterialReclaimerComponent component, MapInitEvent args)
{
component.NextSound = Timing.CurTime;
}
private void OnShutdown(EntityUid uid, MaterialReclaimerComponent component, ComponentShutdown args)
{
_audio.Stop(component.Stream);
}
private void OnExamined(EntityUid uid, MaterialReclaimerComponent component, ExaminedEvent args)
{
args.PushMarkup(Loc.GetString("recycler-count-items", ("items", component.ItemsProcessed)));
}
private void OnEmagged(EntityUid uid, MaterialReclaimerComponent component, ref GotEmaggedEvent args)
{
args.Handled = true;
}
private void OnCollide(EntityUid uid, CollideMaterialReclaimerComponent component, ref StartCollideEvent args)
{
if (args.OurFixtureId != component.FixtureId)
return;
if (!TryComp<MaterialReclaimerComponent>(uid, out var reclaimer))
return;
TryStartProcessItem(uid, args.OtherEntity, reclaimer);
}
private void OnActiveStartup(EntityUid uid, ActiveMaterialReclaimerComponent component, ComponentStartup args)
{
component.ReclaimingContainer = Container.EnsureContainer<Container>(uid, ActiveReclaimerContainerId);
}
/// <summary>
/// Tries to start processing an item via a <see cref="MaterialReclaimerComponent"/>.
/// </summary>
public bool TryStartProcessItem(EntityUid uid, EntityUid item, MaterialReclaimerComponent? component = null, EntityUid? user = null)
{
if (!Resolve(uid, ref component))
return false;
if (!CanStart(uid, component))
return false;
if (HasComp<MobStateComponent>(item) && !CanGib(uid, item, component)) // whitelist? We be gibbing, boy!
return false;
if (component.Whitelist is {} whitelist && !whitelist.IsValid(item))
return false;
if (component.Blacklist is {} blacklist && blacklist.IsValid(item))
return false;
if (Container.TryGetContainingContainer(item, out _) && !Container.TryRemoveFromContainer(item))
return false;
if (user != null)
{
_adminLog.Add(LogType.Action, LogImpact.High,
$"{ToPrettyString(user.Value):player} destroyed {ToPrettyString(item)} in the material reclaimer, {ToPrettyString(uid)}");
}
if (Timing.CurTime > component.NextSound)
{
component.Stream = _audio.PlayPredicted(component.Sound, uid, user)?.Entity;
component.NextSound = Timing.CurTime + component.SoundCooldown;
}
var reclaimedEvent = new GotReclaimedEvent(Transform(uid).Coordinates);
RaiseLocalEvent(item, ref reclaimedEvent);
var duration = GetReclaimingDuration(uid, item, component);
// if it's instant, don't bother with all the active comp stuff.
if (duration == TimeSpan.Zero)
{
Reclaim(uid, item, 1, component);
return true;
}
var active = EnsureComp<ActiveMaterialReclaimerComponent>(uid);
active.Duration = duration;
active.EndTime = Timing.CurTime + duration;
Container.Insert(item, active.ReclaimingContainer);
return true;
}
/// <summary>
/// Finishes processing an item, freeing up the the reclaimer.
/// </summary>
/// <remarks>
/// This doesn't reclaim the entity itself, but rather ends the formal
/// process started with <see cref="ActiveMaterialReclaimerComponent"/>.
/// The actual reclaiming happens in <see cref="Reclaim"/>
/// </remarks>
public virtual bool TryFinishProcessItem(EntityUid uid, MaterialReclaimerComponent? component = null, ActiveMaterialReclaimerComponent? active = null)
{
if (!Resolve(uid, ref component, ref active, false))
return false;
RemCompDeferred(uid, active);
return true;
}
/// <summary>
/// Spawns the materials and chemicals associated
/// with an entity. Also deletes the item.
/// </summary>
public virtual void Reclaim(EntityUid uid,
EntityUid item,
float completion = 1f,
MaterialReclaimerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.ItemsProcessed++;
if (component.CutOffSound)
{
_audio.Stop(component.Stream);
}
Dirty(uid, component);
}
/// <summary>
/// Sets the Enabled field on the reclaimer.
/// </summary>
public void SetReclaimerEnabled(EntityUid uid, bool enabled, MaterialReclaimerComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
component.Enabled = enabled;
AmbientSound.SetAmbience(uid, enabled && component.Powered);
Dirty(uid, component);
}
/// <summary>
/// Whether or not the specified reclaimer can currently
/// begin reclaiming another entity.
/// </summary>
public bool CanStart(EntityUid uid, MaterialReclaimerComponent component)
{
if (HasComp<ActiveMaterialReclaimerComponent>(uid))
return false;
return component.Powered && component.Enabled;
}
/// <summary>
/// Whether or not the reclaimer satisfies the conditions
/// allowing it to gib/reclaim a living creature.
/// </summary>
public bool CanGib(EntityUid uid, EntityUid victim, MaterialReclaimerComponent component)
{
return false; // DeltaV - Kinda LRP
// return component.Powered &&
// component.Enabled &&
// HasComp<BodyComponent>(victim) &&
// HasComp<EmaggedComponent>(uid);
}
/// <summary>
/// Gets the duration of processing a specified entity.
/// Processing is calculated from the sum of the materials within the entity.
/// It does not regard the chemicals within it.
/// </summary>
public TimeSpan GetReclaimingDuration(EntityUid reclaimer,
EntityUid item,
MaterialReclaimerComponent? reclaimerComponent = null,
PhysicalCompositionComponent? compositionComponent = null)
{
if (!Resolve(reclaimer, ref reclaimerComponent))
return TimeSpan.Zero;
if (!reclaimerComponent.ScaleProcessSpeed ||
!Resolve(item, ref compositionComponent, false))
return reclaimerComponent.MinimumProcessDuration;
var materialSum = compositionComponent.MaterialComposition.Values.Sum();
materialSum *= CompOrNull<StackComponent>(item)?.Count ?? 1;
var duration = TimeSpan.FromSeconds(materialSum / reclaimerComponent.MaterialProcessRate);
if (duration < reclaimerComponent.MinimumProcessDuration)
duration = reclaimerComponent.MinimumProcessDuration;
return duration;
}
/// <inheritdoc/>
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<ActiveMaterialReclaimerComponent, MaterialReclaimerComponent>();
while (query.MoveNext(out var uid, out var active, out var reclaimer))
{
if (Timing.CurTime < active.EndTime)
continue;
TryFinishProcessItem(uid, reclaimer, active);
}
}
}
[ByRefEvent]
public record struct GotReclaimedEvent(EntityCoordinates ReclaimerCoordinates);