afxXM_AltitudeConform.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. addField("height", TypeF32, Offset(height, afxXM_AltitudeConformData),
  92. "...");
  93. addField("conformToTerrain", TypeBool, Offset(do_terrain, afxXM_AltitudeConformData),
  94. "...");
  95. addField("conformToInteriors", TypeBool, Offset(do_interiors, afxXM_AltitudeConformData),
  96. "...");
  97. addField("freeze", TypeBool, Offset(do_freeze, afxXM_AltitudeConformData),
  98. "...");
  99. addField("interiorTypes", TypeS32, Offset(interior_types, afxXM_AltitudeConformData),
  100. "...");
  101. addField("terrainTypes", TypeS32, Offset(terrain_types, afxXM_AltitudeConformData),
  102. "...");
  103. Parent::initPersistFields();
  104. }
  105. void afxXM_AltitudeConformData::packData(BitStream* stream)
  106. {
  107. Parent::packData(stream);
  108. stream->write(height);
  109. stream->writeFlag(do_terrain);
  110. stream->writeFlag(do_interiors);
  111. stream->writeFlag(do_freeze);
  112. stream->write(interior_types);
  113. stream->write(terrain_types);
  114. }
  115. void afxXM_AltitudeConformData::unpackData(BitStream* stream)
  116. {
  117. Parent::unpackData(stream);
  118. stream->read(&height);
  119. do_terrain = stream->readFlag();
  120. do_interiors = stream->readFlag();
  121. do_freeze = stream->readFlag();
  122. stream->read(&interior_types);
  123. stream->read(&terrain_types);
  124. }
  125. afxXM_Base* afxXM_AltitudeConformData::create(afxEffectWrapper* fx, bool on_server)
  126. {
  127. return new afxXM_AltitudeConform(this, fx, on_server);
  128. }
  129. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  130. afxXM_AltitudeConform::afxXM_AltitudeConform(afxXM_AltitudeConformData* db, afxEffectWrapper* fxw, bool on_server)
  131. : afxXM_WeightedBase(db, fxw)
  132. {
  133. mConformData = db;
  134. mContainer = (on_server) ? &gServerContainer : &gClientContainer;
  135. mDo_freeze = db->do_freeze;
  136. mIs_frozen = false;
  137. mTerrain_alt = -1.0f;
  138. mInterior_alt = -1.0f;
  139. mConformed_pos.zero();
  140. }
  141. void afxXM_AltitudeConform::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  142. {
  143. if (mIs_frozen)
  144. {
  145. if (mTerrain_alt >= 0.0f)
  146. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  147. if (mInterior_alt >= 0.0f)
  148. fx_wrapper->setInteriorAltitude(mInterior_alt);
  149. params.pos = mConformed_pos;
  150. return;
  151. }
  152. RayInfo rInfo1, rInfo2;
  153. bool hit1 = false, hit2 = false;
  154. bool hit1_is_interior = false;
  155. // find primary ground
  156. Point3F above_pos(params.pos); above_pos.z += 0.1f;
  157. Point3F below_pos(params.pos); below_pos.z -= 10000;
  158. hit1 = mContainer->castRay(above_pos, below_pos, mConformData->interior_types | mConformData->terrain_types, &rInfo1);
  159. // find secondary ground
  160. if (hit1 && rInfo1.object)
  161. {
  162. hit1_is_interior = ((rInfo1.object->getTypeMask() & mConformData->interior_types) != 0);
  163. U32 mask = (hit1_is_interior) ? mConformData->terrain_types : mConformData->interior_types;
  164. hit2 = mContainer->castRay(above_pos, below_pos, mask, &rInfo2);
  165. }
  166. if (hit1)
  167. {
  168. F32 wt_factor = calc_weight_factor(elapsed);
  169. F32 incoming_z = params.pos.z;
  170. F32 ground1_z = rInfo1.point.z + mConformData->height;
  171. F32 pos_z = ground1_z + (1.0f - wt_factor)*(incoming_z - ground1_z);
  172. if (hit1_is_interior)
  173. {
  174. mInterior_alt = incoming_z - pos_z;
  175. fx_wrapper->setInteriorAltitude(mInterior_alt);
  176. if (mConformData->do_interiors)
  177. params.pos.z = pos_z;
  178. }
  179. else
  180. {
  181. mTerrain_alt = incoming_z - pos_z;
  182. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  183. if (mConformData->do_terrain)
  184. params.pos.z = pos_z;
  185. }
  186. if (hit2)
  187. {
  188. F32 ground2_z = rInfo2.point.z + mConformData->height;
  189. F32 z2 = ground2_z + (1.0f - wt_factor)*(incoming_z - ground2_z);
  190. if (hit1_is_interior)
  191. {
  192. mTerrain_alt = incoming_z - z2;
  193. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  194. }
  195. else
  196. {
  197. mInterior_alt = incoming_z - z2;
  198. fx_wrapper->setInteriorAltitude(mInterior_alt);
  199. }
  200. }
  201. // check for case where interior is underground
  202. else if (hit1_is_interior)
  203. {
  204. RayInfo rInfo0;
  205. Point3F lookup_from_pos(params.pos); lookup_from_pos.z -= 0.1f;
  206. Point3F lookup_to_pos(params.pos); lookup_to_pos.z += 10000;
  207. if (mContainer->castRay(lookup_from_pos, lookup_to_pos, TerrainObjectType, &rInfo0))
  208. {
  209. F32 ground2_z = rInfo0.point.z + mConformData->height;
  210. F32 z2 = ground2_z + (1.0f - wt_factor)*(incoming_z - ground2_z);
  211. mTerrain_alt = z2 - incoming_z;
  212. fx_wrapper->setTerrainAltitude(mTerrain_alt);
  213. }
  214. }
  215. if (mDo_freeze)
  216. {
  217. mConformed_pos = params.pos;
  218. mIs_frozen = true;
  219. }
  220. }
  221. }
  222. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//