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

OpenTTD Source 15.0-RC2

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
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/economy_sl_compat.h"
14
15#include "../economy_func.h"
16#include "../economy_base.h"
17
18#include "../safeguards.h"
19
21 struct PRICChunkHandler : ChunkHandler {
22 PRICChunkHandler() : ChunkHandler('PRIC', CH_READONLY) {}
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
34 struct CAPRChunkHandler : ChunkHandler {
35 CAPRChunkHandler() : ChunkHandler('CAPR', CH_READONLY) {}
36
37 void Load() const override
38 {
39 uint num_cargo = IsSavegameVersionBefore(SLV_55) ? 12 : IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO;
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
60 struct ECMYChunkHandler : ChunkHandler {
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
76 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
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[] = {
85 SLE_REF(CargoPayment, front, REF_VEHICLE),
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
91 struct CAPYChunkHandler : ChunkHandler {
92 CAPYChunkHandler() : ChunkHandler('CAPY', CH_TABLE) {}
93
94 void Save() const override
95 {
96 SlTableHeader(_cargopayment_desc);
97
98 for (CargoPayment *cp : CargoPayment::Iterate()) {
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 (CargoPaymentID(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);
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
StartupIndustryDailyChanges
void StartupIndustryDailyChanges(bool init_counter)
Initialize the variables that will maintain the daily industry change system.
Definition economy.cpp:892
economy_sl_compat.h
Loading for economy chunks before table headers were added.
_economy_sl_compat
const SaveLoadCompat _economy_sl_compat[]
Original field order for _economy_desc.
Definition economy_sl_compat.h:16
_cargopayment_sl_compat
const SaveLoadCompat _cargopayment_sl_compat[]
Original field order for _cargopayment_desc.
Definition economy_sl_compat.h:31
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition saveload.cpp:1150
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
saveload.h
Functions/types related to saving and loading games.
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition saveload.h:667
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition saveload.h:616
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLV_181
@ SLV_181
181 25012
Definition saveload.h:260
SLV_EXTEND_CARGOTYPES
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition saveload.h:282
SLV_65
@ SLV_65
65 10210
Definition saveload.h:121
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition saveload.h:332
SLV_126
@ SLV_126
126 17433
Definition saveload.h:194
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_55
@ SLV_55
55 9638
Definition saveload.h:109
SLV_102
@ SLV_102
102 14332
Definition saveload.h:165
SLV_70
@ SLV_70
70 10541
Definition saveload.h:127
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition saveload.h:474
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
CAPRChunkHandler
Cargo payment rates in pre 126 savegames.
Definition economy_sl.cpp:34
CAPRChunkHandler::Load
void Load() const override
Load the chunk.
Definition economy_sl.cpp:37
CAPYChunkHandler
Definition economy_sl.cpp:91
CAPYChunkHandler::Save
void Save() const override
Save the chunk.
Definition economy_sl.cpp:94
CAPYChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition economy_sl.cpp:116
CAPYChunkHandler::Load
void Load() const override
Load the chunk.
Definition economy_sl.cpp:104
CargoPayment
Helper class to perform the cargo payment.
Definition economy_base.h:23
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
ECMYChunkHandler
Economy variables.
Definition economy_sl.cpp:60
ECMYChunkHandler::Load
void Load() const override
Load the chunk.
Definition economy_sl.cpp:72
ECMYChunkHandler::Save
void Save() const override
Save the chunk.
Definition economy_sl.cpp:63
Economy
Data of the economy.
Definition economy_type.h:44
PRICChunkHandler
Prices in pre 126 savegames.
Definition economy_sl.cpp:21
PRICChunkHandler::Load
void Load() const override
Load the chunk.
Definition economy_sl.cpp:24
PoolID
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
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:436
SaveLoad
SaveLoad type struct.
Definition saveload.h:732