using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using UnityEngine ;
namespace Aerostats
{
public class ModuleBalloonCase : PartModule
{
/// <summary>
/// Maximum rate at which gas can be injected in the balloon to fill it, in m^3/s at stp
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float MaxGasFillRate = 1 0 0 ;
/// <summary>
/// Maximum rate at which gas can be removed from the balloon to deflate it, in m^3/s at stp
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float MaxGasVentRate = 5 0 0 ;
/// <summary>
/// Weight, in kilograms, of 1m^3 of gas at 100kPa and 0°C
/// Helium is 179g/m^3
/// Hydrogen is 90g/m^3
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float GasDensity = 0.179f ;
/// <summary>
/// Quantity of gas that the system is trying to get inside the balloon, in m^3 at 100kPa and 0°C. If the balloon contains more gas, some will be vented, otherwise, gas will be injected.
/// If the balloon can not store the target amount of gas (because the maximum volume has been reached), the system won't try to inject more gas, to avoid venting through the security valve of the balloon.
/// </summary>
[KSPField(isPersistant = true, guiActive = true)]
[UI_FloatRange(minValue = 0, maxValue = 20000.0f, stepIncrement = 100.0f)]
public float LiftingGasTargetQuantity = 1 0 0.0f ;
/// <summary>
/// Minimum amount of gas that will be used to inflate the balloon at the beginning. The balloon can not be deflated below this amount.
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float MinimumFillQuantity = 1 0.0f ;
/// <summary>
/// Length of spring that can not be extended (no force applied under this length)
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float SpringRestLength = 1 0.0f ;
/// <summary>
/// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float SpringHardness = 5 0 0 0.0f ;
[KSPField(isPersistant = false, guiActive = true)]
public string Status ;
private bool Deployed ;
private bool Destroyed ;
/// <summary>
/// Gas quantity currently inside the balloon
/// </summary>
[KSPField(guiName = "Gas quantity", isPersistant = true, guiActive = true)]
public float LiftingGasQuantity ;
[KSPField(guiName = "Volume", isPersistant = false, guiActive = true)]
public string VolumeStatus ;
private ModuleBalloon Balloon ;
private LineRenderer Spring ;
[KSPEvent(guiActive = true, active = true, externalToEVAOnly = false, guiActiveUnfocused = true, guiName = "Deploy balloon", unfocusedRange = 5)]
public void Deploy ( )
{
if ( Destroyed | | Deployed )
return ;
Deployed = true ;
UnityEngine . Debug . Log ( "Aerostats: staged" ) ;
Util . PostDebugScreenMessage ( "staged" ) ;
AvailablePart avPart = PartLoader . Instance . parts . Single ( p = > p . name = = "heliumBalloon" ) ;
var balloonPart = ( Part ) UnityEngine . Object . Instantiate ( avPart . partPrefab ) ;
var balloonObject = balloonPart . gameObject ;
balloonObject . transform . position = part . Rigidbody . position + part . Rigidbody . transform . up * 3.0f ;
balloonObject . transform . rotation = part . Rigidbody . rotation ;
balloonPart . SetHierarchyRoot ( balloonPart ) ;
balloonPart . vessel = vessel ;
balloonPart . setParent ( part ) ;
Balloon = balloonObject . GetComponent < ModuleBalloon > ( ) ;
Balloon . part = balloonPart ;
balloonObject . SetActive ( true ) ;
GameEvents . onVesselWasModified . Fire ( vessel ) ;
Balloon . part . decouple ( ) ; // after that, the balloon becomes an independent vessel
Balloon . part . vessel . vesselName = vessel . vesselName + " balloon" ;
float initialGasQuantity = Mathf . Min ( 0.1f , part . RequestResource ( "Helium" , MinimumFillQuantity ) ) ;
Balloon . Initialize ( this , part . rigidbody . velocity , GasDensity , initialGasQuantity ) ;
LiftingGasQuantity = Balloon . LiftingGasQuantity ;
Spring = part . gameObject . AddComponent < LineRenderer > ( ) ;
Spring . useWorldSpace = true ;
Spring . material = new Material ( Shader . Find ( "VertexLit" ) ) ;
Spring . SetColors ( Color . black , Color . black ) ;
Spring . SetWidth ( 0.2f , 0.2f ) ;
Spring . SetVertexCount ( 2 ) ;
part . OnJustAboutToBeDestroyed + = OnPartDestroyed ;
}
[KSPEvent(guiActive = true, active = true, externalToEVAOnly = false, guiActiveUnfocused = true, guiName = "Cut rope", unfocusedRange = 5)]
public void Cut ( )
{
if ( Destroyed | | ! Deployed )
return ;
if ( Balloon ! = null )
{
Balloon . OnDetached ( ) ;
Balloon = null ;
}
Destroy ( Spring ) ;
Spring = null ;
part . OnJustAboutToBeDestroyed - = OnPartDestroyed ;
Destroyed = true ;
}
public override void OnStart ( PartModule . StartState state )
{
if ( ! HighLogic . LoadedSceneIsEditor & & ! HighLogic . LoadedSceneIsFlight ) { return ; }
Util . PostDebugScreenMessage ( "Aerostat loaded" ) ;
part . stagingIcon = "PARACHUTES" ;
}
private void OnPartDestroyed ( )
{
Cut ( ) ;
}
private void FixedUpdate ( )
{
if ( Destroyed )
{
Status = "separated" ;
VolumeStatus = "-" ;
return ;
}
if ( GameSettings . LAUNCH_STAGES . GetKeyDown ( ) & & vessel . isActiveVessel & & ( part . inverseStage = = Staging . CurrentStage - 1 | | Staging . CurrentStage = = 0 ) )
{
Deploy ( ) ;
}
if ( Deployed )
{
float currentMaxQuantity = Balloon . GetCurrentMaxQuantity ( ) ;
LiftingGasTargetQuantity = Math . Max ( MinimumFillQuantity , LiftingGasTargetQuantity ) ;
if ( LiftingGasTargetQuantity > Balloon . LiftingGasQuantity )
{
// inflating balloon
float stepFinalQuantity = Math . Min ( Balloon . LiftingGasQuantity + MaxGasFillRate * Time . fixedDeltaTime , Math . Min ( LiftingGasTargetQuantity , currentMaxQuantity ) ) ;
float step = Math . Max ( stepFinalQuantity - Balloon . LiftingGasQuantity , 0.0f ) ;
float stepResource = part . RequestResource ( "Helium" , step ) ;
LiftingGasQuantity = Balloon . InjectGas ( stepResource ) ;
if ( step > 0.0f )
{
Status = stepResource > 0.0f ? "inflating" : "out-of-gas" ;
}
else
{
Status = "nominal" ;
}
}
else
{
// deflating balloon
Status = LiftingGasTargetQuantity = = Balloon . LiftingGasQuantity ? "nominal" : "deflating" ;
float quantityAfterVenting = Math . Max ( Balloon . LiftingGasQuantity - MaxGasVentRate * Time . fixedDeltaTime , LiftingGasTargetQuantity ) ;
LiftingGasQuantity = Balloon . InjectGas ( quantityAfterVenting - Balloon . LiftingGasQuantity ) ;
}
if ( Balloon . IsVenting )
{
Status = "full (venting)" ;
}
VolumeStatus = Mathf . Round ( Balloon . CurrentVolume ) + " / " + Mathf . Round ( Balloon . MaxVolume ) ;
// spring between balloon and base
float restLength = SpringRestLength + Balloon . Radius ;
var springVec = Balloon . rigidbody . position - part . rigidbody . position ;
float springLength = springVec . magnitude ;
float springForceMag = 0 ;
var balloonAttachPoint = Balloon . rigidbody . position - Balloon . transform . up . normalized * Balloon . Radius ;
if ( springLength > restLength )
{
float tensingLength = springLength - restLength ;
springForceMag = tensingLength * SpringHardness ;
var springForce = springVec * ( springForceMag / springLength * 0.001f ) ;
part . rigidbody . AddForce ( springForce , ForceMode . Force ) ;
Balloon . rigidbody . AddForceAtPosition ( - springForce , balloonAttachPoint , ForceMode . Force ) ;
}
Util . PostDebugSingleScreenMessage ( "spring force" , "Spring force = " + springForceMag + "N" ) ;
}
else
{
Status = "packed" ;
VolumeStatus = "-" ;
}
}
private void LateUpdate ( )
{
if ( Destroyed )
return ;
if ( Deployed )
{
var balloonAttachPoint = Balloon . rigidbody . position - Balloon . transform . up . normalized * Balloon . Radius ;
Spring . SetPosition ( 0 , part . transform . position ) ;
Spring . SetPosition ( 1 , balloonAttachPoint ) ;
}
}
}
}