2024-06-30 10:04:27 +03:00
using System.Diagnostics.CodeAnalysis ;
2023-08-07 09:20:32 -05:00
using Content.Server.Administration.Logs ;
2023-08-13 21:55:06 -04:00
using Content.Server.Atmos.EntitySystems ;
2022-10-23 00:46:28 +02:00
using Content.Server.Body.Systems ;
2023-12-29 04:47:43 -08:00
using Content.Server.Chemistry.Containers.EntitySystems ;
2021-11-09 21:24:35 +01:00
using Content.Server.Construction ;
2021-09-15 03:07:37 +10:00
using Content.Server.Destructible.Thresholds ;
2022-04-01 15:39:26 +13:00
using Content.Server.Destructible.Thresholds.Behaviors ;
using Content.Server.Destructible.Thresholds.Triggers ;
2021-11-09 21:24:35 +01:00
using Content.Server.Explosion.EntitySystems ;
2022-10-23 00:46:28 +02:00
using Content.Server.Fluids.EntitySystems ;
2022-02-17 15:39:56 +13:00
using Content.Server.Stack ;
2021-09-15 03:07:37 +10:00
using Content.Shared.Damage ;
2023-08-07 09:20:32 -05:00
using Content.Shared.Database ;
2022-10-23 00:46:28 +02:00
using Content.Shared.Destructible ;
2022-04-01 15:39:26 +13:00
using Content.Shared.FixedPoint ;
2020-12-23 13:34:57 +01:00
using JetBrains.Annotations ;
2023-11-27 22:12:34 +11:00
using Robust.Server.Audio ;
2021-02-11 01:13:03 -08:00
using Robust.Server.GameObjects ;
2023-10-11 02:18:49 -07:00
using Robust.Shared.Containers ;
2022-02-17 15:39:56 +13:00
using Robust.Shared.Prototypes ;
2021-02-11 01:13:03 -08:00
using Robust.Shared.Random ;
2023-12-29 04:47:43 -08:00
using System.Linq ;
2020-12-23 13:34:57 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Destructible
2020-12-23 13:34:57 +01:00
{
[UsedImplicitly]
2022-05-03 01:43:25 +03:00
public sealed class DestructibleSystem : SharedDestructibleSystem
2020-12-23 13:34:57 +01:00
{
[Dependency] public readonly IRobustRandom Random = default ! ;
2021-11-09 21:24:35 +01:00
public new IEntityManager EntityManager = > base . EntityManager ;
2023-08-13 21:55:06 -04:00
[Dependency] public readonly AtmosphereSystem AtmosphereSystem = default ! ;
2021-11-09 21:24:35 +01:00
[Dependency] public readonly AudioSystem AudioSystem = default ! ;
2022-10-23 00:46:28 +02:00
[Dependency] public readonly BodySystem BodySystem = default ! ;
2021-11-09 21:24:35 +01:00
[Dependency] public readonly ConstructionSystem ConstructionSystem = default ! ;
[Dependency] public readonly ExplosionSystem ExplosionSystem = default ! ;
2022-02-17 15:39:56 +13:00
[Dependency] public readonly StackSystem StackSystem = default ! ;
2022-06-01 01:39:06 -07:00
[Dependency] public readonly TriggerSystem TriggerSystem = default ! ;
2022-09-14 10:15:54 -07:00
[Dependency] public readonly SolutionContainerSystem SolutionContainerSystem = default ! ;
2023-04-10 15:37:03 +10:00
[Dependency] public readonly PuddleSystem PuddleSystem = default ! ;
2023-10-11 02:18:49 -07:00
[Dependency] public readonly SharedContainerSystem ContainerSystem = default ! ;
2022-02-17 15:39:56 +13:00
[Dependency] public readonly IPrototypeManager PrototypeManager = default ! ;
[Dependency] public readonly IComponentFactory ComponentFactory = default ! ;
2023-08-07 09:20:32 -05:00
[Dependency] public readonly IAdminLogManager _adminLogger = default ! ;
2021-09-15 03:07:37 +10:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < DestructibleComponent , DamageChangedEvent > ( Execute ) ;
}
/// <summary>
/// Check if any thresholds were reached. if they were, execute them.
/// </summary>
public void Execute ( EntityUid uid , DestructibleComponent component , DamageChangedEvent args )
{
foreach ( var threshold in component . Thresholds )
{
if ( threshold . Reached ( args . Damageable , this ) )
{
2022-06-22 09:53:41 +10:00
RaiseLocalEvent ( uid , new DamageThresholdReached ( component , threshold ) , true ) ;
2021-09-15 03:07:37 +10:00
2023-08-07 09:20:32 -05:00
// Convert behaviors into string for logs
var triggeredBehaviors = string . Join ( ", " , threshold . Behaviors . Select ( b = >
{
if ( b is DoActsBehavior doActsBehavior )
{
return $"{b.GetType().Name}:{doActsBehavior.Acts.ToString()}" ;
}
return b . GetType ( ) . Name ;
} ) ) ;
if ( args . Origin ! = null )
{
_adminLogger . Add ( LogType . Damaged , LogImpact . Medium ,
$"{ToPrettyString(args.Origin.Value):actor} caused {ToPrettyString(uid):subject} to trigger [{triggeredBehaviors}]" ) ;
}
else
{
_adminLogger . Add ( LogType . Damaged , LogImpact . Medium ,
$"Unknown damage source caused {ToPrettyString(uid):subject} to trigger [{triggeredBehaviors}]" ) ;
}
2023-02-10 17:45:38 -06:00
threshold . Execute ( uid , this , EntityManager , args . Origin ) ;
2021-09-15 03:07:37 +10:00
}
2022-01-18 20:48:17 +13:00
// if destruction behavior (or some other deletion effect) occurred, don't run other triggers.
if ( EntityManager . IsQueuedForDeletion ( uid ) | | Deleted ( uid ) )
return ;
2021-09-15 03:07:37 +10:00
}
}
2022-04-01 15:39:26 +13:00
2024-06-30 10:04:27 +03:00
public bool TryGetDestroyedAt ( Entity < DestructibleComponent ? > ent , [ NotNullWhen ( true ) ] out FixedPoint2 ? destroyedAt )
{
destroyedAt = null ;
if ( ! Resolve ( ent , ref ent . Comp , false ) )
return false ;
destroyedAt = DestroyedAt ( ent , ent . Comp ) ;
return true ;
}
2022-04-01 15:39:26 +13:00
// FFS this shouldn't be this hard. Maybe this should just be a field of the destructible component. Its not
// like there is currently any entity that is NOT just destroyed upon reaching a total-damage value.
/// <summary>
/// Figure out how much damage an entity needs to have in order to be destroyed.
/// </summary>
/// <remarks>
/// This assumes that this entity has some sort of destruction or breakage behavior triggered by a
/// total-damage threshold.
/// </remarks>
public FixedPoint2 DestroyedAt ( EntityUid uid , DestructibleComponent ? destructible = null )
{
if ( ! Resolve ( uid , ref destructible , logMissing : false ) )
return FixedPoint2 . MaxValue ;
// We have nested for loops here, but the vast majority of components only have one threshold with 1-3 behaviors.
// Really, this should probably just be a property of the damageable component.
var damageNeeded = FixedPoint2 . MaxValue ;
foreach ( var threshold in destructible . Thresholds )
{
if ( threshold . Trigger is not DamageTrigger trigger )
continue ;
foreach ( var behavior in threshold . Behaviors )
{
if ( behavior is DoActsBehavior actBehavior & &
actBehavior . HasAct ( ThresholdActs . Destruction | ThresholdActs . Breakage ) )
{
damageNeeded = Math . Min ( damageNeeded . Float ( ) , trigger . Damage ) ;
}
}
}
return damageNeeded ;
}
2021-09-15 03:07:37 +10:00
}
// Currently only used for destructible integration tests. Unless other uses are found for this, maybe this should just be removed and the tests redone.
/// <summary>
/// Event raised when a <see cref="DamageThreshold"/> is reached.
/// </summary>
2022-02-16 00:23:23 -07:00
public sealed class DamageThresholdReached : EntityEventArgs
2021-09-15 03:07:37 +10:00
{
public readonly DestructibleComponent Parent ;
public readonly DamageThreshold Threshold ;
public DamageThresholdReached ( DestructibleComponent parent , DamageThreshold threshold )
{
Parent = parent ;
Threshold = threshold ;
}
2020-12-23 13:34:57 +01:00
}
2021-02-05 13:41:05 +01:00
}