fish-station/Content.Shared/Materials/SharedMaterialReclaimerSystem.cs
VigersRay c0ee7e3cc9 Merge remote-tracking branch 'refs/remotes/wizards/master'
# Conflicts:
#	.github/PULL_REQUEST_TEMPLATE.md
#	Content.Client/Lobby/LobbyUIController.cs
#	Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
#	Content.Client/Players/PlayTimeTracking/JobRequirementsManager.cs
#	Content.Server/Administration/Systems/BwoinkSystem.cs
#	Content.Server/IoC/ServerContentIoC.cs
#	Content.Server/Players/PlayTimeTracking/PlayTimeTrackingSystem.cs
#	Content.Server/Shuttles/Systems/ArrivalsSystem.cs
#	Content.Shared/Preferences/Loadouts/LoadoutPrototype.cs
#	Content.Shared/Roles/JobRequirements.cs
#	Resources/Prototypes/Accents/word_replacements.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/security.yml
#	Resources/Prototypes/Entities/Mobs/Species/base.yml
#	Resources/Prototypes/Loadouts/Jobs/Cargo/cargo_technician.yml
#	Resources/Prototypes/Loadouts/Jobs/Cargo/quartermaster.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/bartender.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/botanist.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/chaplain.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/chef.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/janitor.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/lawyer.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/librarian.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/mime.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/passenger.yml
#	Resources/Prototypes/Loadouts/Jobs/Command/captain.yml
#	Resources/Prototypes/Loadouts/Jobs/Command/head_of_personnel.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/atmospheric_technician.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/chief_engineer.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/station_engineer.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/technical_assistant.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/chemist.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/chief_medical_officer.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/medical_doctor.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/medical_intern.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/paramedic.yml
#	Resources/Prototypes/Loadouts/Jobs/Science/research_director.yml
#	Resources/Prototypes/Loadouts/Jobs/Science/scientist.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/detective.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/head_of_security.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_cadet.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/warden.yml
#	Resources/Prototypes/Loadouts/Miscellaneous/trinkets.yml
#	Resources/Prototypes/Recipes/Lathes/medical.yml
#	Resources/Prototypes/Roles/Jobs/Cargo/quartermaster.yml
#	Resources/Prototypes/Roles/Jobs/Command/captain.yml
#	Resources/Prototypes/Roles/Jobs/Command/head_of_personnel.yml
#	Resources/Prototypes/Roles/Jobs/Engineering/chief_engineer.yml
#	Resources/Prototypes/Roles/Jobs/Medical/chief_medical_officer.yml
#	Resources/Prototypes/Roles/Jobs/Science/research_director.yml
#	Resources/Prototypes/Roles/Jobs/Security/head_of_security.yml
#	Resources/Textures/Clothing/Mask/sadmime.rsi/meta.json
#	Resources/Textures/Clothing/Mask/scaredmime.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coat.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatatmos.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcap.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcargo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatce.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcentcom.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatchem.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcmo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatengi.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatgen.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathop.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathos.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathydro.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatjani.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatmed.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatmime.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatminer.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatparamed.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatqm.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatrd.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatrobo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatsci.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatsec.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatviro.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatwarden.rsi/meta.json
#	Resources/Textures/Structures/Power/apc.rsi/base.png
#	Resources/Textures/Structures/Power/apc.rsi/broken.png
#	Resources/Textures/Structures/Power/apc.rsi/display-charging.png
#	Resources/Textures/Structures/Power/apc.rsi/display-full.png
#	Resources/Textures/Structures/Power/apc.rsi/display-lack.png
#	Resources/Textures/Structures/Power/apc.rsi/display-remote.png
#	Resources/Textures/Structures/Power/apc.rsi/emag-unlit.png
#	Resources/Textures/Structures/Power/apc.rsi/frame.png
#	Resources/Textures/Structures/Power/apc.rsi/static.png
2024-08-05 17:22:05 +03:00

248 lines
9.3 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 Content.Shared.Whitelist;
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!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = 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 (_whitelistSystem.IsWhitelistFail(component.Whitelist, item) ||
_whitelistSystem.IsBlacklistPass(component.Blacklist, item))
return false;
if (Container.TryGetContainingContainer((item, null, null), 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; // Ради казуалов.
// 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);