DataGraph.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef _PARTICLE_DATA_GRAPH_H_
  2. #define _PARTICLE_DATA_GRAPH_H_
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include "../../../common_h/core.h"
  7. #include "../../../common_h/templates.h"
  8. #include "..\..\icommon\memfile.h"
  9. #include "..\..\icommon\GraphVertex.h"
  10. class FieldList;
  11. class TextFile;
  12. extern DWORD GraphRead;
  13. extern DWORD OptGraphRead;
  14. #ifndef _XBOX
  15. class TiXmlElement;
  16. #endif
  17. class DataGraph
  18. {
  19. FieldList* Master;
  20. const char* szName;
  21. const char* szEditorName;
  22. //С какого времени последний раз забирали значение
  23. float MaxCachedTime;
  24. float MinCachedTime;
  25. //Какой был индекс у этого времени
  26. DWORD MaxCachedIndex;
  27. DWORD MinCachedIndex;
  28. //array<GraphVertex> _MinGraph;
  29. //array<GraphVertex> _MaxGraph;
  30. long MinGraphDataStart;
  31. long MaxGraphDataStart;
  32. long MinGraphDataSize;
  33. long MaxGraphDataSize;
  34. array<GraphVertex> * GraphData;
  35. void ResetCachedTime ();
  36. __forceinline float GetMinAtTime (float Time, float LifeTime)
  37. {
  38. if (bRelative) Time = Time / LifeTime * 100.0f;
  39. DWORD Count = MinGraphDataSize;
  40. DWORD Index;
  41. if (MinCachedTime < Time)
  42. Index = MinCachedIndex;
  43. else
  44. Index = 0;
  45. for ( ;Index < (Count-1); Index++)
  46. {
  47. float ToTime = GraphData->GetElement(MinGraphDataStart+Index+1).Time;
  48. //Если время в нужном диапазоне...
  49. //if ((Time >= FromTime) && (Time <= ToTime))
  50. if (Time <= ToTime)
  51. {
  52. float FromTime = GraphData->GetElement(MinGraphDataStart+Index).Time;
  53. float SegmentDeltaTime = ToTime - FromTime;
  54. float ValueDeltaTime = Time - FromTime;
  55. float blend_k ;
  56. if (SegmentDeltaTime > 0.001f)
  57. blend_k = ValueDeltaTime / SegmentDeltaTime;
  58. else
  59. blend_k = 0.0f;
  60. float ValueFirst = GraphData->GetElement(MinGraphDataStart+Index).Val;
  61. float ValueSecond = GraphData->GetElement(MinGraphDataStart+Index+1).Val;
  62. MinCachedTime = Time;
  63. MinCachedIndex = Index;
  64. return Lerp (ValueFirst, ValueSecond, blend_k);
  65. }
  66. }
  67. return 0.0f;
  68. }
  69. __forceinline float GetMaxAtTime (float Time, float LifeTime)
  70. {
  71. if (bRelative) Time = Time / LifeTime * 100.0f;
  72. DWORD Count = MaxGraphDataSize;
  73. DWORD Index;
  74. if (MaxCachedTime < Time)
  75. Index = MaxCachedIndex;
  76. else
  77. Index = 0;
  78. for (; Index < (Count-1); Index++)
  79. {
  80. float ToTime = GraphData->GetElement(MaxGraphDataStart+Index+1).Time;
  81. //Если время в нужном диапазоне...
  82. //if ((Time >= FromTime) && (Time <= ToTime))
  83. if (Time <= ToTime)
  84. {
  85. float FromTime = GraphData->GetElement(MaxGraphDataStart+Index).Time;
  86. float SegmentDeltaTime = ToTime - FromTime;;
  87. float ValueDeltaTime = Time - FromTime;
  88. float blend_k;
  89. if (SegmentDeltaTime > 0.001f)
  90. blend_k = ValueDeltaTime / SegmentDeltaTime;
  91. else
  92. blend_k = 0.0f;
  93. float ValueFirst = GraphData->GetElement(MaxGraphDataStart+Index).Val;
  94. float ValueSecond = GraphData->GetElement(MaxGraphDataStart+Index+1).Val;
  95. MaxCachedTime = Time;
  96. MaxCachedIndex = Index;
  97. return Lerp (ValueFirst, ValueSecond, blend_k);
  98. }
  99. }
  100. return 0.0f;
  101. }
  102. bool bRelative;
  103. bool bNegative;
  104. void CalculateConstData ();
  105. float fConstMin;
  106. float fConstMax;
  107. bool bConstGraph;
  108. public:
  109. //конструктор/деструктор
  110. DataGraph ();
  111. void SetMasterField (FieldList* pMaster);
  112. DataGraph (FieldList* pMaster);
  113. virtual ~DataGraph ();
  114. //Установить/получить могут быть отрицательные значения в графике или нет...
  115. void SetNegative (bool _bNegative);
  116. bool GetNegative ();
  117. //Установить/получить относительный график или нет...
  118. void SetRelative (bool _bRelative);
  119. bool GetRelative ();
  120. //Получить значение (Текущее время, Коэфицент рандома[0..1])
  121. __forceinline float GetValue (float Time, float LifeTime, float K_rand)
  122. {
  123. float pMax = fConstMax;
  124. float pMin = fConstMin;
  125. if (!bConstGraph)
  126. {
  127. GraphRead++;
  128. pMax = GetMaxAtTime (Time, LifeTime);
  129. pMin = GetMinAtTime (Time, LifeTime);
  130. } else
  131. {
  132. OptGraphRead++;
  133. }
  134. return Lerp (pMin, pMax, K_rand);
  135. }
  136. __forceinline float GetRandomValue (float Time, float LifeTime)
  137. {
  138. float pMax = fConstMax;
  139. float pMin = fConstMin;
  140. if (!bConstGraph)
  141. {
  142. GraphRead++;
  143. pMax = GetMaxAtTime (Time, LifeTime);
  144. pMin = GetMinAtTime (Time, LifeTime);
  145. } else
  146. {
  147. OptGraphRead++;
  148. }
  149. //float pMax = GetMaxAtTime (Time, LifeTime);
  150. //float pMin = GetMinAtTime (Time, LifeTime);
  151. return RRnd(pMin, pMax);
  152. }
  153. //Установить значения...
  154. void SetValues (const GraphVertex* MinValues, DWORD MinValuesSize, const GraphVertex* MaxValues, DWORD MaxValuesSize);
  155. //Устанавливает "значение по умолчанию"
  156. void SetDefaultValue (float MaxValue, float MinValue);
  157. //Получить кол-во в графике минимума
  158. DWORD GetMinCount ();
  159. //Получить кол-во в графике максимума
  160. DWORD GetMaxCount ();
  161. //Получить значение по индексу из графика минимума
  162. const GraphVertex& GetMinVertex (DWORD Index);
  163. //Получить значение по индексу из графика максимума
  164. const GraphVertex& GetMaxVertex (DWORD Index);
  165. void Load (MemFile* File);
  166. void Write (MemFile* File);
  167. #ifndef _XBOX
  168. void LoadXML (TiXmlElement* root);
  169. void WriteXML (TextFile* xmlFile, dword level);
  170. #endif
  171. void SetName (const char* szName, const char* szEditorName);
  172. const char* GetEditorName ();
  173. const char* GetName ();
  174. float GetMaxTime ();
  175. void ConvertRadToDeg ();
  176. void ConvertDegToRad ();
  177. void MultiplyBy (float Val);
  178. void Clamp (float MinValue, float MaxValue);
  179. void Reverse (); //Graphs = 1.0f - Graph
  180. void NormalToPercent ();
  181. void PercentToNormal ();
  182. void NormalToAlpha ();
  183. void AlphaToNormal ();
  184. };
  185. #endif