5151#include " Common/Player.h"
5252#include " Common/PlayerList.h"
5353#include " Common/Xfer.h"
54+ #include " GameLogic/GameLogic.h"
5455
5556// ------------------------------------------------------------------------------------------------
5657UnsignedInt Money::withdraw (UnsignedInt amountToWithdraw, Bool playSound)
@@ -78,7 +79,7 @@ UnsignedInt Money::withdraw(UnsignedInt amountToWithdraw, Bool playSound)
7879}
7980
8081// ------------------------------------------------------------------------------------------------
81- void Money::deposit (UnsignedInt amountToDeposit, Bool playSound)
82+ void Money::deposit (UnsignedInt amountToDeposit, Bool playSound, Bool trackIncome )
8283{
8384 if (amountToDeposit == 0 )
8485 return ;
@@ -88,6 +89,12 @@ void Money::deposit(UnsignedInt amountToDeposit, Bool playSound)
8889 triggerAudioEvent (TheAudio->getMiscAudio ()->m_moneyDepositSound );
8990 }
9091
92+ if (trackIncome)
93+ {
94+ m_incomeBuckets[m_currentBucket] += amountToDeposit;
95+ m_cashPerMinute += amountToDeposit;
96+ }
97+
9198 m_money += amountToDeposit;
9299
93100 if ( amountToDeposit > 0 )
@@ -100,6 +107,34 @@ void Money::deposit(UnsignedInt amountToDeposit, Bool playSound)
100107 }
101108}
102109
110+ // ------------------------------------------------------------------------------------------------
111+ void Money::setStartingCash (UnsignedInt amount)
112+ {
113+ m_money = amount;
114+ std::fill (m_incomeBuckets, m_incomeBuckets + ARRAY_SIZE (m_incomeBuckets), 0u );
115+ m_currentBucket = 0u ;
116+ m_cashPerMinute = 0u ;
117+ }
118+
119+ // ------------------------------------------------------------------------------------------------
120+ void Money::updateIncomeBucket ()
121+ {
122+ UnsignedInt frame = TheGameLogic->getFrame ();
123+ UnsignedInt nextBucket = (frame / LOGICFRAMES_PER_SECOND) % ARRAY_SIZE (m_incomeBuckets);
124+ if (nextBucket != m_currentBucket)
125+ {
126+ m_cashPerMinute -= m_incomeBuckets[nextBucket];
127+ m_currentBucket = nextBucket;
128+ m_incomeBuckets[m_currentBucket] = 0u ;
129+ }
130+ }
131+
132+ // ------------------------------------------------------------------------------------------------
133+ UnsignedInt Money::getCashPerMinute () const
134+ {
135+ return m_cashPerMinute;
136+ }
137+
103138void Money::triggerAudioEvent (const AudioEventRTS& audioEvent)
104139{
105140 Real volume = TheAudio->getAudioSettings ()->m_preferredMoneyTransactionVolume ;
@@ -125,19 +160,35 @@ void Money::crc( Xfer *xfer )
125160// ------------------------------------------------------------------------------------------------
126161/* * Xfer method
127162 * Version Info:
128- * 1: Initial version */
163+ * 1: Initial version
164+ * 2: Add saveload support for the cash per minute income tracking */
129165// ------------------------------------------------------------------------------------------------
130166void Money::xfer ( Xfer *xfer )
131167{
132168
133169 // version
170+ #if RETAIL_COMPATIBLE_XFER_SAVE
134171 XferVersion currentVersion = 1 ;
172+ #else
173+ XferVersion currentVersion = 2 ;
174+ #endif
135175 XferVersion version = currentVersion;
136176 xfer->xferVersion ( &version, currentVersion );
137177
138178 // money value
139179 xfer->xferUnsignedInt ( &m_money );
140180
181+ if (version <= 1 )
182+ {
183+ setStartingCash (m_money);
184+ }
185+ else
186+ {
187+ xfer->xferUser (m_incomeBuckets, sizeof (m_incomeBuckets));
188+ xfer->xferUnsignedInt (&m_currentBucket);
189+
190+ m_cashPerMinute = std::accumulate (m_incomeBuckets, m_incomeBuckets + ARRAY_SIZE (m_incomeBuckets), 0u );
191+ }
141192}
142193
143194// ------------------------------------------------------------------------------------------------
@@ -156,5 +207,7 @@ void Money::parseMoneyAmount( INI *ini, void *instance, void *store, const void*
156207{
157208 // Someday, maybe, have mulitple fields like Gold:10000 Wood:1000 Tiberian:10
158209 Money * money = (Money *)store;
159- INI::parseUnsignedInt ( ini, instance, &money->m_money , userData );
210+ UnsignedInt moneyAmount;
211+ INI::parseUnsignedInt ( ini, instance, &moneyAmount, userData );
212+ money->setStartingCash (moneyAmount);
160213}
0 commit comments