OpenTTD Source 20260917-master-gec444f7315
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#include "../vehicle_base.h"
18
19#include "../safeguards.h"
20
22struct PRICChunkHandler : ChunkHandler {
23 PRICChunkHandler() : ChunkHandler("PRIC", ChunkType::ReadOnly) {}
24
25 void Load() const override
26 {
27 size_t num_prices = 49;
28 size_t record_size = (IsSavegameVersionBefore(SaveLoadVersion::UnifyCurrency) ? sizeof(uint32_t) : sizeof(uint64_t)) + sizeof(uint16_t);
29 SlSkipBytes(num_prices * record_size);
30 }
31};
32
34struct CAPRChunkHandler : ChunkHandler {
35 CAPRChunkHandler() : ChunkHandler("CAPR", ChunkType::ReadOnly) {}
36
37 void Load() const override
38 {
40 size_t record_size = sizeof(uint16_t) + (IsSavegameVersionBefore(SaveLoadVersion::UnifyCurrency) ? sizeof(uint32_t) : sizeof(uint64_t));
41 SlSkipBytes(num_cargo * record_size);
42 }
43};
44
45static const SaveLoad _economy_desc[] = {
52 SaveLoad::Variable<VarFileType::U8>("interest_rate", SLE_OBJECT_ADDRESS(Economy, interest_rate)),
54 SaveLoad::Variable<VarFileType::U8>("infl_amount_pr", SLE_OBJECT_ADDRESS(Economy, infl_amount_pr)),
55 SaveLoad::Variable<VarFileType::U32>("industry_daily_change_counter", SLE_OBJECT_ADDRESS(Economy, industry_daily_change_counter), SaveLoadVersion::SpreadIndustryProductionChanges),
56};
57
59struct ECMYChunkHandler : ChunkHandler {
60 ECMYChunkHandler() : ChunkHandler("ECMY", ChunkType::Table) {}
61
62 void Save() const override
63 {
64 SlTableHeader(_economy_desc);
65
66 SlSetArrayIndex(0);
67 SlObject(&_economy, _economy_desc);
68 }
69
70
71 void Load() const override
72 {
73 const std::vector<SaveLoad> slt = SlCompatTableHeader(_economy_desc, _economy_sl_compat);
74
76 SlObject(&_economy, slt);
78
80 }
81};
82
83static const SaveLoad _cargopayment_desc[] = {
88};
89
90struct CAPYChunkHandler : ChunkHandler {
91 CAPYChunkHandler() : ChunkHandler("CAPY", ChunkType::Table) {}
92
93 void Save() const override
94 {
95 SlTableHeader(_cargopayment_desc);
96
98 SlSetArrayIndex(cp->index);
99 SlObject(cp, _cargopayment_desc);
100 }
101 }
102
103 void Load() const override
104 {
105 const std::vector<SaveLoad> slt = SlCompatTableHeader(_cargopayment_desc, _cargopayment_sl_compat);
106
107 int index;
108
109 while ((index = SlIterateArray()) != -1) {
110 CargoPayment *cp = CargoPayment::CreateAtIndex(CargoPaymentID(index));
111 SlObject(cp, slt);
112 }
113 }
114
115 void FixPointers() const override
116 {
117 for (CargoPayment *cp : CargoPayment::Iterate()) {
118 SlObject(cp, _cargopayment_desc);
119 }
120 }
121};
122
123static const CAPYChunkHandler CAPY;
124static const PRICChunkHandler PRIC;
125static const CAPRChunkHandler CAPR;
126static const ECMYChunkHandler ECMY;
127static const ChunkHandlerRef economy_chunk_handlers[] = {
128 CAPY,
129 PRIC,
130 CAPR,
131 ECMY,
132};
133
134extern 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:880
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:745
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.
#define SLE_OBJECT_ADDRESS(base, variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a base c...
Definition saveload.h:754
void SlSkipBytes(size_t length)
Read in bytes from the file/data structure but don't do anything with them, discarding them in effect...
Definition saveload.h:1324
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:513
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:516
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1226
@ ExtendCargotypes
Saveload version: 199, GitHub pull request: 6802 Extend cargotypes to 64.
Definition saveload.h:288
@ UnifyCurrency
Saveload version: 65, SVN revision: 10210 Make all variables related to currency 64 bits.
Definition saveload.h:128
@ CargoReservation
Saveload version: 181, SVN revision: 25012 Persist the reservation of cargo for vehicles instead of ...
Definition saveload.h:267
@ CumulatedInflation
Saveload version: 126, SVN revision: 17433 Store cumulated inflation, and recalculate prices/payment...
Definition saveload.h:201
@ MinVersion
First savegame version.
Definition saveload.h:37
@ NewGRFCargo
Saveload version: 55, SVN revision: 9638 Increase number of cargos and NewGRF control of cargos.
Definition saveload.h:116
@ SpreadIndustryProductionChanges
Saveload version: 102, SVN revision: 14332 Spread the industry production changes over the month,...
Definition saveload.h:172
@ CargoPaymentOverflow
Saveload version: 70, SVN revision: 10541 Fix overflow of cargo payment rates, plus preparation for ...
Definition saveload.h:134
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:338
@ ReadOnly
Chunk is never saved.
Definition saveload.h:454
@ Table
An Array with a header describing the elements.
Definition saveload.h:450
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.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:470
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:764
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:926
static SaveLoad Reference(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
Storage of a reference to a pool element in some savegame versions.
Definition saveload.h:951
Base class for all vehicles.