cacontinuouswaterslot.c 2.4 KB

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