cacontinuousfillbrakes.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. class CAContinuousFillBrakes : CAContinuousBase
  2. {
  3. protected float m_ItemQuantity;
  4. protected float m_SpentQuantity;
  5. protected float m_SpentQuantity_total;
  6. protected float m_EmptySpace; //basically free capacity
  7. protected float m_TimeElpased;
  8. protected float m_QuantityUsedPerSecond;
  9. protected float m_AdjustedQuantityUsedPerSecond;
  10. protected float m_DefaultTimeStep;
  11. protected ref Param1<float> m_SpentUnits;
  12. protected PlayerBase m_Player;
  13. void CAContinuousFillBrakes( float quantity_used_per_second, float time_to_progress )
  14. {
  15. m_QuantityUsedPerSecond = quantity_used_per_second;
  16. m_DefaultTimeStep = time_to_progress;
  17. }
  18. //---------------------------------------------------------------------------
  19. override void Setup( ActionData action_data )
  20. {
  21. m_Player = action_data.m_Player;
  22. Car car = Car.Cast(action_data.m_Target.GetObject());
  23. if ( !car )
  24. return;
  25. m_TimeElpased = 0;
  26. m_SpentQuantity = 0;
  27. if ( !m_SpentUnits )
  28. {
  29. m_SpentUnits = new Param1<float>( 0 );
  30. }
  31. else
  32. {
  33. m_SpentUnits.param1 = 0;
  34. }
  35. float oilCapacity = car.GetFluidCapacity( CarFluid.BRAKE );
  36. float currentOil = car.GetFluidFraction( CarFluid.BRAKE );
  37. currentOil = currentOil * oilCapacity;
  38. m_EmptySpace = (oilCapacity - currentOil) * 1000;
  39. m_ItemQuantity = action_data.m_MainItem.GetQuantity();
  40. if ( m_EmptySpace <= m_ItemQuantity )
  41. m_ItemQuantity = m_EmptySpace;
  42. }
  43. //---------------------------------------------------------------------------
  44. override int Execute( ActionData action_data )
  45. {
  46. Car car = Car.Cast(action_data.m_Target.GetObject());
  47. if ( !car )
  48. return UA_ERROR;
  49. if ( !action_data.m_Player )
  50. {
  51. return UA_ERROR;
  52. }
  53. if ( m_ItemQuantity <= 0 )
  54. {
  55. return UA_FINISHED;
  56. }
  57. else
  58. {
  59. if ( m_SpentQuantity_total < m_ItemQuantity )
  60. {
  61. m_AdjustedQuantityUsedPerSecond = m_QuantityUsedPerSecond;//removed softskills
  62. m_SpentQuantity += m_AdjustedQuantityUsedPerSecond * action_data.m_Player.GetDeltaT();
  63. m_TimeElpased += action_data.m_Player.GetDeltaT();
  64. if ( m_TimeElpased >= m_DefaultTimeStep )
  65. {
  66. CalcAndSetQuantity( action_data );
  67. m_TimeElpased = 0;
  68. //Setup(action_data); //reset data after repeat
  69. }
  70. return UA_PROCESSING;
  71. }
  72. else
  73. {
  74. CalcAndSetQuantity( action_data );
  75. OnCompletePogress(action_data);
  76. return UA_FINISHED;
  77. }
  78. }
  79. }
  80. //---------------------------------------------------------------------------
  81. override int Cancel( ActionData action_data )
  82. {
  83. if ( !action_data.m_Player )
  84. {
  85. return UA_ERROR;
  86. }
  87. CalcAndSetQuantity( action_data );
  88. return UA_CANCEL;
  89. }
  90. //---------------------------------------------------------------------------
  91. override float GetProgress()
  92. {
  93. if ( m_ItemQuantity <= 0 )
  94. return 1;
  95. return -(m_SpentQuantity_total / m_ItemQuantity);
  96. }
  97. //---------------------------------------------------------------------------
  98. void CalcAndSetQuantity( ActionData action_data )
  99. {
  100. m_SpentQuantity_total += m_SpentQuantity;
  101. if ( m_SpentUnits )
  102. {
  103. m_SpentUnits.param1 = m_SpentQuantity;
  104. SetACData(m_SpentUnits);
  105. }
  106. if ( GetGame().IsServer() )
  107. {
  108. if ( action_data.m_MainItem ) // Item EngineOil gets deleted after full consumption
  109. action_data.m_MainItem.AddQuantity( -m_SpentQuantity );
  110. Car car = Car.Cast(action_data.m_Target.GetObject());
  111. car.Fill( CarFluid.BRAKE, (m_SpentQuantity * 0.001) );
  112. }
  113. m_SpentQuantity = 0;
  114. }
  115. }