OpenTTD Source 20260731-master-g77ba2b244a
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 size_t num_prices = 49;
27 size_t record_size = (IsSavegameVersionBefore(SaveLoadVersion::UnifyCurrency) ? sizeof(uint32_t) : sizeof(uint64_t)) + sizeof(uint16_t);
28 SlSkipBytes(num_prices * record_size);
29 }
30};
31
33struct CAPRChunkHandler : ChunkHandler {
34 CAPRChunkHandler() : ChunkHandler("CAPR", ChunkType::ReadOnly) {}
35
36 void Load() const override
37 {
39 size_t record_size = sizeof(uint16_t) + (IsSavegameVersionBefore(SaveLoadVersion::UnifyCurrency) ? sizeof(uint32_t) : sizeof(uint64_t));
40 SlSkipBytes(num_cargo * record_size);
41 }
42};
43
44static const SaveLoad _economy_desc[] = {
51 SLE_VAR(Economy, interest_rate, VarTypes::U8),
52 SLE_VAR(Economy, infl_amount, VarTypes::U8),
53 SLE_VAR(Economy, infl_amount_pr, VarTypes::U8),
55};
56
58struct ECMYChunkHandler : ChunkHandler {
59 ECMYChunkHandler() : ChunkHandler("ECMY", ChunkType::Table) {}
60
61 void Save() const override
62 {
63 SlTableHeader(_economy_desc);
64
65 SlSetArrayIndex(0);
66 SlObject(&_economy, _economy_desc);
67 }
68
69
70 void Load() const override
71 {
72 const std::vector<SaveLoad> slt = SlCompatTableHeader(_economy_desc, _economy_sl_compat);
73
75 SlObject(&_economy, slt);
77
79 }
80};
81
82static const SaveLoad _cargopayment_desc[] = {
84 SLE_VAR(CargoPayment, route_profit, VarTypes::I64),
85 SLE_VAR(CargoPayment, visual_profit, VarTypes::I64),
87};
88
89struct CAPYChunkHandler : ChunkHandler {
90 CAPYChunkHandler() : ChunkHandler("CAPY", ChunkType::Table) {}
91
92 void Save() const override
93 {
94 SlTableHeader(_cargopayment_desc);
95
97 SlSetArrayIndex(cp->index);
98 SlObject(cp, _cargopayment_desc);
99 }
100 }
101
102 void Load() const override
103 {
104 const std::vector<SaveLoad> slt = SlCompatTableHeader(_cargopayment_desc, _cargopayment_sl_compat);
105
106 int index;
107
108 while ((index = SlIterateArray()) != -1) {
109 CargoPayment *cp = CargoPayment::CreateAtIndex(CargoPaymentID(index));
110 SlObject(cp, slt);
111 }
112 }
113
114 void FixPointers() const override
115 {
116 for (CargoPayment *cp : CargoPayment::Iterate()) {
117 SlObject(cp, _cargopayment_desc);
118 }
119 }
120};
121
122static const CAPYChunkHandler CAPY;
123static const PRICChunkHandler PRIC;
124static const CAPRChunkHandler CAPR;
125static const ECMYChunkHandler ECMY;
126static const ChunkHandlerRef economy_chunk_handlers[] = {
127 CAPY,
128 PRIC,
129 CAPR,
130 ECMY,
131};
132
133extern 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:747
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.
@ I64
A 64 bit signed int.
Definition saveload.h:661
@ I32
A 32 bit signed int.
Definition saveload.h:641
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:1353
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:508
@ Vehicle
Load/save a reference to a vehicle.
Definition saveload.h:618
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:511
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1024
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:894
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1268
@ ExtendCargotypes
Saveload version: 199, GitHub pull request: 6802 Extend cargotypes to 64.
Definition saveload.h:285
@ UnifyCurrency
Saveload version: 65, SVN revision: 10210 Make all variables related to currency 64 bits.
Definition saveload.h:125
@ CargoReservation
Saveload version: 181, SVN revision: 25012 Persist the reservation of cargo for vehicles instead of ...
Definition saveload.h:264
@ CumulatedInflation
Saveload version: 126, SVN revision: 17433 Store cumulated inflation, and recalculate prices/payment...
Definition saveload.h:198
@ MinVersion
First savegame version.
Definition saveload.h:34
@ NewGRFCargo
Saveload version: 55, SVN revision: 9638 Increase number of cargos and NewGRF control of cargos.
Definition saveload.h:113
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:424
@ SpreadIndustryProductionChanges
Saveload version: 102, SVN revision: 14332 Spread the industry production changes over the month,...
Definition saveload.h:169
@ CargoPaymentOverflow
Saveload version: 70, SVN revision: 10541 Fix overflow of cargo payment rates, plus preparation for ...
Definition saveload.h:131
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:335
@ ReadOnly
Chunk is never saved.
Definition saveload.h:449
@ Table
An Array with a header describing the elements.
Definition saveload.h:445
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1007
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:465
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:765
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:731
static constexpr VarType U8
Store a 8 bits unsigned int.
Definition saveload.h:729
static constexpr VarType I16
Store a 16 bits signed int.
Definition saveload.h:730
static constexpr VarType I64
Store a 64 bits signed int.
Definition saveload.h:734
static constexpr VarType U64
Store a 64 bits unsigned int.
Definition saveload.h:735
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:733