OpenTTD Source  20240919-master-gdf0233f4c2
subsidy_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 "../subsidy_base.h"
16 
17 #include "../safeguards.h"
18 
19 static const SaveLoad _subsidies_desc[] = {
20  SLE_VAR(Subsidy, cargo_type, SLE_UINT8),
21  SLE_CONDVAR(Subsidy, remaining, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_CUSTOM_SUBSIDY_DURATION),
23  SLE_CONDVAR(Subsidy, awarded, SLE_UINT8, SLV_125, SL_MAX_VERSION),
24  SLE_CONDVAR(Subsidy, src_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
25  SLE_CONDVAR(Subsidy, dst_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
26  SLE_CONDVAR(Subsidy, src, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
27  SLE_CONDVAR(Subsidy, src, SLE_UINT16, SLV_5, SL_MAX_VERSION),
28  SLE_CONDVAR(Subsidy, dst, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
29  SLE_CONDVAR(Subsidy, dst, SLE_UINT16, SLV_5, SL_MAX_VERSION),
30 };
31 
33  SUBSChunkHandler() : ChunkHandler('SUBS', CH_TABLE) {}
34 
35  void Save() const override
36  {
37  SlTableHeader(_subsidies_desc);
38 
39  for (Subsidy *s : Subsidy::Iterate()) {
40  SlSetArrayIndex(s->index);
41  SlObject(s, _subsidies_desc);
42  }
43  }
44 
45  void Load() const override
46  {
47  const std::vector<SaveLoad> slt = SlCompatTableHeader(_subsidies_desc, _subsidies_sl_compat);
48 
49  int index;
50  while ((index = SlIterateArray()) != -1) {
51  Subsidy *s = new (index) Subsidy();
52  SlObject(s, slt);
53  }
54  }
55 };
56 
57 static const SUBSChunkHandler SUBS;
58 static const ChunkHandlerRef subsidy_chunk_handlers[] = {
59  SUBS,
60 };
61 
62 extern const ChunkHandlerTable _subsidy_chunk_handlers(subsidy_chunk_handlers);
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:501
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:857
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
SLV_CUSTOM_SUBSIDY_DURATION
@ SLV_CUSTOM_SUBSIDY_DURATION
292 PR#9081 Configurable subsidy duration.
Definition: saveload.h:330
Subsidy
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
SLV_125
@ SLV_125
125 17113
Definition: saveload.h:193
SUBSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: subsidy_sl.cpp:35
SUBSChunkHandler
Definition: subsidy_sl.cpp:32
subsidy_sl_compat.h
_subsidies_sl_compat
const SaveLoadCompat _subsidies_sl_compat[]
Original field order for _subsidies_desc.
Definition: subsidy_sl_compat.h:16
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:391
SUBSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: subsidy_sl.cpp:45
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:971
Pool::PoolItem<&_subsidy_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
SaveLoad
SaveLoad type struct.
Definition: saveload.h:707
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658