notifierbase.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. class NotifierBase
  2. {
  3. float m_DeltaT; // time in seconds since the last tick
  4. ref Timer m_Timer1; // timer which can be used for whatever
  5. PlayerBase m_Player; //the player this Notifier belongs to
  6. int m_Type;
  7. NotifiersManager m_Manager;
  8. int m_TendencyBufferSize = 3;//for best results, this should be somewhat aligned with modifier frequency
  9. const int TENDENCY_BUFFER_SIZE = 30;//this needs to be bigger or same size as buffer size of any invidual buffer size
  10. bool m_ShowTendency;
  11. bool m_Active;
  12. int m_TickInterval;
  13. int m_TickIntervalLastTick;
  14. float m_TendencyBuffer[TENDENCY_BUFFER_SIZE];
  15. int m_TendencyBufferWriteIterator;
  16. float m_LastTendency;
  17. float m_LastMA;
  18. bool m_FirstPass = true;
  19. PluginPlayerStatus m_ModulePlayerStatus;
  20. void NotifierBase(NotifiersManager manager)
  21. {
  22. m_ModulePlayerStatus = PluginPlayerStatus.Cast(GetPlugin(PluginPlayerStatus));
  23. m_Active = true;
  24. m_Manager = manager;
  25. m_Player = manager.GetPlayer();
  26. m_TickInterval = 1000;
  27. manager.RegisterItself(GetNotifierType(), this);
  28. }
  29. bool IsTimeToTick(int current_time)
  30. {
  31. return (current_time > m_TickIntervalLastTick + m_TickInterval);
  32. }
  33. VirtualHud GetVirtualHud()
  34. {
  35. return m_Player.GetVirtualHud();
  36. }
  37. int GetNotifierType()
  38. {
  39. return m_Type;
  40. }
  41. string GetName()
  42. {
  43. return this.ClassName() + " Notifier";
  44. }
  45. bool IsActive()
  46. {
  47. return m_Active;
  48. }
  49. void SetActive(bool state)
  50. {
  51. m_Active = state;
  52. if (!state)
  53. HideBadge();
  54. }
  55. void DisplayTendency(float delta);
  56. void AddToCyclicBuffer(float value)//for tendency
  57. {
  58. m_TendencyBuffer[m_TendencyBufferWriteIterator] = value;
  59. m_TendencyBufferWriteIterator++;
  60. if (m_TendencyBufferWriteIterator == m_TendencyBufferSize)
  61. {
  62. m_TendencyBufferWriteIterator = 0;
  63. m_ShowTendency = true;
  64. }
  65. }
  66. float ReadFromCyclicBuffer(int index)
  67. {
  68. int indx = m_TendencyBufferWriteIterator + index;
  69. if ( indx >= m_TendencyBufferSize)
  70. {
  71. indx = indx - m_TendencyBufferSize;
  72. }
  73. return m_TendencyBuffer[indx];
  74. }
  75. float GetDeltaAvaraged() //for tendency
  76. {
  77. array<float> values = new array<float>();
  78. for (int i = 0; i < m_TendencyBufferSize; i++)
  79. {
  80. values.Insert(ReadFromCyclicBuffer(i));
  81. }
  82. float valuesSum = 0;
  83. for (i = 0; i < values.Count(); i++)
  84. {
  85. float value = values.Get(i);
  86. valuesSum += value;
  87. }
  88. float sma = valuesSum / m_TendencyBufferSize;
  89. if (m_FirstPass)
  90. {
  91. m_LastMA = sma;
  92. m_FirstPass = false;
  93. }
  94. float tnd = sma - m_LastMA;
  95. m_LastMA = sma;
  96. return tnd;
  97. }
  98. void SmoothOutFloatValues(array<float> values)
  99. {
  100. float value1;
  101. float value2;
  102. for (int i = 0; i < values.Count() - 1; i++)
  103. {
  104. value1 = values.Get(i);
  105. value2 = values.Get(i + 1);
  106. float average = (value1 + value2) / 2;
  107. values.Set(i, average);
  108. values.Set(i + 1, average);
  109. }
  110. int index = values.Count() - 1;
  111. values.Set(index, value2);
  112. }
  113. void OnTick(float current_time)
  114. {
  115. m_TickIntervalLastTick = current_time;
  116. DisplayBadge();
  117. AddToCyclicBuffer(GetObservedValue());
  118. if (m_ShowTendency)
  119. DisplayTendency(GetDeltaAvaraged());
  120. }
  121. protected int CalculateTendency(float delta, float inctresholdlow, float inctresholdmed, float inctresholdhigh, float dectresholdlow, float dectresholdmed, float dectresholdhigh)
  122. {
  123. int tendency = TENDENCY_STABLE;
  124. if (delta > inctresholdlow)
  125. tendency = TENDENCY_INC_LOW;
  126. if (delta > inctresholdmed)
  127. tendency = TENDENCY_INC_MED;
  128. if (delta > inctresholdhigh)
  129. tendency = TENDENCY_INC_HIGH;
  130. if (delta < dectresholdlow)
  131. tendency = TENDENCY_DEC_LOW;
  132. if (delta < dectresholdmed)
  133. tendency = TENDENCY_DEC_MED;
  134. if (delta < dectresholdhigh)
  135. tendency = TENDENCY_DEC_HIGH;
  136. return tendency;
  137. }
  138. protected eBadgeLevel DetermineBadgeLevel(float value, float lvl_1, float lvl_2, float lvl_3)
  139. {
  140. eBadgeLevel level = eBadgeLevel.NONE;
  141. if (value >= lvl_1)
  142. level = eBadgeLevel.FIRST;
  143. if (value >= lvl_2)
  144. level = eBadgeLevel.SECOND;
  145. if (value >= lvl_3)
  146. level = eBadgeLevel.THIRD;
  147. return level;
  148. }
  149. protected void DisplayBadge();
  150. protected void HideBadge();
  151. protected float GetObservedValue()
  152. {
  153. return 0.0;
  154. }
  155. }