cacontinuouswaterslot.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. class CAContinuousWaterSlot : CAContinuousQuantity
  2. {
  3. protected float m_PlantThirstyness;
  4. protected float m_TimeToComplete;
  5. protected float m_SpentQuantityTotal;
  6. protected float m_StartQuantity;
  7. protected Slot m_Slot;
  8. void CAContinuousWaterSlot( float quantity_used_per_second )
  9. {
  10. m_QuantityUsedPerSecond = quantity_used_per_second;
  11. }
  12. override void Setup( ActionData action_data )
  13. {
  14. GardenBase target_GB;
  15. if ( Class.CastTo(target_GB, action_data.m_Target.GetObject()) )
  16. {
  17. m_SpentQuantity = 0;
  18. m_StartQuantity = action_data.m_MainItem.GetQuantity();
  19. if ( !m_SpentUnits )
  20. {
  21. m_SpentUnits = new Param1<float>(0);
  22. }
  23. else
  24. {
  25. m_SpentUnits.param1 = 0;
  26. }
  27. if ( action_data.m_MainItem )
  28. m_ItemQuantity = action_data.m_MainItem.GetQuantity();
  29. if ( target_GB )
  30. {
  31. array<string> selections = new array<string>;
  32. target_GB.GetActionComponentNameList(action_data.m_Target.GetComponentIndex(), selections);
  33. for (int s = 0; s < selections.Count(); s++)
  34. {
  35. string selection = selections[s];
  36. m_Slot = target_GB.GetSlotBySelection( selection );
  37. if (m_Slot)
  38. break;
  39. }
  40. m_PlantThirstyness = m_Slot.GetWaterUsage() - m_Slot.GetWater();
  41. }
  42. m_TimeToComplete = (Math.Min(m_PlantThirstyness,m_ItemQuantity))/m_QuantityUsedPerSecond;
  43. if (m_TimeToComplete <= 0.1) // Division by zero prevention
  44. {
  45. m_TimeToComplete = 0.1;
  46. }
  47. }
  48. }
  49. override int Execute( ActionData action_data )
  50. {
  51. GardenBase target_GB;
  52. Class.CastTo(target_GB, action_data.m_Target.GetObject() );
  53. m_ItemQuantity = action_data.m_MainItem.GetQuantity();
  54. m_ItemMaxQuantity = action_data.m_MainItem.GetQuantityMax();
  55. if ( !action_data.m_Player )
  56. {
  57. return UA_ERROR;
  58. }
  59. if ( m_ItemQuantity <= 0 )
  60. {
  61. return UA_FINISHED;
  62. }
  63. else
  64. {
  65. if ( m_Slot && m_SpentQuantity < m_ItemQuantity )
  66. {
  67. m_SpentQuantity += m_QuantityUsedPerSecond * action_data.m_Player.GetDeltaT();
  68. if (m_SpentQuantity > m_ItemQuantity) // dont transfer more water than the source's quantity
  69. m_SpentQuantity = m_ItemQuantity;
  70. m_Slot.GiveWater( m_SpentQuantity );
  71. m_SpentQuantityTotal += m_SpentQuantity;
  72. CalcAndSetQuantity( action_data );
  73. m_SpentQuantity = 0;
  74. return UA_PROCESSING;
  75. }
  76. else
  77. {
  78. CalcAndSetQuantity( action_data );
  79. OnCompletePogress( action_data );
  80. return UA_FINISHED;
  81. }
  82. }
  83. }
  84. override float GetProgress()
  85. {
  86. if ( m_ItemQuantity <= 0 )
  87. return 1;
  88. float progress = -(m_SpentQuantityTotal / m_StartQuantity);
  89. return progress;
  90. }
  91. };