FireWeaponUpdate.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: FireWeaponUpdate.h /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, August 2002
  25. // Desc: Update fires a weapon at its own feet as quickly as the weapon allows
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Xfer.h"
  30. #include "GameLogic/Object.h"
  31. #include "GameLogic/Module/FireWeaponUpdate.h"
  32. #include "GameLogic/WeaponStatus.h"
  33. #include "GameLogic/GameLogic.h"
  34. #ifdef _INTERNAL
  35. // for occasional debugging...
  36. //#pragma optimize("", off)
  37. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  38. #endif
  39. //-------------------------------------------------------------------------------------------------
  40. FireWeaponUpdateModuleData::FireWeaponUpdateModuleData()
  41. {
  42. m_weaponTemplate = NULL;
  43. m_initialDelayFrames = 0;
  44. m_exclusiveWeaponDelay = 0;
  45. }
  46. //-------------------------------------------------------------------------------------------------
  47. /*static*/ void FireWeaponUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  48. {
  49. UpdateModuleData::buildFieldParse(p);
  50. static const FieldParse dataFieldParse[] =
  51. {
  52. { "Weapon", INI::parseWeaponTemplate, NULL, offsetof( FireWeaponUpdateModuleData, m_weaponTemplate ) },
  53. { "InitialDelay", INI::parseDurationUnsignedInt, NULL, offsetof( FireWeaponUpdateModuleData, m_initialDelayFrames ) },
  54. { "ExclusiveWeaponDelay", INI::parseDurationUnsignedInt, NULL, offsetof( FireWeaponUpdateModuleData, m_exclusiveWeaponDelay ) },
  55. { 0, 0, 0, 0 }
  56. };
  57. p.add(dataFieldParse);
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. //-------------------------------------------------------------------------------------------------
  61. FireWeaponUpdate::FireWeaponUpdate( Thing *thing, const ModuleData* moduleData ) :
  62. UpdateModule( thing, moduleData ),
  63. m_weapon(NULL)
  64. {
  65. const WeaponTemplate *tmpl = getFireWeaponUpdateModuleData()->m_weaponTemplate;
  66. if (tmpl)
  67. {
  68. m_weapon = TheWeaponStore->allocateNewWeapon(tmpl, PRIMARY_WEAPON);
  69. m_weapon->loadAmmoNow( getObject() );
  70. }
  71. m_initialDelayFrame = TheGameLogic->getFrame() + getFireWeaponUpdateModuleData()->m_initialDelayFrames;
  72. }
  73. //-------------------------------------------------------------------------------------------------
  74. //-------------------------------------------------------------------------------------------------
  75. FireWeaponUpdate::~FireWeaponUpdate( void )
  76. {
  77. if (m_weapon)
  78. m_weapon->deleteInstance();
  79. }
  80. //-------------------------------------------------------------------------------------------------
  81. //-------------------------------------------------------------------------------------------------
  82. UpdateSleepTime FireWeaponUpdate::update( void )
  83. {
  84. if ( TheGameLogic->getFrame() < m_initialDelayFrame )
  85. return UPDATE_SLEEP_NONE;
  86. // If my weapon is ready, shoot it.
  87. if( isOkayToFire() )
  88. {
  89. m_weapon->forceFireWeapon( getObject(), getObject()->getPosition() );
  90. }
  91. return UPDATE_SLEEP_NONE;
  92. }
  93. //-------------------------------------------------------------------------------------------------
  94. //-------------------------------------------------------------------------------------------------
  95. Bool FireWeaponUpdate::isOkayToFire()
  96. {
  97. const Object *me = getObject();
  98. const FireWeaponUpdateModuleData *data = getFireWeaponUpdateModuleData();
  99. if( m_weapon == NULL )
  100. return FALSE;
  101. // Weapon is reloading
  102. if( m_weapon->getStatus() != READY_TO_FIRE )
  103. return FALSE;
  104. if( me->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) )
  105. return FALSE; // no hitting with a 0% building, cheater
  106. // Firing a real weapon surpresses this module
  107. if( data->m_exclusiveWeaponDelay > 0 && ( TheGameLogic->getFrame() < (me->getLastShotFiredFrame() + data->m_exclusiveWeaponDelay) ) )
  108. return FALSE;
  109. return TRUE;
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. /** CRC */
  113. // ------------------------------------------------------------------------------------------------
  114. void FireWeaponUpdate::crc( Xfer *xfer )
  115. {
  116. // extend base class
  117. UpdateModule::crc( xfer );
  118. } // end crc
  119. // ------------------------------------------------------------------------------------------------
  120. /** Xfer method
  121. * Version Info:
  122. * 1: Initial version */
  123. // ------------------------------------------------------------------------------------------------
  124. void FireWeaponUpdate::xfer( Xfer *xfer )
  125. {
  126. // version
  127. XferVersion currentVersion = 2;
  128. XferVersion version = currentVersion;
  129. xfer->xferVersion( &version, currentVersion );
  130. // extend base class
  131. UpdateModule::xfer( xfer );
  132. // weapon
  133. xfer->xferSnapshot( m_weapon );
  134. if ( version >= 2 )
  135. xfer->xferUnsignedInt( &m_initialDelayFrame );
  136. } // end xfer
  137. // ------------------------------------------------------------------------------------------------
  138. /** Load post process */
  139. // ------------------------------------------------------------------------------------------------
  140. void FireWeaponUpdate::loadPostProcess( void )
  141. {
  142. // extend base class
  143. UpdateModule::loadPostProcess();
  144. } // end loadPostProcess