(svn r9135) [gamebalance] -Fix(r9026): Buffer overflows in multiplications of FixedT gamebalance
authorcelestar
Tue, 13 Mar 2007 12:03:24 +0000
branchgamebalance
changeset 9882 059933c94c5d
parent 9881 fbb3eab0e186
child 9883 ac3e1a8434b7
(svn r9135) [gamebalance] -Fix(r9026): Buffer overflows in multiplications of FixedT
src/economy_new.h
src/fixedt.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<int32, 16> m_basic_growth;    ///< Basic growth number, depends solely on difficulty setting
-	FixedT<int64, 16> m_activity_level;  ///< Economic Activity Level, an indicator for the GDP per capita of the map
+	FixedT<int32, 16> m_basic_growth;     ///< Basic growth number, depends solely on difficulty setting
+	FixedT<int64, 16> 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<int32, 16> m_long_term_ampl;   ///< Amplitude of the long-term cycle
+	FixedT<int32, 16> 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<int32, 16> 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<int32, 16> 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<int32, 16> GetCyclicGrowth() const
+	{
+		FixedT<int32, 16> long_term (PI * 2 * (_cur_year - 1800), m_long_term_cycle);
+		FixedT<int32, 16> 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();
 
--- 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 <typename T> FixedT& operator *=(const T &value)
 	{
-		Raw::m_data = (Raw::m_data * (Tstorage)FixedHelperT<T>::Raw(value)) >> FixedHelperT<T>::dec_bits;
+		Raw::m_data = ((int64)Raw::m_data * (Tstorage)FixedHelperT<T>::Raw(value)) >> FixedHelperT<T>::dec_bits;
 		return *this;
 	}