-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- This is a semi-strict format, you can add/remove sections as needed but the order/format should be kept the same Remove these comments before submitting --> # Description <!-- Explain this PR in as much detail as applicable Some example prompts to consider: How might this affect the game? The codebase? What might be some alternatives to this? How/Who does this benefit/hurt [the game/codebase]? --> Port from [Goob](Goob-Station/Goob-Station#989) --- # Changelog <!-- You can add an author after the `:cl:` to change the name that appears in the changelog (ex: `:cl: Death`) Leaving it blank will default to your GitHub display name This includes all available types for the changelog --> :cl: Aviu00, Spatison - add: Added material silo --------- Signed-off-by: Spatison <[email protected]> Signed-off-by: VMSolidus <[email protected]> Co-authored-by: VMSolidus <[email protected]>
- Loading branch information
Showing
32 changed files
with
668 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using Content.Shared.Materials; | ||
|
||
namespace Content.Client.Materials; | ||
|
||
public sealed class MaterialSiloSystem : SharedMaterialSiloSystem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Linq; | ||
using Content.Server.Lathe; | ||
using Content.Server.Station.Components; | ||
using Content.Shared.DeviceLinking; | ||
using Content.Shared.Lathe; | ||
using Content.Shared.Materials; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Server.Materials; | ||
|
||
public sealed class MaterialSiloSystem : SharedMaterialSiloSystem | ||
{ | ||
[Dependency] private readonly LatheSystem _lathe = default!; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
SubscribeLocalEvent<BecomesStationComponent, MapInitEvent>(OnMapInit); | ||
SubscribeLocalEvent<MaterialSiloComponent, MaterialAmountChangedEvent>(OnMaterialAmountChanged); | ||
} | ||
|
||
private void OnMaterialAmountChanged(Entity<MaterialSiloComponent> ent, ref MaterialAmountChangedEvent args) | ||
{ | ||
// Spawning a timer because SetUiState in UpdateUserInterfaceState is being networked before | ||
// silo's MaterialStorageComponent state gets handled. | ||
// That causes lathe ui recipe list to not update properly. | ||
Timer.Spawn(20, | ||
() => | ||
{ | ||
if (!TryComp(ent, out DeviceLinkSourceComponent? source)) | ||
return; | ||
|
||
foreach (var utilizerSet in source.Outputs.Where(x => x.Key == SourcePort).Select(x => x.Value)) | ||
{ | ||
foreach (var utilizer in utilizerSet) | ||
{ | ||
if (TryComp(utilizer, out LatheComponent? lathe)) | ||
_lathe.UpdateUserInterfaceState(utilizer, lathe); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void OnMapInit(Entity<BecomesStationComponent> ent, ref MapInitEvent args) | ||
{ | ||
Entity<DeviceLinkSourceComponent>? silo = null; | ||
var siloQuery = AllEntityQuery<MaterialSiloComponent, MaterialStorageComponent, TransformComponent, DeviceLinkSourceComponent>(); | ||
while (siloQuery.MoveNext(out var siloEnt, out _, out _, out var siloXform, out var source)) | ||
{ | ||
if (siloXform.GridUid != ent) | ||
continue; | ||
|
||
silo = (siloEnt, source); | ||
break; | ||
} | ||
|
||
if (silo == null) | ||
return; | ||
|
||
var utilizerQuery = AllEntityQuery<MaterialSiloUtilizerComponent, MaterialStorageComponent, TransformComponent, DeviceLinkSinkComponent>(); | ||
while (utilizerQuery.MoveNext(out var utilizer, out _, out var storage, out var utilizerXform, out var sink)) | ||
{ | ||
if (utilizerXform.GridUid != ent) | ||
continue; | ||
|
||
DeviceLink.LinkDefaults(null, silo.Value, utilizer, silo.Value.Comp, sink); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Robust.Shared.GameStates; | ||
|
||
namespace Content.Shared.Materials; | ||
|
||
[RegisterComponent, NetworkedComponent] | ||
public sealed partial class MaterialSiloComponent : Component; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Robust.Shared.GameStates; | ||
|
||
namespace Content.Shared.Materials; | ||
|
||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] | ||
public sealed partial class MaterialSiloUtilizerComponent : Component | ||
{ | ||
[DataField, AutoNetworkedField] | ||
public EntityUid? Silo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System.Linq; | ||
using Content.Shared.CCVar; | ||
using Content.Shared.DeviceLinking; | ||
using Content.Shared.DeviceLinking.Events; | ||
using Content.Shared.Power; | ||
using Content.Shared.Power.EntitySystems; | ||
using Robust.Shared.Configuration; | ||
using Robust.Shared.Prototypes; | ||
|
||
namespace Content.Shared.Materials; | ||
|
||
public abstract class SharedMaterialSiloSystem : EntitySystem | ||
{ | ||
[Dependency] protected readonly SharedDeviceLinkSystem DeviceLink = default!; | ||
|
||
[Dependency] private readonly SharedPowerReceiverSystem _powerReceiver = default!; | ||
[Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!; | ||
|
||
[Dependency] private readonly IConfigurationManager _cfg = default!; | ||
|
||
private bool _siloEnabled; | ||
|
||
protected ProtoId<SourcePortPrototype> SourcePort = "MaterialSilo"; | ||
protected ProtoId<SinkPortPrototype> SinkPort = "MaterialSiloUtilizer"; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
_cfg.OnValueChanged(CCVars.SiloEnabled, enabled => _siloEnabled = enabled, true); | ||
|
||
SubscribeLocalEvent<MaterialSiloComponent, NewLinkEvent>(OnNewLink); | ||
SubscribeLocalEvent<MaterialSiloComponent, PowerChangedEvent>(OnPowerChanged); | ||
SubscribeLocalEvent<MaterialSiloUtilizerComponent, PortDisconnectedEvent>(OnPortDisconnected); | ||
} | ||
|
||
private void OnPortDisconnected(Entity<MaterialSiloUtilizerComponent> ent, ref PortDisconnectedEvent args) | ||
{ | ||
if (args.Port != SinkPort) | ||
return; | ||
|
||
ent.Comp.Silo = null; | ||
Dirty(ent); | ||
} | ||
|
||
private void OnNewLink(Entity<MaterialSiloComponent> ent, ref NewLinkEvent args) | ||
{ | ||
if (args.SinkPort != SinkPort || args.SourcePort != SourcePort | ||
|| !TryComp(args.Sink, out MaterialSiloUtilizerComponent? utilizer)) | ||
return; | ||
|
||
if (utilizer.Silo != null) | ||
DeviceLink.RemoveSinkFromSource(utilizer.Silo.Value, args.Sink); | ||
|
||
if (TryComp(args.Sink, out MaterialStorageComponent? utilizerStorage) | ||
&& utilizerStorage.Storage.Count != 0 | ||
&& TryComp(ent, out MaterialStorageComponent? siloStorage)) | ||
{ | ||
foreach (var material in utilizerStorage.Storage.Keys.ToArray()) | ||
{ | ||
var materialAmount = utilizerStorage.Storage.GetValueOrDefault(material, 0); | ||
if (_materialStorage.TryChangeMaterialAmount(ent, material, materialAmount, siloStorage)) | ||
_materialStorage.TryChangeMaterialAmount(args.Sink, material, -materialAmount, utilizerStorage); | ||
} | ||
} | ||
|
||
utilizer.Silo = ent; | ||
Dirty(args.Sink, utilizer); | ||
} | ||
|
||
private void OnPowerChanged(Entity<MaterialSiloComponent> ent, ref PowerChangedEvent args) | ||
{ | ||
if (!TryComp(ent, out MaterialStorageComponent? siloStorage)) | ||
return; | ||
|
||
var siloUtilizerQuery = AllEntityQuery<MaterialSiloUtilizerComponent, MaterialStorageComponent>(); | ||
|
||
while (siloUtilizerQuery.MoveNext(out var utilizerUid, out var utilizer, out var utilizerStorage)) | ||
{ | ||
if (utilizer.Silo != ent) | ||
continue; | ||
|
||
foreach (var material in utilizerStorage.Storage.Keys.ToArray()) | ||
{ | ||
var materialAmount = utilizerStorage.Storage.GetValueOrDefault(material, 0); | ||
if (!_materialStorage.TryChangeMaterialAmount(ent, material, materialAmount, siloStorage)) | ||
continue; | ||
|
||
utilizerStorage.Storage[material] -= materialAmount; | ||
|
||
var ev = new MaterialAmountChangedEvent(); | ||
RaiseLocalEvent(utilizerUid, ref ev); | ||
|
||
Dirty(utilizerUid, utilizerStorage); | ||
} | ||
} | ||
} | ||
|
||
public int GetSiloMaterialAmount(EntityUid machine, string material, MaterialSiloUtilizerComponent? utilizer = null) | ||
{ | ||
var silo = GetSiloStorage(machine, utilizer); | ||
return silo == null ? 0 : silo.Value.Comp.Storage.GetValueOrDefault(material, 0); | ||
} | ||
|
||
public int GetSiloTotalMaterialAmount(EntityUid machine, MaterialSiloUtilizerComponent? utilizer = null) | ||
{ | ||
var silo = GetSiloStorage(machine, utilizer); | ||
return silo == null ? 0 : silo.Value.Comp.Storage.Values.Sum(); | ||
} | ||
|
||
public Entity<MaterialStorageComponent>? GetSiloStorage(EntityUid machine, MaterialSiloUtilizerComponent? utilizer = null) | ||
{ | ||
if (!_siloEnabled || !Resolve(machine, ref utilizer) | ||
|| !TryComp(utilizer.Silo, out MaterialStorageComponent? storage) | ||
|| !_powerReceiver.IsPowered(utilizer.Silo.Value)) | ||
return null; | ||
|
||
return (utilizer.Silo.Value, storage); | ||
} | ||
} |
Oops, something went wrong.