SalvageCrateCollide.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: SalvageCrateCollide.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, March 2002
  25. // Desc: The Savlage system can give a Weaponset bonus, a level, or money. Salvagers create them
  26. // by killing marked units, and only WeaponSalvagers can get the WeaponSet bonus
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "GameLogic/Module/SalvageCrateCollide.h"
  31. #include "Common/AudioEventRTS.h"
  32. #include "Common/MiscAudio.h"
  33. #include "Common/Kindof.h"
  34. #include "Common/RandomValue.h"
  35. #include "Common/Player.h"
  36. #include "Common/ThingTemplate.h"
  37. #include "Common/Xfer.h"
  38. #include "GameClient/GameText.h"
  39. #include "GameClient/InGameUI.h"
  40. #include "GameLogic/ExperienceTracker.h"
  41. #include "GameLogic/Object.h"
  42. //-------------------------------------------------------------------------------------------------
  43. //-------------------------------------------------------------------------------------------------
  44. SalvageCrateCollide::SalvageCrateCollide( Thing *thing, const ModuleData* moduleData ) : CrateCollide( thing, moduleData )
  45. {
  46. }
  47. //-------------------------------------------------------------------------------------------------
  48. //-------------------------------------------------------------------------------------------------
  49. SalvageCrateCollide::~SalvageCrateCollide( void )
  50. {
  51. }
  52. //-------------------------------------------------------------------------------------------------
  53. Bool SalvageCrateCollide::isValidToExecute( const Object *other ) const
  54. {
  55. if( ! CrateCollide::isValidToExecute( other ) )
  56. return FALSE;
  57. // Only salvage units can pick up a Salvage crate
  58. if( ! other->getTemplate()->isKindOf( KINDOF_SALVAGER ) )
  59. return FALSE;
  60. return TRUE;
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. Bool SalvageCrateCollide::executeCrateBehavior( Object *other )
  64. {
  65. if( eligibleForWeaponSet( other ) && testWeaponChance() )
  66. {
  67. doWeaponSet( other );
  68. //Play the salvage installation crate pickup sound.
  69. AudioEventRTS soundToPlay = TheAudio->getMiscAudio()->m_crateSalvage;
  70. soundToPlay.setObjectID( other->getID() );
  71. TheAudio->addAudioEvent( &soundToPlay );
  72. //Play the unit voice acknowledgement for upgrading weapons.
  73. //Already handled by the "move order"
  74. //const AudioEventRTS *soundToPlayPtr = other->getTemplate()->getPerUnitSound( "VoiceSalvage" );
  75. //soundToPlay = *soundToPlayPtr;
  76. //soundToPlay.setObjectID( other->getID() );
  77. //TheAudio->addAudioEvent( &soundToPlay );
  78. }
  79. else if( eligibleForLevel( other ) && testLevelChance() )
  80. {
  81. doLevelGain( other );
  82. //Sound will play in
  83. //soundToPlay = TheAudio->getMiscAudio()->m_unitPromoted;
  84. }
  85. else // just assume the testMoneyChance
  86. {
  87. doMoney( other );
  88. AudioEventRTS soundToPlay = TheAudio->getMiscAudio()->m_crateMoney;
  89. soundToPlay.setObjectID( other->getID() );
  90. TheAudio->addAudioEvent(&soundToPlay);
  91. }
  92. return TRUE;
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. Bool SalvageCrateCollide::eligibleForWeaponSet( Object *other )
  96. {
  97. if( other == NULL )
  98. return FALSE;
  99. // A kindof marks eligibility, and you must not be fully upgraded
  100. if( !other->isKindOf(KINDOF_WEAPON_SALVAGER) )
  101. return FALSE;
  102. if( other->testWeaponSetFlag(WEAPONSET_CRATEUPGRADE_TWO) )
  103. return FALSE;
  104. return TRUE;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. Bool SalvageCrateCollide::eligibleForLevel( Object *other )
  108. {
  109. if( other == NULL )
  110. return FALSE;
  111. // Sorry, you are max level
  112. if( other->getExperienceTracker()->getVeterancyLevel() == LEVEL_HEROIC )
  113. return FALSE;
  114. // Sorry, you can't gain levels
  115. if( !other->getExperienceTracker()->isTrainable() )
  116. return FALSE;
  117. return TRUE;
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. Bool SalvageCrateCollide::testWeaponChance()
  121. {
  122. const SalvageCrateCollideModuleData *md = getSalvageCrateCollideModuleData();
  123. if( md->m_weaponChance == 1.0f )
  124. return TRUE; // don't waste a random number for a 100%
  125. Real randomNumber = GameLogicRandomValueReal( 0, 1 );
  126. if( randomNumber < md->m_weaponChance )
  127. return TRUE;
  128. return FALSE;
  129. }
  130. Bool SalvageCrateCollide::testLevelChance()
  131. {
  132. const SalvageCrateCollideModuleData *md = getSalvageCrateCollideModuleData();
  133. if( md->m_levelChance == 1.0f )
  134. return TRUE; // don't waste a random number for a 100%
  135. Real randomNumber = GameLogicRandomValueReal( 0, 1 );
  136. if( randomNumber < md->m_levelChance )
  137. return TRUE;
  138. return FALSE;
  139. }
  140. void SalvageCrateCollide::doWeaponSet( Object *other )
  141. {
  142. if( other->testWeaponSetFlag( WEAPONSET_CRATEUPGRADE_ONE ) )
  143. {
  144. other->clearWeaponSetFlag( WEAPONSET_CRATEUPGRADE_ONE );
  145. other->setWeaponSetFlag( WEAPONSET_CRATEUPGRADE_TWO );
  146. }
  147. else
  148. {
  149. other->setWeaponSetFlag( WEAPONSET_CRATEUPGRADE_ONE );
  150. }
  151. }
  152. void SalvageCrateCollide::doLevelGain( Object *other )
  153. {
  154. other->getExperienceTracker()->gainExpForLevel( 1 );
  155. }
  156. void SalvageCrateCollide::doMoney( Object *other )
  157. {
  158. const SalvageCrateCollideModuleData *md = getSalvageCrateCollideModuleData();
  159. Int money;
  160. if( md->m_minimumMoney != md->m_maximumMoney )// Random value doesn't like to get a constant range
  161. money = GameLogicRandomValue( md->m_minimumMoney, md->m_maximumMoney );
  162. else
  163. money = md->m_minimumMoney;
  164. if( money > 0 )
  165. {
  166. other->getControllingPlayer()->getMoney()->deposit( money );
  167. other->getControllingPlayer()->getScoreKeeper()->addMoneyEarned( money );
  168. //Display cash income floating over the crate. Position is me, everything else is them.
  169. UnicodeString moneyString;
  170. moneyString.format( TheGameText->fetch( "GUI:AddCash" ), money );
  171. Coord3D pos;
  172. pos.set( getObject()->getPosition() );
  173. pos.z += 10.0f; //add a little z to make it show up above the unit.
  174. Color color = other->getControllingPlayer()->getPlayerColor() | GameMakeColor( 0, 0, 0, 230 );
  175. TheInGameUI->addFloatingText( moneyString, &pos, color );
  176. }
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. /** CRC */
  180. // ------------------------------------------------------------------------------------------------
  181. void SalvageCrateCollide::crc( Xfer *xfer )
  182. {
  183. // extend base class
  184. CrateCollide::crc( xfer );
  185. } // end crc
  186. // ------------------------------------------------------------------------------------------------
  187. /** Xfer method
  188. * Version Info:
  189. * 1: Initial version */
  190. // ------------------------------------------------------------------------------------------------
  191. void SalvageCrateCollide::xfer( Xfer *xfer )
  192. {
  193. // version
  194. XferVersion currentVersion = 1;
  195. XferVersion version = currentVersion;
  196. xfer->xferVersion( &version, currentVersion );
  197. // extend base class
  198. CrateCollide::xfer( xfer );
  199. } // end xfer
  200. // ------------------------------------------------------------------------------------------------
  201. /** Load post process */
  202. // ------------------------------------------------------------------------------------------------
  203. void SalvageCrateCollide::loadPostProcess( void )
  204. {
  205. // extend base class
  206. CrateCollide::loadPostProcess();
  207. } // end loadPostProcess