2022-06-11 18:54:41 -07:00
using System.Linq ;
2023-01-20 10:17:57 -06:00
using Content.Server.Administration.Logs ;
2022-04-15 17:20:20 -04:00
using Content.Server.AME.Components ;
2022-03-29 03:58:51 +11:00
using Content.Server.Power.Components ;
2022-04-15 17:20:20 -04:00
using Content.Server.Hands.Components ;
using Content.Server.Popups ;
using Content.Server.Tools ;
2023-01-20 10:17:57 -06:00
using Content.Shared.Database ;
2022-04-15 17:20:20 -04:00
using Content.Shared.Interaction ;
2022-07-09 02:09:52 -07:00
using Content.Shared.Popups ;
2022-04-15 17:20:20 -04:00
using Robust.Shared.Map ;
using Robust.Shared.Player ;
using Robust.Shared.Audio ;
2020-08-29 06:05:44 -05:00
using JetBrains.Annotations ;
2021-06-09 22:19:39 +02:00
namespace Content.Server.AME
2020-08-29 06:05:44 -05:00
{
[UsedImplicitly]
2022-02-16 00:23:23 -07:00
public sealed class AntimatterEngineSystem : EntitySystem
2020-08-29 06:05:44 -05:00
{
2022-04-15 17:20:20 -04:00
[Dependency] private readonly IMapManager _mapManager = default ! ;
[Dependency] private readonly PopupSystem _popupSystem = default ! ;
[Dependency] private readonly ToolSystem _toolSystem = default ! ;
2023-01-20 10:17:57 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default ! ;
2020-08-29 06:05:44 -05:00
private float _accumulatedFrameTime ;
2022-03-29 03:58:51 +11:00
private const float UpdateCooldown = 10f ;
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < AMEControllerComponent , PowerChangedEvent > ( OnAMEPowerChange ) ;
2022-04-15 17:20:20 -04:00
SubscribeLocalEvent < AMEControllerComponent , InteractUsingEvent > ( OnInteractUsing ) ;
SubscribeLocalEvent < AMEPartComponent , InteractUsingEvent > ( OnPartInteractUsing ) ;
2022-03-29 03:58:51 +11:00
}
2020-08-29 06:05:44 -05:00
public override void Update ( float frameTime )
{
base . Update ( frameTime ) ;
2022-03-29 03:58:51 +11:00
// TODO: Won't exactly work with replays I guess?
2020-08-29 06:05:44 -05:00
_accumulatedFrameTime + = frameTime ;
2022-03-29 03:58:51 +11:00
if ( _accumulatedFrameTime > = UpdateCooldown )
2020-08-29 06:05:44 -05:00
{
2021-10-18 14:58:34 +02:00
foreach ( var comp in EntityManager . EntityQuery < AMEControllerComponent > ( ) )
2020-08-29 06:05:44 -05:00
{
comp . OnUpdate ( frameTime ) ;
}
2022-03-29 03:58:51 +11:00
_accumulatedFrameTime - = UpdateCooldown ;
2020-08-29 06:05:44 -05:00
}
2022-03-29 03:58:51 +11:00
}
2020-08-29 06:05:44 -05:00
2022-10-15 15:08:15 +11:00
private static void OnAMEPowerChange ( EntityUid uid , AMEControllerComponent component , ref PowerChangedEvent args )
2022-03-29 03:58:51 +11:00
{
component . UpdateUserInterface ( ) ;
2020-08-29 06:05:44 -05:00
}
2022-04-15 17:20:20 -04:00
private void OnInteractUsing ( EntityUid uid , AMEControllerComponent component , InteractUsingEvent args )
{
if ( ! TryComp ( args . User , out HandsComponent ? hands ) )
{
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-controller-component-interact-using-no-hands-text" ) , uid , args . User ) ;
2022-04-15 17:20:20 -04:00
return ;
}
if ( HasComp < AMEFuelContainerComponent ? > ( args . Used ) )
{
if ( component . HasJar )
{
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-controller-component-interact-using-already-has-jar" ) , uid , args . User ) ;
2022-04-15 17:20:20 -04:00
}
else
{
component . JarSlot . Insert ( args . Used ) ;
2022-07-09 02:09:52 -07:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-controller-component-interact-using-success" ) , uid ,
2022-12-19 10:41:47 +13:00
args . User , PopupType . Medium ) ;
2022-04-15 17:20:20 -04:00
component . UpdateUserInterface ( ) ;
}
}
else
{
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-controller-component-interact-using-fail" ) , uid , args . User ) ;
2022-04-15 17:20:20 -04:00
}
}
private void OnPartInteractUsing ( EntityUid uid , AMEPartComponent component , InteractUsingEvent args )
{
if ( ! HasComp < HandsComponent > ( args . User ) )
{
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-part-component-interact-using-no-hands" ) , uid , args . User ) ;
2022-04-15 17:20:20 -04:00
return ;
}
if ( ! _toolSystem . HasQuality ( args . Used , component . QualityNeeded ) )
return ;
2022-06-20 12:14:35 +12:00
if ( ! _mapManager . TryGetGrid ( args . ClickLocation . GetGridUid ( EntityManager ) , out var mapGrid ) )
2022-04-15 17:20:20 -04:00
return ; // No AME in space.
var snapPos = mapGrid . TileIndicesFor ( args . ClickLocation ) ;
if ( mapGrid . GetAnchoredEntities ( snapPos ) . Any ( sc = > HasComp < AMEShieldComponent > ( sc ) ) )
{
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "ame-part-component-shielding-already-present" ) , uid , args . User ) ;
2022-04-15 17:20:20 -04:00
return ;
}
var ent = EntityManager . SpawnEntity ( "AMEShielding" , mapGrid . GridTileToLocal ( snapPos ) ) ;
2023-01-20 10:17:57 -06:00
_adminLogger . Add ( LogType . Construction , LogImpact . Low , $"{ToPrettyString(args.User):player} unpacked {ToPrettyString(ent)} at {Transform(ent).Coordinates} from {ToPrettyString(uid)}" ) ;
2022-06-12 19:45:47 -04:00
SoundSystem . Play ( component . UnwrapSound . GetSound ( ) , Filter . Pvs ( uid ) , uid ) ;
2022-04-15 17:20:20 -04:00
EntityManager . QueueDeleteEntity ( uid ) ;
}
2020-08-29 06:05:44 -05:00
}
}