DataGraph.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. #include "DataGraph.h"
  2. #include "..\..\icommon\memfile.h"
  3. #include "..\..\icommon\types.h"
  4. #include "..\..\icommon\graphtime.h"
  5. #include "..\..\icommon\names.h"
  6. #include "..\..\..\common_h\core.h"
  7. #include "..\..\TextFile.h"
  8. #include "..\..\service\particleservice.h"
  9. extern ParticleService* PService;
  10. #ifndef _XBOX
  11. #include "..\..\..\common_h\tinyxml\tinyxml.h"
  12. #endif
  13. #include "fieldlist.h"
  14. #include "..\..\..\Common_h\data_swizzle.h"
  15. #pragma warning (disable : 4800)
  16. //Взять случайное число из диапазона
  17. __forceinline float RandomRange (float Min, float Max)
  18. {
  19. return RRnd(Min, Max);
  20. /*
  21. float Temp;
  22. if (Min>Max)
  23. {
  24. Temp = Max;
  25. Max = Min;
  26. Min = Temp;
  27. }
  28. float Rand = (float)(rand()%1000) / 1000.0f;
  29. Rand *= (Max - Min);
  30. return Rand + Min;*/
  31. }
  32. DataGraph::DataGraph () /*: _MinGraph(_FL_), _MaxGraph(_FL_)*/
  33. {
  34. GraphData = NULL;
  35. MinGraphDataStart = -1;
  36. MaxGraphDataStart = -1;
  37. MinGraphDataSize = 0;
  38. MaxGraphDataSize = 0;
  39. bConstGraph = false;
  40. szName = NULL;
  41. szEditorName = NULL;
  42. Master = NULL;
  43. bRelative = false;
  44. bNegative = false;
  45. ResetCachedTime ();
  46. }
  47. void DataGraph::SetMasterField (FieldList* pMaster)
  48. {
  49. Master = pMaster;
  50. }
  51. //конструктор/деструктор
  52. DataGraph::DataGraph (FieldList* pMaster) /*: _MinGraph(_FL_), _MaxGraph(_FL_)*/
  53. {
  54. GraphData = NULL;
  55. MinGraphDataStart = -1;
  56. MaxGraphDataStart = -1;
  57. MinGraphDataSize = 0;
  58. MaxGraphDataSize = 0;
  59. bConstGraph = false;
  60. szName = NULL;
  61. szEditorName = NULL;
  62. Master = pMaster;
  63. bRelative = false;
  64. bNegative = false;
  65. ResetCachedTime ();
  66. }
  67. DataGraph::~DataGraph ()
  68. {
  69. }
  70. void DataGraph::CalculateConstData ()
  71. {
  72. if (MinGraphDataSize == 2 && MaxGraphDataSize == 2)
  73. {
  74. if (GraphData->GetElement(MinGraphDataStart+1).Time >= 7000.0f && GraphData->GetElement(MaxGraphDataStart+1).Time >= 7000.0f)
  75. {
  76. bConstGraph = true;
  77. }
  78. } else
  79. {
  80. if (MinGraphDataSize == 1 && MaxGraphDataSize == 1)
  81. {
  82. bConstGraph = true;
  83. }
  84. }
  85. if (bConstGraph)
  86. {
  87. if (MinGraphDataSize > 0 && MaxGraphDataSize > 0)
  88. {
  89. fConstMin = GraphData->GetElement(MinGraphDataStart).Val;
  90. fConstMax = GraphData->GetElement(MaxGraphDataStart).Val;
  91. } else
  92. {
  93. fConstMin = 0.0f;
  94. fConstMax = 0.0f;
  95. }
  96. }
  97. }
  98. //Установить значения...
  99. void DataGraph::SetValues (const GraphVertex* MinValues, DWORD MinValuesSize, const GraphVertex* MaxValues, DWORD MaxValuesSize)
  100. {
  101. GraphData = PService->AllocateDataForGraphs(MinValuesSize, MaxValuesSize, MinGraphDataStart, MinGraphDataSize, MaxGraphDataStart, MaxGraphDataSize);
  102. DWORD n = 0;
  103. for (n = 0; n < MinValuesSize; n++)
  104. {
  105. GraphData->GetElement(MinGraphDataStart+n) = MinValues[n];
  106. //MinGraph[n] = MinValues[n];
  107. }
  108. for (n = 0; n < MaxValuesSize; n++)
  109. {
  110. GraphData->GetElement(MaxGraphDataStart+n) = MaxValues[n];
  111. //MaxGraph[n] = MaxValues[n];
  112. }
  113. ResetCachedTime ();
  114. if (Master) Master->UpdateCache();
  115. CalculateConstData();
  116. }
  117. //Устанавливает "значение по умолчанию"
  118. void DataGraph::SetDefaultValue (float MaxValue, float MinValue)
  119. {
  120. //AllocateDataForGraphs(2, 2);
  121. GraphData = PService->AllocateDataForGraphs(2, 2, MinGraphDataStart, MinGraphDataSize, MaxGraphDataStart, MaxGraphDataSize);
  122. GraphVertex Min;
  123. Min.Val = MinValue;
  124. Min.Time = MIN_GRAPH_TIME;
  125. //MinGraph.Add(Min);
  126. GraphData->GetElement(MinGraphDataStart) = Min;
  127. Min.Time = MAX_GRAPH_TIME;
  128. //MinGraph.Add(Min);
  129. GraphData->GetElement(MinGraphDataStart+1) = Min;
  130. GraphVertex Max;
  131. Max.Val = MinValue;
  132. Max.Time = MIN_GRAPH_TIME;
  133. //MaxGraph.Add(Max);
  134. GraphData->GetElement(MaxGraphDataStart) = Max;
  135. Max.Time = MAX_GRAPH_TIME;
  136. //MaxGraph.Add(Max);
  137. GraphData->GetElement(MaxGraphDataStart) = Max;
  138. ResetCachedTime ();
  139. CalculateConstData();
  140. }
  141. //Получить кол-во в графике минимума
  142. DWORD DataGraph::GetMinCount ()
  143. {
  144. return MinGraphDataSize;
  145. }
  146. //Получить кол-во в графике максимума
  147. DWORD DataGraph::GetMaxCount ()
  148. {
  149. return MaxGraphDataSize;
  150. }
  151. //Получить значение по индексу из графика минимума
  152. const GraphVertex& DataGraph::GetMinVertex (DWORD Index)
  153. {
  154. return GraphData->GetElement(MinGraphDataStart+Index);
  155. //return MinGraph[Index];
  156. }
  157. //Получить значение по индексу из графика максимума
  158. const GraphVertex& DataGraph::GetMaxVertex (DWORD Index)
  159. {
  160. return GraphData->GetElement(MaxGraphDataStart+Index);
  161. //return MaxGraph[Index];
  162. }
  163. void DataGraph::ResetCachedTime ()
  164. {
  165. MaxCachedTime = NOT_INITED_CACHE_VALUE;
  166. MinCachedTime = NOT_INITED_CACHE_VALUE;
  167. }
  168. void DataGraph::Load (MemFile* File)
  169. {
  170. //MinGraph.DelAll();
  171. //MaxGraph.DelAll();
  172. dword dwNegative = 0;
  173. File->ReadType(dwNegative);
  174. XSwizzleDWord(dwNegative);
  175. SetNegative(dwNegative);
  176. dword dwRelative = 0;
  177. File->ReadType(dwRelative);
  178. XSwizzleDWord(dwRelative);
  179. SetRelative(dwRelative);
  180. dword MaxGraphItemsCount = 0;
  181. File->ReadType(MaxGraphItemsCount);
  182. XSwizzleDWord(MaxGraphItemsCount);
  183. DWORD i = 0;
  184. //MaxGraph.Reserve(MaxGraphItemsCount);
  185. //AllocateDataForGraphs(-1, MaxGraphItemsCount);
  186. GraphData = PService->AllocateDataForGraphs(-1, MaxGraphItemsCount, MinGraphDataStart, MinGraphDataSize, MaxGraphDataStart, MaxGraphDataSize);
  187. for (i = 0; i < MaxGraphItemsCount; i++)
  188. {
  189. float fTime = 0.0f;
  190. File->ReadType(fTime);
  191. XSwizzleFloat(fTime);
  192. float fValue = 0.0f;
  193. File->ReadType(fValue);
  194. XSwizzleFloat(fValue);
  195. if (!bNegative && fValue < 0.0f) fValue = 0.0f;
  196. GraphVertex MaxVertex;
  197. MaxVertex.Time = fTime;
  198. MaxVertex.Val = fValue;
  199. //MaxGraph.Add(MaxVertex);
  200. GraphData->GetElement(MaxGraphDataStart+i) = MaxVertex;
  201. //api->Trace("Max value %d = %3.2f, %3.2f", i, fTime, fValue);
  202. }
  203. dword MinGraphItemsCount = 0;
  204. File->ReadType(MinGraphItemsCount);
  205. XSwizzleDWord(MinGraphItemsCount);
  206. //MinGraph.Reserve(MinGraphItemsCount);
  207. //AllocateDataForGraphs(MinGraphItemsCount, -1);
  208. GraphData = PService->AllocateDataForGraphs(MinGraphItemsCount, -1, MinGraphDataStart, MinGraphDataSize, MaxGraphDataStart, MaxGraphDataSize);
  209. for (i = 0; i < MinGraphItemsCount; i++)
  210. {
  211. float fTime = 0.0f;
  212. File->ReadType(fTime);
  213. XSwizzleFloat(fTime);
  214. float fValue = 0.0f;
  215. File->ReadType(fValue);
  216. XSwizzleFloat(fValue);
  217. if (!bNegative && fValue < 0.0f) fValue = 0.0f;
  218. GraphVertex MinVertex;
  219. MinVertex.Time = fTime;
  220. MinVertex.Val = fValue;
  221. //MinGraph.Add(MinVertex);
  222. GraphData->GetElement(MinGraphDataStart+i) = MinVertex;
  223. //api->Trace("Min value %d = %3.2f, %3.2f", i, fTime, fValue);
  224. }
  225. //static char AttribueName[128];
  226. dword NameLength = 0;
  227. File->ReadType(NameLength);
  228. XSwizzleDWord(NameLength);
  229. Assert (NameLength < 128);
  230. //File->Read(AttribueName, NameLength);
  231. const char* AttribueName = File->GetPointerToString(NameLength);
  232. //api->Trace("Name %s", AttribueName);
  233. SetName (AttribueName, "a");
  234. //HACK ! Для совместимости со старой версией...
  235. //Конвертим после загрузки графики в нужный формат...
  236. //if (crt_stricmp (AttribueName, EMISSION_DIR_X) == 0) ConvertDegToRad ();
  237. //if (crt_stricmp (AttribueName, EMISSION_DIR_Y) == 0) ConvertDegToRad ();
  238. //if (crt_stricmp (AttribueName, EMISSION_DIR_Z) == 0) ConvertDegToRad ();
  239. //if (crt_stricmp (AttribueName, PARTICLE_DRAG) == 0) NormalToPercent();
  240. //if (crt_stricmp (AttribueName, PARTICLE_TRANSPARENCY) == 0) NormalToAlpha();
  241. if (Master) Master->UpdateCache();
  242. CalculateConstData();
  243. }
  244. //Установить/получить могут быть отрицательные значения в графике или нет...
  245. void DataGraph::SetNegative (bool _bNegative)
  246. {
  247. bNegative = _bNegative;
  248. }
  249. bool DataGraph::GetNegative ()
  250. {
  251. return bNegative;
  252. }
  253. //Установить/получить относительный график или нет...
  254. void DataGraph::SetRelative (bool _bRelative)
  255. {
  256. bRelative = _bRelative;
  257. }
  258. bool DataGraph::GetRelative ()
  259. {
  260. return bRelative;
  261. }
  262. void DataGraph::SetName (const char* szName, const char* szEditorName)
  263. {
  264. //api->Trace("DataGraph::SetName - '%s'", szName);
  265. this->szName = szName;
  266. this->szEditorName = szEditorName;
  267. }
  268. const char* DataGraph::GetName ()
  269. {
  270. return szName;
  271. }
  272. void DataGraph::ConvertRadToDeg ()
  273. {
  274. MultiplyBy (MUL_RADTODEG);
  275. }
  276. void DataGraph::ConvertDegToRad ()
  277. {
  278. MultiplyBy (MUL_DEGTORAD);
  279. }
  280. void DataGraph::MultiplyBy (float Val)
  281. {
  282. long n;
  283. for (n = 0; n < MaxGraphDataSize; n++)
  284. {
  285. float & v = GraphData->GetElement(MaxGraphDataStart+n).Val;
  286. v *= Val;
  287. //MaxGraph[n].Val *= Val;
  288. }
  289. for (n = 0; n < MinGraphDataSize; n++)
  290. {
  291. float & v = GraphData->GetElement(MinGraphDataStart+n).Val;
  292. v *= Val;
  293. //MinGraph[n].Val *= Val;
  294. }
  295. }
  296. void DataGraph::Clamp (float MinValue, float MaxValue)
  297. {
  298. long n;
  299. for (n = 0; n < MaxGraphDataSize; n++)
  300. {
  301. float & v = GraphData->GetElement(MaxGraphDataStart+n).Val;
  302. if (v > MaxValue) v = MaxValue;
  303. if (v < MinValue) v = MinValue;
  304. }
  305. for (n = 0; n < MinGraphDataSize; n++)
  306. {
  307. float & v = GraphData->GetElement(MinGraphDataStart+n).Val;
  308. if (v > MaxValue) v = MaxValue;
  309. if (v < MinValue) v = MinValue;
  310. }
  311. CalculateConstData ();
  312. }
  313. void DataGraph::Reverse ()
  314. {
  315. long n;
  316. for (n = 0; n < MaxGraphDataSize; n++)
  317. {
  318. float & v = GraphData->GetElement(MaxGraphDataStart+n).Val;
  319. v = 1.0f - v;
  320. //MaxGraph[n].Val = 1.0f - MaxGraph[n].Val;
  321. }
  322. for (n = 0; n < MinGraphDataSize; n++)
  323. {
  324. float & v = GraphData->GetElement(MinGraphDataStart+n).Val;
  325. v = 1.0f - v;
  326. //MinGraph[n].Val = 1.0f - MinGraph[n].Val;
  327. }
  328. CalculateConstData ();
  329. }
  330. void DataGraph::NormalToPercent ()
  331. {
  332. MultiplyBy (0.01f);
  333. Reverse ();
  334. Clamp (0.0f, 1.0f);
  335. CalculateConstData ();
  336. }
  337. void DataGraph::PercentToNormal ()
  338. {
  339. Reverse ();
  340. MultiplyBy (100.0f);
  341. CalculateConstData ();
  342. }
  343. void DataGraph::NormalToAlpha ()
  344. {
  345. NormalToPercent ();
  346. MultiplyBy (255.0f);
  347. CalculateConstData();
  348. }
  349. void DataGraph::AlphaToNormal ()
  350. {
  351. MultiplyBy (0.00392156862745098f);
  352. PercentToNormal ();
  353. CalculateConstData();
  354. }
  355. float DataGraph::GetMaxTime ()
  356. {
  357. float MaxVal = 10.0f;
  358. float MinVal = 10.0f;
  359. DWORD MaxCount = MaxGraphDataSize;
  360. DWORD MinCount = MinGraphDataSize;
  361. if (MaxCount > 2) MaxVal = GraphData->GetElement(MaxGraphDataStart+MaxCount-2).Time;
  362. if (MinCount > 2) MinVal = GraphData->GetElement(MinGraphDataStart+MaxCount-2).Time;
  363. if (MaxVal > MinVal) return MaxVal;
  364. return MinVal;
  365. }
  366. void DataGraph::Write (MemFile* File)
  367. {
  368. DWORD dwNegative = GetNegative();
  369. File->WriteType(dwNegative);
  370. DWORD dwRelative = GetRelative();
  371. File->WriteType(dwRelative);
  372. DWORD MaxGraphItemsCount = MaxGraphDataSize;
  373. File->WriteType(MaxGraphItemsCount);
  374. DWORD i = 0;
  375. for (i = 0; i < MaxGraphItemsCount; i++)
  376. {
  377. float fTime = GraphData->GetElement(MaxGraphDataStart+i).Time;
  378. File->WriteType(fTime);
  379. float fValue = GraphData->GetElement(MaxGraphDataStart+i).Val;
  380. File->WriteType(fValue);
  381. }
  382. DWORD MinGraphItemsCount = MinGraphDataSize;
  383. File->WriteType(MinGraphItemsCount);
  384. for (i = 0; i < MinGraphItemsCount; i++)
  385. {
  386. float fTime = GraphData->GetElement(MinGraphDataStart+i).Time;
  387. File->WriteType(fTime);
  388. float fValue = GraphData->GetElement(MinGraphDataStart+i).Val;
  389. File->WriteType(fValue);
  390. }
  391. //save name
  392. DWORD NameLength = crt_strlen (szName);
  393. DWORD NameLengthPlusZero = NameLength+1;
  394. File->WriteType(NameLengthPlusZero);
  395. Assert (NameLength < 128);
  396. File->Write(szName, NameLength);
  397. File->WriteZeroByte();
  398. }
  399. const char* DataGraph::GetEditorName ()
  400. {
  401. return szEditorName;
  402. }
  403. #ifndef _XBOX
  404. void DataGraph::WriteXML (TextFile* xmlFile, dword level)
  405. {
  406. xmlFile->Write((level+1), "<Name val = \"%s\" />\n", szName);
  407. xmlFile->Write((level+1), "<Negative val = \"%d\" />\n", GetNegative());
  408. xmlFile->Write((level+1), "<Relative val = \"%d\" />\n", GetRelative());
  409. xmlFile->Write((level+1), "<MaxGraph>\n");
  410. for (long n = 0; n < MaxGraphDataSize; n++)
  411. {
  412. xmlFile->Write((level+1), "<Key>\n");
  413. xmlFile->Write((level+2), "<Time val = \"%f\" />\n", GraphData->GetElement(MaxGraphDataStart+n).Time);
  414. xmlFile->Write((level+2), "<Value val = \"%f\" />\n", GraphData->GetElement(MaxGraphDataStart+n).Val);
  415. xmlFile->Write((level+1), "</Key>\n");
  416. }
  417. xmlFile->Write((level+1), "</MaxGraph>\n");
  418. xmlFile->Write((level+1), "<MinGraph>\n");
  419. for (long n = 0; n < MinGraphDataSize; n++)
  420. {
  421. xmlFile->Write((level+1), "<Key>\n");
  422. xmlFile->Write((level+2), "<Time val = \"%f\" />\n", GraphData->GetElement(MinGraphDataStart+n).Time);
  423. xmlFile->Write((level+2), "<Value val = \"%f\" />\n", GraphData->GetElement(MinGraphDataStart+n).Val);
  424. xmlFile->Write((level+1), "</Key>\n");
  425. }
  426. xmlFile->Write((level+1), "</MinGraph>\n");
  427. }
  428. void DataGraph::LoadXML (TiXmlElement* root)
  429. {
  430. /* FIXME: to using GraphData
  431. MinGraph.DelAll();
  432. MaxGraph.DelAll();
  433. TiXmlElement* name = root->FirstChildElement("Name");
  434. if (name)
  435. {
  436. SetName (name->Attribute("val"), "a");
  437. }
  438. TiXmlElement* neg = root->FirstChildElement("Negative");
  439. if (neg)
  440. {
  441. SetNegative (atoi (neg->Attribute("val")) == 1);
  442. }
  443. TiXmlElement* rel = root->FirstChildElement("Relative");
  444. if (rel)
  445. {
  446. SetRelative (atoi (rel->Attribute("val")) == 1);
  447. }
  448. TiXmlElement* maxGraphNode = NULL;
  449. maxGraphNode = root->FirstChildElement("MaxGraph");
  450. if (maxGraphNode)
  451. {
  452. for(TiXmlElement* child = maxGraphNode->FirstChildElement(); child; child = child->NextSiblingElement())
  453. {
  454. string NodeName = child->Value();
  455. if (NodeName == "Key")
  456. {
  457. GraphVertex MaxVertex;
  458. TiXmlElement* time = child->FirstChildElement("Time");
  459. TiXmlElement* val = child->FirstChildElement("Value");
  460. if (time)
  461. {
  462. MaxVertex.Time = (float)atof (time->Attribute("val"));
  463. }
  464. if (val)
  465. {
  466. MaxVertex.Val = (float)atof (val->Attribute("val"));
  467. }
  468. MaxGraph.Add(MaxVertex);
  469. }
  470. }
  471. }
  472. TiXmlElement* minGraphNode = NULL;
  473. minGraphNode = root->FirstChildElement("MinGraph");
  474. if (minGraphNode)
  475. {
  476. for(TiXmlElement* child = minGraphNode->FirstChildElement(); child; child = child->NextSiblingElement())
  477. {
  478. string NodeName = child->Value();
  479. if (NodeName == "Key")
  480. {
  481. GraphVertex MinVertex;
  482. TiXmlElement* time = child->FirstChildElement("Time");
  483. TiXmlElement* val = child->FirstChildElement("Value");
  484. if (time)
  485. {
  486. MinVertex.Time = (float)atof (time->Attribute("val"));
  487. }
  488. if (val)
  489. {
  490. MinVertex.Val = (float)atof (val->Attribute("val"));
  491. }
  492. MinGraph.Add(MinVertex);
  493. }
  494. }
  495. }
  496. CalculateConstData();
  497. */
  498. }
  499. #endif