afxXM_AltitudeConform.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "afx/afxEffectWrapper.h"
  26. #include "afx/xm/afxXfmMod.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. class afxXM_AltitudeConformData : public afxXM_WeightedBaseData
  29. {
  30. typedef afxXM_WeightedBaseData Parent;
  31. public:
  32. F32 height;
  33. bool do_terrain;
  34. bool do_interiors;
  35. U32 interior_types;
  36. U32 terrain_types;
  37. bool do_freeze;
  38. public:
  39. /*C*/ afxXM_AltitudeConformData();
  40. /*C*/ afxXM_AltitudeConformData(const afxXM_AltitudeConformData&, bool = false);
  41. void packData(BitStream* stream);
  42. void unpackData(BitStream* stream);
  43. static void initPersistFields();
  44. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  45. DECLARE_CONOBJECT(afxXM_AltitudeConformData);
  46. DECLARE_CATEGORY("AFX");
  47. };
  48. class afxXM_AltitudeConform : public afxXM_WeightedBase
  49. {
  50. typedef afxXM_WeightedBase Parent;
  51. afxXM_AltitudeConformData* mConformData;
  52. SceneContainer* mContainer;
  53. bool mDo_freeze;
  54. bool mIs_frozen;
  55. F32 mTerrain_alt;
  56. F32 mInterior_alt;
  57. Point3F mConformed_pos;
  58. public:
  59. /*C*/ afxXM_AltitudeConform(afxXM_AltitudeConformData*, afxEffectWrapper*, bool on_server);
  60. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  61. };
  62. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  63. IMPLEMENT_CO_DATABLOCK_V1(afxXM_AltitudeConformData);
  64. ConsoleDocClass( afxXM_AltitudeConformData,
  65. "@brief An xmod datablock.\n\n"
  66. "@ingroup afxXMods\n"
  67. "@ingroup AFX\n"
  68. "@ingroup Datablocks\n"
  69. );
  70. afxXM_AltitudeConformData::afxXM_AltitudeConformData()
  71. {
  72. height = 0.0f;
  73. do_terrain = false;
  74. do_interiors = true;
  75. do_freeze = false;
  76. interior_types = InteriorLikeObjectType;
  77. terrain_types = TerrainObjectType | TerrainLikeObjectType;
  78. }
  79. afxXM_AltitudeConformData::afxXM_AltitudeConformData(const afxXM_AltitudeConformData& other, bool temp_clone)
  80. : afxXM_WeightedBaseData(other, temp_clone)
  81. {
  82. height = other.height;
  83. do_terrain = other.do_terrain;
  84. do_interiors = other.do_interiors;
  85. do_freeze = other.do_freeze;
  86. interior_types = other.interior_types;
  87. terrain_types = other.terrain_types;
  88. }
  89. void afxXM_AltitudeConformData::initPersistFields()
  90. {
  91. docsURL;
  92. addField("height", TypeF32, Offset(height, afxXM_AltitudeConformData),
  93. "...");
  94. addField("conformToTerrain", TypeBool, Offset(do_terrain, afxXM_AltitudeConformData),
  95. "...");
  96. addField("conformToInteriors", TypeBool, Offset(do_interiors, afxXM_AltitudeConformData),
  97. "...");
  98. addField("freeze", TypeBool, Offset(do_freeze, afxXM_AltitudeConformData),
  99. "...");
  100. addField("interiorTypes", TypeS32, Offset(interior_types, afxXM_AltitudeConformData),
  101. "...");
  102. addField("terrainTypes", TypeS32, Offset(terrain_types, afxXM_AltitudeConformData),
  103. "...");
  104. Parent::initPersistFields();
  105. }
  106. void afxXM_AltitudeConformData::packData(BitStream* stream)
  107. {
  108. Parent::packData(stream);
  109. stream->write(height);
  110. stream->writeFlag(do_terrain);
  111. stream->writeFlag(do_interiors);
  112. stream->writeFlag(do_freeze);
  113. stream->write(interior_types);
  114. stream->write(terrain_types);
  115. }
  116. void afxXM_AltitudeConformData::unpackData(BitStream* stream)
  117. {
  118. Parent::unpackData(stream);
  119. stream->read(&height);
  120. do_terrain = stream->readFlag();
  121. do_interiors = stream->readFlag();
  122. do_freeze = stream->readFlag();
  123. stream->read(&interior_types);
  124. stream->read(&terrain_types);
  125. }
  126. afxXM_Base* afxXM_AltitudeConformData::create(afxEffectWrapper* fx, bool on_server)
  127. {
  128. return new afxXM_AltitudeConform(this, fx, on_server);
  129. }
  130. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  131. afxXM_AltitudeConform::afxXM_AltitudeConform(afxXM_AltitudeConformData* db, afxEffectWrapper* fxw, bool on_server)
  132. : afxXM_WeightedBase(db, fxw)
  133. {
  134. mConformData = db;
  135. mContainer = (on_server) ? &gServerContainer : &gClientContainer;
  136. mDo_freeze = db->do_freeze;
  137. mIs_frozen = false;
  138. mTerrain_alt = -1.0f;
  139. mInterior_alt = -1.0f;
  140. mConformed_pos.zero();
  141. }
  142. void afxXM_AltitudeConform::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  143. {
  144. if (mIs_frozen)
  145. {
  146. if (mTerrain_alt >= 0.0f)
  147. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  148. if (mInterior_alt >= 0.0f)
  149. fx_wrapper->setInteriorAltitude(mInterior_alt);
  150. params.pos = mConformed_pos;
  151. return;
  152. }
  153. RayInfo rInfo1, rInfo2;
  154. bool hit1 = false, hit2 = false;
  155. bool hit1_is_interior = false;
  156. // find primary ground
  157. Point3F above_pos(params.pos); above_pos.z += 0.1f;
  158. Point3F below_pos(params.pos); below_pos.z -= 10000;
  159. hit1 = mContainer->castRay(above_pos, below_pos, mConformData->interior_types | mConformData->terrain_types, &rInfo1);
  160. // find secondary ground
  161. if (hit1 && rInfo1.object)
  162. {
  163. hit1_is_interior = ((rInfo1.object->getTypeMask() & mConformData->interior_types) != 0);
  164. U32 mask = (hit1_is_interior) ? mConformData->terrain_types : mConformData->interior_types;
  165. hit2 = mContainer->castRay(above_pos, below_pos, mask, &rInfo2);
  166. }
  167. if (hit1)
  168. {
  169. F32 wt_factor = calc_weight_factor(elapsed);
  170. F32 incoming_z = params.pos.z;
  171. F32 ground1_z = rInfo1.point.z + mConformData->height;
  172. F32 pos_z = ground1_z + (1.0f - wt_factor)*(incoming_z - ground1_z);
  173. if (hit1_is_interior)
  174. {
  175. mInterior_alt = incoming_z - pos_z;
  176. fx_wrapper->setInteriorAltitude(mInterior_alt);
  177. if (mConformData->do_interiors)
  178. params.pos.z = pos_z;
  179. }
  180. else
  181. {
  182. mTerrain_alt = incoming_z - pos_z;
  183. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  184. if (mConformData->do_terrain)
  185. params.pos.z = pos_z;
  186. }
  187. if (hit2)
  188. {
  189. F32 ground2_z = rInfo2.point.z + mConformData->height;
  190. F32 z2 = ground2_z + (1.0f - wt_factor)*(incoming_z - ground2_z);
  191. if (hit1_is_interior)
  192. {
  193. mTerrain_alt = incoming_z - z2;
  194. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  195. }
  196. else
  197. {
  198. mInterior_alt = incoming_z - z2;
  199. fx_wrapper->setInteriorAltitude(mInterior_alt);
  200. }
  201. }
  202. // check for case where interior is underground
  203. else if (hit1_is_interior)
  204. {
  205. RayInfo rInfo0;
  206. Point3F lookup_from_pos(params.pos); lookup_from_pos.z -= 0.1f;
  207. Point3F lookup_to_pos(params.pos); lookup_to_pos.z += 10000;
  208. if (mContainer->castRay(lookup_from_pos, lookup_to_pos, TerrainObjectType, &rInfo0))
  209. {
  210. F32 ground2_z = rInfo0.point.z + mConformData->height;
  211. F32 z2 = ground2_z + (1.0f - wt_factor)*(incoming_z - ground2_z);
  212. mTerrain_alt = z2 - incoming_z;
  213. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  214. }
  215. }
  216. if (mDo_freeze)
  217. {
  218. mConformed_pos = params.pos;
  219. mIs_frozen = true;
  220. }
  221. }
  222. }
  223. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//