afxPath.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. docsURL;
  126. addField("points", TypeString, Offset(points_string, afxPathData),
  127. "...");
  128. addField("roll", TypeString, Offset(roll_string, afxPathData),
  129. "...");
  130. addField("times", TypeString, Offset(times_string, afxPathData),
  131. "...");
  132. addField("loop", TypeString, Offset(loop_string, afxPathData),
  133. "...");
  134. addField("mult", TypeF32, Offset(mult, afxPathData),
  135. "...");
  136. addField("delay", TypeF32, Offset(delay, afxPathData),
  137. "...");
  138. addField("lifetime", TypeF32, Offset(lifetime, afxPathData),
  139. "...");
  140. addField("timeOffset", TypeF32, Offset(time_offset, afxPathData),
  141. "...");
  142. addField("reverse", TypeBool, Offset(reverse, afxPathData),
  143. "...");
  144. addField("offset", TypePoint3F, Offset(offset, afxPathData),
  145. "...");
  146. addField("echo", TypeBool, Offset(echo, afxPathData),
  147. "...");
  148. addField("concentric", TypeBool, Offset(concentric, afxPathData),
  149. "...");
  150. Parent::initPersistFields();
  151. }
  152. bool afxPathData::onAdd()
  153. {
  154. if (Parent::onAdd() == false)
  155. return false;
  156. update_derived_values();
  157. return true;
  158. }
  159. void afxPathData::onRemove()
  160. {
  161. clear_arrays();
  162. loop_type = 0;
  163. Parent::onRemove();
  164. }
  165. void afxPathData::packData(BitStream* stream)
  166. {
  167. Parent::packData(stream);
  168. stream->write(num_points);
  169. if (num_points > 0)
  170. {
  171. for (U32 i = 0; i < num_points; i++)
  172. mathWrite(*stream, points[i]);
  173. if (stream->writeFlag(rolls != 0))
  174. {
  175. for (U32 i = 0; i < num_points; i++)
  176. stream->write(rolls[i]);
  177. }
  178. if (stream->writeFlag(times != 0))
  179. {
  180. for (U32 i = 0; i < num_points; i++)
  181. stream->write(times[i]);
  182. }
  183. }
  184. stream->writeString(loop_string);
  185. stream->write(delay);
  186. stream->write(lifetime);
  187. stream->write(time_offset);
  188. stream->write(mult);
  189. stream->writeFlag(reverse);
  190. mathWrite(*stream, offset);
  191. stream->writeFlag(echo);
  192. stream->writeFlag(concentric);
  193. }
  194. void afxPathData::unpackData(BitStream* stream)
  195. {
  196. Parent::unpackData(stream);
  197. clear_arrays();
  198. // read the points and rolls
  199. stream->read(&num_points);
  200. if (num_points > 0)
  201. {
  202. points = new Point3F[num_points];
  203. for (U32 i = 0; i < num_points; i++)
  204. mathRead(*stream, &points[i]);
  205. update_points = false;
  206. if (stream->readFlag())
  207. {
  208. rolls = new F32[num_points];
  209. for (U32 i = 0; i < num_points; i++)
  210. stream->read(&rolls[i]);
  211. update_rolls = false;
  212. }
  213. if (stream->readFlag())
  214. {
  215. times = new F32[num_points];
  216. for (U32 i = 0; i < num_points; i++)
  217. stream->read(&times[i]);
  218. update_times = false;
  219. }
  220. }
  221. loop_string = stream->readSTString();
  222. stream->read(&delay);
  223. stream->read(&lifetime);
  224. stream->read(&time_offset);
  225. stream->read(&mult);
  226. reverse = stream->readFlag();
  227. mathRead(*stream, &offset);
  228. echo = stream->readFlag();
  229. concentric = stream->readFlag();
  230. }
  231. void afxPathData::update_derived_values()
  232. {
  233. U32 num_rolls = (rolls != 0) ? num_points : 0;
  234. U32 num_times = (times != 0) ? num_points : 0;
  235. if (update_points)
  236. {
  237. derive_points_array();
  238. update_points = false;
  239. }
  240. if (update_rolls || num_rolls != num_points)
  241. {
  242. derive_rolls_array();
  243. update_rolls = false;
  244. }
  245. if (update_times || num_times != num_points)
  246. {
  247. derive_times_array();
  248. update_times = false;
  249. }
  250. // CAUTION: The following block of code is fragile and tricky since it depends
  251. // on the underlying structures defined by ImplementEnumType/EndImplementEnumType.
  252. // This done because the enum text is parsed from a longer string.
  253. if (loop_string != ST_NULLSTRING)
  254. {
  255. for (unsigned int i = 0; i < _afxPath3DLoopType::_sEnumTable.getNumValues(); i++)
  256. {
  257. if (dStricmp(_afxPath3DLoopType::_sEnumTable[i].mName, loop_string) == 0)
  258. {
  259. loop_type = _afxPath3DLoopType::_sEnumTable[i].mInt;
  260. break;
  261. }
  262. }
  263. }
  264. else
  265. {
  266. loop_string = _afxPath3DLoopType::_sEnumTable[0].mName;
  267. loop_type = _afxPath3DLoopType::_sEnumTable[0].mInt;
  268. }
  269. }
  270. bool afxPathData::preload(bool server, String &errorStr)
  271. {
  272. if (!Parent::preload(server, errorStr))
  273. return false;
  274. update_derived_values();
  275. return true;
  276. }
  277. void afxPathData::onStaticModified(const char* slot, const char* newValue)
  278. {
  279. Parent::onStaticModified(slot, newValue);
  280. if (slot == POINTS_FIELD)
  281. {
  282. update_points = true;
  283. return;
  284. }
  285. if (slot == ROLL_FIELD)
  286. {
  287. update_rolls = true;
  288. return;
  289. }
  290. if (slot == TIMES_FIELD)
  291. {
  292. update_times = true;
  293. return;
  294. }
  295. }
  296. void afxPathData::extract_floats_from_string(Vector<F32>& values, const char* points_str)
  297. {
  298. values.clear();
  299. if (!points_str)
  300. return;
  301. // make a copy of points_str for tokenizing
  302. char* tokCopy = dStrdup(points_str);
  303. // extract each token, convert to float, add to values[]
  304. char* currTok = dStrtok(tokCopy, " \t");
  305. while (currTok != NULL)
  306. {
  307. F32 value = dAtof(currTok);
  308. values.push_back(value);
  309. currTok = dStrtok(NULL, " \t");
  310. }
  311. dFree(tokCopy);
  312. }
  313. Point3F* afxPathData::build_points_array(Vector<F32>& values, U32& n_points, bool reverse)
  314. {
  315. AssertFatal(values.size() > 0, "Values array is empty.");
  316. AssertFatal(values.size()%3 == 0, "Values array is not a multiple of 3.");
  317. n_points = values.size()/3;
  318. Point3F* points = new Point3F[n_points];
  319. if (reverse)
  320. {
  321. U32 p_i = 0;
  322. for (S32 i = values.size()-1; i > 1; i-=3)
  323. points[p_i++].set(values[i-2], values[i-1], values[i]);
  324. }
  325. else
  326. {
  327. U32 p_i = 0;
  328. for (U32 i = 0; i < values.size(); i+=3)
  329. points[p_i++].set(values[i], values[i+1], values[i+2]);
  330. }
  331. return points;
  332. }
  333. F32* afxPathData::build_floats_array(Vector<F32>& values, U32& n_floats, bool reverse)
  334. {
  335. AssertFatal(values.size() > 0, "Values array is empty.");
  336. n_floats = values.size();
  337. F32* floats = new F32[n_floats];
  338. if (reverse)
  339. {
  340. F32* f = floats;
  341. for (S32 i = values.size()-1; i >= 0; i--)
  342. {
  343. *f = values[i];
  344. f++;
  345. }
  346. }
  347. else
  348. {
  349. for (U32 i = 0; i < values.size(); i++)
  350. floats[i] = values[i];
  351. }
  352. return floats;
  353. }
  354. void afxPathData::clear_arrays()
  355. {
  356. num_points = 0;
  357. if (points)
  358. {
  359. delete [] points;
  360. points = 0;
  361. }
  362. if (rolls)
  363. {
  364. delete [] rolls;
  365. rolls = 0;
  366. }
  367. if (times)
  368. {
  369. delete [] times;
  370. times = 0;
  371. }
  372. update_points = true;
  373. update_rolls = true;
  374. update_times = true;
  375. }
  376. void afxPathData::derive_points_array()
  377. {
  378. if (points_string == ST_NULLSTRING)
  379. return;
  380. if (points)
  381. {
  382. delete [] points;
  383. points = 0;
  384. }
  385. num_points = 0;
  386. Vector<F32> values;
  387. extract_floats_from_string(values, points_string);
  388. if (values.size() == 0)
  389. {
  390. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) empty points field, datablock is invalid.", getName());
  391. return;
  392. }
  393. if (values.size()%3 != 0)
  394. {
  395. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total points values is not a multiple of 3, datablock is invalid.", getName());
  396. return;
  397. }
  398. points = build_points_array(values, num_points, reverse);
  399. if (offset.x != 0.0f || offset.y != 0.0f || offset.z != 0.0f)
  400. {
  401. // add offset here for efficiency (saves an addition from afxXM_PathConform)
  402. for (U32 i = 0; i < num_points; i++)
  403. points[i] += offset;
  404. }
  405. }
  406. void afxPathData::derive_rolls_array()
  407. {
  408. if (roll_string == ST_NULLSTRING)
  409. return;
  410. if (rolls)
  411. {
  412. delete [] rolls;
  413. rolls = 0;
  414. }
  415. Vector<F32> values;
  416. extract_floats_from_string(values, roll_string);
  417. if (values.size() == 0)
  418. return;
  419. if (values.size() != num_points)
  420. {
  421. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total roll values is not equal to total points, rolls ignored.", getName());
  422. return;
  423. }
  424. U32 num_rolls = 0;
  425. rolls = build_floats_array(values, num_rolls, reverse);
  426. AssertFatal(num_rolls == num_points, "Unexpected error: num_rolls disagrees with num_points.");
  427. }
  428. void afxPathData::derive_times_array()
  429. {
  430. if (times_string == ST_NULLSTRING)
  431. return;
  432. if (times)
  433. {
  434. delete [] times;
  435. times = 0;
  436. }
  437. Vector<F32> values;
  438. extract_floats_from_string(values, times_string);
  439. if (values.size() == 0)
  440. return;
  441. if (values.size() != num_points)
  442. {
  443. Con::warnf(ConsoleLogEntry::General, "afxPathData(%s) total time values is not equal to total points, times ignored", getName());
  444. return;
  445. }
  446. U32 num_times = 0;
  447. times = build_floats_array(values, num_times, reverse);
  448. AssertFatal(num_times == num_points, "Unexpected error: num_times disagrees with num_points.");
  449. }
  450. void afxPathData::onPerformSubstitutions()
  451. {
  452. Parent::onPerformSubstitutions();
  453. update_derived_values();
  454. }
  455. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//