OpenTTD Source 20260711-master-g3fb3006dff
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
21struct PRICChunkHandler : ChunkHandler {
22 PRICChunkHandler() : ChunkHandler('PRIC', ChunkType::ReadOnly) {}
23
24 void Load() const override
25 {
26 /* Old games store 49 base prices, very old games store them as int32_t */
28 SlCopy(nullptr, 49, vt | VarMemType::Null);
30 }
31};
32
34struct CAPRChunkHandler : ChunkHandler {
35 CAPRChunkHandler() : ChunkHandler('CAPR', ChunkType::ReadOnly) {}
36
44};
45
46static const SaveLoad _economy_desc[] = {
53 SLE_VAR(Economy, interest_rate, VarTypes::U8),
54 SLE_VAR(Economy, infl_amount, VarTypes::U8),
55 SLE_VAR(Economy, infl_amount_pr, VarTypes::U8),
57};
58
60struct ECMYChunkHandler : ChunkHandler {
61 ECMYChunkHandler() : ChunkHandler('ECMY', ChunkType::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);
79
81 }
82};
83
84static const SaveLoad _cargopayment_desc[] = {
86 SLE_VAR(CargoPayment, route_profit, VarTypes::I64),
87 SLE_VAR(CargoPayment, visual_profit, VarTypes::I64),
89};
90
91struct CAPYChunkHandler : ChunkHandler {
92 CAPYChunkHandler() : ChunkHandler('CAPY', ChunkType::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 = CargoPayment::CreateAtIndex(CargoPaymentID(index));
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 constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition economy.cpp:879
Base classes related to the economy.
Functions related to the economy.
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.
A number of safeguards to prevent using unsafe methods.
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:734
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SaveLoadType::Variables to/from a savegame.
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
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.
@ Null
useful to write zeros in savegame.
Definition saveload.h:687
@ I64
A 64 bit signed int.
Definition saveload.h:685
VarFileType
The types/structures of data that can be stored in the file.
Definition saveload.h:657
@ I32
A 32 bit signed int.
Definition saveload.h:665
@ I64
A 64 bit signed int.
Definition saveload.h:667
@ U16
A 16 bit unsigned int.
Definition saveload.h:664
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:530
@ Vehicle
Load/save a reference to a vehicle.
Definition saveload.h:643
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:533
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:904
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
@ ExtendCargotypes
Saveload version: 199, GitHub pull request: 6802 Extend cargotypes to 64.
Definition saveload.h:282
@ UnifyCurrency
Saveload version: 65, SVN revision: 10210 Make all variables related to currency 64 bits.
Definition saveload.h:122
@ CargoReservation
Saveload version: 181, SVN revision: 25012 Persist the reservation of cargo for vehicles instead of ...
Definition saveload.h:261
@ CumulatedInflation
Saveload version: 126, SVN revision: 17433 Store cumulated inflation, and recalculate prices/payment...
Definition saveload.h:195
@ MinVersion
First savegame version.
Definition saveload.h:31
@ NewGRFCargo
Saveload version: 55, SVN revision: 9638 Increase number of cargos and NewGRF control of cargos.
Definition saveload.h:110
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:421
@ SpreadIndustryProductionChanges
Saveload version: 102, SVN revision: 14332 Spread the industry production changes over the month,...
Definition saveload.h:166
@ CargoPaymentOverflow
Saveload version: 70, SVN revision: 10541 Fix overflow of cargo payment rates, plus preparation for ...
Definition saveload.h:128
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:332
@ ReadOnly
Chunk is never saved.
Definition saveload.h:479
@ Table
An Array with a header describing the elements.
Definition saveload.h:475
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
Definition of base types and functions in a cross-platform compatible way.
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.
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< CargoPayment > Iterate(size_t from=0)
static T * CreateAtIndex(CargoPaymentID index, Targs &&... args)
SaveLoad type struct.
Definition saveload.h:787
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:754
static constexpr VarType U8
Store a 8 bits unsigned int.
Definition saveload.h:752
static constexpr VarType I16
Store a 16 bits signed int.
Definition saveload.h:753
static constexpr VarType I64
Store a 64 bits signed int.
Definition saveload.h:757
static constexpr VarType U64
Store a 64 bits unsigned int.
Definition saveload.h:758
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:756