7 |
7 |
8 #include "debug.h" // debug outputs |
8 #include "debug.h" // debug outputs |
9 #include "fixedt.h" // FixedT data types |
9 #include "fixedt.h" // FixedT data types |
10 #include "date.h" // current year / date |
10 #include "date.h" // current year / date |
11 #include "variables.h" // difficulty |
11 #include "variables.h" // difficulty |
|
12 #include "functions.h" // RandomRange |
12 |
13 |
13 /** |
14 /** |
14 * Handles all the economic data and events |
15 * Handles all the economic data and events |
15 */ |
16 */ |
16 class CEconomy { |
17 class CEconomy { |
17 private: |
18 private: |
18 FixedT<int32, 16> m_basic_growth; ///< Basic growth number, depends solely on difficulty setting |
19 FixedT<int32, 16> m_basic_growth; ///< Basic growth number, depends solely on difficulty setting |
19 FixedT<int64, 16> m_activity_level; ///< Economic Activity Level, an indicator for the GDP per capita of the map |
20 FixedT<int64, 16> m_activity_level; ///< Economic Activity Level, an indicator for the GDP per capita of the map |
|
21 byte m_long_term_cycle; ///< The period of the long-term cycle suggested by Kondratiev |
|
22 byte m_short_term_cycle; ///< The period of the short-term cycle (see Juglar, others) |
|
23 FixedT<int32, 16> m_long_term_ampl; ///< Amplitude of the long-term cycle |
|
24 FixedT<int32, 16> m_short_term_ampl; ///< Amplitude of the short-term cycle |
20 |
25 |
21 public: |
26 public: |
22 /** |
27 /** |
23 * Starts the economy. This sets the basic growth by the difficulty level and adjust the current |
28 * Starts the economy. This sets the basic growth by the difficulty level and adjust the current |
24 * EAL by the year of the game. It also stores the world's population at the beginning of the |
29 * EAL by the year of the game. We also set the economic cycles here. |
25 * game. |
|
26 * @warning This should be run once per game only |
30 * @warning This should be run once per game only |
27 */ |
31 */ |
28 CEconomy() { |
32 CEconomy() { |
|
33 /* Set basic growth */ |
29 FixedT<int32, 16> growth(_opt.diff.economic_growth, 2); |
34 FixedT<int32, 16> growth(_opt.diff.economic_growth, 2); |
30 growth += 1; |
35 m_basic_growth = (growth + 1) / 100 + 1; |
31 m_basic_growth = growth / 100 + 1; |
|
32 DEBUG(eco, 3, "Starting a new economy with a basic growth factor of %.3f in the year %d", (double)m_basic_growth, _cur_year); |
36 DEBUG(eco, 3, "Starting a new economy with a basic growth factor of %.3f in the year %d", (double)m_basic_growth, _cur_year); |
|
37 |
|
38 /* Set up the economic cycles */ |
|
39 m_long_term_cycle = RandomRange(15) + 45; |
|
40 m_short_term_cycle = RandomRange(4) + 6; |
|
41 m_long_term_ampl = RandomRange(5) + 10; |
|
42 m_short_term_ampl = RandomRange(10) + 15; |
|
43 m_long_term_ampl /= 1000; |
|
44 m_short_term_ampl /= 1000; |
|
45 DEBUG(eco, 4, "Adjusting economic cycles to %d and %d years", m_long_term_cycle, m_short_term_cycle); |
|
46 DEBUG(eco, 4, "Adjusting economic cycles to %f and %f (amplitude)", (double)m_long_term_ampl, (double)m_short_term_ampl); |
|
47 |
33 m_activity_level = 1; |
48 m_activity_level = 1; |
34 m_activity_level = pow(m_basic_growth, _cur_year - 1820); |
49 m_activity_level = pow(m_basic_growth, _cur_year - 1820); |
35 DEBUG(eco, 4, "Adjusting EAL for current year (offset %d) to %.3f", _cur_year - 1820, (double)m_activity_level); |
50 DEBUG(eco, 4, "Adjusting basic EAL for current year (offset %d) to %.3f", _cur_year - 1820, (double)m_activity_level); |
|
51 |
36 } |
52 } |
37 |
53 |
38 /** |
54 /** |
39 * Removes an economy. |
55 * Removes an economy. |
40 */ |
56 */ |
45 /** |
61 /** |
46 * Adjusts the economic settings on a yearly level. |
62 * Adjusts the economic settings on a yearly level. |
47 */ |
63 */ |
48 void YearlyLoop() |
64 void YearlyLoop() |
49 { |
65 { |
|
66 FixedT<int32, 16> growth = m_basic_growth; |
|
67 growth += GetCyclicGrowth(); |
50 DEBUG(eco, 3, "Entering the yearly loop for economy"); |
68 DEBUG(eco, 3, "Entering the yearly loop for economy"); |
51 m_activity_level *= m_basic_growth; |
69 m_activity_level *= growth; |
52 DEBUG(eco, 4, "Set global EAL to %.3f", (double)m_activity_level); |
70 DEBUG(eco, 4, "Set global EAL to %.3f", (double)m_activity_level); |
53 } |
71 } |
54 |
72 |
|
73 /** |
|
74 * Computes the modification of economic growth by cyclic events |
|
75 * @return The growth modification |
|
76 */ |
|
77 FixedT<int32, 16> GetCyclicGrowth() const |
|
78 { |
|
79 FixedT<int32, 16> long_term (PI * 2 * (_cur_year - 1800), m_long_term_cycle); |
|
80 FixedT<int32, 16> short_term(PI * 2 * (_cur_year - 1800), m_short_term_cycle); |
|
81 |
|
82 DEBUG(eco, 5, "Cyclic Growth is %.4f", (double)(m_long_term_ampl * cos(long_term) + m_short_term_ampl * cos(short_term))); |
|
83 return ( m_long_term_ampl * cos(long_term) + m_short_term_ampl * cos(short_term) ); |
|
84 } |
55 }; |
85 }; |
|
86 |
56 |
87 |
57 extern CEconomy *_eco; |
88 extern CEconomy *_eco; |
58 void InitializeEconomy(); |
89 void InitializeEconomy(); |
59 |
90 |
60 #endif /* ECONOMY_NEW_H */ |
91 #endif /* ECONOMY_NEW_H */ |