staticShape.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/dnet.h"
  24. #include "core/stream/bitStream.h"
  25. #include "app/game.h"
  26. #include "math/mMath.h"
  27. #include "console/simBase.h"
  28. #include "console/console.h"
  29. #include "console/consoleTypes.h"
  30. #include "T3D/gameBase/moveManager.h"
  31. #include "ts/tsShapeInstance.h"
  32. #include "T3D/staticShape.h"
  33. #include "math/mathIO.h"
  34. #include "sim/netConnection.h"
  35. #include "scene/sceneObjectLightingPlugin.h"
  36. extern void wireCube(F32 size,Point3F pos);
  37. static const U32 sgAllowedDynamicTypes = 0xffffff;
  38. //----------------------------------------------------------------------------
  39. IMPLEMENT_CO_DATABLOCK_V1(StaticShapeData);
  40. ConsoleDocClass( StaticShapeData,
  41. "@brief The most basic ShapeBaseData derrived shape datablock available in Torque 3D.\n\n"
  42. "When it comes to placing 3D objects in the scene, you effectively have two options:\n\n"
  43. "1. TSStatic objects\n\n"
  44. "2. ShapeBase derived objects\n\n"
  45. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  46. "will use one of its child classes instead. Several game related objects are derived "
  47. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  48. "When you need a 3D object with datablock capabilities, you will use an object derived "
  49. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  50. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  51. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  52. "additional functionality you see in Player, Item, Vehicle or the other game play "
  53. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  54. "a datablock provides a location for common variables as well as having access to "
  55. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  56. "@tsexample\n"
  57. "// Create a StaticShape using a datablock\n"
  58. "datablock StaticShapeData(BasicShapeData)\n"
  59. "{\n"
  60. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  61. " testVar = \"Simple string, not a stock variable\";\n"
  62. "};\n\n"
  63. "new StaticShape()\n"
  64. "{\n"
  65. " dataBlock = \"BasicShapeData\";\n"
  66. " position = \"0.0 0.0 0.0\";\n"
  67. " rotation = \"1 0 0 0\";\n"
  68. " scale = \"1 1 1\";\n"
  69. "};\n"
  70. "@endtsexample\n\n"
  71. "@see StaticShape\n"
  72. "@see ShapeBaseData\n"
  73. "@see TSStatic\n\n"
  74. "@ingroup gameObjects\n"
  75. "@ingroup Datablocks");
  76. StaticShapeData::StaticShapeData()
  77. {
  78. dynamicTypeField = 0;
  79. shadowEnable = true;
  80. noIndividualDamage = false;
  81. }
  82. void StaticShapeData::initPersistFields()
  83. {
  84. addField("noIndividualDamage", TypeBool, Offset(noIndividualDamage, StaticShapeData), "Deprecated\n\n @internal");
  85. addField("dynamicType", TypeS32, Offset(dynamicTypeField, StaticShapeData),
  86. "@brief An integer value which, if speficied, is added to the value retured by getType().\n\n"
  87. "This allows you to extend the type mask for a StaticShape that uses this datablock. Type masks "
  88. "are used for container queries, etc.");
  89. Parent::initPersistFields();
  90. }
  91. void StaticShapeData::packData(BitStream* stream)
  92. {
  93. Parent::packData(stream);
  94. stream->writeFlag(noIndividualDamage);
  95. stream->write(dynamicTypeField);
  96. }
  97. void StaticShapeData::unpackData(BitStream* stream)
  98. {
  99. Parent::unpackData(stream);
  100. noIndividualDamage = stream->readFlag();
  101. stream->read(&dynamicTypeField);
  102. }
  103. //----------------------------------------------------------------------------
  104. IMPLEMENT_CO_NETOBJECT_V1(StaticShape);
  105. ConsoleDocClass( StaticShape,
  106. "@brief The most basic 3D shape with a datablock available in Torque 3D.\n\n"
  107. "When it comes to placing 3D objects in the scene, you technically have two options:\n\n"
  108. "1. TSStatic objects\n\n"
  109. "2. ShapeBase derived objects\n\n"
  110. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  111. "will use one of its child classes instead. Several game related objects are derived "
  112. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  113. "When you need a 3D object with datablock capabilities, you will use an object derived "
  114. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  115. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  116. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  117. "additional functionality you see in Player, Item, Vehicle or the other game play "
  118. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  119. "a datablock provides a location for common variables as well as having access to "
  120. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  121. "@tsexample\n"
  122. "// Create a StaticShape using a datablock\n"
  123. "datablock StaticShapeData(BasicShapeData)\n"
  124. "{\n"
  125. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  126. " testVar = \"Simple string, not a stock variable\";\n"
  127. "};\n\n"
  128. "new StaticShape()\n"
  129. "{\n"
  130. " dataBlock = \"BasicShapeData\";\n"
  131. " position = \"0.0 0.0 0.0\";\n"
  132. " rotation = \"1 0 0 0\";\n"
  133. " scale = \"1 1 1\";\n"
  134. "};\n"
  135. "@endtsexample\n\n"
  136. "@see StaticShapeData\n"
  137. "@see ShapeBase\n"
  138. "@see TSStatic\n\n"
  139. "@ingroup gameObjects\n");
  140. StaticShape::StaticShape()
  141. {
  142. mTypeMask |= StaticShapeObjectType | StaticObjectType;
  143. mDataBlock = 0;
  144. }
  145. StaticShape::~StaticShape()
  146. {
  147. }
  148. //----------------------------------------------------------------------------
  149. void StaticShape::initPersistFields()
  150. {
  151. Parent::initPersistFields();
  152. }
  153. bool StaticShape::onAdd()
  154. {
  155. if(!Parent::onAdd() || !mDataBlock)
  156. return false;
  157. // We need to modify our type mask based on what our datablock says...
  158. mTypeMask |= (mDataBlock->dynamicTypeField & sgAllowedDynamicTypes);
  159. addToScene();
  160. if (isServerObject())
  161. scriptOnAdd();
  162. return true;
  163. }
  164. bool StaticShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  165. {
  166. mDataBlock = dynamic_cast<StaticShapeData*>(dptr);
  167. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  168. return false;
  169. scriptOnNewDataBlock();
  170. return true;
  171. }
  172. void StaticShape::onRemove()
  173. {
  174. scriptOnRemove();
  175. removeFromScene();
  176. Parent::onRemove();
  177. }
  178. //----------------------------------------------------------------------------
  179. void StaticShape::processTick(const Move* move)
  180. {
  181. Parent::processTick(move);
  182. // Image Triggers
  183. if (move && mDamageState == Enabled) {
  184. setImageTriggerState(0,move->trigger[0]);
  185. setImageTriggerState(1,move->trigger[1]);
  186. }
  187. if (isMounted()) {
  188. MatrixF mat;
  189. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  190. Parent::setTransform(mat);
  191. Parent::setRenderTransform(mat);
  192. }
  193. }
  194. void StaticShape::interpolateTick(F32 delta)
  195. {
  196. if (isMounted()) {
  197. MatrixF mat;
  198. mMount.object->getRenderMountTransform( delta, mMount.node, mMount.xfm, &mat );
  199. Parent::setRenderTransform(mat);
  200. }
  201. }
  202. void StaticShape::setTransform(const MatrixF& mat)
  203. {
  204. Parent::setTransform(mat);
  205. setMaskBits(PositionMask);
  206. }
  207. void StaticShape::onUnmount(ShapeBase*,S32)
  208. {
  209. // Make sure the client get's the final server pos.
  210. setMaskBits(PositionMask);
  211. }
  212. //----------------------------------------------------------------------------
  213. U32 StaticShape::packUpdate(NetConnection *connection, U32 mask, BitStream *bstream)
  214. {
  215. U32 retMask = Parent::packUpdate(connection,mask,bstream);
  216. if (bstream->writeFlag(mask & PositionMask | ExtendedInfoMask))
  217. {
  218. // Write the transform (do _not_ use writeAffineTransform. Since this is a static
  219. // object, the transform must be RIGHT THE *&)*$&^ ON or it will goof up the
  220. // synchronization between the client and the server.
  221. mathWrite(*bstream,mObjToWorld);
  222. mathWrite(*bstream, mObjScale);
  223. }
  224. // powered?
  225. bstream->writeFlag(mPowered);
  226. if (mLightPlugin)
  227. {
  228. retMask |= mLightPlugin->packUpdate(this, ExtendedInfoMask, connection, mask, bstream);
  229. }
  230. return retMask;
  231. }
  232. void StaticShape::unpackUpdate(NetConnection *connection, BitStream *bstream)
  233. {
  234. Parent::unpackUpdate(connection,bstream);
  235. if (bstream->readFlag())
  236. {
  237. MatrixF mat;
  238. mathRead(*bstream,&mat);
  239. Parent::setTransform(mat);
  240. Parent::setRenderTransform(mat);
  241. VectorF scale;
  242. mathRead(*bstream, &scale);
  243. setScale(scale);
  244. }
  245. // powered?
  246. mPowered = bstream->readFlag();
  247. if (mLightPlugin)
  248. {
  249. mLightPlugin->unpackUpdate(this, connection, bstream);
  250. }
  251. }
  252. //----------------------------------------------------------------------------
  253. // This appears to be legacy T2 stuff
  254. // Marked internal, as this is flagged to be deleted
  255. // [8/1/2010 mperry]
  256. ConsoleMethod( StaticShape, setPoweredState, void, 3, 3, "(bool isPowered)"
  257. "@internal")
  258. {
  259. if(!object->isServerObject())
  260. return;
  261. object->setPowered(dAtob(argv[2]));
  262. }
  263. ConsoleMethod( StaticShape, getPoweredState, bool, 2, 2, "@internal")
  264. {
  265. if(!object->isServerObject())
  266. return(false);
  267. return(object->isPowered());
  268. }