OpenTTD Source 20241224-master-gf74b0cf984
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
12#include "saveload.h"
14
15#include "../economy_func.h"
16#include "../economy_base.h"
17
18#include "../safeguards.h"
19
23
24 void Load() const override
25 {
26 /* Old games store 49 base prices, very old games store them as int32_t */
27 int vt = IsSavegameVersionBefore(SLV_65) ? SLE_FILE_I32 : SLE_FILE_I64;
28 SlCopy(nullptr, 49, vt | SLE_VAR_NULL);
29 SlCopy(nullptr, 49, SLE_FILE_U16 | SLE_VAR_NULL);
30 }
31};
32
36
37 void Load() const override
38 {
40 int vt = IsSavegameVersionBefore(SLV_65) ? SLE_FILE_I32 : SLE_FILE_I64;
41 SlCopy(nullptr, num_cargo, vt | SLE_VAR_NULL);
42 SlCopy(nullptr, num_cargo, SLE_FILE_U16 | SLE_VAR_NULL);
43 }
44};
45
46static const SaveLoad _economy_desc[] = {
47 SLE_CONDVAR(Economy, old_max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_65),
48 SLE_CONDVAR(Economy, old_max_loan_unround, SLE_INT64, SLV_65, SLV_126),
49 SLE_CONDVAR(Economy, old_max_loan_unround_fract, SLE_UINT16, SLV_70, SLV_126),
50 SLE_CONDVAR(Economy, inflation_prices, SLE_UINT64, SLV_126, SL_MAX_VERSION),
51 SLE_CONDVAR(Economy, inflation_payment, SLE_UINT64, SLV_126, SL_MAX_VERSION),
52 SLE_VAR(Economy, fluct, SLE_INT16),
53 SLE_VAR(Economy, interest_rate, SLE_UINT8),
54 SLE_VAR(Economy, infl_amount, SLE_UINT8),
55 SLE_VAR(Economy, infl_amount_pr, SLE_UINT8),
56 SLE_CONDVAR(Economy, industry_daily_change_counter, SLE_UINT32, SLV_102, SL_MAX_VERSION),
57};
58
61 ECMYChunkHandler() : ChunkHandler('ECMY', CH_TABLE) {}
62
63 void Save() const override
64 {
65 SlTableHeader(_economy_desc);
66
67 SlSetArrayIndex(0);
68 SlObject(&_economy, _economy_desc);
69 }
70
71
72 void Load() const override
73 {
74 const std::vector<SaveLoad> slt = SlCompatTableHeader(_economy_desc, _economy_sl_compat);
75
77 SlObject(&_economy, slt);
78 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many ECMY entries");
79
80 StartupIndustryDailyChanges(IsSavegameVersionBefore(SLV_102)); // old savegames will need to be initialized
81 }
82};
83
84static const SaveLoad _cargopayment_desc[] = {
86 SLE_VAR(CargoPayment, route_profit, SLE_INT64),
87 SLE_VAR(CargoPayment, visual_profit, SLE_INT64),
88 SLE_CONDVAR(CargoPayment, visual_transfer, SLE_INT64, SLV_181, SL_MAX_VERSION),
89};
90
92 CAPYChunkHandler() : ChunkHandler('CAPY', CH_TABLE) {}
93
94 void Save() const override
95 {
96 SlTableHeader(_cargopayment_desc);
97
99 SlSetArrayIndex(cp->index);
100 SlObject(cp, _cargopayment_desc);
101 }
102 }
103
104 void Load() const override
105 {
106 const std::vector<SaveLoad> slt = SlCompatTableHeader(_cargopayment_desc, _cargopayment_sl_compat);
107
108 int index;
109
110 while ((index = SlIterateArray()) != -1) {
111 CargoPayment *cp = new (index) CargoPayment();
112 SlObject(cp, slt);
113 }
114 }
115
116 void FixPointers() const override
117 {
118 for (CargoPayment *cp : CargoPayment::Iterate()) {
119 SlObject(cp, _cargopayment_desc);
120 }
121 }
122};
123
124static const CAPYChunkHandler CAPY;
125static const PRICChunkHandler PRIC;
126static const CAPRChunkHandler CAPR;
127static const ECMYChunkHandler ECMY;
128static const ChunkHandlerRef economy_chunk_handlers[] = {
129 CAPY,
130 PRIC,
131 CAPR,
132 ECMY,
133};
134
135extern const ChunkHandlerTable _economy_chunk_handlers(economy_chunk_handlers);
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition economy.cpp:909
Loading for economy chunks before table headers were added.
const SaveLoadCompat _economy_sl_compat[]
Original field order for _economy_desc.
const SaveLoadCompat _cargopayment_sl_compat[]
Original field order for _cargopayment_desc.
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:659
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:351
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Functions/types related to saving and loading games.
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition saveload.h:653
@ CH_READONLY
Chunk is never saved.
Definition saveload.h:459
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:509
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:512
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1019
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:868
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1263
@ SLV_181
181 25012
Definition saveload.h:260
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition saveload.h:282
@ SLV_65
65 10210
Definition saveload.h:121
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition saveload.h:332
@ SLV_126
126 17433
Definition saveload.h:194
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:399
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
@ SLV_55
55 9638
Definition saveload.h:109
@ SLV_102
102 14332
Definition saveload.h:165
@ SLV_70
70 10541
Definition saveload.h:127
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition saveload.h:602
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1002
Cargo payment rates in pre 126 savegames.
void Load() const override
Load the chunk.
void Save() const override
Save the chunk.
void FixPointers() const override
Fix the pointers.
void Load() const override
Load the chunk.
Helper class to perform the cargo payment.
Handlers and description of chunk.
Definition saveload.h:463
Economy variables.
void Load() const override
Load the chunk.
void Save() const override
Save the chunk.
Data of the economy.
Prices in pre 126 savegames.
void Load() const override
Load the chunk.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
SaveLoad type struct.
Definition saveload.h:717