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,9 +89,43 @@ 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
101+ // ------------------------------------------------------------------------------------------------
102+ void Money::setStartingCash (UnsignedInt amount)
103+ {
104+ m_money = amount;
105+ std::fill (m_incomeBuckets, m_incomeBuckets + ARRAY_SIZE (m_incomeBuckets), 0u );
106+ m_currentBucket = 0u ;
107+ m_cashPerMinute = 0u ;
108+ }
109+
110+ // ------------------------------------------------------------------------------------------------
111+ void Money::updateIncomeBucket ()
112+ {
113+ UnsignedInt frame = TheGameLogic->getFrame ();
114+ UnsignedInt nextBucket = (frame / LOGICFRAMES_PER_SECOND) % ARRAY_SIZE (m_incomeBuckets);
115+ if (nextBucket != m_currentBucket)
116+ {
117+ m_cashPerMinute -= m_incomeBuckets[nextBucket];
118+ m_currentBucket = nextBucket;
119+ m_incomeBuckets[m_currentBucket] = 0u ;
120+ }
121+ }
122+
123+ // ------------------------------------------------------------------------------------------------
124+ UnsignedInt Money::getCashPerMinute () const
125+ {
126+ return m_cashPerMinute;
127+ }
128+
94129void Money::triggerAudioEvent (const AudioEventRTS& audioEvent)
95130{
96131 Real volume = TheAudio->getAudioSettings ()->m_preferredMoneyTransactionVolume ;
@@ -116,19 +151,35 @@ void Money::crc( Xfer *xfer )
116151// ------------------------------------------------------------------------------------------------
117152/* * Xfer method
118153 * Version Info:
119- * 1: Initial version */
154+ * 1: Initial version
155+ * 2: Add saveload support for the cash per minute income tracking */
120156// ------------------------------------------------------------------------------------------------
121157void Money::xfer ( Xfer *xfer )
122158{
123159
124160 // version
161+ #if RETAIL_COMPATIBLE_XFER_SAVE
125162 XferVersion currentVersion = 1 ;
163+ #else
164+ XferVersion currentVersion = 2 ;
165+ #endif
126166 XferVersion version = currentVersion;
127167 xfer->xferVersion ( &version, currentVersion );
128168
129169 // money value
130170 xfer->xferUnsignedInt ( &m_money );
131171
172+ if (version <= 1 )
173+ {
174+ setStartingCash (m_money);
175+ }
176+ else
177+ {
178+ xfer->xferUser (m_incomeBuckets, sizeof (m_incomeBuckets));
179+ xfer->xferUnsignedInt (&m_currentBucket);
180+
181+ m_cashPerMinute = std::accumulate (m_incomeBuckets, m_incomeBuckets + ARRAY_SIZE (m_incomeBuckets), 0u );
182+ }
132183}
133184
134185// ------------------------------------------------------------------------------------------------
@@ -147,5 +198,7 @@ void Money::parseMoneyAmount( INI *ini, void *instance, void *store, const void*
147198{
148199 // Someday, maybe, have mulitple fields like Gold:10000 Wood:1000 Tiberian:10
149200 Money * money = (Money *)store;
150- INI::parseUnsignedInt ( ini, instance, &money->m_money , userData );
201+ UnsignedInt moneyAmount;
202+ INI::parseUnsignedInt ( ini, instance, &moneyAmount, userData );
203+ money->setStartingCash (moneyAmount);
151204}
0 commit comments