src/economy.cpp
changeset 10146 cfea97f96067
parent 10080 4ac7070a5b0b
child 10158 8f570dcb0997
equal deleted inserted replaced
10145:849ba8b8626b 10146:cfea97f96067
   784 {
   784 {
   785 	assert(price < NUM_PRICES);
   785 	assert(price < NUM_PRICES);
   786 	price_base_multiplier[price] = factor;
   786 	price_base_multiplier[price] = factor;
   787 }
   787 }
   788 
   788 
       
   789 /**
       
   790  * Initialize the variables that will maintain the daily industry change system.
       
   791  * @param init_counter specifies if the counter is required to be initialized
       
   792  */
       
   793 static void StartupIndustryDailyChanges(bool init_counter)
       
   794 {
       
   795 	uint map_size = MapLogX() + MapLogY();
       
   796 	/* After getting map size, it needs to be scaled appropriately and divided by 31,
       
   797 	 * which stands for the days in a month.
       
   798 	 * Using just 31 will make it so that a monthly reset (based on the real number of days of that month)
       
   799 	 * would not be needed.
       
   800 	 * Since it is based on "fractionnal parts", the leftover days will not make much of a difference
       
   801 	 * on the overall total number of changes performed */
       
   802 	_economy.industry_daily_increment = (1 << map_size) / 31;
       
   803 
       
   804 	if (init_counter) {
       
   805 		/* A new game or a savegame from an older version will require the counter to be initialized */
       
   806 		_economy.industry_daily_change_counter = 0;
       
   807 	}
       
   808 }
       
   809 
   789 void StartupEconomy()
   810 void StartupEconomy()
   790 {
   811 {
   791 	int i;
   812 	int i;
   792 
   813 
   793 	assert(sizeof(_price) == NUM_PRICES * sizeof(Money));
   814 	assert(sizeof(_price) == NUM_PRICES * sizeof(Money));
   814 	_economy.interest_rate = _settings_game.difficulty.initial_interest;
   835 	_economy.interest_rate = _settings_game.difficulty.initial_interest;
   815 	_economy.infl_amount = _settings_game.difficulty.initial_interest;
   836 	_economy.infl_amount = _settings_game.difficulty.initial_interest;
   816 	_economy.infl_amount_pr = max(0, _settings_game.difficulty.initial_interest - 1);
   837 	_economy.infl_amount_pr = max(0, _settings_game.difficulty.initial_interest - 1);
   817 	_economy.max_loan_unround = _economy.max_loan = _settings_game.difficulty.max_loan;
   838 	_economy.max_loan_unround = _economy.max_loan = _settings_game.difficulty.max_loan;
   818 	_economy.fluct = GB(Random(), 0, 8) + 168;
   839 	_economy.fluct = GB(Random(), 0, 8) + 168;
       
   840 
       
   841 	StartupIndustryDailyChanges(true); // As we are starting a new game, initialize the counter too
       
   842 
   819 }
   843 }
   820 
   844 
   821 void ResetEconomy()
   845 void ResetEconomy()
   822 {
   846 {
   823 	/* Test if resetting the economy is needed. */
   847 	/* Test if resetting the economy is needed. */
  1922 	SlArray(&_cargo_payment_rates,      num_cargo, vt);
  1946 	SlArray(&_cargo_payment_rates,      num_cargo, vt);
  1923 	SlArray(&_cargo_payment_rates_frac, num_cargo, SLE_UINT16);
  1947 	SlArray(&_cargo_payment_rates_frac, num_cargo, SLE_UINT16);
  1924 }
  1948 }
  1925 
  1949 
  1926 static const SaveLoad _economy_desc[] = {
  1950 static const SaveLoad _economy_desc[] = {
  1927 	SLE_CONDVAR(Economy, max_loan,         SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
  1951 	SLE_CONDVAR(Economy, max_loan,                      SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
  1928 	SLE_CONDVAR(Economy, max_loan,         SLE_INT64,                  65, SL_MAX_VERSION),
  1952 	SLE_CONDVAR(Economy, max_loan,                      SLE_INT64,                  65, SL_MAX_VERSION),
  1929 	SLE_CONDVAR(Economy, max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
  1953 	SLE_CONDVAR(Economy, max_loan_unround,              SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
  1930 	SLE_CONDVAR(Economy, max_loan_unround, SLE_INT64,                  65, SL_MAX_VERSION),
  1954 	SLE_CONDVAR(Economy, max_loan_unround,              SLE_INT64,                  65, SL_MAX_VERSION),
  1931 	SLE_CONDVAR(Economy, max_loan_unround_fract, SLE_UINT16,           70, SL_MAX_VERSION),
  1955 	SLE_CONDVAR(Economy, max_loan_unround_fract,        SLE_UINT16,                 70, SL_MAX_VERSION),
  1932 	    SLE_VAR(Economy, fluct,            SLE_INT16),
  1956 	    SLE_VAR(Economy, fluct,                         SLE_INT16),
  1933 	    SLE_VAR(Economy, interest_rate,    SLE_UINT8),
  1957 	    SLE_VAR(Economy, interest_rate,                 SLE_UINT8),
  1934 	    SLE_VAR(Economy, infl_amount,      SLE_UINT8),
  1958 	    SLE_VAR(Economy, infl_amount,                   SLE_UINT8),
  1935 	    SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),
  1959 	    SLE_VAR(Economy, infl_amount_pr,                SLE_UINT8),
       
  1960 	SLE_CONDVAR(Economy, industry_daily_change_counter, SLE_UINT32,                102, SL_MAX_VERSION),
  1936 	    SLE_END()
  1961 	    SLE_END()
  1937 };
  1962 };
  1938 
  1963 
  1939 /** Economy variables */
  1964 /** Economy variables */
  1940 static void SaveLoad_ECMY()
  1965 static void Save_ECMY()
  1941 {
  1966 {
  1942 	SlObject(&_economy, _economy_desc);
  1967 	SlObject(&_economy, _economy_desc);
       
  1968 }
       
  1969 
       
  1970 /** Economy variables */
       
  1971 static void Load_ECMY()
       
  1972 {
       
  1973 	SlObject(&_economy, _economy_desc);
       
  1974 	StartupIndustryDailyChanges(CheckSavegameVersion(102));  // old savegames will need to be initialized
  1943 }
  1975 }
  1944 
  1976 
  1945 extern const ChunkHandler _economy_chunk_handlers[] = {
  1977 extern const ChunkHandler _economy_chunk_handlers[] = {
  1946 	{ 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH},
  1978 	{ 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH},
  1947 	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
  1979 	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
  1948 	{ 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
  1980 	{ 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
  1949 	{ 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
  1981 	{ 'ECMY', Save_ECMY,     Load_ECMY,     CH_RIFF | CH_LAST},
  1950 };
  1982 };