Energy.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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: Energy.cpp /////////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: Energy.cpp
  36. //
  37. // Created: Steven Johnson, October 2001
  38. //
  39. // Desc: @todo
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  43. #include "Common/Energy.h"
  44. #include "Common/Player.h"
  45. #include "Common/PlayerList.h"
  46. #include "Common/ThingTemplate.h"
  47. #include "Common/Xfer.h"
  48. #include "GameLogic/GameLogic.h"
  49. #include "GameLogic/Object.h"
  50. #ifdef _INTERNAL
  51. // for occasional debugging...
  52. //#pragma optimize("", off)
  53. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  54. #endif
  55. //-----------------------------------------------------------------------------
  56. Energy::Energy()
  57. {
  58. m_energyProduction = 0;
  59. m_energyConsumption = 0;
  60. m_owner = NULL;
  61. m_powerSabotagedTillFrame = 0;
  62. }
  63. //-----------------------------------------------------------------------------
  64. Int Energy::getProduction() const
  65. {
  66. if( TheGameLogic->getFrame() < m_powerSabotagedTillFrame )
  67. {
  68. //Power sabotaged, therefore no power.
  69. return 0;
  70. }
  71. return m_energyProduction;
  72. }
  73. //-----------------------------------------------------------------------------
  74. Real Energy::getEnergySupplyRatio() const
  75. {
  76. DEBUG_ASSERTCRASH(m_energyProduction >= 0 && m_energyConsumption >= 0, ("neg Energy numbers\n"));
  77. if( TheGameLogic->getFrame() < m_powerSabotagedTillFrame )
  78. {
  79. //Power sabotaged, therefore no power, no ratio.
  80. return 0.0f;
  81. }
  82. if (m_energyConsumption == 0)
  83. return (Real)m_energyProduction;
  84. return (Real)m_energyProduction / (Real)m_energyConsumption;
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. Bool Energy::hasSufficientPower(void) const
  88. {
  89. if( TheGameLogic->getFrame() < m_powerSabotagedTillFrame )
  90. {
  91. //Power sabotaged, therefore no power.
  92. return FALSE;
  93. }
  94. return m_energyProduction >= m_energyConsumption;
  95. }
  96. //-------------------------------------------------------------------------------------------------
  97. void Energy::adjustPower(Int powerDelta, Bool adding)
  98. {
  99. if (powerDelta == 0) {
  100. return;
  101. }
  102. if (powerDelta > 0) {
  103. if (adding) {
  104. addProduction(powerDelta);
  105. } else {
  106. addProduction(-powerDelta);
  107. }
  108. } else {
  109. // Seems a little odd, however, consumption is reversed. Negative power is positive consumption.
  110. if (adding) {
  111. addConsumption(-powerDelta);
  112. } else {
  113. addConsumption(powerDelta);
  114. }
  115. }
  116. }
  117. //-------------------------------------------------------------------------------------------------
  118. /** new 'obj' will now add/subtract from this energy construct */
  119. //-------------------------------------------------------------------------------------------------
  120. void Energy::objectEnteringInfluence( Object *obj )
  121. {
  122. // sanity
  123. if( obj == NULL )
  124. return;
  125. // get the amount of energy this object produces or consumes
  126. Int energy = obj->getTemplate()->getEnergyProduction();
  127. // adjust energy
  128. if( energy < 0 )
  129. addConsumption( -energy );
  130. else if( energy > 0 )
  131. addProduction( energy );
  132. // sanity
  133. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  134. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  135. m_energyProduction, m_energyConsumption) );
  136. } // end objectEnteringInfluence
  137. //-------------------------------------------------------------------------------------------------
  138. /** 'obj' will now no longer add/subtrack from this energy construct */
  139. //-------------------------------------------------------------------------------------------------
  140. void Energy::objectLeavingInfluence( Object *obj )
  141. {
  142. // sanity
  143. if( obj == NULL )
  144. return;
  145. // get the amount of energy this object produces or consumes
  146. Int energy = obj->getTemplate()->getEnergyProduction();
  147. // adjust energy
  148. if( energy < 0 )
  149. addConsumption( energy );
  150. else if( energy > 0 )
  151. addProduction( -energy );
  152. // sanity
  153. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  154. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  155. m_energyProduction, m_energyConsumption) );
  156. }
  157. //-------------------------------------------------------------------------------------------------
  158. /** Adds an energy bonus to the player's pool of energy when the "Control Rods" upgrade
  159. is made to the American Cold Fusion Plant */
  160. //-------------------------------------------------------------------------------------------------
  161. void Energy::addPowerBonus( Object *obj )
  162. {
  163. // sanity
  164. if( obj == NULL )
  165. return;
  166. addProduction(obj->getTemplate()->getEnergyBonus());
  167. // sanity
  168. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  169. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  170. m_energyProduction, m_energyConsumption) );
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. /** Removed an energy bonus */
  174. // ------------------------------------------------------------------------------------------------
  175. void Energy::removePowerBonus( Object *obj )
  176. {
  177. // sanity
  178. if( obj == NULL )
  179. return;
  180. addProduction( -obj->getTemplate()->getEnergyBonus() );
  181. // sanity
  182. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  183. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  184. m_energyProduction, m_energyConsumption) );
  185. } // end removePowerBonus
  186. // ------------------------------------------------------------------------------------------------
  187. // ------------------------------------------------------------------------------------------------
  188. // Private functions
  189. // ------------------------------------------------------------------------------------------------
  190. void Energy::addProduction(Int amt)
  191. {
  192. m_energyProduction += amt;
  193. if( m_owner == NULL )
  194. return;
  195. // A repeated Brownout signal does nothing bad, and we need to handle more than just edge cases.
  196. // Like low power, now even more low power, refresh disable.
  197. m_owner->onPowerBrownOutChange( !hasSufficientPower() );
  198. }
  199. // ------------------------------------------------------------------------------------------------
  200. void Energy::addConsumption(Int amt)
  201. {
  202. m_energyConsumption += amt;
  203. if( m_owner == NULL )
  204. return;
  205. m_owner->onPowerBrownOutChange( !hasSufficientPower() );
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. /** CRC */
  209. // ------------------------------------------------------------------------------------------------
  210. void Energy::crc( Xfer *xfer )
  211. {
  212. } // end crc
  213. // ------------------------------------------------------------------------------------------------
  214. /** Xfer method
  215. * Version Info:
  216. * 1: Initial version */
  217. // ------------------------------------------------------------------------------------------------
  218. void Energy::xfer( Xfer *xfer )
  219. {
  220. // version
  221. XferVersion currentVersion = 3;
  222. XferVersion version = currentVersion;
  223. xfer->xferVersion( &version, currentVersion );
  224. // It is actually incorrect to save these, as they are reconstructed when the buildings are loaded
  225. // I need to version though so old games will load wrong rather than crashing
  226. // production
  227. if( version < 2 )
  228. xfer->xferInt( &m_energyProduction );
  229. // consumption
  230. if( version < 2 )
  231. xfer->xferInt( &m_energyConsumption );
  232. // owning player index
  233. Int owningPlayerIndex;
  234. if( xfer->getXferMode() == XFER_SAVE )
  235. owningPlayerIndex = m_owner->getPlayerIndex();
  236. xfer->xferInt( &owningPlayerIndex );
  237. m_owner = ThePlayerList->getNthPlayer( owningPlayerIndex );
  238. //Sabotage
  239. if( version >= 3 )
  240. {
  241. xfer->xferUnsignedInt( &m_powerSabotagedTillFrame );
  242. }
  243. } // end xfer
  244. // ------------------------------------------------------------------------------------------------
  245. /** Load post process */
  246. // ------------------------------------------------------------------------------------------------
  247. void Energy::loadPostProcess( void )
  248. {
  249. } // end loadPostProcess