Generalized material spawning (#12489)
This commit is contained in:
@@ -67,7 +67,6 @@ namespace Content.Server.Cloning
|
||||
SubscribeLocalEvent<CloningPodComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<CloningPodComponent, RefreshPartsEvent>(OnPartsRefreshed);
|
||||
SubscribeLocalEvent<CloningPodComponent, UpgradeExamineEvent>(OnUpgradeExamine);
|
||||
SubscribeLocalEvent<CloningPodComponent, MachineDeconstructedEvent>(OnDeconstruct);
|
||||
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
|
||||
SubscribeLocalEvent<BeingClonedComponent, MindAddedMessage>(HandleMindAdded);
|
||||
SubscribeLocalEvent<CloningPodComponent, PortDisconnectedEvent>(OnPortDisconnected);
|
||||
@@ -96,11 +95,6 @@ namespace Content.Server.Cloning
|
||||
args.AddPercentageUpgrade("cloning-pod-component-upgrade-biomass-requirement", component.BiomassRequirementMultiplier);
|
||||
}
|
||||
|
||||
private void OnDeconstruct(EntityUid uid, CloningPodComponent component, MachineDeconstructedEvent args)
|
||||
{
|
||||
_serverStackSystem.SpawnMultiple(component.MaterialCloningOutput, _material.GetMaterialAmount(uid, component.RequiredMaterial), Transform(uid).Coordinates);
|
||||
}
|
||||
|
||||
internal void TransferMindToClone(Mind.Mind mind)
|
||||
{
|
||||
if (!ClonesWaitingForMind.TryGetValue(mind, out var entity) ||
|
||||
@@ -322,8 +316,7 @@ namespace Content.Server.Cloning
|
||||
}
|
||||
_spillableSystem.SpillAt(uid, bloodSolution, "PuddleBlood");
|
||||
|
||||
var biomassStack = Spawn(clonePod.MaterialCloningOutput, transform.Coordinates);
|
||||
_stackSystem.SetCount(biomassStack, _robustRandom.Next(1, (int) (clonePod.UsedBiomass / 2.5)));
|
||||
_serverStackSystem.SpawnMultipleFromMaterial(_robustRandom.Next(1, (int) (clonePod.UsedBiomass / 2.5)), clonePod.RequiredMaterial, Transform(uid).Coordinates);
|
||||
|
||||
clonePod.UsedBiomass = 0;
|
||||
RemCompDeferred<ActiveCloningPodComponent>(uid);
|
||||
|
||||
@@ -33,12 +33,6 @@ namespace Content.Server.Cloning.Components
|
||||
[DataField("requiredMaterial", customTypeSerializer: typeof(PrototypeIdSerializer<MaterialPrototype>)), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string RequiredMaterial = "Biomass";
|
||||
|
||||
/// <summary>
|
||||
/// The entity that is spawned on machine deconstruct as well as failed cloning.
|
||||
/// </summary>
|
||||
[DataField("materialCloningOutput", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>)), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string MaterialCloningOutput = "MaterialBiomass";
|
||||
|
||||
/// <summary>
|
||||
/// The base amount of time it takes to clone a body
|
||||
/// </summary>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Shared.Materials;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Server.Stack;
|
||||
using Content.Shared.Database;
|
||||
|
||||
namespace Content.Server.Materials;
|
||||
|
||||
@@ -15,6 +17,21 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
[Dependency] private readonly StackSystem _stackSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<MaterialStorageComponent, MachineDeconstructedEvent>(OnDeconstructed);
|
||||
}
|
||||
|
||||
private void OnDeconstructed(EntityUid uid, MaterialStorageComponent component, MachineDeconstructedEvent args)
|
||||
{
|
||||
foreach (var (material, amount) in component.Storage)
|
||||
{
|
||||
_stackSystem.SpawnMultipleFromMaterial(amount, material, Transform(uid).Coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null)
|
||||
{
|
||||
|
||||
@@ -55,12 +55,6 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float YieldPerUnitMass = default;
|
||||
|
||||
/// <summary>
|
||||
/// The entity that is output by the reclaimer
|
||||
/// </summary>
|
||||
[DataField("outputEntityId", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>)), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string OutputEntityId = "MaterialBiomass";
|
||||
|
||||
/// <summary>
|
||||
/// The base yield per mass unit when no components are upgraded.
|
||||
/// </summary>
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
continue;
|
||||
}
|
||||
|
||||
_stackSystem.SpawnMultiple(reclaimer.OutputEntityId, reclaimer.CurrentExpectedYield, Transform(reclaimer.Owner).Coordinates);
|
||||
_stackSystem.SpawnMultipleFromMaterial((int) reclaimer.CurrentExpectedYield, "Biomass", Transform(reclaimer.Owner).Coordinates);
|
||||
|
||||
reclaimer.BloodReagent = null;
|
||||
reclaimer.SpawnedEntities.Clear();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Materials;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Stack
|
||||
@@ -16,6 +16,7 @@ namespace Content.Server.Stack
|
||||
public sealed class StackSystem : SharedStackSystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly SharedStackSystem _sharedStack = default!;
|
||||
|
||||
public static readonly int[] DefaultSplitAmounts = { 1, 5, 10, 20, 30, 50 };
|
||||
|
||||
@@ -87,24 +88,81 @@ namespace Content.Server.Stack
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Say you want to spawn 97 stacks of something that has a max stack count of 30.
|
||||
/// Say you want to spawn 97 units of something that has a max stack count of 30.
|
||||
/// This would spawn 3 stacks of 30 and 1 stack of 7.
|
||||
/// </summary>
|
||||
public List<EntityUid> SpawnMultiple(string entityPrototype, int amount, EntityCoordinates spawnPosition)
|
||||
public List<EntityUid> SpawnMultiple(int amount, MaterialPrototype materialProto, EntityCoordinates coordinates)
|
||||
{
|
||||
var proto = _prototypeManager.Index<EntityPrototype>(entityPrototype);
|
||||
proto.TryGetComponent<StackComponent>(out var stack);
|
||||
var maxCountPerStack = GetMaxCount(stack);
|
||||
var spawnedEnts = new List<EntityUid>();
|
||||
var list = new List<EntityUid>();
|
||||
if (amount <= 0)
|
||||
return list;
|
||||
|
||||
// At least 1 is being spawned, we'll use the first to extract otherwise inaccessible information
|
||||
// ??TODO??: Indexing the entity proto and extracting from its component registry could possibly be better?
|
||||
// it doesn't look like it would save LOC even compressing this to a single loop and I'm not sure what other issues it might introduce
|
||||
var firstSpawn = Spawn(materialProto.StackEntity, coordinates);
|
||||
list.Add(firstSpawn);
|
||||
|
||||
if (!TryComp<StackComponent>(firstSpawn, out var stack) || stack.StackTypeId == null)
|
||||
return list;
|
||||
|
||||
if (!TryComp<MaterialComponent>(firstSpawn, out var material))
|
||||
return list;
|
||||
|
||||
int maxCountPerStack = _sharedStack.GetMaxCount(stack);
|
||||
var materialPerStack = material._materials[materialProto.ID];
|
||||
|
||||
var materialPerMaxCount = maxCountPerStack * materialPerStack;
|
||||
|
||||
// no material duping for you
|
||||
if (amount < materialPerStack)
|
||||
{
|
||||
Del(firstSpawn);
|
||||
return list;
|
||||
}
|
||||
|
||||
if (amount > materialPerMaxCount)
|
||||
{
|
||||
SetCount(firstSpawn, maxCountPerStack, stack);
|
||||
amount -= materialPerMaxCount;
|
||||
} else
|
||||
{
|
||||
SetCount(firstSpawn, (amount / materialPerStack), stack);
|
||||
amount = 0;
|
||||
}
|
||||
|
||||
while (amount > 0)
|
||||
{
|
||||
var entity = Spawn(entityPrototype, spawnPosition);
|
||||
spawnedEnts.Add(entity);
|
||||
var countAmount = Math.Min(maxCountPerStack, amount);
|
||||
SetCount(entity, countAmount);
|
||||
amount -= countAmount;
|
||||
var entity = Spawn(materialProto.StackEntity, coordinates);
|
||||
list.Add(entity);
|
||||
var nextStack = Comp<StackComponent>(entity);
|
||||
if (amount > materialPerMaxCount)
|
||||
{
|
||||
SetCount(entity, materialPerMaxCount, nextStack);
|
||||
amount -= materialPerMaxCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCount(entity, (amount / materialPerStack), nextStack);
|
||||
amount = 0;
|
||||
}
|
||||
}
|
||||
return spawnedEnts;
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawn an amount of a material in stack entities. Note the 'amount' is material dependent. 1 biomass = 1 biomass in its stack,
|
||||
/// but 100 plasma = 1 sheet of plasma, etc.
|
||||
/// </summary>
|
||||
public List<EntityUid> SpawnMultipleFromMaterial(int amount, string material, EntityCoordinates coordinates)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex<MaterialPrototype>(material, out var stackType))
|
||||
{
|
||||
Logger.Error("Failed to index material prototype " + material);
|
||||
return new List<EntityUid>();
|
||||
}
|
||||
|
||||
return SpawnMultiple(amount, stackType, coordinates);
|
||||
}
|
||||
|
||||
private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetVerbsEvent<AlternativeVerb> args)
|
||||
|
||||
@@ -200,7 +200,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
{
|
||||
var cashId = proto.Cash[value];
|
||||
var amountToSpawn = (int) MathF.Floor((float) (amountRemaining / value));
|
||||
var ents = _stack.SpawnMultiple(cashId, amountToSpawn, coordinates);
|
||||
var ents = _stack.SpawnMultipleFromMaterial(amountToSpawn, cashId, coordinates);
|
||||
_hands.PickupOrDrop(buyer, ents.First());
|
||||
amountRemaining -= value * amountToSpawn;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user