OpenTTD Source: src/saveload/economy_sl.cpp Source File

OpenTTD Source 1.11.0-RC1

economy_sl.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../economy_func.h"
12 #include "../economy_base.h"
13
14 #include "saveload.h"
15
16 #include "../safeguards.h"
17
19 static void Load_PRIC()
20 {
21 /* Old games store 49 base prices, very old games store them as int32 */
22 int vt = IsSavegameVersionBefore(SLV_65) ? SLE_FILE_I32 : SLE_FILE_I64;
23 SlArray(nullptr, 49, vt | SLE_VAR_NULL);
24 SlArray(nullptr, 49, SLE_FILE_U16 | SLE_VAR_NULL);
25 }
26
28 static void Load_CAPR()
29 {
30 uint num_cargo = IsSavegameVersionBefore(SLV_55) ? 12 : IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO;
31 int vt = IsSavegameVersionBefore(SLV_65) ? SLE_FILE_I32 : SLE_FILE_I64;
32 SlArray(nullptr, num_cargo, vt | SLE_VAR_NULL);
33 SlArray(nullptr, num_cargo, SLE_FILE_U16 | SLE_VAR_NULL);
34 }
35
36 static const SaveLoad _economy_desc[] = {
37 SLE_CONDNULL(4, SL_MIN_VERSION, SLV_65), // max_loan
38 SLE_CONDNULL(8, SLV_65, SLV_144), // max_loan
39 SLE_CONDVAR(Economy, old_max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_65),
40 SLE_CONDVAR(Economy, old_max_loan_unround, SLE_INT64, SLV_65, SLV_126),
41 SLE_CONDVAR(Economy, old_max_loan_unround_fract, SLE_UINT16, SLV_70, SLV_126),
42 SLE_CONDVAR(Economy, inflation_prices, SLE_UINT64, SLV_126, SL_MAX_VERSION),
43 SLE_CONDVAR(Economy, inflation_payment, SLE_UINT64, SLV_126, SL_MAX_VERSION),
44 SLE_VAR(Economy, fluct, SLE_INT16),
45 SLE_VAR(Economy, interest_rate, SLE_UINT8),
46 SLE_VAR(Economy, infl_amount, SLE_UINT8),
47 SLE_VAR(Economy, infl_amount_pr, SLE_UINT8),
48 SLE_CONDVAR(Economy, industry_daily_change_counter, SLE_UINT32, SLV_102, SL_MAX_VERSION),
49 SLE_END()
50 };
51
53 static void Save_ECMY()
54 {
55 SlObject(&_economy, _economy_desc);
56 }
57
59 static void Load_ECMY()
60 {
61 SlObject(&_economy, _economy_desc);
62 StartupIndustryDailyChanges(IsSavegameVersionBefore(SLV_102)); // old savegames will need to be initialized
63 }
64
65 static const SaveLoad _cargopayment_desc[] = {
66 SLE_REF(CargoPayment, front, REF_VEHICLE),
67 SLE_VAR(CargoPayment, route_profit, SLE_INT64),
68 SLE_VAR(CargoPayment, visual_profit, SLE_INT64),
69 SLE_CONDVAR(CargoPayment, visual_transfer, SLE_INT64, SLV_181, SL_MAX_VERSION),
70 SLE_END()
71 };
72
73 static void Save_CAPY()
74 {
75 for (CargoPayment *cp : CargoPayment::Iterate()) {
76 SlSetArrayIndex(cp->index);
77 SlObject(cp, _cargopayment_desc);
78 }
79 }
80
81 static void Load_CAPY()
82 {
83 int index;
84
85 while ((index = SlIterateArray()) != -1) {
86 CargoPayment *cp = new (index) CargoPayment();
87 SlObject(cp, _cargopayment_desc);
88 }
89 }
90
91 static void Ptrs_CAPY()
92 {
93 for (CargoPayment *cp : CargoPayment::Iterate()) {
94 SlObject(cp, _cargopayment_desc);
95 }
96 }
97
98
99 extern const ChunkHandler _economy_chunk_handlers[] = {
100 { 'CAPY', Save_CAPY, Load_CAPY, Ptrs_CAPY, nullptr, CH_ARRAY},
101 { 'PRIC', nullptr, Load_PRIC, nullptr, nullptr, CH_RIFF },
102 { 'CAPR', nullptr, Load_CAPR, nullptr, nullptr, CH_RIFF },
103 { 'ECMY', Save_ECMY, Load_ECMY, nullptr, nullptr, CH_RIFF | CH_LAST},
104 };
SLV_65
@ SLV_65
65 10210
Definition: saveload.h:121
SLV_144
@ SLV_144
144 20334
Definition: saveload.h:215
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:445
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
Save_ECMY
static void Save_ECMY()
Economy variables.
Definition: economy_sl.cpp:53
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLV_102
@ SLV_102
102 14332
Definition: saveload.h:165
SLV_181
@ SLV_181
181 25012
Definition: saveload.h:260
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:629
Load_CAPR
static void Load_CAPR()
Cargo payment rates in pre 126 savegames.
Definition: economy_sl.cpp:28
SLV_55
@ SLV_55
55 9638
Definition: saveload.h:109
CargoPayment
Helper class to perform the cargo payment.
Definition: economy_base.h:24
SLV_126
@ SLV_126
126 17433
Definition: saveload.h:194
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
SLV_EXTEND_CARGOTYPES
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition: saveload.h:282
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:391
Load_PRIC
static void Load_PRIC()
Prices in pre 126 savegames.
Definition: economy_sl.cpp:19
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
StartupIndustryDailyChanges
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition: economy.cpp:893
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
Pool::PoolItem<&_cargo_payment_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
Economy
Data of the economy.
Definition: economy_type.h:28
SLV_70
@ SLV_70
70 10541
Definition: saveload.h:127
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1051
Load_ECMY
static void Load_ECMY()
Economy variables.
Definition: economy_sl.cpp:59
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631