2022-12-24 18:59:49 -06:00
using Content.Server.Administration.Logs ;
2023-02-28 16:55:25 +01:00
using Content.Server.Cargo.Systems ;
2021-07-25 08:41:50 -07:00
using Content.Server.Storage.Components ;
2022-12-24 18:59:49 -06:00
using Content.Shared.Database ;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems ;
2022-03-13 01:33:23 +13:00
using Content.Shared.Interaction.Events ;
2021-07-25 08:41:50 -07:00
using Robust.Shared.Audio ;
2023-02-28 16:55:25 +01:00
using Robust.Shared.Map ;
2021-07-25 08:41:50 -07:00
using Robust.Shared.Player ;
using Robust.Shared.Random ;
2023-02-28 16:55:25 +01:00
using static Content . Shared . Storage . EntitySpawnCollection ;
2021-07-25 08:41:50 -07:00
namespace Content.Server.Storage.EntitySystems
{
2022-02-16 00:23:23 -07:00
public sealed class SpawnItemsOnUseSystem : EntitySystem
2021-07-25 08:41:50 -07:00
{
[Dependency] private readonly IRobustRandom _random = default ! ;
2022-12-24 18:59:49 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default ! ;
2023-02-28 16:55:25 +01:00
[Dependency] private readonly SharedHandsSystem _hands = default ! ;
[Dependency] private readonly PricingSystem _pricing = default ! ;
2021-07-25 08:41:50 -07:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < SpawnItemsOnUseComponent , UseInHandEvent > ( OnUseInHand ) ;
2023-02-28 16:55:25 +01:00
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 ;
2021-07-25 08:41:50 -07:00
}
private void OnUseInHand ( EntityUid uid , SpawnItemsOnUseComponent component , UseInHandEvent args )
{
if ( args . Handled )
return ;
2023-08-26 09:29:15 -07:00
// If starting with zero or less uses, this component is a no-op
if ( component . Uses < = 0 )
return ;
2022-03-28 09:58:13 -07:00
var coords = Transform ( args . User ) . Coordinates ;
2023-02-28 16:55:25 +01:00
var spawnEntities = GetSpawns ( component . Items , _random ) ;
2021-12-26 15:32:45 +13:00
EntityUid ? entityToPlaceInHands = null ;
2021-07-25 08:41:50 -07:00
2022-03-28 09:58:13 -07:00
foreach ( var proto in spawnEntities )
{
entityToPlaceInHands = Spawn ( proto , coords ) ;
2023-10-19 12:34:31 -07:00
_adminLogger . Add ( LogType . EntitySpawn , LogImpact . Low , $"{ToPrettyString(args.User)} used {ToPrettyString(uid)} which spawned {ToPrettyString(entityToPlaceInHands.Value)}" ) ;
2021-07-25 08:41:50 -07:00
}
if ( component . Sound ! = null )
2023-10-29 15:29:30 +11:00
SoundSystem . Play ( component . Sound . GetSound ( ) , Filter . Pvs ( uid ) , uid ) ;
2021-07-25 08:41:50 -07:00
component . Uses - - ;
2023-08-26 09:29:15 -07:00
// Delete entity only if component was successfully used
if ( component . Uses < = 0 )
2021-07-25 08:41:50 -07:00
{
args . Handled = true ;
2021-12-05 18:09:01 +01:00
EntityManager . DeleteEntity ( uid ) ;
2021-07-25 08:41:50 -07:00
}
2022-03-17 20:13:31 +13:00
if ( entityToPlaceInHands ! = null )
2021-07-25 08:41:50 -07:00
{
2023-02-28 16:55:25 +01:00
_hands . PickupOrDrop ( args . User , entityToPlaceInHands . Value ) ;
2021-07-25 08:41:50 -07:00
}
}
}
}