HeightDieUpdate.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: HeightDieUpdate.cpp //////////////////////////////////////////////////////////////////////
  24. // Author: Objects that will die when the are a certain height above the terrain or objects
  25. // Desc: Colin Day, April 2002
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/ThingTemplate.h"
  30. #include "Common/Xfer.h"
  31. #include "GameClient/InGameUI.h"
  32. #include "GameClient/ParticleSys.h"
  33. #include "GameLogic/GameLogic.h"
  34. #include "GameLogic/Object.h"
  35. #include "GameLogic/PartitionManager.h"
  36. #include "GameLogic/TerrainLogic.h"
  37. #include "GameLogic/Module/ContainModule.h"
  38. #include "GameLogic/Module/HeightDieUpdate.h"
  39. #include "GameLogic/Module/PhysicsUpdate.h"
  40. #ifdef _INTERNAL
  41. // for occasional debugging...
  42. //#pragma optimize("", off)
  43. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  44. #endif
  45. //-------------------------------------------------------------------------------------------------
  46. //-------------------------------------------------------------------------------------------------
  47. HeightDieUpdateModuleData::HeightDieUpdateModuleData( void )
  48. {
  49. m_targetHeightAboveTerrain = 0.0f;
  50. m_targetHeightIncludesStructures = FALSE;
  51. m_onlyWhenMovingDown = FALSE;
  52. m_destroyAttachedParticlesAtHeight = -1.0f;
  53. m_snapToGroundOnDeath = FALSE;
  54. m_initialDelay = 0;
  55. } // end HeightDieUpdateModuleData
  56. //-------------------------------------------------------------------------------------------------
  57. //-------------------------------------------------------------------------------------------------
  58. void HeightDieUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  59. {
  60. UpdateModuleData::buildFieldParse( p );
  61. static const FieldParse dataFieldParse[] =
  62. {
  63. { "TargetHeight", INI::parseReal, NULL, offsetof( HeightDieUpdateModuleData, m_targetHeightAboveTerrain ) },
  64. { "TargetHeightIncludesStructures", INI::parseBool, NULL, offsetof( HeightDieUpdateModuleData, m_targetHeightIncludesStructures ) },
  65. { "OnlyWhenMovingDown", INI::parseBool, NULL, offsetof( HeightDieUpdateModuleData, m_onlyWhenMovingDown ) },
  66. { "DestroyAttachedParticlesAtHeight", INI::parseReal, NULL, offsetof( HeightDieUpdateModuleData, m_destroyAttachedParticlesAtHeight ) },
  67. { "SnapToGroundOnDeath", INI::parseBool, NULL, offsetof( HeightDieUpdateModuleData, m_snapToGroundOnDeath ) },
  68. { "InitialDelay", INI::parseDurationUnsignedInt, NULL, offsetof( HeightDieUpdateModuleData, m_initialDelay ) },
  69. { 0, 0, 0, 0 }
  70. };
  71. p.add(dataFieldParse);
  72. } // end buildFieldParse
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////
  76. //-------------------------------------------------------------------------------------------------
  77. //-------------------------------------------------------------------------------------------------
  78. HeightDieUpdate::HeightDieUpdate( Thing *thing, const ModuleData* moduleData )
  79. : UpdateModule( thing, moduleData )
  80. {
  81. m_hasDied = FALSE;
  82. m_particlesDestroyed = FALSE;
  83. m_lastPosition.x = -1.0f;
  84. m_lastPosition.y = -1.0f;
  85. m_lastPosition.z = -1.0f;
  86. m_earliestDeathFrame = UINT_MAX;
  87. // m_lastPosition = *thing->getPosition();
  88. } // end HeightDieUpdate
  89. //-------------------------------------------------------------------------------------------------
  90. //-------------------------------------------------------------------------------------------------
  91. HeightDieUpdate::~HeightDieUpdate( void )
  92. {
  93. } // end ~HeightDieUpdate
  94. //-------------------------------------------------------------------------------------------------
  95. //-------------------------------------------------------------------------------------------------
  96. UpdateSleepTime HeightDieUpdate::update( void )
  97. {
  98. UnsignedInt now = TheGameLogic->getFrame();
  99. if( m_earliestDeathFrame == UINT_MAX )
  100. m_earliestDeathFrame = now + getHeightDieUpdateModuleData()->m_initialDelay;
  101. // If at least a one frame delay has been set, then stop for a while
  102. if( m_earliestDeathFrame > now )
  103. return UPDATE_SLEEP_NONE;
  104. // do nothing if we're contained within other objects ... like a transport
  105. if( getObject()->getContainedBy() != NULL )
  106. {
  107. // keep track of our last position even though we're not doing anything yet
  108. m_lastPosition = *getObject()->getPosition();
  109. // get outta here
  110. return UPDATE_SLEEP_NONE;
  111. } // end if
  112. // get the module data
  113. const HeightDieUpdateModuleData *modData = getHeightDieUpdateModuleData();
  114. // get our current position
  115. const Coord3D *pos = getObject()->getPosition();
  116. Bool directionOK = TRUE;
  117. if( m_hasDied == FALSE )
  118. {
  119. if( modData->m_onlyWhenMovingDown )
  120. {
  121. if( pos->z >= m_lastPosition.z )
  122. directionOK = FALSE;
  123. } // end fi
  124. // get the terrain height
  125. Real terrainHeightAtPos = TheTerrainLogic->getGroundHeight( pos->x, pos->y );
  126. // if including structures, check for bridges
  127. if (modData->m_targetHeightIncludesStructures)
  128. {
  129. PathfindLayerEnum layer = TheTerrainLogic->getHighestLayerForDestination(pos);
  130. if (layer != LAYER_GROUND)
  131. {
  132. Real layerHeight = TheTerrainLogic->getLayerHeight(pos->x, pos->y, layer);
  133. if (layerHeight > terrainHeightAtPos)
  134. terrainHeightAtPos = layerHeight;
  135. }
  136. }
  137. //
  138. // our target height to die at is by default the height specified in the INI entry above
  139. // the terrain ... we may change our target height if we care about dying above
  140. // objects under us (see below)
  141. //
  142. Real targetHeight = terrainHeightAtPos + modData->m_targetHeightAboveTerrain;
  143. //
  144. // if we consider objects under us ... we will die when we are the specified distance above
  145. // those objects
  146. //
  147. if( modData->m_targetHeightIncludesStructures == TRUE )
  148. {
  149. // scan all objects in the radius of our extent and find the tallest height among them
  150. PartitionFilterAcceptByKindOf filter1( MAKE_KINDOF_MASK( KINDOF_STRUCTURE ),KINDOFMASK_NONE );
  151. PartitionFilter *filters[] = { &filter1, NULL };
  152. Real range = getObject()->getGeometryInfo().getBoundingCircleRadius();
  153. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( getObject(),
  154. range,
  155. FROM_BOUNDINGSPHERE_3D,
  156. filters );
  157. MemoryPoolObjectHolder hold( iter );
  158. Object *obj;
  159. Real tallestHeight = 0.0f;
  160. Real thisHeight;
  161. for( obj = iter->first(); obj; obj = iter->next() )
  162. {
  163. // ignore ourselves
  164. if( obj == getObject() )
  165. continue;
  166. // store the height of the tallest object under us
  167. thisHeight = obj->getGeometryInfo().getMaxHeightAbovePosition();
  168. if( thisHeight > tallestHeight )
  169. tallestHeight = thisHeight;
  170. } // end for obj
  171. //
  172. // our target height is either the height above the terrain as specified by the INI
  173. // entry for the object that has this update ... or it is the building height of the
  174. // tallest thing under us
  175. //
  176. if( tallestHeight > modData->m_targetHeightAboveTerrain )
  177. targetHeight = tallestHeight + terrainHeightAtPos;
  178. } // end if
  179. // if we are below the target height ... DIE!
  180. if( pos->z < targetHeight && directionOK )
  181. {
  182. // if we're supposed to snap us to the ground on death do so
  183. // AND: even if we're not snapping to ground, be sure we don't go BELOW ground
  184. if( modData->m_snapToGroundOnDeath || pos->z < terrainHeightAtPos )
  185. {
  186. Coord3D ground;
  187. ground.x = pos->x;
  188. ground.y = pos->y;
  189. ground.z = terrainHeightAtPos;
  190. getObject()->setPosition( &ground );
  191. }
  192. // kill the object
  193. getObject()->kill();
  194. // we have died ... don't do this again
  195. m_hasDied = TRUE;
  196. } // end if
  197. } // end if
  198. //
  199. // if our height is below the destroy attached particles height above the terrain, clean
  200. // them up from the particle system
  201. //
  202. if( m_particlesDestroyed == FALSE && pos->z < modData->m_destroyAttachedParticlesAtHeight && (m_hasDied || directionOK) )
  203. {
  204. // destroy them
  205. TheParticleSystemManager->destroyAttachedSystems( getObject() );
  206. // don't do this again
  207. m_particlesDestroyed = TRUE;
  208. } // end if
  209. // save our current position as the last position we monitored
  210. m_lastPosition = *pos;
  211. return UPDATE_SLEEP_NONE;
  212. } // end update
  213. // ------------------------------------------------------------------------------------------------
  214. /** CRC */
  215. // ------------------------------------------------------------------------------------------------
  216. void HeightDieUpdate::crc( Xfer *xfer )
  217. {
  218. // extend base class
  219. UpdateModule::crc( xfer );
  220. } // end crc
  221. // ------------------------------------------------------------------------------------------------
  222. /** Xfer method
  223. * Version Info:
  224. * 1: Initial version
  225. * 2: m_earliestDeathFrame
  226. */
  227. // ------------------------------------------------------------------------------------------------
  228. void HeightDieUpdate::xfer( Xfer *xfer )
  229. {
  230. // version
  231. XferVersion currentVersion = 2;
  232. XferVersion version = currentVersion;
  233. xfer->xferVersion( &version, currentVersion );
  234. // extend base class
  235. UpdateModule::xfer( xfer );
  236. // has died
  237. xfer->xferBool( &m_hasDied );
  238. // particles destroyed
  239. xfer->xferBool( &m_particlesDestroyed );
  240. // last position
  241. xfer->xferCoord3D( &m_lastPosition );
  242. if( version >= 2 )
  243. xfer->xferUnsignedInt( &m_earliestDeathFrame );
  244. else
  245. m_earliestDeathFrame = 0;
  246. } // end xfer
  247. // ------------------------------------------------------------------------------------------------
  248. /** Load post process */
  249. // ------------------------------------------------------------------------------------------------
  250. void HeightDieUpdate::loadPostProcess( void )
  251. {
  252. // extend base class
  253. UpdateModule::loadPostProcess();
  254. } // end loadPostProcess