afxEA_Projectile.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "lighting/lightInfo.h"
  27. #include "T3D/projectile.h"
  28. #include "afx/afxEffectDefs.h"
  29. #include "afx/afxEffectWrapper.h"
  30. #include "afx/afxChoreographer.h"
  31. #include "afx/ce/afxProjectile.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. // afxEA_Projectile
  34. class afxEA_Projectile : public afxEffectWrapper
  35. {
  36. typedef afxEffectWrapper Parent;
  37. ProjectileData* projectile_data;
  38. afxProjectileData* afx_projectile_data;
  39. afxProjectile* projectile;
  40. bool launched;
  41. bool impacted;
  42. bool projectile_done;
  43. afxConstraint* launch_cons;
  44. Point3F launch_dir_bias;
  45. void do_runtime_substitutions();
  46. public:
  47. /*C*/ afxEA_Projectile();
  48. /*D*/ ~afxEA_Projectile();
  49. virtual bool isDone();
  50. virtual void ea_set_datablock(SimDataBlock*);
  51. virtual bool ea_start();
  52. virtual bool ea_update(F32 dt);
  53. virtual void ea_finish(bool was_stopped);
  54. virtual void onDeleteNotify(SimObject*);
  55. };
  56. //~~~~~~~~~~~~~~~~~~~~//
  57. afxEA_Projectile::afxEA_Projectile()
  58. {
  59. projectile_data = 0;
  60. afx_projectile_data = 0;
  61. projectile = 0;
  62. launched = false;
  63. impacted = false;
  64. projectile_done = false;
  65. launch_cons = 0;
  66. launch_dir_bias.zero();
  67. }
  68. afxEA_Projectile::~afxEA_Projectile()
  69. {
  70. if (projectile)
  71. clearNotify(projectile);
  72. //if (projectile_data && projectile_data->isTempClone())
  73. // delete projectile_data;
  74. projectile_data = 0;
  75. afx_projectile_data = 0;
  76. }
  77. bool afxEA_Projectile::isDone()
  78. {
  79. return (mDatablock->use_as_cons_obj || mDatablock->use_ghost_as_cons_obj) ? projectile_done : impacted;
  80. }
  81. void afxEA_Projectile::ea_set_datablock(SimDataBlock* db)
  82. {
  83. projectile_data = dynamic_cast<ProjectileData*>(db);
  84. afx_projectile_data = dynamic_cast<afxProjectileData*>(projectile_data);
  85. }
  86. bool afxEA_Projectile::ea_start()
  87. {
  88. if (!projectile_data)
  89. {
  90. Con::errorf("afxEA_Projectile::ea_start() -- missing or incompatible datablock.");
  91. return false;
  92. }
  93. do_runtime_substitutions();
  94. if (!afx_projectile_data)
  95. {
  96. projectile = new afxProjectile();
  97. }
  98. else
  99. {
  100. if (mDatablock->use_ghost_as_cons_obj && mDatablock->effect_name != ST_NULLSTRING)
  101. projectile = new afxProjectile(afx_projectile_data->networking, mChoreographer->getChoreographerId(), mDatablock->effect_name);
  102. else
  103. projectile = new afxProjectile(afx_projectile_data->networking, 0, ST_NULLSTRING);
  104. projectile->ignoreSourceTimeout = afx_projectile_data->ignore_src_timeout;
  105. if (afx_projectile_data->override_collision_masks)
  106. {
  107. projectile->dynamicCollisionMask = afx_projectile_data->dynamicCollisionMask;
  108. projectile->staticCollisionMask = afx_projectile_data->staticCollisionMask;
  109. }
  110. afxConstraintID launch_pos_id = mCons_mgr->getConstraintId(afx_projectile_data->launch_pos_def);
  111. launch_cons = mCons_mgr->getConstraint(launch_pos_id);
  112. launch_dir_bias = afx_projectile_data->launch_dir_bias;
  113. }
  114. projectile->onNewDataBlock(projectile_data, false);
  115. return true;
  116. }
  117. bool afxEA_Projectile::ea_update(F32 dt)
  118. {
  119. if (!launched && projectile)
  120. {
  121. if (mIn_scope)
  122. {
  123. afxConstraint* pos_cons = getPosConstraint();
  124. ShapeBase* src_obj = (pos_cons) ? (dynamic_cast<ShapeBase*>(pos_cons->getSceneObject())) : 0;
  125. F32 muzzle_vel = projectile_data->muzzleVelocity;
  126. Point3F dir_vec;
  127. if (afx_projectile_data)
  128. {
  129. switch (afx_projectile_data->launch_dir_method)
  130. {
  131. case afxProjectileData::OrientConstraint:
  132. dir_vec.set(0,0,1);
  133. mUpdated_xfm.mulV(dir_vec);
  134. break;
  135. case afxProjectileData::LaunchDirField:
  136. dir_vec.set(0,0,1);
  137. break;
  138. case afxProjectileData::TowardPos2Constraint:
  139. default:
  140. dir_vec = mUpdated_aim - mUpdated_pos;
  141. break;
  142. }
  143. }
  144. else
  145. dir_vec = mUpdated_aim - mUpdated_pos;
  146. dir_vec.normalizeSafe();
  147. if (!launch_dir_bias.isZero())
  148. {
  149. dir_vec += launch_dir_bias;
  150. dir_vec.normalizeSafe();
  151. }
  152. dir_vec *= muzzle_vel;
  153. Point3F launch_pos;
  154. if (launch_cons && launch_cons->getPosition(launch_pos))
  155. {
  156. ShapeBase* launch_obj = (launch_cons) ? (dynamic_cast<ShapeBase*>(launch_cons->getSceneObject())) : 0;
  157. projectile->init(launch_pos, dir_vec, (launch_obj) ? launch_obj : src_obj);
  158. }
  159. else
  160. projectile->init(mUpdated_pos, dir_vec, src_obj);
  161. if (!projectile->registerObject())
  162. {
  163. delete projectile;
  164. projectile = 0;
  165. Con::errorf("afxEA_Projectile::ea_update() -- effect failed to register.");
  166. return false;
  167. }
  168. deleteNotify(projectile);
  169. if (projectile)
  170. projectile->setDataField(StringTable->insert("afxOwner"), 0, mChoreographer->getIdString());
  171. }
  172. launched = true;
  173. }
  174. if (launched && projectile)
  175. {
  176. if (mIn_scope)
  177. {
  178. mUpdated_xfm = projectile->getRenderTransform();
  179. mUpdated_xfm.getColumn(3, &mUpdated_pos);
  180. }
  181. }
  182. return true;
  183. }
  184. void afxEA_Projectile::ea_finish(bool was_stopped)
  185. {
  186. if (projectile)
  187. {
  188. clearNotify(projectile);
  189. projectile = 0;
  190. }
  191. launched = false;
  192. impacted = false;
  193. }
  194. void afxEA_Projectile::onDeleteNotify(SimObject* obj)
  195. {
  196. // projectile deleted?
  197. Projectile* del_projectile = dynamic_cast<Projectile*>(obj);
  198. if (del_projectile == projectile)
  199. {
  200. projectile = NULL;
  201. projectile_done = true;
  202. }
  203. }
  204. void afxEA_Projectile::do_runtime_substitutions()
  205. {
  206. // only clone the datablock if there are substitutions
  207. if (projectile_data->getSubstitutionCount() > 0)
  208. {
  209. if (typeid(afxProjectileData) == typeid(*projectile_data))
  210. {
  211. afxProjectileData* orig_db = (afxProjectileData*)projectile_data;
  212. afx_projectile_data = new afxProjectileData(*orig_db, true);
  213. projectile_data = afx_projectile_data;
  214. orig_db->performSubstitutions(projectile_data, mChoreographer, mGroup_index);
  215. }
  216. else
  217. {
  218. // clone the datablock and perform substitutions
  219. ProjectileData* orig_db = projectile_data;
  220. afx_projectile_data = 0;
  221. projectile_data = new ProjectileData(*orig_db, true);
  222. orig_db->performSubstitutions(projectile_data, mChoreographer, mGroup_index);
  223. }
  224. }
  225. }
  226. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  227. class afxEA_ProjectileDesc : public afxEffectAdapterDesc, public afxEffectDefs
  228. {
  229. static afxEA_ProjectileDesc desc;
  230. public:
  231. virtual bool testEffectType(const SimDataBlock*) const;
  232. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  233. virtual bool runsOnServer(const afxEffectWrapperData*) const;
  234. virtual bool runsOnClient(const afxEffectWrapperData*) const;
  235. virtual afxEffectWrapper* create() const { return new afxEA_Projectile; }
  236. };
  237. afxEA_ProjectileDesc afxEA_ProjectileDesc::desc;
  238. bool afxEA_ProjectileDesc::testEffectType(const SimDataBlock* db) const
  239. {
  240. if (typeid(ProjectileData) == typeid(*db))
  241. return true;
  242. if (typeid(afxProjectileData) == typeid(*db))
  243. return true;
  244. return false;
  245. }
  246. bool afxEA_ProjectileDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  247. {
  248. return ((ew->use_as_cons_obj || ew->use_ghost_as_cons_obj) && timing.lifetime < 0);
  249. }
  250. bool afxEA_ProjectileDesc::runsOnServer(const afxEffectWrapperData* ew) const
  251. {
  252. afxProjectileData* afx_projectile_data = dynamic_cast<afxProjectileData*>(ew->effect_data);
  253. if (!afx_projectile_data)
  254. return true;
  255. U8 networking = ((const afxProjectileData*)ew->effect_data)->networking;
  256. return ((networking & CLIENT_ONLY) == 0);
  257. }
  258. bool afxEA_ProjectileDesc::runsOnClient(const afxEffectWrapperData* ew) const
  259. {
  260. afxProjectileData* afx_projectile_data = dynamic_cast<afxProjectileData*>(ew->effect_data);
  261. if (!afx_projectile_data)
  262. return false;
  263. U8 networking = ((const afxProjectileData*)ew->effect_data)->networking;
  264. return ((networking & CLIENT_ONLY) != 0);
  265. }
  266. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//