FireOCLAfterWeaponCooldownUpdate.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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: FireOCLAfterWeaponCooldownUpdate.cpp //////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, September 2002
  25. // Desc: This system tracks the objects status with regards to firing, and whenever the object stops
  26. // firing, and all the conditions are met, then it'll create the specified OCL.
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #define DEFINE_WEAPONSLOTTYPE_NAMES //TheWeaponSlotTypeNamesLookupList
  31. #include "Common/Player.h"
  32. #include "Common/ThingTemplate.h"
  33. #include "Common/Xfer.h"
  34. #include "GameClient/Drawable.h"
  35. #include "GameClient/InGameUI.h"
  36. #include "GameLogic/GameLogic.h"
  37. #include "GameLogic/Object.h"
  38. #include "GameLogic/ObjectCreationList.h"
  39. #include "GameLogic/Weapon.h"
  40. #include "GameLogic/Weaponset.h"
  41. #include "GameLogic/Module/FireOCLAfterWeaponCooldownUpdate.h"
  42. #include "GameLogic/Module/LifetimeUpdate.h"
  43. #include "GameLogic/Module/AIUpdate.h"
  44. #include "GameLogic/Module/BodyModule.h"
  45. #ifdef _INTERNAL
  46. // for occasional debugging...
  47. //#pragma optimize("", off)
  48. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  49. #endif
  50. //-------------------------------------------------------------------------------------------------
  51. FireOCLAfterWeaponCooldownUpdateModuleData::FireOCLAfterWeaponCooldownUpdateModuleData()
  52. {
  53. m_weaponSlot = PRIMARY_WEAPON;
  54. m_minShotsRequired = 1;
  55. m_oclLifetimePerSecond = 1000;
  56. m_oclMaxFrames = 1000;
  57. m_ocl = NULL;
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. void FireOCLAfterWeaponCooldownUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  61. {
  62. UpdateModuleData::buildFieldParse(p);
  63. static const FieldParse dataFieldParse[] =
  64. {
  65. { "WeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_weaponSlot ) },
  66. { "OCL", INI::parseObjectCreationList, NULL, offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_ocl ) },
  67. { "MinShotsToCreateOCL", INI::parseUnsignedInt, NULL, offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_minShotsRequired ) },
  68. { "OCLLifetimePerSecond", INI::parseUnsignedInt, NULL, offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_oclLifetimePerSecond ) },
  69. { "OCLLifetimeMaxCap", INI::parseDurationUnsignedInt, NULL, offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_oclMaxFrames ) },
  70. { 0, 0, 0, 0 }
  71. };
  72. p.add(dataFieldParse);
  73. p.add(UpgradeMuxData::getFieldParse(), offsetof( FireOCLAfterWeaponCooldownUpdateModuleData, m_upgradeMuxData ));
  74. }
  75. //-------------------------------------------------------------------------------------------------
  76. FireOCLAfterWeaponCooldownUpdate::FireOCLAfterWeaponCooldownUpdate( Thing *thing, const ModuleData *moduleData ) : UpdateModule( thing, moduleData )
  77. {
  78. m_valid = false;
  79. resetStats();
  80. }
  81. //-------------------------------------------------------------------------------------------------
  82. //-------------------------------------------------------------------------------------------------
  83. FireOCLAfterWeaponCooldownUpdate::~FireOCLAfterWeaponCooldownUpdate( void )
  84. {
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. UpdateSleepTime FireOCLAfterWeaponCooldownUpdate::update( void )
  88. {
  89. const FireOCLAfterWeaponCooldownUpdateModuleData* data = getFireOCLAfterWeaponCooldownUpdateModuleData();
  90. UpgradeMaskType activation, conflicting;
  91. getUpgradeActivationMasks( activation, conflicting );
  92. Bool validThisFrame = true;
  93. Bool validToFireOCL = true;
  94. Object *obj = getObject();
  95. //Get current weapon
  96. Weapon *weapon = obj->getCurrentWeapon();
  97. if( weapon )
  98. {
  99. if( weapon->getWeaponSlot() != data->m_weaponSlot )
  100. {
  101. //Not the right weapon slot -- it's possible we switched weapons in which case it's
  102. //possible to fire the OCL.
  103. validThisFrame = false;
  104. }
  105. }
  106. else
  107. {
  108. //No weapon selected -- possible to switch off weapons, possible to have just
  109. //finished firing our weapon.
  110. validThisFrame = false;
  111. }
  112. UpgradeMaskType objectMask = obj->getObjectCompletedUpgradeMask();
  113. UpgradeMaskType playerMask = obj->getControllingPlayer()->getCompletedUpgradeMask();
  114. UpgradeMaskType maskToCheck = playerMask;
  115. maskToCheck.set( objectMask );
  116. if( validThisFrame && !testUpgradeConditions( maskToCheck ) )
  117. {
  118. //Can't use this period if this object doesn't have any of the upgrades
  119. validThisFrame = false;
  120. validToFireOCL = false;
  121. }
  122. UnsignedInt now = TheGameLogic->getFrame();
  123. if( validThisFrame )
  124. {
  125. if( weapon->getLastShotFrame() == now - 1 )
  126. {
  127. m_consecutiveShots++;
  128. if( m_consecutiveShots == 1 )
  129. {
  130. //Our first shot -- so record the frame number so we can easily calculate the
  131. //time we've been firing!
  132. m_startFrame = now;
  133. }
  134. }
  135. else if( weapon->getPossibleNextShotFrame() < now )
  136. {
  137. //Means we could have shot but didn't!
  138. if( data->m_minShotsRequired <= m_consecutiveShots )
  139. {
  140. //We have fired enough shots to create the OCL.
  141. fireOCL();
  142. }
  143. }
  144. }
  145. else if( validToFireOCL )
  146. {
  147. //We've stopped being valid -- which means we may have stopped firing our weapon.
  148. Weapon *weapon = obj->getWeaponInWeaponSlot( data->m_weaponSlot );
  149. if( weapon && data->m_minShotsRequired <= m_consecutiveShots )
  150. {
  151. //We switched weapons! Fire OCL!
  152. fireOCL();
  153. }
  154. }
  155. if( validThisFrame != m_valid )
  156. {
  157. //There has been a validity change, so reset all values!
  158. m_valid = validThisFrame;
  159. resetStats();
  160. }
  161. return UPDATE_SLEEP_NONE;
  162. }
  163. //-------------------------------------------------------------------------------------------------
  164. void FireOCLAfterWeaponCooldownUpdate::resetStats()
  165. {
  166. m_consecutiveShots = 0;
  167. m_startFrame = 0;
  168. }
  169. //-------------------------------------------------------------------------------------------------
  170. void FireOCLAfterWeaponCooldownUpdate::fireOCL()
  171. {
  172. const FireOCLAfterWeaponCooldownUpdateModuleData* data = getFireOCLAfterWeaponCooldownUpdateModuleData();
  173. Object *obj = getObject();
  174. //Calculate the lifetime of the OCL.
  175. UnsignedInt now = TheGameLogic->getFrame();
  176. Real seconds = (now - m_startFrame) * SECONDS_PER_LOGICFRAME_REAL;
  177. seconds *= data->m_oclLifetimePerSecond * 0.001f;
  178. UnsignedInt oclFrames = (UnsignedInt)(seconds * LOGICFRAMES_PER_SECOND);
  179. oclFrames = MIN( oclFrames, data->m_oclMaxFrames );
  180. ObjectCreationList::create( data->m_ocl, obj, obj, oclFrames );
  181. resetStats();
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. /** CRC */
  185. // ------------------------------------------------------------------------------------------------
  186. void FireOCLAfterWeaponCooldownUpdate::crc( Xfer *xfer )
  187. {
  188. // extend base class
  189. UpdateModule::crc( xfer );
  190. // extend base class
  191. UpgradeMux::upgradeMuxCRC( xfer );
  192. } // end crc
  193. // ------------------------------------------------------------------------------------------------
  194. /** Xfer method
  195. * Version Info:
  196. * 1: Initial version */
  197. // ------------------------------------------------------------------------------------------------
  198. void FireOCLAfterWeaponCooldownUpdate::xfer( Xfer *xfer )
  199. {
  200. // version
  201. XferVersion currentVersion = 1;
  202. XferVersion version = currentVersion;
  203. xfer->xferVersion( &version, currentVersion );
  204. // extend base class
  205. UpdateModule::xfer( xfer );
  206. // extend base class
  207. UpgradeMux::upgradeMuxXfer( xfer );
  208. // valid
  209. xfer->xferBool( &m_valid );
  210. // consecutive shots
  211. xfer->xferUnsignedInt( &m_consecutiveShots );
  212. // start frame
  213. xfer->xferUnsignedInt( &m_startFrame );
  214. } // end xfer
  215. // ------------------------------------------------------------------------------------------------
  216. /** Load post process */
  217. // ------------------------------------------------------------------------------------------------
  218. void FireOCLAfterWeaponCooldownUpdate::loadPostProcess( void )
  219. {
  220. // extend base class
  221. UpdateModule::loadPostProcess();
  222. // extend base class
  223. UpgradeMux::upgradeMuxLoadPostProcess();
  224. } // end loadPostProcess