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[] = {
68 SLE_REF(CargoPayment, front, REF_VEHICLE),
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;
78 FOR_ALL_CARGO_PAYMENTS(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;
97 FOR_ALL_CARGO_PAYMENTS(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 };
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
Load_ECMY
static void Load_ECMY()
Economy variables.
Definition: economy_sl.cpp:61
Load_CAPR
static void Load_CAPR()
Cargo payment rates in pre 126 savegames.
Definition: economy_sl.cpp:30
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:304
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Save_ECMY
static void Save_ECMY()
Economy variables.
Definition: economy_sl.cpp:55
FOR_ALL_CARGO_PAYMENTS
#define FOR_ALL_CARGO_PAYMENTS(var)
Iterate over all cargo payments.
Definition: economy_base.h:63
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1186
saveload.h
Functions/types related to saving and loading games.
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
CargoPayment
Helper class to perform the cargo payment.
Definition: economy_base.h:26
Economy
Data of the economy.
Definition: economy_type.h:21
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:82
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:140
Load_PRIC
static void Load_PRIC()
Prices in pre 126 savegames.
Definition: economy_sl.cpp:21
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
StartupIndustryDailyChanges
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition: economy.cpp:909
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
CH_LAST
Last chunk in this array.
Definition: saveload.h:104