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