afxEA_AreaDamage.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "afx/afxEffectDefs.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/ce/afxAreaDamage.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_AreaDamage
  32. class afxEA_AreaDamage : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxAreaDamageData* damage_data;
  36. Point3F impact_pos;
  37. bool damage_is_done;
  38. SceneObject* cons_obj;
  39. void do_runtime_substitutions();
  40. void deal_area_damage();
  41. void apply_damage(ShapeBase*, F32 damage, const char* flavor, Point3F& pos);
  42. void apply_impulse(ShapeBase*, F32 impulse, Point3F& pos);
  43. void notify_damage_source(ShapeBase* damaged, F32 damage, const char* flavor, Point3F& pos);
  44. public:
  45. /*C*/ afxEA_AreaDamage();
  46. /*C*/ ~afxEA_AreaDamage();
  47. virtual bool isDone();
  48. virtual void ea_set_datablock(SimDataBlock*);
  49. virtual bool ea_start();
  50. virtual bool ea_update(F32 dt);
  51. virtual void ea_finish(bool was_stopped);
  52. };
  53. //~~~~~~~~~~~~~~~~~~~~//
  54. afxEA_AreaDamage::afxEA_AreaDamage()
  55. {
  56. damage_data = 0;
  57. impact_pos.zero();
  58. damage_is_done = false;
  59. cons_obj = 0;
  60. }
  61. afxEA_AreaDamage::~afxEA_AreaDamage()
  62. {
  63. if (damage_data && damage_data->isTempClone())
  64. delete damage_data;
  65. damage_data = 0;
  66. }
  67. bool afxEA_AreaDamage::isDone()
  68. {
  69. return damage_is_done;
  70. }
  71. void afxEA_AreaDamage::ea_set_datablock(SimDataBlock* db)
  72. {
  73. damage_data = dynamic_cast<afxAreaDamageData*>(db);
  74. }
  75. bool afxEA_AreaDamage::ea_start()
  76. {
  77. if (!damage_data)
  78. {
  79. Con::errorf("afxEA_AreaDamage::ea_start() -- missing or incompatible datablock.");
  80. return false;
  81. }
  82. do_runtime_substitutions();
  83. return true;
  84. }
  85. bool afxEA_AreaDamage::ea_update(F32 dt)
  86. {
  87. if (!damage_is_done)
  88. {
  89. afxConstraint* pos_cons = getPosConstraint();
  90. if (pos_cons)
  91. {
  92. pos_cons->getPosition(impact_pos);
  93. cons_obj = pos_cons->getSceneObject();
  94. }
  95. deal_area_damage();
  96. damage_is_done = true;
  97. }
  98. return true;
  99. }
  100. void afxEA_AreaDamage::ea_finish(bool was_stopped)
  101. {
  102. damage_is_done = false;
  103. }
  104. void afxEA_AreaDamage::do_runtime_substitutions()
  105. {
  106. // only clone the datablock if there are substitutions
  107. if (damage_data->getSubstitutionCount() > 0)
  108. {
  109. // clone the datablock and perform substitutions
  110. afxAreaDamageData* orig_db = damage_data;
  111. damage_data = new afxAreaDamageData(*orig_db, true);
  112. orig_db->performSubstitutions(damage_data, mChoreographer, mGroup_index);
  113. }
  114. }
  115. // radiusDamage(%sourceObject, %position, %radius, %damage, %damageType, %impulse, %excluded)
  116. void afxEA_AreaDamage::deal_area_damage()
  117. {
  118. // initContainerRadiusSearch -- afterwards Container::mSearchList contains objects within radius sorted by distance
  119. gServerContainer.initRadiusSearch(impact_pos, damage_data->radius, ShapeBaseObjectType);
  120. F32 halfradius = damage_data->radius*0.5f;
  121. const Vector<SimObjectPtr<SceneObject>*>& list = gServerContainer.getRadiusSearchList();
  122. for (S32 i = 0; i < list.size(); i++)
  123. {
  124. if (!list[i]->isNull())
  125. {
  126. ShapeBase* shape = dynamic_cast<ShapeBase*>((SceneObject*)(*list[i]));
  127. if (!shape || (shape->getTypeMask() & CameraObjectType))
  128. continue;
  129. if (damage_data->exclude_cons_obj && cons_obj == *list[i])
  130. continue;
  131. #if 0 // AFX_T3D_DISABLED -- calcExplosionCoverage() is a script function
  132. // so we currently assign a coverage value of 1.0.
  133. U32 mask = InteriorObjectType | TerrainObjectType | VehicleObjectType;
  134. F32 coverage = calcExplosionCoverage(impact_pos, shape, mask);
  135. if (coverage == 0.0f)
  136. continue;
  137. #else
  138. F32 coverage = 1.0f;
  139. #endif
  140. // calulate distance
  141. Point3F pos;
  142. shape->getWorldBox().getCenter(&pos);
  143. F32 dist = (pos - impact_pos).len();
  144. F32 min_dist = shape->getWorldBox().len_x();
  145. if (shape->getWorldBox().len_y() < min_dist)
  146. min_dist = shape->getWorldBox().len_y();
  147. if (shape->getWorldBox().len_z() < min_dist)
  148. min_dist = shape->getWorldBox().len_z();
  149. dist -= min_dist;
  150. if (dist < 0)
  151. dist = 0;
  152. F32 dist_scale = (dist < halfradius) ? 1.0f : 1.0f - ((dist - halfradius)/halfradius);
  153. F32 damage = damage_data->amount*coverage*dist_scale;
  154. apply_damage(shape, damage, damage_data->flavor, impact_pos);
  155. apply_impulse(shape, damage_data->impulse*dist_scale, impact_pos);
  156. if (damage_data->notify_damage_src)
  157. notify_damage_source(shape, damage, damage_data->flavor, impact_pos);
  158. }
  159. }
  160. }
  161. void afxEA_AreaDamage::notify_damage_source(ShapeBase* damaged, F32 damage, const char* flavor, Point3F& pos)
  162. {
  163. if (mIsZero(damage))
  164. return;
  165. char *posArg = Con::getArgBuffer(64);
  166. dSprintf(posArg, 64, "%f %f %f", pos.x, pos.y, pos.z);
  167. Con::executef(mChoreographer->getDataBlock(), "onInflictedAreaDamage",
  168. mChoreographer->getIdString(),
  169. damaged->getIdString(),
  170. Con::getFloatArg(damage),
  171. flavor,
  172. posArg);
  173. }
  174. void afxEA_AreaDamage::apply_damage(ShapeBase* shape, F32 damage, const char* flavor, Point3F& pos)
  175. {
  176. if (mIsZero(damage))
  177. return;
  178. char *posArg = Con::getArgBuffer(64);
  179. dSprintf(posArg, 64, "%f %f %f", pos.x, pos.y, pos.z);
  180. Con::executef(shape, "damage",
  181. mChoreographer->getIdString(),
  182. posArg,
  183. Con::getFloatArg(damage),
  184. flavor);
  185. }
  186. void afxEA_AreaDamage::apply_impulse(ShapeBase* shape, F32 impulse, Point3F& pos)
  187. {
  188. if (impulse <= 0.0f)
  189. return;
  190. Point3F center; shape->getWorldBox().getCenter(&center);
  191. VectorF impulse_vec = center - pos;
  192. impulse_vec.normalizeSafe();
  193. impulse_vec *= impulse;
  194. shape->applyImpulse(pos, impulse_vec);
  195. }
  196. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  197. class afxEA_AreaDamageDesc : public afxEffectAdapterDesc, public afxEffectDefs
  198. {
  199. static afxEA_AreaDamageDesc desc;
  200. public:
  201. virtual bool testEffectType(const SimDataBlock*) const;
  202. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const { return false; }
  203. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  204. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  205. virtual afxEffectWrapper* create() const { return new afxEA_AreaDamage; }
  206. };
  207. afxEA_AreaDamageDesc afxEA_AreaDamageDesc::desc;
  208. bool afxEA_AreaDamageDesc::testEffectType(const SimDataBlock* db) const
  209. {
  210. return (typeid(afxAreaDamageData) == typeid(*db));
  211. }
  212. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//