afxProjectile.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 "T3D/shapeBase.h"
  26. #include "afx/ce/afxProjectile.h"
  27. #include "afx/afxChoreographer.h"
  28. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. // afxProjectileData
  31. IMPLEMENT_CO_DATABLOCK_V1(afxProjectileData);
  32. ConsoleDocClass( afxProjectileData,
  33. "@brief A datablock that specifies a Projectile effect.\n\n"
  34. "afxProjectileData inherits from ProjectileData and adds some AFX specific fields."
  35. "\n\n"
  36. "@ingroup afxEffects\n"
  37. "@ingroup AFX\n"
  38. "@ingroup Datablocks\n"
  39. );
  40. afxProjectileData::afxProjectileData()
  41. {
  42. networking = GHOSTABLE;
  43. launch_pos_spec = ST_NULLSTRING;
  44. launch_dir_bias.zero();
  45. ignore_src_timeout = false;
  46. dynamicCollisionMask = 0;
  47. staticCollisionMask = 0;
  48. override_collision_masks = false;
  49. launch_dir_method = TowardPos2Constraint;
  50. }
  51. afxProjectileData::afxProjectileData(const afxProjectileData& other, bool temp_clone) : ProjectileData(other, temp_clone)
  52. {
  53. networking = other.networking;
  54. launch_pos_spec = other.launch_pos_spec;
  55. launch_pos_def = other.launch_pos_def;
  56. launch_dir_bias = other.launch_dir_bias;
  57. ignore_src_timeout = other.ignore_src_timeout;
  58. dynamicCollisionMask = other.dynamicCollisionMask;
  59. staticCollisionMask = other.staticCollisionMask;
  60. override_collision_masks = other.override_collision_masks;
  61. launch_dir_method = other.launch_dir_method;
  62. }
  63. ImplementEnumType( afxProjectile_LaunchDirType, "Possible projectile launch direction types.\n" "@ingroup afxProjectile\n\n" )
  64. { afxProjectileData::TowardPos2Constraint, "towardPos2Constraint", "..." },
  65. { afxProjectileData::OrientConstraint, "orientConstraint", "..." },
  66. { afxProjectileData::LaunchDirField, "launchDirField", "..." },
  67. EndImplementEnumType;
  68. #define myOffset(field) Offset(field, afxProjectileData)
  69. void afxProjectileData::initPersistFields()
  70. {
  71. addGroup("Physics");
  72. addField("ignoreSourceTimeout", TypeBool, myOffset(ignore_src_timeout), "...");
  73. addField("dynamicCollisionMask", TypeS32, myOffset(dynamicCollisionMask), "...");
  74. addField("staticCollisionMask", TypeS32, myOffset(staticCollisionMask), "...");
  75. addField("overrideCollisionMasks", TypeBool, myOffset(override_collision_masks), "...");
  76. endGroup("Physics");
  77. addGroup("Physics-Launch");
  78. addField("launchPosSpec", TypeString, myOffset(launch_pos_spec), "...");
  79. addField("launchDirBias", TypePoint3F, myOffset(launch_dir_bias), "...");
  80. addField("launchDirMethod", TYPEID<afxProjectileData::LaunchDirType>(), myOffset(launch_dir_method),
  81. "Possible values: towardPos2Constraint, orientConstraint, or launchDirField.");
  82. endGroup("Physics-Launch");
  83. addGroup("Networking");
  84. addField("networking", TypeS8, myOffset(networking), "...");
  85. endGroup("Networking");
  86. Parent::initPersistFields();
  87. }
  88. bool afxProjectileData::onAdd()
  89. {
  90. if (Parent::onAdd() == false)
  91. return false;
  92. bool runs_on_s = ((networking & (SERVER_ONLY | SERVER_AND_CLIENT)) != 0);
  93. bool runs_on_c = ((networking & (CLIENT_ONLY | SERVER_AND_CLIENT)) != 0);
  94. launch_pos_def.parseSpec(launch_pos_spec, runs_on_s, runs_on_c);
  95. return true;
  96. }
  97. void afxProjectileData::packData(BitStream* stream)
  98. {
  99. Parent::packData(stream);
  100. stream->write(networking);
  101. stream->writeString(launch_pos_spec);
  102. if (stream->writeFlag(!launch_dir_bias.isZero()))
  103. {
  104. stream->write(launch_dir_bias.x);
  105. stream->write(launch_dir_bias.y);
  106. stream->write(launch_dir_bias.z);
  107. }
  108. stream->writeFlag(ignore_src_timeout);
  109. if (stream->writeFlag(override_collision_masks))
  110. {
  111. stream->write(dynamicCollisionMask);
  112. stream->write(staticCollisionMask);
  113. }
  114. stream->writeInt(launch_dir_method, 2);
  115. }
  116. void afxProjectileData::unpackData(BitStream* stream)
  117. {
  118. Parent::unpackData(stream);
  119. stream->read(&networking);
  120. launch_pos_spec = stream->readSTString();
  121. if (stream->readFlag())
  122. {
  123. stream->read(&launch_dir_bias.x);
  124. stream->read(&launch_dir_bias.y);
  125. stream->read(&launch_dir_bias.z);
  126. }
  127. else
  128. launch_dir_bias.zero();
  129. ignore_src_timeout = stream->readFlag();
  130. if ((override_collision_masks = stream->readFlag()) == true)
  131. {
  132. stream->read(&dynamicCollisionMask);
  133. stream->read(&staticCollisionMask);
  134. }
  135. else
  136. {
  137. dynamicCollisionMask = 0;
  138. staticCollisionMask = 0;
  139. }
  140. launch_dir_method = (U32) stream->readInt(2);
  141. }
  142. void afxProjectileData::gather_cons_defs(Vector<afxConstraintDef>& defs)
  143. {
  144. if (launch_pos_def.isDefined())
  145. defs.push_back(launch_pos_def);
  146. };
  147. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  148. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  149. // afxProjectile
  150. IMPLEMENT_CO_NETOBJECT_V1(afxProjectile);
  151. ConsoleDocClass( afxProjectile,
  152. "@brief A Projectile effect as defined by an afxProjectileData datablock.\n\n"
  153. "@ingroup afxEffects\n"
  154. "@ingroup AFX\n"
  155. );
  156. afxProjectile::afxProjectile()
  157. {
  158. chor_id = 0;
  159. hookup_with_chor = false;
  160. ghost_cons_name = ST_NULLSTRING;
  161. client_only = false;
  162. }
  163. afxProjectile::afxProjectile(U32 networking, U32 chor_id, StringTableEntry cons_name)
  164. {
  165. if (networking & SCOPE_ALWAYS)
  166. {
  167. mNetFlags.clear();
  168. mNetFlags.set(Ghostable | ScopeAlways);
  169. client_only = false;
  170. }
  171. else if (networking & GHOSTABLE)
  172. {
  173. mNetFlags.clear();
  174. mNetFlags.set(Ghostable);
  175. client_only = false;
  176. }
  177. else if (networking & SERVER_ONLY)
  178. {
  179. mNetFlags.clear();
  180. client_only = false;
  181. }
  182. else // if (networking & CLIENT_ONLY)
  183. {
  184. mNetFlags.clear();
  185. mNetFlags.set(IsGhost);
  186. client_only = true;
  187. }
  188. this->chor_id = chor_id;
  189. hookup_with_chor = false;
  190. this->ghost_cons_name = cons_name;
  191. }
  192. afxProjectile::~afxProjectile()
  193. {
  194. }
  195. void afxProjectile::init(Point3F& pos, Point3F& vel, ShapeBase* src_obj)
  196. {
  197. mCurrPosition = pos;
  198. mCurrVelocity = vel;
  199. if (src_obj)
  200. {
  201. mSourceObject = src_obj;
  202. mSourceObjectId = src_obj->getId();
  203. mSourceObjectSlot = 0;
  204. }
  205. setPosition(mCurrPosition);
  206. }
  207. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  208. bool afxProjectile::onNewDataBlock(GameBaseData* dptr, bool reload)
  209. {
  210. return Parent::onNewDataBlock(dptr, reload);
  211. }
  212. void afxProjectile::processTick(const Move* move)
  213. {
  214. // note: this deletion test must occur before calling to the parent's
  215. // processTick() because if this is a server projectile, the parent
  216. // might decide to delete it and then client_only will no longer be
  217. // valid after the return.
  218. bool do_delete = (client_only && mCurrTick >= mDataBlock->lifetime);
  219. Parent::processTick(move);
  220. if (do_delete)
  221. deleteObject();
  222. }
  223. void afxProjectile::interpolateTick(F32 delta)
  224. {
  225. if (client_only)
  226. return;
  227. Parent::interpolateTick(delta);
  228. }
  229. void afxProjectile::advanceTime(F32 dt)
  230. {
  231. Parent::advanceTime(dt);
  232. if (hookup_with_chor)
  233. {
  234. afxChoreographer* chor = arcaneFX::findClientChoreographer(chor_id);
  235. if (chor)
  236. {
  237. chor->setGhostConstraintObject(this, ghost_cons_name);
  238. hookup_with_chor = false;
  239. }
  240. }
  241. }
  242. bool afxProjectile::onAdd()
  243. {
  244. if(!Parent::onAdd())
  245. return false;
  246. if (isClientObject())
  247. {
  248. // add to client side mission cleanup
  249. SimGroup *cleanup = dynamic_cast<SimGroup *>( Sim::findObject( "ClientMissionCleanup") );
  250. if( cleanup != NULL )
  251. {
  252. cleanup->addObject( this );
  253. }
  254. else
  255. {
  256. AssertFatal( false, "Error, could not find ClientMissionCleanup group" );
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. void afxProjectile::onRemove()
  263. {
  264. Parent::onRemove();
  265. }
  266. //~~~~~~~~~~~~~~~~~~~~
  267. U32 afxProjectile::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
  268. {
  269. U32 retMask = Parent::packUpdate(conn, mask, stream);
  270. // InitialUpdate
  271. if (stream->writeFlag(mask & InitialUpdateMask))
  272. {
  273. stream->write(chor_id);
  274. stream->writeString(ghost_cons_name);
  275. }
  276. return retMask;
  277. }
  278. //~~~~~~~~~~~~~~~~~~~~//
  279. void afxProjectile::unpackUpdate(NetConnection * conn, BitStream * stream)
  280. {
  281. Parent::unpackUpdate(conn, stream);
  282. // InitialUpdate
  283. if (stream->readFlag())
  284. {
  285. stream->read(&chor_id);
  286. ghost_cons_name = stream->readSTString();
  287. if (chor_id != 0 && ghost_cons_name != ST_NULLSTRING)
  288. hookup_with_chor = true;
  289. }
  290. }
  291. class afxProjectileDeleteEvent : public SimEvent
  292. {
  293. public:
  294. void process(SimObject *object)
  295. {
  296. object->deleteObject();
  297. }
  298. };
  299. void afxProjectile::explode(const Point3F& p, const Point3F& n, const U32 collideType)
  300. {
  301. // Make sure we don't explode twice...
  302. if ( isHidden() )
  303. return;
  304. Parent::explode(p, n, collideType);
  305. if (isClientObject() && client_only)
  306. Sim::postEvent(this, new afxProjectileDeleteEvent, Sim::getCurrentTime() + DeleteWaitTime);
  307. }
  308. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//