StickyBombUpdate.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: StickyBombUpdate.cpp //////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, July 2002
  25. // Desc: Updates stickybomb to stay on target while it counts down to detonation (lifetime update)
  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/Drawable.h"
  32. #include "GameClient/InGameUI.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameLogic/Module/StickyBombUpdate.h"
  35. #include "GameLogic/Module/LifetimeUpdate.h"
  36. #include "GameLogic/Module/AIUpdate.h"
  37. #include "GameLogic/Module/BodyModule.h"
  38. #include "GameLogic/GameLogic.h"
  39. // PRIVATE ////////////////////////////////////////////////////////////////////////////////////////
  40. // PUBLIC /////////////////////////////////////////////////////////////////////////////////////////
  41. //-------------------------------------------------------------------------------------------------
  42. //-------------------------------------------------------------------------------------------------
  43. StickyBombUpdate::StickyBombUpdate( Thing *thing, const ModuleData *moduleData ) : UpdateModule( thing, moduleData )
  44. {
  45. m_targetID = INVALID_ID;
  46. m_dieFrame = 0;
  47. //Added By Sadullah Nader
  48. //Initialization(s) inserted
  49. m_nextPingFrame = 0;
  50. //
  51. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  52. }
  53. //-------------------------------------------------------------------------------------------------
  54. //-------------------------------------------------------------------------------------------------
  55. StickyBombUpdate::~StickyBombUpdate( void )
  56. {
  57. }
  58. //-------------------------------------------------------------------------------------------------
  59. void StickyBombUpdate::onObjectCreated()
  60. {
  61. //This first step is an initialization step. Immediately after the stickybomb is created,
  62. //the producerID is stored in the object to signify the shooter of this weapon. From there,
  63. //we can get the target. The target information will be used to determine where the sticky
  64. //bomb will go.
  65. ObjectID shooterID = getObject()->getProducerID();
  66. Object* shooter = TheGameLogic->findObjectByID( shooterID );
  67. if( shooter )
  68. {
  69. //Find the shooters target!
  70. AIUpdateInterface *ai = shooter->getAIUpdateInterface();
  71. if( ai )
  72. {
  73. Object *target = ai->getGoalObject();
  74. if( target )
  75. {
  76. init( target, NULL);
  77. }
  78. }
  79. }
  80. }
  81. //-------------------------------------------------------------------------------------------------
  82. void StickyBombUpdate::init( const Object *target, const Object *bomber)
  83. {
  84. //Store the target.
  85. m_targetID = target ? target->getID() : INVALID_ID;
  86. //Set the target as the producer ID -- so we can access it from other areas.
  87. getObject()->setProducer( target );
  88. UnsignedInt now = TheGameLogic->getFrame();
  89. //Also determine our lifetime.... for countdown purposes.
  90. static NameKeyType key_LifetimeUpdate = NAMEKEY( "LifetimeUpdate" );
  91. LifetimeUpdate *update = (LifetimeUpdate*)getObject()->findUpdateModule( key_LifetimeUpdate );
  92. if( update )
  93. {
  94. //we are a timer bomb
  95. m_dieFrame = update->getDieFrame();
  96. //Calculate the number of seconds (rounded down)
  97. UnsignedInt pings = (m_dieFrame - now) / LOGICFRAMES_PER_SECOND;
  98. //Now determine the next frame we will make a "ping" sound.
  99. m_nextPingFrame = m_dieFrame - (pings * LOGICFRAMES_PER_SECOND);
  100. }
  101. else
  102. {
  103. //we are a remotely triggered bomb.
  104. m_dieFrame = 0;
  105. //Because we don't die -- make our ping in a second.
  106. m_nextPingFrame = now + LOGICFRAMES_PER_SECOND;
  107. }
  108. setWakeFrame( getObject(), UPDATE_SLEEP_NONE );
  109. if( target )
  110. {
  111. const StickyBombUpdateModuleData* d = getStickyBombUpdateModuleData();
  112. Coord3D pos = *target->getPosition();
  113. // make this exception, if bomber has placed bomb on a structure
  114. // let the bomb just stay where it was first put, so a mine clearing unit can get to it later
  115. if(target->isKindOf( KINDOF_IMMOBILE ) && bomber )
  116. {
  117. pos = *bomber->getPosition();
  118. pos.z = TheTerrainLogic->getGroundHeight(pos.x, pos.y);
  119. //keep it at ground height for mine clearing units to reach
  120. }
  121. else
  122. pos.z += d->m_offsetZ; // ride on the roof of the truck/tank
  123. getObject()->setPosition( &pos );
  124. }
  125. }
  126. //-------------------------------------------------------------------------------------------------
  127. UpdateSleepTime StickyBombUpdate::update( void )
  128. {
  129. // Continually reset position of stickybomb to match the position of the target.
  130. const Object *target = getTargetObject();
  131. Object *self = getObject();
  132. if( target )
  133. {
  134. if( target->isEffectivelyDead() )
  135. {
  136. //If the target is dead, then
  137. TheGameLogic->destroyObject( self );
  138. return UPDATE_SLEEP_NONE;
  139. }
  140. if ( target->isKindOf( KINDOF_IMMOBILE) )
  141. {
  142. const Coord3D *pos = self->getPosition();
  143. Coord3D newPos;
  144. newPos.x = pos->x;
  145. newPos.y = pos->y;
  146. newPos.z = TheTerrainLogic->getGroundHeight(newPos.x, newPos.y);
  147. //keep it at ground height for mine clearing units to reach
  148. self->setPosition( &newPos );
  149. }
  150. else // make the bomb follow the target around
  151. {
  152. const StickyBombUpdateModuleData* d = getStickyBombUpdateModuleData();
  153. const Coord3D *pos = target->getPosition();
  154. Coord3D newPos;
  155. newPos.x = pos->x;
  156. newPos.y = pos->y;
  157. newPos.z = pos->z + d->m_offsetZ;
  158. self->setPosition( &newPos );
  159. }
  160. }
  161. UnsignedInt now = TheGameLogic->getFrame();
  162. if( now >= m_nextPingFrame )
  163. {
  164. m_nextPingFrame += LOGICFRAMES_PER_SECOND;
  165. //Play the "ping" sound.
  166. AudioEventRTS sound = *self->getTemplate()->getPerUnitSound( "UnitBombPing" );
  167. sound.setObjectID( self->getID() );
  168. TheAudio->addAudioEvent( &sound );
  169. }
  170. return UPDATE_SLEEP_NONE;
  171. }
  172. //-------------------------------------------------------------------------------------------------
  173. Object* StickyBombUpdate::getTargetObject() const
  174. {
  175. return TheGameLogic->findObjectByID( m_targetID );
  176. }
  177. //-------------------------------------------------------------------------------------------------
  178. void StickyBombUpdate::setTargetObject( Object *obj )
  179. {
  180. m_targetID = obj ? obj->getID() : INVALID_ID;
  181. }
  182. //-------------------------------------------------------------------------------------------------
  183. void StickyBombUpdate::detonate()
  184. {
  185. getObject()->kill();
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. /** CRC */
  189. // ------------------------------------------------------------------------------------------------
  190. void StickyBombUpdate::crc( Xfer *xfer )
  191. {
  192. // extend base class
  193. UpdateModule::crc( xfer );
  194. } // end crc
  195. // ------------------------------------------------------------------------------------------------
  196. /** Xfer method
  197. * Version Info:
  198. * 1: Initial version */
  199. // ------------------------------------------------------------------------------------------------
  200. void StickyBombUpdate::xfer( Xfer *xfer )
  201. {
  202. // version
  203. XferVersion currentVersion = 1;
  204. XferVersion version = currentVersion;
  205. xfer->xferVersion( &version, currentVersion );
  206. // extend base class
  207. UpdateModule::xfer( xfer );
  208. // target id
  209. xfer->xferObjectID( &m_targetID );
  210. // die frame
  211. xfer->xferUnsignedInt( &m_dieFrame );
  212. //Next frame that a ping sound will play.
  213. xfer->xferUnsignedInt( &m_nextPingFrame );
  214. } // end xfer
  215. // ------------------------------------------------------------------------------------------------
  216. /** Load post process */
  217. // ------------------------------------------------------------------------------------------------
  218. void StickyBombUpdate::loadPostProcess( void )
  219. {
  220. // extend base class
  221. UpdateModule::loadPostProcess();
  222. } // end loadPostProcess