Make pricing system aware of SpawnItemsOnUseComponent (#13626)

* Make appraisal tool aware of SpawnItemsOnUseComponent

* Move to SpawnItemsOnUseSystem
This commit is contained in:
Visne
2023-02-28 16:55:25 +01:00
committed by GitHub
parent 5e6a446c02
commit 7f8860187e
5 changed files with 132 additions and 44 deletions

View File

@@ -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);
}
}
}