Energy.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: 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/Object.h"
  49. //-----------------------------------------------------------------------------
  50. Real Energy::getEnergySupplyRatio() const
  51. {
  52. DEBUG_ASSERTCRASH(m_energyProduction >= 0 && m_energyConsumption >= 0, ("neg Energy numbers\n"));
  53. if (m_energyConsumption == 0)
  54. return (Real)m_energyProduction;
  55. return (Real)m_energyProduction / (Real)m_energyConsumption;
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. Bool Energy::hasSufficientPower(void) const
  59. {
  60. return m_energyProduction >= m_energyConsumption;
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. void Energy::adjustPower(Int powerDelta, Bool adding)
  64. {
  65. if (powerDelta == 0) {
  66. return;
  67. }
  68. if (powerDelta > 0) {
  69. if (adding) {
  70. addProduction(powerDelta);
  71. } else {
  72. addProduction(-powerDelta);
  73. }
  74. } else {
  75. // Seems a little odd, however, consumption is reversed. Negative power is positive consumption.
  76. if (adding) {
  77. addConsumption(-powerDelta);
  78. } else {
  79. addConsumption(powerDelta);
  80. }
  81. }
  82. }
  83. //-------------------------------------------------------------------------------------------------
  84. /** new 'obj' will now add/subtract from this energy construct */
  85. //-------------------------------------------------------------------------------------------------
  86. void Energy::objectEnteringInfluence( Object *obj )
  87. {
  88. // sanity
  89. if( obj == NULL )
  90. return;
  91. // get the amount of energy this object produces or consumes
  92. Int energy = obj->getTemplate()->getEnergyProduction();
  93. // adjust energy
  94. if( energy < 0 )
  95. addConsumption( -energy );
  96. else if( energy > 0 )
  97. addProduction( energy );
  98. // sanity
  99. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  100. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  101. m_energyProduction, m_energyConsumption) );
  102. } // end objectEnteringInfluence
  103. //-------------------------------------------------------------------------------------------------
  104. /** 'obj' will now no longer add/subtrack from this energy construct */
  105. //-------------------------------------------------------------------------------------------------
  106. void Energy::objectLeavingInfluence( Object *obj )
  107. {
  108. // sanity
  109. if( obj == NULL )
  110. return;
  111. // get the amount of energy this object produces or consumes
  112. Int energy = obj->getTemplate()->getEnergyProduction();
  113. // adjust energy
  114. if( energy < 0 )
  115. addConsumption( energy );
  116. else if( energy > 0 )
  117. addProduction( -energy );
  118. // sanity
  119. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  120. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  121. m_energyProduction, m_energyConsumption) );
  122. }
  123. //-------------------------------------------------------------------------------------------------
  124. /** Adds an energy bonus to the player's pool of energy when the "Control Rods" upgrade
  125. is made to the American Cold Fusion Plant */
  126. //-------------------------------------------------------------------------------------------------
  127. void Energy::addPowerBonus( Object *obj )
  128. {
  129. // sanity
  130. if( obj == NULL )
  131. return;
  132. addProduction(obj->getTemplate()->getEnergyBonus());
  133. // sanity
  134. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  135. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  136. m_energyProduction, m_energyConsumption) );
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. /** Removed an energy bonus */
  140. // ------------------------------------------------------------------------------------------------
  141. void Energy::removePowerBonus( Object *obj )
  142. {
  143. // sanity
  144. if( obj == NULL )
  145. return;
  146. addProduction( -obj->getTemplate()->getEnergyBonus() );
  147. // sanity
  148. DEBUG_ASSERTCRASH( m_energyProduction >= 0 && m_energyConsumption >= 0,
  149. ("Energy - Negative Energy numbers, Produce=%d Consume=%d\n",
  150. m_energyProduction, m_energyConsumption) );
  151. } // end removePowerBonus
  152. // ------------------------------------------------------------------------------------------------
  153. // ------------------------------------------------------------------------------------------------
  154. // Private functions
  155. // ------------------------------------------------------------------------------------------------
  156. void Energy::addProduction(Int amt)
  157. {
  158. m_energyProduction += amt;
  159. if( m_owner == NULL )
  160. return;
  161. // A repeated Brownout signal does nothing bad, and we need to handle more than just edge cases.
  162. // Like low power, now even more low power, refresh disable.
  163. m_owner->onPowerBrownOutChange( !hasSufficientPower() );
  164. }
  165. // ------------------------------------------------------------------------------------------------
  166. void Energy::addConsumption(Int amt)
  167. {
  168. m_energyConsumption += amt;
  169. if( m_owner == NULL )
  170. return;
  171. m_owner->onPowerBrownOutChange( !hasSufficientPower() );
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. /** CRC */
  175. // ------------------------------------------------------------------------------------------------
  176. void Energy::crc( Xfer *xfer )
  177. {
  178. } // end crc
  179. // ------------------------------------------------------------------------------------------------
  180. /** Xfer method
  181. * Version Info:
  182. * 1: Initial version */
  183. // ------------------------------------------------------------------------------------------------
  184. void Energy::xfer( Xfer *xfer )
  185. {
  186. // version
  187. XferVersion currentVersion = 2;
  188. XferVersion version = currentVersion;
  189. xfer->xferVersion( &version, currentVersion );
  190. // It is actually incorrect to save these, as they are reconstructed when the buildings are loaded
  191. // I need to version though so old games will load wrong rather than crashing
  192. // production
  193. if( version < 2 )
  194. xfer->xferInt( &m_energyProduction );
  195. // consumption
  196. if( version < 2 )
  197. xfer->xferInt( &m_energyConsumption );
  198. // owning player index
  199. Int owningPlayerIndex;
  200. if( xfer->getXferMode() == XFER_SAVE )
  201. owningPlayerIndex = m_owner->getPlayerIndex();
  202. xfer->xferInt( &owningPlayerIndex );
  203. m_owner = ThePlayerList->getNthPlayer( owningPlayerIndex );
  204. } // end xfer
  205. // ------------------------------------------------------------------------------------------------
  206. /** Load post process */
  207. // ------------------------------------------------------------------------------------------------
  208. void Energy::loadPostProcess( void )
  209. {
  210. } // end loadPostProcess