Make pricing system aware of SpawnItemsOnUseComponent (#13626)
* Make appraisal tool aware of SpawnItemsOnUseComponent * Move to SpawnItemsOnUseSystem
This commit is contained in:
@@ -3,7 +3,6 @@ using Content.Server.Administration;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.Cargo.Components;
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Stack;
|
||||
using Content.Shared.Administration;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
@@ -88,6 +87,9 @@ public sealed class PricingSystem : EntitySystem
|
||||
|
||||
private void CalculateMobPrice(EntityUid uid, MobPriceComponent component, ref PriceCalculationEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (!TryComp<BodyComponent>(uid, out var body) || !TryComp<MobStateComponent>(uid, out var state))
|
||||
{
|
||||
Logger.ErrorS("pricing", $"Tried to get the mob price of {ToPrettyString(uid)}, which has no {nameof(BodyComponent)} and no {nameof(MobStateComponent)}.");
|
||||
@@ -106,6 +108,9 @@ public sealed class PricingSystem : EntitySystem
|
||||
|
||||
private void CalculateStackPrice(EntityUid uid, StackPriceComponent component, ref PriceCalculationEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (!TryComp<StackComponent>(uid, out var stack))
|
||||
{
|
||||
Logger.ErrorS("pricing", $"Tried to get the stack price of {ToPrettyString(uid)}, which has no {nameof(StackComponent)}.");
|
||||
@@ -117,6 +122,9 @@ public sealed class PricingSystem : EntitySystem
|
||||
|
||||
private void CalculateSolutionPrice(EntityUid uid, SolutionContainerManagerComponent component, ref PriceCalculationEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
var price = 0f;
|
||||
|
||||
foreach (var solution in component.Solutions.Values)
|
||||
@@ -133,6 +141,9 @@ public sealed class PricingSystem : EntitySystem
|
||||
|
||||
private void CalculateStaticPrice(EntityUid uid, StaticPriceComponent component, ref PriceCalculationEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Price += component.Price;
|
||||
}
|
||||
|
||||
@@ -187,6 +198,9 @@ public sealed class PricingSystem : EntitySystem
|
||||
var ev = new PriceCalculationEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
|
||||
if (ev.Handled)
|
||||
return ev.Price;
|
||||
|
||||
//TODO: Add an OpaqueToAppraisal component or similar for blocking the recursive descent into containers, or preventing material pricing.
|
||||
|
||||
if (TryComp<MaterialComponent>(uid, out var material) && !HasComp<StackPriceComponent>(uid))
|
||||
@@ -249,5 +263,10 @@ public struct PriceCalculationEvent
|
||||
/// </summary>
|
||||
public double Price = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this event was already handled.
|
||||
/// </summary>
|
||||
public bool Handled = false;
|
||||
|
||||
public PriceCalculationEvent() { }
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace Content.Server.Storage.Components
|
||||
/// <summary>
|
||||
/// The list of entities to spawn, with amounts and orGroups.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DataField("items", required: true)]
|
||||
public List<EntitySpawnEntry> Items = new();
|
||||
|
||||
@@ -25,7 +24,6 @@ namespace Content.Server.Storage.Components
|
||||
/// <summary>
|
||||
/// How many uses before the item should delete itself.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DataField("uses")]
|
||||
public int Uses = 1;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Cargo.Systems;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Storage;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using static Content.Shared.Storage.EntitySpawnCollection;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
@@ -14,13 +17,47 @@ namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _hands = default!;
|
||||
[Dependency] private readonly PricingSystem _pricing = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<SpawnItemsOnUseComponent, UseInHandEvent>(OnUseInHand);
|
||||
SubscribeLocalEvent<SpawnItemsOnUseComponent, PriceCalculationEvent>(CalculatePrice, before: new[] { typeof(PricingSystem) });
|
||||
}
|
||||
|
||||
private void CalculatePrice(EntityUid uid, SpawnItemsOnUseComponent component, ref PriceCalculationEvent args)
|
||||
{
|
||||
var ungrouped = CollectOrGroups(component.Items, out var orGroups);
|
||||
|
||||
foreach (var entry in ungrouped)
|
||||
{
|
||||
var protUid = Spawn(entry.PrototypeId, MapCoordinates.Nullspace);
|
||||
|
||||
// Calculate the average price of the possible spawned items
|
||||
args.Price += _pricing.GetPrice(protUid) * entry.SpawnProbability * entry.GetAmount(getAverage: true);
|
||||
|
||||
EntityManager.DeleteEntity(protUid);
|
||||
}
|
||||
|
||||
foreach (var group in orGroups)
|
||||
{
|
||||
foreach (var entry in group.Entries)
|
||||
{
|
||||
var protUid = Spawn(entry.PrototypeId, MapCoordinates.Nullspace);
|
||||
|
||||
// Calculate the average price of the possible spawned items
|
||||
args.Price += _pricing.GetPrice(protUid) *
|
||||
(entry.SpawnProbability / group.CumulativeProbability) *
|
||||
entry.GetAmount(getAverage: true);
|
||||
|
||||
EntityManager.DeleteEntity(protUid);
|
||||
}
|
||||
}
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnUseInHand(EntityUid uid, SpawnItemsOnUseComponent component, UseInHandEvent args)
|
||||
@@ -29,7 +66,7 @@ namespace Content.Server.Storage.EntitySystems
|
||||
return;
|
||||
|
||||
var coords = Transform(args.User).Coordinates;
|
||||
var spawnEntities = EntitySpawnCollection.GetSpawns(component.Items, _random);
|
||||
var spawnEntities = GetSpawns(component.Items, _random);
|
||||
EntityUid? entityToPlaceInHands = null;
|
||||
|
||||
foreach (var proto in spawnEntities)
|
||||
@@ -50,7 +87,7 @@ namespace Content.Server.Storage.EntitySystems
|
||||
|
||||
if (entityToPlaceInHands != null)
|
||||
{
|
||||
_handsSystem.PickupOrDrop(args.User, entityToPlaceInHands.Value);
|
||||
_hands.PickupOrDrop(args.User, entityToPlaceInHands.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user