afxPath.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/stream/bitStream.h"
  27. #include "math/mathIO.h"
  28. #include "afx/util/afxPath.h"
  29. #include "afx/util/afxPath3D.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxPathData
  32. IMPLEMENT_CO_DATABLOCK_V1(afxPathData);
  33. ConsoleDocClass( afxPathData,
  34. "@brief A datablock for specifiying a 3D path for use with AFX.\n\n"
  35. "@ingroup afxUtil\n"
  36. "@ingroup AFX\n"
  37. "@ingroup Datablocks\n"
  38. );
  39. StringTableEntry afxPathData::POINTS_FIELD;
  40. StringTableEntry afxPathData::ROLL_FIELD;
  41. StringTableEntry afxPathData::TIMES_FIELD;
  42. ImplementEnumType( afxPath3DLoopType, "Possible loop types for an afxPath.\n" "@ingroup afxPath\n\n" )
  43. { afxPath3D::LOOP_CONSTANT, "constant", "..." },
  44. { afxPath3D::LOOP_CYCLE, "cycle", "..." },
  45. { afxPath3D::LOOP_OSCILLATE, "oscillate", "..." },
  46. EndImplementEnumType;
  47. afxPathData::afxPathData()
  48. {
  49. if (POINTS_FIELD == 0)
  50. {
  51. POINTS_FIELD = StringTable->insert("points");
  52. ROLL_FIELD = StringTable->insert("roll");
  53. TIMES_FIELD = StringTable->insert("times");
  54. }
  55. loop_string = ST_NULLSTRING;
  56. delay = 0;
  57. lifetime = 0;
  58. loop_type = 0;
  59. mult = 1.0f;
  60. time_offset = 0.0f;
  61. resolved = false;
  62. reverse = false;
  63. offset.zero();
  64. echo = false;
  65. concentric = false;
  66. points_string = ST_NULLSTRING;
  67. points = 0;
  68. num_points = 0;
  69. update_points = true;
  70. roll_string = ST_NULLSTRING;
  71. rolls = 0;
  72. update_rolls = true;
  73. times_string = ST_NULLSTRING;
  74. times = 0;
  75. update_times = true;
  76. }
  77. afxPathData::afxPathData(const afxPathData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  78. {
  79. points_string = other.points_string;
  80. roll_string = other.roll_string;
  81. loop_string = other.loop_string;
  82. delay = other.delay;
  83. lifetime = other.lifetime;
  84. loop_type = other.loop_type; // --
  85. mult = other.mult;
  86. time_offset = other.time_offset;
  87. resolved = other.resolved; // --
  88. reverse = other.reverse;
  89. offset = other.offset;
  90. echo = other.echo;
  91. concentric = other.concentric;
  92. times_string = other.times_string;
  93. num_points = other.num_points; // --
  94. if (other.points && num_points > 0)
  95. {
  96. points = new Point3F[num_points];
  97. dMemcpy(points, other.points, sizeof(Point3F)*num_points); // --
  98. }
  99. else
  100. points = 0;
  101. if (other.rolls && num_points > 0)
  102. {
  103. rolls = new F32[num_points];
  104. dMemcpy(rolls, other.rolls, sizeof(F32)*num_points); // --
  105. }
  106. else
  107. rolls = 0;
  108. if (other.times && num_points > 0)
  109. {
  110. times = new F32[num_points];
  111. dMemcpy(times, other.times, sizeof(F32)*num_points); // --
  112. }
  113. else
  114. times = 0;
  115. update_points = other.update_points; // --
  116. update_rolls = other.update_rolls; // --
  117. update_times = other.update_times; // --
  118. }
  119. afxPathData::~afxPathData()
  120. {
  121. clear_arrays();
  122. }
  123. void afxPathData::initPersistFields()
  124. {
  125. addField("points", TypeString, Offset(points_string, afxPathData),
  126. "...");
  127. addField("roll", TypeString, Offset(roll_string, afxPathData),
  128. "...");
  129. addField("times", TypeString, Offset(times_string, afxPathData),
  130. "...");
  131. addField("loop", TypeString, Offset(loop_string, afxPathData),
  132. "...");
  133. addField("mult", TypeF32, Offset(mult, afxPathData),
  134. "...");
  135. addField("delay", TypeF32, Offset(delay, afxPathData),
  136. "...");
  137. addField("lifetime", TypeF32, Offset(lifetime, afxPathData),
  138. "...");
  139. addField("timeOffset", TypeF32, Offset(time_offset, afxPathData),
  140. "...");
  141. addField("reverse", TypeBool, Offset(reverse, afxPathData),
  142. "...");
  143. addField("offset", TypePoint3F, Offset(offset, afxPathData),
  144. "...");
  145. addField("echo", TypeBool, Offset(echo, afxPathData),
  146. "...");
  147. addField("concentric", TypeBool, Offset(concentric, afxPathData),
  148. "...");
  149. Parent::initPersistFields();
  150. }
  151. bool afxPathData::onAdd()
  152. {
  153. if (Parent::onAdd() == false)
  154. return false;
  155. update_derived_values();
  156. return true;
  157. }
  158. void afxPathData::onRemove()
  159. {
  160. clear_arrays();
  161. loop_type = 0;
  162. Parent::onRemove();
  163. }
  164. void afxPathData::packData(BitStream* stream)
  165. {
  166. Parent::packData(stream);
  167. stream->write(num_points);
  168. if (num_points > 0)
  169. {
  170. for (U32 i = 0; i < num_points; i++)
  171. mathWrite(*stream, points[i]);
  172. if (stream->writeFlag(rolls != 0))
  173. {
  174. for (U32 i = 0; i < num_points; i++)
  175. stream->write(rolls[i]);
  176. }
  177. if (stream->writeFlag(times != 0))
  178. {
  179. for (U32 i = 0; i < num_points; i++)
  180. stream->write(times[i]);
  181. }
  182. }
  183. stream->writeString(loop_string);
  184. stream->write(delay);
  185. stream->write(lifetime);
  186. stream->write(time_offset);
  187. stream->write(mult);
  188. stream->writeFlag(reverse);
  189. mathWrite(*stream, offset);
  190. stream->writeFlag(echo);
  191. stream->writeFlag(concentric);
  192. }
  193. void afxPathData::unpackData(BitStream* stream)
  194. {
  195. Parent::unpackData(stream);
  196. clear_arrays();
  197. // read the points and rolls
  198. stream->read(&num_points);
  199. if (num_points > 0)
  200. {
  201. points = new Point3F[num_points];
  202. for (U32 i = 0; i < num_points; i++)
  203. mathRead(*stream, &points[i]);
  204. update_points = false;
  205. if (stream->readFlag())
  206. {
  207. rolls = new F32[num_points];
  208. for (U32 i = 0; i < num_points; i++)
  209. stream->read(&rolls[i]);
  210. update_rolls = false;
  211. }
  212. if (stream->readFlag())
  213. {
  214. times = new F32[num_points];
  215. for (U32 i = 0; i < num_points; i++)
  216. stream->read(&times[i]);
  217. update_times = false;
  218. }
  219. }
  220. loop_string = stream->readSTString();
  221. stream->read(&delay);
  222. stream->read(&lifetime);
  223. stream->read(&time_offset);
  224. stream->read(&mult);
  225. reverse = stream->readFlag();
  226. mathRead(*stream, &offset);
  227. echo = stream->readFlag();
  228. concentric = stream->readFlag();
  229. }
  230. void afxPathData::update_derived_values()
  231. {
  232. U32 num_rolls = (rolls != 0) ? num_points : 0;
  233. U32 num_times = (times != 0) ? num_points : 0;
  234. if (update_points)
  235. {
  236. derive_points_array();
  237. update_points = false;
  238. }
  239. if (update_rolls || num_rolls != num_points)
  240. {
  241. derive_rolls_array();
  242. update_rolls = false;
  243. }
  244. if (update_times || num_times != num_points)
  245. {
  246. derive_times_array();
  247. update_times = false;
  248. }
  249. // CAUTION: The following block of code is fragile and tricky since it depends
  250. // on the underlying structures defined by ImplementEnumType/EndImplementEnumType.
  251. // This done because the enum text is parsed from a longer string.
  252. if (loop_string != ST_NULLSTRING)
  253. {
  254. for (unsigned int i = 0; i < _afxPath3DLoopType::_sEnumTable.getNumValues(); i++)
  255. {
  256. if (dStricmp(_afxPath3DLoopType::_sEnumTable[i].mName, loop_string) == 0)
  257. {
  258. loop_type = _afxPath3DLoopType::_sEnumTable[i].mInt;
  259. break;
  260. }
  261. }
  262. }
  263. else
  264. {
  265. loop_string = _afxPath3DLoopType::_sEnumTable[0].mName;
  266. loop_type = _afxPath3DLoopType::_sEnumTable[0].mInt;
  267. }
  268. }
  269. bool afxPathData::preload(bool server, String &errorStr)
  270. {
  271. if (!Parent::preload(server, errorStr))
  272. return false;
  273. update_derived_values();
  274. return true;
  275. }
  276. void afxPathData::onStaticModified(const char* slot, const char* newValue)
  277. {
  278. Parent::onStaticModified(slot, newValue);
  279. if (slot == POINTS_FIELD)
  280. {
  281. update_points = true;
  282. return;
  283. }
  284. if (slot == ROLL_FIELD)
  285. {
  286. update_rolls = true;
  287. return;
  288. }
  289. if (slot == TIMES_FIELD)
  290. {
  291. update_times = true;
  292. return;
  293. }
  294. }
  295. void afxPathData::extract_floats_from_string(Vector<F32>& values, const char* points_str)
  296. {
  297. values.clear();
  298. if (!points_str)
  299. return;
  300. // make a copy of points_str for tokenizing
  301. char* tokCopy = dStrdup(points_str);
  302. // extract each token, convert to float, add to values[]
  303. char* currTok = dStrtok(tokCopy, " \t");
  304. while (currTok != NULL)
  305. {
  306. F32 value = dAtof(currTok);
  307. values.push_back(value);
  308. currTok = dStrtok(NULL, " \t");
  309. }
  310. dFree(tokCopy);
  311. }
  312. Point3F* afxPathData::build_points_array(Vector<F32>& values, U32& n_points, bool reverse)
  313. {
  314. AssertFatal(values.size() > 0, "Values array is empty.");
  315. AssertFatal(values.size()%3 == 0, "Values array is not a multiple of 3.");
  316. n_points = values.size()/3;
  317. Point3F* points = new Point3F[n_points];
  318. if (reverse)
  319. {
  320. U32 p_i = 0;
  321. for (S32 i = values.size()-1; i > 1; i-=3)
  322. points[p_i++].set(values[i-2], values[i-1], values[i]);
  323. }
  324. else
  325. {
  326. U32 p_i = 0;
  327. for (U32 i = 0; i < values.size(); i+=3)
  328. points[p_i++].set(values[i], values[i+1], values[i+2]);
  329. }
  330. return points;
  331. }
  332. F32* afxPathData::build_floats_array(Vector<F32>& values, U32& n_floats, bool reverse)
  333. {
  334. AssertFatal(values.size() > 0, "Values array is empty.");
  335. n_floats = values.size();
  336. F32* floats = new F32[n_floats];
  337. if (reverse)
  338. {
  339. F32* f = floats;
  340. for (S32 i = values.size()-1; i >= 0; i--)
  341. {
  342. *f = values[i];
  343. f++;
  344. }
  345. }
  346. else
  347. {
  348. for (U32 i = 0; i < values.size(); i++)
  349. floats[i] = values[i];
  350. }
  351. return floats;
  352. }
  353. void afxPathData::clear_arrays()
  354. {
  355. num_points = 0;
  356. if (points)
  357. {
  358. delete [] points;
  359. points = 0;
  360. }
  361. if (rolls)
  362. {
  363. delete [] rolls;
  364. rolls = 0;
  365. }
  366. if (times)
  367. {
  368. delete [] times;
  369. times = 0;
  370. }
  371. update_points = true;
  372. update_rolls = true;
  373. update_times = true;
  374. }
  375. void afxPathData::derive_points_array()
  376. {
  377. if (points_string == ST_NULLSTRING)
  378. return;
  379. if (points)
  380. {
  381. delete [] points;
  382. points = 0;
  383. }
  384. num_points = 0;
  385. Vector<F32> values;
  386. extract_floats_from_string(values, points_string);
  387. if (values.size() == 0)
  388. {
  389. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) empty points field, datablock is invalid.", getName());
  390. return;
  391. }
  392. if (values.size()%3 != 0)
  393. {
  394. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total points values is not a multiple of 3, datablock is invalid.", getName());
  395. return;
  396. }
  397. points = build_points_array(values, num_points, reverse);
  398. if (offset.x != 0.0f || offset.y != 0.0f || offset.z != 0.0f)
  399. {
  400. // add offset here for efficiency (saves an addition from afxXM_PathConform)
  401. for (U32 i = 0; i < num_points; i++)
  402. points[i] += offset;
  403. }
  404. }
  405. void afxPathData::derive_rolls_array()
  406. {
  407. if (roll_string == ST_NULLSTRING)
  408. return;
  409. if (rolls)
  410. {
  411. delete [] rolls;
  412. rolls = 0;
  413. }
  414. Vector<F32> values;
  415. extract_floats_from_string(values, roll_string);
  416. if (values.size() == 0)
  417. return;
  418. if (values.size() != num_points)
  419. {
  420. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total roll values is not equal to total points, rolls ignored.", getName());
  421. return;
  422. }
  423. U32 num_rolls = 0;
  424. rolls = build_floats_array(values, num_rolls, reverse);
  425. AssertFatal(num_rolls == num_points, "Unexpected error: num_rolls disagrees with num_points.");
  426. }
  427. void afxPathData::derive_times_array()
  428. {
  429. if (times_string == ST_NULLSTRING)
  430. return;
  431. if (times)
  432. {
  433. delete [] times;
  434. times = 0;
  435. }
  436. Vector<F32> values;
  437. extract_floats_from_string(values, times_string);
  438. if (values.size() == 0)
  439. return;
  440. if (values.size() != num_points)
  441. {
  442. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total time values is not equal to total points, times ignored", getName());
  443. return;
  444. }
  445. U32 num_times = 0;
  446. times = build_floats_array(values, num_times, reverse);
  447. AssertFatal(num_times == num_points, "Unexpected error: num_times disagrees with num_points.");
  448. }
  449. void afxPathData::onPerformSubstitutions()
  450. {
  451. Parent::onPerformSubstitutions();
  452. update_derived_values();
  453. }
  454. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//