CashHackSpecialPower.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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: CashHackSpecialPower.cpp /////////////////////////////////////////////////////////////
  24. // Author: Amit Kumar, July 2002
  25. // Desc: The Cash Hack will steal money from an enemy player
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Player.h"
  30. #include "Common/Team.h"
  31. #include "Common/Xfer.h"
  32. #include "GameClient/GameText.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameLogic/PartitionManager.h"
  35. #include "GameLogic/Module/CashHackSpecialPower.h"
  36. #include "GameClient/InGameUI.h"
  37. // ------------------------------------------------------------------------------------------------
  38. // ------------------------------------------------------------------------------------------------
  39. CashHackSpecialPowerModuleData::CashHackSpecialPowerModuleData( void )
  40. {
  41. m_upgrades.clear();
  42. m_defaultAmountToSteal = 0;
  43. }
  44. //-------------------------------------------------------------------------------------------------
  45. //-------------------------------------------------------------------------------------------------
  46. static void parseCashHackUpgradePair( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
  47. {
  48. CashHackSpecialPowerModuleData::Upgrades up;
  49. INI::parseScience(ini, NULL, &up.m_science, NULL);
  50. INI::parseInt(ini, NULL, &up.m_amountToSteal, NULL);
  51. std::vector<CashHackSpecialPowerModuleData::Upgrades>* s = (std::vector<CashHackSpecialPowerModuleData::Upgrades>*)store;
  52. s->push_back(up);
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // ------------------------------------------------------------------------------------------------
  56. /*static*/ void CashHackSpecialPowerModuleData::buildFieldParse(MultiIniFieldParse& p)
  57. {
  58. SpecialPowerModuleData::buildFieldParse( p );
  59. static const FieldParse dataFieldParse[] =
  60. {
  61. { "UpgradeMoneyAmount", parseCashHackUpgradePair, NULL, offsetof( CashHackSpecialPowerModuleData, m_upgrades ) },
  62. { "MoneyAmount", INI::parseInt, NULL, offsetof( CashHackSpecialPowerModuleData, m_defaultAmountToSteal ) },
  63. { 0, 0, 0, 0 }
  64. };
  65. p.add(dataFieldParse);
  66. } // end buildFieldParse
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////
  69. ///////////////////////////////////////////////////////////////////////////////////////////////////
  70. // ------------------------------------------------------------------------------------------------
  71. // ------------------------------------------------------------------------------------------------
  72. CashHackSpecialPower::CashHackSpecialPower( Thing *thing, const ModuleData *moduleData )
  73. : SpecialPowerModule( thing, moduleData )
  74. {
  75. } // end CashHackSpecialPower
  76. // ------------------------------------------------------------------------------------------------
  77. // ------------------------------------------------------------------------------------------------
  78. CashHackSpecialPower::~CashHackSpecialPower( void )
  79. {
  80. } // end ~CashHackSpecialPower
  81. // ------------------------------------------------------------------------------------------------
  82. // ------------------------------------------------------------------------------------------------
  83. void CashHackSpecialPower::doSpecialPowerAtLocation( const Coord3D *loc, Real angle, UnsignedInt commandOptions )
  84. {
  85. if (getObject()->isDisabled())
  86. return;
  87. // only allowed at objects
  88. return;
  89. }
  90. //-------------------------------------------------------------------------------------------------
  91. //-------------------------------------------------------------------------------------------------
  92. Int CashHackSpecialPower::findAmountToSteal() const
  93. {
  94. const CashHackSpecialPowerModuleData* d = getCashHackSpecialPowerModuleData();
  95. const Player* controller = getObject()->getControllingPlayer();
  96. if (controller != NULL)
  97. {
  98. for (std::vector<CashHackSpecialPowerModuleData::Upgrades>::const_iterator it = d->m_upgrades.begin();
  99. it != d->m_upgrades.end();
  100. ++it)
  101. {
  102. if (controller->hasScience(it->m_science))
  103. return it->m_amountToSteal;
  104. }
  105. }
  106. return d->m_defaultAmountToSteal;
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. // ------------------------------------------------------------------------------------------------
  110. void CashHackSpecialPower::doSpecialPowerAtObject( Object *victim, UnsignedInt commandOptions )
  111. {
  112. if (getObject()->isDisabled())
  113. return;
  114. // sanity
  115. if (!victim)
  116. return;
  117. // call the base class action cause we are *EXTENDING* functionality
  118. SpecialPowerModule::doSpecialPowerAtObject( victim, commandOptions );
  119. // get our module data
  120. Object *self = getObject();
  121. //Steal a thousand cash from the other team!
  122. Money *targetMoney = victim->getControllingPlayer()->getMoney();
  123. Money *selfMoney = self->getControllingPlayer()->getMoney();
  124. if( targetMoney && selfMoney )
  125. {
  126. UnsignedInt cash = targetMoney->countMoney();
  127. UnsignedInt desiredAmount = findAmountToSteal();
  128. //Check to see if they have 1000 cash, otherwise, take the remainder!
  129. cash = min( desiredAmount, cash );
  130. if( cash > 0 )
  131. {
  132. //Steal the cash
  133. targetMoney->withdraw( cash );
  134. selfMoney->deposit( cash );
  135. self->getControllingPlayer()->getScoreKeeper()->addMoneyEarned( cash );
  136. //Display cash income floating over the blacklotus
  137. UnicodeString moneyString;
  138. moneyString.format( TheGameText->fetch( "GUI:AddCash" ), cash );
  139. Coord3D pos;
  140. pos.zero();
  141. pos.add( self->getPosition() );
  142. pos.z += 20.0f; //add a little z to make it show up above the unit.
  143. TheInGameUI->addFloatingText( moneyString, &pos, GameMakeColor( 0, 255, 0, 255 ) );
  144. //Display cash lost floating over the target
  145. moneyString.format( TheGameText->fetch( "GUI:LoseCash" ), cash );
  146. pos.zero();
  147. pos.add( victim->getPosition() );
  148. pos.z += 30.0f; //add a little z to make it show up above the unit.
  149. TheInGameUI->addFloatingText( moneyString, &pos, GameMakeColor( 255, 0, 0, 255 ) );
  150. }
  151. }
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. /** CRC */
  155. // ------------------------------------------------------------------------------------------------
  156. void CashHackSpecialPower::crc( Xfer *xfer )
  157. {
  158. // extend base class
  159. SpecialPowerModule::crc( xfer );
  160. } // end crc
  161. // ------------------------------------------------------------------------------------------------
  162. /** Xfer method
  163. * Version Info:
  164. * 1: Initial version */
  165. // ------------------------------------------------------------------------------------------------
  166. void CashHackSpecialPower::xfer( Xfer *xfer )
  167. {
  168. // version
  169. XferVersion currentVersion = 1;
  170. XferVersion version = currentVersion;
  171. xfer->xferVersion( &version, currentVersion );
  172. // extend base class
  173. SpecialPowerModule::xfer( xfer );
  174. } // end xfer
  175. // ------------------------------------------------------------------------------------------------
  176. /** Load post process */
  177. // ------------------------------------------------------------------------------------------------
  178. void CashHackSpecialPower::loadPostProcess( void )
  179. {
  180. // extend base class
  181. SpecialPowerModule::loadPostProcess();
  182. } // end loadPostProcess