AutoDepositUpdate.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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: AutoDepositUpdate.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Aug 2002
  34. //
  35. // Filename: AutoDepositUpdate.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose: The meat of the auto deposit update module
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. //-----------------------------------------------------------------------------
  44. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  45. //-----------------------------------------------------------------------------
  46. //-----------------------------------------------------------------------------
  47. // USER INCLUDES //////////////////////////////////////////////////////////////
  48. //-----------------------------------------------------------------------------
  49. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  50. #include "Common/BuildAssistant.h"
  51. #include "Common/Thing.h"
  52. #include "Common/ThingTemplate.h"
  53. #include "Common/INI.h"
  54. #include "Common/RandomValue.h"
  55. #include "Common/Player.h"
  56. #include "Common/Xfer.h"
  57. #include "GameLogic/GameLogic.h"
  58. #include "GameLogic/Module/AutoDepositUpdate.h"
  59. #include "GameLogic/Module/AIUpdate.h"
  60. #include "GameLogic/Object.h"
  61. #include "GameClient/InGameUI.h"
  62. #include "GameClient/Color.h"
  63. #include "GameClient/GameText.h"
  64. //-------------------------------------------------------------------------------------------------
  65. void parseUpgradePair( INI *ini, void *instance, void *store, const void *userData )
  66. {
  67. upgradePair info;
  68. info.type = "";
  69. info.amount = 0;
  70. const char *token = ini->getNextToken( ini->getSepsColon() );
  71. if ( stricmp(token, "UpgradeType") == 0 )
  72. {
  73. token = ini->getNextTokenOrNull( ini->getSepsColon() );
  74. if (!token) throw INI_INVALID_DATA;
  75. info.type = token;
  76. }
  77. else
  78. throw INI_INVALID_DATA;
  79. token = ini->getNextTokenOrNull( ini->getSepsColon() );
  80. if ( stricmp(token, "Boost") == 0 )
  81. info.amount = INI::scanInt(ini->getNextToken( ini->getSepsColon() ));
  82. else
  83. throw INI_INVALID_DATA;
  84. // Insert the info into the upgrade list
  85. std::list<upgradePair> * theList = (std::list<upgradePair>*)store;
  86. theList->push_back(info);
  87. } // end parseFactionObjectCreationList
  88. //-----------------------------------------------------------------------------
  89. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  90. //-----------------------------------------------------------------------------
  91. AutoDepositUpdate::AutoDepositUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  92. {
  93. m_depositOnFrame = TheGameLogic->getFrame() + getAutoDepositUpdateModuleData()->m_depositFrame;
  94. m_awardInitialCaptureBonus = FALSE;
  95. m_initialized = FALSE;
  96. }
  97. //-------------------------------------------------------------------------------------------------
  98. //-------------------------------------------------------------------------------------------------
  99. AutoDepositUpdate::~AutoDepositUpdate( void )
  100. {
  101. }
  102. //-------------------------------------------------------------------------------------------------
  103. //-------------------------------------------------------------------------------------------------
  104. void AutoDepositUpdate::awardInitialCaptureBonus( Player *player )
  105. {
  106. m_depositOnFrame = TheGameLogic->getFrame() + getAutoDepositUpdateModuleData()->m_depositFrame;
  107. if(!player || !m_awardInitialCaptureBonus || getAutoDepositUpdateModuleData()->m_initialCaptureBonus <= 0)
  108. return;
  109. player->getMoney()->deposit( getAutoDepositUpdateModuleData()->m_initialCaptureBonus );
  110. player->getScoreKeeper()->addMoneyEarned( getAutoDepositUpdateModuleData()->m_initialCaptureBonus );
  111. //Display cash income floating over the blacklotus
  112. if(getAutoDepositUpdateModuleData()->m_initialCaptureBonus > 0)
  113. {
  114. UnicodeString moneyString;
  115. moneyString.format( TheGameText->fetch( "GUI:AddCash" ), getAutoDepositUpdateModuleData()->m_initialCaptureBonus );
  116. Coord3D pos;
  117. pos.set( getObject()->getPosition() );
  118. pos.z += 10.0f; //add a little z to make it show up above the unit.
  119. Color color = player->getPlayerColor() | GameMakeColor( 0, 0, 0, 230 );
  120. TheInGameUI->addFloatingText( moneyString, &pos, color );
  121. }
  122. m_awardInitialCaptureBonus = FALSE;
  123. }
  124. //-------------------------------------------------------------------------------------------------
  125. //-------------------------------------------------------------------------------------------------
  126. UpdateSleepTime AutoDepositUpdate::update( void )
  127. {
  128. const AutoDepositUpdateModuleData *modData = getAutoDepositUpdateModuleData();
  129. /// @todo srj use SLEEPY_UPDATE here
  130. if( TheGameLogic->getFrame() >= m_depositOnFrame)
  131. {
  132. if (!m_initialized) {
  133. // Note - we have to set these in update, because during load the team is set,
  134. // and we don't want to award initial bonus on load. jba :)
  135. m_awardInitialCaptureBonus = TRUE;
  136. m_initialized = TRUE;
  137. }
  138. m_depositOnFrame = TheGameLogic->getFrame() + modData->m_depositFrame;
  139. if(getObject()->isNeutralControlled() || modData->m_depositAmount <= 0 )
  140. return UPDATE_SLEEP_NONE;
  141. // makes sure that buildings under construction do not get a bonus CCB
  142. if( getObject()->getConstructionPercent() != CONSTRUCTION_COMPLETE )
  143. return UPDATE_SLEEP_NONE;
  144. int moneyAmount = modData->m_depositAmount + getUpgradedSupplyBoost();
  145. if( modData->m_isActualMoney )
  146. {
  147. getObject()->getControllingPlayer()->getMoney()->deposit( moneyAmount );
  148. getObject()->getControllingPlayer()->getScoreKeeper()->addMoneyEarned( modData->m_depositAmount);
  149. }
  150. Bool displayMoney = moneyAmount > 0 ? TRUE : FALSE;
  151. if( getObject()->testStatus(OBJECT_STATUS_STEALTHED) )
  152. {
  153. // OY LOOK! I AM USING LOCAL PLAYER. Do not put anything other than TheInGameUI->addFloatingText in the block this controls!!!
  154. if( !getObject()->isLocallyControlled() && !getObject()->testStatus(OBJECT_STATUS_DETECTED) )
  155. {
  156. displayMoney = FALSE;
  157. }
  158. }
  159. if( displayMoney )
  160. {
  161. const Object *owner = getObject();
  162. if ( owner )
  163. {
  164. // OY LOOK! I AM USING LOCAL PLAYER. Do not put anything other than TheInGameUI->addFloatingText in the block this controls!!!
  165. UnicodeString moneyString;
  166. moneyString.format( TheGameText->fetch( "GUI:AddCash" ), moneyAmount );
  167. Coord3D pos;
  168. pos.set( getObject()->getPosition() );
  169. pos.z += 10.0f; //add a little z to make it show up above the unit.
  170. if ( owner->isKindOf( KINDOF_STRUCTURE ) )
  171. {
  172. Real width = owner->getGeometryInfo().getMajorRadius() * 0.3f;
  173. Real depth = owner->getGeometryInfo().getMinorRadius() * 0.3f;
  174. pos.x += GameClientRandomValue(-width,width);
  175. pos.y += GameClientRandomValue(-depth,depth);
  176. }
  177. Color color = getObject()->getControllingPlayer()->getPlayerColor() | GameMakeColor( 0, 0, 0, 230 );
  178. TheInGameUI->addFloatingText( moneyString, &pos, color );
  179. }
  180. }
  181. }
  182. return UPDATE_SLEEP_NONE;
  183. }
  184. //------------------------------------------------------------------------------------------------
  185. Int AutoDepositUpdate::getUpgradedSupplyBoost() const
  186. {
  187. Player *player = getObject()->getControllingPlayer();
  188. if (!player) return 0;
  189. // Loop through the upgrade pairs and see if an upgrade is present that adds supply boost
  190. std::list<upgradePair>::const_iterator it = getAutoDepositUpdateModuleData()->m_upgradeBoost.begin();
  191. while (it != getAutoDepositUpdateModuleData()->m_upgradeBoost.end())
  192. {
  193. upgradePair info = *it;
  194. // Check if the player has the desired upgrade. If so return the boost
  195. static const UpgradeTemplate *upgradeTemplate = TheUpgradeCenter->findUpgrade( info.type.c_str() );
  196. if (player && upgradeTemplate && player->hasUpgradeComplete(upgradeTemplate))
  197. {
  198. return info.amount;
  199. }
  200. // check next
  201. ++it;
  202. }
  203. return 0;
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. /** CRC */
  207. // ------------------------------------------------------------------------------------------------
  208. void AutoDepositUpdate::crc( Xfer *xfer )
  209. {
  210. // extend base class
  211. UpdateModule::crc( xfer );
  212. } // end crc
  213. // ------------------------------------------------------------------------------------------------
  214. /** Xfer method
  215. * Version Info:
  216. * 1: Initial version */
  217. // ------------------------------------------------------------------------------------------------
  218. void AutoDepositUpdate::xfer( Xfer *xfer )
  219. {
  220. // version
  221. XferVersion currentVersion = 2;
  222. XferVersion version = currentVersion;
  223. xfer->xferVersion( &version, currentVersion );
  224. // extend base class
  225. UpdateModule::xfer( xfer );
  226. // deposit on frame
  227. xfer->xferUnsignedInt( &m_depositOnFrame );
  228. // award initial capture bonus
  229. xfer->xferBool( &m_awardInitialCaptureBonus );
  230. if (version>1) {
  231. xfer->xferBool(&m_initialized);
  232. }
  233. } // end xfer
  234. // ------------------------------------------------------------------------------------------------
  235. /** Load post process */
  236. // ------------------------------------------------------------------------------------------------
  237. void AutoDepositUpdate::loadPostProcess( void )
  238. {
  239. // extend base class
  240. UpdateModule::loadPostProcess();
  241. } // end loadPostProcess