2020-05-24 19:44:22 +00:00
using System ;
2020-05-27 20:05:12 -03:00
using System.Runtime.Remoting ;
2020-05-11 15:26:07 +02:00
using Content.Server.GameObjects.Components.Chemistry ;
using Content.Server.GameObjects.EntitySystems ;
2020-05-23 14:26:41 +02:00
using Content.Server.Interfaces ;
2020-05-27 20:05:12 -03:00
using Content.Server.Interfaces.Chat ;
using Content.Server.Interfaces.GameObjects ;
2020-05-11 15:26:07 +02:00
using Content.Shared.Chemistry ;
using Content.Shared.GameObjects ;
using Content.Shared.GameObjects.Components.Interactable ;
2020-05-27 20:05:12 -03:00
using Content.Shared.Interfaces ;
2020-05-11 15:26:07 +02:00
using Robust.Server.GameObjects ;
using Robust.Shared.GameObjects ;
using Robust.Shared.Interfaces.GameObjects ;
using Robust.Shared.Interfaces.Random ;
using Robust.Shared.IoC ;
using Robust.Shared.Localization ;
using Robust.Shared.Utility ;
using Robust.Shared.ViewVariables ;
namespace Content.Server.GameObjects.Components.Interactable
{
[RegisterComponent]
[ComponentReference(typeof(ToolComponent))]
2020-05-27 20:05:12 -03:00
public class WelderComponent : ToolComponent , IExamine , IUse , ISuicideAct
2020-05-11 15:26:07 +02:00
{
#pragma warning disable 649
[Dependency] private IEntitySystemManager _entitySystemManager ;
2020-05-23 14:26:41 +02:00
[Dependency] private IServerNotifyManager _notifyManager ;
2020-05-11 15:26:07 +02:00
#pragma warning restore 649
public override string Name = > "Welder" ;
public override uint? NetID = > ContentNetIDs . WELDER ;
/// <summary>
/// Default Cost of using the welder fuel for an action
/// </summary>
public const float DefaultFuelCost = 10 ;
/// <summary>
/// Rate at which we expunge fuel from ourselves when activated
/// </summary>
public const float FuelLossRate = 0.5f ;
private bool _welderLit = false ;
private WelderSystem _welderSystem ;
private SpriteComponent _spriteComponent ;
private SolutionComponent _solutionComponent ;
[ViewVariables]
public float Fuel = > _solutionComponent ? . Solution . GetReagentQuantity ( "chem.WeldingFuel" ) . Float ( ) ? ? 0f ;
[ViewVariables]
public float FuelCapacity = > _solutionComponent ? . MaxVolume . Float ( ) ? ? 0f ;
/// <summary>
/// Status of welder, whether it is ignited
/// </summary>
[ViewVariables]
public bool WelderLit
{
get = > _welderLit ;
private set
{
_welderLit = value ;
Dirty ( ) ;
}
}
public override void Initialize ( )
{
base . Initialize ( ) ;
2020-05-19 13:55:52 +02:00
AddQuality ( ToolQuality . Welding ) ;
2020-05-11 15:26:07 +02:00
_welderSystem = _entitySystemManager . GetEntitySystem < WelderSystem > ( ) ;
Owner . TryGetComponent ( out _solutionComponent ) ;
Owner . TryGetComponent ( out _spriteComponent ) ;
}
public override ComponentState GetComponentState ( )
{
return new WelderComponentState ( FuelCapacity , Fuel , WelderLit ) ;
}
2020-05-19 13:55:52 +02:00
public override bool UseTool ( IEntity user , IEntity target , ToolQuality toolQualityNeeded )
2020-05-11 15:26:07 +02:00
{
2020-05-19 13:55:52 +02:00
var canUse = base . UseTool ( user , target , toolQualityNeeded ) ;
2020-05-23 14:26:41 +02:00
return toolQualityNeeded . HasFlag ( ToolQuality . Welding ) ? canUse & & TryWeld ( DefaultFuelCost , user ) : canUse ;
2020-05-11 15:26:07 +02:00
}
2020-05-19 13:55:52 +02:00
public bool UseTool ( IEntity user , IEntity target , ToolQuality toolQualityNeeded , float fuelConsumed )
2020-05-17 12:58:54 +02:00
{
2020-05-23 14:26:41 +02:00
return base . UseTool ( user , target , toolQualityNeeded ) & & TryWeld ( fuelConsumed , user ) ;
2020-05-17 12:58:54 +02:00
}
2020-05-27 20:05:12 -03:00
private bool TryWeld ( float value , IEntity user = null , bool silent = false )
2020-05-11 15:26:07 +02:00
{
2020-05-23 14:26:41 +02:00
if ( ! WelderLit )
{
2020-05-27 20:05:12 -03:00
if ( ! silent ) _notifyManager . PopupMessage ( Owner , user , Loc . GetString ( "The welder is turned off!" ) ) ;
2020-05-23 14:26:41 +02:00
return false ;
}
if ( ! CanWeld ( value ) )
{
2020-05-27 20:05:12 -03:00
if ( ! silent ) _notifyManager . PopupMessage ( Owner , user , Loc . GetString ( "The welder does not have enough fuel for that!" ) ) ;
return false ;
2020-05-23 14:26:41 +02:00
}
if ( _solutionComponent = = null )
2020-05-11 15:26:07 +02:00
return false ;
return _solutionComponent . TryRemoveReagent ( "chem.WeldingFuel" , ReagentUnit . New ( value ) ) ;
}
2020-05-19 13:55:52 +02:00
private bool CanWeld ( float value )
2020-05-11 15:26:07 +02:00
{
2020-05-19 13:55:52 +02:00
return Fuel > value | | Qualities ! = ToolQuality . Welding ;
2020-05-11 15:26:07 +02:00
}
2020-05-19 13:55:52 +02:00
private bool CanLitWelder ( )
2020-05-11 15:26:07 +02:00
{
2020-05-19 13:55:52 +02:00
return Fuel > 0 | | Qualities ! = ToolQuality . Welding ;
2020-05-11 15:26:07 +02:00
}
/// <summary>
/// Deactivates welding tool if active, activates welding tool if possible
/// </summary>
2020-05-23 14:26:41 +02:00
private bool ToggleWelderStatus ( IEntity user = null )
2020-05-11 15:26:07 +02:00
{
2020-05-24 19:44:22 +00:00
var item = Owner . GetComponent < ItemComponent > ( ) ;
2020-05-11 15:26:07 +02:00
if ( WelderLit )
{
WelderLit = false ;
// Layer 1 is the flame.
2020-05-24 19:44:22 +00:00
item . EquippedPrefix = "off" ;
2020-05-11 15:26:07 +02:00
_spriteComponent . LayerSetVisible ( 1 , false ) ;
PlaySoundCollection ( "WelderOff" , - 5 ) ;
_welderSystem . Unsubscribe ( this ) ;
return true ;
}
2020-05-23 14:26:41 +02:00
if ( ! CanLitWelder ( ) )
{
_notifyManager . PopupMessage ( Owner , user , Loc . GetString ( "The welder has no fuel left!" ) ) ;
return false ;
}
2020-05-11 15:26:07 +02:00
WelderLit = true ;
2020-05-24 19:44:22 +00:00
item . EquippedPrefix = "on" ;
2020-05-11 15:26:07 +02:00
_spriteComponent . LayerSetVisible ( 1 , true ) ;
PlaySoundCollection ( "WelderOn" , - 5 ) ;
_welderSystem . Subscribe ( this ) ;
return true ;
}
2020-05-17 12:58:54 +02:00
public bool UseEntity ( UseEntityEventArgs eventArgs )
2020-05-11 15:26:07 +02:00
{
2020-05-23 14:26:41 +02:00
return ToggleWelderStatus ( eventArgs . User ) ;
2020-05-11 15:26:07 +02:00
}
2020-05-31 19:29:06 +01:00
public void Examine ( FormattedMessage message , bool inDetailsRange )
2020-05-11 15:26:07 +02:00
{
if ( WelderLit )
{
message . AddMarkup ( Loc . GetString ( "[color=orange]Lit[/color]\n" ) ) ;
}
else
{
message . AddText ( Loc . GetString ( "Not lit\n" ) ) ;
}
2020-05-31 19:29:06 +01:00
if ( inDetailsRange )
{
message . AddMarkup ( Loc . GetString ( "Fuel: [color={0}]{1}/{2}[/color]." ,
Fuel < FuelCapacity / 4f ? "darkorange" : "orange" , Math . Round ( Fuel ) , FuelCapacity ) ) ;
}
2020-05-11 15:26:07 +02:00
}
public void OnUpdate ( float frameTime )
{
2020-05-19 13:55:52 +02:00
if ( ! HasQuality ( ToolQuality . Welding ) | | ! WelderLit )
2020-05-11 15:26:07 +02:00
return ;
_solutionComponent . TryRemoveReagent ( "chem.WeldingFuel" , ReagentUnit . New ( FuelLossRate * frameTime ) ) ;
if ( Fuel = = 0 )
ToggleWelderStatus ( ) ;
Dirty ( ) ;
}
2020-05-27 20:05:12 -03:00
public SuicideKind Suicide ( IEntity victim , IChatManager chat )
{
if ( TryWeld ( 5 , victim , silent : true ) )
{
PlaySoundCollection ( "Welder" , - 5 ) ;
chat . EntityMe ( victim , Loc . GetString ( "welds {0:their} every orifice closed! It looks like {0:theyre} trying to commit suicide!" , victim ) ) ; //TODO: theyre macro
return SuicideKind . Heat ;
}
chat . EntityMe ( victim , Loc . GetString ( "bashes {0:themselves} with the {1}!" , victim , Owner . Name ) ) ;
return SuicideKind . Brute ;
}
2020-05-11 15:26:07 +02:00
}
}