catchingresultbasic.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. class CatchingResultBasic
  2. {
  3. protected EntityAI m_Owner;
  4. protected bool m_OverrideChanceActive = false; //do not perform chance updates
  5. protected bool m_OverrideQualityActive = false; //overrides quality
  6. protected float m_CatchChance = 1.0; //guaranteed catch by default
  7. protected float m_Quality = 1.0; //max quality default
  8. protected YieldItemBase m_YItem;
  9. void CatchingResultBasic(EntityAI owner)
  10. {
  11. m_Owner = owner;
  12. }
  13. void SetYieldItem(YieldItemBase yItem)
  14. {
  15. m_YItem = yItem;
  16. }
  17. int GetYieldItemParticleId()
  18. {
  19. if (m_YItem)
  20. return m_YItem.GetCatchParticleID();
  21. return ParticleList.INVALID;
  22. }
  23. protected void SetCatchChance(float val)
  24. {
  25. m_CatchChance = Math.Clamp(val,0,1);
  26. }
  27. protected void SetQuality(float val)
  28. {
  29. m_Quality = Math.Clamp(val,0,1);
  30. }
  31. void SetCatchChanceOverride(bool ovrd, float val = 0.0)
  32. {
  33. if (ovrd)
  34. m_CatchChance = val;
  35. m_OverrideChanceActive = ovrd;
  36. }
  37. void SetQualityOverride(bool ovrd, float val = 0.0)
  38. {
  39. if (ovrd)
  40. m_Quality = val;
  41. m_OverrideQualityActive = ovrd;
  42. }
  43. void UpdateCatchQuality(CatchingContextBase ctx)
  44. {
  45. if (m_OverrideQualityActive)
  46. return;
  47. if (!m_YItem)
  48. return;
  49. float val = m_YItem.GetQualityForYieldItem(ctx);
  50. SetQuality(val);
  51. }
  52. void UpdateCatchChance(CatchingContextBase ctx)
  53. {
  54. if (m_OverrideChanceActive)
  55. return;
  56. if (!m_YItem)
  57. return;
  58. float val = m_YItem.GetChanceForYieldItem(ctx);
  59. SetCatchChance(val);
  60. }
  61. bool RollChance()
  62. {
  63. if (m_CatchChance >= 1)
  64. return true;
  65. if (m_CatchChance <= 0)
  66. return false;
  67. float roll;
  68. if (m_Owner && m_Owner.GetHierarchyRootPlayer())
  69. roll = RollChanceSeeded();
  70. else
  71. roll = Math.RandomFloat01();
  72. return roll < m_CatchChance;
  73. }
  74. protected float RollChanceSeeded()
  75. {
  76. return 1.0;
  77. }
  78. EntityAI SpawnAndSetup(out int yItemIdx, vector v = vector.Zero)
  79. {
  80. vector pos = v;
  81. if (v == vector.Zero && m_Owner != null)
  82. pos = m_Owner.GetPosition();
  83. if (!m_YItem)
  84. return null;
  85. yItemIdx = m_YItem.GetRegistrationIdx();
  86. EntityAI ret = EntityAI.Cast(GetGame().CreateObjectEx(m_YItem.GetType(), pos, ECE_PLACE_ON_SURFACE));
  87. if (ret)
  88. m_YItem.OnEntityYieldSpawned(ret);
  89. return ret;
  90. }
  91. };