staticShape.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "core/dnet.h"
  28. #include "core/stream/bitStream.h"
  29. #include "app/game.h"
  30. #include "math/mMath.h"
  31. #include "console/simBase.h"
  32. #include "console/console.h"
  33. #include "console/consoleTypes.h"
  34. #include "console/engineAPI.h"
  35. #include "T3D/gameBase/moveManager.h"
  36. #include "ts/tsShapeInstance.h"
  37. #include "T3D/staticShape.h"
  38. #include "math/mathIO.h"
  39. #include "sim/netConnection.h"
  40. #include "scene/sceneObjectLightingPlugin.h"
  41. extern void wireCube(F32 size,Point3F pos);
  42. static const U32 sgAllowedDynamicTypes = 0xffffff;
  43. //----------------------------------------------------------------------------
  44. IMPLEMENT_CO_DATABLOCK_V1(StaticShapeData);
  45. ConsoleDocClass( StaticShapeData,
  46. "@brief The most basic ShapeBaseData derrived shape datablock available in Torque 3D.\n\n"
  47. "When it comes to placing 3D objects in the scene, you effectively have two options:\n\n"
  48. "1. TSStatic objects\n\n"
  49. "2. ShapeBase derived objects\n\n"
  50. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  51. "will use one of its child classes instead. Several game related objects are derived "
  52. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  53. "When you need a 3D object with datablock capabilities, you will use an object derived "
  54. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  55. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  56. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  57. "additional functionality you see in Player, Item, Vehicle or the other game play "
  58. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  59. "a datablock provides a location for common variables as well as having access to "
  60. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  61. "@tsexample\n"
  62. "// Create a StaticShape using a datablock\n"
  63. "datablock StaticShapeData(BasicShapeData)\n"
  64. "{\n"
  65. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  66. " testVar = \"Simple string, not a stock variable\";\n"
  67. "};\n\n"
  68. "new StaticShape()\n"
  69. "{\n"
  70. " dataBlock = \"BasicShapeData\";\n"
  71. " position = \"0.0 0.0 0.0\";\n"
  72. " rotation = \"1 0 0 0\";\n"
  73. " scale = \"1 1 1\";\n"
  74. "};\n"
  75. "@endtsexample\n\n"
  76. "@see StaticShape\n"
  77. "@see ShapeBaseData\n"
  78. "@see TSStatic\n\n"
  79. "@ingroup gameObjects\n"
  80. "@ingroup Datablocks");
  81. StaticShapeData::StaticShapeData()
  82. {
  83. dynamicTypeField = 0;
  84. noIndividualDamage = false;
  85. }
  86. StaticShapeData::StaticShapeData(const StaticShapeData& other, bool temp_clone) : ShapeBaseData(other, temp_clone)
  87. {
  88. noIndividualDamage = other.noIndividualDamage;
  89. dynamicTypeField = other.dynamicTypeField;
  90. isShielded = other.isShielded; // -- uninitialized, unused
  91. energyPerDamagePoint = other.energyPerDamagePoint; // -- uninitialized, unused
  92. }
  93. void StaticShapeData::initPersistFields()
  94. {
  95. docsURL;
  96. Parent::initPersistFields();
  97. addField("noIndividualDamage", TypeBool, Offset(noIndividualDamage, StaticShapeData), "Deprecated\n\n @internal");
  98. addField("dynamicType", TypeS32, Offset(dynamicTypeField, StaticShapeData),
  99. "@brief An integer value which, if speficied, is added to the value retured by getType().\n\n"
  100. "This allows you to extend the type mask for a StaticShape that uses this datablock. Type masks "
  101. "are used for container queries, etc.");
  102. }
  103. void StaticShapeData::packData(BitStream* stream)
  104. {
  105. Parent::packData(stream);
  106. stream->writeFlag(noIndividualDamage);
  107. stream->write(dynamicTypeField);
  108. }
  109. void StaticShapeData::unpackData(BitStream* stream)
  110. {
  111. Parent::unpackData(stream);
  112. noIndividualDamage = stream->readFlag();
  113. stream->read(&dynamicTypeField);
  114. }
  115. //----------------------------------------------------------------------------
  116. IMPLEMENT_CO_NETOBJECT_V1(StaticShape);
  117. ConsoleDocClass( StaticShape,
  118. "@brief The most basic 3D shape with a datablock available in Torque 3D.\n\n"
  119. "When it comes to placing 3D objects in the scene, you technically have two options:\n\n"
  120. "1. TSStatic objects\n\n"
  121. "2. ShapeBase derived objects\n\n"
  122. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  123. "will use one of its child classes instead. Several game related objects are derived "
  124. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  125. "When you need a 3D object with datablock capabilities, you will use an object derived "
  126. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  127. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  128. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  129. "additional functionality you see in Player, Item, Vehicle or the other game play "
  130. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  131. "a datablock provides a location for common variables as well as having access to "
  132. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  133. "@tsexample\n"
  134. "// Create a StaticShape using a datablock\n"
  135. "datablock StaticShapeData(BasicShapeData)\n"
  136. "{\n"
  137. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  138. " testVar = \"Simple string, not a stock variable\";\n"
  139. "};\n\n"
  140. "new StaticShape()\n"
  141. "{\n"
  142. " dataBlock = \"BasicShapeData\";\n"
  143. " position = \"0.0 0.0 0.0\";\n"
  144. " rotation = \"1 0 0 0\";\n"
  145. " scale = \"1 1 1\";\n"
  146. "};\n"
  147. "@endtsexample\n\n"
  148. "@see StaticShapeData\n"
  149. "@see ShapeBase\n"
  150. "@see TSStatic\n\n"
  151. "@ingroup gameObjects\n");
  152. StaticShape::StaticShape()
  153. {
  154. mTypeMask |= StaticShapeObjectType | StaticObjectType;
  155. mDataBlock = 0;
  156. }
  157. StaticShape::~StaticShape()
  158. {
  159. }
  160. //----------------------------------------------------------------------------
  161. void StaticShape::initPersistFields()
  162. {
  163. docsURL;
  164. Parent::initPersistFields();
  165. }
  166. bool StaticShape::onAdd()
  167. {
  168. if(!Parent::onAdd() || !mDataBlock)
  169. return false;
  170. // We need to modify our type mask based on what our datablock says...
  171. mTypeMask |= (mDataBlock->dynamicTypeField & sgAllowedDynamicTypes);
  172. addToScene();
  173. if (isServerObject())
  174. scriptOnAdd();
  175. return true;
  176. }
  177. bool StaticShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  178. {
  179. mDataBlock = dynamic_cast<StaticShapeData*>(dptr);
  180. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  181. return false;
  182. scriptOnNewDataBlock();
  183. return true;
  184. }
  185. void StaticShape::onRemove()
  186. {
  187. scriptOnRemove();
  188. removeFromScene();
  189. Parent::onRemove();
  190. }
  191. //----------------------------------------------------------------------------
  192. void StaticShape::processTick(const Move* move)
  193. {
  194. Parent::processTick(move);
  195. // Image Triggers
  196. if (move && mDamageState == Enabled) {
  197. setImageTriggerState(0,move->trigger[0]);
  198. setImageTriggerState(1,move->trigger[1]);
  199. }
  200. }
  201. void StaticShape::interpolateTick(F32 delta)
  202. {
  203. Parent::interpolateTick(delta);
  204. // PATHSHAPE
  205. updateRenderChangesByParent();
  206. // PATHSHAPE END
  207. }
  208. void StaticShape::setTransform(const MatrixF& mat)
  209. {
  210. Parent::setTransform(mat);
  211. setMaskBits(PositionMask);
  212. }
  213. void StaticShape::onUnmount(SceneObject* obj, S32 node)
  214. {
  215. Parent::onUnmount(obj, node);
  216. // Make sure the client get's the final server pos.
  217. setMaskBits(PositionMask);
  218. }
  219. //----------------------------------------------------------------------------
  220. U32 StaticShape::packUpdate(NetConnection *connection, U32 mask, BitStream *bstream)
  221. {
  222. U32 retMask = Parent::packUpdate(connection,mask,bstream);
  223. if (bstream->writeFlag(mask & (PositionMask | ExtendedInfoMask)))
  224. {
  225. // Write the transform (do _not_ use writeAffineTransform. Since this is a static
  226. // object, the transform must be RIGHT THE *&)*$&^ ON or it will goof up the
  227. // synchronization between the client and the server.
  228. mathWrite(*bstream,mObjToWorld);
  229. mathWrite(*bstream, mObjScale);
  230. }
  231. // powered?
  232. bstream->writeFlag(mPowered);
  233. if (mLightPlugin)
  234. {
  235. retMask |= mLightPlugin->packUpdate(this, ExtendedInfoMask, connection, mask, bstream);
  236. }
  237. return retMask;
  238. }
  239. void StaticShape::unpackUpdate(NetConnection *connection, BitStream *bstream)
  240. {
  241. Parent::unpackUpdate(connection,bstream);
  242. if (bstream->readFlag())
  243. {
  244. MatrixF mat;
  245. mathRead(*bstream,&mat);
  246. Parent::setTransform(mat);
  247. Parent::setRenderTransform(mat);
  248. VectorF scale;
  249. mathRead(*bstream, &scale);
  250. setScale(scale);
  251. }
  252. // powered?
  253. mPowered = bstream->readFlag();
  254. if (mLightPlugin)
  255. {
  256. mLightPlugin->unpackUpdate(this, connection, bstream);
  257. }
  258. }
  259. //----------------------------------------------------------------------------
  260. // This appears to be legacy T2 stuff
  261. // Marked internal, as this is flagged to be deleted
  262. // [8/1/2010 mperry]
  263. DefineEngineMethod( StaticShape, setPoweredState, void, (bool isPowered), , "(bool isPowered)"
  264. "@internal")
  265. {
  266. if(!object->isServerObject())
  267. return;
  268. object->setPowered(isPowered);
  269. }
  270. DefineEngineMethod( StaticShape, getPoweredState, bool, (), , "@internal")
  271. {
  272. if(!object->isServerObject())
  273. return(false);
  274. return(object->isPowered());
  275. }