cacontinuousfertilizegardenslot.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. class CAContinuousFertilizeGardenSlot : CAContinuousQuantity
  2. {
  3. protected float m_SlotFertilizerNeed;
  4. protected float m_AlreadyFilledAmount; // amount of fertilizer present within slot during the setup of action
  5. protected float m_TimeToComplete;
  6. protected Slot m_Slot;
  7. protected string m_Selection;
  8. void CAContinuousFertilizeGardenSlot(float quantity_used_per_second)
  9. {
  10. m_QuantityUsedPerSecond = quantity_used_per_second;
  11. m_TimeToComplete = 0;
  12. }
  13. override void Setup(ActionData action_data)
  14. {
  15. GardenBase targetGB;
  16. if (Class.CastTo(targetGB, action_data.m_Target.GetObject()))
  17. {
  18. m_SpentQuantity = 0;
  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 (targetGB)
  30. {
  31. array<string> selections = new array<string>;
  32. targetGB.GetActionComponentNameList(action_data.m_Target.GetComponentIndex(), selections);
  33. for (int s = 0; s < selections.Count(); s++)
  34. {
  35. m_Selection = selections[s];
  36. m_Slot = targetGB.GetSlotBySelection(m_Selection);
  37. if (m_Slot)
  38. break;
  39. }
  40. string itemType = action_data.m_MainItem.GetType();
  41. float consumedQuantity = GetGame().ConfigGetFloat("cfgVehicles " + itemType + " Horticulture ConsumedQuantity");
  42. float actionLength = GetGame().ConfigGetFloat("cfgVehicles " + itemType + " Horticulture FertilizeLength");
  43. if (actionLength == 0)
  44. actionLength = 1;
  45. m_Slot.SetFertilizerQuantityMax(consumedQuantity);
  46. m_AlreadyFilledAmount = m_Slot.GetFertilizerQuantity();
  47. m_SlotFertilizerNeed = consumedQuantity - m_AlreadyFilledAmount;
  48. }
  49. float defaultTimeComplete = consumedQuantity / m_QuantityUsedPerSecond;
  50. float speedMultiplier = defaultTimeComplete / actionLength;
  51. m_QuantityUsedPerSecond *= speedMultiplier;
  52. }
  53. }
  54. override int Execute(ActionData action_data)
  55. {
  56. if (!action_data.m_Player)
  57. {
  58. return UA_ERROR;
  59. }
  60. if (m_SpentQuantity >= m_ItemQuantity)
  61. {
  62. return UA_FINISHED;
  63. }
  64. else
  65. {
  66. if (m_SpentQuantity <= m_ItemQuantity)
  67. {
  68. //! Reset slot fertilizer type and quantity in case current type in target slot is not matching
  69. string itemType = action_data.m_MainItem.GetType();
  70. if (m_Slot.GetFertilityType() != "" && m_Slot.GetFertilityType() != itemType)
  71. {
  72. m_Slot.SetFertilityType(itemType);
  73. }
  74. m_SpentQuantity = m_QuantityUsedPerSecond * action_data.m_Player.GetDeltaT();
  75. GardenBase gardenBase;
  76. if (!Class.CastTo(gardenBase, action_data.m_Target.GetObject()))
  77. {
  78. return UA_ERROR;
  79. }
  80. FertilizeSlot(action_data.m_MainItem, gardenBase, m_SpentQuantity);
  81. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  82. {
  83. action_data.m_MainItem.AddQuantity(-m_SpentQuantity);
  84. }
  85. return UA_PROCESSING;
  86. }
  87. else
  88. {
  89. CalcAndSetQuantity(action_data);
  90. OnCompletePogress(action_data);
  91. return UA_FINISHED;
  92. }
  93. }
  94. }
  95. protected void FertilizeSlot(ItemBase item, GardenBase gardenBase, float consumedQuantity)
  96. {
  97. if (m_Slot)
  98. {
  99. string itemType = item.GetType();
  100. if (m_Slot.GetFertilityType() == "" || m_Slot.GetFertilityType() == itemType)
  101. {
  102. m_Slot.SetFertilityType(itemType);
  103. float addEnergyToSlot = GetGame().ConfigGetFloat(string.Format("cfgVehicles %1 Horticulture AddEnergyToSlot", itemType));
  104. m_Slot.m_FertilizerUsage = GetGame().ConfigGetFloat(string.Format("cfgVehicles %1 Horticulture ConsumedQuantity", itemType));
  105. float coef = Math.Clamp(consumedQuantity / m_Slot.m_FertilizerUsage, 0.0, 1.0);
  106. addEnergyToSlot = coef * addEnergyToSlot;
  107. float fertilizerMax = m_Slot.GetFertilizerQuantityMax();
  108. float newFertilizerQuantity = (m_Slot.m_FertilizerQuantity + consumedQuantity);
  109. newFertilizerQuantity = Math.Clamp(newFertilizerQuantity, 0.0, fertilizerMax);
  110. m_Slot.SetFertilizerQuantity(newFertilizerQuantity);
  111. float newfertility = (m_Slot.m_Fertility + addEnergyToSlot);
  112. float fertilityMax = m_Slot.GetFertilityMax();
  113. newfertility = Math.Clamp(newfertility, 0.0, fertilityMax);
  114. m_Slot.SetFertility(newfertility);
  115. }
  116. }
  117. }
  118. override float GetProgress()
  119. {
  120. return -((m_Slot.GetFertilizerQuantity() - m_AlreadyFilledAmount) / m_SlotFertilizerNeed);
  121. }
  122. //! DEPRECATED
  123. protected float m_SpentQuantityTotal;
  124. protected float m_StartQuantity;
  125. };