# HG changeset patch # User celestar # Date 1173787404 0 # Node ID 059933c94c5dc4d000b7229b3eb8075c3ec48959 # Parent fbb3eab0e1862d904e94e9282eced7393b233d06 (svn r9135) [gamebalance] -Fix(r9026): Buffer overflows in multiplications of FixedT diff -r fbb3eab0e186 -r 059933c94c5d src/economy_new.h --- a/src/economy_new.h Sun Mar 11 14:02:43 2007 +0000 +++ b/src/economy_new.h Tue Mar 13 12:03:24 2007 +0000 @@ -9,30 +9,46 @@ #include "fixedt.h" // FixedT data types #include "date.h" // current year / date #include "variables.h" // difficulty +#include "functions.h" // RandomRange /** * Handles all the economic data and events */ class CEconomy { private: - FixedT m_basic_growth; ///< Basic growth number, depends solely on difficulty setting - FixedT m_activity_level; ///< Economic Activity Level, an indicator for the GDP per capita of the map + FixedT m_basic_growth; ///< Basic growth number, depends solely on difficulty setting + FixedT m_activity_level; ///< Economic Activity Level, an indicator for the GDP per capita of the map + byte m_long_term_cycle; ///< The period of the long-term cycle suggested by Kondratiev + byte m_short_term_cycle; ///< The period of the short-term cycle (see Juglar, others) + FixedT m_long_term_ampl; ///< Amplitude of the long-term cycle + FixedT m_short_term_ampl; ///< Amplitude of the short-term cycle public: /** * Starts the economy. This sets the basic growth by the difficulty level and adjust the current - * EAL by the year of the game. It also stores the world's population at the beginning of the - * game. + * EAL by the year of the game. We also set the economic cycles here. * @warning This should be run once per game only */ CEconomy() { + /* Set basic growth */ FixedT growth(_opt.diff.economic_growth, 2); - growth += 1; - m_basic_growth = growth / 100 + 1; + m_basic_growth = (growth + 1) / 100 + 1; DEBUG(eco, 3, "Starting a new economy with a basic growth factor of %.3f in the year %d", (double)m_basic_growth, _cur_year); + + /* Set up the economic cycles */ + m_long_term_cycle = 50; //RandomRange(15) + 45; + m_short_term_cycle = 10; //RandomRange(4) + 6; + m_long_term_ampl = RandomRange(5) + 10; + m_short_term_ampl = RandomRange(10) + 15; + m_long_term_ampl /= 1000; + m_short_term_ampl /= 1000; + DEBUG(eco, 4, "Adjusting economic cycles to %d and %d years", m_long_term_cycle, m_short_term_cycle); + DEBUG(eco, 4, "Adjusting economic cycles to %f and %f (amplitude)", (double)m_long_term_ampl, (double)m_short_term_ampl); + m_activity_level = 1; m_activity_level = pow(m_basic_growth, _cur_year - 1820); - DEBUG(eco, 4, "Adjusting EAL for current year (offset %d) to %.3f", _cur_year - 1820, (double)m_activity_level); + DEBUG(eco, 4, "Adjusting basic EAL for current year (offset %d) to %.3f", _cur_year - 1820, (double)m_activity_level); + } /** @@ -47,13 +63,31 @@ */ void YearlyLoop() { + FixedT growth = m_basic_growth; + growth += GetCyclicGrowth(); DEBUG(eco, 3, "Entering the yearly loop for economy"); - m_activity_level *= m_basic_growth; + m_activity_level *= growth; DEBUG(eco, 4, "Set global EAL to %.3f", (double)m_activity_level); } + /** + * Computes the modification of economic growth by cyclic events + * @return The growth modification + */ + FixedT GetCyclicGrowth() const + { + FixedT long_term (PI * 2 * (_cur_year - 1800), m_long_term_cycle); + FixedT short_term(PI * 2 * (_cur_year - 1800), m_short_term_cycle); + + //std::cout << long_term << /* ":" << short_term << */ std::endl; + //std::cout << cos(long_term) << /* ":" << cos(short_term) << */ std::endl; + + //DEBUG(eco, 5, "Cyclic Growth is %.4f", (double)(m_long_term_ampl * cos(long_term) + m_short_term_ampl * cos(short_term))); + return ( m_long_term_ampl * cos(long_term) + m_short_term_ampl * cos(short_term) ); + } }; + extern CEconomy *_eco; void InitializeEconomy(); diff -r fbb3eab0e186 -r 059933c94c5d src/fixedt.h --- a/src/fixedt.h Sun Mar 11 14:02:43 2007 +0000 +++ b/src/fixedt.h Tue Mar 13 12:03:24 2007 +0000 @@ -285,7 +285,7 @@ */ template FixedT& operator *=(const T &value) { - Raw::m_data = (Raw::m_data * (Tstorage)FixedHelperT::Raw(value)) >> FixedHelperT::dec_bits; + Raw::m_data = ((int64)Raw::m_data * (Tstorage)FixedHelperT::Raw(value)) >> FixedHelperT::dec_bits; return *this; }