afxMooring.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "math/mathIO.h"
  26. #include "afx/afxChoreographer.h"
  27. #include "afx/ce/afxMooring.h"
  28. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  29. // afxMooringData
  30. IMPLEMENT_CO_DATABLOCK_V1(afxMooringData);
  31. ConsoleDocClass( afxMooringData,
  32. "@brief A datablock that specifies a Mooring effect.\n\n"
  33. "A Mooring is an invisible effect object which can be positioned and oriented within a scene like other objects. Its main "
  34. "purpose is to serve as a common mount point for other effects within the same choreographer. Typically one uses AFX "
  35. "animation features to create movement for a Mooring and then other effects are bound to it using effect-to-effect "
  36. "constraints (#effect)."
  37. "\n\n"
  38. "@ingroup afxEffects\n"
  39. "@ingroup AFX\n"
  40. "@ingroup Datablocks\n"
  41. );
  42. afxMooringData::afxMooringData()
  43. {
  44. track_pos_only = false;
  45. networking = SCOPE_ALWAYS;
  46. display_axis_marker = false;
  47. }
  48. afxMooringData::afxMooringData(const afxMooringData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  49. {
  50. track_pos_only = other.track_pos_only;
  51. networking = other.networking;
  52. display_axis_marker = other.display_axis_marker;
  53. }
  54. #define myOffset(field) Offset(field, afxMooringData)
  55. void afxMooringData::initPersistFields()
  56. {
  57. docsURL;
  58. addField("displayAxisMarker", TypeBool, myOffset(display_axis_marker),
  59. "Specifies whether to display an axis to help visualize the position and orientation "
  60. "of the mooring.");
  61. addField("trackPosOnly", TypeBool, myOffset(track_pos_only),
  62. "This field is only meaningful for networking settings of SCOPE_ALWAYS and GHOSTABLE. "
  63. "In these cases, client moorings are ghosting a mooring on the server, and "
  64. "trackPosOnly determines if the client moorings need to be updated with the server "
  65. "mooring's complete transform or just its position. If only the position needs to be "
  66. "tracked, setting trackPosOnly to true will reduce the network traffic.");
  67. addField("networking", TypeS8, myOffset(networking),
  68. "Specifies the networking model used for the mooring and should be one of: "
  69. "$AFX::SCOPE_ALWAYS, $AFX::GHOSTABLE, $AFX::SERVER_ONLY, or $AFX::CLIENT_ONLY");
  70. Parent::initPersistFields();
  71. }
  72. bool afxMooringData::onAdd()
  73. {
  74. if (Parent::onAdd() == false)
  75. return false;
  76. return true;
  77. }
  78. void afxMooringData::packData(BitStream* stream)
  79. {
  80. Parent::packData(stream);
  81. stream->write(display_axis_marker);
  82. stream->write(track_pos_only);
  83. stream->write(networking);
  84. }
  85. void afxMooringData::unpackData(BitStream* stream)
  86. {
  87. Parent::unpackData(stream);
  88. stream->read(&display_axis_marker);
  89. stream->read(&track_pos_only);
  90. stream->read(&networking);
  91. }
  92. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  93. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  94. // afxMooring
  95. IMPLEMENT_CO_NETOBJECT_V1(afxMooring);
  96. ConsoleDocClass( afxMooring,
  97. "@brief A Mooring effect as defined by an afxMooringData datablock.\n\n"
  98. "A Mooring is an invisible effect object which can be positioned and oriented within "
  99. "a scene like other objects. Its main purpose is to serve as a common mount point for "
  100. "other effects within the same choreographer. Typically one uses AFX animation "
  101. "features to create movement for a Mooring and then other effects are bound to it "
  102. "using effect-to-effect constraints (#effect).\n"
  103. "@ingroup afxEffects\n"
  104. "@ingroup AFX\n"
  105. );
  106. afxMooring::afxMooring()
  107. {
  108. mNetFlags.set(Ghostable | ScopeAlways);
  109. chor_id = 0;
  110. hookup_with_chor = false;
  111. ghost_cons_name = ST_NULLSTRING;
  112. mDataBlock = NULL;
  113. }
  114. afxMooring::afxMooring(U32 networking, U32 chor_id, StringTableEntry cons_name)
  115. {
  116. if (networking & SCOPE_ALWAYS)
  117. {
  118. mNetFlags.clear();
  119. mNetFlags.set(Ghostable | ScopeAlways);
  120. }
  121. else if (networking & GHOSTABLE)
  122. {
  123. mNetFlags.clear();
  124. mNetFlags.set(Ghostable);
  125. }
  126. else if (networking & SERVER_ONLY)
  127. {
  128. mNetFlags.clear();
  129. }
  130. else // if (networking & CLIENT_ONLY)
  131. {
  132. mNetFlags.clear();
  133. mNetFlags.set(IsGhost);
  134. }
  135. this->chor_id = chor_id;
  136. hookup_with_chor = false;
  137. this->ghost_cons_name = cons_name;
  138. mDataBlock = NULL;
  139. }
  140. afxMooring::~afxMooring()
  141. {
  142. }
  143. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  144. bool afxMooring::onNewDataBlock(GameBaseData* dptr, bool reload)
  145. {
  146. mDataBlock = dynamic_cast<afxMooringData*>(dptr);
  147. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  148. return false;
  149. return true;
  150. }
  151. void afxMooring::advanceTime(F32 dt)
  152. {
  153. Parent::advanceTime(dt);
  154. if (hookup_with_chor)
  155. {
  156. afxChoreographer* chor = arcaneFX::findClientChoreographer(chor_id);
  157. if (chor)
  158. {
  159. chor->setGhostConstraintObject(this, ghost_cons_name);
  160. hookup_with_chor = false;
  161. }
  162. }
  163. Point3F pos = getRenderPosition();
  164. }
  165. U32 afxMooring::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
  166. {
  167. U32 retMask = Parent::packUpdate(conn, mask, stream);
  168. // InitialUpdate
  169. if (stream->writeFlag(mask & InitialUpdateMask))
  170. {
  171. stream->write(chor_id);
  172. stream->writeString(ghost_cons_name);
  173. }
  174. if (stream->writeFlag(mask & PositionMask))
  175. {
  176. if (mDataBlock->track_pos_only)
  177. mathWrite(*stream, mObjToWorld.getPosition());
  178. else
  179. stream->writeAffineTransform(mObjToWorld);
  180. }
  181. return retMask;
  182. }
  183. //~~~~~~~~~~~~~~~~~~~~//
  184. void afxMooring::unpackUpdate(NetConnection * conn, BitStream * stream)
  185. {
  186. Parent::unpackUpdate(conn, stream);
  187. // InitialUpdate
  188. if (stream->readFlag())
  189. {
  190. stream->read(&chor_id);
  191. ghost_cons_name = stream->readSTString();
  192. if (chor_id != 0 && ghost_cons_name != ST_NULLSTRING)
  193. hookup_with_chor = true;
  194. }
  195. if (stream->readFlag())
  196. {
  197. if (mDataBlock->track_pos_only)
  198. {
  199. Point3F pos;
  200. mathRead(*stream, &pos);
  201. setPosition(pos);
  202. }
  203. else
  204. {
  205. MatrixF mat;
  206. stream->readAffineTransform(&mat);
  207. setTransform(mat);
  208. setRenderTransform(mat);
  209. }
  210. }
  211. }
  212. void afxMooring::setTransform(const MatrixF& mat)
  213. {
  214. Parent::setTransform(mat);
  215. setMaskBits(PositionMask);
  216. }
  217. bool afxMooring::onAdd()
  218. {
  219. if(!Parent::onAdd())
  220. return false;
  221. mObjBox = Box3F(Point3F(-0.5, -0.5, -0.5), Point3F(0.5, 0.5, 0.5));
  222. addToScene();
  223. return true;
  224. }
  225. void afxMooring::onRemove()
  226. {
  227. removeFromScene();
  228. Parent::onRemove();
  229. }
  230. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//