modifierbase.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. enum eModifiersTickType
  2. {
  3. TICK = 1,
  4. ACTIVATE_CHECK = 2,
  5. DEACTIVATE_CHECK = 4,
  6. }
  7. class ModifierBase
  8. {
  9. int m_ID = 0;
  10. ModifiersManager m_Manager; //! the manager instance
  11. string m_System = "Modifiers";
  12. float m_ActivatedTime; //! overall time this modifier was active
  13. bool m_TrackActivatedTime; //!should this modifier track overall time it was active ?
  14. bool m_IsPersistent; //! is this modifier saved to the DB ?
  15. PlayerBase m_Player;
  16. float m_TickIntervalInactive = 5;
  17. float m_TickIntervalActive = 3;
  18. bool m_IsActive;
  19. bool m_ShouldBeActive;
  20. float m_AccumulatedTimeActive;
  21. float m_AccumulatedTimeInactive;
  22. float m_LastTickedActive;
  23. int m_TickType = (eModifiersTickType.TICK | eModifiersTickType.ACTIVATE_CHECK | eModifiersTickType.DEACTIVATE_CHECK);//some modifiers do not need to check activate condition, as they get activated by request
  24. float m_LastTickedInactive;
  25. bool m_IsLocked = false;
  26. EActivationType m_ActivationType;
  27. eModifierSyncIDs m_SyncID; //! max 32 synced modifiers supported, 0 == no sync
  28. PluginPlayerStatus m_ModulePlayerStatus;
  29. protected bool m_AnalyticsStatsEnabled;
  30. void ModifierBase()
  31. {
  32. Class.CastTo(m_ModulePlayerStatus, GetPlugin(PluginPlayerStatus));
  33. }
  34. void InitBase(PlayerBase player, ModifiersManager manager)
  35. {
  36. m_Manager = manager;
  37. m_Player = player;
  38. Init();
  39. }
  40. void Init()
  41. {
  42. if (m_AnalyticsStatsEnabled)
  43. AnalyticsRegisterStat(m_ID, "state");
  44. }
  45. PlayerBase GetPlayer()
  46. {
  47. return m_Player;
  48. }
  49. bool IsPersistent()
  50. {
  51. return m_IsPersistent;
  52. }
  53. void MakeParamObjectPersistent(Param object)
  54. {
  55. m_Manager.m_ParamList.Insert(object);
  56. }
  57. void ResetLastTickTime()
  58. {
  59. m_LastTickedActive = 0;
  60. }
  61. string GetDebugText()
  62. {
  63. return "";
  64. }
  65. string GetDebugTextSimple()
  66. {
  67. return "";
  68. }
  69. void DisableActivateCheck()
  70. {
  71. m_TickType = (m_TickType & ~eModifiersTickType.ACTIVATE_CHECK);
  72. }
  73. void DisableDeactivateCheck()
  74. {
  75. m_TickType = (m_TickType & ~eModifiersTickType.DEACTIVATE_CHECK);
  76. }
  77. void Tick(float delta_time)
  78. {
  79. if (!m_IsActive && m_ShouldBeActive)
  80. Activate();
  81. if (m_IsActive)
  82. {
  83. m_AccumulatedTimeActive += delta_time;
  84. if (m_AccumulatedTimeActive > m_TickIntervalActive)
  85. {
  86. if (m_TickType & eModifiersTickType.DEACTIVATE_CHECK && DeactivateCondition(m_Player) && !IsLocked())
  87. {
  88. Deactivate();
  89. }
  90. else
  91. {
  92. m_ActivatedTime += m_AccumulatedTimeActive;
  93. OnTick(m_Player, m_AccumulatedTimeActive);
  94. }
  95. m_AccumulatedTimeActive = 0;
  96. }
  97. }
  98. else if (m_TickType & eModifiersTickType.ACTIVATE_CHECK)
  99. {
  100. m_AccumulatedTimeInactive += delta_time;
  101. if (m_AccumulatedTimeInactive > m_TickIntervalInactive)
  102. {
  103. if (ActivateCondition(m_Player))
  104. {
  105. if (!IsLocked())
  106. ActivateRequest(EActivationType.TRIGGER_EVENT_ON_ACTIVATION);
  107. }
  108. m_AccumulatedTimeInactive = 0;
  109. }
  110. }
  111. }
  112. bool IsActive()
  113. {
  114. return m_IsActive;
  115. }
  116. void SetLock(bool state)
  117. {
  118. m_IsLocked = state;
  119. }
  120. bool IsLocked()
  121. {
  122. return m_IsLocked;
  123. }
  124. bool IsTrackAttachedTime()
  125. {
  126. return m_TrackActivatedTime;
  127. }
  128. float GetAttachedTime()
  129. {
  130. return m_ActivatedTime;
  131. }
  132. void SetAttachedTime(float time)
  133. {
  134. m_ActivatedTime = time;
  135. }
  136. int GetModifierID()
  137. {
  138. return m_ID;
  139. }
  140. string GetName()
  141. {
  142. string name = ClassName();
  143. int indexStart = name.Length() - 4;
  144. int indexEnd = name.Length();
  145. name = name.SubstringInverted(name, indexStart, indexEnd);
  146. return name;
  147. }
  148. bool ActivateCondition(PlayerBase player)
  149. {
  150. return false;
  151. }
  152. bool DeactivateCondition(PlayerBase player)
  153. {
  154. return false;
  155. }
  156. //! is called when an inactive modifier gets activated during gameplay, is NOT called on activation upon player server connection(see OnReconnect)
  157. void OnActivate(PlayerBase player);
  158. //! is called when a modifier is being re-activated upon player server connection, use to activate systems which are not persistent and need to run alongside active modifiers
  159. void OnReconnect(PlayerBase player);
  160. void OnDeactivate(PlayerBase player);
  161. void Activate()
  162. {
  163. if (m_AnalyticsStatsEnabled)
  164. {
  165. AnalyticsRegisterStat(m_ID, "state");
  166. AnalyticsSetState(m_ID, 1.0);
  167. }
  168. m_IsActive = true;
  169. m_Player.m_SyncedModifiers = (m_Player.m_SyncedModifiers | m_SyncID);
  170. if (m_ActivationType == EActivationType.TRIGGER_EVENT_ON_ACTIVATION)
  171. OnActivate(m_Player);
  172. else if (m_ActivationType == EActivationType.TRIGGER_EVENT_ON_CONNECT)
  173. OnReconnect(m_Player);
  174. m_Player.SetSynchDirty();
  175. }
  176. void ActivateRequest(EActivationType trigger)
  177. {
  178. m_ShouldBeActive = true;
  179. m_ActivationType = trigger;
  180. }
  181. void Deactivate(bool trigger = true)
  182. {
  183. if (!m_IsActive)
  184. return;
  185. if (m_AnalyticsStatsEnabled)
  186. AnalyticsSetState(m_ID, -1.0);
  187. m_Player.m_SyncedModifiers = (m_Player.m_SyncedModifiers & ~m_SyncID);
  188. m_ShouldBeActive = false;
  189. m_IsActive = false;
  190. m_ActivatedTime = 0;
  191. if (trigger)
  192. OnDeactivate(m_Player);
  193. }
  194. void OnStoreSave(ParamsWriteContext ctx);
  195. private void OnTick(PlayerBase player, float deltaT);
  196. private void AnalyticsRegisterStat(int modifierId, string keySuffix)
  197. {
  198. string modifierIdName = EnumTools.EnumToString(eModifiers, modifierId);
  199. modifierIdName.ToLower();
  200. m_Player.StatRegister(string.Format("%1_%2", modifierIdName, keySuffix));
  201. }
  202. //! special treating of the value to set state only (without counter use)
  203. private void AnalyticsSetState(int modifierId, float value)
  204. {
  205. string modifierStatKey = EnumTools.EnumToString(eModifiers, modifierId);
  206. modifierStatKey.ToLower();
  207. string formatttedKey = string.Format("%1_state", modifierStatKey);
  208. if (value == -1.0)
  209. {
  210. if(m_Player.StatGet(formatttedKey) == 0.0)
  211. return;
  212. }
  213. if (value == 1.0)
  214. {
  215. if(m_Player.StatGet(formatttedKey) == 1.0)
  216. return;
  217. }
  218. m_Player.StatUpdate(formatttedKey, value);
  219. }
  220. }