OpenTTD
economy_sl.cpp
Go to the documentation of this file.
1 /* $Id: economy_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../economy_func.h"
14 #include "../economy_base.h"
15 
16 #include "saveload.h"
17 
18 #include "../safeguards.h"
19 
21 static void Load_PRIC()
22 {
23  /* Old games store 49 base prices, very old games store them as int32 */
24  int vt = IsSavegameVersionBefore(65) ? SLE_FILE_I32 : SLE_FILE_I64;
25  SlArray(NULL, 49, vt | SLE_VAR_NULL);
26  SlArray(NULL, 49, SLE_FILE_U16 | SLE_VAR_NULL);
27 }
28 
30 static void Load_CAPR()
31 {
32  uint num_cargo = IsSavegameVersionBefore(55) ? 12 : NUM_CARGO;
33  int vt = IsSavegameVersionBefore(65) ? SLE_FILE_I32 : SLE_FILE_I64;
34  SlArray(NULL, num_cargo, vt | SLE_VAR_NULL);
35  SlArray(NULL, num_cargo, SLE_FILE_U16 | SLE_VAR_NULL);
36 }
37 
38 static const SaveLoad _economy_desc[] = {
39  SLE_CONDNULL(4, 0, 64), // max_loan
40  SLE_CONDNULL(8, 65, 143), // max_loan
41  SLE_CONDVAR(Economy, old_max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
42  SLE_CONDVAR(Economy, old_max_loan_unround, SLE_INT64, 65, 125),
43  SLE_CONDVAR(Economy, old_max_loan_unround_fract, SLE_UINT16, 70, 125),
44  SLE_CONDVAR(Economy, inflation_prices, SLE_UINT64, 126, SL_MAX_VERSION),
45  SLE_CONDVAR(Economy, inflation_payment, SLE_UINT64, 126, SL_MAX_VERSION),
46  SLE_VAR(Economy, fluct, SLE_INT16),
47  SLE_VAR(Economy, interest_rate, SLE_UINT8),
48  SLE_VAR(Economy, infl_amount, SLE_UINT8),
49  SLE_VAR(Economy, infl_amount_pr, SLE_UINT8),
50  SLE_CONDVAR(Economy, industry_daily_change_counter, SLE_UINT32, 102, SL_MAX_VERSION),
51  SLE_END()
52 };
53 
55 static void Save_ECMY()
56 {
57  SlObject(&_economy, _economy_desc);
58 }
59 
61 static void Load_ECMY()
62 {
63  SlObject(&_economy, _economy_desc);
64  StartupIndustryDailyChanges(IsSavegameVersionBefore(102)); // old savegames will need to be initialized
65 }
66 
67 static const SaveLoad _cargopayment_desc[] = {
69  SLE_VAR(CargoPayment, route_profit, SLE_INT64),
70  SLE_VAR(CargoPayment, visual_profit, SLE_INT64),
71  SLE_CONDVAR(CargoPayment, visual_transfer, SLE_INT64, 181, SL_MAX_VERSION),
72  SLE_END()
73 };
74 
75 static void Save_CAPY()
76 {
77  CargoPayment *cp;
79  SlSetArrayIndex(cp->index);
80  SlObject(cp, _cargopayment_desc);
81  }
82 }
83 
84 static void Load_CAPY()
85 {
86  int index;
87 
88  while ((index = SlIterateArray()) != -1) {
89  CargoPayment *cp = new (index) CargoPayment();
90  SlObject(cp, _cargopayment_desc);
91  }
92 }
93 
94 static void Ptrs_CAPY()
95 {
96  CargoPayment *cp;
98  SlObject(cp, _cargopayment_desc);
99  }
100 }
101 
102 
103 extern const ChunkHandler _economy_chunk_handlers[] = {
104  { 'CAPY', Save_CAPY, Load_CAPY, Ptrs_CAPY, NULL, CH_ARRAY},
105  { 'PRIC', NULL, Load_PRIC, NULL, NULL, CH_RIFF | CH_AUTO_LENGTH},
106  { 'CAPR', NULL, Load_CAPR, NULL, NULL, CH_RIFF | CH_AUTO_LENGTH},
107  { 'ECMY', Save_ECMY, Load_ECMY, NULL, NULL, CH_RIFF | CH_LAST},
108 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
static void Load_ECMY()
Economy variables.
Definition: economy_sl.cpp:61
static void Load_CAPR()
Cargo payment rates in pre 126 savegames.
Definition: economy_sl.cpp:30
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:304
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
static void Save_ECMY()
Economy variables.
Definition: economy_sl.cpp:55
#define FOR_ALL_CARGO_PAYMENTS(var)
Iterate over all cargo payments.
Definition: economy_base.h:63
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1186
Functions/types related to saving and loading games.
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
Helper class to perform the cargo payment.
Definition: economy_base.h:26
Data of the economy.
Definition: economy_type.h:21
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
Load/save a reference to a vehicle.
Definition: saveload.h:82
Handlers and description of chunk.
Definition: saveload.h:66
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
useful to write zeros in savegame.
Definition: saveload.h:140
static void Load_PRIC()
Prices in pre 126 savegames.
Definition: economy_sl.cpp:21
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition: economy.cpp:909
SaveLoad type struct.
Definition: saveload.h:208
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
Last chunk in this array.
Definition: saveload.h:104