* eye on the prize * OnStackInteractUsing, TryMergeStacks, TryMergeToHands, TryMergeToContacts * namespace * Use, get count, getMaxCount * component access * add regions, mark TODO * obsolete TryAdd, public TryMergeStacks * GetMaxCount * event handlers * event handlers * SetCount * client server event handlers * move to shared * Revert "move to shared" This reverts commit 45540a2d6b8e1e6d2a8f83a584267776c7edcd73. * misc changes to shared * split * spawn and SpawnNextToOrDrop * SpawnMultipleAtPosition, SpawnMultipleNextToOrDrop, CalculateSpawns, general server cleanup * Rename Use to TryUse. * Small misc changes * Remove obsolete functions * Remove some SetCount calls * Partialize * small misc change * don't nuke the git dif with the namespace block * Comments and reordering * touchup to UpdateLingering * Summary comment for StackStatusControl * Last pass * Actual last pass (for now) * I know myself too well * fixup * goodbye lingering * fixes * review * fix test * second look * fix test * forgot * remove early comp getting --------- Co-authored-by: iaada <iaada@users.noreply.github.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
61 lines
2.5 KiB
C#
61 lines
2.5 KiB
C#
using Robust.Shared.Random;
|
|
using Content.Shared.Stacks;
|
|
using Content.Shared.Prototypes;
|
|
using Content.Shared.VendingMachines;
|
|
|
|
namespace Content.Server.Destructible.Thresholds.Behaviors
|
|
{
|
|
/// <summary>
|
|
/// Spawns a portion of the total items from one of the canRestock
|
|
/// inventory entries on a VendingMachineRestock component.
|
|
/// </summary>
|
|
[Serializable]
|
|
[DataDefinition]
|
|
public sealed partial class DumpRestockInventory: IThresholdBehavior
|
|
{
|
|
/// <summary>
|
|
/// The percent of each inventory entry that will be salvaged
|
|
/// upon destruction of the package.
|
|
/// </summary>
|
|
[DataField("percent", required: true)]
|
|
public float Percent = 0.5f;
|
|
|
|
[DataField("offset")]
|
|
public float Offset { get; set; } = 0.5f;
|
|
|
|
public void Execute(EntityUid owner, DestructibleSystem system, EntityUid? cause = null)
|
|
{
|
|
if (!system.EntityManager.TryGetComponent<VendingMachineRestockComponent>(owner, out var packagecomp) ||
|
|
!system.EntityManager.TryGetComponent<TransformComponent>(owner, out var xform))
|
|
return;
|
|
|
|
var randomInventory = system.Random.Pick(packagecomp.CanRestock);
|
|
|
|
if (!system.PrototypeManager.TryIndex(randomInventory, out VendingMachineInventoryPrototype? packPrototype))
|
|
return;
|
|
|
|
foreach (var (entityId, count) in packPrototype.StartingInventory)
|
|
{
|
|
var toSpawn = (int) Math.Round(count * Percent);
|
|
|
|
if (toSpawn == 0) continue;
|
|
|
|
if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.EntityManager.ComponentFactory))
|
|
{
|
|
var spawned = system.EntityManager.SpawnEntity(entityId, xform.Coordinates.Offset(system.Random.NextVector2(-Offset, Offset)));
|
|
system.StackSystem.SetCount((spawned, null), toSpawn);
|
|
system.EntityManager.GetComponent<TransformComponent>(spawned).LocalRotation = system.Random.NextAngle();
|
|
}
|
|
else
|
|
{
|
|
for (var i = 0; i < toSpawn; i++)
|
|
{
|
|
var spawned = system.EntityManager.SpawnEntity(entityId, xform.Coordinates.Offset(system.Random.NextVector2(-Offset, Offset)));
|
|
system.EntityManager.GetComponent<TransformComponent>(spawned).LocalRotation = system.Random.NextAngle();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|